test_mul_op.cpp 2.8 KB
Newer Older
E
eclipsess 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

E
eclipsess 已提交
15
#include "../executor_for_test.h"
E
eclipsess 已提交
16
#include "../test_include.h"
E
eclipsess 已提交
17
#include "operators/mul_op.h"
E
eclipsess 已提交
18

E
eclipsess 已提交
19
int main() {
20
  paddle_mobile::Loader<paddle_mobile::CPU> loader;
E
eclipsess 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  auto program = loader.Load(g_resnet);
  PADDLE_MOBILE_ENFORCE(program.originProgram != nullptr,
                        "program file read fail");

  Executor4Test<paddle_mobile::CPU,
                paddle_mobile::operators::MulOp<paddle_mobile::CPU, float>>
      executor(program, "mul");

  // 1. input_tensors;
  vector<Tensor> input_tensors;

  Tensor input1;
  auto input1_data = CreateInput<float>(&input1, {3, 2, 1, 1}, 0, 1);
  input_tensors.push_back(input1);
  Tensor input2;
  auto input2_data = CreateInput<float>(&input2, {2, 3}, 0, 1);
  input_tensors.push_back(input2);

  // 2. input_names
  vector<string> input_names({
      "pool2d_0.tmp_0",
      "fc_0.w_0",
  });

  // 3. output_names
  vector<string> output_names({"fc_0.tmp_0"});

  // 4. out_dims;
  vector<DDim> out_ddims;
  auto out_ddim = paddle_mobile::framework::make_ddim({3, 3});
  out_ddims.push_back(out_ddim);

L
liuruilong 已提交
53
  auto output = executor.Predict<LoDTensor>(input_tensors, input_names,
E
eclipsess 已提交
54 55 56 57 58 59 60 61 62
                                            output_names, out_ddims);

  auto output0_data = output[0]->data<float>();

  auto dim_1 = input1.numel() / input1.dims()[0];
  DLOG << " input1 : ";
  for (int i = 0; i < input1.dims()[0]; ++i) {
    for (int j = 0; j < dim_1; ++j) {
      DLOGF("%f ", input1_data[i * dim_1 + j]);
E
eclipsess 已提交
63
    }
64 65 66
    DLOGF("\n");
  }

E
eclipsess 已提交
67 68 69 70 71
  auto dim_2 = input2.numel() / input2.dims()[0];
  DLOG << " input2 : ";
  for (int i = 0; i < input2.dims()[0]; ++i) {
    for (int j = 0; j < dim_2; ++j) {
      DLOGF("%f ", input2_data[i * dim_2 + j]);
E
eclipsess 已提交
72
    }
73 74 75
    DLOGF("\n");
  }

E
eclipsess 已提交
76
  auto dim_output0 = output[0]->numel() / output[0]->dims()[0];
77
  DLOG << " output : ";
E
eclipsess 已提交
78 79 80
  for (int i = 0; i < output[0]->dims()[0]; ++i) {
    for (int j = 0; j < dim_output0; ++j) {
      DLOGF("%f ", output0_data[i * dim_2 + j]);
E
eclipsess 已提交
81
    }
82 83
    DLOGF("\n");
  }
E
eclipsess 已提交
84

85
  /// output (3,3)
E
eclipsess 已提交
86 87
  DLOG << "output memory size : " << output[0]->memory_size();
  DLOG << "output numel : " << output[0]->numel();
E
eclipsess 已提交
88

E
eclipsess 已提交
89 90
  DLOG << input1_data[0] << " x " << input2_data[0] << " + " << input1_data[1]
       << " x " << input2_data[0 + 3] << " = " << output0_data[0];
91
  return 0;
E
eclipsess 已提交
92
}