-
-
Notifications
You must be signed in to change notification settings - Fork 52
Numpy HPy migration notes: blockers and concerns
Stepan Sindelar edited this page Jul 13, 2022
·
7 revisions
- NumPy uses the
METH_FASTCALL | METH_KEYWORDS
convention and has its own argument parser for that- this could be solved by HPy support for various calling conventions (~arg clinic): https://github.com/hpyproject/hpy/issues/129
- metaclass support for heap types is missing in CPython GitHub issue for this
- meanwhile added to CPython, there is a PR for HPy including older CPython versions support: https://github.com/hpyproject/hpy/pull/335
-
tp_vectorcall
is not supposed to be used for heap types - NumPy accesses
tp_
slots directly. Edit: not an issue since NumPy is moving away from that.-
to compare them (We could provide
bool HPyType_CheckSlot(HPyContext*, HPy, HPyDef expected)
) - to read things like
tp_name
,tp_base
,tp_dict
- for fast paths bypassing some CPython logic (e.g.: getting attribute without raising exception if missing)
-
to compare them (We could provide
Migration path concerns:
- NumPy API: expose second capsule and header(s) with HPy based APIs?
- legacy NumPy APIs would eventually delegate to the HPy versions
- opportunity to get rid of legacy NumPy APIs/do some NumPy API cleanup
- global caches
- there are some global (as in C level global variables) caches, those need some indirection, e.g.: store them in capsule accessed via
HPyGlobal
, but then the cost of loading theHPy
fromHPyGlobal
and loading of the capsule contents (2 HPy API calls) may spoil the caching benefit altogether - can be solved by module state and "arg clinic" which would directly pass the module state as an argument
- there are some global (as in C level global variables) caches, those need some indirection, e.g.: store them in capsule accessed via
Architecture/code style concerns:
-
PyArrayObject*
->HPy
removes type information and type checking- Numpy uses the struct types in many helper/infrastructure functions, changing all those to
HPy
removes the type information, which makes the code less pleasant to work with and more error prone - Sometimes it is desirable to pass around additional argument for
PyArrayObject*
alongside theHPy
handle if the struct was already retrieved - this is cumbersome
- Numpy uses the struct types in many helper/infrastructure functions, changing all those to
- Some ideas:
- generate additional struct that holds both the handle and the struct and helper methods for it, e.g.,
typedef struct { HPy handle; PyArrayObject *data; } HPyArray;
- generate additional struct to wrap just the handle to "attach" type information to it + generate conversion helpers
- depending on whether we see similar patters in other packages, this could be just infrastructure in numpy port codebase or provided by HPy
- alternatively: always pass two arguments for everything, e.g.:
foo(HPyContext *ctx, HPy h_arr, PyArrayObject *arr)
, wherearr
can beNULL
and the callee will be responsible to lazily initialize it before using it, we can have some convenience macros for that - the "arg clinic" can pass the struct as an argument (https://github.com/hpyproject/hpy/issues/129), which would have also performance benefits, but does not solve the question of how to pass that around internal Numpy helper functions
- generate additional struct that holds both the handle and the struct and helper methods for it, e.g.,
- 5 September 2024
- 4 April 2024
- 7 March 2024
- 1 February 2024
- 11 January 2024
- 7 December 2023
- 9 November 2023
- 5 October 2023
- 14 September 2023
- 3 August 2023
- 6 July 2023
- 1 June 2023
- 4 May 2023
- 13 April 2023
- 2 March 2023
- 2 February 2023
- 12 January 2023
- 1 December 2022
- 3 November 2022
- 6 October 2022
- 8 September 2022
- 4 August 2022
- 7 July 2022
- 2 June 2022
- 5 May 2022
- 7 April 2022
- 3 March 2022
- 3 February 2022
- 13 January 2022
- 2 December 2021
- 4 November 2021
- 7 October 2021
- 2 September 2021
- 12 August 2021
- 8 July 2021
- 6 May 2021
- 4 March 2021
- 7 January 2021
- 3 December 2020
- 5 November 2020