Skip to content

Fuse operations with torch_index_select op (aka gather op) #3562

Closed

Description

@hanhanW

Current status

The op lives in its own dispatch function, and we lower it to indexed_generic op on buffers.

Planning

A gather op is a pointer math. We don't have concrete plan for this op now. A blocker for more general solution could be to support "symbolic expressions" in Linalg ops' indexing maps. To get a more concrete idea, we can support this incrementally.

In short term, we can fuse it with its consumers, using lower the op to indexed_generic op on tensors. One constraint is that we need a buffer/memref for the "lookup table" in IREE. This can be done by making the op be the root op of a dispatch function.

To fuse it with its producer, there are more things to investigate and prototype. One idea is to have a kernel to fetch all inputs (e.g., the lookup table) into workgroup local memory before running the loop consuming it. Fuse the fetch loop and and actual operations on the fetched data (ie gather op) when possible, eg, we already have a global buffer for the lookup table.

The plan is to support to fuse it with its consumers, and then we can prototype more on the fusion with its producers.

Activity

  1. added
    codegen
    on Oct 21, 2020
  2. self-assigned this
    on Oct 21, 2020
  3. benvanik commented on Oct 21, 2020

    @benvanik
    Collaborator

    An idea is that we could look at some of the uses and see if we can turn them into flow.tensor.slice ops. Specifically we'd want to look for uses where the index being selected is a value that originates on the host like an index argument to a function. Using flow.tensor.slice in those cases will allow for much better buffer allocation behavior.

    This won't be a good idea if the indices are computed on the device (such as the result of a topk), as then it would require a readback. But I suspect with something like #3139 we'd be able to end up with a lot of cases where the index (loop iterators, etc) is knowable on the host side.

  4. hanhanW commented on Oct 21, 2020

    @hanhanW
    ContributorAuthor

    Good point, I believe this can be done for some of gather ops. The indices are depending on the shape of the input, only one index is related to anther input (which is called indices in TF).

    The current codegen pipeline lowers the load to something like

    %[[VAL2:.+]] = load %[[INPUT]][%[[I]], %[[J]], %[[CAST]], %[[L]]] : memref<4x7x8x2xf32>
    

    The I, J, L are known on host, and the order is also known on host (which depends on attribute batch_dim, and axis). The CAST variable is derived from another input indices, this is where the indirect access happens. So it is totally fair to turn it into flow.tensor.slice and we won't need to compute the indices on device side, ie, no indexed_generic op anymore.

    Another idea is to transpose the input, so we can make the random access dim to the last dim. In this case, we only need a much smaller local memory for the lookup table and make it more streamable.

  5. benvanik commented on Oct 21, 2020

    @benvanik
    Collaborator

    Good idea - doing that early on (HLO) would let us fuse the transpose into a producer, too! These are mostly optimizations, and it's probably always going to be required that we at least support the fallback case of the op ending up in codegen, but maybe that means that fusing with the consumer like you plan is enough work to start if the others (when added) are covered by other possible optimizations!

  6. hanhanW commented on Oct 21, 2020

    @hanhanW
    ContributorAuthor

    Yes, and the transpose op will be a no-op if it's applied on a constant, which is the case in the first gather op in MobileBert.

  7. reopened this on Nov 9, 2020
  8. benvanik commented on Nov 22, 2020

    @benvanik
    Collaborator

    #1354 will negate the need for this - if we can accelerate that instead it's preferred.

  9. MaheshRavishankar commented on Nov 23, 2020

    @MaheshRavishankar
    Collaborator

    Lets leave it open for now. Its unclear what such ops will look like in tensor world. When we have a better idea, lets revisit.

  10. benvanik commented on Nov 23, 2020

    @benvanik
    Collaborator

    Out of curiosity @MaheshRavishankar do you say that because there's a representational problem in linalg for things like reductions (where input and output shapes don't match)? I'd expect that these ops are no longer visible to IREE after we perform fusion on linalg stuff as they should be fused into linalg thingies IREE passes along, but if they can't be then we should track that and ensure @nicolasvasilache puts that in the queue for future planning.

    For this issue specifically it'd be useful to categorize this as whether it would be a temporary improvement in the current infra for IREE's current hacky IdentifyDispatchRegions pass or whether it's a longer term improvement once that pass disappears. Otherwise it's a forever bug that'll never get closed :)

  11. MaheshRavishankar commented on Nov 23, 2020

    @MaheshRavishankar
    Collaborator

    Sorry, wrong response above. We already translate index_select operations to linalg on tensors and they are supposed to be fused with its consumers and producers. For fusion with producers, we can only fuse with the producers of the index operand. We cannot fuse with the producers of the input operand since it is not actually an operand to the generated linalg op.

  12. MaheshRavishankar commented on Nov 30, 2020

    @MaheshRavishankar
    Collaborator

    @hanhanW I think I have a fix for this FYI. Its WIP and need to make sure all the tests pass.

    @benvanik Just reread the comment above. My response seems like was unrelated to what you actually asked. Sorry about that. What I was saying is that torch_index_select has two operands, the input and the index operand. When lowered to linalg, the input operand is not represented as an operand of the linalg op. Reason for this is that the accesses to the input use indirections (i.e. data dependent) and this cannot be represented using the AffineMap that is used to represent accesses to the operands. To get around this, the input is implicitly captured in the body of the generated operation and the value is "loaded" (its a tensor, so its extracted). So it is a representational problem.
    Some of the same challenges exist with representing sparse computation in Linalg. Since that is being developed, I think the enhancements to Linalg necessitated by sparse computation will allow us to address the representation issue with the gather operation as well.
    So currently we can fuse the gather with its consumers, but not producer. You can fuse the gather op with the producer of the index operand, but AFAIK, the index operand is usually just data.

  13. hanhanW commented on Nov 30, 2020

    @hanhanW
    ContributorAuthor

    +1 on the detailed explanation.

    @MaheshRavishankar the fix is for what? Is it for #3761?

  14. MaheshRavishankar commented on Nov 30, 2020

    @MaheshRavishankar
    Collaborator

    @hanhanW yes, thats the bug I was looking for. Its requires a little bit of enhancement to the tensor_reshape -> linalg.generic/linalg.indexed_generic fusion. Ill send it out for review soon (got it to work late last night, so need to add tests and clean it up)

  15. MaheshRavishankar commented on Sep 2, 2021

    @MaheshRavishankar
    Collaborator

    Closing this. Gathers are fused with consumers now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

codegen

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions