Skip to content

Commit 93ef504

Browse files
committed
added simple template generator frontend
- added a few new list/skip/force options to the generator
1 parent 5f971bf commit 93ef504

4 files changed

Lines changed: 157 additions & 7 deletions

File tree

scripts/gen_template.sh

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/bin/bash
2+
3+
DOMAIN=$1
4+
OPERATOR=$2
5+
VERSION=$3
6+
7+
ROOT=$(git rev-parse --show-toplevel)
8+
cd $ROOT/scripts
9+
10+
if [[ -z $DOMAIN ]]
11+
then
12+
echo "usage: $0 [domain] [operator] [version]"
13+
echo "possible domains:"
14+
python3 -m onnx_generator \
15+
--list-domains \
16+
-- ..
17+
exit 1
18+
fi
19+
20+
if [[ -z $OPERATOR ]]
21+
then
22+
echo "usage: $0 [domain] [operator] [version]"
23+
echo "possible operators:"
24+
if ! python3 -m onnx_generator \
25+
--list-operators \
26+
--domains $DOMAIN \
27+
-- ..
28+
then
29+
echo "no operators found! check specified domain!"
30+
fi
31+
exit 1
32+
fi
33+
34+
if [[ -z $VERSION ]]
35+
then
36+
echo "usage: $0 [domain] [operator] [version]"
37+
echo "possible versions:"
38+
if ! python3 -m onnx_generator \
39+
--list-versions \
40+
--domains $DOMAIN \
41+
-i '^'$OPERATOR'$' \
42+
-- ..
43+
then
44+
echo "no versions found! check specified domain and operator!"
45+
fi
46+
exit 1
47+
fi
48+
49+
RESULT=$(python3 -m onnx_generator \
50+
--list-versions \
51+
--domains $DOMAIN \
52+
-i '^'$OPERATOR'$' \
53+
--version $VERSION \
54+
-- ..)
55+
if [[ $RESULT != $@ ]]
56+
then
57+
echo "could not find combination of specified domain, operator and version!"
58+
exit 1
59+
fi
60+
61+
echo "### GENERATING TEMPLATE FOR $@ ###"
62+
echo
63+
python3 -m onnx_generator -vv \
64+
--domains $DOMAIN \
65+
-i '^'$OPERATOR'$' \
66+
--version $VERSION \
67+
--no-sets \
68+
-- ..
69+
echo
70+
71+
OPERATORS=
72+
echo "### GENERATING NEW OPERATOR SET ###"
73+
echo
74+
python -m onnx_generator -v \
75+
-i $(find ../src/operators -maxdepth 2 -mindepth 2 -type d -printf "^%f$ " )\
76+
--version latest \
77+
--force-pattern \
78+
'^'$ROOT'/src/operators/'$DOMAIN'/opdomain_.*$' \
79+
'^'$ROOT'/src/operators/operator_set.c$' \
80+
--skip-pattern '.*' \
81+
-- ..

scripts/onnx_generator/__main__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,33 @@
1414
default=args.force,
1515
help="overwrite existing files"
1616
)
17+
parser.add_argument(
18+
"--list-domains",
19+
action='count',
20+
help="list all domains"
21+
)
22+
parser.add_argument(
23+
"--list-operators",
24+
action='count',
25+
help="list all operators in selected domains"
26+
)
27+
parser.add_argument(
28+
"--list-versions",
29+
action='count',
30+
help="list all versions of selected operators in selected domains"
31+
)
32+
parser.add_argument(
33+
"--skip-pattern",
34+
nargs="+",
35+
default=args.skip_pattern,
36+
help="skip path matching pattern"
37+
)
38+
parser.add_argument(
39+
"--force-pattern",
40+
nargs="+",
41+
default=args.force_pattern,
42+
help="for write of path matching pattern"
43+
)
1744
parser.add_argument(
1845
"--force-header",
1946
action='count',

scripts/onnx_generator/args.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
onnx = []
33
verbose = 0
44
header = ["src/operators"]
5+
skip_pattern = []
6+
force_pattern = []
57
no_header = 0
68
force_header = 0
79
resolve = ["src/operators/"]

scripts/onnx_generator/run.py

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def fatal(text, error=1):
4949
version2schema = name2version2schema.setdefault(schema.name,{})
5050
version2schema[schema.version] = schema
5151

52-
note("selecting domains")
52+
note("selecting domains",1)
5353
domains = domain2name2version2schema.keys()
5454
note(f"onnx operator schemas have {len(domains)} domains: {', '.join(domains)}",2)
5555
if "all" in args.domains:
@@ -101,7 +101,7 @@ def fatal(text, error=1):
101101
for name in names:
102102
del domain2name2version2schema[domain][name]
103103

104-
note("selecting onnx operator schema versions")
104+
note("selecting onnx operator schema versions",1)
105105
delete_versions = {}
106106
for domain, name2version2schema in domain2name2version2schema.items():
107107
for name, version2schema in name2version2schema.items():
@@ -134,6 +134,25 @@ def fatal(text, error=1):
134134
for schema in version2schema.values():
135135
schemas.append(schema)
136136

137+
if args.list_domains or args.list_operators or args.list_versions:
138+
empty = True
139+
for domain, name2version2schema in domain2name2version2schema.items():
140+
if args.list_domains:
141+
empty = False
142+
print(domain)
143+
for name, version2schema in name2version2schema.items():
144+
if args.list_operators:
145+
empty = False
146+
print(f"{domain} {name}")
147+
for version in version2schema.keys():
148+
if args.list_versions:
149+
empty = False
150+
print(f"{domain} {name} {version}")
151+
if empty:
152+
sys.exit(1)
153+
else:
154+
sys.exit(0)
155+
137156
note("generating onnx operator headers")
138157
path = f"{args.path[-1]}/{args.header[-1]}/"
139158
headers = [ OperatorHeader.Header(s,path) for s in schemas ]
@@ -162,18 +181,39 @@ def fatal(text, error=1):
162181
files.extend(map(lambda x: (bool(args.force_info),x),info))
163182

164183
writecount = 0
184+
existcount = 0
185+
skipcount = 0
165186
note("Writing files",1)
166187
if not args.path[-1]:
167188
warning("skipping write because args.path is not set")
168189
else:
169190
for force,obj in files:
170191
path = obj.filepath()
171-
if path.exists() and not (args.force or force):
172-
warning(f"skipping existing file '{path}'",1)
173-
continue
174-
note(f"writing file {path}",3)
192+
overwrite = args.force or force
193+
if not overwrite:
194+
for pattern in args.force_pattern:
195+
if re.match(pattern,str(path)):
196+
overwrite=True
197+
break
198+
skip = False
199+
for pattern in args.skip_pattern:
200+
if re.match(pattern,str(path)):
201+
skip = True
202+
break
203+
if not overwrite:
204+
if path.exists():
205+
existcount += 1
206+
warning(f"skipping existing file '{path}'",2)
207+
continue
208+
elif skip:
209+
skipcount += 1
210+
warning(f"skipping file '{path}'",2)
211+
continue
212+
note(f"writing file {path}",1)
213+
else:
214+
warning(f"overwriting file {path}",1)
175215
if not args.dryrun:
176216
os.makedirs(path.parent,exist_ok=True)
177217
path.open("w").write(str(obj))
178218
writecount += 1
179-
note(f"wrote {writecount} of {len(files)} files")
219+
note(f"wrote {writecount} of {len(files)} files ({existcount} already existed, {skipcount} skipped)")

0 commit comments

Comments
 (0)