CompressedGemma commited on
Commit
b36e2fc
·
verified ·
1 Parent(s): 6616aa9

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +68 -0
README.md CHANGED
@@ -2,3 +2,71 @@
2
  license: mit
3
  ---
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  license: mit
3
  ---
4
 
5
+ # 🛠️ Gemma 4 31B SwiGLU Fusion Toolkit
6
+
7
+ This toolkit provides a robust pipeline for generating and injecting custom, functionally correct MLP weights into a Gemma 4 31B safetensors model. It is specifically designed to handle Gemma 4's unique architectural features—namely its interleaved sliding-window attention (SWA) and global full-context layers—while preserving model stability through advanced alignment and fusion techniques.
8
+
9
+ ## ✨ Features
10
+
11
+ * **Targeted Architecture:** Explicitly accounts for Gemma 4's interleaved attention structure (5 SWA + 1 Global per period).
12
+ * **Double-Wide MLP Support:** Automatically detects and generates intermediate sizes for global full-context layers.
13
+ * **Sign-Symmetric Alignment:** Generates weights using paired frame vectors ($+gc+gc$ and $-gc-gc$) to ensure the net activation remains zero-centered, preventing distribution shifts.
14
+ * **Shape-Contoured Fusion (SCF):** Instead of naive addition, the fusion process smoothly merges synthetic weights into the model's existing learned contours, minimizing destabilization.
15
+ * **Controlled Adjustment:** Implements dynamic scaling ($\alpha$) and clamping ($\gamma$-cap) to control the magnitude of weight updates.
16
+ * **Selective Fusion:** Allows users to target specific layers for modification.
17
+
18
+ ## ⚙️ Pipeline Overview
19
+
20
+ The toolkit operates in two stages, assuming you have generated the source Neuron and derivitive Neurons which are re-usable:
21
+
22
+ ### 1. The Generator (`weights.py`)
23
+
24
+ This script creates synthetic, functionally correct MLP projections (`gate_proj`, `up_proj`, `down_proj`) based on reference source neurons. It maps piece-wise linear behaviors into the non-linear SwiGLU block without introducing mean-shift destabilization.
25
+
26
+ * **Key Function:** Generates balanced, sign-symmetric delta weights.
27
+ * **Output:** Saves the generated delta weights to an output directory.
28
+
29
+ ### 2. The Fuser (`applyweights.py`)
30
+
31
+ This script applies the safetensor delta files generated by `weights.py` to your existing Gemma 4 31B model using Shape-Contoured Fusion (SCF).
32
+
33
+ * **`down_proj` (Contoured Multiplicative Delta):** The update is scaled by the existing weight profile and variance-normalized to ensure smooth integration.
34
+ $$\text{W}_{\text{down}} = \text{W}_{\text{down}} + (\alpha_{\text{dynamic}} \cdot \Delta_{\text{down}} \cdot \text{W}_{\text{down}})$$
35
+ * **`gate_proj` (Multiplicative Gamma Scaling):** Applies a clamped fractional adjustment to prevent unbounded activation spikes.
36
+ $$\text{W}_{\text{gate}} = \text{W}_{\text{gate}} \cdot (1 + \text{clamp}(\Delta_{\text{gate}}, -\gamma, \gamma))$$
37
+ * **`up_proj` (Untouched):** The linear value path is intentionally skipped during fusion.
38
+
39
+ ## 🚀 Usage
40
+
41
+ ### Stage 1: Generating Delta Weights
42
+
43
+ Use `weights.py` to generate the synthetic delta weights.
44
+
45
+ ```bash
46
+ python weights.py \
47
+ --base-model /path/to/original_gemma_model \
48
+ --output-dir generated_weights \
49
+ --seed 42
50
+ ```
51
+
52
+ ### Stage 2: Applying Weights (Fusion)
53
+
54
+ Use `applyweights.py` to merge the generated weights into your target model.
55
+
56
+ ```bash
57
+ python applyweights.py \
58
+ --model /path/to/original_gemma_model \
59
+ --weights generated_weights \
60
+ --output /path/to/fused_gemma_model \
61
+ --alpha 0.02 \
62
+ --gamma-cap 0.05
63
+ ```
64
+
65
+ ## 💡 Helpful Flags
66
+
67
+ | Flag | Description | Default |
68
+ | :--- | :--- | :--- |
69
+ | `--dry-run` | Simulates the fusion process, identifying missing keys, calculating partial coverage matrices, and verifying sharded indexing without writing files. | N/A |
70
+ | `--layers` | A space-separated list of specific layer indices to fuse (e.g., `--layers 5 11 17`), skipping the rest. | None |
71
+ | `--alpha` | Controls the variance scale multiplier for the `down_proj` update. | `0.02` |
72
+ | `--gamma-cap` | Sets the maximum fractional adjustment allowed for the `gate_proj`. | `0.05` |