| 25 | # model authoring side. |
| 26 | @dataclass(frozen=True) |
| 27 | class ComputeNativeFunctionStub: |
| 28 | @method_with_native_function |
| 29 | def __call__(self, f: NativeFunction) -> str | None: |
| 30 | if Variant.function not in f.variants: |
| 31 | return None |
| 32 | |
| 33 | sig = DispatcherSignature.from_schema( |
| 34 | f.func, prefix=f"wrapper_CPU_{f.func.name.overload_name}_", symint=False |
| 35 | ) |
| 36 | assert sig is not None |
| 37 | if len(f.func.returns) == 0: |
| 38 | ret_name = "" |
| 39 | elif len(f.func.returns) == 1: |
| 40 | if f.func.arguments.out: |
| 41 | ret_name = f.func.arguments.out[0].name |
| 42 | else: |
| 43 | ret_name = next( |
| 44 | ( |
| 45 | a.name |
| 46 | for a in f.func.arguments.flat_non_out |
| 47 | if a.type == f.func.returns[0].type |
| 48 | ), |
| 49 | "", |
| 50 | ) |
| 51 | if not ret_name: |
| 52 | # if return type is tensor |
| 53 | if f.func.returns[0].type == BaseType(BaseTy.Tensor): |
| 54 | # Returns an empty tensor |
| 55 | ret_name = "at::Tensor()" |
| 56 | else: |
| 57 | raise Exception( # noqa: TRY002 |
| 58 | f"Can't handle this return type {f.func}" |
| 59 | ) # noqa: TRY002 |
| 60 | elif len(f.func.arguments.out) == len(f.func.returns): |
| 61 | # Returns a tuple of out arguments |
| 62 | tensor_type = "at::Tensor &" |
| 63 | comma = ", " |
| 64 | ret_name = f"""::std::tuple<{comma.join([tensor_type] * len(f.func.returns))}>( |
| 65 | {comma.join([r.name for r in f.func.arguments.out])} |
| 66 | )""" |
| 67 | else: |
| 68 | assert all( |
| 69 | a.type == BaseType(BaseTy.Tensor) for a in f.func.returns |
| 70 | ), f"Only support tensor returns but got {f.func.returns}" |
| 71 | # Returns a tuple of empty tensors |
| 72 | tensor_type = "at::Tensor" |
| 73 | comma = ", " |
| 74 | ret_name = f"""::std::tuple<{comma.join([tensor_type] * len(f.func.returns))}>( |
| 75 | {comma.join(["at::Tensor()" for _ in f.func.returns])} |
| 76 | )""" |
| 77 | ret_str = f"return {ret_name};" if len(f.func.returns) > 0 else "" |
| 78 | return f""" |
| 79 | {sig.defn()} {{ |
| 80 | {ret_str} |
| 81 | }} |
| 82 | """ |
| 83 | |
| 84 |
no outgoing calls