dense_tensor.td 6.1 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4
#ifdef DT_OPS
#else
#define DT_OPS

5
include "paddle/infrt/dialect/infrt/ir/infrt_base.td"
Y
Yan Chunwei 已提交
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
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
  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.
108
  let arguments = (ins StrAttr:$path);
109
  let results = (outs DenseHostTensorMap:$out);
Y
Yan Chunwei 已提交
110

111
  let assemblyFormat = "`(``)`attr-dict";
Y
Yan Chunwei 已提交
112 113
}

114

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

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

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

132
def TensorMapGetSizeOp : DT_Op<"tensor_map_get_size", [NoSideEffect]> {
W
Wilber 已提交
133
  let summary = "dt.tensor_map_get_size operation";
134 135 136 137 138

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

139
  let arguments = (ins DenseHostTensorMap:$map);
140 141 142 143
  let results = (outs I32:$size);
  let assemblyFormat = "`(` $map `)` attr-dict `->` type($size)";
}

W
Wilber 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
def Infrt_TensorListGetTensorOp : DT_Op<"tensor_list_get_tensor", [NoSideEffect]> {
  let summary = "dt.tensor_list_get_tensor operation";

  let description = [{
    An operation that can get a tensor from a TensorList.
  }];

  let arguments = (ins
          DenseTensorList:$l,
          I32Attr:$id
          );
  let results = (outs DenseTensor:$output);
  let verifier = ?;
}

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

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

  let arguments = (ins DenseTensorList:$map);
  let results = (outs I32:$size);
}

Y
Yan Chunwei 已提交
170 171 172 173 174 175 176
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.
  }];

177
  let arguments = (ins DenseTensor:$input);
Y
Yan Chunwei 已提交
178 179 180 181
  let results = (outs TS_Shape:$output);
  let assemblyFormat = "$input attr-dict `:` type($input) `->` type($output)";
}

182 183 184 185 186 187 188 189
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.
  }];
190 191
  let arguments = (ins DenseTensor:$a, DenseTensor:$b);
  let results = (outs DenseTensor:$output);
192 193 194 195 196 197 198 199 200 201 202
  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.
  }];
203 204
  let arguments = (ins DenseTensor:$x, DenseTensor:$w);
  let results = (outs DenseTensor:$output);
205 206 207
  let assemblyFormat = "`(` $x `,` $w `)` attr-dict `:` `(` type($x) `,` type($w) `)` `->` type($output)";
}

Y
Yan Chunwei 已提交
208 209 210 211
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>;
212 213
  def DT_NaiveElementwiseAddOp_#dtype : NaiveElementwiseAddOp<dtype>;
  def DT_NaiveMatmulOp_#dtype : NaiveMatmulOp<dtype>;
Y
Yan Chunwei 已提交
214 215 216
}

#endif  // DT_OPS