fc.mlir 2.3 KB
Newer Older
Y
Yan Chunwei 已提交
1
// CHECK-LABEL: @fc
2 3 4
func @fc(%input : !Infrt.tensor<X86, NCHW, F32>,
         %w : !Infrt.tensor<X86, NCHW, F32>,
         %bias : !Infrt.tensor<X86, NCHW, F32>) -> !Infrt.tensor<X86, NCHW, F32>
Y
Yan Chunwei 已提交
5
{
6 7
  %out = dt.create_uninit_tensor.f32 [30, 50] -> !Infrt.tensor<X86, NCHW, F32>
  // dt.fill_tensor_with_constant.f32 (%out : !Infrt.tensor<X86, NCHW, F32>) {value=0.0:f32}
Y
Yan Chunwei 已提交
8 9

  // fc1
10 11 12
  "external.matmul"(%input, %w, %out) {}: (!Infrt.tensor<X86, NCHW, F32>, !Infrt.tensor<X86, NCHW, F32>, !Infrt.tensor<X86, NCHW, F32>) -> ()
  "external.elementwise_add"(%out, %bias, %out) {axis = -1}: (!Infrt.tensor<X86, NCHW, F32>, !Infrt.tensor<X86, NCHW, F32>, !Infrt.tensor<X86, NCHW, F32>) -> ()
  "external.sigmoid"(%out, %out) {}: (!Infrt.tensor<X86, NCHW, F32>, !Infrt.tensor<X86, NCHW, F32>) -> ()
Y
Yan Chunwei 已提交
13 14

  // fc2
15 16 17
  "external.matmul"(%out, %w, %out) {}: (!Infrt.tensor<X86, NCHW, F32>, !Infrt.tensor<X86, NCHW, F32>, !Infrt.tensor<X86, NCHW, F32>) -> ()
  "external.elementwise_add"(%out, %bias, %out) {axis = -1}: (!Infrt.tensor<X86, NCHW, F32>, !Infrt.tensor<X86, NCHW, F32>, !Infrt.tensor<X86, NCHW, F32>) -> ()
  "external.sigmoid"(%out, %out) {}: (!Infrt.tensor<X86, NCHW, F32>, !Infrt.tensor<X86, NCHW, F32>) -> ()
Y
Yan Chunwei 已提交
18

19
  Infrt.return %out : !Infrt.tensor<X86, NCHW, F32>
Y
Yan Chunwei 已提交
20 21 22 23
}

// CHECK-LABEL: @benchmark
func @benchmark() {
24 25
  %input = dt.create_uninit_tensor.f32 [30, 50] -> !Infrt.tensor<X86, NCHW, F32>
  dt.fill_tensor_with_constant.f32 (%input : !Infrt.tensor<X86, NCHW, F32>) {value=1.0:f32}
Y
Yan Chunwei 已提交
26

27 28
  %w = dt.create_uninit_tensor.f32 [50, 50] -> !Infrt.tensor<X86, NCHW, F32>
  dt.fill_tensor_with_constant.f32 (%w : !Infrt.tensor<X86, NCHW, F32>) {value=2.0:f32}
Y
Yan Chunwei 已提交
29

30 31
  %bias = dt.create_uninit_tensor.f32 [30, 50] -> !Infrt.tensor<X86, NCHW, F32>
  dt.fill_tensor_with_constant.f32 (%bias : !Infrt.tensor<X86, NCHW, F32>) {value=3.0:f32}
Y
Yan Chunwei 已提交
32

33 34 35 36
  Infrt.benchmark "add.f32"(
          %input:!Infrt.tensor<X86, NCHW, F32>,
          %w:!Infrt.tensor<X86, NCHW, F32>,
          %bias:!Infrt.tensor<X86, NCHW, F32>)
Y
Yan Chunwei 已提交
37 38
          duration_secs = 100, max_count = 300000, num_warmup_runs = 3
  {
39 40
    %res = Infrt.call @fc(%input, %w, %bias) : (!Infrt.tensor<X86, NCHW, F32>, !Infrt.tensor<X86, NCHW, F32>, !Infrt.tensor<X86, NCHW, F32>) -> (!Infrt.tensor<X86, NCHW, F32>)
    Infrt.return %res : !Infrt.tensor<X86, NCHW, F32>
Y
Yan Chunwei 已提交
41
  }
42
  Infrt.return
Y
Yan Chunwei 已提交
43
}