(j2_env, model_tag, graph_idx, graph, output)
| 103 | |
| 104 | |
| 105 | def save_graph_to_code(j2_env, model_tag, graph_idx, graph, output): |
| 106 | graph_tag = graph.name |
| 107 | template_name = "tensor_source.jinja2" |
| 108 | counter = 0 |
| 109 | for tensor in graph.tensors: |
| 110 | # convert tensor |
| 111 | source = j2_env.get_template(template_name).render( |
| 112 | tensor=tensor, |
| 113 | tensor_id=counter, |
| 114 | model_tag=model_tag, |
| 115 | graph_tag=graph_tag, |
| 116 | ) |
| 117 | cc_path = output + "/" + graph_tag + "_tensor" + str(counter) + ".cc" |
| 118 | with open(cc_path, "w") as f: |
| 119 | f.write(source) |
| 120 | counter += 1 |
| 121 | |
| 122 | template_name = "operator.jinja2" |
| 123 | counter = 0 |
| 124 | op_size = len(graph.op) |
| 125 | device = 0 |
| 126 | for arg in graph.arg: |
| 127 | if arg.name == MaceKeyword.mace_runtime_type_str: |
| 128 | device = arg.i |
| 129 | for start in range(0, op_size, 10): |
| 130 | source = j2_env.get_template(template_name).render( |
| 131 | start=start, |
| 132 | end=min(start + 10, op_size), |
| 133 | net=graph, |
| 134 | model_tag=model_tag, |
| 135 | graph_tag=graph_tag, |
| 136 | device=device, |
| 137 | ) |
| 138 | cc_path = output + "/" + graph_tag + "_op" + str(counter) + ".cc" |
| 139 | with open(cc_path, "w") as f: |
| 140 | f.write(source) |
| 141 | counter += 1 |
| 142 | |
| 143 | template_name = "graph.jinja2" |
| 144 | source = j2_env.get_template(template_name).render( |
| 145 | net=graph, |
| 146 | graph_id=graph_idx, |
| 147 | model_tag=model_tag, |
| 148 | graph_tag=graph_tag, |
| 149 | ) |
| 150 | with open(output + "/" + graph_tag + "_graph.cc", "w") as f: |
| 151 | f.write(source) |
| 152 | |
| 153 | |
| 154 | def save_model_to_code(namespace, model, params, model_checksum, |
no test coverage detected