-
Notifications
You must be signed in to change notification settings - Fork 56
/
transfomer_models.py
138 lines (124 loc) · 6.25 KB
/
transfomer_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import transformers
import torch
import onnx_tool
tmpfile = 'tmp.onnx'
def transfomer_llama():
config = {"bos_token_id": 0, "eos_token_id": 1, "hidden_act": "silu", "hidden_size": 4096,
"intermediate_size": 11008, "initializer_range": 0.02, "max_sequence_length": 2048, "model_type": "llama",
"num_attention_heads": 32, "num_hidden_layers": 1, "pad_token_id": -1, "rms_norm_eps": 1e-06,
"torch_dtype": "float16", "transformers_version": "4.27.0.dev0", "use_cache": True, "vocab_size": 32000,
"max_position_embeddings": 2048}
modelname = f"{config['model_type']}_{config['hidden_size']}_{config['num_attention_heads']}_{config['num_hidden_layers']}.onnx"
config = transformers.PretrainedConfig(**config)
m = transformers.LlamaForCausalLM(config)
ids = torch.zeros((1, 512), dtype=torch.long)
torch.onnx.export(m, ids, tmpfile)
onnx_tool.model_profile(tmpfile, save_profile='llama-1layer.csv', mcfg={'constant_folding': True, 'verbose': True},
shape_only=True, save_model=modelname)
def transfomer_gptj():
config = {"activation_function": "gelu_new",
"architectures": [
"GPTJForCausalLM"
],
"attn_pdrop": 0.0,
"bos_token_id": 50256,
"embd_pdrop": 0.0,
"eos_token_id": 50256,
"gradient_checkpointing": False,
"initializer_range": 0.02,
"layer_norm_epsilon": 1e-05,
"model_type": "gptj",
"n_embd": 2048,
"hidden_size": 2048,
"n_head": 16,
"num_attention_heads": 16,
"n_inner": None,
"n_layer": 1,
"n_positions": 2048,
"resid_pdrop": 0.0,
"rotary": True,
"rotary_dim": 64,
"scale_attn_weights": True,
"summary_activation": None,
"summary_first_dropout": 0.1,
"summary_proj_to_labels": True,
"summary_type": "cls_index",
"summary_use_proj": True,
"task_specific_params": {
"text-generation": {
"do_sample": True,
"max_length": 50,
"temperature": 1.0
}
},
"tie_word_embeddings": False,
"tokenizer_class": "GPT2Tokenizer",
"transformers_version": "4.18.0.dev0",
"use_cache": True,
"vocab_size": 50400, "max_position_embeddings": 2048}
modelname = f"{config['model_type']}_{config['n_embd']}_{config['n_head']}_{config['n_layer']}.onnx"
config = transformers.PretrainedConfig(**config)
m = transformers.GPTJForCausalLM(config)
ids = torch.ones((1, 8), dtype=torch.long)
# out = m(ids)
# print(out)
torch.onnx.export(m, ids, tmpfile)
onnx_tool.model_profile(tmpfile, save_profile='gptj-1layer.csv', mcfg={'constant_folding': True, 'verbose': True},
shape_only=True, save_model=modelname)
def transformer_mpt():
from mpt.configuration_mpt import MPTConfig
from mpt.modeling_mpt import MPTForCausalLM
config = MPTConfig(n_layers=1, attn_config={'attn_impl': 'torch'})
m = MPTForCausalLM(config)
modelname = f"mpt_{config.d_model}_{config.n_heads}_{config.n_layers}.onnx"
ids = torch.zeros((1, 512), dtype=torch.long)
torch.onnx.export(m, ids, tmpfile)
onnx_tool.model_profile(tmpfile, save_profile='mpt'
'-1layer.csv', mcfg={'constant_folding': True, 'verbose': True},
shape_only=True, save_model=modelname)
def transformer_qwen():
from onnx_tool.llm import QWen_7B
QWen_7B['num_hidden_layers']=1
modelname = f"{QWen_7B['model_type']}_{QWen_7B['hidden_size']}_{QWen_7B['num_attention_heads']}_{QWen_7B['num_hidden_layers']}.onnx"
config = transformers.PretrainedConfig(**QWen_7B)
m = transformers.Qwen2ForCausalLM(config)
ids = torch.zeros((1, 512), dtype=torch.long)
torch.onnx.export(m, ids, tmpfile)
onnx_tool.model_profile(tmpfile, save_profile='llama-1layer.csv', mcfg={'constant_folding': True, 'verbose': True},
shape_only=True, save_model=modelname)
def transformer_llama3():
from onnx_tool.llm import Llama3_8B
Llama3_8B['num_hidden_layers']=2
modelname = f"{Llama3_8B['model_type']}_{Llama3_8B['hidden_size']}_{Llama3_8B['num_attention_heads']}_{Llama3_8B['num_hidden_layers']}.onnx"
config = transformers.PretrainedConfig(**Llama3_8B)
m = transformers.LlamaForCausalLM(config)
ids = torch.zeros((1, 512), dtype=torch.long)
torch.onnx.export(m, ids, tmpfile)
# onnx_tool.model_profile(tmpfile, save_profile='llama-1layer.csv', mcfg={'constant_folding': True, 'verbose': True},
# shape_only=True, save_model=modelname)
def transformer_llama3():
from onnx_tool.llm import Llama3_8B
Llama3_8B['num_hidden_layers']=2
modelname = f"{Llama3_8B['model_type']}_{Llama3_8B['hidden_size']}_{Llama3_8B['num_attention_heads']}_{Llama3_8B['num_hidden_layers']}.onnx"
config = transformers.PretrainedConfig(**Llama3_8B)
m = transformers.LlamaForCausalLM(config)
ids = torch.zeros((1, 512), dtype=torch.long)
torch.onnx.export(m, ids, tmpfile)
# onnx_tool.model_profile(tmpfile, save_profile='llama-1layer.csv', mcfg={'constant_folding': True, 'verbose': True},
# shape_only=True, save_model=modelname)
def transformer_phi3():
from onnx_tool.llm import phi3_mini
phi3_mini['num_hidden_layers']=2
modelname = f"{phi3_mini['model_type']}_{phi3_mini['hidden_size']}_{phi3_mini['num_attention_heads']}_{phi3_mini['num_hidden_layers']}.onnx"
config = transformers.PretrainedConfig(**phi3_mini)
m = transformers.Phi3ForCausalLM(config)
ids = torch.zeros((1, 512), dtype=torch.long)
torch.onnx.export(m, ids, tmpfile)
# onnx_tool.model_profile(tmpfile, save_profile='llama-1layer.csv', mcfg={'constant_folding': True, 'verbose': True},
# shape_only=True, save_model=modelname)
# transfomer_llama()
# transfomer_gptj()
# transformer_mpt()
transformer_phi3()
# transformer_llama3()
# transformer_qwen()