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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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/env python3
"""
Apply generated SwiGLU MLP weights to a Gemma 4 31B safetensors model.
Layer files contain gate_proj.weight / up_proj.weight / down_proj.weight
as pre-computed delta tensors — fused via Shape-Contoured Fusion (SCF).

SCF replaces the old naive additive delta approach:
  - down_proj : contoured multiplicative delta  (dynamic_alpha * delta * W_existing)
  - gate_proj : multiplicative gamma scaling    (W * (1 + clamp(delta, +/-gamma_cap)))
  - up_proj   : intentionally unchanged         (linear path, as in fuzer.py)

Gemma 4 31B interleaved attention: 5 SWA + 1 global per period (60 layers total).
Global layers (5, 11, 17, 23, 29, 35, 41, 47, 53, 59) may carry double-wide MLP tensors;
partial coverage is handled transparently via row/col clamping.
"""

import argparse
import json
import shutil
from pathlib import Path

import numpy as np
import torch
from safetensors.torch import load, load_file, save_file

PROJ_KEYS = ("gate_proj.weight", "up_proj.weight", "down_proj.weight")

INTERLEAVE_PERIOD   = 6
GLOBAL_LAYER_OFFSET = 5


def is_global_attention_layer(layer_idx: int) -> bool:
    return (
        layer_idx >= GLOBAL_LAYER_OFFSET
        and (layer_idx - GLOBAL_LAYER_OFFSET) % INTERLEAVE_PERIOD == 0
    )


def detect_key_prefix(tensor_keys, layer_idx: int, proj: str) -> str:
    """Dynamically locate the exact key prefix in the target file.

    Gemma 4 is a VLM: always prefer language_model matches over vision tower.
    """
    suffix  = f"layers.{layer_idx}.mlp.{proj}"
    matches = [k for k in tensor_keys if k.endswith(suffix)]
    for k in matches:
        if "language_model" in k:
            return k[: -len(suffix)]
    if matches:
        return matches[0][: -len(suffix)]
    return "model.language_model.model."


def discover_generated_layers(weights_dir: Path) -> dict:
    layers = {}
    for f in sorted(weights_dir.glob("layer_*.safetensors")):
        try:
            idx = int(f.stem.split("_")[1])
            layers[idx] = f
        except (IndexError, ValueError):
            continue
    return layers


# ---------------------------------------------------------------------------
# Shape-Contoured Fusion applied to pre-computed delta tensors
# ---------------------------------------------------------------------------

def fuse_layer_deltas(
    layer_idx: int,
    gate_w:      torch.Tensor,   # float32, modified in-place
    up_w:        torch.Tensor,   # float32, intentionally NOT modified
    down_w:      torch.Tensor,   # float32, modified in-place
    new_weights: dict,
    args:        argparse.Namespace,
) -> None:
    """
    Apply SCF to one layer using pre-computed delta tensors.

    down_proj -- contoured additive:
        delta is scaled by the existing weight profile so the update respects
        the model's learned contour.  dynamic_alpha is variance-normalised so
        scale stays consistent across layers regardless of initialisation.

    gate_proj -- multiplicative gamma:
        gamma = 1 + clamp(delta, +-gamma_cap)
        Matches fuzer's W*gamma pattern without needing raw adapter weights.

    up_proj -- unchanged:
        Linear value path in SwiGLU must not receive non-linear scaling.
        Intentional, mirrors fuzer's explicit decision.
    """

    # down_proj: contoured multiplicative delta
    if "down_proj.weight" in new_weights:
        delta_down = new_weights["down_proj.weight"].float()
        nr = min(delta_down.shape[0], down_w.shape[0])
        nc = min(delta_down.shape[1], down_w.shape[1])

        fan_in        = down_w.shape[1]
        expected_var  = 1.0 / fan_in
        down_var      = down_w[:nr, :nc].var().item()
        dynamic_alpha = float(np.clip(
            args.alpha * (down_var / (expected_var + 1e-8)),
            args.alpha * 0.1,
            args.alpha * 10.0,
        ))

        contoured = dynamic_alpha * delta_down[:nr, :nc] * down_w[:nr, :nc]
        down_w[:nr, :nc] = down_w[:nr, :nc] + contoured

        if nr < down_w.shape[0] or nc < down_w.shape[1]:
            print(f"  [warn] Layer {layer_idx}: down_proj delta covers "
                  f"{nr}x{nc} of {down_w.shape[0]}x{down_w.shape[1]} -- partial fusion")

    # gate_proj: multiplicative gamma
    if "gate_proj.weight" in new_weights:
        delta_gate = new_weights["gate_proj.weight"].float()
        nr = min(delta_gate.shape[0], gate_w.shape[0])
        nc = min(delta_gate.shape[1], gate_w.shape[1])

        gamma = 1.0 + delta_gate[:nr, :nc].clamp(-args.gamma_cap, args.gamma_cap)
        gate_w[:nr, :nc] = gate_w[:nr, :nc] * gamma

    # up_proj: intentionally untouched -- linear path must stay unchanged


# ---------------------------------------------------------------------------
# Single-file apply
# ---------------------------------------------------------------------------

def apply_single_file(model_path: Path, output_dir: Path, layer_files: dict, args) -> int:
    dry_run = args.dry_run
    print(f"\n[model] Processing file: {model_path.name}")

    # load_file uses memory-mapping — avoids reading the whole file into RAM twice
    tensors = load_file(str(model_path))

    fused   = 0
    skipped = 0

    for layer_idx, layer_path in sorted(layer_files.items()):
        layer_type = "global" if is_global_attention_layer(layer_idx) else "swa"

        new_weights = load_file(str(layer_path))

        if not any(k in new_weights for k in PROJ_KEYS):
            print(f"  [skip] Layer {layer_idx}: none of {PROJ_KEYS} found. "
                  f"Got: {list(new_weights.keys())}")
            skipped += 1
            continue

        proj_model_keys = {}
        all_found = True
        for proj in PROJ_KEYS:
            prefix    = detect_key_prefix(tensors.keys(), layer_idx, proj)
            model_key = f"{prefix}layers.{layer_idx}.mlp.{proj}"
            if model_key not in tensors:
                print(f"  [skip] Key not found in model: {model_key!r}")
                all_found = False
                break
            proj_model_keys[proj] = model_key

        if not all_found:
            skipped += 1
            continue

        gate_key = proj_model_keys["gate_proj.weight"]
        up_key   = proj_model_keys["up_proj.weight"]
        down_key = proj_model_keys["down_proj.weight"]

        orig_gate_dtype = tensors[gate_key].dtype
        orig_down_dtype = tensors[down_key].dtype

        gate_w = tensors[gate_key].clone().float()
        up_w   = tensors[up_key].clone().float()
        down_w = tensors[down_key].clone().float()

        if not dry_run:
            fuse_layer_deltas(layer_idx, gate_w, up_w, down_w, new_weights, args)
            tensors[gate_key] = gate_w.to(orig_gate_dtype)
            # up_w unchanged by SCF -- no write-back needed
            tensors[down_key] = down_w.to(orig_down_dtype)

        fused += 1
        print(f"  {'[dry]' if dry_run else '[ok]'} Fused layer {layer_idx:02d} [{layer_type}]"
              f"  gate*gamma + down contoured (up unchanged)")

    if skipped > 0 and fused == 0:
        raise RuntimeError(
            f"No layers were fused -- all {skipped} layer(s) were skipped.\n"
            f"Sample model keys: {list(tensors.keys())[:4]}"
        )
    if skipped > 0:
        print(f"  [warn] {skipped} layer(s) skipped, {fused} fused.")

    if not dry_run:
        out_path = output_dir / model_path.name
        save_file(tensors, str(out_path))
        print(f"  Saved -> {out_path.resolve()}")

    return fused


# ---------------------------------------------------------------------------
# Sharded apply
# ---------------------------------------------------------------------------

def apply_sharded(model_dir: Path, output_dir: Path, layer_files: dict, args) -> int:
    dry_run    = args.dry_run
    index_path = model_dir / "model.safetensors.index.json"
    if not index_path.exists():
        raise FileNotFoundError(f"Sharded index missing: {index_path}")

    with open(index_path) as f:
        index = json.load(f)
    weight_map = index["weight_map"]

    # Per-projection fusion plan keyed by shard.
    # Each entry: (layer_idx, proj, model_key, delta_tensor, layer_type).
    # A layer whose projections span multiple shards will appear in several
    # shard buckets — one entry per projection — instead of being skipped.
    fusion_plan: dict = {}
    skipped = 0

    for layer_idx, layer_path in sorted(layer_files.items()):
        layer_type = "global" if is_global_attention_layer(layer_idx) else "swa"

        new_weights = load_file(str(layer_path))

        if not any(k in new_weights for k in PROJ_KEYS):
            print(f"  [skip] Layer {layer_idx}: none of {PROJ_KEYS} found. "
                  f"Got: {list(new_weights.keys())}")
            skipped += 1
            continue

        proj_registered = 0
        for proj in PROJ_KEYS:
            if proj not in new_weights:
                continue
            prefix    = detect_key_prefix(weight_map.keys(), layer_idx, proj)
            model_key = f"{prefix}layers.{layer_idx}.mlp.{proj}"
            if model_key not in weight_map:
                print(f"  [skip] Layer {layer_idx}: {model_key!r} not in weight_map")
                continue
            shard_name = weight_map[model_key]
            fusion_plan.setdefault(shard_name, []).append(
                (layer_idx, proj, model_key, new_weights[proj], layer_type)
            )
            proj_registered += 1

        if proj_registered == 0:
            skipped += 1

    if not fusion_plan:
        sample = list(weight_map.keys())[:6]
        raise RuntimeError(
            f"No layers matched in weight_map. Sample keys: {sample}"
        )

    # Identify which shards will be modified so we can copy non-modified files lazily.
    modified_shards = set(fusion_plan.keys())

    if not dry_run:
        output_dir.mkdir(parents=True, exist_ok=True)
        # Copy all non-shard files (config, tokenizer, index, etc.) eagerly.
        # Shard files are copied individually just before they are modified,
        # avoiding a full model copy upfront that can exhaust RAM and disk I/O.
        for src_file in model_dir.iterdir():
            dst_file = output_dir / src_file.name
            if src_file.name not in modified_shards:
                if src_file.is_dir():
                    shutil.copytree(src_file, dst_file, dirs_exist_ok=True)
                else:
                    shutil.copy2(src_file, dst_file)
        # Copy unmodified shards (they just need to be present in the output).
        all_shards = {v for v in weight_map.values()}
        for shard_name in all_shards - modified_shards:
            src = model_dir / shard_name
            dst = output_dir / shard_name
            if src.exists() and not dst.exists():
                shutil.copy2(src, dst)

    fused_layer_idxs: set = set()

    for shard_name, ops in sorted(fusion_plan.items()):
        shard_src = model_dir / shard_name
        shard_dst = output_dir / shard_name

        # load_file uses memory-mapped I/O — no full f.read() into RAM
        tensors = load_file(str(shard_src))

        # Re-group by layer so fuse_layer_deltas is called once per layer per shard.
        by_layer: dict = {}
        for layer_idx, proj, model_key, delta, layer_type in ops:
            by_layer.setdefault(layer_idx, []).append((proj, model_key, delta, layer_type))

        for layer_idx, proj_ops in sorted(by_layer.items()):
            layer_type = proj_ops[0][3]

            # Deltas restricted to projections whose tensors live in this shard.
            # fuse_layer_deltas gates every block on presence in new_weights, so
            # absent projections are never touched regardless of the tensor passed.
            partial_new_weights = {proj: delta for proj, _, delta, _ in proj_ops}

            # Build weight tensors for projections present in this shard; supply
            # an empty sentinel for absent slots — they are never accessed because
            # their keys are absent from partial_new_weights.
            proj_tensors = {
                proj: (model_key, tensors[model_key].clone().float())
                for proj, model_key, _, _ in proj_ops
            }
            gate_w = proj_tensors.get("gate_proj.weight", (None, torch.empty(0)))[1]
            up_w   = proj_tensors.get("up_proj.weight",   (None, torch.empty(0)))[1]
            down_w = proj_tensors.get("down_proj.weight", (None, torch.empty(0)))[1]

            orig_dtypes = {
                proj: tensors[model_key].dtype
                for proj, model_key, _, _ in proj_ops
            }

            if not dry_run:
                fuse_layer_deltas(layer_idx, gate_w, up_w, down_w, partial_new_weights, args)
                for proj, model_key, _, _ in proj_ops:
                    if proj == "gate_proj.weight":
                        tensors[model_key] = gate_w.to(orig_dtypes[proj])
                    elif proj == "down_proj.weight":
                        tensors[model_key] = down_w.to(orig_dtypes[proj])
                    # up_proj: SCF intentionally leaves it unchanged

            fused_layer_idxs.add(layer_idx)
            proj_names = [p.split(".")[0] for p, *_ in proj_ops]
            print(f"  {'[dry]' if dry_run else '[ok]'} Fused layer {layer_idx:02d} [{layer_type}]"
                  f"  ({', '.join(proj_names)} in this shard)")

        if not dry_run:
            save_file(tensors, str(shard_dst))
            print(f"  [ok] Saved shard {shard_name} ({len(by_layer)} layer(s))")
        del tensors  # free RAM before loading next shard

    if skipped > 0:
        print(f"  [warn] {skipped} layer(s) fully skipped, "
              f"{len(fused_layer_idxs)} unique layer(s) fused.")

    return len(fused_layer_idxs)


# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------

def main():
    parser = argparse.ArgumentParser(
        description="Apply delta weights to a model via Shape-Contoured Fusion."
    )
    parser.add_argument("--model",     required=True)
    parser.add_argument("--weights",   required=True)
    parser.add_argument("--output",    required=True)
    parser.add_argument("--layers",    type=int, nargs="+", default=None)
    parser.add_argument("--dry-run",   action="store_true")
    parser.add_argument("--alpha",     type=float, default=0.02,
                        help="down-proj variance scale multiplier (default: 0.02)")
    parser.add_argument("--gamma-cap", type=float, default=0.05,
                        help="max fractional gate_proj adjustment (default: 0.05)")
    args = parser.parse_args()

    model_path  = Path(args.model)
    weights_dir = Path(args.weights)
    output_dir  = Path(args.output)

    layer_files = discover_generated_layers(weights_dir)
    if not layer_files:
        raise FileNotFoundError(
            f"No layer_*.safetensors files found in: {weights_dir.resolve()}"
        )
    if args.layers is not None:
        layer_files = {i: layer_files[i] for i in args.layers if i in layer_files}
        if not layer_files:
            available = sorted(discover_generated_layers(weights_dir).keys())
            raise ValueError(f"--layers filter empty. Available: {available}")

    print(f"[info] Found {len(layer_files)} layer file(s): indices {sorted(layer_files.keys())}")
    print(f"[info] SCF params: alpha={args.alpha}, gamma_cap={args.gamma_cap}")

    if not args.dry_run:
        output_dir.mkdir(parents=True, exist_ok=True)

    if model_path.is_file() and model_path.suffix == ".safetensors":
        apply_single_file(model_path, output_dir, layer_files, args)

    elif model_path.is_dir():
        single = model_path / "model.safetensors"
        index  = model_path / "model.safetensors.index.json"

        if single.exists() and not index.exists():
            if not args.dry_run:
                for f in model_path.iterdir():
                    if f.name != "model.safetensors":
                        dst = output_dir / f.name
                        if f.is_dir():
                            shutil.copytree(f, dst, dirs_exist_ok=True)
                        else:
                            shutil.copy2(f, dst)
            apply_single_file(single, output_dir, layer_files, args)

        elif index.exists():
            apply_sharded(model_path, output_dir, layer_files, args)

        else:
            raise FileNotFoundError(
                f"No model.safetensors or model.safetensors.index.json in {model_path}"
            )
    else:
        raise FileNotFoundError(f"--model not found: {model_path}")

    config_path = (
        model_path / "config.json"
        if model_path.is_dir()
        else model_path.parent / "config.json"
    )
    if config_path.exists() and not args.dry_run:
        shutil.copy2(config_path, output_dir / "config.json")
        print("  [ok] Copied config.json (activation unchanged).")


if __name__ == "__main__":
    main()