Upload 2 files
Browse files
quantization/__pycache__/export_gguf.cpython-314.pyc
ADDED
|
Binary file (5.62 kB). View file
|
|
|
quantization/export_gguf.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import json
|
| 3 |
+
import sys
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
from torch import nn
|
| 8 |
+
|
| 9 |
+
ROOT = Path(__file__).resolve().parents[1]
|
| 10 |
+
if str(ROOT) not in sys.path:
|
| 11 |
+
sys.path.append(str(ROOT))
|
| 12 |
+
|
| 13 |
+
from model.transformer import MarsLMConfig, MarsLMForCausalLM
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def quantize_int8(model: nn.Module) -> nn.Module:
|
| 17 |
+
try:
|
| 18 |
+
from torch.ao.quantization import quantize_dynamic
|
| 19 |
+
return quantize_dynamic(model, {nn.Linear}, dtype=torch.qint8)
|
| 20 |
+
except Exception:
|
| 21 |
+
return model
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def quantize_int4(model: nn.Module) -> dict:
|
| 25 |
+
payload = {}
|
| 26 |
+
for name, module in model.named_modules():
|
| 27 |
+
if isinstance(module, nn.Linear):
|
| 28 |
+
weights = module.weight.detach().float()
|
| 29 |
+
max_abs = weights.abs().max().clamp_min(1e-8)
|
| 30 |
+
q = torch.round(weights / max_abs * 7).clamp(-8, 7).to(torch.int8)
|
| 31 |
+
payload[name + ".weight"] = q
|
| 32 |
+
payload[name + ".scale"] = torch.tensor([max_abs.item()], dtype=torch.float32)
|
| 33 |
+
return payload
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def export_gguf(model: nn.Module, output_path: Path):
|
| 37 |
+
metadata = {
|
| 38 |
+
"format": "gguf-stub",
|
| 39 |
+
"description": "This export is a scaffold for llama.cpp / Ollama integration.",
|
| 40 |
+
}
|
| 41 |
+
output_path.parent.mkdir(parents=True, exist_ok=True)
|
| 42 |
+
output_path.write_text(json.dumps(metadata, indent=2), encoding="utf-8")
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def main():
|
| 46 |
+
parser = argparse.ArgumentParser(description="Export a MarsLM checkpoint for quantized inference")
|
| 47 |
+
parser.add_argument("--model-path", required=True)
|
| 48 |
+
parser.add_argument("--output", required=True)
|
| 49 |
+
parser.add_argument("--quant-type", choices=["int8", "int4", "gguf"], default="int8")
|
| 50 |
+
args = parser.parse_args()
|
| 51 |
+
|
| 52 |
+
model_path = Path(args.model_path).resolve()
|
| 53 |
+
output_path = Path(args.output).resolve()
|
| 54 |
+
state = torch.load(model_path, map_location="cpu")
|
| 55 |
+
|
| 56 |
+
config = MarsLMConfig()
|
| 57 |
+
model = MarsLMForCausalLM(config)
|
| 58 |
+
if isinstance(state, dict) and "model_state" in state:
|
| 59 |
+
model.load_state_dict(state["model_state"])
|
| 60 |
+
else:
|
| 61 |
+
model.load_state_dict(state)
|
| 62 |
+
|
| 63 |
+
output_path.parent.mkdir(parents=True, exist_ok=True)
|
| 64 |
+
if args.quant_type == "int8":
|
| 65 |
+
quantized = quantize_int8(model)
|
| 66 |
+
torch.save(quantized.state_dict(), output_path.with_suffix(".pt"))
|
| 67 |
+
print(f"Saved INT8 weights to {output_path.with_suffix('.pt')}")
|
| 68 |
+
elif args.quant_type == "int4":
|
| 69 |
+
payload = quantize_int4(model)
|
| 70 |
+
torch.save(payload, output_path.with_suffix(".pt"))
|
| 71 |
+
print(f"Saved INT4 weights to {output_path.with_suffix('.pt')}")
|
| 72 |
+
else:
|
| 73 |
+
export_gguf(model, output_path)
|
| 74 |
+
print(f"Wrote GGUF metadata stub to {output_path}")
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
main()
|