Skip to content

Add fusion of named ops with consumer elementwise operations. #5040

Closed

Description

@MaheshRavishankar

The linalg on tensors path does not currently fuse the named ops with producer elementwise operations, though it is setup to do that.

Fusion in Linalg is implemented in "pull mode", i.e. you tile the operation and fuse its producers with it by creating the tile needed by the operation in place. This is done by instantiating a tiled version of the producer op within the tiling loops.

%0 = linalg.generic....
scf.for %iv0 = ...
  scf.for %iv1 = ...
     ...
     linalg.matmul ins(%0, ...)

gets fused as

%0 = linalg.generic....
scf.for %iv0 = ...
  scf.for %iv1 = ...
     ...
     %t0 = linalg.generic
     linalg.matmul ins(%t0, ...)

Creating a dispatch region first tiles and distributes the root operation and then does the fusion as shown above.

The logic for what to fuse is implemented in decideFusableOps(here) . It looks at Linalg operations and finds
a) Operations that are the root operation to use for creating a dispatch region. These operations are tagged with an IntegerAttr with name __root_op__. The attribute captures a unique number for that root operation.
b) Operations that are to be fused with the root operation are tagged with __fused_op__ attribute. This is an ArrayAttribute containing IntegerAttrs which is the identifier for the root operations (i.e the value in __root_op__ attribute on the root operation) that the operation can be fused with. An operation can be fused with multiple consumers. For example a linalg.fill op can be fused with all its users. This is valid thing to do, especially in tensor representation.

An operation cannot have both the __root_op__ attribute and the __fused_op__ attribute.

This mechanism is designed to allow an external (for example, search) to set these attributes on the ops, and the dispatch region creation will fuse things as needed. The only restriction being that there all ops to be fused with the root op form a DAG with the root operation at the root of the DAG.

To implement fusion of named op with its consumer, the consumer can be marked as the root and the named op as being fusable with it. Some considerations here to start with:

  1. The consumer must be an elementwise parallel operation with loop dimensionality same as the number of loops of the named op that would be tiled if it were not being fused.
  2. The named op has no other uses apart from the consumer (no reason to replicate the computation of the named op)
  3. The indexing map used to access the result of the named op in the consumer is an identity map. While it is not a requirement for fusion, it might be better to keep that as default to avoid surprising performance behavior.

A better solution might be to add a utility method to upstream Linalg that implements fuseConsumerOfTensor (which is analogous to the fuseProducerOfTensorhere).

Activity

  1. changed the title [-]Add fusion of named ops, i.e matmul, batch_matmul, etc. with consumer elementwise operations. This requires a simple change to the existing dispatch region creation to make this work naturally (TODO : add more details here)[/-] [+]Add fusion of named ops, i.e matmul, batch_matmul, etc. with consumer elementwise operations.[/+] on Mar 10, 2021
  2. changed the title [-]Add fusion of named ops, i.e matmul, batch_matmul, etc. with consumer elementwise operations.[/-] [+]Add fusion of named ops with consumer elementwise operations.[/+] on Mar 10, 2021
  3. added
    codegen
    on Mar 10, 2021
  4. MaheshRavishankar commented on Sep 5, 2021

    @MaheshRavishankar
    CollaboratorAuthor

    Superceded by Issue #6973. Closing this one.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    codegen

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions