|
| 1 | +from graph_net.torch import utils |
| 2 | +import argparse |
| 3 | +import torch |
| 4 | +import logging |
| 5 | +from pathlib import Path |
| 6 | +from typing import Type, Any |
| 7 | +import sys |
| 8 | +from graph_net.torch.imp_util import load_class_from_file |
| 9 | +import hashlib |
| 10 | +from contextlib import contextmanager |
| 11 | +import json |
| 12 | +import inspect |
| 13 | +import imp_util |
| 14 | +import record_util |
| 15 | +import copy |
| 16 | + |
| 17 | + |
| 18 | +def main(args): |
| 19 | + model_path = args.model_path |
| 20 | + name2input_param_attrs = _get_name2input_param_attrs(model_path) |
| 21 | + name_and_annotation_types = _get_name_and_annotation_types(model_path) |
| 22 | + input_name_and_meta_attrs = _get_input_name_and_meta_attrs( |
| 23 | + name2input_param_attrs, name_and_annotation_types |
| 24 | + ) |
| 25 | + input_name_and_constraint_attrs = _get_input_name_and_constraint_attrs( |
| 26 | + input_name_and_meta_attrs |
| 27 | + ) |
| 28 | + _dump_input_name_and_constraint_attrs( |
| 29 | + input_name_and_constraint_attrs, args.output_path |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +def _dump_input_name_and_constraint_attrs(input_name_and_constraint_attrs, output_path): |
| 34 | + py_code = record_util.serialize_to_py_code( |
| 35 | + [attr for _, attr in input_name_and_constraint_attrs], |
| 36 | + class_prefix="ProgramInputConstraint", |
| 37 | + ) |
| 38 | + print(f"{output_path=}") |
| 39 | + with open(output_path, "w") as f: |
| 40 | + f.write(py_code) |
| 41 | + |
| 42 | + |
| 43 | +def _get_input_name_and_constraint_attrs(input_name_and_meta_attrs): |
| 44 | + seq_no = 0 |
| 45 | + dim2seq = {} |
| 46 | + |
| 47 | + def find_or_new_seq(dim): |
| 48 | + nonlocal seq_no |
| 49 | + nonlocal dim2seq |
| 50 | + if dim in dim2seq: |
| 51 | + return dim2seq[dim] |
| 52 | + ret = seq_no |
| 53 | + dim2seq[dim] = ret |
| 54 | + seq_no += 1 |
| 55 | + return ret |
| 56 | + |
| 57 | + def make_symoblic_shape(shape): |
| 58 | + return type(shape)( |
| 59 | + [ |
| 60 | + symbolic_dim_desc |
| 61 | + for dim in shape |
| 62 | + for dim_seq_no in [find_or_new_seq(dim)] |
| 63 | + for symbolic_dim_desc in [ |
| 64 | + {"symbol_name": f"s{dim_seq_no}", "example_value": dim} |
| 65 | + ] |
| 66 | + ] |
| 67 | + ) |
| 68 | + |
| 69 | + def make_constraint_attrs(attrs): |
| 70 | + attrs = copy.deepcopy(attrs) |
| 71 | + attrs["shape"] = make_symoblic_shape(attrs["shape"]) |
| 72 | + return attrs |
| 73 | + |
| 74 | + return [ |
| 75 | + (name, symbolic_attrs) |
| 76 | + for name, attrs in input_name_and_meta_attrs |
| 77 | + for symbolic_attrs in [make_constraint_attrs(attrs)] |
| 78 | + ] |
| 79 | + |
| 80 | + |
| 81 | +def _get_input_name_and_meta_attrs(name2input_param_attrs, name_and_annotation_types): |
| 82 | + def constructed_from_self(name): |
| 83 | + return name.find("self_") != -1 |
| 84 | + |
| 85 | + def is_tensor_type(annotation_type): |
| 86 | + return annotation_type is torch.Tensor |
| 87 | + |
| 88 | + ret = [ |
| 89 | + (name, meta_attr) |
| 90 | + for name, annotation_type in name_and_annotation_types |
| 91 | + if is_tensor_type(annotation_type) |
| 92 | + if not constructed_from_self(name) |
| 93 | + for meta_attr in [name2input_param_attrs[name]] |
| 94 | + ] |
| 95 | + assert len(ret) > 0 |
| 96 | + return ret |
| 97 | + |
| 98 | + |
| 99 | +def _get_name_and_annotation_types(model_path): |
| 100 | + model_class = load_class_from_file( |
| 101 | + f"{model_path}/model.py", class_name="GraphModule" |
| 102 | + ) |
| 103 | + annotations = inspect.getfullargspec(model_class.forward).annotations |
| 104 | + return [(k, v) for k, v in annotations.items()] |
| 105 | + |
| 106 | + |
| 107 | +def _get_name2input_param_attrs(model_path): |
| 108 | + def get_classes(): |
| 109 | + input_meta_file = f"{model_path}/input_meta.py" |
| 110 | + for _, cls in imp_util.load_name_and_classes_from_file(input_meta_file): |
| 111 | + yield cls |
| 112 | + |
| 113 | + weight_meta_file = f"{model_path}/weight_meta.py" |
| 114 | + for _, cls in imp_util.load_name_and_classes_from_file(weight_meta_file): |
| 115 | + yield cls |
| 116 | + |
| 117 | + return { |
| 118 | + name: attr |
| 119 | + for cls in get_classes() |
| 120 | + for attr in [record_util.make_attrs_from_class(cls)] |
| 121 | + for name in [attr["name"]] |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + parser = argparse.ArgumentParser(description="generate constraint proposal file") |
| 127 | + parser.add_argument( |
| 128 | + "--model-path", |
| 129 | + type=str, |
| 130 | + required=True, |
| 131 | + help="Path to folder e.g '../../samples/torch/resnet18'", |
| 132 | + ) |
| 133 | + parser.add_argument( |
| 134 | + "--output-path", |
| 135 | + type=str, |
| 136 | + required=True, |
| 137 | + help="output file path", |
| 138 | + ) |
| 139 | + args = parser.parse_args() |
| 140 | + main(args=args) |
0 commit comments