55# LICENSE file in the root directory of this source tree.
66
77import torch
8- from executorch .exir .dialects ._ops import ops as exir_ops
9- from executorch .exir .dialects .edge ._ops import EdgeOpOverload
108from executorch .exir .pass_base import ExportPass , PassResult
11- from torchao .quantization .pt2e .utils import get_new_attr_name_with_prefix
129
13- from .utils import copy_meta , create_const_node
10+ from .utils import copy_meta
1411
1512
1613class DecomposeVar (ExportPass ):
@@ -29,17 +26,12 @@ def __init__(self):
2926 self .var_targets = {
3027 torch .ops .aten .var .correction ,
3128 torch .ops .aten .var .dim ,
32- exir_ops .edge .aten .var .correction ,
33- exir_ops .edge .aten .var .dim ,
3429 }
3530
3631 def _get_correction (self , node ):
3732 """Extract the correction factor from node args based on op variant."""
3833 target = node .target
39- if target in (
40- torch .ops .aten .var .correction ,
41- exir_ops .edge .aten .var .correction ,
42- ):
34+ if target in {torch .ops .aten .var .correction }:
4335 # var.correction(Tensor self, int[1]? dim=None, *, Scalar? correction=None, bool keepdim=False)
4436 # correction is a kwarg, but in the graph it may appear in kwargs
4537 correction = node .kwargs .get ("correction" , None )
@@ -54,10 +46,7 @@ def _get_correction(self, node):
5446 def _get_dim_and_keepdim (self , node ):
5547 """Extract dim and keepdim from node args based on op variant."""
5648 target = node .target
57- if target in (
58- torch .ops .aten .var .correction ,
59- exir_ops .edge .aten .var .correction ,
60- ):
49+ if target in {torch .ops .aten .var .correction }:
6150 # var.correction(Tensor self, int[1]? dim=None, *, Scalar? correction=None, bool keepdim=False)
6251 dim = node .args [1 ] if len (node .args ) > 1 else None
6352 keepdim = node .kwargs .get ("keepdim" , False )
@@ -70,31 +59,18 @@ def _get_dim_and_keepdim(self, node):
7059
7160 def call (self , graph_module : torch .fx .GraphModule ):
7261 graph = graph_module .graph
73- const_cache = {}
7462
7563 for node in list (graph .nodes ):
7664 if node .op == "call_function" and node .target in self .var_targets :
7765 x_node = node .args [0 ]
78- is_edge = isinstance (node .target , EdgeOpOverload )
7966 meta = node .meta
8067
8168 correction = self ._get_correction (node )
8269 dim , keepdim = self ._get_dim_and_keepdim (node )
8370
84- mean_op = (
85- exir_ops .edge .aten .mean .dim if is_edge else torch .ops .aten .mean .dim
86- )
87- sub_op = (
88- exir_ops .edge .aten .sub .Tensor
89- if is_edge
90- else torch .ops .aten .sub .Tensor
91- )
92- mul_op = (
93- exir_ops .edge .aten .mul .Tensor
94- if is_edge
95- else torch .ops .aten .mul .Tensor
96- )
97-
71+ mean_op = torch .ops .aten .mean .dim
72+ sub_op = torch .ops .aten .sub .Tensor
73+ mul_op = torch .ops .aten .mul .Tensor
9874 # Handle dim=None: reduce over all dimensions
9975 input_shape = node .args [0 ].meta ["val" ].shape
10076 if dim is None :
@@ -148,22 +124,8 @@ def call(self, graph_module: torch.fx.GraphModule):
148124 # Guard against division by zero (e.g. single-element dim with correction=1).
149125 # Using inf matches the native PyTorch behavior where 0 * inf → nan.
150126 scale = float ("inf" ) if denom == 0 else float (n ) / denom
151-
152- if is_edge :
153- cache_key = ("_var_scale_" , scale )
154- if cache_key not in const_cache :
155- attr_name = get_new_attr_name_with_prefix (
156- "_var_scale_const_"
157- )(graph_module )
158- const_cache [cache_key ] = create_const_node (
159- graph , graph_module , attr_name , scale , node
160- )
161- scale_node = const_cache [cache_key ]
162- else :
163- scale_node = scale
164-
165127 result_node = graph .create_node (
166- "call_function" , mul_op , (var_node , scale_node )
128+ "call_function" , mul_op , (var_node , scale )
167129 )
168130 result_node .meta = copy_meta (meta )
169131 else :
0 commit comments