rokeya71 commited on
Commit
4e8c1b1
·
verified ·
1 Parent(s): f18b9b7

Upload 10 files

Browse files
onnx/config/config.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "ibm-granite/granite-embedding-30m-english",
3
+ "architectures": [
4
+ "RobertaModel"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "bos_token_id": 0,
8
+ "classifier_dropout": null,
9
+ "eos_token_id": 2,
10
+ "hidden_act": "gelu",
11
+ "hidden_dropout_prob": 0.1,
12
+ "hidden_size": 384,
13
+ "initializer_range": 0.02,
14
+ "intermediate_size": 1536,
15
+ "layer_norm_eps": 1e-12,
16
+ "max_position_embeddings": 514,
17
+ "model_type": "roberta",
18
+ "num_attention_heads": 12,
19
+ "num_hidden_layers": 6,
20
+ "pad_token_id": 1,
21
+ "position_embedding_type": "absolute",
22
+ "torch_dtype": "bfloat16",
23
+ "transformers_version": "4.46.3",
24
+ "type_vocab_size": 2,
25
+ "use_cache": true,
26
+ "vocab_size": 50265
27
+ }
onnx/granite_embedding_model.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a723af95b77d48c4bec7f8d71c8091ca9e91dfdab6fef8e16d7a5780a0de7b50
3
+ size 121327615
onnx/model_uint8.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c000bf5e8142c5dd9c14ae1e41c071821d68b90a8ca9e44e633221feb8f87398
3
+ size 30640016
onnx/onnx_conv.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoTokenizer, AutoModel, AutoConfig
3
+ import os
4
+
5
+ # Define the model name and output paths
6
+ model_name = "ibm-granite/granite-embedding-30m-english"
7
+ onnx_model_path = "./granite_embedding_model.onnx"
8
+ tokenizer_path = "./tokenizer"
9
+ config_path = "./config"
10
+
11
+ # Load the model, tokenizer, and config
12
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
13
+ model = AutoModel.from_pretrained(model_name)
14
+ config = AutoConfig.from_pretrained(model_name)
15
+
16
+ # Save the tokenizer and config for later use
17
+ tokenizer.save_pretrained(tokenizer_path)
18
+ config.save_pretrained(config_path)
19
+
20
+ # Set the model to evaluation mode
21
+ model.eval()
22
+
23
+ # Example input for tracing
24
+ dummy_input = tokenizer("This is a test sentence.", return_tensors="pt")
25
+ input_ids = dummy_input["input_ids"]
26
+ attention_mask = dummy_input["attention_mask"]
27
+
28
+ # Export the model to ONNX
29
+ torch.onnx.export(
30
+ model,
31
+ (input_ids, attention_mask), # The model's inputs
32
+ onnx_model_path, # Path to save the ONNX model
33
+ input_names=["input_ids", "attention_mask"], # Input names
34
+ output_names=["output"], # Output names
35
+ dynamic_axes={
36
+ "input_ids": {
37
+ 0: "batch_size",
38
+ 1: "sequence_length",
39
+ }, # Batch size and sequence length can vary
40
+ "attention_mask": {0: "batch_size", 1: "sequence_length"},
41
+ "output": {0: "batch_size", 1: "sequence_length"},
42
+ },
43
+ opset_version=14, # ONNX opset version
44
+ )
45
+
46
+ print(f"Model saved as ONNX to {onnx_model_path}")
47
+ print(f"Tokenizer saved to {tokenizer_path}")
48
+ print(f"Config saved to {config_path}")
onnx/tokenizer/merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
onnx/tokenizer/special_tokens_map.json ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "<s>",
4
+ "lstrip": false,
5
+ "normalized": true,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "cls_token": {
10
+ "content": "<s>",
11
+ "lstrip": false,
12
+ "normalized": true,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "eos_token": {
17
+ "content": "</s>",
18
+ "lstrip": false,
19
+ "normalized": true,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ },
23
+ "mask_token": {
24
+ "content": "<mask>",
25
+ "lstrip": true,
26
+ "normalized": true,
27
+ "rstrip": false,
28
+ "single_word": false
29
+ },
30
+ "pad_token": {
31
+ "content": "<pad>",
32
+ "lstrip": false,
33
+ "normalized": true,
34
+ "rstrip": false,
35
+ "single_word": false
36
+ },
37
+ "sep_token": {
38
+ "content": "</s>",
39
+ "lstrip": false,
40
+ "normalized": true,
41
+ "rstrip": false,
42
+ "single_word": false
43
+ },
44
+ "unk_token": {
45
+ "content": "<unk>",
46
+ "lstrip": false,
47
+ "normalized": true,
48
+ "rstrip": false,
49
+ "single_word": false
50
+ }
51
+ }
onnx/tokenizer/tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
onnx/tokenizer/tokenizer_config.json ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_prefix_space": false,
3
+ "added_tokens_decoder": {
4
+ "0": {
5
+ "content": "<s>",
6
+ "lstrip": false,
7
+ "normalized": true,
8
+ "rstrip": false,
9
+ "single_word": false,
10
+ "special": true
11
+ },
12
+ "1": {
13
+ "content": "<pad>",
14
+ "lstrip": false,
15
+ "normalized": true,
16
+ "rstrip": false,
17
+ "single_word": false,
18
+ "special": true
19
+ },
20
+ "2": {
21
+ "content": "</s>",
22
+ "lstrip": false,
23
+ "normalized": true,
24
+ "rstrip": false,
25
+ "single_word": false,
26
+ "special": true
27
+ },
28
+ "3": {
29
+ "content": "<unk>",
30
+ "lstrip": false,
31
+ "normalized": true,
32
+ "rstrip": false,
33
+ "single_word": false,
34
+ "special": true
35
+ },
36
+ "50264": {
37
+ "content": "<mask>",
38
+ "lstrip": true,
39
+ "normalized": true,
40
+ "rstrip": false,
41
+ "single_word": false,
42
+ "special": true
43
+ }
44
+ },
45
+ "additional_special_tokens": [],
46
+ "bos_token": "<s>",
47
+ "clean_up_tokenization_spaces": true,
48
+ "cls_token": "<s>",
49
+ "eos_token": "</s>",
50
+ "errors": "replace",
51
+ "mask_token": "<mask>",
52
+ "model_max_length": 512,
53
+ "pad_token": "<pad>",
54
+ "sep_token": "</s>",
55
+ "tokenizer_class": "RobertaTokenizer",
56
+ "trim_offsets": true,
57
+ "unk_token": "<unk>"
58
+ }
onnx/tokenizer/vocab.json ADDED
The diff for this file is too large to render. See raw diff
 
onnx/tools.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import onnx
2
+
3
+ # # Load the ONNX model
4
+ # model_path = "model_uint8.onnx" # Replace with the path to your ONNX model
5
+ # onnx_model = onnx.load(model_path)
6
+
7
+ # # Print model's input and output shapes
8
+ # for input_tensor in onnx_model.graph.input:
9
+ # print(f"Input Name: {input_tensor.name}")
10
+ # print(
11
+ # f"Input Shape: {[dim.dim_value for dim in input_tensor.type.tensor_type.shape.dim]}"
12
+ # )
13
+
14
+ # for output_tensor in onnx_model.graph.output:
15
+ # print(f"Output Name: {output_tensor.name}")
16
+ # print(
17
+ # f"Output Shape: {[dim.dim_value for dim in output_tensor.type.tensor_type.shape.dim]}"
18
+ # )
19
+
20
+
21
+ from onnxruntime.quantization import quantize_dynamic, QuantType
22
+
23
+ # Define the path to the original ONNX model and the quantized output model
24
+ onnx_model_path = "./granite_embedding_model.onnx" # Path to the original ONNX model
25
+ quantized_model_path = "./model_uint8.onnx" # Path to save the quantized ONNX model
26
+
27
+ # Perform dynamic quantization to UInt8
28
+ quantize_dynamic(
29
+ model_input=onnx_model_path, # Input ONNX model path
30
+ model_output=quantized_model_path, # Output quantized model path
31
+ weight_type=QuantType.QUInt8, # Use UInt8 for weights
32
+ )
33
+
34
+ # Print confirmation of quantization
35
+ print(f"Quantized model saved to {quantized_model_path}")