forked from Amshaker/SwiftFormer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_block.py
91 lines (78 loc) · 3.48 KB
/
export_block.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
import argparse
from pathlib import Path
import torch
from models.swiftformer import SwiftFormerEncoder
def main(args):
print('Setting up SwiftFormerEncoder Block...')
if args.depth > 1:
raise NotImplementedError('TODO')
else:
net = SwiftFormerEncoder(
args.dim, mlp_ratio=args.mlp_ratio,
# act_layer=act_layer, drop_path=block_dpr,
# use_layer_scale=use_layer_scale,
# layer_scale_init_value=layer_scale_init_value
)
input = torch.rand((args.b_size, args.dim) + args.input_size)
out = net(input)
print('Exporting...')
if not args.output.parent.exists():
print(f'Making folder: {args.output.parent}')
args.output.parent.mkdir(exist_ok=True, parents=True)
torch_out = torch.onnx.export(
net.cpu(),
(input),
args.output,
verbose=False,
export_params=True,
input_names=['input'], output_names=['args.output'],
do_constant_folding=True,
opset_version=args.opset_version
# training=torch.onnx.TrainingMode.EVAL,
)
if args.output.exists():
print(f'🎉 Model exported: {args.output.absolute()}')
else:
raise FileNotFoundError(
'😱 You shall not pass: EXPORT FAILED ⚠️ Good luck 🧑💻🔍🐛, wish '
'you the on-device Gods to be with you!'
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Export SwiftFormerEncoder (Attention) Block'
)
parser.add_argument('--output', type=Path,
default=Path('checkpoints/model.onnx'))
parser.add_argument('--input_size', type=int, nargs='+', default=(64, 64))
parser.add_argument('--b_size', type=int, default=1, help='Batch size')
parser.add_argument('--output_file', action='store_true', default=True,
help='Path is a file')
parser.add_argument('--overwrite', '-f', action='store_true', default=False,
help='Overwrite existing onnx')
parser.add_argument('--opset_version', type=int, default=11,
help='ONNX Opset version')
parser.add_argument('--dim', type=int, default=96, help='Embedding dimension')
parser.add_argument('--mlp_ratio', type=float, default=4.0, help='MLP ratio')
parser.add_argument('--num_heads', type=int, default=1, help='Num heads')
parser.add_argument('--depth', type=int, default=1, help='Depth parameter')
args = parser.parse_args()
# Sanitization & verbosity
if not isinstance(args.input_size, tuple):
args.input_size = tuple(args.input_size)
if args.num_heads != 1:
raise NotImplementedError('SwiftFormer uses num_heads=1')
head_dim = args.dim // args.num_heads
print(f'{head_dim=} := {args.dim=}, {args.num_heads=}')
# Mobile friendly choices reminder
# awful_choice = (args.num_heads % 8 != 0 or args.embed_dim % 32 != 0 or
# args.head_dim % 32 != 0)
# if args.b_size == 1 and awful_choice:
# print('❗Awful hyperparemeter setup. Stop assuming GPUs, Samsung biz'
# ' is not there❗\n Develop mobile-friendly models'
# f'\n{args.num_heads=}, {args.embed_dim=} {args.head_dim=}'
# )
if args.output_file and args.output.suffix != '.onnx':
args.output = args.output / 'model.onnx'
if args.output.exists() and not args.overwrite:
raise ValueError(f'{args.output=} existed. Set --overwrite / -f')
main(args)