def test_convert_benchmarks(benchmark_name, feature_dict, data_func, input_keys, output_key): feature_size = [str(v) for k,v in feature_dict.items()] tabla_path = f"{OUTPATH}/{benchmark_name}_{'_'.join(feature_size)}_onnx_tabla.json" ref_tabla_path = f"{OUTPATH}/{benchmark_name}_{'_'.join(feature_size)}_tabla.json" filename = f"{benchmark_name}{'_'.join(feature_size)}.onnx" filepath = f"{BENCH_DIR}/ml_algorithms/{filename}" assert Path(filepath).exists() graph = pm.from_onnx(filepath, use_filename=False) # Apply transformations and/or generate verilog using 'transformed_graph' int_feat_dict = {k: int(v) for k,v in feature_dict.items()} _, ref_in_info, ref_out_info, ref_keys = data_func(**int_feat_dict) int_feat_dict['coarse'] = True ref_graph, in_info, out_info, ref_keys = data_func(**int_feat_dict) translated_inputs = {input_keys[k]: v for k,v in in_info.items() if k in input_keys} for i in output_key: input_cpy = pickle.loads(pickle.dumps(translated_inputs)) np_res = out_info[i[0]] onnx_res = graph(i[1], input_cpy) np.testing.assert_allclose(np.squeeze(np_res), np.squeeze(onnx_res)) print(f"Starting tabla compilation\n\n") tabla_ir, tabla_graph = pm.generate_tabla(graph, feature_dict, tabla_path,debug=False, context_dict={}, add_kwargs=True) ref_tabla_ir, ref_tabla_graph = pm.generate_tabla(ref_graph, feature_dict, ref_tabla_path,debug=False, context_dict={}, add_kwargs=True) ref_ocount_pass = pm.CountOpTypes(skip=['temp', 'parameter', ref_tabla_graph.name]) _ = ref_ocount_pass(ref_tabla_graph) ocount_pass = pm.CountOpTypes(skip=['temp', 'parameter', 'output', 'write', tabla_graph.name]) _ = ocount_pass(tabla_graph) pprint.pprint(ref_ocount_pass.op_types) pprint.pprint(ocount_pass.op_types) if set(ocount_pass.op_types.keys()) != set(ref_ocount_pass.op_types.keys()): raise RuntimeError(f"Unequal amounts of operations for graphs:\n" f"\tReference: {ref_ocount_pass.op_types.keys()}\n" f"\tActual: {ocount_pass.op_types.keys()}") for k,v in ocount_pass.op_types.items(): if v != ref_ocount_pass.op_types[k]: raise RuntimeError(f"Unequal operations for key {k}:\n" f"\tRef: {ref_ocount_pass.op_types[k]}\n" f"\tActual: {v}\n") assert len(ref_tabla_ir) == len(tabla_ir)
def test_reco_embedded_values(m, n, k): shape_dict = {"m": m, "n": n, "k": k} graph, input_info, out_info, keys = reco(m=m, n=n, k=k, coarse=True) ngraph, input_info, out_info, keys = reco(m=m, n=n, k=k, coarse=False) tabla_path = f"{OUTPATH}/{graph.name}_{m}_{n}_{k}_tabla.json" tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path, context_dict=input_info, add_kwargs=True)
def test_linear_reg_embedded_values(m_): shape_dict = {"m": m_} graph, input_info, out_info, keys = linear(m=m_, coarse=True) lgraph, input_info, out_info, keys = linear(m=m_, coarse=False) tabla_path = f"{OUTPATH}/{graph.name}_{m_}_tabla.json" tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path, context_dict=input_info, add_kwargs=True)
def test_reco_state_write(m_, n_, k_): shape_dict = {"m": m_, "n": n_, "k": k_} graph, input_info, out_info, keys = reco(m=m_, n=n_, k=k_, coarse=True) coarse_eval = graph(keys, input_info) np.testing.assert_allclose(coarse_eval[0], out_info["w1"]) np.testing.assert_allclose(coarse_eval[1], out_info["w2"]) tabla_path = f"{OUTPATH}/{graph.name}_tabla.json" lowered = set_shape_and_lower(graph, shape_dict) tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path)
def test_linear_reg_inf(m_): shape_dict = {"m": m_} graph, input_info, out_info, keys = linear_inf(m=m_, coarse=True) coarse_eval = graph(keys, input_info) np.testing.assert_allclose(coarse_eval, out_info[keys]) tabla_path = f"{OUTPATH}/{graph.name}_tabla.json" tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path)
def test_svm_embedded_values(m): shape_dict = {"m": m} graph, input_info, out_info, keys = svm(m=m, coarse=True) ngraph, input_info, out_info, keys = svm(m=m, coarse=False) tabla_path = f"{OUTPATH}/{graph.name}_{m}_tabla.json" tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path, context_dict=input_info, add_kwargs=True)
def test_svm(m_): shape_dict = {"m": m_} graph, input_info, out_info, keys = svm(m=m_, coarse=True) coarse_eval = graph(keys, input_info) np.testing.assert_allclose(coarse_eval, out_info["w"]) tabla_path = f"{OUTPATH}/{graph.name}_tabla.json" tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path) validation_path = f"{CWD}/tabla_examples/{graph.name}_{m_}.json" compare_tabla_dfg(validation_path, tabla_ir, tabla_graph)
def create_linear(m): shape_dict = {"m": m} graph, input_info, out_info, keys = linear(m=m, coarse=True) _, input_info, out_info, keys = linear(m=m, coarse=False) cwd = Path(f"{__file__}").parent full_path = f"{cwd}/outputs" tabla_path = f"{full_path}/{graph.name}_{m}_tabla.json" tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path, context_dict=input_info, add_kwargs=True)
def create_reco(m, n, k, onnx_graph=None): shape_dict = {"m": m, "n": n, "k": k} graph, input_info, out_info, keys = reco(m_=m, n_=n, k_=k, coarse=True) _, input_info, out_info, keys = reco(m_=m, n_=n, k_=k, coarse=False) cwd = Path(f"{__file__}").parent full_path = f"{cwd}/outputs" tabla_path = f"{full_path}/{graph.name}_{m}_{n}_{k}_tabla.json" if onnx_graph is not None: graph = onnx_graph tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path, context_dict=input_info, add_kwargs=True)
def test_fft(m): graph, input_info, out_info, keys = unwound_fft(m, coarse=True) shape_dict = {"N": m[0]} out_real = input_info['x'].dot(input_info['M_real'])**2 out_imag = input_info['x'].dot(input_info['M_imag'])**2 out_t = np.sqrt(out_imag + out_real) coarse_eval = graph(keys, input_info) # np.testing.assert_allclose(out_t, out_info['X']) # np.testing.assert_allclose(coarse_eval[0], out_info['X']) tabla_path = f"{OUTPATH}/{graph.name}_tabla.json" # lowered = set_shape_and_lower(graph, shape_dict) tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path)
def create_svm(m, onnx_graph=None): shape_dict = {"m": m} graph, input_info, out_info, keys = svm(m=m, coarse=True) _, input_info, out_info, keys = svm(m=m, coarse=False) if onnx_graph is not None: graph = onnx_graph cwd = Path(f"{__file__}").parent full_path = f"{cwd}/outputs" tabla_path = f"{full_path}/{graph.name}_{m}_tabla.json" tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path, context_dict=input_info, add_kwargs=True)
def test_backprop_embedded_values(l1, l2, l3): shape_dict = {"l1": l1, "l2": l2 , "l3": l3} graph, input_info, out_info, keys = backprop(l1, l2, l3, coarse=True, debug=False) test_out = graph(["w1","w2"], input_info) np.testing.assert_allclose(test_out[0], out_info["w1"]) np.testing.assert_allclose(test_out[1], out_info["w2"]) _, input_info, out_info, keys = backprop(l1, l2, l3, coarse=False, pbar=True) tabla_path = f"{OUTPATH}/{graph.name}_{l1}_{l2}_{l3}_tabla.json" tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path, context_dict=input_info, add_kwargs=True, debug=True)
def test_conv_embedded_values(x_shape, w_shape, params): shape_dict = {"n": x_shape[0], "ic": x_shape[1], "ih": x_shape[2], "iw": x_shape[3], "nf": w_shape[0], "kh": w_shape[2], "kw": w_shape[3], "stride": params["stride"], "pad": params["pad"]} graph, input_info0, out_info, keys = conv(x_shape, w_shape, params, coarse=True, debug_matrix=True) ngraph, input_info1, out_info, keys = conv(x_shape, w_shape, params, coarse=False, debug_matrix=True) lower_pass = pm.Lower({}) lowered = lower_pass(ngraph) res0 = np.asarray(lowered(keys, input_info1)).reshape(out_info["out"].shape) np.testing.assert_allclose(res0, out_info["out"]) tabla_path = f"{OUTPATH}/{graph.name}_tabla.json" tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path, context_dict=input_info1, add_kwargs=True, debug=True)
def create_svm_wifi(features, locations, lr=0.0001, deltav=1, train_size=7703): with pm.Node(name="svm_wifi") as graph: learning_rate = pm.parameter("learning_rate", default=lr) delta = pm.parameter("delta", default=deltav) n_features = pm.parameter("n_features", default=features) n_locations = pm.parameter("n_locations", default=locations) x_train = pm.input("x_train", shape=(n_features, )) y_train = pm.input("y_train", shape=(n_locations, )) y_train_inv = pm.input("y_train_inv", shape=(n_locations, )) weights = pm.state("weights", shape=(n_features, n_locations)) i = pm.index(0, n_features - 1, name="i") j = pm.index(0, n_locations - 1, name="j") scores = pm.sum([i], (weights[i, j] * x_train[i]), name="scores") correct_class_score = pm.sum([j], (scores[j] * y_train[j]), name="correct_class_score") h = ((scores[j] - correct_class_score + delta).set_name("h") > 0) # margin = (pm.cast(np.float32, h[j]) * y_train_inv[j]).set_name("margin") margin = (h[j] * y_train_inv[j]).set_name("margin") valid_margin_count = pm.sum([j], margin[j], name="valid_margin_count") partial = (y_train[j] * valid_margin_count).set_name("partial") updated_margin = (margin[j] - partial[j]).set_name("updated_margin") # # # dW = (x_train[i] * updated_margin[j]).set_name("dW") weights[i, j] = (weights[i, j] - learning_rate * dW[i, j]).set_name("weights_update") shape_dict = {"n_features": features, "n_locations": locations} input_info, keys, out_info = svm_wifi_datagen(features, locations, lr, deltav, lowered=True) cwd = Path(f"{__file__}").parent full_path = f"{cwd}/outputs" tabla_path = f"{full_path}/{graph.name}_{locations}_{features}_tabla.json" tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path, context_dict=input_info, add_kwargs=True)
def test_svm_wifi(lr, delta, features, locations, train_size): shape_dict = {"n_locations": locations, "n_features": features} graph, input_info0, out_info, keys = svm_wifi(features, locations, coarse=True) tabla_path = f"{OUTPATH}/{graph.name}_{features}_{locations}_tabla.json" # res0 = graph(keys, input_info0)[0] # np.testing.assert_allclose(res0, out_info['weights']) ngraph, input_info1, out_info, keys = svm_wifi(features, locations, coarse=False) # tabla_path = f"{OUTPATH}/{graph.name}_{locations}_{features}_tabla.json" tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path, context_dict=input_info1, add_kwargs=True) srdfg_path = f"{OUTPATH}/" pm.pb_store(tabla_graph, srdfg_path)
def create_backprop(l1, l2, l3, onnx_graph=None): shape_dict = {"l1": l1, "l2": l2, "l3": l3} graph, input_info, out_info, keys = backprop(l1, l2, l3, coarse=True) _, input_info, out_info, keys = backprop(l1, l2, l3, coarse=False, debug=True) cwd = Path(f"{__file__}").parent full_path = f"{cwd}/outputs" tabla_path = f"{full_path}/{graph.name}_{l1}_{l2}_{l3}_tabla.json" if onnx_graph is not None: graph = onnx_graph tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path, context_dict=input_info, add_kwargs=True)
def test_tabla_linear(m_): shape_dict = {"m": m_} graph, input_info, out_info, keys = linear(m=m_, coarse=True) lgraph, input_info, out_info, keys = linear(m=m_, coarse=False) cwd = Path(f"{__file__}").parent base_path = f"{cwd}/pmlang_examples" full_path = f"{base_path}/outputs" graph_name = f"{graph.name}_{m_}" tabla_path = f"{full_path}/{graph_name}_tabla.json" tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path, context_dict=input_info, add_kwargs=True) cwd = Path(f"{__file__}").parent base_path = f"{cwd}/pmlang_examples" full_path = f"{base_path}/outputs" pb_path = f"{full_path}/{graph.name}.srdfg" pm.pb_store(graph, full_path) node = pm.pb_load(pb_path)
def test_translate_linear_regressor(m): out_key_map = {"y": "y:0", "x": "x:0", "w": "W:0"} in_key_map = [("w", "W:0")] fpath = f"{ONNX_FILE_DIR}/linear_{m}.onnx" shape_dict = {"m": m} graph = pm.from_onnx(fpath) test_graph, input_info, out_info, keys = linear(m=m, coarse=True) tinput_info = copy.deepcopy(input_info) tkeys = copy.deepcopy(keys) test_res = test_graph(tkeys, tinput_info) np.testing.assert_allclose(test_res, (out_info["w"])) onx_input_info = copy.deepcopy(input_info) translated_inputs = {out_key_map[k]: v for k,v in input_info.items() if k in out_key_map} onnx_res = graph(in_key_map[0][1], translated_inputs) np.testing.assert_allclose(onnx_res, (out_info["w"])) tabla_path = f"{OUTPATH}/{graph.name}{m}_tabla.json" tabla_ir = pm.generate_tabla(graph, shape_dict, tabla_path)
def test_svm_wifi_inference(lr, delta, features, locations, train_size): shape_dict = {"n_locations": locations, "n_features": features} graph, input_info0, out_info, keys = svm_wifi_inf(features, locations, coarse=True) # tabla_path = f"{OUTPATH}/{graph.name}_{features}_{locations}_tabla.json" # res0 = graph(keys, input_info0)[0] np.testing.assert_allclose(res0, out_info['scores']) # ngraph, input_info1, out_info, keys = svm_wifi_inf(features, locations, coarse=False) # # lower_pass = pm.Lower({}) # lowered = lower_pass(ngraph) # res1 = np.asarray(lowered(keys, input_info1)).reshape(out_info["scores"].shape) # np.testing.assert_allclose(res1, out_info["scores"]) tabla_path = f"{OUTPATH}/{graph.name}_{locations}_{features}_tabla.json" tabla_ir, tabla_graph = pm.generate_tabla(graph, shape_dict, tabla_path, context_dict=input_info1, add_kwargs=True) srdfg_path = f"{OUTPATH}/" pm.pb_store(tabla_graph, srdfg_path)