2021-10-16 06:23:59 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is licensed 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
|
|
|
|
// Also available under a BSD-style license. See LICENSE.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "torch-mlir/Dialect/Torch/Utils/Utils.h"
|
Add type promotion code to refine types.
The types have different levels of categories: where
complex > floating > integral > boolean (> means left hand
side has higher category).
The operands have different levels of priorities where:
dimensioned tensor > 0-dim tensor > scalar == wrapped 0-dim tensor.
This is represented by the `ResultTypeState.dimResult`,
`ResultTypeState.zeroResult` and `ResultTypeState..wrappedResult` in
the source code.
For operands of the same priorities, the result type should be the
highest categories with sufficient width to hold all operands.
By default, only the highest priority operands participate in the type
promotion logic. Lower priority operands participate if they are in
a higher category than any higher priority operands.
For example, <[],f32> (lower priority) and <[1], si64> tensor would
result in <[?],f32> tensor because floating > integeral. Another example
<[],f64> (lower priority) and <[1], f32> tensor would result in
<[?], f32> tensor because f32 and f64 are the same category.
The ScalarType enum definition, type promotion table, ResultTypeState
struct definition and some helpers are copied from
aten/src/ATen/native/TypeProperties.*
Other references:
- https://pytorch.org/docs/stable/tensor_attributes.html#type-promotion-doc
- https://github.com/pytorch/pytorch/issues/9515
Other minor changes:
1. Fix `visitExpandLikeOp` to consider cases where the given sizes list
size is larger than the input rank.
2. Add back the somehow deleted `torch.aten.softmax.int` tests in
decompose-complex-ops.mlir.
2021-10-21 03:31:28 +08:00
|
|
|
#include "torch-mlir/Dialect/Torch/IR/TorchOps.h"
|
2021-10-16 06:23:59 +08:00
|
|
|
|
2022-03-16 08:08:45 +08:00
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::torch;
|
|
|
|
using namespace mlir::torch::Torch;
|
2022-02-04 19:43:25 +08:00
|
|
|
|
2022-03-16 08:08:45 +08:00
|
|
|
int64_t Torch::toPositiveDim(int64_t dim, int64_t inputRank) {
|
2021-10-16 06:23:59 +08:00
|
|
|
return dim >= 0 ? dim : dim + inputRank;
|
|
|
|
}
|
|
|
|
|
2022-03-16 08:08:45 +08:00
|
|
|
bool Torch::isValidDim(int64_t dim, int64_t inputRank) {
|
2021-10-16 06:23:59 +08:00
|
|
|
return dim >= 0 && dim < inputRank;
|
|
|
|
}
|
|
|
|
|
2022-03-16 08:08:45 +08:00
|
|
|
bool Torch::getListConstructElements(Value v, SmallVectorImpl<Value> &elems) {
|
Add type promotion code to refine types.
The types have different levels of categories: where
complex > floating > integral > boolean (> means left hand
side has higher category).
The operands have different levels of priorities where:
dimensioned tensor > 0-dim tensor > scalar == wrapped 0-dim tensor.
This is represented by the `ResultTypeState.dimResult`,
`ResultTypeState.zeroResult` and `ResultTypeState..wrappedResult` in
the source code.
For operands of the same priorities, the result type should be the
highest categories with sufficient width to hold all operands.
By default, only the highest priority operands participate in the type
promotion logic. Lower priority operands participate if they are in
a higher category than any higher priority operands.
For example, <[],f32> (lower priority) and <[1], si64> tensor would
result in <[?],f32> tensor because floating > integeral. Another example
<[],f64> (lower priority) and <[1], f32> tensor would result in
<[?], f32> tensor because f32 and f64 are the same category.
The ScalarType enum definition, type promotion table, ResultTypeState
struct definition and some helpers are copied from
aten/src/ATen/native/TypeProperties.*
Other references:
- https://pytorch.org/docs/stable/tensor_attributes.html#type-promotion-doc
- https://github.com/pytorch/pytorch/issues/9515
Other minor changes:
1. Fix `visitExpandLikeOp` to consider cases where the given sizes list
size is larger than the input rank.
2. Add back the somehow deleted `torch.aten.softmax.int` tests in
decompose-complex-ops.mlir.
2021-10-21 03:31:28 +08:00
|
|
|
auto listConstruct = v.getDefiningOp<PrimListConstructOp>();
|
|
|
|
if (!listConstruct)
|
|
|
|
return false;
|
|
|
|
elems = llvm::to_vector<4>(listConstruct.elements());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-16 08:08:45 +08:00
|
|
|
torch_upstream::ScalarType Torch::getScalarTypeForType(Type type) {
|
2022-02-04 19:43:25 +08:00
|
|
|
if (type.isa<Float32Type>())
|
2022-03-16 08:08:45 +08:00
|
|
|
return torch_upstream::ScalarType::Float;
|
2022-02-04 19:43:25 +08:00
|
|
|
if (type.isa<Float64Type>())
|
2022-03-16 08:08:45 +08:00
|
|
|
return torch_upstream::ScalarType::Double;
|
2022-02-04 19:43:25 +08:00
|
|
|
if (type.isSignedInteger(64))
|
2022-03-16 08:08:45 +08:00
|
|
|
return torch_upstream::ScalarType::Long;
|
2022-02-04 19:43:25 +08:00
|
|
|
if (type.isSignedInteger(32))
|
2022-03-16 08:08:45 +08:00
|
|
|
return torch_upstream::ScalarType::Int;
|
2022-02-04 19:43:25 +08:00
|
|
|
if (type.isUnsignedInteger(1))
|
2022-03-16 08:08:45 +08:00
|
|
|
return torch_upstream::ScalarType::Bool;
|
2022-02-04 19:43:25 +08:00
|
|
|
llvm::report_fatal_error("unhandled type for getScalarTypeForType");
|
|
|
|
}
|
|
|
|
|
2022-03-16 08:08:45 +08:00
|
|
|
static Value getDtypeIntValueForType(PatternRewriter &rewriter, Location loc,
|
|
|
|
Type dtype) {
|
2022-03-03 00:48:15 +08:00
|
|
|
int intType = (int)getScalarTypeForType(dtype);
|
|
|
|
return rewriter.create<ConstantIntOp>(loc,
|
|
|
|
rewriter.getI64IntegerAttr(intType));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper to convert a tensor to a specific scalar type.
|
2022-03-16 08:08:45 +08:00
|
|
|
Value Torch::convertTensorToDtype(PatternRewriter &rewriter, Location loc,
|
|
|
|
Value input, Type dtype) {
|
2022-03-03 00:48:15 +08:00
|
|
|
BaseTensorType origType = input.getType().cast<BaseTensorType>();
|
|
|
|
Type newType = origType.getWithSizesAndDtype(origType.getSizes(), dtype);
|
|
|
|
// `convertIntVal` contains the corresponding integer for the dtype which is
|
|
|
|
// used by the aten.to.dtype op.
|
|
|
|
Value convertIntVal = getDtypeIntValueForType(rewriter, loc, dtype);
|
|
|
|
Value falseVal = rewriter.create<ConstantBoolOp>(loc, false);
|
|
|
|
Value noneVal = rewriter.create<ConstantNoneOp>(loc);
|
|
|
|
Value converted = rewriter.create<AtenToDtypeOp>(
|
|
|
|
loc, newType, input, convertIntVal, falseVal, falseVal, noneVal);
|
|
|
|
return converted;
|
|
|
|
}
|