#!/usr/bin/env python3 """Convert native Gab weights (a headerless flat float32 buffer in MiniGPT.js parameters() order) to model.safetensors with Transformers-style names. MiniGPT.js stores matrices as [in, out] row-major; nn.Linear wants [out, in]. The per-head Wq/Wk/Wv/Wo are fused into one [768, 768] matrix each: Q/K/V by stacking the heads' outputs, O by stacking the heads' inputs (summing per-head output projections equals one projection of the concatenated heads). Usage: convert.py gab1b.weights [config.json] [model.safetensors] """ import json import sys import numpy as np from safetensors.numpy import save_file def convert(weights_path, config_path="config.json", out_path="model.safetensors"): c = json.load(open(config_path)) d, nh, hd = c["hidden_size"], c["num_attention_heads"], c["head_dim"] ne, h, v = c["num_experts"], c["intermediate_size"], c["vocab_size"] flat = np.fromfile(weights_path, dtype=" {out_path} ({len(t)} tensors)") if __name__ == "__main__": convert(*sys.argv[1:])