Download quantization/export_gguf.py from Ian26/MarsLM: direct link, hf CLI and curl.
- Browser
- Download file 2.69 kB
-
https://huggingface.co/Ian26/MarsLM/resolve/main/quantization/export_gguf.py
- Command line
-
hf download hf://Ian26/MarsLM/quantization/export_gguf.py
-
curl -L -o export_gguf.py https://huggingface.co/Ian26/MarsLM/resolve/main/quantization/export_gguf.py
2.69 kB
| import argparse | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import torch | |
| from torch import nn | |
| ROOT = Path(__file__).resolve().parents[1] | |
| if str(ROOT) not in sys.path: | |
| sys.path.append(str(ROOT)) | |
| from model.transformer import MarsLMConfig, MarsLMForCausalLM | |
| def quantize_int8(model: nn.Module) -> nn.Module: | |
| try: | |
| from torch.ao.quantization import quantize_dynamic | |
| return quantize_dynamic(model, {nn.Linear}, dtype=torch.qint8) | |
| except Exception: | |
| return model | |
| def quantize_int4(model: nn.Module) -> dict: | |
| payload = {} | |
| for name, module in model.named_modules(): | |
| if isinstance(module, nn.Linear): | |
| weights = module.weight.detach().float() | |
| max_abs = weights.abs().max().clamp_min(1e-8) | |
| q = torch.round(weights / max_abs * 7).clamp(-8, 7).to(torch.int8) | |
| payload[name + ".weight"] = q | |
| payload[name + ".scale"] = torch.tensor([max_abs.item()], dtype=torch.float32) | |
| return payload | |
| def export_gguf(model: nn.Module, output_path: Path): | |
| metadata = { | |
| "format": "gguf-stub", | |
| "description": "This export is a scaffold for llama.cpp / Ollama integration.", | |
| } | |
| output_path.parent.mkdir(parents=True, exist_ok=True) | |
| output_path.write_text(json.dumps(metadata, indent=2), encoding="utf-8") | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Export a MarsLM checkpoint for quantized inference") | |
| parser.add_argument("--model-path", required=True) | |
| parser.add_argument("--output", required=True) | |
| parser.add_argument("--quant-type", choices=["int8", "int4", "gguf"], default="int8") | |
| args = parser.parse_args() | |
| model_path = Path(args.model_path).resolve() | |
| output_path = Path(args.output).resolve() | |
| state = torch.load(model_path, map_location="cpu") | |
| config = MarsLMConfig() | |
| model = MarsLMForCausalLM(config) | |
| if isinstance(state, dict) and "model_state" in state: | |
| model.load_state_dict(state["model_state"]) | |
| else: | |
| model.load_state_dict(state) | |
| output_path.parent.mkdir(parents=True, exist_ok=True) | |
| if args.quant_type == "int8": | |
| quantized = quantize_int8(model) | |
| torch.save(quantized.state_dict(), output_path.with_suffix(".pt")) | |
| print(f"Saved INT8 weights to {output_path.with_suffix('.pt')}") | |
| elif args.quant_type == "int4": | |
| payload = quantize_int4(model) | |
| torch.save(payload, output_path.with_suffix(".pt")) | |
| print(f"Saved INT4 weights to {output_path.with_suffix('.pt')}") | |
| else: | |
| export_gguf(model, output_path) | |
| print(f"Wrote GGUF metadata stub to {output_path}") | |
| if __name__ == "__main__": | |
| main() | |