Skip to content
This repository was archived by the owner on Jul 1, 2025. It is now read-only.
This repository was archived by the owner on Jul 1, 2025. It is now read-only.

Teach Glow how to support custom alginment requirements for tensor dimensions #2686

@opti-mix

Description

@opti-mix

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:

  1. Glow Tensors could be extended to explicitly support custom strides. Type would have a dedicated strides_ member in addition to sizes_.

    • 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.
  2. It is also possible to try tweaking the representation of tensors only in the specific backends that need it, without changing the Tensor and Type classes. 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.
  1. It is also possible to use just the usual Tensor and Type classes. 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"

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions