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

Method gen_from_yaml

codegen/model.py:64–126  ·  view source on GitHub ↗

Generate ETKernelKeys from arg kernel specs Multiple ETKernelKeys are returned due to dtype permutations from utilizing type_alias_map (actualizing each potential type permutation as a KernelKey) Args: args: Mapping from argument name to kernel specs

(
        args: dict[str, tuple[str, str]],
        type_alias_map: dict[str, list[str]],  # TODO: Support unwrapped str val
        dim_order_alias_map: dict[str, list[int]],
    )

Source from the content-addressed store, hash-verified

62
63 @staticmethod
64 def gen_from_yaml(
65 args: dict[str, tuple[str, str]],
66 type_alias_map: dict[str, list[str]], # TODO: Support unwrapped str val
67 dim_order_alias_map: dict[str, list[int]],
68 ) -> list[ETKernelKey]:
69 """Generate ETKernelKeys from arg kernel specs
70 Multiple ETKernelKeys are returned due to dtype permutations from utilizing
71 type_alias_map (actualizing each potential type permutation as a KernelKey)
72
73 Args:
74 args: Mapping from argument name to kernel specs
75 Kernel specs are a tuple of (dtype, dim_order).
76 Currently tuple entries must be aliased via the alias map arguments
77 type_alias_map: Mapping from type alias to potential type enums
78 i.e { T0 : [Double, Int] } means T0 can be either Double or Int
79 Used for lookup by args
80 dim_order_alias_map: Mapping from alias to a list of dimension orders
81 Used for lookup by args
82 """
83 # Cast to dim order to int
84 dim_order_alias_map = {
85 k: [int(alias) for alias in v] for k, v in dim_order_alias_map.items()
86 }
87 kernel_keys = []
88
89 # Get all used Dtype Alias
90 dtype_alias_used = set()
91 for type_alias, dim_order in args.values():
92 # Enforce usage of alias initially
93 # TODO: Support inlined arguments
94 assert type_alias in type_alias_map, "Undefined type alias: " + str(
95 type_alias
96 )
97 assert (
98 dim_order in dim_order_alias_map
99 ), f"Undefined dim_order alias: {dim_order}"
100 dtype_alias_used.add(type_alias)
101
102 # Generate all permutations of dtype alias values
103 alias_dtypes = [
104 [(alias, dtype) for dtype in type_alias_map[alias]]
105 for alias in dtype_alias_used
106 ]
107 alias_permutations = [
108 dict(permutation) for permutation in list(itertools.product(*alias_dtypes))
109 ]
110
111 # Using each alias value permutation, generate kernel keys
112 op_arg_cache = {}
113 for permutation in alias_permutations:
114 arg_list = []
115 for arg_name, arg_spec in args.items():
116 dtype = permutation[arg_spec[0]]
117 dim_order = dim_order_alias_map[arg_spec[1]] # type: ignore[assignment]
118 if (
119 cache_key := (arg_name, dtype, tuple(dim_order))
120 ) not in op_arg_cache:
121 op_arg_cache[cache_key] = ETKernelKeyOpArgMeta(*cache_key) # type: ignore[arg-type]

Calls 6

ETKernelKeyClass · 0.85
itemsMethod · 0.80
valuesMethod · 0.80
addMethod · 0.45
appendMethod · 0.45