(
inputs,
outputs,
qnn_in_order: Optional[List[int]] = None,
executorch_in_order: Optional[List[int]] = None,
executorch_out_order: Optional[List[int]] = None,
)
| 810 | ) |
| 811 | |
| 812 | def build_graph( |
| 813 | inputs, |
| 814 | outputs, |
| 815 | qnn_in_order: Optional[List[int]] = None, |
| 816 | executorch_in_order: Optional[List[int]] = None, |
| 817 | executorch_out_order: Optional[List[int]] = None, |
| 818 | ): |
| 819 | # custom op declaration |
| 820 | inputs_str = "Tensor[] inputs" |
| 821 | func_proto = f"{op_name}({inputs_str}) -> Any" |
| 822 | custom_op = Library(OpContextLoader.namespace, "FRAGMENT") |
| 823 | custom_op.define(func_proto) |
| 824 | # custom op implementation |
| 825 | implement_op(custom_op, op_name, outputs) |
| 826 | |
| 827 | # model architecture mimicking context binary |
| 828 | class Model(torch.nn.Module): |
| 829 | """ |
| 830 | The args of forward() can be thought of as what executorch is accepting as input. |
| 831 | The getattr inside the forward() can be thought of as qnn context binary. |
| 832 | When we first pass in the input, we need to use the executorch's(nn.module) input order. |
| 833 | After we get into forward(), we then need to convert input order to qnn's input order. |
| 834 | Same as return, when qnn returns the value, we need to reorder them back to executorh's output order. |
| 835 | """ |
| 836 | |
| 837 | def __init__(self, qnn_in_order, executorch_out_order): |
| 838 | super().__init__() |
| 839 | self.qnn_in_order = qnn_in_order |
| 840 | self.executorch_out_order = executorch_out_order |
| 841 | |
| 842 | def forward(self, *inputs): # executorch |
| 843 | if self.qnn_in_order: |
| 844 | inputs = tuple(inputs[i] for i in self.qnn_in_order) |
| 845 | ret = getattr( |
| 846 | getattr(torch.ops, OpContextLoader.namespace), op_name |
| 847 | ).default(inputs) |
| 848 | return ( |
| 849 | [ret[idx] for idx in self.executorch_out_order] |
| 850 | if self.executorch_out_order |
| 851 | else ret |
| 852 | ) |
| 853 | |
| 854 | inputs = ( |
| 855 | tuple(tuple(inputs.values())[i] for i in executorch_in_order) |
| 856 | if executorch_in_order |
| 857 | else tuple(inputs.values()) |
| 858 | ) |
| 859 | |
| 860 | model = Model(qnn_in_order, executorch_out_order) |
| 861 | prog = torch.export.export(model, inputs, strict=True) |
| 862 | # bookkeeping for variables' life cycle |
| 863 | return { |
| 864 | "custom_op": custom_op, |
| 865 | "custom_module": model, |
| 866 | "exported_program": prog, |
| 867 | } |
| 868 | |
| 869 | def build_tensor(tensors, dtype_map): |
no test coverage detected