-
Notifications
You must be signed in to change notification settings - Fork 701
Teach Glow how to support custom alginment requirements for tensor dimensions #2686
Description
Some backends may have specific requirements on the alignment of data inside tensors. E.g. each row of the innermost dimension to be cache line size aligned. In principle, any of the dimensions may have an alignment requirement.
Glow's Tensor class as of today is not able to represent this requirements, because they underlying Type cannot express it. Type only stores the actual dimensions, but not the aligned dimensions.
So, how could one use Glow Tensors to enforce any requirements on alignments?
Here are some possible alternatives:
-
Glow Tensors could be extended to explicitly support custom strides.
Typewould have a dedicatedstrides_member in addition tosizes_.- This change could be useful for different backends.
- Any code that accesses tensor elements only via logical indices and never through raw pointers should continue to work correctly.
- But it may require a lot of changes all over the place, especially in libjit and some kernels and operations that iterate over the content of a tensor using raw pointers.
- Everything that uses row pointers would start iterating over "padding" elements. These elementds may contain junk data and thus all such places should be updated to skip such elements.
- libjit kernels should get additional parameters for strides, so that they can correctly compute linear indices into tensors.
-
It is also possible to try tweaking the representation of tensors only in the specific backends that need it, without changing the
TensorandTypeclasses. E.g. one could introduce custom backend-specific Tensor classes. This is certainly doable, but it is very likely that this logic will be re-implemented in a number of different backends.
- The disadvantages of this approach are:
- it may require more memory to keep both the original and backend specific tensors.
- it may involve a lot of copying between padded and non-padded tensors
- none of the existing kernels can be re-used, because they work only with Glow's standard tensors.
- It is also possible to use just the usual
TensorandTypeclasses. Every time when a custom-aligned tensor is needed, one would represent it e.g. as a Tensor with bigger dimensions that accommodate for the required alignments and contains some junk padding elements. Such a tensor would have a desired memory layout. But it is important to note that some of the existing operations on tensors would not produce correct results when applied to this tensor, because all those operations assume that all elements contain real data and need to be processed, which is not the case due to the added junk padding elements. The content of the original tensor would be copied into the "padded" tensor before the operation that makes use of custom alignments and would be copied back into the original tensor after that operation.- The disadvantages of this approach are:
- it may require more memory to keep both the original and the padded tensor.
- it may involve a lot of copying between padded and non-padded tensors
- Some of the existing kernels/operations cannot be re-used with the "padded" tensors, because they would try to process "padding" elements. One would need to develop new kernels for "padded kernels"
- The disadvantages of this approach are:
I'm probably missing some further alternatives here.
It would be interesting to hear from those who faced a similar issue what was their experience and how they solved this problem. May be we can learn from it.
Personally, I tend to think that (1) is the cleanest solution, but I'm afraid it may involve a lot of re-factoring in existing kernels.
Note: This issue is related to #1214.