dense_tensor.td 5.4 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#ifdef DT_OPS
#else
#define DT_OPS

include "paddle/infrt/dialect/infrt_base.td"
include "paddle/infrt/dialect/tensor_shape_base.td"
include "mlir/Interfaces/SideEffectInterfaces.td"

def DT_Dialect : Dialect {
  let name = "dt";

  let description = [{
      The DenseTensor dialect.
  }];

  let cppNamespace = "::infrt::dt";
}

class DT_Op<string mnemonic, list<OpTrait> traits = []> :
      Op<DT_Dialect, mnemonic, traits>;

class CreateUninitTensorOp<string dtype>
      : DT_Op<"create_uninit_tensor." # dtype, [NoSideEffect]> {
  let summary = "dt.create_uninit_tensor operation";

  let description = [{
      An operation that creates an uninitialized tensor.
  }];

  let arguments = (ins I64ArrayAttr:$shape);
31
  let results = (outs DenseTensor:$output);
Y
Yan Chunwei 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45

  let parser  = [{ return infrt::dt::parseCreateUninitTensorOp(parser, result); }];
  let printer = [{ return infrt::dt::printCreateUninitTensorOp(p, *this); }];
}


def ShallowCopyTensorOp
      : DT_Op<"shallow_copy_tensor", [NoSideEffect]> {
  let summary = "dt.shallow_copy_tensor operation";

  let description = [{
      An operation that copy a tensor shallowly.
  }];

46 47
  let arguments = (ins DenseTensor:$input);
  let results = (outs DenseTensor:$output);
Y
Yan Chunwei 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61

  let assemblyFormat = "$input attr-dict `:` type($input) `->` type($output)";
}


class FillTensorWithConstantOp<string dtype> :
      DT_Op<"fill_tensor_with_constant." # dtype> {
  let summary = "dt.fill_tensor_with_constant operation";

  let description = [{
      An operation that fills an input tensor with a value.
  }];

  let arguments = (ins
62
      DenseTensor:$input,
Y
Yan Chunwei 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
      AnyAttr:$value
  );
  let results = (outs);

  // TODO: can be removed?
  //let parser  = [{ return infrt::dt::parseFillTensorWithConstantOp(parser, result); }];
  //let printer = [{ return infrt::dt::printFillTensorWithConstantOp(p, *this); }];
  let assemblyFormat = "`(` $input `:` type($input) `)`  attr-dict";
}

def PrintTensorOp : DT_Op<"print_tensor"> {
  let summary = "dt.print_tensor operation";

  let description = [{
    An operation that prints a tensor.
  }];

80
  let arguments = (ins DenseTensor:$input);
Y
Yan Chunwei 已提交
81 82 83 84 85 86 87 88 89 90 91 92
  let results = (outs);
  let assemblyFormat = "`(` $input `:` type($input) `)` attr-dict";
}

class SetTensorOp<string dtype> :
      DT_Op<"set_tensor_with_constant_values." # dtype> {
  let summary = "dt.set_tensor_with_constant_values operation";

  let description = [{
    An operation that sets an input tensor with given values.
  }];

93
  let arguments = (ins DenseTensor);
Y
Yan Chunwei 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  let results = (outs);

  let parser  = [{ return infrt::dt::parseSetTensorOp(parser, result); }];
  let printer = [{ return infrt::dt::printSetTensorOp(p, *this); }];
}

def LoadParamsOp : DT_Op<"load_params", [NoSideEffect]> {
  let summary = "dt.load_params operation";

  let description = [{
    An operation that can load tensors to TensorMap.
  }];

  // input path of model params.
  let arguments = (ins StringType:$path);
  let results = (outs TensorMapType);

  let assemblyFormat = "`(` operands `)` attr-dict";
  let verifier = ?;
}

115

116 117
def TensorMapGetTensorOp : DT_Op<"tensor_map_get_tensor", [NoSideEffect]> {
  let summary = "dt.tensor_map_get_tensor operation";
Y
Yan Chunwei 已提交
118 119

  let description = [{
120
    An operation that can get a tensor from a TensorMap.
Y
Yan Chunwei 已提交
121 122 123 124 125
  }];

  // input path of model params.
  let arguments = (ins
          TensorMapType:$map,
126
          StrAttr:$name
Y
Yan Chunwei 已提交
127
          );
128
  let results = (outs DenseTensor:$output);
129
  let assemblyFormat = "`(` operands `)` attr-dict `->` type($output)";
Y
Yan Chunwei 已提交
130 131 132
  let verifier = ?;
}

133 134 135 136 137 138 139 140 141 142 143 144
def TensorMapGetSizeOp : DT_Op<"tensor_map_get_size", [NoSideEffect]> {
  let summary = "ddt.tensor_map_get_size operation";

  let description = [{
    An operation that get the size of a TensorMap.
  }];

  let arguments = (ins TensorMapType:$map);
  let results = (outs I32:$size);
  let assemblyFormat = "`(` $map `)` attr-dict `->` type($size)";
}

Y
Yan Chunwei 已提交
145 146 147 148 149 150 151
def GetTensorShapeOp : DT_Op<"get_tensor_shape", [NoSideEffect]> {
  let summary = "dt.get_tensor_shape operation";

  let description = [{
      An operation that returns the shape of the input tensor.
  }];

152
  let arguments = (ins DenseTensor:$input);
Y
Yan Chunwei 已提交
153 154 155 156
  let results = (outs TS_Shape:$output);
  let assemblyFormat = "$input attr-dict `:` type($input) `->` type($output)";
}

157 158 159 160 161 162 163 164
class NaiveElementwiseAddOp<string dtype> :
    DT_Op<"naive_elementwise_add." # dtype, [NoSideEffect]> {
  let summary = "dt.naive_elementwise_add operation";

  let description = [{
    Naive elementwise_add operation.
    Just for testing.
  }];
165 166
  let arguments = (ins DenseTensor:$a, DenseTensor:$b);
  let results = (outs DenseTensor:$output);
167 168 169 170 171 172 173 174 175 176 177
  let assemblyFormat = "`(` $a `,` $b `)` attr-dict `:` `(` type($a) `,` type($b) `)` `->` type($output)";
}

class NaiveMatmulOp<string dtype> :
    DT_Op<"naive_matmul." # dtype, [NoSideEffect]> {
  let summary = "dt.naive_matmul operation";

  let description = [{
    Naive matmul operation.
    Just for testing.
  }];
178 179
  let arguments = (ins DenseTensor:$x, DenseTensor:$w);
  let results = (outs DenseTensor:$output);
180 181 182
  let assemblyFormat = "`(` $x `,` $w `)` attr-dict `:` `(` type($x) `,` type($w) `)` `->` type($output)";
}

Y
Yan Chunwei 已提交
183 184 185 186
foreach dtype = ["ui8", "ui16", "ui32", "ui64", "i32", "f32", "f64", "i64"] in {
  def DT_CreateUninitTensorOp_#dtype : CreateUninitTensorOp<dtype>;
  def DT_FillTensorOp_#dtype : FillTensorWithConstantOp<dtype>;
  def DT_SetTensorOp_#dtype : SetTensorOp<dtype>;
187 188
  def DT_NaiveElementwiseAddOp_#dtype : NaiveElementwiseAddOp<dtype>;
  def DT_NaiveMatmulOp_#dtype : NaiveMatmulOp<dtype>;
Y
Yan Chunwei 已提交
189 190 191
}

#endif  // DT_OPS