File size: 12,974 Bytes
d91766b | 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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | import os
import json
import torch
import torch.nn as nn
from tqdm import tqdm
from glob import glob
from functools import partial
from safetensors import safe_open
from diffulex.config import Config
from diffulex.logger import get_logger
from diffulex.utils.checkpoint import LoadContext, ResolvedWeight
logger = get_logger(__name__)
def load_lora_config(lora_path: str) -> dict:
"""Load LoRA configuration from adapter_config.json."""
config_path = os.path.join(lora_path, "adapter_config.json")
if os.path.exists(config_path):
with open(config_path, "r") as f:
return json.load(f)
return {}
def enable_lora_for_model(model: nn.Module, lora_config: dict):
"""Enable LoRA for existing linear layers in the model."""
r = lora_config.get("r", 16)
lora_alpha = lora_config.get("lora_alpha", 32.0)
lora_dropout = lora_config.get("lora_dropout", 0.0)
target_modules = lora_config.get("target_modules", [])
for name, module in model.named_modules():
if hasattr(module, "__init_lora__"):
should_apply = True
if target_modules:
leaf = name.split(".")[-1] if name else name
should_apply = any(target == leaf for target in target_modules)
if should_apply:
module.__init_lora__(r, lora_alpha, lora_dropout)
return model
def default_weight_loader(param: nn.Parameter, loaded_weight: torch.Tensor):
param.data.copy_(loaded_weight)
def resolve_weight_spec(
model: nn.Module,
weight_name: str,
*,
config: Config,
named_modules: dict[str, nn.Module] | None = None,
) -> ResolvedWeight | None:
if named_modules is None:
named_modules = dict(model.named_modules())
ctx = LoadContext(config=config, full_name=weight_name)
parts = weight_name.split(".")
for i in range(len(parts), 0, -1):
prefix = ".".join(parts[:i])
module = named_modules.get(prefix)
if module is None:
continue
resolver = getattr(module, "resolve_checkpoint_weight", None)
if resolver is None:
continue
suffix = ".".join(parts[i:])
spec = resolver(suffix, ctx)
if spec is not None:
return spec
root_resolver = getattr(model, "resolve_checkpoint_weight", None)
if root_resolver is not None:
return root_resolver(weight_name, ctx)
return None
def apply_resolved_weight(spec: ResolvedWeight, loaded_weight: torch.Tensor):
if spec.skip:
return
if spec.transform is not None:
loaded_weight = spec.transform(loaded_weight)
if spec.loader is not None:
spec.loader(loaded_weight)
return
if spec.param is not None:
weight_loader = getattr(spec.param, "weight_loader", default_weight_loader)
if spec.shard_id is None:
weight_loader(spec.param, loaded_weight)
else:
weight_loader(spec.param, loaded_weight, spec.shard_id)
return
if spec.buffer is not None:
spec.buffer.copy_(loaded_weight)
return
raise ValueError("ResolvedWeight must specify loader, param, buffer, or skip.")
def try_load_direct(model: nn.Module, weight_name: str, loaded_weight: torch.Tensor) -> bool:
try:
param = model.get_parameter(weight_name)
weight_loader = getattr(param, "weight_loader", default_weight_loader)
weight_loader(param, loaded_weight)
return True
except (AttributeError, KeyError):
pass
try:
buffer = model.get_buffer(weight_name)
buffer.copy_(loaded_weight)
return True
except (AttributeError, KeyError):
return False
def try_load_via_packed_mapping(
model: nn.Module,
packed_modules_mapping: dict,
weight_name: str,
loaded_weight: torch.Tensor,
config: Config,
) -> bool:
for k in packed_modules_mapping:
if k not in weight_name:
continue
if config.model_name == "llada" and k == "ff_out" and "transformer.ff_out" in weight_name:
continue
elif config.model_name == "llada" and k == "transformer.ff_out":
v, shard_id = packed_modules_mapping[k]
assert v == "lm_head"
param_name = "lm_head.weight"
else:
v, shard_id = packed_modules_mapping[k]
param_name = weight_name.replace(k, v)
if "layernorm" in param_name:
try:
param = model.get_parameter(param_name)
weight_loader = getattr(param, "weight_loader", default_weight_loader)
weight_loader(param, loaded_weight)
except (AttributeError, KeyError):
try:
buffer = model.get_buffer(param_name)
buffer.copy_(loaded_weight)
except (AttributeError, KeyError):
pass
else:
try:
param = model.get_parameter(param_name)
weight_loader = partial(
getattr(param, "weight_loader"),
param,
loaded_weight,
)
if shard_id is None:
weight_loader()
else:
weight_loader(shard_id)
except (AttributeError, KeyError):
pass
return True
return False
def load_model(model: nn.Module, config: Config):
"""Load model weights and optionally LoRA weights."""
# Enable LoRA for linear layers if LoRA is enabled
if config.use_lora and config.lora_path:
lora_config = load_lora_config(config.lora_path)
if lora_config:
logger.info(f"LoRA Config Loaded: {lora_config}")
model = enable_lora_for_model(model, lora_config)
else:
logger.info("No adapter_config.json found, using default LoRA parameters")
default_config = {"r": 16, "lora_alpha": 32.0, "lora_dropout": 0.0}
model = enable_lora_for_model(model, default_config)
# Load base model weights
packed_modules_mapping = getattr(model, "packed_modules_mapping", {})
named_modules = dict(model.named_modules())
for file in tqdm(glob(os.path.join(config.model, "*.safetensors")), desc="Loading base model"):
with safe_open(file, "pt", "cpu") as f:
for weight_name in f.keys():
loaded_weight = f.get_tensor(weight_name)
spec = resolve_weight_spec(
model,
weight_name,
config=config,
named_modules=named_modules,
)
if spec is not None:
apply_resolved_weight(spec, loaded_weight)
continue
if try_load_via_packed_mapping(model, packed_modules_mapping, weight_name, loaded_weight, config):
continue
try_load_direct(model, weight_name, loaded_weight)
# Load LoRA weights if enabled
if config.use_lora and config.lora_path:
if os.path.exists(config.lora_path):
logger.info(f"Loading LoRA weights from {config.lora_path}")
model = load_lora_weights(
model,
config.lora_path,
packed_modules_mapping=packed_modules_mapping if config.model_name == "llada" else None,
pre_merge_lora=getattr(config, "pre_merge_lora", False),
)
else:
logger.warning(f"LoRA path {config.lora_path} does not exist, skipping LoRA loading")
return model
def load_lora_weights(
model: nn.Module,
lora_path: str,
packed_modules_mapping: dict | None = None,
pre_merge_lora: bool = False,
):
"""Load LoRA weights into LoRA-enabled layers.
Args:
model: The model with LoRA-enabled linear layers.
lora_path: Path to LoRA checkpoint.
packed_modules_mapping: Optional mapping for packed modules (e.g. llada lm_head).
pre_merge_lora: If True, merge LoRA into base weights after loading so that
forward does not need to run LoRA computation each time. If False, keep
LoRA separate and apply it in lora_forward during each forward pass.
"""
try:
lora_config = load_lora_config(lora_path)
target_modules = lora_config.get("target_modules", [])
lora_weights = {}
for file in tqdm(glob(os.path.join(lora_path, "*.safetensors")), desc="Loading LoRA"):
with safe_open(file, "pt", "cpu") as f:
for weight_name in f.keys():
lora_weights[weight_name] = f.get_tensor(weight_name)
applied_count = 0
modified_modules = None
if packed_modules_mapping is not None:
modified_modules = [v for k, (v, _) in packed_modules_mapping.items() if k in target_modules]
rev_mapping = {v: k for k, (v, _) in packed_modules_mapping.items()}
for name, module in model.named_modules():
if hasattr(module, "lora_A") and hasattr(module, "lora_B"):
should_apply = True
if modified_modules is not None:
modified_module_type = ".".join(name.split(".")[-2:])
org_module_type = rev_mapping[modified_module_type]
org_name = name.replace(modified_module_type, org_module_type)
should_apply = any(target in modified_module_type for target in modified_modules)
elif target_modules:
module_type = name.split(".")[-1] if "." in name else name
should_apply = any(target in module_type for target in target_modules)
if not should_apply:
continue
base_patterns = (
[
name,
f"base_model.model.{name}",
f"model.{name}",
]
if modified_modules is None
else [
org_name,
f"base_model.model.{org_name}",
f"model.{org_name}",
]
)
found_a = found_b = None
for base_name in base_patterns:
lora_a_keys = [
f"{base_name}.lora_A.weight",
f"{base_name}.lora_A.default.weight",
f"{base_name}.lora_A",
]
lora_b_keys = [
f"{base_name}.lora_B.weight",
f"{base_name}.lora_B.default.weight",
f"{base_name}.lora_B",
]
for key in lora_a_keys:
if key in lora_weights:
found_a = lora_weights[key]
break
for key in lora_b_keys:
if key in lora_weights:
found_b = lora_weights[key]
break
if found_a is not None and found_b is not None:
break
if found_a is not None and found_b is not None:
if hasattr(module, "tp_size") and module.tp_size > 1:
if hasattr(module, "tp_dim") and module.tp_dim == 0:
shard_size = found_b.size(0) // module.tp_size
start_idx = module.tp_rank * shard_size
found_b = found_b[start_idx : start_idx + shard_size]
elif hasattr(module, "tp_dim") and module.tp_dim == 1:
shard_size = found_a.size(1) // module.tp_size
start_idx = module.tp_rank * shard_size
found_a = found_a[:, start_idx : start_idx + shard_size]
try:
module.lora_A.data.copy_(found_a)
module.lora_B.data.copy_(found_b)
applied_count += 1
except Exception as e:
logger.warning(f"Failed to load LoRA weights for {name}: {e}")
if pre_merge_lora:
mergeable_modules = [module for module in model.modules() if hasattr(module, "merge_lora")]
for module in tqdm(mergeable_modules, desc="Merging LoRA"):
module.merge_lora()
logger.info(f"LoRA weights applied to {applied_count} layers and merged into base")
else:
logger.info(f"LoRA weights applied to {applied_count} layers (unmerged, applied per forward)")
except Exception as e:
logger.error(f"Error loading LoRA weights: {e}")
logger.warning("Continuing with base model only")
return model
|