Git Product home page Git Product logo

Comments (3)

stas-sl avatar stas-sl commented on May 27, 2024 2

is this really MSA? are we just linearly projecting to a greater dimension specified by hyperparameters and then projecting back to the embedding dimension?

For some reason you missed those lines from Attention.forward:

qkv = self.to_qkv(x).chunk(3, dim = -1)
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = self.heads), qkv)

dots = torch.matmul(q, k.transpose(-1, -2)) * self.scale

attn = self.attend(dots)
attn = self.dropout(attn)

out = torch.matmul(attn, v)

So it is not just linear projection to a greater dimension and back. Basically it does exactly what attention should do according to formula:
image

First, it computes similarity between keys and queries, then takes softmax (non linearity) to get attention weights and then applies them to values.

pytorch's MultiHeadAttention is definitely more verbose, it supports various mask, which are needed if you need causality (decoder) or you have sequences of different length, or it could use some cuda optimized hardware implementation, but in the simplest form that's basically the attention.

from vit-pytorch.

lucidrains avatar lucidrains commented on May 27, 2024

@stas-sl thank you Stas

@JavierUrenaPhDProjects i would not recommend pytorch's MHA. you will quickly find it inflexible if you want to do anything out of the ordinary

from vit-pytorch.

lucidrains avatar lucidrains commented on May 27, 2024

closing because not an issue

from vit-pytorch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.