Skip to content

Instantly share code, notes, and snippets.

@catid
Last active February 16, 2024 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catid/7705835601ea71e4588652135a3a587e to your computer and use it in GitHub Desktop.
Save catid/7705835601ea71e4588652135a3a587e to your computer and use it in GitHub Desktop.
DoRA the explora
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
# This layer is dropped into your pre-trained PyTorch model where nn.Linear is used
class DoRALayer(nn.Module):
def __init__(self, d_in, d_out, rank=4):
super().__init__()
# Match original layer weights
self.weight = nn.Parameter(torch.Tensor(d_out, d_in), requires_grad=False)
self.bias = nn.Parameter(torch.Tensor(d_out), requires_grad=False)
# m = Magnitude column-wise across output dimension
self.m = nn.Parameter(self.weight.norm(p=2, dim=0, keepdim=True))
self.lora_A = nn.Parameter(torch.randn(d_out, rank))
self.lora_B = nn.Parameter(torch.randn(rank, d_in))
def forward(self, x):
lora = torch.matmul(self.lora_A, self.lora_B)
adapted = self.weight + lora
column_norm = adapted.norm(p=2, dim=0, keepdim=True)
norm_adapted = adapted / column_norm
calc_weights = self.m * norm_adapted
return F.linear(x, calc_weights, self.bias)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment