Download quantization/quantize.py from ramachandrajoshi/LoopedPhai-A1-35B: direct link, hf CLI and curl.
- Browser
- Download file 1.7 kB
-
https://huggingface.co/ramachandrajoshi/LoopedPhai-A1-35B/resolve/main/quantization/quantize.py
- Command line
-
hf download hf://ramachandrajoshi/LoopedPhai-A1-35B/quantization/quantize.py
-
curl -L -o quantize.py https://huggingface.co/ramachandrajoshi/LoopedPhai-A1-35B/resolve/main/quantization/quantize.py
1.7 kB
| # 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) | |