(pte_path, compiler_specs)
| 76 | |
| 77 | |
| 78 | def get_io_info(pte_path, compiler_specs): |
| 79 | dtype_map = {} |
| 80 | for type_map in (QNN_QUANT_TYPE_MAP, QNN_TENSOR_TYPE_MAP): |
| 81 | for k, v in type_map.items(): |
| 82 | dtype_map.setdefault(v, k) |
| 83 | |
| 84 | def fill_tensor_info(info, qnn_tensors, category): |
| 85 | for tensor in qnn_tensors: |
| 86 | encoding = tensor.GetEncodings() |
| 87 | quantization_info = { |
| 88 | "scale": encoding.data["scale"].tolist(), |
| 89 | "offset": encoding.data["offset"].tolist(), |
| 90 | "axis": encoding.axis, |
| 91 | } |
| 92 | |
| 93 | info[category].append( |
| 94 | { |
| 95 | "name": tensor.GetName(), |
| 96 | "shape": tensor.GetDims().tolist(), |
| 97 | "dtype": dtype_map[tensor.GetDataType()], |
| 98 | "encoding": quantization_info, |
| 99 | } |
| 100 | ) |
| 101 | |
| 102 | in_key, out_key = "inputs", "outputs" |
| 103 | tensor_info = {in_key: [], out_key: []} |
| 104 | |
| 105 | path_of_pte = Path(pte_path) |
| 106 | dump_context_from_pte(path_of_pte.absolute()) |
| 107 | ctx_bin = [f for f in os.listdir(path_of_pte.parent) if Path(f).suffix == ".bin"][0] |
| 108 | # assume graph is fully delegated or it will be too hard to handle |
| 109 | with open(f"{path_of_pte.parent}/{ctx_bin}", "rb") as f: |
| 110 | ctx_bin = preprocess_binary(f.read(), compiler_specs) |
| 111 | # leverage QNN pybind interface to retrieve tensor encodings |
| 112 | qnn_mgr = PyQnnManagerAdaptor.QnnManager( |
| 113 | generate_qnn_executorch_option(compiler_specs), ctx_bin |
| 114 | ) |
| 115 | assert qnn_mgr.Init().value == 0, "failed to initialize backend" |
| 116 | graph_name = qnn_mgr.GetGraphNames()[0] |
| 117 | qnn_mgr.AllocateTensor(graph_name) |
| 118 | fill_tensor_info(tensor_info, qnn_mgr.GetGraphInputs(graph_name), in_key) |
| 119 | fill_tensor_info(tensor_info, qnn_mgr.GetGraphOutputs(graph_name), out_key) |
| 120 | qnn_mgr.Destroy() |
| 121 | |
| 122 | return tensor_info |
| 123 | |
| 124 | |
| 125 | def get_input_list_description(type: str): |
no test coverage detected