Giter VIP home page Giter VIP logo

Comments (3)

zRzRzRzRzRzRzR avatar zRzRzRzRzRzRzR commented on September 13, 2024

xformers是有支持v100的版本的,直接换成支持v100版本的xformers试试?

from cogvlm2.

Easily2 avatar Easily2 commented on September 13, 2024

我之前在测试cogvlm1的时候,也有遇到同样的问题,替换为self.attention(q, k, v)结果完全不正确;
cogvlm2的这块Attention应该和cogvlm1是一致的;
可以参考xformers的文档:https://facebookresearch.github.io/xformers/components/ops.html#:~:text=Equivalent%20pytorch%20code

进行下列替换,我在cogvlm1上的测试结果是正常的:

class Attention(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.num_heads = config.num_heads
        head_dim = config.hidden_size // config.num_heads
        self.scale = head_dim ** -0.5
        self.query_key_value = nn.Linear(config.hidden_size, config.hidden_size * 3)
        self.dense = nn.Linear(config.hidden_size, config.hidden_size)
        self.output_dropout = torch.nn.Dropout(config.dropout_prob)
        self.dropout = torch.nn.Dropout(config.dropout_prob)

    def forward(self, x: "tensor(B, L, D)") -> "tensor(B, L, D)":
        B, L, _ = x.shape
        qkv = self.query_key_value(x)
        qkv = qkv.reshape(B, L, 3, self.num_heads, -1).permute(2, 0, 1, 3, 4)  # 3, B, L, H, D
        query, key, value = qkv[0], qkv[1], qkv[2]

        # out = xops.memory_efficient_attention(
        #     q, k, v, scale=self.scale,
        # )
        # out = self.attention(query, key, value)

        scale = 1.0 / query.shape[-1] ** 0.5
        query = query * scale
        query = query.transpose(1, 2)
        key = key.transpose(1, 2)
        value = value.transpose(1, 2)
        attn = query @ key.transpose(-2, -1)
        attn = attn.softmax(-1)
        attn = self.dropout(attn)
        attn = attn @ value
        out = attn.transpose(1, 2)

        output = self.dense(out.contiguous().view(B, L, -1))
        output = self.output_dropout(output)
        return output

from cogvlm2.

morgan-bc avatar morgan-bc commented on September 13, 2024

@Easily2 谢谢,问题解决了

from cogvlm2.

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.