2021-04-28 06:15:50 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2021-09-30 00:03:40 +08:00
|
|
|
// Also available under a BSD-style license. See LICENSE.
|
2021-04-28 06:15:50 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2022-08-11 03:12:51 +08:00
|
|
|
#include "torch-mlir/Conversion/TorchToArith/TorchToArith.h"
|
2021-04-28 06:15:50 +08:00
|
|
|
|
|
|
|
#include "../PassDetail.h"
|
2022-10-05 21:28:06 +08:00
|
|
|
#include "mlir/Dialect/Arith/IR/Arith.h"
|
2022-02-13 02:47:12 +08:00
|
|
|
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
|
2022-04-25 21:12:45 +08:00
|
|
|
#include "mlir/Dialect/Math/IR/Math.h"
|
2022-01-05 00:00:48 +08:00
|
|
|
#include "mlir/Dialect/Tensor/IR/Tensor.h"
|
2022-08-25 06:10:40 +08:00
|
|
|
#include "mlir/IR/DialectResourceBlobManager.h"
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
#include "mlir/Transforms/DialectConversion.h"
|
2022-04-25 21:12:45 +08:00
|
|
|
#include "torch-mlir/Conversion/Utils/Utils.h"
|
[torch-mlir earthmoving (1/N)] C/C++ code movement.
This creates the `external/torch-mlir` directory as an
LLVM_EXTERNAL_PROJECTS-compatible project (analogous to
`iree-dialects`) and completes movement/rename of all pure MLIR C/C++
compiler code into there. The next step will be to move all the Python
code / code that links/includes PyTorch C++ code (which currently lives
in `frontends/pytorch`) into a subdirectory here.
I call this "earthmoving" because it is mostly mechanical changes and
renames. As a quick summary (we can change this down the road easily)
- C++ `mlir::NPCOMP::Torch -> mlir::torch::Torch`
- CAPI `npcompTorchListTypeGet -> torchMlirTorchListTypeGet`
- preprocessor `#ifndef NPCOMP_ -> #ifndef TORCHMLIR_`
- CMake `NPCOMPFoo -> TorchMLIRFoo`
The goal of this is to create a standalone project creating a center of
mass for entry into the MLIR ecosystem from PyTorch, suitable in scope
for eventual inclusion/ownership in PyTorch. The idea is that
`external/torch-mlir` will some day be pulled out into its own
repository, and then npcomp will simply pull it in as a submodule.
Layering-wise, what lives in `torch-mlir` lowers code from PyTorch
(currently TorchScript, but TorchFX or pytorch/xla-style tracing are
possible extensions) down to what we have been calling the "Torch
backend contract" which is cleaned up IR (inlining, simplifcation,
conversion to value tensors, ...) entirely in the `torch` dialect. This
is the branching off point for further lowering, of which npcomp takes
one opinion (outside `torch-mlir` of course!), namely the
`TorchConversion` dialect/transforms which lower to IR suitable for IREE
and other linalg-on-tensors based lower-level compilers.
Summary of changes:
- move `{include,lib,test}/Dialect/Torch` into `torch-mlir`
- move relevant parts of CAPI into `torch-mlir`.
- leave a few things related to the `torch-mlir` Python build commented
out, which should be resolved in a subsequent change.
2021-09-10 03:24:10 +08:00
|
|
|
#include "torch-mlir/Dialect/Torch/IR/TorchDialect.h"
|
|
|
|
#include "torch-mlir/Dialect/Torch/IR/TorchOps.h"
|
2022-05-23 23:26:02 +08:00
|
|
|
#include "torch-mlir/Dialect/Torch/Utils/Utils.h"
|
2021-09-24 11:59:12 +08:00
|
|
|
#include "torch-mlir/Dialect/TorchConversion/Transforms/BackendTypeConversion.h"
|
2021-04-28 06:15:50 +08:00
|
|
|
|
|
|
|
using namespace mlir;
|
2021-09-24 11:59:12 +08:00
|
|
|
using namespace mlir::torch;
|
[torch-mlir earthmoving (1/N)] C/C++ code movement.
This creates the `external/torch-mlir` directory as an
LLVM_EXTERNAL_PROJECTS-compatible project (analogous to
`iree-dialects`) and completes movement/rename of all pure MLIR C/C++
compiler code into there. The next step will be to move all the Python
code / code that links/includes PyTorch C++ code (which currently lives
in `frontends/pytorch`) into a subdirectory here.
I call this "earthmoving" because it is mostly mechanical changes and
renames. As a quick summary (we can change this down the road easily)
- C++ `mlir::NPCOMP::Torch -> mlir::torch::Torch`
- CAPI `npcompTorchListTypeGet -> torchMlirTorchListTypeGet`
- preprocessor `#ifndef NPCOMP_ -> #ifndef TORCHMLIR_`
- CMake `NPCOMPFoo -> TorchMLIRFoo`
The goal of this is to create a standalone project creating a center of
mass for entry into the MLIR ecosystem from PyTorch, suitable in scope
for eventual inclusion/ownership in PyTorch. The idea is that
`external/torch-mlir` will some day be pulled out into its own
repository, and then npcomp will simply pull it in as a submodule.
Layering-wise, what lives in `torch-mlir` lowers code from PyTorch
(currently TorchScript, but TorchFX or pytorch/xla-style tracing are
possible extensions) down to what we have been calling the "Torch
backend contract" which is cleaned up IR (inlining, simplifcation,
conversion to value tensors, ...) entirely in the `torch` dialect. This
is the branching off point for further lowering, of which npcomp takes
one opinion (outside `torch-mlir` of course!), namely the
`TorchConversion` dialect/transforms which lower to IR suitable for IREE
and other linalg-on-tensors based lower-level compilers.
Summary of changes:
- move `{include,lib,test}/Dialect/Torch` into `torch-mlir`
- move relevant parts of CAPI into `torch-mlir`.
- leave a few things related to the `torch-mlir` Python build commented
out, which should be resolved in a subsequent change.
2021-09-10 03:24:10 +08:00
|
|
|
using namespace mlir::torch::Torch;
|
2021-04-28 06:15:50 +08:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Patterns (as this grows, it should be organized into multiple files)
|
|
|
|
// -----------------------------------------------------------------------------
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
// This is going to eventually be O(#torch operators), which is in the 100s.
|
2021-04-28 06:15:50 +08:00
|
|
|
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
namespace {
|
2021-04-28 06:15:50 +08:00
|
|
|
// Note: Confusingly, ATen's "dim" means "number of dimensions" which is what
|
|
|
|
// MLIR calls "rank".
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
class ConvertAtenDimOp : public OpConversionPattern<AtenDimOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
LogicalResult
|
2021-11-16 07:00:53 +08:00
|
|
|
matchAndRewrite(AtenDimOp op, OpAdaptor adaptor,
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2024-01-30 01:59:33 +08:00
|
|
|
auto rank =
|
|
|
|
rewriter.create<tensor::RankOp>(op->getLoc(), adaptor.getSelf());
|
2021-10-16 02:34:29 +08:00
|
|
|
rewriter.replaceOpWithNewOp<arith::IndexCastOp>(
|
2021-06-17 06:53:15 +08:00
|
|
|
op, getTypeConverter()->convertType(op.getType()), rank);
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
2021-04-28 06:15:50 +08:00
|
|
|
|
2022-02-08 22:18:08 +08:00
|
|
|
namespace {
|
|
|
|
class ConvertRuntimeAssertOp : public OpConversionPattern<RuntimeAssertOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(RuntimeAssertOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2022-12-08 04:20:41 +08:00
|
|
|
rewriter.replaceOpWithNewOp<cf::AssertOp>(op, adaptor.getCondition(),
|
|
|
|
adaptor.getMessage());
|
2022-02-08 22:18:08 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2021-11-24 08:16:32 +08:00
|
|
|
namespace {
|
2021-12-17 23:24:56 +08:00
|
|
|
template <typename AtenOp, typename BinOp>
|
|
|
|
class ConvertAtenBinaryOp : public OpConversionPattern<AtenOp> {
|
2021-11-24 08:16:32 +08:00
|
|
|
public:
|
2021-12-17 23:24:56 +08:00
|
|
|
using OpConversionPattern<AtenOp>::OpConversionPattern;
|
2021-11-24 08:16:32 +08:00
|
|
|
LogicalResult
|
2021-12-17 23:24:56 +08:00
|
|
|
matchAndRewrite(AtenOp op,
|
|
|
|
typename OpConversionPattern<AtenOp>::OpAdaptor adaptor,
|
2021-11-24 08:16:32 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2024-08-08 00:55:27 +08:00
|
|
|
Value a = adaptor.getA();
|
|
|
|
Value b = adaptor.getB();
|
|
|
|
if (llvm::is_one_of<AtenOp, AtenAddFloatIntOp>::value)
|
|
|
|
b = convertScalarToDtype(rewriter, op.getLoc(), b, a.getType());
|
|
|
|
rewriter.template replaceOpWithNewOp<BinOp>(op, a, b);
|
2021-11-24 08:16:32 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2022-04-25 21:12:45 +08:00
|
|
|
namespace {
|
|
|
|
template <typename AtenOp, typename UnaryOp>
|
2022-05-19 22:54:16 +08:00
|
|
|
class ConvertAtenUnaryOpToFloatMathOp : public OpConversionPattern<AtenOp> {
|
2022-04-25 21:12:45 +08:00
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOp>::OpConversionPattern;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(AtenOp op,
|
|
|
|
typename OpConversionPattern<AtenOp>::OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2022-05-19 22:54:16 +08:00
|
|
|
Location loc = op.getLoc();
|
2022-12-08 04:20:41 +08:00
|
|
|
Value input = adaptor.getA();
|
2022-04-25 21:12:45 +08:00
|
|
|
Type resultType =
|
|
|
|
this->getTypeConverter()->convertType(op->getResult(0).getType());
|
2024-04-28 05:00:56 +08:00
|
|
|
if (!isa<mlir::FloatType>(input.getType()))
|
2022-05-19 22:54:16 +08:00
|
|
|
input = convertScalarToDtype(rewriter, loc, input, rewriter.getF64Type());
|
|
|
|
Value result = rewriter.create<UnaryOp>(loc, input);
|
|
|
|
rewriter.replaceOp(op,
|
|
|
|
convertScalarToDtype(rewriter, loc, result, resultType));
|
2022-04-25 21:12:45 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2022-10-06 21:11:52 +08:00
|
|
|
namespace {
|
|
|
|
class ConvertAtenDivIntOp : public OpConversionPattern<AtenDivIntOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenDivIntOp>::OpConversionPattern;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(AtenDivIntOp op,
|
|
|
|
typename OpConversionPattern<AtenDivIntOp>::OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
Location loc = op.getLoc();
|
2024-01-30 01:59:33 +08:00
|
|
|
Value a = convertScalarToDtype(rewriter, loc, adaptor.getA(),
|
|
|
|
rewriter.getF64Type());
|
|
|
|
Value b = convertScalarToDtype(rewriter, loc, adaptor.getB(),
|
|
|
|
rewriter.getF64Type());
|
2022-10-06 21:11:52 +08:00
|
|
|
rewriter.replaceOpWithNewOp<arith::DivFOp>(op, a, b);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2021-06-17 06:53:15 +08:00
|
|
|
namespace {
|
2022-02-12 05:39:30 +08:00
|
|
|
// Lowers aten integer comparison ops.
|
|
|
|
template <typename AtenOp, arith::CmpIPredicate Pred>
|
|
|
|
class ConvertAtenIntComparisonOp : public OpConversionPattern<AtenOp> {
|
2021-06-17 06:53:15 +08:00
|
|
|
public:
|
2022-02-12 05:39:30 +08:00
|
|
|
using OpConversionPattern<AtenOp>::OpConversionPattern;
|
2021-06-17 06:53:15 +08:00
|
|
|
LogicalResult
|
2022-02-12 05:39:30 +08:00
|
|
|
matchAndRewrite(AtenOp op,
|
|
|
|
typename OpConversionPattern<AtenOp>::OpAdaptor adaptor,
|
2021-06-17 06:53:15 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2022-12-08 04:20:41 +08:00
|
|
|
rewriter.replaceOpWithNewOp<arith::CmpIOp>(op, Pred, adaptor.getA(),
|
|
|
|
adaptor.getB());
|
2021-06-17 06:53:15 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
2021-04-30 06:38:11 +08:00
|
|
|
|
2022-04-25 21:12:45 +08:00
|
|
|
namespace {
|
|
|
|
// Lowers aten float and float_int comparison ops.
|
|
|
|
template <typename AtenOp, arith::CmpFPredicate Pred>
|
|
|
|
class ConvertAtenFloatComparisonOp : public OpConversionPattern<AtenOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOp>::OpConversionPattern;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(AtenOp op,
|
|
|
|
typename OpConversionPattern<AtenOp>::OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2022-12-08 04:20:41 +08:00
|
|
|
Value lhs = adaptor.getA(), rhs = adaptor.getB();
|
2022-04-25 21:12:45 +08:00
|
|
|
rhs = convertScalarToDtype(rewriter, op.getLoc(), rhs, lhs.getType());
|
|
|
|
rewriter.replaceOpWithNewOp<arith::CmpFOp>(op, Pred, lhs, rhs);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2021-12-03 12:13:00 +08:00
|
|
|
// Tensors with integer types need to be converted to signless integer
|
|
|
|
// element type. All tensors with element types other than integer can reuse
|
|
|
|
// existing elements attribute.
|
|
|
|
namespace {
|
|
|
|
class ConvertTorchTensorLiteralOp
|
|
|
|
: public OpConversionPattern<ValueTensorLiteralOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<ValueTensorLiteralOp>::OpConversionPattern;
|
|
|
|
using OpAdaptor = ValueTensorLiteralOp::Adaptor;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(ValueTensorLiteralOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
MLIRContext *context = op->getContext();
|
2024-04-28 05:00:56 +08:00
|
|
|
if (auto elements = dyn_cast<DenseIntElementsAttr>(op.getValueAttr())) {
|
|
|
|
if (auto type = dyn_cast<RankedTensorType>(elements.getType())) {
|
2023-11-04 02:44:07 +08:00
|
|
|
Type elemTy = op.getValueAttr().getElementType();
|
|
|
|
unsigned bitWidth = elemTy.getIntOrFloatBitWidth();
|
|
|
|
Type builtinTensorElemTy = IntegerType::get(context, bitWidth);
|
|
|
|
auto shapedType =
|
2024-01-30 01:59:33 +08:00
|
|
|
RankedTensorType::get(type.getShape(), builtinTensorElemTy);
|
2023-11-04 02:44:07 +08:00
|
|
|
auto rawData = elements.getRawData();
|
2024-01-30 01:59:33 +08:00
|
|
|
DenseElementsAttr newAttr =
|
|
|
|
DenseElementsAttr::getFromRawBuffer(shapedType, rawData);
|
2023-11-04 02:44:07 +08:00
|
|
|
rewriter.replaceOpWithNewOp<arith::ConstantOp>(op, newAttr);
|
|
|
|
return success();
|
|
|
|
}
|
2021-12-03 12:13:00 +08:00
|
|
|
}
|
2024-01-30 01:59:33 +08:00
|
|
|
if (auto elements =
|
2024-04-28 05:00:56 +08:00
|
|
|
dyn_cast<DenseResourceElementsAttr>(op.getValueAttr())) {
|
|
|
|
if (auto type = dyn_cast<RankedTensorType>(elements.getType())) {
|
|
|
|
if (auto intType = dyn_cast<IntegerType>(type.getElementType())) {
|
2021-12-03 12:13:00 +08:00
|
|
|
Type builtinTensorElemTy =
|
|
|
|
IntegerType::get(context, intType.getIntOrFloatBitWidth());
|
|
|
|
auto shapedType =
|
|
|
|
RankedTensorType::get(type.getShape(), builtinTensorElemTy);
|
|
|
|
rewriter.replaceOpWithNewOp<arith::ConstantOp>(
|
2023-11-04 02:44:07 +08:00
|
|
|
op, DenseResourceElementsAttr::get(shapedType,
|
|
|
|
elements.getRawHandle()));
|
2021-12-03 12:13:00 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-08 04:20:41 +08:00
|
|
|
rewriter.replaceOpWithNewOp<arith::ConstantOp>(op, op.getValueAttr());
|
2021-12-03 12:13:00 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2021-06-17 23:52:13 +08:00
|
|
|
namespace {
|
2021-06-19 01:44:43 +08:00
|
|
|
template <typename OpTy>
|
|
|
|
class ConvertTorchConstantOp : public OpConversionPattern<OpTy> {
|
2021-06-17 23:52:13 +08:00
|
|
|
public:
|
2021-06-19 01:44:43 +08:00
|
|
|
using OpConversionPattern<OpTy>::OpConversionPattern;
|
2021-11-16 07:00:53 +08:00
|
|
|
using OpAdaptor = typename OpTy::Adaptor;
|
2021-06-17 23:52:13 +08:00
|
|
|
LogicalResult
|
2021-11-16 07:00:53 +08:00
|
|
|
matchAndRewrite(OpTy op, OpAdaptor adaptor,
|
2021-06-17 23:52:13 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2022-12-08 04:20:41 +08:00
|
|
|
rewriter.replaceOpWithNewOp<arith::ConstantOp>(op, op.getValueAttr());
|
2021-06-17 23:52:13 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
2023-02-11 05:59:03 +08:00
|
|
|
|
|
|
|
class ConvertTorchConstantIntOp
|
|
|
|
: public OpConversionPattern<Torch::ConstantIntOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<Torch::ConstantIntOp>::OpConversionPattern;
|
|
|
|
using OpAdaptor = Torch::ConstantIntOp::Adaptor;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(Torch::ConstantIntOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2023-05-23 08:21:34 +08:00
|
|
|
// note: arith.constant only accept signless integer, so convert signed to
|
|
|
|
// signless
|
2023-02-11 05:59:03 +08:00
|
|
|
rewriter.replaceOpWithNewOp<arith::ConstantOp>(
|
|
|
|
op, rewriter.getIntegerAttr(rewriter.getI64Type(),
|
|
|
|
op.getValueAttr().getValue()));
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
2021-06-17 23:52:13 +08:00
|
|
|
} // namespace
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
|
2024-03-27 03:41:40 +08:00
|
|
|
namespace {
|
2024-04-02 01:05:08 +08:00
|
|
|
template <typename AtenOp>
|
|
|
|
class ConvertAtenCastOp : public OpConversionPattern<AtenOp> {
|
2024-03-27 03:41:40 +08:00
|
|
|
public:
|
2024-04-02 01:05:08 +08:00
|
|
|
using OpConversionPattern<AtenOp>::OpConversionPattern;
|
2023-02-28 10:32:21 +08:00
|
|
|
LogicalResult
|
2024-04-02 01:05:08 +08:00
|
|
|
matchAndRewrite(AtenOp op,
|
|
|
|
typename OpConversionPattern<AtenOp>::OpAdaptor adaptor,
|
2023-02-28 10:32:21 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
Type resultType =
|
|
|
|
this->getTypeConverter()->convertType(op->getResult(0).getType());
|
|
|
|
Value result =
|
|
|
|
convertScalarToDtype(rewriter, op.getLoc(), adaptor.getA(), resultType);
|
|
|
|
rewriter.replaceOp(op, result);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2024-08-08 00:55:27 +08:00
|
|
|
namespace {
|
|
|
|
template <typename AtenOp>
|
|
|
|
class ConvertAtenScalarArithOp : public OpConversionPattern<AtenOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<AtenOp>::OpConversionPattern;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(AtenOp op,
|
|
|
|
typename OpConversionPattern<AtenOp>::OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
Type resultType =
|
|
|
|
this->getTypeConverter()->convertType(op->getResult(0).getType());
|
|
|
|
Value result =
|
|
|
|
convertScalarToDtype(rewriter, op.getLoc(), adaptor.getA(), resultType);
|
|
|
|
rewriter.replaceOp(op, result);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2023-02-28 10:32:21 +08:00
|
|
|
namespace {
|
|
|
|
class ConvertAtenAddOp : public OpConversionPattern<AtenAddOp> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern::OpConversionPattern;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(AtenAddOp op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
Location loc = op.getLoc();
|
|
|
|
Type resultType =
|
|
|
|
this->getTypeConverter()->convertType(op->getResult(0).getType());
|
|
|
|
Value operandA =
|
|
|
|
convertScalarToDtype(rewriter, loc, adaptor.getA(), resultType);
|
|
|
|
Value operandB =
|
|
|
|
convertScalarToDtype(rewriter, loc, adaptor.getB(), resultType);
|
2024-04-11 21:47:35 +08:00
|
|
|
if (isa<mlir::FloatType>(resultType)) {
|
2023-02-28 10:32:21 +08:00
|
|
|
rewriter.replaceOpWithNewOp<arith::AddFOp>(op, operandA, operandB);
|
2024-04-11 21:47:35 +08:00
|
|
|
} else if (isa<mlir::IntegerType>(resultType)) {
|
2023-02-28 10:32:21 +08:00
|
|
|
rewriter.replaceOpWithNewOp<arith::AddIOp>(op, operandA, operandB);
|
|
|
|
} else {
|
|
|
|
return rewriter.notifyMatchFailure(
|
|
|
|
op, "unimplemented: only support integer or float result type");
|
|
|
|
}
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2022-05-23 23:26:02 +08:00
|
|
|
namespace {
|
2022-12-08 13:46:54 +08:00
|
|
|
template <typename OpTy, typename BinOp>
|
2022-06-02 09:20:25 +08:00
|
|
|
class ConvertAtenAnyOrAllBoolOp : public OpConversionPattern<OpTy> {
|
2022-05-23 23:26:02 +08:00
|
|
|
public:
|
2022-06-02 09:20:25 +08:00
|
|
|
using OpConversionPattern<OpTy>::OpConversionPattern;
|
|
|
|
using OpAdaptor = typename OpTy::Adaptor;
|
|
|
|
virtual bool reductionFunction(ArrayRef<bool> inputArray) const = 0;
|
2022-05-23 23:26:02 +08:00
|
|
|
LogicalResult
|
2022-06-02 09:20:25 +08:00
|
|
|
matchAndRewrite(OpTy op, OpAdaptor adaptor,
|
2022-05-23 23:26:02 +08:00
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
2022-12-08 13:46:54 +08:00
|
|
|
Location loc = op.getLoc();
|
|
|
|
Value result;
|
2022-05-23 23:26:02 +08:00
|
|
|
SmallVector<Value> inputListTorchBool;
|
2022-12-08 04:20:41 +08:00
|
|
|
if (!getListConstructElements(op.getSelf(), inputListTorchBool)) {
|
2022-05-23 23:26:02 +08:00
|
|
|
return rewriter.notifyMatchFailure(
|
2022-12-08 13:46:54 +08:00
|
|
|
op, "unimplemented: input list not constructed from ListConstruct");
|
2022-05-23 23:26:02 +08:00
|
|
|
}
|
2022-12-08 13:46:54 +08:00
|
|
|
SmallVector<Value> inputList = getTypeConvertedValues(
|
|
|
|
rewriter, loc, this->getTypeConverter(), inputListTorchBool);
|
|
|
|
result = inputList[0];
|
|
|
|
for (unsigned i = 1; i < inputList.size(); i++)
|
|
|
|
result = rewriter.create<BinOp>(loc, result, inputList[i]);
|
|
|
|
rewriter.replaceOp(op, result);
|
2022-05-23 23:26:02 +08:00
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
2022-06-02 09:20:25 +08:00
|
|
|
|
2022-12-08 13:46:54 +08:00
|
|
|
class ConvertAtenAnyOp
|
|
|
|
: public ConvertAtenAnyOrAllBoolOp<AtenAnyBoolOp, arith::OrIOp> {
|
|
|
|
using ConvertAtenAnyOrAllBoolOp<AtenAnyBoolOp,
|
|
|
|
arith::OrIOp>::ConvertAtenAnyOrAllBoolOp;
|
2022-06-02 09:20:25 +08:00
|
|
|
bool reductionFunction(ArrayRef<bool> inputArray) const override {
|
|
|
|
return llvm::any_of(inputArray,
|
|
|
|
[](bool inputListElem) { return inputListElem; });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-08 13:46:54 +08:00
|
|
|
class ConvertAtenAllOp
|
|
|
|
: public ConvertAtenAnyOrAllBoolOp<AtenAllBoolOp, arith::AndIOp> {
|
|
|
|
using ConvertAtenAnyOrAllBoolOp<AtenAllBoolOp,
|
|
|
|
arith::AndIOp>::ConvertAtenAnyOrAllBoolOp;
|
2022-06-02 09:20:25 +08:00
|
|
|
bool reductionFunction(ArrayRef<bool> inputArray) const override {
|
|
|
|
return llvm::all_of(inputArray,
|
|
|
|
[](bool inputListElem) { return inputListElem; });
|
|
|
|
}
|
|
|
|
};
|
2022-05-23 23:26:02 +08:00
|
|
|
} // namespace
|
|
|
|
|
2022-05-20 16:26:52 +08:00
|
|
|
namespace {
|
|
|
|
template <typename OpTy, typename CmpOpTy, typename CmpOpPred, CmpOpPred Pred>
|
|
|
|
class ConvertAtenBoolLikeOp : public OpConversionPattern<OpTy> {
|
|
|
|
public:
|
|
|
|
using OpConversionPattern<OpTy>::OpConversionPattern;
|
|
|
|
using OpAdaptor = typename OpTy::Adaptor;
|
|
|
|
LogicalResult
|
|
|
|
matchAndRewrite(OpTy op, OpAdaptor adaptor,
|
|
|
|
ConversionPatternRewriter &rewriter) const override {
|
|
|
|
Location loc = op.getLoc();
|
2022-12-08 04:20:41 +08:00
|
|
|
Type inputType = adaptor.getA().getType();
|
2022-05-20 16:26:52 +08:00
|
|
|
Value cstZero = rewriter.create<arith::ConstantOp>(
|
|
|
|
loc, rewriter.getZeroAttr(inputType));
|
|
|
|
Value cstTrue =
|
|
|
|
rewriter.create<arith::ConstantOp>(loc, rewriter.getBoolAttr(true));
|
|
|
|
Value cstFalse =
|
|
|
|
rewriter.create<arith::ConstantOp>(loc, rewriter.getBoolAttr(false));
|
|
|
|
|
|
|
|
Value cmpPred;
|
2022-12-08 04:20:41 +08:00
|
|
|
cmpPred = rewriter.create<CmpOpTy>(loc, Pred, adaptor.getA(), cstZero);
|
2022-05-20 16:26:52 +08:00
|
|
|
rewriter.replaceOpWithNewOp<arith::SelectOp>(op, cmpPred, cstTrue,
|
|
|
|
cstFalse);
|
|
|
|
return success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2021-04-28 06:15:50 +08:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// The pass
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
2024-01-30 01:59:33 +08:00
|
|
|
class ConvertTorchToArith
|
|
|
|
: public ConvertTorchToArithBase<ConvertTorchToArith> {
|
2021-04-28 06:15:50 +08:00
|
|
|
public:
|
|
|
|
void getDependentDialects(DialectRegistry ®istry) const override {
|
2022-03-16 18:44:23 +08:00
|
|
|
registry.insert<func::FuncDialect>();
|
2022-10-05 21:28:06 +08:00
|
|
|
registry.insert<arith::ArithDialect>();
|
2022-01-05 00:00:48 +08:00
|
|
|
registry.insert<tensor::TensorDialect>();
|
2022-02-13 02:47:12 +08:00
|
|
|
registry.insert<cf::ControlFlowDialect>();
|
2022-04-25 21:12:45 +08:00
|
|
|
registry.insert<math::MathDialect>();
|
Add TorchToIREE and factor out TorchConversion dialect.
This converts a basic list op (torch.prim.ListConstruct) to the IREE
dialect.
```
def forward(self, x: float):
return [x, x]
```
turns into:
```
builtin.func @forward(%arg0: !torch.float) -> !torch.list<!torch.float> {
%0 = torch.prim.ListConstruct %arg0, %arg0 : (!torch.float, !torch.float) -> !torch.list<!torch.float>
return %0 : !torch.list<!torch.float>
}
```
which turns into:
```
builtin.func @forward(%arg0: f64) -> !iree.list<f64> {
%c1 = constant 1 : index
%c0 = constant 0 : index
%c2 = constant 2 : index
%0 = iree.list.create %c2 : !iree.list<f64>
iree.list.set %0[%c0], %arg0 : !iree.list<f64>, f64
iree.list.set %0[%c1], %arg0 : !iree.list<f64>, f64
return %0 : !iree.list<f64>
}
```
As part of doing this, I realized that it was time to formalize the IR
form that we reach right before running TorchTo{Linalg,Std,...}. We now
call it the "Torch backend contract". We then lower the "Torch backend
contract" to the "npcomp backend contract", which involves the new
TorchConversion (`torch_c`) dialect, which holds ops that need to
operate on both the npcomp backend types (e.g. builtin tensors, i1, IREE
list, etc.) and the `!torch` types.
This made more sense, as I realized that if I didn't factor out
`torch_c` then the Torch dialect would have a dependency on IREE
dialect (we previously didn't notice this was an issue because we only
depended on `builtin` types), which seemed wrong to me.
Recommended review order:
- TorchToIREE.cpp / `TorchToIREE/basic.mlir`
- Look at the new structure of createTorchScriptToNpcompBackendPipeline.
It now lives in TorchConversion/Transforms/Passes.cpp and cleanly
calls into `Torch::createTorchScriptToTorchBackendPipeline` for the
frontend lowering to the Torch backend contract.
- Mechanical change extracting
`torch_c.{to,from}_{i1,i64,f64,builtin_tensor,iree_list}` into a new
TorchConversion dialect, and a few passes specific to the lowering
from the Torch backend contract to the npcomp backend contract.
- Minor fixes to TorchToLinalg.cpp to use unconverted operands (now that
we convert lists as part of operand materialization, we need to use
the original operands). Also added test for AtenMaxPool2dOp and fixed
m_TorchConstantIntList.
- TmpDeleteDeadIREELists pass. Temporary pass for deleting dead IREE lists that
are created as part of operand materialization for conv/max pool/avg pool ops
in TorchToLinalg.
2021-08-12 05:40:08 +08:00
|
|
|
TorchConversion::getBackendTypeConversionDependentDialects(registry);
|
2021-04-28 06:15:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void runOnOperation() override {
|
|
|
|
MLIRContext *context = &getContext();
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
ConversionTarget target(*context);
|
2022-03-16 18:44:23 +08:00
|
|
|
target.addLegalDialect<Torch::TorchDialect, func::FuncDialect,
|
2022-10-05 21:28:06 +08:00
|
|
|
arith::ArithDialect, tensor::TensorDialect,
|
2022-04-25 21:12:45 +08:00
|
|
|
cf::ControlFlowDialect, math::MathDialect>();
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
|
|
|
|
TypeConverter typeConverter;
|
|
|
|
typeConverter.addConversion([](Type type) { return type; });
|
Add TorchToIREE and factor out TorchConversion dialect.
This converts a basic list op (torch.prim.ListConstruct) to the IREE
dialect.
```
def forward(self, x: float):
return [x, x]
```
turns into:
```
builtin.func @forward(%arg0: !torch.float) -> !torch.list<!torch.float> {
%0 = torch.prim.ListConstruct %arg0, %arg0 : (!torch.float, !torch.float) -> !torch.list<!torch.float>
return %0 : !torch.list<!torch.float>
}
```
which turns into:
```
builtin.func @forward(%arg0: f64) -> !iree.list<f64> {
%c1 = constant 1 : index
%c0 = constant 0 : index
%c2 = constant 2 : index
%0 = iree.list.create %c2 : !iree.list<f64>
iree.list.set %0[%c0], %arg0 : !iree.list<f64>, f64
iree.list.set %0[%c1], %arg0 : !iree.list<f64>, f64
return %0 : !iree.list<f64>
}
```
As part of doing this, I realized that it was time to formalize the IR
form that we reach right before running TorchTo{Linalg,Std,...}. We now
call it the "Torch backend contract". We then lower the "Torch backend
contract" to the "npcomp backend contract", which involves the new
TorchConversion (`torch_c`) dialect, which holds ops that need to
operate on both the npcomp backend types (e.g. builtin tensors, i1, IREE
list, etc.) and the `!torch` types.
This made more sense, as I realized that if I didn't factor out
`torch_c` then the Torch dialect would have a dependency on IREE
dialect (we previously didn't notice this was an issue because we only
depended on `builtin` types), which seemed wrong to me.
Recommended review order:
- TorchToIREE.cpp / `TorchToIREE/basic.mlir`
- Look at the new structure of createTorchScriptToNpcompBackendPipeline.
It now lives in TorchConversion/Transforms/Passes.cpp and cleanly
calls into `Torch::createTorchScriptToTorchBackendPipeline` for the
frontend lowering to the Torch backend contract.
- Mechanical change extracting
`torch_c.{to,from}_{i1,i64,f64,builtin_tensor,iree_list}` into a new
TorchConversion dialect, and a few passes specific to the lowering
from the Torch backend contract to the npcomp backend contract.
- Minor fixes to TorchToLinalg.cpp to use unconverted operands (now that
we convert lists as part of operand materialization, we need to use
the original operands). Also added test for AtenMaxPool2dOp and fixed
m_TorchConstantIntList.
- TmpDeleteDeadIREELists pass. Temporary pass for deleting dead IREE lists that
are created as part of operand materialization for conv/max pool/avg pool ops
in TorchToLinalg.
2021-08-12 05:40:08 +08:00
|
|
|
TorchConversion::setupBackendTypeConversion(target, typeConverter);
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
|
2021-04-28 06:15:50 +08:00
|
|
|
RewritePatternSet patterns(context);
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
target.addIllegalOp<AtenDimOp>();
|
|
|
|
patterns.add<ConvertAtenDimOp>(typeConverter, context);
|
2022-02-08 22:18:08 +08:00
|
|
|
target.addIllegalOp<RuntimeAssertOp>();
|
|
|
|
patterns.add<ConvertRuntimeAssertOp>(typeConverter, context);
|
2024-03-28 01:06:43 +08:00
|
|
|
target.addIllegalOp<AtenNeIntOp, AtenEqIntOp, AtenGtIntOp, AtenGeIntOp,
|
|
|
|
AtenLtIntOp, AtenLeIntOp>();
|
2022-02-12 05:39:30 +08:00
|
|
|
patterns
|
|
|
|
.add<ConvertAtenIntComparisonOp<AtenNeIntOp, arith::CmpIPredicate::ne>>(
|
|
|
|
typeConverter, context);
|
|
|
|
patterns
|
|
|
|
.add<ConvertAtenIntComparisonOp<AtenEqIntOp, arith::CmpIPredicate::eq>>(
|
|
|
|
typeConverter, context);
|
|
|
|
patterns.add<
|
|
|
|
ConvertAtenIntComparisonOp<AtenGtIntOp, arith::CmpIPredicate::sgt>>(
|
|
|
|
typeConverter, context);
|
2024-03-28 01:06:43 +08:00
|
|
|
patterns.add<
|
|
|
|
ConvertAtenIntComparisonOp<AtenLtIntOp, arith::CmpIPredicate::slt>>(
|
|
|
|
typeConverter, context);
|
2022-07-22 20:51:26 +08:00
|
|
|
patterns.add<
|
|
|
|
ConvertAtenIntComparisonOp<AtenGeIntOp, arith::CmpIPredicate::sge>>(
|
|
|
|
typeConverter, context);
|
2024-03-28 01:06:43 +08:00
|
|
|
patterns.add<
|
|
|
|
ConvertAtenIntComparisonOp<AtenLeIntOp, arith::CmpIPredicate::sle>>(
|
|
|
|
typeConverter, context);
|
2022-04-25 21:12:45 +08:00
|
|
|
target.addIllegalOp<AtenGeFloatOp, AtenGeFloatIntOp, AtenNeFloatIntOp,
|
|
|
|
AtenGtFloatIntOp>();
|
|
|
|
patterns.add<
|
|
|
|
ConvertAtenFloatComparisonOp<AtenGeFloatOp, arith::CmpFPredicate::UGE>>(
|
|
|
|
typeConverter, context);
|
|
|
|
patterns.add<ConvertAtenFloatComparisonOp<AtenGeFloatIntOp,
|
|
|
|
arith::CmpFPredicate::UGE>>(
|
|
|
|
typeConverter, context);
|
|
|
|
patterns.add<ConvertAtenFloatComparisonOp<AtenNeFloatIntOp,
|
|
|
|
arith::CmpFPredicate::UNE>>(
|
|
|
|
typeConverter, context);
|
|
|
|
patterns.add<ConvertAtenFloatComparisonOp<AtenGtFloatIntOp,
|
|
|
|
arith::CmpFPredicate::UGT>>(
|
|
|
|
typeConverter, context);
|
2021-06-17 23:52:13 +08:00
|
|
|
target.addIllegalOp<ValueTensorLiteralOp>();
|
2021-12-03 12:13:00 +08:00
|
|
|
patterns.add<ConvertTorchTensorLiteralOp>(typeConverter, context);
|
|
|
|
|
2021-06-19 01:44:43 +08:00
|
|
|
target.addIllegalOp<ConstantBoolOp>();
|
|
|
|
patterns.add<ConvertTorchConstantOp<ConstantBoolOp>>(typeConverter,
|
|
|
|
context);
|
|
|
|
target.addIllegalOp<Torch::ConstantFloatOp>();
|
|
|
|
patterns.add<ConvertTorchConstantOp<Torch::ConstantFloatOp>>(typeConverter,
|
|
|
|
context);
|
|
|
|
target.addIllegalOp<Torch::ConstantIntOp>();
|
2023-02-11 05:59:03 +08:00
|
|
|
patterns.add<ConvertTorchConstantIntOp>(typeConverter, context);
|
|
|
|
|
2024-05-23 01:19:08 +08:00
|
|
|
target.addIllegalOp<AtenIntBoolOp, AtenFloatScalarOp, AtenIntScalarOp>();
|
2024-04-02 01:05:08 +08:00
|
|
|
patterns.add<ConvertAtenCastOp<AtenIntBoolOp>>(typeConverter, context);
|
|
|
|
patterns.add<ConvertAtenCastOp<AtenFloatScalarOp>>(typeConverter, context);
|
2024-05-23 01:19:08 +08:00
|
|
|
patterns.add<ConvertAtenCastOp<AtenIntScalarOp>>(typeConverter, context);
|
2024-03-27 03:41:40 +08:00
|
|
|
|
2023-02-28 10:32:21 +08:00
|
|
|
target.addIllegalOp<AtenAddOp>();
|
|
|
|
patterns.add<ConvertAtenAddOp>(typeConverter, context);
|
|
|
|
|
2024-08-08 00:55:27 +08:00
|
|
|
target.addIllegalOp<AtenAddIntOp, AtenAddFloatIntOp, AtenSubIntOp,
|
|
|
|
AtenMulIntOp>();
|
2021-12-17 23:24:56 +08:00
|
|
|
patterns.add<ConvertAtenBinaryOp<AtenAddIntOp, arith::AddIOp>>(
|
|
|
|
typeConverter, context);
|
2024-08-08 00:55:27 +08:00
|
|
|
patterns.add<ConvertAtenBinaryOp<AtenAddFloatIntOp, arith::AddFOp>>(
|
|
|
|
typeConverter, context);
|
2021-12-17 23:24:56 +08:00
|
|
|
patterns.add<ConvertAtenBinaryOp<AtenSubIntOp, arith::SubIOp>>(
|
|
|
|
typeConverter, context);
|
|
|
|
patterns.add<ConvertAtenBinaryOp<AtenMulIntOp, arith::MulIOp>>(
|
|
|
|
typeConverter, context);
|
2024-02-06 08:23:04 +08:00
|
|
|
target.addIllegalOp<AtenSubFloatOp, AtenMulFloatOp>();
|
2022-02-17 00:34:03 +08:00
|
|
|
patterns.add<ConvertAtenBinaryOp<AtenSubFloatOp, arith::SubFOp>>(
|
|
|
|
typeConverter, context);
|
2024-02-06 08:23:04 +08:00
|
|
|
patterns.add<ConvertAtenBinaryOp<AtenMulFloatOp, arith::MulFOp>>(
|
|
|
|
typeConverter, context);
|
2022-10-06 21:11:52 +08:00
|
|
|
target.addIllegalOp<AtenDivIntOp>();
|
|
|
|
patterns.add<ConvertAtenDivIntOp>(typeConverter, context);
|
2022-04-25 20:06:41 +08:00
|
|
|
target.addIllegalOp<AtenDivFloatOp>();
|
|
|
|
patterns.add<ConvertAtenBinaryOp<AtenDivFloatOp, arith::DivFOp>>(
|
|
|
|
typeConverter, context);
|
2022-11-04 15:57:29 +08:00
|
|
|
target.addIllegalOp<AtenFloordivIntOp>();
|
|
|
|
patterns.add<ConvertAtenBinaryOp<AtenFloordivIntOp, arith::FloorDivSIOp>>(
|
|
|
|
typeConverter, context);
|
2022-12-08 13:46:54 +08:00
|
|
|
target.addIllegalOp<PrimMaxIntOp>();
|
|
|
|
patterns.add<ConvertAtenBinaryOp<PrimMaxIntOp, arith::MaxSIOp>>(
|
|
|
|
typeConverter, context);
|
2023-05-26 06:40:12 +08:00
|
|
|
target.addIllegalOp<PrimMinIntOp>();
|
|
|
|
patterns.add<ConvertAtenBinaryOp<PrimMinIntOp, arith::MinSIOp>>(
|
|
|
|
typeConverter, context);
|
2022-04-25 21:12:45 +08:00
|
|
|
target.addIllegalOp<AtenCeilFloatOp>();
|
2022-05-19 22:54:16 +08:00
|
|
|
patterns
|
|
|
|
.add<ConvertAtenUnaryOpToFloatMathOp<AtenCeilFloatOp, math::CeilOp>>(
|
|
|
|
typeConverter, context);
|
|
|
|
target.addIllegalOp<AtenSqrtIntOp>();
|
|
|
|
patterns.add<ConvertAtenUnaryOpToFloatMathOp<AtenSqrtIntOp, math::SqrtOp>>(
|
2022-04-25 21:12:45 +08:00
|
|
|
typeConverter, context);
|
2022-06-02 09:20:25 +08:00
|
|
|
target.addIllegalOp<AtenAnyBoolOp, AtenAllBoolOp>();
|
|
|
|
patterns.add<ConvertAtenAnyOp>(typeConverter, context);
|
|
|
|
patterns.add<ConvertAtenAllOp>(typeConverter, context);
|
2022-05-20 16:26:52 +08:00
|
|
|
target.addIllegalOp<AtenBoolFloatOp, AtenBoolIntOp>();
|
|
|
|
patterns.add<
|
|
|
|
ConvertAtenBoolLikeOp<AtenBoolFloatOp, arith::CmpFOp,
|
|
|
|
arith::CmpFPredicate, arith::CmpFPredicate::UNE>>(
|
|
|
|
typeConverter, context);
|
|
|
|
patterns.add<
|
|
|
|
ConvertAtenBoolLikeOp<AtenBoolIntOp, arith::CmpIOp,
|
|
|
|
arith::CmpIPredicate, arith::CmpIPredicate::ne>>(
|
|
|
|
typeConverter, context);
|
2021-12-17 23:24:56 +08:00
|
|
|
|
Introduce `!torch.tensor` / `!torch.vtensor` types.
This removes our reliance on the numpy dialect and avoids our off-label
use of the builtin tnesor type for modeling unknown dtypes. The
`!torch.vtensor` (`ValueTensorType`) type is a value-semantic tensor.
The `!torch.tensor` (`NonValueTensorType`) type is a non-value-semantic
tensor. The new types look as follows syntactically:
```
// Least-static-information, non-value-semantic tensor.
!torch.tensor
// Explicit form of least-static-information variant.
!torch.tensor<*,unk>
// Least-static-information, value-semantic tensor.
!torch.vtensor
// Explicit form of least-static-information variant.
!torch.vtensor<*,unk>
// Fixed-set of allowable element types, with first-class support for
// Torch's frontend signedness semantics.
!torch.tensor<*,si32>
// First-class support for unknown dtypes.
!torch.tensor<[?,?,?],unk>
// Standard MLIR representation of `?` for unknown dimensions.
!torch.tensor<[?,2,?,4],unk>
// Statically shaped / dtyped example.
!torch.vtensor<[1,2,3,4],f32>
```
This required fairly significant changes throughout the compiler, but
overall it is a big cleanup. We now have a much clearer layering of "the
Torch frontend lowering" vs "lowering to std + linalg + etc.".
At the C++ level, there is `ValueTensorType`, `NonValueTensorType`.
We also have a helper `BaseTensorType` (kind of like ShapedType) which
interoperates with those two.
Included changes:
- New `torch.tensor(dense<0.0> : tensor<5xf32>) : !torch.tensor` op for
creating torch tensor literals in the frontend.
- Consistently use signedness for the types (except i1 which I didn't
touch -- we need to sort out the situation with !basicpy.BoolType
there anyway so will be attending to that soon)
- Frontend can annotate whether an argument to the function has value
semantics. We currently require this, as our backend contract does not
currently allow us to even model the non-value-semantic case. Before,
the value-semantic assumption was randomly injected in the middle of
the pass pipeline.
- Move ArrayToTensor (now called MaximizeValueSemantics) and
RefinePublicReturn passes to torch dialect.
- The TorchToStd and TorchToLinalg passes are now type conversions from
`!torch.vtensor` to `tensor` and use the dialect conversion infra.
The overall conversion pipeline is set up following the best practices
of the "Type Conversions the Not-So-Hard Way" talk. This required
introducing `torch-func-builtin-tensorize` and
`torch-finalizing-builtin-tensorize` passes analogous to the upstream
bufferization passes with the corresponding names (mostly just
copypasta from there).
- Misc Torch-level canonicalizations -- we now cleanly layer the
lowering to std later in the pipeline, so we are gradually lessening
our reliance on random std constant folding before we get to that
point.
Recommended review order:
- New types in TorchTypes.td/TorchTypes.h/TorchDialect.cpp
- New ops in TorchOps.td / TorchOps.cpp
- Less important / more mechanical stuff
- Frontend changes.
- Pass changes/additions in `Torch/Transforms` and `Conversion/`
2021-05-21 08:07:18 +08:00
|
|
|
if (failed(applyPartialConversion(getOperation(), target,
|
|
|
|
std::move(patterns))))
|
|
|
|
return signalPassFailure();
|
2021-04-28 06:15:50 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2022-04-27 03:27:51 +08:00
|
|
|
std::unique_ptr<OperationPass<func::FuncOp>>
|
2022-08-11 03:12:51 +08:00
|
|
|
mlir::torch::createConvertTorchToArithPass() {
|
|
|
|
return std::make_unique<ConvertTorchToArith>();
|
2021-04-28 06:15:50 +08:00
|
|
|
}
|