Spaces:
Runtime error
Runtime error
File size: 14,017 Bytes
0558aa4 |
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from dataclasses import dataclass, field
from typing import List, Literal, Optional, Tuple
import torch
from megatron.core.dist_checkpointing.mapping import ShardedStateDict
from torch import nn
from nemo.collections.llm.peft.lora import LinearAdapter, LoRALinear
from nemo.collections.llm.peft.module_matcher import ModuleMatcher
from nemo.collections.llm.peft.utils import get_adapter_attributes_from_linear, is_expert_linear
from nemo.lightning.pytorch.callbacks.peft import PEFT, AdapterWrapper
from nemo.utils import logging
class ModuleDict(nn.ModuleDict):
"""
nn.ModuleDict with a sharded_state_dict implementation for checkpointing
"""
def sharded_state_dict(
self,
prefix: str = '',
sharded_offsets: Tuple[Tuple[int, int, int]] = (),
metadata: Optional[dict] = None,
) -> "ShardedStateDict":
"""Retrieve the sharded state dictionary of the wrapped module and adapter.
This method is used for distributed checkpointing, combining the sharded states
of both the main module and the adapter.
Args:
prefix (str): A prefix added to parameter and buffer names. Defaults to ''.
sharded_offsets (Tuple[Tuple[int, int, int]]): Offsets for sharded parameters.
Defaults to an empty tuple.
metadata (Optional[dict]): Additional metadata for the sharded state.
Defaults to None.
Returns:
ShardedStateDict: The combined sharded state dictionary.
"""
sharded_state_dict = {}
for key, layer in self.items():
sharded_state_dict.update(layer.sharded_state_dict(f"{prefix}{key}.", sharded_offsets, metadata))
return sharded_state_dict
class LoRALinearSplitQKV(AdapterWrapper):
"""An adapter wrapper for `linear_qkv` where q, k, v are three separate adapters.
This module that adds the output of the adapters to the output of the wrapped module while taking care of shape.
This class is designed to be used with LoRA (Low-Rank Adaptation) and similar techniques
where the adapter's output is added to the main module's output. It extends the AdapterWrapper
class to provide a specific implementation of the forward method.
"""
def forward(self, x):
# pylint: disable=C0115,C0116
linear_output, bias, layernorm_output = self.base_linear_forward(x)
query = self.adapter.adapter_q(layernorm_output)
key = self.adapter.adapter_k(layernorm_output)
value = self.adapter.adapter_v(layernorm_output)
query_4d = query.reshape(query.shape[0], query.shape[1], -1, self.to_wrap.config.kv_channels)
key_4d = key.reshape(key.shape[0], key.shape[1], -1, self.to_wrap.config.kv_channels)
value_4d = value.reshape(value.shape[0], value.shape[1], -1, self.to_wrap.config.kv_channels)
qkv_4d = torch.cat([query_4d, key_4d, value_4d], dim=2)
adapter_output = qkv_4d.reshape(qkv_4d.shape[0], qkv_4d.shape[1], -1)
return linear_output + adapter_output.reshape(linear_output.shape), bias
class LoRALinearSplitFC1UpGate(AdapterWrapper):
"""An adapter wrapper for `linear_fc1` where up_proj and gate_proj are two separate adapters.
This module that adds the output of the adapters to the output of the wrapped module while taking care of shape.
This class is designed to be used with LoRA (Low-Rank Adaptation) and similar techniques
where the adapter's output is added to the main module's output. It extends the AdapterWrapper
class to provide a specific implementation of the forward method.
"""
def forward(self, x):
# pylint: disable=C0115,C0116
linear_output, bias, layernorm_output = self.base_linear_forward(x)
adapter_output_gate = self.adapter.adapter_gate(layernorm_output)
adapter_output_up = self.adapter.adapter_up(layernorm_output)
adapter_output = torch.cat([adapter_output_gate, adapter_output_up], dim=2)
return linear_output + adapter_output.reshape(linear_output.shape), bias
@dataclass
class CanonicalLoRA(PEFT, ModuleMatcher):
"""
Implements the LoRA (Low-Rank Adaptation) module for parameter-efficient fine-tuning.
Canonical LoRA applies LoRA on Q, K, V projection matrices separately, as well as Up and Gate projection
matrices separately. This follows more closely with Huggingface's implementation of LoRA.
Args:
target_modules (List[str], optional): A list of module names to apply LoRA to.
Defaults to all linear layers ['linear_q', 'linear_k', 'linear_v', 'linear_proj',
'linear_fc1_up', 'linear_fc1_gate', 'linear_fc2'].
- 'linear_q', 'linear_k', 'linear_v': Apply LoRA to the linear layer used for query, key, and value
projections in self-attention. This is fused into one matrix in NeMo LoRA, but left as three
separate matrices in Canonical LoRA.
- 'linear_proj': Apply LoRA to the linear layer used for projecting the output of self-attention.
- 'linear_fc1_up', 'linear_fc1_proj': Apply LoRA to the Up proj and Gate proj layers.
These two together constitute the first fully-connected layer in MLP in NeMo LoRA.
- 'linear_fc2': Apply LoRA to the second fully-connected layer in MLP.
Target modules can also contain wildcards. For example, you can specify
target_modules=['*.layers.0.*.linear_q', '*.layers.1.*.linear_q'] to add LoRA to only linear_q
on the first two layers.
exclude_modules (List[str], optional): A list of module names not to apply LoRa to. It will
match all nn.Linear & nn.Linear-adjacent modules whose name does not match any string in
exclude_modules. If used, will require target_modules to be empty list or None.
dim (int): Dimension of the low-rank projection space. Defaults to 32.
alpha (int): Weighting factor for the low-rank projection. Defaults to 32.
dropout (float): Dropout rate for the low-rank projection. Defaults to 0.0.
dropout_position (Literal['pre', 'post'], optional): Position for applying dropout.
Can be 'pre' (before the low-rank projection) or 'post' (after). Defaults to 'pre'.
Example:
--------
>>> from nemo.collections import llm
>>> lora = llm.peft.CanonicalLoRA(target_modules=['linear_q', 'linear_k', 'linear_v', 'linear_fc1_up'], dim=32)
>>> model = llm.Mistral7BModel(model_transform=lora)
>>> # (set up trainer and data)
>>> trainer.fit(model, data)
References:
-----------
Hu, E. J., Shen, Y., Wallis, P., Allen-Zhu, Z., Li, Y., Wang, S., Wang, L., & Chen, W. (2021).
LoRA: Low-Rank Adaptation of Large Language Models. arXiv preprint arXiv:2106.09685.
https://arxiv.org/abs/2106.09685
)
"""
target_modules: List[str] = field(
default_factory=lambda: [
'linear_q',
'linear_k',
'linear_v',
'linear_proj',
'linear_fc1_up',
'linear_fc1_gate',
'linear_fc2',
]
)
dim: int = 32
alpha: int = 32
dropout: float = 0.0
dropout_position: Literal['pre', 'post'] = 'pre'
lora_A_init_method: str = "xavier"
lora_B_init_method: str = "zero"
def __post_init__(self):
"""
Construct a mapping from the target module as supported in LoRA() to the specific parts of the layer for which
adapter is applied.
For example, if user specifies target_module = ['linear_q', 'linear_k', 'linear_proj', 'linear_fc1_up'], then
canonical_lora_mapping = {
"linear_qkv": {'linear_q', 'linear_k'},
"linear_proj": {'linear_proj'}, # the value of this key does not matter
"linear_fc1": {'linear_fc1_up'},
}
If user specifies target_module = ['*.layers.0.*.linear_q', '*.layers.1.*.linear_q'], then
canonical_lora_mapping = {
"'*.layers.0.*.linear_qkv'": {'linear_q'},
"'*.layers.1.*.linear_qkv'": {'linear_q'},
}
"""
for target in self.target_modules:
assert not target.endswith("linear_qkv"), (
"Canonical LoRA does not support target 'linear_qkv'. Either use 'linear_qkv' with LoRA() or "
"use ['linear_q', 'linear_k', 'linear_v'] with Canonical LoRA"
)
assert not target.endswith("linear_fc1"), (
"Canonical LoRA does not support target 'linear_fc1'. Either use 'linear_fc1' with LoRA() or "
"use ['linear_fc1_up', 'linear_fc1_gate'] with Canonical LoRA"
)
if 'linear_q' in target:
self.canonical_mapping[target.replace('linear_q', 'linear_qkv')].add('linear_q')
elif 'linear_k' in target:
self.canonical_mapping[target.replace('linear_k', 'linear_qkv')].add('linear_k')
elif 'linear_v' in target:
self.canonical_mapping[target.replace('linear_v', 'linear_qkv')].add('linear_v')
elif 'linear_fc1_up' in target:
self.canonical_mapping[target.replace('linear_fc1_up', 'linear_fc1')].add('linear_fc1_up')
elif 'linear_fc1_gate' in target:
self.canonical_mapping[target.replace('linear_fc1_gate', 'linear_fc1')].add('linear_fc1_gate')
else:
self.canonical_mapping[target].add(target)
def transform(self, m: nn.Module, name=None, prefix=None):
"""
Applies LoRA to a specific module within the model architecture.
Args:
m (nn.Module): The module to apply LoRA to.
name (str, optional): Name of the module (if applicable). Defaults to None.
prefix (str, optional): Prefix for the module name (if applicable). Defaults to None.
Returns:
nn.Module: The modified module with LoRA applied, or the original module if not a target.
"""
from nemo.collections.llm.peft.utils import ParallelLinearAdapter
if (ans := self.match(m, name, prefix)) is not None:
(match, full_name) = ans
if isinstance(m, nn.Linear):
return LinearAdapter(
m, dim=self.dim, alpha=self.alpha, dropout=self.dropout, lora_A_init_method=self.lora_A_init_method
)
input_is_parallel, in_features, out_features, disable_sp_comm, base_linear_is_parallel = (
get_adapter_attributes_from_linear(m)
)
adapter_kwargs = dict(
dim=self.dim,
base_linear_name=full_name,
activation='identity',
norm_type=None,
column_init_method=self.lora_A_init_method,
row_init_method=self.lora_B_init_method,
gather_output=False,
input_is_parallel=input_is_parallel,
dropout=self.dropout,
dropout_position=self.dropout_position,
model_parallel_config=getattr(m, "config", None),
alpha=self.alpha,
is_expert=is_expert_linear(full_name),
disable_sequence_parallel_comm=disable_sp_comm,
base_linear_is_parallel=base_linear_is_parallel,
)
if name in ['linear_proj', 'linear_fc2']:
adapter = ParallelLinearAdapter(in_features, out_features, **adapter_kwargs)
logging.info(f"Adding lora to: {full_name}")
return LoRALinear(m, adapter)
canonical_submodules = self.canonical_mapping[match]
logging.info(f"Adding lora to: {full_name} ({canonical_submodules})")
if name == 'linear_qkv':
adapter_q, adapter_k, adapter_v = None, None, None
kv_out_features = m.config.kv_channels * m.config.num_query_groups
if 'linear_q' in canonical_submodules:
adapter_q = ParallelLinearAdapter(in_features, in_features, **adapter_kwargs)
if 'linear_k' in canonical_submodules:
adapter_k = ParallelLinearAdapter(in_features, kv_out_features, **adapter_kwargs)
if 'linear_v' in canonical_submodules:
adapter_v = ParallelLinearAdapter(in_features, kv_out_features, **adapter_kwargs)
adapters = ModuleDict({'adapter_q': adapter_q, 'adapter_k': adapter_k, 'adapter_v': adapter_v})
return LoRALinearSplitQKV(m, adapters)
if name == 'linear_fc1':
adapter_up, adapter_gate = None, None
if 'linear_fc1_up' in canonical_submodules:
adapter_up = ParallelLinearAdapter(in_features, out_features // 2, **adapter_kwargs)
if 'linear_fc1_gate' in canonical_submodules:
adapter_gate = ParallelLinearAdapter(in_features, out_features // 2, **adapter_kwargs)
adapters = ModuleDict({'adapter_up': adapter_up, 'adapter_gate': adapter_gate})
return LoRALinearSplitFC1UpGate(m, adapters)
return m
|