"""Export pinned Qwen3-8B with the same ONNX Runtime GenAI CPU path as Lite/Advanced.""" import json from pathlib import Path import time import torch from transformers import AutoModelForCausalLM from onnxruntime_genai.models import builder ROOT = Path('/tmp/ImageToolbox-Assistant-Max') SOURCE = ROOT / 'source' OUTPUT = ROOT / 'package' CACHE = ROOT / 'export-cache' # Keep source BF16 tensors during export; the builder lazily converts tensors to # FP32 before CPU quantization. This avoids an unnecessary full FP32 model copy. def load_weights(self, input_path): return AutoModelForCausalLM.from_pretrained( input_path, dtype='auto', local_files_only=True, trust_remote_code=False ) builder.Qwen3Model.load_weights = load_weights torch.set_num_threads(4) options = [ 'block_size=128', 'is_symmetric=true', 'accuracy_level=4', 'shared_embeddings=false', 'op_types_to_quantize=MatMul/Gather', 'hf_token=false', ] runtime_config = { 'model': {'decoder': {'session_options': { 'intra_op_num_threads': 2, 'inter_op_num_threads': 1 }}}, 'search': {'max_length': 4096, 'num_beams': 1, 'do_sample': False, 'past_present_share_buffer': False} } started = time.perf_counter() kwargs = builder.parse_extra_options( None, str(SOURCE), str(OUTPUT), 'int4', 'cpu', str(CACHE), options, runtime_config=json.dumps(runtime_config) ) builder.create_model(None, str(SOURCE), str(OUTPUT), 'int4', 'cpu', str(CACHE), **kwargs) config_path = OUTPUT / 'genai_config.json' config = json.loads(config_path.read_text()) config['model']['decoder']['session_options']['mlas.disable_kleidiai'] = '1' config_path.write_text(json.dumps(config, indent=2) + '\n') print(json.dumps({'exportSeconds': time.perf_counter() - started}, indent=2), flush=True)