Sharjeelbaig commited on
Commit
03081fc
·
verified ·
1 Parent(s): 04c5825

Add verified INT8 ONNX model with KV cache

Browse files

Browser-ready Transformers.js layout with Python Optimum support, model card, tokenizer compatibility fixes, quantization metadata, and validated cached generation.

README.md ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers.js
3
+ pipeline_tag: text-generation
4
+ base_model: SupraLabs/Supra-Router-51M
5
+ tags:
6
+ - onnx
7
+ - transformers.js
8
+ - browser
9
+ - web
10
+ - int8
11
+ - quantized
12
+ - llama
13
+ - router
14
+ - text-generation
15
+ ---
16
+
17
+ # Supra-Router-51M ONNX INT8
18
+
19
+ Browser-ready ONNX conversion of [SupraLabs/Supra-Router-51M](https://huggingface.co/SupraLabs/Supra-Router-51M). The graph is dynamically quantized to signed INT8 and packaged using the Hugging Face ONNX repository layout.
20
+
21
+ - **Parameters:** 51.8M
22
+ - **ONNX size:** approximately 66 MB
23
+ - **Architecture:** `LlamaForCausalLM`
24
+ - **Execution:** ONNX Runtime Web (WASM) or ONNX Runtime Python
25
+ - **Conversion:** FP32 export, opset 17, per-channel dynamic INT8 weight quantization
26
+ - **KV cache:** included for efficient autoregressive generation
27
+
28
+ ## Transformers.js pipeline
29
+
30
+ ```bash
31
+ npm install @huggingface/transformers
32
+ ```
33
+
34
+ ```javascript
35
+ import { pipeline } from "@huggingface/transformers";
36
+
37
+ const modelId = "Sharjeelbaig/Supra-Router-51M-ONNX";
38
+ const router = await pipeline("text-generation", modelId, {
39
+ dtype: "int8",
40
+ });
41
+
42
+ const userPrompt = "Write Python code to find all primes below one million efficiently.";
43
+ const input = `Task: ${userPrompt}\nAnalysis: `;
44
+
45
+ const output = await router(input, {
46
+ max_new_tokens: 128,
47
+ do_sample: false,
48
+ return_full_text: false,
49
+ });
50
+
51
+ console.log(output[0].generated_text.trim());
52
+ ```
53
+
54
+ The model returns a pipe-separated routing record:
55
+
56
+ ```text
57
+ Domain: ... | Complexity: 1-5 | Math: True/False | Code: True/False | Route: small model/big model | Justification: ...
58
+ ```
59
+
60
+ ## Python with Optimum ONNX Runtime
61
+
62
+ ```bash
63
+ pip install "optimum-onnx[onnxruntime]" transformers
64
+ ```
65
+
66
+ ```python
67
+ from transformers import AutoTokenizer, pipeline
68
+ from optimum.onnxruntime import ORTModelForCausalLM
69
+
70
+ model_id = "Sharjeelbaig/Supra-Router-51M-ONNX"
71
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
72
+ model = ORTModelForCausalLM.from_pretrained(
73
+ model_id,
74
+ subfolder="onnx",
75
+ file_name="model_int8.onnx",
76
+ use_cache=False,
77
+ )
78
+
79
+ router = pipeline("text-generation", model=model, tokenizer=tokenizer)
80
+ prompt = "Explain why database deadlocks occur and provide code to prevent them."
81
+ result = router(
82
+ f"Task: {prompt}\nAnalysis: ",
83
+ max_new_tokens=128,
84
+ do_sample=False,
85
+ return_full_text=False,
86
+ )
87
+ print(result[0]["generated_text"].strip())
88
+ ```
89
+
90
+ ## Direct ONNX Runtime loading
91
+
92
+ The graph accepts:
93
+
94
+ - `input_ids`: `int64[batch, sequence]`
95
+ - `attention_mask`: `int64[batch, past_sequence + sequence]`
96
+ - `position_ids`: `int64[batch, sequence]`
97
+ - `past_key_values.{0..11}.{key,value}`: cached attention tensors
98
+
99
+ It returns `logits`: `float32[batch, sequence, 32000]` plus `present.{0..11}.{key,value}` cache tensors. For direct integration, initialize each cache with shape `[batch, 4, 0, 64]`, then pass each returned `present` tensor back as the corresponding `past_key_values` input on the next step. Hugging Face pipelines manage this automatically.
100
+
101
+ ## Validation
102
+
103
+ - Passed `onnx.checker`.
104
+ - Tested with dynamic sequence lengths.
105
+ - Cached FP32 ONNX maximum absolute logit error versus PyTorch was below `1.4e-4` during export validation.
106
+ - INT8 and FP32 generated identical complete routing strings on representative `small model` and `big model` prompts.
107
+ - The uploaded INT8 interface was run end-to-end through both a Transformers.js text-generation pipeline and a Python Optimum text-generation pipeline.
108
+
109
+ ## Limitations and responsible use
110
+
111
+ This is a small routing model trained on 992 examples. Treat its route as a heuristic, not a security boundary or sole safety control. Add deterministic policy checks, timeouts, input-length limits, and a conservative fallback in production.
112
+
113
+ The upstream model card does not declare a license at the time of this conversion. This repository does not add or replace upstream rights. Confirm use and redistribution terms with SupraLabs before commercial deployment or redistribution.
114
+
115
+ This conversion was produced independently and is not an official SupraLabs release. Refer to the [upstream model card](https://huggingface.co/SupraLabs/Supra-Router-51M) for intended input formatting and training details.
config.json ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "LlamaForCausalLM"
4
+ ],
5
+ "attention_bias": false,
6
+ "attention_dropout": 0.0,
7
+ "bos_token_id": 0,
8
+ "dtype": "float32",
9
+ "eos_token_id": 2,
10
+ "head_dim": 64,
11
+ "hidden_act": "silu",
12
+ "hidden_size": 512,
13
+ "initializer_range": 0.02,
14
+ "intermediate_size": 1408,
15
+ "max_position_embeddings": 5120,
16
+ "mlp_bias": false,
17
+ "model_type": "llama",
18
+ "num_attention_heads": 8,
19
+ "num_hidden_layers": 12,
20
+ "num_key_value_heads": 4,
21
+ "pad_token_id": 1,
22
+ "pretraining_tp": 1,
23
+ "rms_norm_eps": 1e-06,
24
+ "rope_parameters": {
25
+ "factor": 1.0,
26
+ "rope_theta": 10000.0,
27
+ "rope_type": "linear",
28
+ "type": "linear"
29
+ },
30
+ "rope_scaling": null,
31
+ "rope_theta": 10000.0,
32
+ "tie_word_embeddings": true,
33
+ "transformers_version": "4.57.6",
34
+ "use_cache": true,
35
+ "vocab_size": 32000
36
+ }
generation_config.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 0,
4
+ "eos_token_id": 2,
5
+ "pad_token_id": 1,
6
+ "transformers_version": "4.57.6"
7
+ }
onnx/model_int8.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dc09518497a8fee5a449207e58aaa5163a8d6e5b409d9ec992480edea6e177ff
3
+ size 69447683
onnx/model_quantized.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dc09518497a8fee5a449207e58aaa5163a8d6e5b409d9ec992480edea6e177ff
3
+ size 69447683
quantize_config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "modes": ["int8", "q8"],
3
+ "per_channel": true,
4
+ "reduce_range": false,
5
+ "block_size": null,
6
+ "is_symmetric": true,
7
+ "accuracy_level": null,
8
+ "quant_type": 1
9
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "backend": "tokenizers",
3
+ "add_bos_token": false,
4
+ "add_eos_token": false,
5
+ "bos_token": "<s>",
6
+ "clean_up_tokenization_spaces": false,
7
+ "eos_token": "</s>",
8
+ "is_local": true,
9
+ "local_files_only": false,
10
+ "model_max_length": 5120,
11
+ "pad_token": "<pad>",
12
+ "tokenizer_class": "LlamaTokenizerFast",
13
+ "unk_token": "<unk>"
14
+ }