MCPcopy Create free account
hub / github.com/pytorch/executorch / Unboxing

Class Unboxing

codegen/api/unboxing.py:32–232  ·  view source on GitHub ↗

Takes a sequence of Bindings and unbox EValues to these Bindings. Return generated code that performs correct unboxing. A sample generated code: // aten::mul.out(Tensor self, Tensor other, *, Tensor(a!) out) -> Tensor(a!) void mul_out(Span stack) { EValue& self = *s

Source from the content-addressed store, hash-verified

30
31@dataclass(frozen=True)
32class Unboxing:
33 """
34 Takes a sequence of Bindings and unbox EValues to these Bindings. Return generated code that performs correct unboxing.
35 A sample generated code:
36 // aten::mul.out(Tensor self, Tensor other, *, Tensor(a!) out) -> Tensor(a!)
37 void mul_out(Span<EValue*> stack) {
38 EValue& self = *stack[0];
39 EValue& other = *stack[1];
40 EValue& out = *stack[2];
41 const torch::executor::Tensor & self_base = self.to<torch::executor::Tensor>();
42 const torch::executor::Tensor & other_base = other.to<torch::executor::Tensor>();
43 torch::executor::Tensor & out_base = out.to<torch::executor::Tensor>();
44
45 EXECUTORCH_SCOPE_PROF("native_call_mul.out");
46 torch::executor::mul_outf(self_base, other_base, out_base);
47
48
49 }
50 """
51
52 # this is a callable that converts a JIT argument, into its C++ type.
53 # Translates (type, mutability, binds) to NamedCType. E.g., torchgen.api.cpp.argumenttype_type.
54 argument_type_gen: Callable[
55 ...,
56 NamedCType,
57 ]
58
59 # Convert all the arguments in a NativeFunction to C++ code
60 def convert_arguments(
61 self, args: Sequence[Binding]
62 ) -> tuple[list[Binding], list[str]]:
63 code_list = [f"EValue& {args[i].name} = *stack[{i}];" for i in range(len(args))]
64 binding_list = []
65 for arg in args:
66 # expecting only Argument
67 if not isinstance(arg.argument, Argument):
68 raise Exception( # noqa: TRY002
69 f"Unexpected argument type, expecting `Argument` but got {arg}"
70 )
71 argument: Argument = arg.argument
72 unboxed_name, _, code, decl = self.argumenttype_evalue_convert(
73 argument.type, argument.name, mutable=argument.is_write
74 )
75 code_list.extend(decl)
76 code_list.extend(code)
77 binding_list.append(arg.with_name(unboxed_name))
78 return binding_list, code_list
79
80 def argumenttype_evalue_convert(
81 self, t: Type, arg_name: str, *, mutable: bool = False
82 ) -> tuple[str, CType, list[str], list[str]]:
83 """
84 Takes in the type, name and mutability corresponding to an argument, and generates a tuple of:
85 (1) the C++ code necessary to unbox the argument
86 (2) A Binding corresponding to the newly created unboxed variable, including variable name and its CType
87 :param t: a `Type` of an argument
88 :param arg_name: argument name
89 :param mutable: boolean for whether this argument type is mutable

Callers 2

__call__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected