"""Reconstruct BF16 weights from the native quantized Inkling 2 Mini release. Requires the MLX-VLM revision and MLX version documented in the model card. This is dequantization, not recovery of original pre-quantization weights. """ import argparse import shutil from pathlib import Path import mlx.core as mx from mlx.utils import tree_unflatten from mlx_vlm.models.switch_layers import SwitchLinear from mlx_vlm.quantization.nvfp4 import ScaledQuantizedSwitchLinear from mlx_vlm.utils import fetch_from_hub, get_model_path, save_config, save_weights parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--source', default='thinkingmachines/Inkling-2-Mini') parser.add_argument('--revision', default='c9d028302cd0f035fdf8e7e91f089a4ce94d2195') parser.add_argument('--output', required=True) args = parser.parse_args() source = get_model_path(args.source, revision=args.revision) output = Path(args.output) output.mkdir(parents=True, exist_ok=True) mx.set_cache_limit(0) model, config, processor = fetch_from_hub(source, lazy=True) paths = [path for path, module in model.named_modules() if hasattr(module, 'scales')] assert len(paths) == 72 for path in paths: old = dict(model.named_modules())[path] weight = mx.dequantize( old.weight, old.scales, old.get('biases'), old.group_size, old.bits, old.mode, dtype=mx.float32, ) if isinstance(old, ScaledQuantizedSwitchLinear): scale = old.global_scale weight = old.apply_scale( weight, scale.reshape(scale.shape + (1,) * (weight.ndim - scale.ndim)) ) weight = weight.astype(mx.bfloat16) mx.eval(weight) if not bool(mx.all(mx.isfinite(weight)).item()): raise ValueError(f'Non-finite reconstructed weights in {path}') new = SwitchLinear(weight.shape[2], weight.shape[1], weight.shape[0], bias='bias' in old) new.weight = weight if 'bias' in old: new.bias = old.bias model.update_modules(tree_unflatten([(path, new)])) del old, new, weight mx.clear_cache() for key in ('quantization', 'quantization_config'): config.pop(key, None) config['dtype'] = 'bfloat16' save_weights(output, model, donate_weights=True) save_config(config, output / 'config.json') for filename in ( 'chat_template.jinja', 'generation_config.json', 'mtp.safetensors', 'processor_config.json', 'special_tokens_map.json', 'tokenizer.json', 'tokenizer_config.json', 'tiktoken/tokenizer.model', ): target = output / filename target.parent.mkdir(parents=True, exist_ok=True) shutil.copyfile(source / filename, target) shutil.copyfile(source / 'hf_quant_config.json', output / 'source_hf_quant_config.json') print(output)