Manh Lai
commited on
Commit
·
897662d
1
Parent(s):
67a897e
fix: update code
Browse files- conver_to_onnx.py → convert_to_onnx.py +25 -25
- onnx/added_tokens.json +0 -3
- onnx/config.json +0 -28
- onnx/model_quantized.onnx +3 -0
- onnx/special_tokens_map.json +0 -51
- test_model.js +13 -9
- onnx/tokenizer_config.json → tokenizer.json +0 -0
conver_to_onnx.py → convert_to_onnx.py
RENAMED
@@ -1,5 +1,6 @@
|
|
1 |
from pathlib import Path
|
2 |
import onnx
|
|
|
3 |
from onnxconverter_common import float16
|
4 |
from onnxruntime.quantization import quantize_dynamic, QuantType
|
5 |
|
@@ -18,37 +19,36 @@ print("Exporting the FP32 model...")
|
|
18 |
model = ORTModelForFeatureExtraction.from_pretrained(model_name, export=True)
|
19 |
model.save_pretrained(output_dir)
|
20 |
|
21 |
-
# Save
|
22 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
23 |
-
tokenizer.save_pretrained(
|
24 |
|
25 |
-
# Define
|
26 |
model_fp32_path = output_dir / "model.onnx"
|
|
|
27 |
|
28 |
# -------------------------------------------
|
29 |
-
# Step 2:
|
30 |
# -------------------------------------------
|
31 |
-
print("
|
32 |
-
model_fp16_path = output_dir / "model-fp16.onnx"
|
33 |
-
# Load the FP32 ONNX model
|
34 |
-
model_fp32 = onnx.load(model_fp32_path.as_posix())
|
35 |
-
# Convert weights to FP16 while keeping input/output types in FP32 if needed
|
36 |
-
model_fp16 = float16.convert_float_to_float16(model_fp32, keep_io_types=True)
|
37 |
-
# Save the FP16 model
|
38 |
-
onnx.save(model_fp16, model_fp16_path.as_posix())
|
39 |
-
|
40 |
-
# -------------------------------------------
|
41 |
-
# Step 3: Convert FP32 model to INT8 (Dynamic Quantization)
|
42 |
-
# -------------------------------------------
|
43 |
-
print("Converting to INT8 (dynamic quantization)...")
|
44 |
-
model_int8_path = output_dir / "model-int8.onnx"
|
45 |
quantize_dynamic(
|
46 |
-
model_fp32_path.as_posix(),
|
47 |
-
|
48 |
-
weight_type=QuantType.QInt8
|
49 |
)
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from pathlib import Path
|
2 |
import onnx
|
3 |
+
import shutil
|
4 |
from onnxconverter_common import float16
|
5 |
from onnxruntime.quantization import quantize_dynamic, QuantType
|
6 |
|
|
|
19 |
model = ORTModelForFeatureExtraction.from_pretrained(model_name, export=True)
|
20 |
model.save_pretrained(output_dir)
|
21 |
|
22 |
+
# Save tokenizer
|
23 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
24 |
+
tokenizer.save_pretrained(output_dir)
|
25 |
|
26 |
+
# Define model paths
|
27 |
model_fp32_path = output_dir / "model.onnx"
|
28 |
+
model_quantized_path = output_dir / "model_quantized.onnx"
|
29 |
|
30 |
# -------------------------------------------
|
31 |
+
# Step 2: Quantize to INT8
|
32 |
# -------------------------------------------
|
33 |
+
print("Quantizing to INT8 (dynamic quantization)...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
quantize_dynamic(
|
35 |
+
model_input=model_fp32_path.as_posix(),
|
36 |
+
model_output=model_quantized_path.as_posix(),
|
37 |
+
weight_type=QuantType.QInt8,
|
38 |
)
|
39 |
|
40 |
+
# -------------------------------------------
|
41 |
+
# Step 3: Move JSON files to parent folder
|
42 |
+
# -------------------------------------------
|
43 |
+
print("Moving JSON files to parent folder...")
|
44 |
+
parent_dir = output_dir.parent
|
45 |
+
json_files = list(output_dir.glob("*.json"))
|
46 |
+
|
47 |
+
for json_file in json_files:
|
48 |
+
shutil.move(str(json_file), str(parent_dir / json_file.name))
|
49 |
+
|
50 |
+
print("✅ Conversion complete!")
|
51 |
+
print(f"Original FP32 model: {model_fp32_path}")
|
52 |
+
print(f"Quantized INT8 model: {model_quantized_path}")
|
53 |
+
print(f"Tokenizer files moved to: {[f.name for f in json_files]}")
|
54 |
+
print(f"ONNX files remain in: {output_dir}")
|
onnx/added_tokens.json
DELETED
@@ -1,3 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"<mask>": 64000
|
3 |
-
}
|
|
|
|
|
|
|
|
onnx/config.json
DELETED
@@ -1,28 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"architectures": [
|
3 |
-
"RobertaModel"
|
4 |
-
],
|
5 |
-
"attention_probs_dropout_prob": 0.1,
|
6 |
-
"bos_token_id": 0,
|
7 |
-
"classifier_dropout": null,
|
8 |
-
"eos_token_id": 2,
|
9 |
-
"gradient_checkpointing": false,
|
10 |
-
"hidden_act": "gelu",
|
11 |
-
"hidden_dropout_prob": 0.1,
|
12 |
-
"hidden_size": 768,
|
13 |
-
"initializer_range": 0.02,
|
14 |
-
"intermediate_size": 3072,
|
15 |
-
"layer_norm_eps": 1e-05,
|
16 |
-
"max_position_embeddings": 258,
|
17 |
-
"model_type": "roberta",
|
18 |
-
"num_attention_heads": 12,
|
19 |
-
"num_hidden_layers": 12,
|
20 |
-
"pad_token_id": 1,
|
21 |
-
"position_embedding_type": "absolute",
|
22 |
-
"tokenizer_class": "PhobertTokenizer",
|
23 |
-
"torch_dtype": "float32",
|
24 |
-
"transformers_version": "4.50.3",
|
25 |
-
"type_vocab_size": 1,
|
26 |
-
"use_cache": true,
|
27 |
-
"vocab_size": 64001
|
28 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onnx/model_quantized.onnx
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7292219a5f4f8c1f5a71fd2c1688debeb67167a180e4186b3c78c27bfe257c83
|
3 |
+
size 135252876
|
onnx/special_tokens_map.json
DELETED
@@ -1,51 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"bos_token": {
|
3 |
-
"content": "<s>",
|
4 |
-
"lstrip": false,
|
5 |
-
"normalized": false,
|
6 |
-
"rstrip": false,
|
7 |
-
"single_word": false
|
8 |
-
},
|
9 |
-
"cls_token": {
|
10 |
-
"content": "<s>",
|
11 |
-
"lstrip": false,
|
12 |
-
"normalized": false,
|
13 |
-
"rstrip": false,
|
14 |
-
"single_word": false
|
15 |
-
},
|
16 |
-
"eos_token": {
|
17 |
-
"content": "</s>",
|
18 |
-
"lstrip": false,
|
19 |
-
"normalized": false,
|
20 |
-
"rstrip": false,
|
21 |
-
"single_word": false
|
22 |
-
},
|
23 |
-
"mask_token": {
|
24 |
-
"content": "<mask>",
|
25 |
-
"lstrip": false,
|
26 |
-
"normalized": false,
|
27 |
-
"rstrip": false,
|
28 |
-
"single_word": false
|
29 |
-
},
|
30 |
-
"pad_token": {
|
31 |
-
"content": "<pad>",
|
32 |
-
"lstrip": false,
|
33 |
-
"normalized": false,
|
34 |
-
"rstrip": false,
|
35 |
-
"single_word": false
|
36 |
-
},
|
37 |
-
"sep_token": {
|
38 |
-
"content": "</s>",
|
39 |
-
"lstrip": false,
|
40 |
-
"normalized": false,
|
41 |
-
"rstrip": false,
|
42 |
-
"single_word": false
|
43 |
-
},
|
44 |
-
"unk_token": {
|
45 |
-
"content": "<unk>",
|
46 |
-
"lstrip": false,
|
47 |
-
"normalized": false,
|
48 |
-
"rstrip": false,
|
49 |
-
"single_word": false
|
50 |
-
}
|
51 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_model.js
CHANGED
@@ -1,14 +1,18 @@
|
|
1 |
-
|
2 |
-
process.env.HF_HUB_OFFLINE = "1";
|
3 |
-
|
4 |
-
import { AutoModel } from '@xenova/transformers';
|
5 |
|
6 |
async function run() {
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
}
|
13 |
|
14 |
run();
|
|
|
1 |
+
import { pipeline } from '@xenova/transformers';
|
|
|
|
|
|
|
2 |
|
3 |
async function run() {
|
4 |
+
// Load the pipeline for feature extraction (embeddings)
|
5 |
+
const extractor = await pipeline(
|
6 |
+
'feature-extraction',
|
7 |
+
'laituanmanh32/vietnamese-embedding-onnx',
|
8 |
+
{
|
9 |
+
quantized: false, // Disable quantization (if using full-precision ONNX)
|
10 |
+
}
|
11 |
+
);
|
12 |
+
|
13 |
+
// Generate embeddings for Vietnamese text
|
14 |
+
const output = await extractor('Mô hình thử nghiệm', { pooling: 'mean' });
|
15 |
+
console.log(output);
|
16 |
}
|
17 |
|
18 |
run();
|
onnx/tokenizer_config.json → tokenizer.json
RENAMED
File without changes
|