Rhett1 commited on
Commit
00d3b28
·
verified ·
1 Parent(s): 105140a

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

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