activation_op_mlu_test.cc 4.4 KB
Newer Older
F
fwenguang 已提交
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/* Copyright (c) 2021 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. */

#include <gtest/gtest.h>

#include "paddle/fluid/operators/activation_op.h"
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/platform/device/mlu/device_context.h"
#include "paddle/fluid/platform/place.h"

namespace fw = paddle::framework;
namespace plat = paddle::platform;
namespace math = paddle::operators::math;

USE_OP(relu);
USE_OP_DEVICE_KERNEL(relu, MLU);

// relu
template <typename T>
inline T relu(T x) {
  return x > 0 ? x : 0.;
}

template <typename T>
inline T relu_grad_dx(T x, T out, T dout) {
  return out > 0 ? dout : 0;
}

template <typename T>
void Compare(fw::Scope* scope, const plat::DeviceContext& ctx,
             std::string op_type) {
  // init
  auto x = scope->Var("X");
  auto tensor_x = x->GetMutable<fw::LoDTensor>();

  const int num = 10;
  std::vector<T> init_x;
  for (int64_t i = 0; i < num * num; ++i) {
    init_x.push_back(static_cast<T>(i - 50));
  }
  TensorFromVector(init_x, ctx, tensor_x);
  tensor_x->Resize({num, num});

  auto place = ctx.GetPlace();
  auto out = scope->Var("Out");
  auto tensor_out = out->GetMutable<fw::LoDTensor>();

  fw::AttributeMap attrs;
  auto op = fw::OpRegistry::CreateOp(op_type, {{"X", {"X"}}},
                                     {{"Out", {"Out"}}}, attrs);
  op->Run(*scope, place);

  ctx.Wait();

  // eval time
  struct timeval start, end;
  gettimeofday(&start, NULL);

  for (int i = 0; i < 100; i++) {
    op->Run(*scope, place);
  }

  ctx.Wait();

  gettimeofday(&end, NULL);
  int micros =
      (((end.tv_sec - start.tv_sec) * 1000000) + end.tv_usec) - (start.tv_usec);
  printf("used time: %d\n", micros / 100);

  // eval value
  std::vector<T> out_vec;
  TensorToVector(*tensor_out, ctx, &out_vec);

  ctx.Wait();

  for (uint32_t i = 0; i < out_vec.size(); i++) {
    EXPECT_FLOAT_EQ(out_vec[i], relu<T>(init_x[i]));
  }
}

template <typename T>
void CompareGrad(fw::Scope* scope, const plat::DeviceContext& ctx,
                 std::string op_type) {
  auto dout = scope->Var("DOut");
  auto tensor_dout = dout->GetMutable<fw::LoDTensor>();
  auto out = scope->Var("Out");
  auto tensor_out = out->GetMutable<fw::LoDTensor>();

  const int num = 10;
  std::vector<T> init_dout;
  for (int64_t i = 0; i < num * num; ++i) {
    init_dout.push_back(static_cast<T>(1.0));
  }

  std::vector<T> init_out;
  for (int64_t i = 0; i < num * num; ++i) {
    init_out.push_back(static_cast<T>(i - 50));
  }

  TensorFromVector(init_dout, ctx, tensor_dout);
  tensor_dout->Resize({num, num});
  TensorFromVector(init_out, ctx, tensor_out);
  tensor_out->Resize({num, num});

  auto dx = scope->Var("DX");
  auto tensor_dx = dx->GetMutable<fw::LoDTensor>();

  // run
  auto place = ctx.GetPlace();
  fw::AttributeMap attrs;
  auto op = fw::OpRegistry::CreateOp(op_type,
                                     {{"Out@GRAD", {"DOut"}}, {"Out", {"Out"}}},
                                     {{"X@GRAD", {"DX"}}}, attrs);
  op->Run(*scope, place);

  ctx.Wait();

  // eval time
  struct timeval start, end;
  gettimeofday(&start, NULL);

  for (int i = 0; i < 100; i++) {
    op->Run(*scope, place);
  }

  ctx.Wait();

  gettimeofday(&end, NULL);
  int micros =
      (((end.tv_sec - start.tv_sec) * 1000000) + end.tv_usec) - (start.tv_usec);
  printf("used time: %d\n", micros / 100);

  // eval value
  std::vector<T> dx_vec;
  TensorToVector(*tensor_dx, ctx, &dx_vec);

  ctx.Wait();

  for (uint32_t i = 0; i < dx_vec.size(); i++) {
    EXPECT_FLOAT_EQ(dx_vec[i],
                    relu_grad_dx<T>(dx_vec[i], init_out[i], init_dout[i]));
  }
}

TEST(relu, MLU_fp32) {
  fw::Scope scope;
  auto* ctx = plat::DeviceContextPool::Instance().Get(plat::MLUPlace(0));
  Compare<float>(&scope, *ctx, "relu");
}

TEST(relu_grad, MLU_fp32) {
  fw::Scope scope;
  auto* ctx = plat::DeviceContextPool::Instance().Get(plat::MLUPlace(0));
  CompareGrad<float>(&scope, *ctx, "relu_grad");
}