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

Method maybe_create

backends/mlx/passes.py:69–162  ·  view source on GitHub ↗

Match RMSNorm pattern starting from final mul(weight, normalized).

(cls, head: Node, **context)

Source from the content-addressed store, hash-verified

67
68 @classmethod
69 def maybe_create(cls, head: Node, **context) -> Optional["RMSNormMatch"]:
70 """Match RMSNorm pattern starting from final mul(weight, normalized)."""
71 # Head must be mul
72 if not match_target(head, torch.ops.aten.mul.Tensor):
73 return None
74
75 if len(head.args) < 2:
76 return None
77
78 # Try both orderings: mul(weight, normalized) or mul(normalized, weight)
79 for weight_idx, norm_idx in [(0, 1), (1, 0)]:
80 weight_node = head.args[weight_idx]
81 norm_node = head.args[norm_idx]
82
83 if not isinstance(norm_node, Node):
84 continue
85
86 # Match entire chain with single walk_back:
87 # [_to_copy] -> mul(input, rsqrt) -> rsqrt -> add -> mean -> pow -> [_to_copy]
88 # The mul follows arg_index=1 to get rsqrt (not input)
89 result = walk_back(
90 norm_node,
91 [
92 OpStep(
93 op=torch.ops.aten._to_copy.default,
94 optional=True,
95 kwargs={
96 "dtype",
97 "layout",
98 "device",
99 "pin_memory",
100 "non_blocking",
101 "memory_format",
102 },
103 ),
104 OpStep(op=torch.ops.aten.mul.Tensor, nargs=2, arg_index=1),
105 OpStep(op=torch.ops.aten.rsqrt.default),
106 OpStep(op=torch.ops.aten.add.Tensor, nargs=2),
107 OpStep(op=torch.ops.aten.mean.dim, nargs=(2, 3), kwargs={"dtype"}),
108 OpStep(op=torch.ops.aten.pow.Tensor_Scalar, nargs=2),
109 OpStep(
110 op=torch.ops.aten._to_copy.default,
111 optional=True,
112 require_single_user=False, # _to_copy output used by both pow and mul
113 kwargs={
114 "dtype",
115 "layout",
116 "device",
117 "pin_memory",
118 "non_blocking",
119 "memory_format",
120 },
121 ),
122 ],
123 )
124 if result is None:
125 continue
126

Callers 1

callMethod · 0.45

Calls 5

match_targetFunction · 0.90
walk_backFunction · 0.90
OpStepClass · 0.90
keysMethod · 0.80

Tested by

no test coverage detected