Rhett1 commited on
Commit
82a7adb
·
verified ·
1 Parent(s): 449c083

Upload tr3/heal/llama2-7b-base/README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. tr3/heal/llama2-7b-base/README.md +129 -0
tr3/heal/llama2-7b-base/README.md ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: llama2
3
+ base_model: NousResearch/Llama-2-7b-hf
4
+ tags:
5
+ - tensor-decomposition
6
+ - tensor-ring
7
+ - model-compression
8
+ - llama2
9
+ ---
10
+
11
+ # TR-3: LLaMA-2-7B (base) compressed with a Tensor Ring
12
+
13
+ LLaMA-2-7B base with every attention/MLP projection replaced by a **3-site Tensor Ring**
14
+ (a cyclic tensor-network factorization — the ring closes with a non-trivial boundary bond)
15
+ plus a 5% sparse residual, then healed with pure knowledge distillation.
16
+
17
+ - **4.02B params** (down from 6.74B, ~40% reduction)
18
+ - **60M healing tokens** (0.228 epoch, pure forward-KL distillation from the dense 7B base teacher)
19
+ - KL divergence to teacher: **0.6714 → 0.0899** over healing
20
+
21
+ ## Evaluation (probe only — see note)
22
+
23
+ Probe MMLU (lm-eval 0.4.12, limit=40 questions/subject, n=2280, same harness for all rows):
24
+
25
+ | Model | probe MMLU |
26
+ |---|---:|
27
+ | Dense 7B base (teacher, fp16) | 47.50% |
28
+ | TR-3 compressed, unhealed | 27.02% |
29
+ | **TR-3 compressed, healed 60M** | **41.97%** |
30
+
31
+ ⚠️ **Only probe-subset MMLU has been run on this base model.** Full-benchmark numbers
32
+ (full MMLU / HellaSwag / BoolQ / ARC / Winogrande) have intentionally not been produced for
33
+ this checkpoint; do not cite the numbers above as full-benchmark results. The evaluation
34
+ protocol used for the probe is included as `eval_protocol_llama2_7b_base.json`.
35
+
36
+ ## ⚠️ This is NOT a standard `transformers` checkpoint
37
+
38
+ The weight tensors in `state_dict_fp16.pt` are **ring cores**, not dense `nn.Linear` weights —
39
+ loading them requires reconstructing the model with the custom `TRLinear`/`SparseAugTRLinear`
40
+ layers from `tensor_formats.py` (included in this repo), using `ranks.json` for the per-layer
41
+ topology. `config.json` is included only as architecture metadata (stock LLaMA-2-7B config) —
42
+ you cannot `AutoModelForCausalLM.from_pretrained()` this repo directly.
43
+
44
+ ## How to load it
45
+
46
+ ```python
47
+ import json
48
+ import torch
49
+ from huggingface_hub import hf_hub_download
50
+ from transformers import AutoModelForCausalLM, AutoTokenizer
51
+
52
+ REPO = "Tensor-compression/TR_main"
53
+ BASE = "tr3/heal/llama2-7b-base" # path prefix inside the repo
54
+
55
+ ranks = json.loads(open(hf_hub_download(REPO, f"{BASE}/ranks.json")).read())
56
+ state_dict_path = hf_hub_download(REPO, f"{BASE}/state_dict_fp16.pt")
57
+ tensor_formats_path = hf_hub_download(REPO, f"{BASE}/tensor_formats.py")
58
+
59
+ # tensor_formats.py has no local package dependencies -- exec it into a namespace
60
+ ns = {}
61
+ exec(open(tensor_formats_path).read(), ns)
62
+ TRLinear, SparseAugTRLinear = ns["TRLinear"], ns["SparseAugTRLinear"]
63
+
64
+ def set_sub(model, path, new):
65
+ parts = path.split(".")
66
+ parent = model
67
+ for p in parts[:-1]:
68
+ parent = getattr(parent, p)
69
+ setattr(parent, parts[-1], new)
70
+
71
+ model = AutoModelForCausalLM.from_pretrained(
72
+ "NousResearch/Llama-2-7b-hf",
73
+ revision="8efe6c9b93655b934e27bd9981e3ec13e55aee9d",
74
+ torch_dtype=torch.float16,
75
+ )
76
+ for path, spec in ranks.items():
77
+ osp = (spec["o1"], spec["o2"], spec["o3"])
78
+ isp = (spec["i1"], spec["i2"], spec["i3"])
79
+ rr = (spec["r0"], spec["r1"], spec["r2"])
80
+ dims = [o * i for o, i in zip(osp, isp)]
81
+ cores = [torch.zeros(rr[k], dims[k], rr[(k + 1) % 3], dtype=torch.float16) for k in range(3)]
82
+ nnz = spec.get("nnz", 0)
83
+ layer = (SparseAugTRLinear(cores, osp, isp,
84
+ torch.zeros(nnz, dtype=torch.int64),
85
+ torch.zeros(nnz, dtype=torch.float16))
86
+ if nnz else TRLinear(cores, osp, isp))
87
+ set_sub(model, path, layer)
88
+
89
+ sd = torch.load(state_dict_path, map_location="cpu")
90
+ missing, unexpected = model.load_state_dict(
91
+ {k: (v.to(torch.float16) if v.is_floating_point() else v) for k, v in sd.items()},
92
+ strict=False)
93
+ assert not missing and not unexpected
94
+
95
+ # generation-heavy inference (e.g. batch decoding) is 100x+ faster after this --
96
+ # forward() otherwise redoes the full ring contraction on every call. Never call
97
+ # before/during training: it detaches the cache from the ring cores' gradients.
98
+ for m in model.modules():
99
+ if isinstance(m, TRLinear):
100
+ m.materialize_()
101
+
102
+ tok = AutoTokenizer.from_pretrained("NousResearch/Llama-2-7b-hf")
103
+ ```
104
+
105
+ ## Files
106
+
107
+ | File | What it is |
108
+ |---|---|
109
+ | `state_dict_fp16.pt` | Healed weights (ring-core form), fp16 |
110
+ | `ranks.json` | Per-layer ring topology (output/input mode splits + bond ranks + sparse-residual nnz) |
111
+ | `tensor_formats.py` | `TRLinear`/`SparseAugTRLinear` layer implementations + ring contraction/fitting code |
112
+ | `config.json` | Stock LLaMA-2-7B architecture config, for reference only |
113
+ | `eval_protocol_llama2_7b_base.json` | Exact harness config used for the probe MMLU reported above |
114
+
115
+ ## Provenance / method
116
+
117
+ Compression: 3-site tensor ring, TT-SVD initialization + ALS refinement (multi-restart, never
118
+ returns worse than its own TT baseline), diagonal 3-way input-activation whitening, 5% sparse
119
+ residual (activation-weighted magnitude selection). Base model pinned to
120
+ `NousResearch/Llama-2-7b-hf` @ `8efe6c9b`. Healing: pure forward-KL distillation
121
+ (alpha=1.0, T=1.0), lr=2e-5 with cosine decay, 60M tokens (32768 tokens/step, 1831 steps) on a
122
+ decontaminated alpaca/openhermes/ultrachat mixture; student in bf16, teacher in fp16.
123
+ Full method code and the complete experiment record are not included in this repo.
124
+
125
+ ## License
126
+
127
+ Derived from Meta's LLaMA 2 (`NousResearch/Llama-2-7b-hf`, an unofficial redistribution of
128
+ the same weights). Subject to the **LLaMA 2 Community License** — see
129
+ https://ai.meta.com/llama/license/. This repo does not grant any rights beyond that license.