ENH: Help with custom alignments for (structured) dtypes
It has come up more often recently (also CC @leofang, but I remember another issue that I think is probably closed).
In practice, it is becoming relatively common for users to have custom alignment needs that fit a structured dtype relatively well. I.e. in C++ one would use a:
struct {
int a;
struct alignas(8) {
int4_t b;
int4_t c;
};
}because later compute kernels require that alignment. NumPy can handle this type of dtype by using custom offsets=[] when creating the dtype.
The issue is that there are two notions of "alignment" here:
- The user desired one needed for a specific compute kernel, that NumPy may or may not be able to (or even need to) ensure.
- One used by NumPy (not often for structured dtypes, since we don't do much with them). This is used to decide for example whether we need to copy an array before running a ufunc on it.
So allowing users to just set/change the dtype alignment may currently lead to awkward copies at least.
Solutions: I can see three things that we can do (which are not necessary exclusive)
- Add a helper to
np.dtypes.<...>to construct a properly aligned structured dtype (or make a dtype properly aligned). (could be part ofnp.dtype, but a helper seems easier). - Just allow setting the alignment at least of structured dtype. Either we live with the two concepts colliding, or we ask for
custom_alignment <= max_alignment. If the use-cases are mostlycustom_alignment <= max_alignment, I actually don't see a problem with this. - We could actually have a second, custom, alignment that can be larger and is enforced only while constructing a new structured dtype (instead of the CPU alignment need). This would mainly allow nesting and indexing have the custom alignment survive. It would be very targeted to structured dtypes, I think (i.e. other dtypes don't carry a custom alignment).
Some form of helper may make sense either way, I think. All of these seem pretty reasonable to me (2. without custom_alignment <= max_alignment may be a bit "I know what I am doing").
Source: numpy/numpy