Instructions to use LMTLS5/ternary-qwen3.5-4b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use LMTLS5/ternary-qwen3.5-4b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="LMTLS5/ternary-qwen3.5-4b")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("LMTLS5/ternary-qwen3.5-4b", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use LMTLS5/ternary-qwen3.5-4b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "LMTLS5/ternary-qwen3.5-4b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "LMTLS5/ternary-qwen3.5-4b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/LMTLS5/ternary-qwen3.5-4b
- SGLang
How to use LMTLS5/ternary-qwen3.5-4b with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "LMTLS5/ternary-qwen3.5-4b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "LMTLS5/ternary-qwen3.5-4b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "LMTLS5/ternary-qwen3.5-4b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "LMTLS5/ternary-qwen3.5-4b", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use LMTLS5/ternary-qwen3.5-4b with Docker Model Runner:
docker model run hf.co/LMTLS5/ternary-qwen3.5-4b
Ternary Qwen3.5-4B
Qwen3.5-4B with linear weights decomposed as
W β B Β· diag(D) Β· C B, C β {-1, 0, +1} D: fp16 scales
| Variant | Decomposed weights |
|---|---|
| plain | MLP gate_proj, up_proj, down_proj in all 32 layers (96 weights) |
| imatrix | the same 96 MLP weights plus attention: q/k/v/o_proj in the 8 full-attention layers and in_proj_qkv, in_proj_z, out_proj in the 24 linear-attention layers (200 weights) |
Everything else (embeddings, norms, vision tower, other small tensors) is unchanged.
Contents
| Folder | What it is | Size |
|---|---|---|
qwen3.5-4b-ternary-plain/ |
Decomposed matrices (plain greedy decomposition) | 6.3 GB |
qwen3.5-4b-ternary-imatrix/ |
Decomposed matrices (imatrix-weighted), inner rank k = 3.0 Γ min(m, n) | 5.8 GB |
qwen3.5-4b-ternary-imatrix-m3.5/ |
Same algorithm, k = 3.5 Γ min(m, n) | 6.4 GB |
qwen3.5-4b-ternary-recon-bf16/ |
Dense bf16 model reconstructed from the imatrix variant at mult 3.0 (so MLP and attention are both ternary-reconstructed), standard HF format | 9.3 GB |
qwen3.5-4b-ternary-recon-m3.5-bf16/ |
Dense bf16 reconstruction of the mult 3.5 variant | 9.3 GB |
load_ternary.py |
Loader for the decomposed files |
The decomposed files are a single model.safetensors each. Per weight they store bit-packed
B_mask, B_sign, C_mask, C_sign and fp16 D; shapes and energy preserved are in the ternary metadata.
The two files use different bit packing, and load_ternary.py detects which automatically.
Quality vs. original bf16
KL(P_bf16 β P_model) per token, on the WikiText-2 test split (64 chunks Γ 2048 tokens), measured on the reconstructed dense bf16 models. Original bf16 perplexity: 9.019.
| Imatrix variant | Mean KLD (nats) | Median KLD | p99 KLD | Top-1 agreement | Perplexity |
|---|---|---|---|---|---|
| mult 3.0 | 0.0441 | 0.0194 | 0.343 | 91.1% | 9.299 (+3.1%) |
| mult 3.5 | 0.0224 | 0.0088 | 0.150 | 94.0% | 9.090 (+0.8%) |
Per-matrix energy preserved (squared-norm): mult 3.5 has min 99.21% / mean 99.76% over the 200 decomposed matrices. Single eval text and no confidence intervals; use as a relative comparison.
Use the reconstructed model
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from huggingface_hub import snapshot_download
path = snapshot_download("LMTLS5/ternary-qwen3.5-4b", allow_patterns="qwen3.5-4b-ternary-recon-bf16/*")
path += "/qwen3.5-4b-ternary-recon-bf16"
tok = AutoTokenizer.from_pretrained(path)
model = AutoModelForCausalLM.from_pretrained(path, dtype=torch.bfloat16).cuda().eval()
Needs a transformers version that supports Qwen3.5.
Load the ternary factors directly with transformers
LMTLS5/ternary-qwen3.5-4b-hf packages the mult 3.5 imatrix factors as a
transformers checkpoint. Each projection is a DecomposedLinear with A, B, C tensors, and W is never built:
model = AutoModelForCausalLM.from_pretrained("LMTLS5/ternary-qwen3.5-4b-hf", trust_remote_code=True,
dtype=torch.bfloat16, device_map="cuda")
Load the decomposed matrices
from huggingface_hub import hf_hub_download
from load_ternary import ternary_names, load_ternary, reconstruct # load_ternary.py from this repo
p = hf_hub_download("LMTLS5/ternary-qwen3.5-4b", "qwen3.5-4b-ternary-imatrix/model.safetensors")
names = ternary_names(p) # all decomposed weights
B, D, C = load_ternary(p, names[0]) # B (m,k), D (k,), C (k,n); B, C in {-1,0,+1}
W = reconstruct(B, D, C) # dense (m, n) weight
y = ((x @ C.T) * D) @ B.T # same as x @ W.T, without building W
Load one weight at a time: unpacking every weight to fp32 at once needs a lot of RAM.
Non-decomposed tensors are ordinary tensors in the same file and can be read with safetensors.safe_open.