Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

please add comments with meaning of einsum dimensions #8

Closed
bionicles opened this issue Jul 11, 2020 · 2 comments
Closed

please add comments with meaning of einsum dimensions #8

bionicles opened this issue Jul 11, 2020 · 2 comments

Comments

@bionicles
Copy link
Contributor

just curious about this line:

KV = torch.einsum("nshd,nshm->nhmd", K, values)

does 'n' means 'batch size', 'h' is 'heads', 'd' means 'channels'?

what are the 's' and 'm' dimensions?

@bionicles
Copy link
Contributor Author

just switching this up for JAX with channels first (for performance) and without batch size (jax.vmap automatically batches)

            x: The input features of shape (N, L, E) where N is the batch size,
               L is the sequence length (padded) and E is d_model passed in the
               constructor.

https://github.com/idiap/fast-transformers/blob/master/fast_transformers/transformers.py

"BLHC,BLHc->BHCc"

then without batch, it's

"LHC,LHc->HCc"

and with channels first it's:

"CHL,cHL->hCc"

@bionicles
Copy link
Contributor Author

        # D/d: d_head; H: n_heads; L: sequence length
        key_value = np.einsum("DHL,dHL->HDd", key, value)
        norm = 1.0 / (np.einsum("DHL,DH->HL", query, key.sum(2)) + 1e-6)
        attended = np.einsum("DHL,HDd,HL->DHL", query, key_value, norm)
        attended = attended.reshape((d_head * self.n_heads, length))

works like a charm! a lot faster too! thanks for your work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant