Views
No views yet
MatMulNBitsQuantizer (the same path the INT4 repo uses), which produces a smaller graph and a much faster cold load.| File | Size | Description |
|---|---|---|
cohere-encoder.int8.onnx | 5.9 MB | Encoder graph |
cohere-encoder.int8.onnx.data | 2.7 GB | Encoder weights (INT8, block_size=32, symmetric) |
cohere-decoder.int8.onnx | 0.5 MB | Decoder graph |
cohere-decoder.int8.onnx.data | 213 MB | Decoder weights (INT8, block_size=32, symmetric) |
tokens.txt | 219 KB | 16 384-entry vocabulary |
| Build | Quantiser | Encoder weights | Cold load (5.4 s clip) | Inference (5.4 s clip) |
|---|---|---|---|---|
| Tristan's original | quantize_dynamic (full-graph) | 2.1 GB | 26.5 s | 9.8 s |
This repo (MatMulNBits, bs=32) | MatMulNBitsQuantizer (bits=8) | 2.7 GB | 17.5 s | 9.4 s |
onnx-final bs=128 (not published) | MatMulNBitsQuantizer (bits=8) | 2.6 GB | 22.2 s | 11.2 s |
onnxruntime 1.23.2, CPU-only, 8 threads, on a Cascade Lake-class VPS. Audio: voxpopuli_test_en_demo.wav (5.4 s, 16 kHz mono).quantize_dynamic path materialises the entire encoder weight tensor (~7 GB at F32) in RAM, which OOMs on small machines. MatMulNBitsQuantizer streams via the external-data format and runs on the same 8 GB box that exports the INT4 build.| INT4 (cstr/cohere-transcribe-onnx-int4) | INT8 (this repo) | |
|---|---|---|
| Total size | 1.94 GB | 2.91 GB |
| Encoder weights | 1.8 GB | 2.7 GB |
| Cold load | 7.5 s | 17.5 s |
| Inference (5.4 s) | 9.7 s | 9.4 s |
| Total wall (5.4 s) | 17.2 s | 27.0 s |
MatMulNBits format because the INT4 weights pack 2× more elements per cache line and the same number of vpdpbusd instructions process them. The main reason to ship INT8 alongside INT4 is numerical robustness: 8-bit symmetric quantisation has about 16× the dynamic range of 4-bit, which matters for languages and accents where the 4-bit version drifts.pip install onnxruntime numpy soundfile librosa1import onnxruntime as ort
2import numpy as np
3import librosa
4
5# Load audio (must be 16 kHz mono)
6audio, sr = librosa.load("your_audio.wav", sr=16000, mono=True)
7
8# Load models
9enc = ort.InferenceSession("cohere-encoder.int8.onnx")
10dec = ort.InferenceSession("cohere-decoder.int8.onnx")
11
12# Load tokens
13tokens = {}
14with open("tokens.txt", "r", encoding="utf-8") as f:
15 for line in f:
16 parts = line.strip().rsplit(" ", 1)
17 if len(parts) == 2:
18 tokens[int(parts[1])] = parts[0]
19token_to_id = {v: k for k, v in tokens.items()}
20
21# Build prompt
22prompt_ids = [token_to_id[t] for t in [
23 "<|startofcontext|>", "<|startoftranscript|>", "<|emo:undefined|>",
24 "<|en|>", "<|en|>", "<|pnc|>", "<|noitn|>", "<|notimestamp|>", "<|nodiarize|>"
25]]
26
27# Run encoder
28cross_k, cross_v = enc.run(None, {"audio": audio.reshape(1, -1).astype(np.float32)})
29
30# Run decoder (autoregressive greedy decoding)
31N_LAYERS, HEADS, HEAD_DIM, MAX_CTX = 8, 8, 128, 1024
32self_k = np.zeros((N_LAYERS, 1, HEADS, MAX_CTX, HEAD_DIM), dtype=np.float32)
33self_v = np.zeros((N_LAYERS, 1, HEADS, MAX_CTX, HEAD_DIM), dtype=np.float32)
34eos_id = token_to_id["<|endoftext|>"]
35
36generated = list(prompt_ids)
37current = np.array([prompt_ids], dtype=np.int64)
38offset = np.array(0, dtype=np.int64)
39
40for _ in range(256):
41 logits, self_k, self_v = dec.run(None, {
42 "tokens": current, "in_n_layer_self_k_cache": self_k,
43 "in_n_layer_self_v_cache": self_v, "n_layer_cross_k": cross_k,
44 "n_layer_cross_v": cross_v, "offset": offset,
45 })
46 next_id = int(np.argmax(logits[0, -1, :]))
47 if next_id == eos_id:
48 break
49 generated.append(next_id)
50 offset = np.array(int(offset) + current.shape[1], dtype=np.int64)
51 current = np.array([[next_id]], dtype=np.int64)
52
53text = "".join(
54 tokens.get(t, "").replace("\u2581", " ")
55 for t in generated[len(prompt_ids):]
56 if not tokens.get(t, "").startswith("<|")
57).strip()
58print(text)1from onnxruntime.quantization import matmul_nbits_quantizer, quant_utils
2
3config = matmul_nbits_quantizer.DefaultWeightOnlyQuantConfig(
4 block_size=32,
5 is_symmetric=True,
6 accuracy_level=4,
7 quant_format=quant_utils.QuantFormat.QOperator,
8 op_types_to_quantize=("MatMul",),
9 bits=8,
10)
11
12model = quant_utils.load_model_with_shape_infer("cohere-encoder.onnx")
13quantizer = matmul_nbits_quantizer.MatMulNBitsQuantizer(model, algo_config=config)
14quantizer.process()
15quantizer.model.save_model_to_file("cohere-encoder.int8.onnx", True)MatMulNBitsQuantizer(bits=8).CohereLabs.apache-2.0. This repository redistributes under the same terms; it grants no rights the upstream licence does not.