[Torch] Expose non-finite initializer option (#25138) I ran into this while compiling a PyTorch training graph through `Turbine → IREE → WebGPU` inside JupyterLite. In a LLVM/MLIR 23.1.2 wasm64 build, a minimal FP32 l`og_softmax` on a 4×10 tensor aborts in Tint with `TINT_ASSERT(std::isfinite(v.value))`. Selecting Torch-MLIR’s existing finite-initializer option allows it to compile successfully. Torch-MLIR already provides `allow-non-finites=false` for using finite initializers. This exposes the option as `--iree-torch-allow-non-finites` and forwards it through the existing Torch-to-Linalg, Torch-to-TMTensor and ONNX conversions. We should keep the default as true. Signed-off-by: anutosh491 <andersonbhat491@gmail.com>
diff --git a/compiler/plugins/input/Torch/InputConversion/Passes.cpp b/compiler/plugins/input/Torch/InputConversion/Passes.cpp index 72d3ca7..23a7bf8 100644 --- a/compiler/plugins/input/Torch/InputConversion/Passes.cpp +++ b/compiler/plugins/input/Torch/InputConversion/Passes.cpp
@@ -60,7 +60,8 @@ pm.addNestedPass<func::FuncOp>(torch::Torch::createFuseQuantizedOpsPass()); pm.addNestedPass<func::FuncOp>(createCanonicalizerPass()); pm.addNestedPass<func::FuncOp>(torch::Torch::createScalarizeShapesPass()); - pm.addNestedPass<func::FuncOp>(torch::createConvertTorchToTMTensorPass()); + pm.addNestedPass<func::FuncOp>( + torch::createConvertTorchToTMTensorPass(options.allowNonFinites)); pm.addNestedPass<func::FuncOp>( TorchInput::createConvertTMTensorToLinalgExtPass()); pm.addNestedPass<func::FuncOp>(torch::createConvertTorchToTensorPass()); @@ -68,7 +69,8 @@ TorchInput::createConvertTorchUnstructuredToLinalgExtPass()); pm.addNestedPass<func::FuncOp>( TorchInput::createConvertTorchQuantizationToLinalgExtPass()); - pm.addNestedPass<func::FuncOp>(torch::createConvertTorchToLinalgPass()); + pm.addNestedPass<func::FuncOp>( + torch::createConvertTorchToLinalgPass(options.allowNonFinites)); pm.addNestedPass<func::FuncOp>(createCSEPass()); pm.addNestedPass<func::FuncOp>(torch::createConvertTorchToSCFPass()); pm.addNestedPass<func::FuncOp>(torch::createConvertTorchToArithPass());
diff --git a/compiler/plugins/input/Torch/InputConversion/Passes.h b/compiler/plugins/input/Torch/InputConversion/Passes.h index bf9592a..2e3e7dd 100644 --- a/compiler/plugins/input/Torch/InputConversion/Passes.h +++ b/compiler/plugins/input/Torch/InputConversion/Passes.h
@@ -42,6 +42,10 @@ Option<bool> decompose{*this, "decompose", llvm::cl::desc("Decompose complex torch operations."), llvm::cl::init(true)}; + Option<bool> allowNonFinites{ + *this, "allow-non-finites", + llvm::cl::desc("Allow non-finite values in Torch lowering initializers."), + llvm::cl::init(true)}; Option<bool> externalizeTransients{ *this, "externalize-transients", llvm::cl::desc(
diff --git a/compiler/plugins/input/Torch/InputConversion/test/auto_input_conversion.mlir b/compiler/plugins/input/Torch/InputConversion/test/auto_input_conversion.mlir index 89c00e0..84c722e 100644 --- a/compiler/plugins/input/Torch/InputConversion/test/auto_input_conversion.mlir +++ b/compiler/plugins/input/Torch/InputConversion/test/auto_input_conversion.mlir
@@ -1,4 +1,5 @@ -// RUN: iree-compile --compile-to=input --split-input-file %s | FileCheck %s +// RUN: iree-compile --compile-to=input --split-input-file %s | FileCheck %s --check-prefixes=CHECK,NONFINITE +// RUN: iree-compile --compile-to=input --split-input-file --iree-torch-allow-non-finites=false %s | FileCheck %s --check-prefixes=CHECK,FINITE // Check that the auto input conversion pipeline uses this plugin. @@ -19,3 +20,13 @@ func.func @nop(%arg0: !torch.vtensor<[5],f32>) -> !torch.vtensor<[5],f32> attributes {torch.assume_strict_symbolic_shapes} { return %arg0 : !torch.vtensor<[5],f32> } + +// ----- + +// CHECK-LABEL: util.func public @empty_reduce_max$async( +// NONFINITE: arith.constant dense<0xFF800000> : tensor<1xf32> +// FINITE: arith.constant dense<-3.40282347E+38> : tensor<1xf32> +func.func @empty_reduce_max(%input: !torch.vtensor<[0],f32>) -> !torch.vtensor<[1],f32> attributes {torch.onnx_meta.opset_version = 13 : si64} { + %result = torch.operator "onnx.ReduceMax"(%input) : (!torch.vtensor<[0],f32>) -> !torch.vtensor<[1],f32> + return %result : !torch.vtensor<[1],f32> +}
diff --git a/compiler/plugins/input/Torch/InputConversion/test/torch_to_iree.mlir b/compiler/plugins/input/Torch/InputConversion/test/torch_to_iree.mlir index c827643..8ef3a6c 100644 --- a/compiler/plugins/input/Torch/InputConversion/test/torch_to_iree.mlir +++ b/compiler/plugins/input/Torch/InputConversion/test/torch_to_iree.mlir
@@ -1,4 +1,7 @@ -// RUN: iree-opt --split-input-file --torch-to-iree %s | FileCheck %s +// RUN: iree-opt --split-input-file --torch-to-iree %s | FileCheck %s --check-prefixes=CHECK,NONFINITE +// RUN: iree-opt --split-input-file --torch-to-iree="allow-non-finites=false" %s | FileCheck %s --check-prefixes=CHECK,FINITE +// RUN: iree-compile --compile-to=input --split-input-file %s | FileCheck %s --check-prefixes=CHECK,NONFINITE +// RUN: iree-compile --compile-to=input --split-input-file --iree-torch-allow-non-finites=false %s | FileCheck %s --check-prefixes=CHECK,FINITE // This is just a smoke test that the pipeline functions, and it also // validates any features different from upstream. @@ -72,3 +75,29 @@ -> !torch.vtensor<[4,8],f32> return %result : !torch.vtensor<[4,8],f32> } + +// ----- + +// CHECK-LABEL: util.func public @amax$async( +// NONFINITE: arith.constant 0xFF800000 : f32 +// FINITE: arith.constant -3.40282347E+38 : f32 +func.func @amax(%input: !torch.vtensor<[4,8],f32>) -> !torch.vtensor<[4],f32> { + %dim = torch.constant.int 1 + %dims = torch.prim.ListConstruct %dim : (!torch.int) -> !torch.list<int> + %false = torch.constant.bool false + %result = torch.aten.amax %input, %dims, %false : !torch.vtensor<[4,8],f32>, !torch.list<int>, !torch.bool -> !torch.vtensor<[4],f32> + return %result : !torch.vtensor<[4],f32> +} + +// ----- + +// CHECK-LABEL: util.func public @scatter_amin$async( +// NONFINITE: arith.constant 0x7C00 : f16 +// FINITE: arith.constant 6.550400e+04 : f16 +func.func @scatter_amin(%input: !torch.vtensor<[8],f16>, %indices: !torch.vtensor<[4],si64>, %updates: !torch.vtensor<[4],f16>) -> !torch.vtensor<[8],f16> { + %dim = torch.constant.int 0 + %reduce = torch.constant.str "amin" + %false = torch.constant.bool false + %result = torch.aten.scatter_reduce.two %input, %dim, %indices, %updates, %reduce, %false : !torch.vtensor<[8],f16>, !torch.int, !torch.vtensor<[4],si64>, !torch.vtensor<[4],f16>, !torch.str, !torch.bool -> !torch.vtensor<[8],f16> + return %result : !torch.vtensor<[8],f16> +}
diff --git a/compiler/plugins/input/Torch/PluginRegistration.cpp b/compiler/plugins/input/Torch/PluginRegistration.cpp index 4dff2dd..dfa0a3d 100644 --- a/compiler/plugins/input/Torch/PluginRegistration.cpp +++ b/compiler/plugins/input/Torch/PluginRegistration.cpp
@@ -29,6 +29,7 @@ struct TorchOptions { bool strictSymbolicShapes = true; bool decompose = true; + bool allowNonFinites = true; bool externalizeTransients = false; bool enableShapeRefinement = false; bool emitAsyncEntryPoints = true; @@ -42,6 +43,12 @@ llvm::cl::cat(category), llvm::cl::desc("Decompose complex torch operations.")); binder.opt<bool>( + "iree-torch-allow-non-finites", allowNonFinites, + llvm::cl::cat(category), + llvm::cl::desc( + "Allow non-finite values in Torch lowering initializers. " + "When disabled, use the nearest finite values.")); + binder.opt<bool>( "iree-torch-externalize-transients", externalizeTransients, llvm::cl::cat(category), llvm::cl::desc("If enabled, an external hal buffer will be appended to " @@ -105,6 +112,7 @@ // ONNX input is a pre-processing step to torch. mlir::torch::Torch::TorchLoweringPipelineOptions torchOnnxPipelineOptions; torchOnnxPipelineOptions.decompose = options.decompose; + torchOnnxPipelineOptions.allowNonFinites = options.allowNonFinites; torchOnnxPipelineOptions.backendLegalOps = TorchInput::BackendLegalOps::get(); mlir::torch::Torch::createTorchOnnxToTorchBackendPipeline( @@ -115,6 +123,7 @@ TorchInput::TorchToIREELoweringPipelineOptions torchOptions; torchOptions.strictSymbolicShapes = options.strictSymbolicShapes; torchOptions.decompose = options.decompose; + torchOptions.allowNonFinites = options.allowNonFinites; torchOptions.externalizeTransients = options.externalizeTransients; torchOptions.enableShapeRefinement = options.enableShapeRefinement; torchOptions.emitAsyncEntryPoints = options.emitAsyncEntryPoints;