Views
No views yet

| Precision | Size | Use Case |
|---|---|---|
| Q4F16 | ~5GB | Recommended (Q4 MoE + FP16 dense) |
| FP16 | ~16GB | Higher quality |
| Q4 | ~5GB | Smallest size |
onnx/
├── model.onnx # FP32 model graph
├── model.onnx_data* # FP32 weights
├── model_fp16.onnx # FP16 model graph
├── model_fp16.onnx_data* # FP16 weights
├── model_q4.onnx # Q4 model graph
├── model_q4.onnx_data* # Q4 weights
├── model_q4f16.onnx # Q4 MoE experts + FP16 dense (recommended)
└── model_q4f16.onnx_data* # Q4F16 weights
* Large models (>2GB) split weights across multiple files:
model.onnx_data, model.onnx_data_1, model.onnx_data_2, etc.
All data files must be in the same directory as the .onnx file.1pip install onnxruntime transformers numpy huggingface_hub
2# or with GPU support:
3pip install onnxruntime-gpu transformers numpy huggingface_hub1from transformers import AutoConfig, AutoTokenizer
2import onnxruntime
3import numpy as np
4from huggingface_hub import snapshot_download
5
6# 1. Load config, processor, and model
7model_id = "LiquidAI/LFM2-8B-A1B-ONNX"
8config = AutoConfig.from_pretrained(model_id)
9tokenizer = AutoTokenizer.from_pretrained(model_id)
10eos_token_id = config.eos_token_id
11
12filename = "model_q4.onnx" # Options: "model.onnx", "model_fp16.onnx", "model_q4.onnx", "model_q4f16.onnx"
13model_path = snapshot_download(repo_id=model_id, allow_patterns=f"onnx/{filename}*") # Download the graph + weights
14session = onnxruntime.InferenceSession(f"{model_path}/onnx/{filename}")
15
16# 2. Prepare inputs
17prompt = "What is C. elegans?"
18messages = [{"role": "user", "content": prompt}]
19inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="np")
20input_ids = inputs['input_ids']
21attention_mask = inputs['attention_mask']
22batch_size = input_ids.shape[0]
23num_logits_to_keep = np.array(1, dtype=np.int64)
24
25past_cache_values = {}
26for inp in session.get_inputs():
27 name = inp.name
28 shape = inp.shape
29 dtype = np.float32 if inp.type == "tensor(float)" else np.float16
30 if name.startswith("past_key_values"):
31 # Attention KV cache: shape [batch_size, num_kv_heads, 0, head_dim]
32 past_cache_values[name] = np.zeros([batch_size, shape[1], 0, shape[3]], dtype=dtype)
33 elif name.startswith("past_conv"):
34 # Conv cache: shape [batch_size, hidden_size, conv_L_cache]
35 past_cache_values[name] = np.zeros([batch_size, shape[1], shape[2]], dtype=dtype)
36
37# 3. Generation loop
38max_new_tokens = 1024
39generated_tokens = np.array([[]], dtype=np.int64)
40for i in range(max_new_tokens):
41 logits, *present_cache_values = session.run(None, dict(
42 input_ids=input_ids,
43 attention_mask=attention_mask,
44 num_logits_to_keep=num_logits_to_keep,
45 **past_cache_values,
46 ))
47
48 ## Update values for next generation loop
49 input_ids = logits[:, -1].argmax(-1, keepdims=True)
50 attention_mask = np.concatenate([attention_mask, np.ones_like(input_ids, dtype=np.int64)], axis=-1)
51 for j, key in enumerate(past_cache_values):
52 past_cache_values[key] = present_cache_values[j]
53 generated_tokens = np.concatenate([generated_tokens, input_ids], axis=-1)
54 if np.isin(input_ids, eos_token_id).any():
55 break
56
57 ## (Optional) Streaming
58 print(tokenizer.decode(input_ids[0]), end='', flush=True)
59print()
60
61# 4. Output result
62print(tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0])