Skip to content

[FEATURE] Potential optimization for the selective_scan_seq #250

Description

@nwuestefeld

By replacing explicite tensor operations with torch.einsum() in the Zero-Order-Hold transformation, performance and readability can be improved.

Replacing the original Zero-Order-Hold transformation in line 518 of mamba_arch.py

     deltaA = torch.exp(delta.unsqueeze(-1) * A) 
     deltaB = delta.unsqueeze(-1) * B.unsqueeze(2) 
     BX = deltaB * (x.unsqueeze(-1))

with:

deltaA = torch.einsum('bld,dn->bldn', dt, A)
   BX = torch.einsum('bld,bld,bln->bldn', dt, u, B) 

can improve execution time by up to ~40% while requiring the same number of FLOPS. (See attached plot)

Image

Moreover, vectorization of the loop does not further improve execution time.

Activity

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

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions