nicolasembleton commited on
Commit
e242920
·
verified ·
1 Parent(s): aeaed2e

Add config, tokenizer, README

Browse files
.gitattributes CHANGED
@@ -34,3 +34,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
  model.onnx_data filter=lfs diff=lfs merge=lfs -text
 
 
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
  model.onnx_data filter=lfs diff=lfs merge=lfs -text
37
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ tags:
4
+ - onnx
5
+ - nanbeige
6
+ - browser-inference
7
+ - webgpu
8
+ - wasm
9
+ - cross-browser
10
+ - transformers.js
11
+ - onnx-runtime-web
12
+ pipeline_tag: text-generation
13
+ ---
14
+
15
+ # Nanbeige4.2-3B-ONNX
16
+
17
+ ONNX export of [Nanbeige/Nanbeige4.2-3B](https://huggingface.co/Nanbeige/Nanbeige4.2-3B) for cross-browser inference via ONNX Runtime Web.
18
+
19
+ This is the companion to [`nicolasembleton/Nanbeige4.2-3B-GGUF`](https://huggingface.co/nicolasembleton/Nanbeige4.2-3B-GGUF) (native/server-side via llama.cpp, Ollama, LM Studio).
20
+
21
+ ## Architecture note
22
+
23
+ Nanbeige uses a loop transformer (`num_loops=2` — two passes per physical layer). Stock ONNX Runtime Web doesn't have a `MatMulNBits` loop unroller for this. We solve it by unrolling the loop **at the Python level**: 44 sequential layer calls share 22 weight matrices. The exported graph is a standard ONNX opset-18 graph that runs in stock ONNX Runtime Web and transformers.js — no custom kernels needed.
24
+
25
+ Validation: bit-exact match against the stock PyTorch model.
26
+
27
+ ## Files
28
+
29
+ - `model.onnx` — 1.8 MB graph
30
+ - `model.onnx_data` — 4.0 GB consolidated BF16 weights
31
+ - `config.json`, `tokenizer*`, `vocab.json`, etc.
32
+
33
+ ## Browser usage (cross-browser, including Apple Safari)
34
+
35
+ ```js
36
+ import * as ort from "onnxruntime-web";
37
+
38
+ const session = await ort.InferenceSession.create(
39
+ "https://huggingface.co/nicolasembleton/Nanbeige4.2-3B-ONNX/resolve/main/model.onnx",
40
+ { executionProviders: ["webgpu", "wasm"] }, // Safari 17 macOS falls back to WASM
41
+ );
42
+
43
+ const tokens = [166100, 1234, 5678]; // your token ids
44
+ const feeds = {
45
+ input_ids: new ort.Tensor("int64", BigInt64Array.from(tokens.map(BigInt)), [1, tokens.length]),
46
+ attention_mask: new ort.Tensor("int64", BigInt64Array.from(tokens.map(() => 1n)), [1, tokens.length]),
47
+ position_ids: new ort.Tensor("int64", BigInt64Array.from(tokens.map((_, i) => BigInt(i))), [1, tokens.length]),
48
+ };
49
+ const { logits } = await session.run(feeds);
50
+ ```
51
+
52
+ ### Alternative: transformers.js
53
+
54
+ ```js
55
+ import { pipeline } from "@huggingface/transformers";
56
+
57
+ const generator = await pipeline(
58
+ "text-generation",
59
+ "nicolasembleton/Nanbeige4.2-3B-ONNX",
60
+ { device: "webgpu" }, // or "wasm"
61
+ );
62
+ const output = await generator("Hello, how are you?", { max_new_tokens: 256 });
63
+ ```
64
+
65
+ > **Note:** This model is prefill-only (forward pass, no KV cache baked in). For autoregressive generation you'll need to feed inputs back through and argmax over logits. KV-cache export is a future enhancement.
66
+
67
+ > **Safari note:** Safari 17+ on macOS Sonoma supports partial WebGPU. iOS Safari has no WebGPU — use the WASM execution provider (slower but works). Node.js also works via WASM.
68
+
69
+ ## License
70
+
71
+ Apache 2.0 (inherited from [Nanbeige/Nanbeige4.2-3B](https://huggingface.co/Nanbeige/Nanbeige4.2-3B)).
72
+
73
+ ## Citation
74
+
75
+ ```bibtex
76
+ @misc{nanbeige42-3b-onnx,
77
+ title = {{Nanbeige4.2-3B-ONNX}},
78
+ author = {{nicolasembleton}},
79
+ year = {{2026}},
80
+ howpublished = {{Hugging Face}},
81
+ note = {{Cross-browser ONNX export with Python-level num_loops=2 unroll. BF16, 4 GB.}},
82
+ }}
83
+ ```
added_tokens.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "</think>": 166104,
3
+ "</tool_call>": 166106,
4
+ "<think>": 166103,
5
+ "<tool_call>": 166105,
6
+ "<|endoftext|>": 166102,
7
+ "<|im_end|>": 166101,
8
+ "<|im_start|>": 166100
9
+ }
config.json ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "NanbeigeForCausalLM"
4
+ ],
5
+ "attention_bias": false,
6
+ "attention_dropout": 0.0,
7
+ "auto_map": {
8
+ "AutoConfig": "configuration_nanbeige.NanbeigeConfig",
9
+ "AutoModel": "modeling_nanbeige.NanbeigeModel",
10
+ "AutoModelForCausalLM": "modeling_nanbeige.NanbeigeForCausalLM"
11
+ },
12
+ "bos_token_id": 166100,
13
+ "eos_token_id": 166101,
14
+ "head_dim": 128,
15
+ "hidden_act": "silu",
16
+ "hidden_size": 3072,
17
+ "initializer_range": 0.02,
18
+ "intermediate_size": 10752,
19
+ "kv_channels": 128,
20
+ "loop_loss_weights": [],
21
+ "max_length": null,
22
+ "max_position_embeddings": 262144,
23
+ "model_type": "nanbeige",
24
+ "num_attention_heads": 48,
25
+ "num_hidden_layers": 22,
26
+ "num_key_value_heads": 8,
27
+ "num_loops": 2,
28
+ "pad_token_id": 0,
29
+ "pretraining_tp": 1,
30
+ "rms_norm_eps": 1e-05,
31
+ "rope_scaling": null,
32
+ "rope_theta": 70000000,
33
+ "skip_loop_final_norm": false,
34
+ "tie_word_embeddings": false,
35
+ "torch_dtype": "bfloat16",
36
+ "transformers_version": "4.42.4",
37
+ "use_cache": true,
38
+ "vocab_size": 166144
39
+ }
generation_config.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 166100,
3
+ "do_sample": true,
4
+ "eos_token_id": 166101,
5
+ "pad_token_id": 0,
6
+ "temperature": 0.6,
7
+ "top_k": 20,
8
+ "top_p": 0.95,
9
+ "transformers_version": "4.51.0"
10
+ }
special_tokens_map.json ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "additional_special_tokens": [
3
+ "<|endoftext|>"
4
+ ],
5
+ "bos_token": {
6
+ "content": "<|im_start|>",
7
+ "lstrip": false,
8
+ "normalized": false,
9
+ "rstrip": false,
10
+ "single_word": false
11
+ },
12
+ "eos_token": {
13
+ "content": "<|im_end|>",
14
+ "lstrip": false,
15
+ "normalized": false,
16
+ "rstrip": false,
17
+ "single_word": false
18
+ },
19
+ "pad_token": {
20
+ "content": "<unk>",
21
+ "lstrip": false,
22
+ "normalized": true,
23
+ "rstrip": false,
24
+ "single_word": false
25
+ },
26
+ "unk_token": {
27
+ "content": "<unk>",
28
+ "lstrip": false,
29
+ "normalized": true,
30
+ "rstrip": false,
31
+ "single_word": false
32
+ }
33
+ }
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1d858a0fc007f22af6ae18bfa1ae52d30e398aa9cd1ea06e7777176869346a3f
3
+ size 18450979
tokenizer.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fb41d04798b714520a9b075727b0226538b7330254299062742c50ec8374bc36
3
+ size 2782298
tokenizer_config.json ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": true,
3
+ "add_eos_token": false,
4
+ "add_prefix_space": true,
5
+ "added_tokens_decoder": {
6
+ "0": {
7
+ "content": "<unk>",
8
+ "lstrip": false,
9
+ "normalized": true,
10
+ "rstrip": false,
11
+ "single_word": false,
12
+ "special": true
13
+ },
14
+ "1": {
15
+ "content": "<s>",
16
+ "lstrip": false,
17
+ "normalized": true,
18
+ "rstrip": false,
19
+ "single_word": false,
20
+ "special": true
21
+ },
22
+ "2": {
23
+ "content": "</s>",
24
+ "lstrip": false,
25
+ "normalized": true,
26
+ "rstrip": false,
27
+ "single_word": false,
28
+ "special": true
29
+ },
30
+ "166100": {
31
+ "content": "<|im_start|>",
32
+ "lstrip": false,
33
+ "normalized": false,
34
+ "rstrip": false,
35
+ "single_word": false,
36
+ "special": true
37
+ },
38
+ "166101": {
39
+ "content": "<|im_end|>",
40
+ "lstrip": false,
41
+ "normalized": false,
42
+ "rstrip": false,
43
+ "single_word": false,
44
+ "special": true
45
+ },
46
+ "166102": {
47
+ "content": "<|endoftext|>",
48
+ "lstrip": false,
49
+ "normalized": false,
50
+ "rstrip": false,
51
+ "single_word": false,
52
+ "special": true
53
+ },
54
+ "166103": {
55
+ "content": "<think>",
56
+ "lstrip": false,
57
+ "normalized": true,
58
+ "rstrip": false,
59
+ "single_word": false,
60
+ "special": false
61
+ },
62
+ "166104": {
63
+ "content": "</think>",
64
+ "lstrip": false,
65
+ "normalized": true,
66
+ "rstrip": false,
67
+ "single_word": false,
68
+ "special": false
69
+ },
70
+ "166105": {
71
+ "content": "<tool_call>",
72
+ "lstrip": false,
73
+ "normalized": true,
74
+ "rstrip": false,
75
+ "single_word": false,
76
+ "special": false
77
+ },
78
+ "166106": {
79
+ "content": "</tool_call>",
80
+ "lstrip": false,
81
+ "normalized": true,
82
+ "rstrip": false,
83
+ "single_word": false,
84
+ "special": false
85
+ }
86
+ },
87
+ "additional_special_tokens": [
88
+ "<|endoftext|>"
89
+ ],
90
+ "bos_token": "<|im_start|>",
91
+ "chat_template": "\n\n{%- macro visible_text(content) -%}\n {%- if content is string -%}\n {{- content }}\n {%- elif content is iterable and content is not mapping -%}\n {%- for item in content -%}\n {%- if item is mapping and item.type == 'text' -%}\n {{- item.text }}\n {%- elif item is string -%}\n {{- item }}\n {%- elif item is mapping and item.type in ['image', 'image_url', 'video', 'video_url', 'audio', 'audio_url', 'input_audio'] -%}\n {%- set media_type = item.type | replace('_url', '') | replace('input_', '') -%}\n {{- \"<reminder>You are unable to process this \" ~ media_type ~ \" because you don't have multi-modal input ability. Try different methods.</reminder>\" }}\n {%- endif -%}\n {%- endfor -%}\n {%- else -%}\n {{- content }}\n {%- endif -%}\n{%- endmacro -%}\n\n\n{%- set tool_call_format = tool_call_format if tool_call_format is defined else 'xml' %}\n{%- if tools %}\n {{- '<|im_start|>system\\n' }} \n {%- if messages|length > 0 and messages[0].get('role', '') == 'system' %}\n {{- visible_text(messages[0].content) + '\\n\\n' }}\n {%- else %} \n {{- '你是一位工具函数调用专家,你会得到一个问题和一组可能的工具函数。根据问题,你需要进行一个或多个函数/工具调用以实现目的,请尽量尝试探索通过工具解决问题。\\n如果没有一个函数可以使用,请直接使用自然语言回复用户。\\n如果给定的问题缺少函数所需的参数,请使用自然语言进行提问,向用户询问必要信息。\\n如果调用结果已经足够回答用户问题,请对历史结果进行总结,使用自然语言回复用户。' }} \n {%- endif %}\n\n {{- \"# Tools\\n\\nYou may call one or more functions to assist with the user query.\\n\\nYou are provided with function signatures within <tools></tools> XML tags:\\n<tools>\" }}\n {%- for tool in tools %}\n {{- \"\\n\" }}\n {{- tool | tojson }}\n {%- endfor %}\n \n {%- if tool_call_format == 'json' %}\n {{- \"\\n</tools>\\n\\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\\n\" }}\n {{- '<tool_call>\\n{\\\"name\\\": <function-name>, \\\"arguments\\\": <args-json-object>}\\n</tool_call><|im_end|>\\n' }}\n {%- else %}\n {{- \"\\n</tools>\\n\\nFor each function call, output the function name and arguments within the following XML format:\\n\" }}\n {{- '<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call><|im_end|>\\n' }}\n {%- endif %}\n \n{%- else %}\n {%- if messages|length > 0 and messages[0].get('role', '') == 'system' %}\n {{- '<|im_start|>system\\n' + visible_text(messages[0].content) + '<|im_end|>\\n' }}\n {%- else %} \n {{- '<|im_start|>system\\n你是南北阁,一款由BOSS直聘自主研发并训练的专业大语言模型。<|im_end|>\\n' }} \n {%- endif %}\n{%- endif %}\n\n{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}\n{%- for message in messages[::-1] %}\n {%- set index = (messages|length - 1) - loop.index0 %}\n {%- if ns.multi_step_tool and message.get('role', '') == \"user\" and visible_text(message.content) is string and not(visible_text(message.content).startswith('<tool_response>') and visible_text(message.content).endswith('</tool_response>')) %}\n {%- set ns.multi_step_tool = false %}\n {%- set ns.last_query_index = index %}\n {%- endif %}\n{%- endfor %}\n\n{%- for message in messages %}\n {%- if visible_text(message.content) is string %}\n {%- set content = visible_text(message.content) %}\n {%- else %}\n {%- set content = '' %}\n {%- endif %}\n \n {%- if message.get('role', '') == \"system\" %}\n {%- if not loop.first %}\n {{- raise_exception('System message must be at the beginning.') }}\n {%- endif %}\n \n {%- elif message.get('role', '') == \"assistant\" %}\n {%- set reasoning_content = '' %}\n {%- if message.reasoning_content is string %}\n {%- set reasoning_content = message.reasoning_content %}\n {%- else %}\n {%- if '</think>' in content %}\n {%- set reasoning_content = content.split('</think>')[0].rstrip('\\n').split('<think>')[-1].lstrip('\\n') %}\n {%- set content = content.split('</think>')[-1].lstrip('\\n').rstrip('\\n') %}\n {%- endif %}\n {%- endif %}\n {%- set reasoning_content = reasoning_content|trim %}\n \n {%- if (preserve_thinking is defined and preserve_thinking is false) and (loop.index0 < ns.last_query_index) %}\n {{- '<|im_start|>' + message.get('role', '') + '\\n<think>\\n\\n</think>\\n\\n' + content }}\n {%- else %}\n {{- '<|im_start|>' + message.get('role', '') + '\\n<think>\\n' + reasoning_content + '\\n</think>\\n\\n' + content }}\n {%- endif %}\n \n {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}\n {%- if tool_call_format == 'json' %}\n {%- for tool_call in message.tool_calls %}\n {%- if (loop.first and content) or (not loop.first) %}\n {{- '\\n' }}\n {%- endif %}\n {%- if tool_call.function %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n {{- '<tool_call>\\n{\"name\": \"' }}\n {{- tool_call.name }}\n {{- '\", \"arguments\": ' }}\n {%- if tool_call.arguments is string %}\n {{- tool_call.arguments }}\n {%- else %}\n {{- tool_call.arguments | tojson }}\n {%- endif %}\n {{- '}\\n</tool_call>' }}\n {%- endfor %}\n {%- else %}\n {%- for tool_call in message.tool_calls %}\n {%- if tool_call.function is defined %}\n {%- set tool_call = tool_call.function %}\n {%- endif %}\n \n {%- if loop.first %}\n {%- if content|trim %}\n {{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}\n {%- else %}\n {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}\n {%- endif %}\n {%- else %}\n {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}\n {%- endif %}\n \n {%- if tool_call.arguments is defined %}\n {%- for args_name, args_value in tool_call.arguments|items %}\n {{- '<parameter=' + args_name + '>\n' }}\n {%- set args_value = args_value | string if args_value is string else args_value | tojson | safe %}\n {{- args_value }}\n {{- '\n</parameter>\n' }}\n {%- endfor %}\n {%- endif %}\n {{- '</function>\n</tool_call>' }}\n {%- endfor %}\n {%- endif %}\n {%- endif %}\n {{- '<|im_end|>\\n' }}\n \n {%- elif message.get('role', '') == \"tool\" %}\n {%- if loop.previtem and loop.previtem.get('role', '') != \"tool\" %}\n {{- '<|im_start|>user' }}\n {%- endif %}\n {{- '\\n<tool_response>\\n' }}\n {{- content }}\n {{- '\\n</tool_response>' }}\n {%- if loop.last or loop.nextitem.get('role', '') != \"tool\" %}\n {{- '<|im_end|>\\n' }}\n {%- endif %}\n {%- elif message.get('role', '') != '' %}\n {{- '<|im_start|>' + message.get('role', '') + '\\n' + content + '<|im_end|>' + '\\n' }}\n {%- endif %}\n{%- endfor %}\n\n{%- if add_generation_prompt %}\n {{- '<|im_start|>assistant\\n' }}\n \n {%- if enable_thinking is defined and enable_thinking is false %}\n {{- '<think>\n\n</think>\n\n' }}\n {%- else %}\n {{- '<think>\n' }}\n {%- endif %}\n \n{%- endif %}\n",
92
+ "clean_up_tokenization_spaces": false,
93
+ "eos_token": "<|im_end|>",
94
+ "extra_special_tokens": {},
95
+ "legacy": false,
96
+ "model_max_length": 1000000000000000019884624838656,
97
+ "pad_token": "<unk>",
98
+ "sp_model_kwargs": {},
99
+ "spaces_between_special_tokens": false,
100
+ "tokenizer_class": "LlamaTokenizer",
101
+ "unk_token": "<unk>",
102
+ "use_default_system_prompt": false
103
+ }