2020-05-07 09:41:54 +08:00
|
|
|
//===-------------------------------------------------------*- tablegen -*-===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef TCF_OPS
|
|
|
|
#define TCF_OPS
|
|
|
|
|
|
|
|
include "npcomp/Dialect/TCF/IR/TCFBase.td"
|
|
|
|
|
|
|
|
class TCF_Op<string mnemonic, list<OpTrait> traits = []>
|
|
|
|
: Op<TCF_Dialect, mnemonic, traits> {
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: investigate effects framework for defining error semantics
|
|
|
|
// TODO: define in a general way across the dialect what "encounters an error" means.
|
|
|
|
|
2020-09-22 10:14:27 +08:00
|
|
|
class BinaryArithmeticOp<string mnemonic, list<OpTrait> traits = []> :
|
|
|
|
TCF_Op<mnemonic, traits> {
|
2020-05-07 09:41:54 +08:00
|
|
|
let arguments = (ins AnyTensor:$lhs, AnyTensor:$rhs);
|
|
|
|
let results = (outs AnyTensor:$result);
|
2020-09-19 05:05:36 +08:00
|
|
|
let assemblyFormat = "$lhs `,` $rhs attr-dict `:` functional-type(operands, results)";
|
2020-05-07 09:41:54 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 10:14:27 +08:00
|
|
|
def TCF_AddOp : BinaryArithmeticOp<"add"> {
|
|
|
|
let summary = "Addition of two tensors.";
|
|
|
|
let description = [{
|
|
|
|
Addition of two tensors.
|
|
|
|
|
|
|
|
Numpy-style broadcasting is allowed.
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
def TCF_MaxOp : BinaryArithmeticOp<"max"> {
|
|
|
|
let summary = "Maximum of two tensors.";
|
|
|
|
let description = [{
|
|
|
|
Maximum of two tensors.
|
|
|
|
|
|
|
|
Numpy-style broadcasting is allowed.
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2020-10-27 23:04:15 +08:00
|
|
|
def TCF_MulOp : BinaryArithmeticOp<"mul"> {
|
|
|
|
let summary = "Multiply an input tensor by a scalar tensor.";
|
|
|
|
let description = [{
|
|
|
|
Multiplies each element of the input `input` with the scalar `other` and returns a new resulting tensor. The tensor types must match and shapes must be broadcastable.
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2020-09-25 08:14:21 +08:00
|
|
|
class UnaryArithmeticOp<string mnemonic, list<OpTrait> traits = []> :
|
|
|
|
TCF_Op<mnemonic,
|
|
|
|
!listconcat(traits, [AllTypesMatch<["operand", "result"]>])>,
|
|
|
|
AllTypesMatch<["operand", "result"]> {
|
|
|
|
let arguments = (ins AnyTensor:$operand);
|
|
|
|
let results = (outs AnyTensor:$result);
|
|
|
|
let assemblyFormat = "$operand attr-dict `:` type($operand)";
|
|
|
|
}
|
|
|
|
|
|
|
|
def TCF_ExpOp : UnaryArithmeticOp<"exp"> {
|
|
|
|
let summary = "base-e exponential";
|
|
|
|
let description = [{
|
|
|
|
See std.exp for more details.
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
def TCF_TanhOp : UnaryArithmeticOp<"tanh"> {
|
|
|
|
let summary = "hyperbolic tangent";
|
|
|
|
let description = [{
|
|
|
|
See std.tanh for more details.
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2020-09-18 09:56:01 +08:00
|
|
|
// TODO: Generalize this op appropriately and add more verification.
|
|
|
|
// For example, an unranked operand probably should be allowed and verified
|
|
|
|
// dynamically in TCF->TCP lowering if needed.
|
|
|
|
def TCF_MatmulOp : TCF_Op<"matmul"> {
|
|
|
|
let summary = "Performs a matrix multiplication";
|
2020-05-07 09:41:54 +08:00
|
|
|
let description = [{
|
2020-09-18 09:56:01 +08:00
|
|
|
Performs a matrix multiplication.
|
2020-05-07 09:41:54 +08:00
|
|
|
|
2020-09-18 09:56:01 +08:00
|
|
|
The tensors have dimensions:
|
|
|
|
- lhs: [M, K]
|
|
|
|
- rhs: [K, N]
|
|
|
|
- result: [M, N]
|
2020-05-07 09:41:54 +08:00
|
|
|
|
2020-09-18 09:56:01 +08:00
|
|
|
If the `K` dimension mismatches between the operands, this op aborts the
|
|
|
|
program.
|
2020-05-07 09:41:54 +08:00
|
|
|
}];
|
2020-09-18 09:56:01 +08:00
|
|
|
let arguments = (ins 2DTensorOf<[F32]>:$lhs, 2DTensorOf<[F32]>:$rhs);
|
|
|
|
let results = (outs 2DTensorOf<[F32]>:$result);
|
2020-05-07 09:41:54 +08:00
|
|
|
|
2020-09-18 09:56:01 +08:00
|
|
|
let assemblyFormat = "$lhs `,` $rhs attr-dict `:` functional-type(operands, results)";
|
2020-05-07 09:41:54 +08:00
|
|
|
}
|
|
|
|
|
2020-12-16 04:53:12 +08:00
|
|
|
def TCF_ConvNCHWOp : TCF_Op<"conv_2d_nchw"> {
|
|
|
|
let summary = "2-D convolution";
|
|
|
|
let description = [{
|
|
|
|
Performs 2-D convolution. This op is inspired by PyTorch's Conv2d layer (https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html).
|
|
|
|
|
|
|
|
The tensors have dimensions:
|
|
|
|
- in: [N, Cin, H, W]
|
|
|
|
- filter: [Cout, Cin, KH, KW]
|
|
|
|
- result: [N, Cout, Hout, Wout]
|
|
|
|
|
|
|
|
The tensors must meet the following conditions; otherwise, this op aborts the program.
|
|
|
|
- H is greater than or equal to KH
|
|
|
|
- W is greater than or equal to KW
|
|
|
|
- Cin matches between in and filter
|
|
|
|
}];
|
|
|
|
let arguments = (ins 4DTensorOf<[F32]>:$in, 4DTensorOf<[F32]>:$filter);
|
|
|
|
let results = (outs 4DTensorOf<[F32]>:$result);
|
|
|
|
|
|
|
|
let assemblyFormat = "$in `,` $filter attr-dict `:` functional-type(operands, results)";
|
|
|
|
}
|
|
|
|
|
2020-05-07 09:41:54 +08:00
|
|
|
#endif // #ifndef TCF_OPS
|