-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_to_hardware.py
More file actions
295 lines (248 loc) · 13.2 KB
/
Copy pathexport_to_hardware.py
File metadata and controls
295 lines (248 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
import hydra
import jax
import jax.numpy as jnp
import equinox as eqx
import numpy as np
import omegaconf
import torch
from tqdm import tqdm
import pickle
import subprocess
import tempfile
import sys
from dataset_loaders.dataset_utils import init_torch_dataloader
from model import quantization
from model.dropout import PartialLayerDropoutMask
from model.timeseries_decoder import TimeseriesPatchedDecoder
from model.vit import VIT
from trainer import get_best_model_name
from utils import export_utils
from utils.splitting_utils import get_neuron_slices, get_weight_slices
import os
import logging
# Convert scalings to pure Python types (nested dicts with floats)
def convert_to_python(obj):
"""Recursively convert to pure Python types."""
if isinstance(obj, dict):
return {k: convert_to_python(v) for k, v in obj.items()}
elif isinstance(obj, (list, tuple)):
return [convert_to_python(item) for item in obj]
elif hasattr(obj, '__array__'): # JAX or NumPy array
arr = np.array(obj)
if arr.size == 1: # Scalar
return float(arr.item())
return arr
elif isinstance(obj, (np.integer, np.floating)):
return float(obj)
else:
return obj
def run_export(model, X, scalings_weight, scalings_activation, cfg, path_output):
# Convert all JAX/Equinox data to pure NumPy
model_weight_dict_numpy = jax.tree.map(lambda x: np.array(x), model.get_weight_dict())
pruning_state_dict = jax.tree.map(lambda x: np.array(x), model.get_pruning_state_dict())
example_input_numpy = np.array(X)
scalings_weight_python = convert_to_python(scalings_weight)
scalings_activation_python = convert_to_python(scalings_activation)
# o_split_input_python = [int(x) for x in model.decoder.multi_head_attention_layers[0].o_split_input]
# Prepare data for export
export_data = {
'name': 'decoder',
'weight_dict': model_weight_dict_numpy,
'pruning_dict': pruning_state_dict,
'num_heads': int(model.decoder.multi_head_attention_layers[0].num_heads),
'o_split_input': model.decoder.multi_head_attention_layers[0].o_split_input,
'scalings_weight': scalings_weight_python,
'scalings_activation': scalings_activation_python,
'num_devices': int(cfg.num_devices),
'example_input': example_input_numpy,
'num_bits': 8,
'attention_only': False,
'path_output': path_output
}
# Save to pickle file
pickle_dir = "/data/distributed_transformer/export_temp"
os.makedirs(pickle_dir, exist_ok=True)
pickle_path = os.path.join(pickle_dir, f"export_data_{cfg.num_devices}_{int(cfg.per_step_pruning_ratio * cfg.pruning_step*100)}.pkl")
with open(pickle_path, 'wb') as f:
pickle.dump(export_data, f)
# Call the standalone export script via subprocess
script_path = os.path.join(os.path.dirname(__file__), "export_standalone.py")
result = subprocess.run(
[sys.executable, script_path, pickle_path],
capture_output=True,
text=True
)
# Print output
# if result.stdout:
# print(result.stdout)
# if result.stderr:
# print("STDERR:", result.stderr)
if result.returncode != 0:
if result.stdout:
print("stdout:")
print(result.stdout)
if result.stderr:
print("STDERR:", result.stderr)
print(f"Export script failed with return code {result.returncode}")
sys.exit(1)
@hydra.main(config_path="parameters", config_name="main", version_base="1.1")
def main(cfg: omegaconf.OmegaConf):
logger = logging.getLogger(__name__)
if False:
jax.config.update('jax_platform_name', 'cpu')
key = jax.random.PRNGKey(1)
keys = jax.random.split(key, 3)
model = TimeseriesPatchedDecoder(1, cfg, keys[0], apply_padding=False)
print(f"Number parameters: {model.get_number_of_parameters()}")
input_dim = cfg.model.input_patch_length*1
X = 2 * jax.random.uniform(keys[2], (cfg.dataset.context_length // cfg.model.input_patch_length, input_dim)) - 1.0
X_flattened = jnp.array([X.flatten()]).T
# Example usage
if cfg.pruning_ratio > 0.0:
model = model.prune_step(cfg.pruning_ratio)
# model = model.change_dropout_probability(0.1, 0.1, 0.1, 0.0)
# def _print(x):
# if isinstance(x, PartialLayerDropoutMask):
# assert x.dropout_prob_mode1 == 0.1
# assert x.dropout_prob_mode2 == 0.1
# assert x.dropout_prob_mode3 == 0.1
# return x
# jax.tree_map(_print, model, is_leaf=lambda x: isinstance(x, PartialLayerDropoutMask))
# Y, record_activations = jax.vmap(residual_block, (0, None, None, None, None, None))(X, None, None, True, keys[0], True)
Y, record_activations = model(X_flattened, None, None, None, True, keys[0])
record_weights = model.get_weight_dict()
scalings_activation = quantization.calculate_scalings(record_activations, num_bits=8)
scalings_weight = quantization.calculate_scalings(record_weights, num_bits=8)
path_output = f"/data/distributed_transformer/code_export/code_{cfg.num_devices}_{int(cfg.pruning_ratio*100)}"
os.makedirs(path_output, exist_ok=True)
export_utils.export_transformer("decoder",
transformer=model.decoder,
scalings_weight=scalings_weight,
scalings_activation=scalings_activation,
num_devices=cfg.num_devices,
example_input=X,
num_bits=8,
attention_only=True,
path_output=path_output)
# test
Y, _ = model(X_flattened, None, scalings_weight, scalings_activation, True, keys[0])
if type(Y) is tuple:
Y = Y[0]
print(quantization.quantize_forward(Y, scalings_activation["out"]["sum"], rounding=True))
else:
jax.config.update('jax_platform_name', 'cpu')
key = jax.random.PRNGKey(1)
keys = jax.random.split(key, 3)
torch.manual_seed(1)
np.random.seed(1)
path_output = f"/data/distributed_transformer/code_export/cat_vs_dog/code_{cfg.num_devices}_{int(cfg.per_step_pruning_ratio * cfg.pruning_step*100)}"
os.makedirs(path_output, exist_ok=True)
logger.info("Loading model...")
model = VIT(cfg, keys[0])
if cfg.per_step_pruning_ratio > 0.0:
model = model.prune_step(cfg.per_step_pruning_ratio)
model_path = "/data/distributed_transformer/cat_vs_dog_pruning2/cat_vs_dog/12_384_384_False/11/checkpoints"
model_name = f"best_{int(cfg.per_step_pruning_ratio * cfg.pruning_step*100):03d}"
#model = model.load_model(f"{model_path}/{model_name}", absolute_path=True)
logger.info(f"Model loaded, number parameters: {model.get_number_of_parameters()}")
exit()
logger.info("Starting calibration...")
# load dataset
data_source = hydra.utils.instantiate(cfg.dataset)
train_dataset, train_sampler = init_torch_dataloader(data_source.get_train_data_source(), 64, cfg.num_workers, cfg.do_distributed_training, 1)
val_dataset, val_sampler = init_torch_dataloader(data_source.get_val_data_source(), 64, cfg.num_workers, cfg.do_distributed_training, 1)
test_dataset, test_sampler = init_torch_dataloader(data_source.get_test_data_source(), 2, cfg.num_workers, cfg.do_distributed_training, 1)
@eqx.filter_jit
def predict_batch(model,
batch,
weight_scalings,
activation_scalings,
inference,
key):
if type(model) == TimeseriesPatchedDecoder:
subkeys = jax.random.split(key, len(batch["target"]))
pred = jax.vmap(model, (0, 0 if batch["padding_mask"] is not None else None, None, None, None, 0))(batch["target"][:, 0:cfg.dataset.context_length, ...],
batch["padding_mask"],
weight_scalings,
activation_scalings,
inference,
subkeys)
return pred
elif type(model) == VIT:
subkeys = jax.random.split(key, len(batch["image"]))
pred = jax.vmap(model, in_axes=(0, None, None, None, 0))(batch["image"],
weight_scalings,
activation_scalings,
inference,
subkeys)
return pred
else:
raise NotImplementedError(f"Model type {type(model)} not implemented in predict_batch.")
train_dataset_iterator = iter(train_dataset)
scalings_activation = None
for i in tqdm(range(10), desc="Calibration"):
batch = next(train_dataset_iterator)
if type(batch["target"]) is not np.ndarray:
batch = {k: np.asarray(v) for k, v in batch.items()}
Y, record_activations = predict_batch(model,
batch,
None,
None,
True,
keys[0])
scalings_activation_temp = quantization.calculate_scalings(record_activations, num_bits=8)
if scalings_activation is None:
scalings_activation = scalings_activation_temp
else:
scalings_activation = jax.tree_util.tree_map(lambda x, y: jnp.minimum(x, y), scalings_activation, scalings_activation_temp)
scalings_weight = quantization.calculate_scalings(model.get_weight_dict(), num_bits=8)
logger.info("Calibration done.")
# Example usage
val_dataset_iterator = iter(val_dataset) # iter(test_dataset) # iter(val_dataset)
batch = next(val_dataset_iterator)
batch = {k: np.asarray(v) for k, v in batch.items()}
Y, _ = predict_batch(model,
batch,
scalings_weight,
scalings_activation,
True,
keys[0])
# Plot 8x8 grid of images with predictions and true classes
import matplotlib.pyplot as plt
fig, axes = plt.subplots(8, 8, figsize=(16, 16))
axes = axes.flatten()
num_images = min(64, len(batch["image"]))
predictions = jnp.argmax(Y, axis=-1) if len(Y.shape) > 1 else Y
correct = jnp.sum(predictions == batch["target"])
accuracy = correct / num_images
logger.info(f"Accuracy on batch: {accuracy:.2%} ({correct}/{num_images})")
for idx in range(num_images):
ax = axes[idx]
img = torch.tensor(batch["image"][idx])
img = img * torch.tensor([0.229, 0.224, 0.225]) + torch.tensor([0.485, 0.456, 0.406])
img = torch.clamp(img, 0, 1)
pred_class = int(predictions[idx])
true_class = int(batch["target"][idx])
color = 'green' if pred_class == true_class else 'red'
ax.imshow(img)
ax.set_title(f"Pred: {pred_class}\nTrue: {true_class}\n{quantization.quantize_forward(Y[idx, ...], scalings_activation["out"]["sum"], rounding=True)}", color=color, fontsize=8)
ax.axis('off')
# Hide unused subplots
for idx in range(num_images, 64):
axes[idx].axis('off')
plt.tight_layout()
plt.savefig(os.path.join(path_output, 'predictions_grid.png'), dpi=150, bbox_inches='tight')
plt.close()
logger.info(f"Saved predictions grid to {os.path.join(path_output, 'predictions_grid.png')}")
print(batch["image"][0, ...].shape)
print("----------------------------------------------------")
Y_, _ = model(jnp.array(batch["image"][0, ...]), scalings_weight, scalings_activation, True, keys[0], True)
print(quantization.quantize_forward(Y_[idx, ...], scalings_activation["out"]["sum"], rounding=True))
X = model.patchify_input(jnp.array(batch["image"][0, ...]))
#exit()
print(X.shape)
X = jnp.concatenate((X, model.decoder.cls_token), axis=0)
run_export(model, X, scalings_weight, scalings_activation, cfg, path_output)
logger.info("Export done.")
if __name__ == "__main__":
main()