2019-11-02 05:09:48 +08:00
|
|
|
//===- onnx_ops.cpp - MLIR ONNX Operations --------------------------------===//
|
|
|
|
//
|
|
|
|
// Copyright 2019 The IBM Research Authors.
|
|
|
|
//
|
|
|
|
// =============================================================================
|
|
|
|
//
|
|
|
|
// This file defines ONNX operations in the MLIR operation set.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/SetVector.h"
|
|
|
|
#include "llvm/ADT/SmallBitVector.h"
|
|
|
|
#include "mlir/IR/Block.h"
|
|
|
|
#include "mlir/IR/Builders.h"
|
|
|
|
#include "mlir/IR/Function.h"
|
|
|
|
#include "mlir/IR/IntegerSet.h"
|
|
|
|
#include "mlir/IR/Matchers.h"
|
|
|
|
#include "mlir/IR/OpImplementation.h"
|
|
|
|
#include "mlir/IR/PatternMatch.h"
|
|
|
|
|
|
|
|
#include "onnx_ops.hpp"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ONNXOpsDialect
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// Dialect creation, the instance will be owned by the context. This is the
|
|
|
|
/// point of registration of custom types and operations for the dialect.
|
|
|
|
ONNXOpsDialect::ONNXOpsDialect(mlir::MLIRContext* ctx)
|
|
|
|
: mlir::Dialect(getDialectNamespace(), ctx) {
|
|
|
|
addOperations<
|
|
|
|
#define GET_OP_LIST
|
|
|
|
#include "src/compiler/onnx.cpp.inc"
|
|
|
|
>();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ONNX Operations
|
2019-12-06 09:08:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Exp
|
|
|
|
/// Infer the output shape of the ONNXExpOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
2019-12-14 04:28:56 +08:00
|
|
|
void ONNXExpOp::inferShapes() { getResult()->setType(getOperand()->getType()); }
|
2019-12-06 09:08:09 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Tanh
|
|
|
|
/// Infer the output shape of the ONNXTanhOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
|
|
|
void ONNXTanhOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand()->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Sinh
|
|
|
|
/// Infer the output shape of the ONNXSinhOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
|
|
|
void ONNXSinhOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand()->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Cosh
|
|
|
|
/// Infer the output shape of the ONNXCoshOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
|
|
|
void ONNXCoshOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand()->getType());
|
|
|
|
}
|
|
|
|
|
[MLIR] Add support for Max, Min, Sum, Elu, Selu, LeakyRelu, HardSigmoid (#395)
* Lower ONNXSumOp
* Add inferShapes() and test cases
* Load the first operand to the result
* Update SharingWork.md
* Update SharingWork.md
* Update SharingWork.md
* Add support for Max, Min
* Pass operation instead of location to mapToLowerScalarOp
* Add support for Elu, Selu, LeakyRelu, HardSigmoid
* Add test cases
* Update SharingWork.md
* Rewrite the part of lowering variadic ops and use it for binary ops
* Use two diffenrent templates for Unary and Variadic Ops
* Revise the code
2019-12-12 10:49:50 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// HardSigmoid
|
|
|
|
/// Infer the output shape of the ONNXHardSigmoidOp. This method is required by
|
|
|
|
/// the shape inference interface.
|
|
|
|
void ONNXHardSigmoidOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand()->getType());
|
|
|
|
}
|
|
|
|
|
2019-12-06 09:08:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Sigmoid
|
|
|
|
/// Infer the output shape of the ONNXSigmoidOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
|
|
|
void ONNXSigmoidOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand()->getType());
|
|
|
|
}
|
|
|
|
|
[MLIR] Add support for Max, Min, Sum, Elu, Selu, LeakyRelu, HardSigmoid (#395)
* Lower ONNXSumOp
* Add inferShapes() and test cases
* Load the first operand to the result
* Update SharingWork.md
* Update SharingWork.md
* Update SharingWork.md
* Add support for Max, Min
* Pass operation instead of location to mapToLowerScalarOp
* Add support for Elu, Selu, LeakyRelu, HardSigmoid
* Add test cases
* Update SharingWork.md
* Rewrite the part of lowering variadic ops and use it for binary ops
* Use two diffenrent templates for Unary and Variadic Ops
* Revise the code
2019-12-12 10:49:50 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Elu
|
|
|
|
/// Infer the output shape of the ONNXEluOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
2019-12-14 04:28:56 +08:00
|
|
|
void ONNXEluOp::inferShapes() { getResult()->setType(getOperand()->getType()); }
|
[MLIR] Add support for Max, Min, Sum, Elu, Selu, LeakyRelu, HardSigmoid (#395)
* Lower ONNXSumOp
* Add inferShapes() and test cases
* Load the first operand to the result
* Update SharingWork.md
* Update SharingWork.md
* Update SharingWork.md
* Add support for Max, Min
* Pass operation instead of location to mapToLowerScalarOp
* Add support for Elu, Selu, LeakyRelu, HardSigmoid
* Add test cases
* Update SharingWork.md
* Rewrite the part of lowering variadic ops and use it for binary ops
* Use two diffenrent templates for Unary and Variadic Ops
* Revise the code
2019-12-12 10:49:50 +08:00
|
|
|
|
2019-12-06 13:31:17 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Relu
|
|
|
|
/// Infer the output shape of the ONNXReluOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
|
|
|
void ONNXReluOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand()->getType());
|
|
|
|
}
|
|
|
|
|
[MLIR] Add support for Max, Min, Sum, Elu, Selu, LeakyRelu, HardSigmoid (#395)
* Lower ONNXSumOp
* Add inferShapes() and test cases
* Load the first operand to the result
* Update SharingWork.md
* Update SharingWork.md
* Update SharingWork.md
* Add support for Max, Min
* Pass operation instead of location to mapToLowerScalarOp
* Add support for Elu, Selu, LeakyRelu, HardSigmoid
* Add test cases
* Update SharingWork.md
* Rewrite the part of lowering variadic ops and use it for binary ops
* Use two diffenrent templates for Unary and Variadic Ops
* Revise the code
2019-12-12 10:49:50 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LeakyRelu
|
|
|
|
/// Infer the output shape of the ONNXLeakyReluOp. This method is required by
|
|
|
|
/// the shape inference interface.
|
|
|
|
void ONNXLeakyReluOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand()->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Selu
|
|
|
|
/// Infer the output shape of the ONNXSeluOp. This method is required by
|
|
|
|
/// the shape inference interface.
|
|
|
|
void ONNXSeluOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand()->getType());
|
2019-12-16 14:23:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Reciprocal
|
|
|
|
/// Infer the output shape of the ONNXReciprocalOp. This method is required by
|
|
|
|
/// the shape inference interface.
|
|
|
|
void ONNXReciprocalOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand()->getType());
|
[MLIR] Add support for Max, Min, Sum, Elu, Selu, LeakyRelu, HardSigmoid (#395)
* Lower ONNXSumOp
* Add inferShapes() and test cases
* Load the first operand to the result
* Update SharingWork.md
* Update SharingWork.md
* Update SharingWork.md
* Add support for Max, Min
* Pass operation instead of location to mapToLowerScalarOp
* Add support for Elu, Selu, LeakyRelu, HardSigmoid
* Add test cases
* Update SharingWork.md
* Rewrite the part of lowering variadic ops and use it for binary ops
* Use two diffenrent templates for Unary and Variadic Ops
* Revise the code
2019-12-12 10:49:50 +08:00
|
|
|
}
|
|
|
|
|
2019-11-02 05:09:48 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-11-13 02:37:46 +08:00
|
|
|
// Add
|
2019-11-19 10:08:21 +08:00
|
|
|
/// Infer the output shape of the ONNXAddOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
2019-11-08 00:42:40 +08:00
|
|
|
void ONNXAddOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand(0)->getType());
|
|
|
|
}
|
|
|
|
|
[MLIR] Lower ONNX element-wise binary ops: Mul, Div, Sub, And, Or, Xor (#388)
* Lower ONNX element-wise binary ops: Mul, Div, Sub, And, Or, Xor
* Edit gen_doc.py to avoid changes about AnyTypeOf<[AnyMemRef, AnyTensor]>
* Miss a space
* Add tests
* Shorten ONNXElementWiseBinaryOpLowering into ONNXEWBinaryOpLowering
* Move lowering patterns into runOnModule()
* Redundant space
2019-12-04 00:17:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Mul
|
|
|
|
/// Infer the output shape of the ONNXMulOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
|
|
|
void ONNXMulOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand(0)->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Div
|
|
|
|
/// Infer the output shape of the ONNXDivOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
|
|
|
void ONNXDivOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand(0)->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Sub
|
|
|
|
/// Infer the output shape of the ONNXSubOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
|
|
|
void ONNXSubOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand(0)->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// And
|
|
|
|
/// Infer the output shape of the ONNXAndOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
|
|
|
void ONNXAndOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand(0)->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Or
|
|
|
|
/// Infer the output shape of the ONNXOrOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
2019-12-14 04:28:56 +08:00
|
|
|
void ONNXOrOp::inferShapes() { getResult()->setType(getOperand(0)->getType()); }
|
[MLIR] Lower ONNX element-wise binary ops: Mul, Div, Sub, And, Or, Xor (#388)
* Lower ONNX element-wise binary ops: Mul, Div, Sub, And, Or, Xor
* Edit gen_doc.py to avoid changes about AnyTypeOf<[AnyMemRef, AnyTensor]>
* Miss a space
* Add tests
* Shorten ONNXElementWiseBinaryOpLowering into ONNXEWBinaryOpLowering
* Move lowering patterns into runOnModule()
* Redundant space
2019-12-04 00:17:21 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Xor
|
|
|
|
/// Infer the output shape of the ONNXXorOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
|
|
|
void ONNXXorOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand(0)->getType());
|
|
|
|
}
|
|
|
|
|
2019-11-13 02:37:46 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[MLIR] Add support for Max, Min, Sum, Elu, Selu, LeakyRelu, HardSigmoid (#395)
* Lower ONNXSumOp
* Add inferShapes() and test cases
* Load the first operand to the result
* Update SharingWork.md
* Update SharingWork.md
* Update SharingWork.md
* Add support for Max, Min
* Pass operation instead of location to mapToLowerScalarOp
* Add support for Elu, Selu, LeakyRelu, HardSigmoid
* Add test cases
* Update SharingWork.md
* Rewrite the part of lowering variadic ops and use it for binary ops
* Use two diffenrent templates for Unary and Variadic Ops
* Revise the code
2019-12-12 10:49:50 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Sum
|
|
|
|
/// Infer the output shape of the ONNXSumOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
|
|
|
void ONNXSumOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand(0)->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Max
|
|
|
|
/// Infer the output shape of the ONNXMaxOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
|
|
|
void ONNXMaxOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand(0)->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Min
|
|
|
|
/// Infer the output shape of the ONNXMinOp. This method is required by the
|
|
|
|
/// shape inference interface.
|
|
|
|
void ONNXMinOp::inferShapes() {
|
|
|
|
getResult()->setType(getOperand(0)->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-11-13 02:37:46 +08:00
|
|
|
// MatMul
|
|
|
|
|
|
|
|
void ONNXMatMulOp::inferShapes() {
|
2019-11-16 02:10:41 +08:00
|
|
|
// Cannot infer shape if no shape exists.
|
|
|
|
if (!getOperand(0)->getType().isa<RankedTensorType>() ||
|
|
|
|
!getOperand(1)->getType().isa<RankedTensorType>())
|
|
|
|
return;
|
2019-11-13 02:37:46 +08:00
|
|
|
auto lhsTy = getOperand(0)->getType().cast<RankedTensorType>();
|
|
|
|
auto rhsTy = getOperand(1)->getType().cast<RankedTensorType>();
|
2019-11-16 02:10:41 +08:00
|
|
|
SmallVector<int64_t, 2> dims;
|
|
|
|
dims.emplace_back(lhsTy.getShape()[0]);
|
2019-11-13 02:37:46 +08:00
|
|
|
dims.emplace_back(rhsTy.getShape()[1]);
|
|
|
|
getResult()->setType(RankedTensorType::get(dims, lhsTy.getElementType()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// Verify that matrix sizes are valid.
|
|
|
|
// Take into account the dimensionality of the matrix.
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Gemm
|
|
|
|
|
|
|
|
void ONNXGemmOp::inferShapes() {
|
2019-11-16 02:10:41 +08:00
|
|
|
// Cannot infer shape if no shape exists.
|
|
|
|
if (!getOperand(0)->getType().isa<RankedTensorType>() ||
|
|
|
|
!getOperand(1)->getType().isa<RankedTensorType>())
|
|
|
|
return;
|
2019-11-13 02:37:46 +08:00
|
|
|
auto lhsTy = getOperand(0)->getType().cast<RankedTensorType>();
|
|
|
|
auto rhsTy = getOperand(1)->getType().cast<RankedTensorType>();
|
2019-11-16 02:10:41 +08:00
|
|
|
SmallVector<int64_t, 2> dims;
|
|
|
|
dims.emplace_back(lhsTy.getShape()[0]);
|
2019-11-13 02:37:46 +08:00
|
|
|
dims.emplace_back(rhsTy.getShape()[1]);
|
|
|
|
getResult()->setType(RankedTensorType::get(dims, lhsTy.getElementType()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// FullGemm
|
|
|
|
|
|
|
|
void ONNXFullGemmOp::inferShapes() {
|
2019-11-16 02:10:41 +08:00
|
|
|
// Cannot infer shape if no shape exists.
|
|
|
|
if (!getOperand(0)->getType().isa<RankedTensorType>() ||
|
|
|
|
!getOperand(1)->getType().isa<RankedTensorType>())
|
|
|
|
return;
|
2019-11-13 02:37:46 +08:00
|
|
|
auto lhsTy = getOperand(0)->getType().cast<RankedTensorType>();
|
|
|
|
auto rhsTy = getOperand(1)->getType().cast<RankedTensorType>();
|
2019-11-16 02:10:41 +08:00
|
|
|
SmallVector<int64_t, 2> dims;
|
|
|
|
dims.emplace_back(lhsTy.getShape()[0]);
|
2019-11-13 02:37:46 +08:00
|
|
|
dims.emplace_back(rhsTy.getShape()[1]);
|
|
|
|
getResult()->setType(RankedTensorType::get(dims, lhsTy.getElementType()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// Verify that matrix sizes are valid for multiplication and addition.
|
|
|
|
// Take into account the dimensionality of the matrix.
|
|
|
|
|
2019-12-14 04:28:56 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Reshape
|
|
|
|
|
|
|
|
void ONNXReshapeOp::inferShapes() {
|
|
|
|
// Cannot infer shape if no shape tensor is specified.
|
|
|
|
if (!getOperand(1)->getType().isa<RankedTensorType>())
|
|
|
|
emitError("Shape tensor not ranked.");
|
|
|
|
|
|
|
|
auto inputTensorTy = getOperand(0)->getType().cast<RankedTensorType>();
|
|
|
|
auto shapeTensorTy = getOperand(1)->getType().cast<RankedTensorType>();
|
|
|
|
|
|
|
|
// Only rank 1 shape tensors are supported.
|
|
|
|
if (shapeTensorTy.getShape().size() != 1)
|
|
|
|
emitError("Shape tensor must have rank one.");
|
|
|
|
|
|
|
|
int64_t outputRank = shapeTensorTy.getShape()[0];
|
|
|
|
|
|
|
|
// Shape tensor must have constant shape.
|
|
|
|
if (outputRank < 0)
|
|
|
|
emitError("Shape tensor must have constant shape.");
|
|
|
|
|
|
|
|
SmallVector<int64_t, 2> dims;
|
|
|
|
for (int i = 0; i < outputRank; ++i)
|
|
|
|
dims.emplace_back(-1);
|
|
|
|
|
|
|
|
getResult()->setType(
|
|
|
|
RankedTensorType::get(dims, inputTensorTy.getElementType()));
|
|
|
|
}
|
|
|
|
|
2019-11-02 05:09:48 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TableGen'd op method definitions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define GET_OP_CLASSES
|
|
|
|
#include "src/compiler/onnx.cpp.inc"
|