constant folding for AvgPool2d
❓ Questions and Help
exporting simple AvgPool2d using torch_xla 2.3 results in two different stablehlo.reduce_window ops, the second one only takes args as constants. Is there a way to fold it into a constant in exported_program_to_stablehlo? @lsy323 @qihqi
e.g. %4 in the following example.
import torch
import torch.nn as nn
from torch_xla.stablehlo import exported_program_to_stablehlo
m = nn.AvgPool2d(kernel_size=2)
inp_args = (torch.randn(1, 4, 4),)
em = torch.export.export(m, inp_args)
stablehlo_program = exported_program_to_stablehlo(em)
print(stablehlo_program.get_stablehlo_text())
module @IrToHlo.26 attributes {mhlo.cross_program_prefetches = [], mhlo.is_dynamic = false, mhlo.use_auto_spmd_partitioning = false} {
func.func @main(%arg0: tensor<1x4x4xf32>) -> tensor<1x2x2xf32> {
%0 = stablehlo.constant dense<1.000000e+00> : tensor<4x4xf32>
%1 = stablehlo.constant dense<0.000000e+00> : tensor<f32>
%2 = stablehlo.reshape %arg0 : (tensor<1x4x4xf32>) -> tensor<1x1x4x4xf32>
%3 = "stablehlo.reduce_window"(%2, %1) ({
^bb0(%arg1: tensor<f32>, %arg2: tensor<f32>):
%8 = stablehlo.add %arg1, %arg2 : tensor<f32>
stablehlo.return %8 : tensor<f32>
}) {base_dilations = array<i64: 1, 1, 1, 1>, padding = dense<0> : tensor<4x2xi64>, window_dilations = array<i64: 1, 1, 1, 1>, window_dimensions = array<i64: 1, 1, 2, 2>, window_strides = array<i64: 1, 1, 2, 2>} : (tensor<1x1x4x4xf32>, tensor<f32>) -> tensor<1x1x2x2xf32>
%4 = "stablehlo.reduce_window"(%0, %1) ({
^bb0(%arg1: tensor<f32>, %arg2: tensor<f32>):
%8 = stablehlo.add %arg1, %arg2 : tensor<f32>
stablehlo.return %8 : tensor<f32>
}) {base_dilations = array<i64: 1, 1>, padding = dense<0> : tensor<2x2xi64>, window_dilations = array<i64: 1, 1>, window_dimensions = array<i64: 2, 2>, window_strides = array<i64: 2, 2>} : (tensor<4x4xf32>, tensor<f32>) -> tensor<2x2xf32>
%5 = stablehlo.reshape %4 : (tensor<2x2xf32>) -> tensor<1x1x2x2xf32>
%6 = stablehlo.divide %3, %5 : tensor<1x1x2x2xf32>
%7 = stablehlo.reshape %6 : (tensor<1x1x2x2xf32>) -> tensor<1x2x2xf32>
return %7 : tensor<1x2x2xf32>
}
}
I found this stablehlo program exported with torch_xla 2.2, which only has a single stablehlo.reduce_window
func.func @main(%15: tensor<1x64x114x114xf32>) -> tensor<1x64x56x56xf32> {
%6 = stablehlo.constant dense<1.0> : tensor<f32>
%0 = stablehlo.constant dense<9.0> : tensor<1x64x56x56xf32>
%16 = stablehlo.uniform_quantize %15 : (tensor<1x64x114x114xf32>) -> tensor<1x64x114x114x!quant.uniform<u8:f32, 3.303910e-03>>
%17 = stablehlo.uniform_dequantize %16 : (tensor<1x64x114x114x!quant.uniform<u8:f32, 3.303910e-03>>) -> tensor<1x64x114x114xf32>
%19 = "stablehlo.reduce_window"(%17, %6) ({
^bb0(%arg41: tensor<f32>, %arg42: tensor<f32>):
%193 = stablehlo.add %arg41, %arg42 : tensor<f32>
stablehlo.return %193 : tensor<f32>
}) {window_dimensions = array<i64: 1, 1, 3, 3>, window_strides = array<i64: 1, 1, 2, 2>} : (tensor<1x64x114x114xf32>, tensor<f32>) -> tensor<1x64x56x56xf32>
%divide = stablehlo.divide %19, %0 : tensor<1x64x56x56xf32>
%20 = stablehlo.uniform_quantize %divide : (tensor<1x64x56x56xf32>) -> tensor<1x64x56x56x!quant.uniform<u8:f32, 3.303910e-03>>
%21 = stablehlo.uniform_dequantize %20 : (tensor<1x64x56x56x!quant.uniform<u8:f32, 3.303910e-03>>) -> tensor<1x64x56x56xf32>
return %21 : tensor<1x64x56x56xf32>
}
this https://github.com/pytorch/xla/pull/6409 is the related change between 2.2 and 2.3, does that cause the expansion?
More generally, is there a way to guarantee a stablehlo export that is fully constant folded?
cc @JackCaoG
Hi @thong3le Thanks for reporting the issue! I'm not sure what causes the expansion of the 2nd reduce window, the PR mentioned above should be related. BTW the stablehlo in the 2 comments seem to be different, not sure if they are from the same model code (the 2nd one has qdq ops)
Hi @mfrancio, we don't do constant folding during lowering, in general, we rely on backend compiler to do the constant folding. I'm not aware of any MLIR pass can do the constant folding either cc @GleasonK in case I missed something.
@lsy323 Thanks for the quick reply. Since constant folding is not done as part of the export, I would be interested to know if there is a place (anywhere in the MLIR side of the pipeline) where constant folding is implemented so that we could leverage it as part of our pipeline.
Hello! There's plenty of places, and we're working on consolidating.
For now I'd say the most established is MHLO's canonicalizers and folders (Op::fold methods / use the MLIR canonicalizer pass when in MHLO):
https://github.com/openxla/xla/blob/c6df436f9e3d2a706a3a9595e37224276fb6013b/xla/mlir_hlo/mhlo/IR/hlo_ops.cc#L2209
In StableHLO we have some scattered in opt-in folding passes, this will be reworked in the following PR: https://github.com/openxla/stablehlo/pull/2253
Then lastly, we're looking into upstreaming some interesting canonicalizations from the jax-enzyme project, a research project that saw pretty notable performance gains by making clever canonicalizations that did things like pushing slice ops around (i.e. op->slice to slice->op) so that less computation needs to be done. Those are in: https://github.com/EnzymeAD/Enzyme-JAX/blob/main/src/enzyme_ad/jax/Passes/EnzymeHLOOpt.cpp
Now that I'm seeing the example, I'm not sure either has a folder for reduce_window, it may be possible to leverage the StableHLO reference interpreter to do some more general constant folding, or add a pattern for this op. Folding FP ops may lead to precision issues (depending on the op and folder impl).
Edit: looks like we have ReduceWindowOp::fold, but only for a small subset of the possible reduce_window ops: https://github.com/openxla/xla/blob/c6df436f9e3d2a706a3a9595e37224276fb6013b/xla/mlir_hlo/mhlo/IR/hlo_ops.cc#L3371
Thanks @GleasonK for the pointers - i was indeed expecting something similar in the xla pipeline. It would be great to see those canonicalizations consolidated in the stablehlo repo! :)
thanks so much for your support @lsy323 @GleasonK