Angshul commited on
Commit
f8cf4de
·
verified ·
1 Parent(s): ff02904

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +189 -3
  2. UPLOAD_TO_HF.txt +7 -0
  3. VERSION.txt +1 -0
  4. pyproject.toml +27 -0
README.md CHANGED
@@ -1,3 +1,189 @@
1
- ---
2
- license: h-research
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # LayerInteractions
2
+
3
+ A small Python API for **order-2 interaction-aware transformer depth pruning** of Hugging Face causal language models.
4
+
5
+ This repository packages the method used in the accompanying experiments. It is source code, not a dataset and not a web service.
6
+
7
+ ## Method
8
+
9
+ Let `D0` be the calibration NLL of the full model, `Di` the NLL after deleting layer `i`, and `Dij` the NLL after deleting layers `i` and `j`. We form
10
+
11
+ ```text
12
+ a_i = D_i - D_0
13
+
14
+ a_ij = D_ij - D_i - D_j + D_0
15
+ ```
16
+
17
+ For a current deleted set `S`, the predicted marginal cost of deleting layer `i` is
18
+
19
+ ```text
20
+ a_i + sum_{j in S} a_ij.
21
+ ```
22
+
23
+ The greedy selector repeatedly deletes the layer with the smallest marginal predicted NLL increase. The implementation measures all single and pair deletions, checkpoints after every measurement, constructs the quadratic surrogate, and returns the retained layer set at any target depth.
24
+
25
+ ## Install directly from Hugging Face
26
+
27
+ After uploading this folder to a Hugging Face repository:
28
+
29
+ ```bash
30
+ pip install "git+https://huggingface.co/<HF_USERNAME>/LayerInteractions"
31
+ ```
32
+
33
+ Or clone and install locally:
34
+
35
+ ```bash
36
+ git clone https://huggingface.co/<HF_USERNAME>/LayerInteractions
37
+ cd LayerInteractions
38
+ pip install .
39
+ ```
40
+
41
+ For the example scripts:
42
+
43
+ ```bash
44
+ pip install ".[examples]"
45
+ ```
46
+
47
+ ## API
48
+
49
+ ```python
50
+ from datasets import load_dataset
51
+ from layer_interactions import Order2Pruner
52
+
53
+ texts = [
54
+ x["text"]
55
+ for x in load_dataset(
56
+ "Salesforce/wikitext", "wikitext-2-raw-v1", split="train"
57
+ )
58
+ if x["text"].strip()
59
+ ]
60
+
61
+ with Order2Pruner(
62
+ "allenai/OLMoE-1B-7B-0924",
63
+ dtype="bfloat16",
64
+ ) as pruner:
65
+ result = pruner.fit(
66
+ texts=texts,
67
+ n_sequences=32,
68
+ sequence_length=128,
69
+ seed=42,
70
+ checkpoint_path="order2_measurements.json",
71
+ max_delete=12,
72
+ )
73
+
74
+ print(result.select(8))
75
+ print(result.select(4))
76
+ ```
77
+
78
+ The output of `select()` is
79
+
80
+ ```python
81
+ {
82
+ "retained_layers": [...],
83
+ "deleted_layers": [...],
84
+ }
85
+ ```
86
+
87
+ ## One-call pruning
88
+
89
+ ```python
90
+ with Order2Pruner("allenai/OLMoE-1B-7B-0924") as pruner:
91
+ model, result = pruner.prune(
92
+ 8,
93
+ texts=texts,
94
+ checkpoint_path="order2_measurements.json",
95
+ )
96
+ ```
97
+
98
+ The returned `model` has the selected transformer blocks removed in place.
99
+
100
+ ## Save a pruned Hugging Face checkpoint
101
+
102
+ ```python
103
+ pruner.save_pruned(
104
+ "OLMoE-Order2-8",
105
+ target_layers=8,
106
+ result=result,
107
+ )
108
+ ```
109
+
110
+ This writes the model, tokenizer and `layer_interaction_selection.json` using the normal Hugging Face `save_pretrained()` format.
111
+
112
+ ## Resuming long measurements
113
+
114
+ Pair measurements are the expensive part. Pass a checkpoint path:
115
+
116
+ ```python
117
+ result = pruner.fit(
118
+ texts=texts,
119
+ checkpoint_path="order2_measurements.json",
120
+ resume=True,
121
+ )
122
+ ```
123
+
124
+ The JSON file is updated after the baseline, every single-layer deletion, and every pair deletion. Re-running resumes from completed measurements.
125
+
126
+ For a depth-`L` model, order-2 requires `L(L-1)/2` pair measurements. For example:
127
+
128
+ ```text
129
+ L = 30 -> 435 pairs
130
+ L = 16 -> 120 pairs
131
+ ```
132
+
133
+ ## Memory management
134
+
135
+ By default, the API uses Hugging Face/Accelerate `device_map="auto"`. On CUDA, it reserves approximately 4 GiB of free GPU memory as activation headroom and makes the remaining memory available for weights. The balance can be controlled explicitly:
136
+
137
+ ```python
138
+ pruner = Order2Pruner(
139
+ model_id,
140
+ gpu_memory_gib=16,
141
+ cpu_memory_gib=32,
142
+ activation_headroom_gib=4,
143
+ )
144
+ ```
145
+
146
+ The NLL is computed manually from the logits rather than by passing `labels=...`; this keeps the scoring path compatible with CPU/GPU-dispatched models.
147
+
148
+ ## Supported model layout
149
+
150
+ Automatic layer discovery currently recognizes the common paths:
151
+
152
+ ```text
153
+ model.layers
154
+ transformer.h
155
+ gpt_neox.layers
156
+ ```
157
+
158
+ For another architecture, supply the path explicitly:
159
+
160
+ ```python
161
+ Order2Pruner(model_id, layer_path="decoder.layers")
162
+ ```
163
+
164
+ The DeepSeek-LLM-7B and OLMoE examples use the standard `model.layers` layout.
165
+
166
+ ## Examples
167
+
168
+ - `examples/deepseek_7b.py`: 30 -> 15 and 30 -> 7.
169
+ - `examples/olmoe_7b.py`: 16 -> 8 and 16 -> 4.
170
+
171
+ ## Public objects
172
+
173
+ ```python
174
+ from layer_interactions import Order2Pruner, Order2Result
175
+ ```
176
+
177
+ `Order2Result` exposes:
178
+
179
+ ```text
180
+ baseline_nll
181
+ single_nll
182
+ pair_nll
183
+ first_order
184
+ second_order
185
+ delete_order
186
+ greedy_path
187
+ ```
188
+
189
+ and can be saved or loaded as JSON.
UPLOAD_TO_HF.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ Create a Hugging Face repository named LayerInteractions and upload the CONTENTS of this folder.
2
+
3
+ This is a code-only Python package. It does not require a Space, endpoint, server, database, or dataset repository.
4
+
5
+ After upload, users can install it with:
6
+
7
+ pip install "git+https://huggingface.co/<HF_USERNAME>/LayerInteractions"
VERSION.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ 0.1.0
pyproject.toml ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [build-system]
2
+ requires = ["setuptools>=68", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "layer-interactions"
7
+ version = "0.1.0"
8
+ description = "Second-order interaction pruning for Hugging Face causal language models"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ authors = [
12
+ {name = "Angshul Majumdar"}
13
+ ]
14
+ dependencies = [
15
+ "torch>=2.2",
16
+ "transformers>=4.45",
17
+ "accelerate>=1.0",
18
+ "psutil>=5.9"
19
+ ]
20
+
21
+ [project.optional-dependencies]
22
+ examples = ["datasets>=3.0"]
23
+ test = ["pytest>=8.0"]
24
+
25
+ [tool.setuptools.packages.find]
26
+ where = ["."]
27
+ include = ["layer_interactions*"]