| 792 | |
| 793 | |
| 794 | def from_context_binary( # noqa: C901 |
| 795 | ctx_path: str | bytes, |
| 796 | op_name: str, |
| 797 | soc_model: QcomChipset = QcomChipset.SM8650, |
| 798 | custom_info: Dict = None, |
| 799 | ): |
| 800 | from pathlib import Path |
| 801 | |
| 802 | def implement_op(custom_op, op_name, outputs): |
| 803 | @torch.library.impl( |
| 804 | custom_op, str(op_name), dispatch_key="CompositeExplicitAutograd" |
| 805 | ) |
| 806 | def op_impl(inputs: List[torch.Tensor]): |
| 807 | return tuple( |
| 808 | torch.zeros(tuple(v.shape), device="meta", dtype=v.dtype) |
| 809 | for v in outputs.values() |
| 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 |