-
Notifications
You must be signed in to change notification settings - Fork 5
/
onnx-llama-to-fp16.py
37 lines (27 loc) · 1.17 KB
/
onnx-llama-to-fp16.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
#
# convert an onnx model to fp16
#
import onnx
from onnxruntime.transformers.onnx_model import OnnxModel
import argparse
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--input', required=True, help='fp32 input')
parser.add_argument('--output', required=True, help='fp16 output')
parser.add_argument('--keep-io', action='store_true', help='keep io types')
parser.add_argument('--external-data', action='store_true', help='keep io types')
parser.add_argument('--block', required=False, help='op block list')
parser.add_argument('--block-nodes', required=False, help='node block list')
args = parser.parse_args()
if (args.block):
args.block = args.block.split(',')
if (args.block_nodes):
args.block_nodes = args.block_nodes.split(',')
return args
def main():
args = get_args()
model = OnnxModel(onnx.load_model(args.input, load_external_data=True))
model.convert_float_to_float16(keep_io_types=args.keep_io, op_block_list=args.block, node_block_list=args.block_nodes)
model.save_model_to_file(args.output, use_external_data_format=args.external_data)
if __name__ == '__main__':
main()