Coder Social home page Coder Social logo

About GPU utilization about lora HOT 1 CLOSED

TemugeB avatar TemugeB commented on May 23, 2024
About GPU utilization

from lora.

Comments (1)

TemugeB avatar TemugeB commented on May 23, 2024

After some more investigation, not specifically creating dW seems to work.

The LoRA forward pass is modified to accept low rank matrices directly. Then, dW is not specifically created and the GPU memory utilization goes down to 2000MB.

    def forward_lora(self, x, dWa, dWb):
        B, S, E = x.shape
        
        #calculate frozen model output
        qkv = self.qkv(x).reshape(B, 3, S, E).permute(1, 0, 2, 3)

        #calculate LoRA adaption
        dqkv = (x@dWb.T @ dWa.T).reshape(B, 3, S, E).permute(1, 0, 2, 3)
        #add as in equation (3) in paper
        qkv = qkv + dqkv
        q, k, v = qkv.unbind(0)
        
        attn = q @ k.transpose(-2, -1)
        x = attn @ v
        return x

I tried to do this using pytorch parameterization. But this seems to copy the original weight tensor:

import torch.nn.utils.parametrize as parametrize

class LoRA_Layer_Mod(nn.Module):

    def __init__(self, embed_dim, rank, device):
        super().__init__()

        self.embed_dim = embed_dim
        self.dWa = nn.Parameter(torch.normal(0, 1, (3*embed_dim, rank))/sqrt(rank)).to(device)
        self.dWb = nn.Parameter(torch.normal(0, 1, (rank, embed_dim))/sqrt(rank)).to(device)

    def forward(self, qkv):
        return qkv + self.dWa @ self.dWb


basemodel = BaseModel(embed).to('cuda:0')
basemodel.requires_grad_(False)
x = torch.ones((1, seq, embed)).to('cuda:0')

lora_layer = LoRA_Layer_Mod(embed_dim=embed, rank=2, device='cuda:0')
parametrize.register_parametrization(basemodel.qkv, "weight", lora_layer)
out = basemodel(x)
out = torch.sum(out)
out.backward()
input('check nvidia-smi for GPU utilization')

The conclusion seems to be to not create AB matrix specifically and don't use pytorch parametrization.

from lora.

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.