Text Generation
English
code
File size: 5,720 Bytes
c1b402f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""
=====================================
 Shulker Code — LoRA / PEFT Module
 Low-Rank Adaptation for fine-tuning
 Developed by @kopeedev / CyeroX
=====================================

LoRA: Instead of updating all weights W, we learn two small matrices:
    W' = W + α * (A @ B)
where A ∈ R^{d×r} and B ∈ R^{r×k}, rank r << d,k

This makes fine-tuning with minimal parameters possible!
"""

import math
from typing import Optional, List, Dict, Any
import torch
import torch.nn as nn
import torch.nn.functional as F


class LoRALinear(nn.Module):
    """
    A Linear layer augmented with LoRA adapters.
    Wraps an existing nn.Linear and adds low-rank update matrices.
    """

    def __init__(
        self,
        original_layer: nn.Linear,
        rank: int = 8,
        alpha: float = 16.0,
        dropout: float = 0.05,
        enabled: bool = True,
    ):
        super().__init__()

        self.rank = rank
        self.alpha = alpha
        self.scaling = alpha / rank
        self.enabled = enabled

        in_features = original_layer.in_features
        out_features = original_layer.out_features

        # Keep the original frozen weights
        self.weight = original_layer.weight
        self.bias = original_layer.bias

        if enabled and rank > 0:
            # LoRA matrices: A (init with Kaiming), B (init with zeros)
            self.lora_A = nn.Parameter(torch.empty(rank, in_features))
            self.lora_B = nn.Parameter(torch.zeros(out_features, rank))
            self.lora_dropout = nn.Dropout(dropout)

            # Initialize A with kaiming uniform (standard)
            nn.init.kaiming_uniform_(self.lora_A, a=math.sqrt(5))
        else:
            self.lora_A = None
            self.lora_B = None
            self.lora_dropout = None

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        # Base frozen forward pass
        base_out = F.linear(x, self.weight, self.bias)

        # Add LoRA delta (if enabled)
        if self.enabled and self.rank > 0:
            lora_out = self.lora_dropout(x) @ self.lora_A.T @ self.lora_B.T
            return base_out + lora_out * self.scaling

        return base_out

    def merge_weights(self):
        """
        Merge LoRA weights into the base weights permanently.
        Call before exporting/deploying to remove LoRA overhead.
        """
        if self.enabled and self.rank > 0:
            delta = (self.lora_B @ self.lora_A) * self.scaling
            self.weight.data += delta
            self.lora_A = None
            self.lora_B = None
            self.enabled = False
            print("✅ LoRA weights merged into base layer.")


class LoRAConfig:
    """Configuration for LoRA fine-tuning."""
    def __init__(
        self,
        rank: int = 8,
        alpha: float = 16.0,
        dropout: float = 0.05,
        # Which modules to apply LoRA to
        target_modules: Optional[List[str]] = None,
    ):
        self.rank = rank
        self.alpha = alpha
        self.dropout = dropout
        # Default: apply to attention projection layers
        self.target_modules = target_modules or ["q_proj", "v_proj", "o_proj"]


def apply_lora(model: nn.Module, lora_config: LoRAConfig) -> nn.Module:
    """
    Apply LoRA adapters to a model's target linear layers.

    Steps:
    1. Freeze ALL base model parameters
    2. Replace target Linear layers with LoRALinear wrappers
    3. Only LoRA parameters (A, B) are trainable

    Args:
        model: The base ShulkerCodeModel
        lora_config: LoRA configuration

    Returns:
        Modified model with LoRA applied
    """
    # Step 1: Freeze everything
    for param in model.parameters():
        param.requires_grad = False

    # Step 2: Replace target layers
    replaced = 0
    for name, module in model.named_modules():
        for target in lora_config.target_modules:
            if name.endswith(target) and isinstance(module, nn.Linear):
                # Navigate to parent module
                parent_name, child_name = name.rsplit(".", 1)
                parent = dict(model.named_modules())[parent_name]

                # Replace with LoRA wrapper
                lora_layer = LoRALinear(
                    original_layer=module,
                    rank=lora_config.rank,
                    alpha=lora_config.alpha,
                    dropout=lora_config.dropout,
                )
                setattr(parent, child_name, lora_layer)
                replaced += 1
                break

    # Step 3: Count trainable params
    trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
    total = sum(p.numel() for p in model.parameters())
    print(f"✅ LoRA applied to {replaced} layers")
    print(f"   Trainable params: {trainable:,} / {total:,} ({100*trainable/total:.2f}%)")

    return model


def save_lora_weights(model: nn.Module, path: str):
    """Save only LoRA adapter weights (very small files!)."""
    import os
    os.makedirs(path, exist_ok=True)
    lora_state = {
        name: param
        for name, param in model.named_parameters()
        if "lora_" in name
    }
    torch.save(lora_state, os.path.join(path, "lora_weights.pt"))
    print(f"✅ LoRA weights saved to {path} ({len(lora_state)} tensors)")


def load_lora_weights(model: nn.Module, path: str, device: str = "cpu"):
    """Load LoRA adapter weights back into a model."""
    import os
    weights_path = os.path.join(path, "lora_weights.pt")
    lora_state = torch.load(weights_path, map_location=device)
    missing, unexpected = model.load_state_dict(lora_state, strict=False)
    print(f"✅ LoRA weights loaded. Missing: {len(missing)}, Unexpected: {len(unexpected)}")
    return model