File size: 1,698 Bytes
2f04653
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Phai-A1-35B-1M -> FP8_BLOCK (compressed-tensors) with llm-compressor; data-free, keeps vision + MTP.
import os, shutil, collections, re
from transformers import AutoModelForImageTextToText, AutoProcessor
from llmcompressor import oneshot
from llmcompressor.modifiers.quantization import QuantizationModifier
from llmcompressor.utils import load_context

SRC = "src"
OUT = "out/Phai-A1-35B-1M-FP8"

with load_context(AutoModelForImageTextToText):
    model = AutoModelForImageTextToText.from_pretrained(SRC, dtype="auto", device_map="auto_offload")
print(type(model).__name__, "has mtp:", hasattr(model, "mtp"))
lin = collections.Counter(re.sub(r"\.\d+(\.|$)", r".N\1", n) for n, m in model.named_modules() if m.__class__.__name__ == "Linear")
for k, v in sorted(lin.items()): print("LINEAR", v, k)

recipe = QuantizationModifier(
    targets="Linear",
    scheme="FP8_BLOCK",
    ignore=[
        "re:.*lm_head",
        "re:.*visual.*",
        "re:.*mlp\.gate$",
        "re:.*shared_expert_gate$",
        "re:.*linear_attn\.in_proj_a$",
        "re:.*linear_attn\.in_proj_b$",
        "re:.*mtp\.fc$",
    ],
)
oneshot(model=model, recipe=recipe)
model.save_pretrained(OUT, save_compressed=True)
AutoProcessor.from_pretrained(SRC).save_pretrained(OUT)
# keep the fine-tune's original tokenizer / chat template / processor files byte-identical
for f in ["chat_template.jinja", "tokenizer.json", "tokenizer_config.json", "generation_config.json",
          "preprocessor_config.json", "processor_config.json"]:
    shutil.copy(os.path.join(SRC, f), os.path.join(OUT, f))
shutil.copy("off/video_preprocessor_config.json", os.path.join(OUT, "video_preprocessor_config.json"))
print("SAVED", OUT)