forked from vllm-project/llm-compressor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreap_moonlight_16b.py
More file actions
42 lines (36 loc) · 1.55 KB
/
Copy pathreap_moonlight_16b.py
File metadata and controls
42 lines (36 loc) · 1.55 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
# NOTE: to use a custom dataset, see examples/custom_dataset_example.py
from compressed_tensors.offload import dispatch_model
from transformers import AutoModelForCausalLM, AutoTokenizer
from llmcompressor import oneshot
from llmcompressor.modifiers.pruning import REAPPruningModifier
# Select model and load it.
# Note: for the Moonlight model, you may need to install the
# `tiktoken` package for the tokenizer.
model_id = "moonshotai/Moonlight-16B-A3B-Instruct"
model = AutoModelForCausalLM.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)
# Prune 25% of the experts in each MoE layer, based on saliency.
# You can adjust this value to prune more or less aggressively.
recipe = REAPPruningModifier(sparsity=0.25)
# Apply algorithms.
oneshot(
model=model,
dataset="perfectblend",
recipe=recipe,
max_seq_length=2048,
num_calibration_samples=512,
moe_calibrate_all_experts=False, # Disable calibrating all experts for REAP
)
# Confirm generations of the compressed model look sane.
print("\n\n")
print("========== SAMPLE GENERATION ==============")
dispatch_model(model)
sample = tokenizer("Hello my name is", return_tensors="pt")
sample = {key: value.to(model.device) for key, value in sample.items()}
output = model.generate(**sample, max_new_tokens=100)
print(tokenizer.decode(output[0]))
print("==========================================\n\n")
# Save to disk compressed.
SAVE_DIR = model_id.rstrip("/").split("/")[-1] + "-REAP-25"
model.save_pretrained(SAVE_DIR, save_compressed=True)
tokenizer.save_pretrained(SAVE_DIR)