|
| 1 | +/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#ifndef _WIN32 |
| 16 | +#include <unistd.h> |
| 17 | +#endif |
| 18 | + |
| 19 | +#include <string> |
| 20 | +#include <thread> // NOLINT |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +#include "gtest/gtest.h" |
| 24 | +#include "paddle/fluid/framework/op_registry.h" |
| 25 | +#include "paddle/fluid/framework/operator.h" |
| 26 | +#include "paddle/fluid/framework/program_desc.h" |
| 27 | +#include "paddle/fluid/operators/dropout_op.h" |
| 28 | +#include "paddle/fluid/operators/math/math_function.h" |
| 29 | +#include "paddle/fluid/string/printf.h" |
| 30 | + |
| 31 | +namespace f = paddle::framework; |
| 32 | +namespace p = paddle::platform; |
| 33 | +namespace m = paddle::operators::math; |
| 34 | + |
| 35 | +USE_OP(gelu); |
| 36 | +USE_OP_DEVICE_KERNEL(gelu, NPU); |
| 37 | + |
| 38 | +template <typename T> |
| 39 | +void Compare(f::Scope* scope, const p::DeviceContext& ctx) { |
| 40 | + // init |
| 41 | + auto x = scope->Var("X"); |
| 42 | + auto tensor_x = x->GetMutable<f::LoDTensor>(); |
| 43 | + |
| 44 | + std::vector<T> init_x; |
| 45 | + for (int64_t i = 0; i < 10 * 10; ++i) { |
| 46 | + init_x.push_back(static_cast<T>(1.0)); |
| 47 | + } |
| 48 | + |
| 49 | + TensorFromVector(init_x, ctx, tensor_x); |
| 50 | + tensor_x->Resize({10, 10}); |
| 51 | + |
| 52 | + auto out = scope->Var("Out"); |
| 53 | + auto tensor_out = out->GetMutable<f::LoDTensor>(); |
| 54 | + |
| 55 | + f::AttributeMap attrs; |
| 56 | + |
| 57 | + ctx.Wait(); |
| 58 | + |
| 59 | + // run |
| 60 | + auto place = ctx.GetPlace(); |
| 61 | + |
| 62 | + auto op = f::OpRegistry::CreateOp("gelu", {{"X", {"X"}}}, |
| 63 | + {{"Out", {"Out"}}}, attrs); |
| 64 | + op->Run(*scope, place); |
| 65 | + |
| 66 | + ctx.Wait(); |
| 67 | + |
| 68 | + // eval time |
| 69 | + struct timeval start, end; |
| 70 | + gettimeofday(&start, NULL); |
| 71 | + |
| 72 | + for (int i = 0; i < 100; i++) { |
| 73 | + op->Run(*scope, place); |
| 74 | + } |
| 75 | + |
| 76 | + ctx.Wait(); |
| 77 | + |
| 78 | + gettimeofday(&end, NULL); |
| 79 | + int micros = (((end.tv_sec - start.tv_sec) * 1000000) + |
| 80 | + end.tv_usec) - (start.tv_usec); |
| 81 | + printf("used time: %d\n", micros / 100); |
| 82 | + |
| 83 | + // eval value |
| 84 | + std::vector<T> out_vec; |
| 85 | + TensorToVector(*tensor_out, ctx, &out_vec); |
| 86 | + |
| 87 | + float expected = 0.841192; |
| 88 | + for (uint32_t i = 0; i < out_vec.size(); i++) { |
| 89 | + EXPECT_FLOAT_EQ(out_vec[i], static_cast<T>(expected)); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +template <typename T> |
| 94 | +void CompareGrad(f::Scope* scope, const p::DeviceContext& ctx) { |
| 95 | + auto dout = scope->Var("DOut"); |
| 96 | + auto tensor_dout = dout->GetMutable<f::LoDTensor>(); |
| 97 | + |
| 98 | + auto x = scope->Var("X"); |
| 99 | + auto tensor_x = x->GetMutable<f::LoDTensor>(); |
| 100 | + |
| 101 | + std::vector<T> init_dout; |
| 102 | + for (int64_t i = 0; i < 10 * 10; ++i) { |
| 103 | + init_dout.push_back(static_cast<T>(1.0)); |
| 104 | + } |
| 105 | + |
| 106 | + std::vector<T> init_x; |
| 107 | + for (int64_t i = 0; i < 10 * 10; ++i) { |
| 108 | + init_x.push_back(static_cast<T>(1.0)); |
| 109 | + } |
| 110 | + |
| 111 | + TensorFromVector(init_dout, ctx, tensor_dout); |
| 112 | + tensor_dout->Resize({10, 10}); |
| 113 | + TensorFromVector(init_x, ctx, tensor_x); |
| 114 | + tensor_x->Resize({10, 10}); |
| 115 | + |
| 116 | + auto dx = scope->Var("DX"); |
| 117 | + auto tensor_dx = dx->GetMutable<f::LoDTensor>(); |
| 118 | + |
| 119 | + f::AttributeMap attrs; |
| 120 | + |
| 121 | + ctx.Wait(); |
| 122 | + |
| 123 | + // run |
| 124 | + auto place = ctx.GetPlace(); |
| 125 | + |
| 126 | + auto op = f::OpRegistry::CreateOp("gelu_grad", |
| 127 | + {{"Out@GRAD", {"DOut"}}, {"X", {"X"}}}, |
| 128 | + {{"X@GRAD", {"DX"}}}, attrs); |
| 129 | + op->Run(*scope, place); |
| 130 | + |
| 131 | + ctx.Wait(); |
| 132 | + |
| 133 | + // eval time |
| 134 | + struct timeval start, end; |
| 135 | + gettimeofday(&start, NULL); |
| 136 | + |
| 137 | + for (int i = 0; i < 100; i++) { |
| 138 | + op->Run(*scope, place); |
| 139 | + } |
| 140 | + |
| 141 | + ctx.Wait(); |
| 142 | + |
| 143 | + gettimeofday(&end, NULL); |
| 144 | + int micros = (((end.tv_sec - start.tv_sec) * 1000000) + |
| 145 | + end.tv_usec) - (start.tv_usec); |
| 146 | + printf("used time: %d\n", micros / 100); |
| 147 | + |
| 148 | + // eval value |
| 149 | + std::vector<T> dx_vec; |
| 150 | + TensorToVector(*tensor_dx, ctx, &dx_vec); |
| 151 | + |
| 152 | + float expected = 1.082964; |
| 153 | + for (uint32_t i = 0; i < dx_vec.size(); i++) { |
| 154 | + EXPECT_FLOAT_EQ(dx_vec[i], static_cast<T>(expected)); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +TEST(gelu, NPU_fp32) { |
| 159 | + f::Scope scope; |
| 160 | + p::NPUDeviceContext ctx(p::NPUPlace(0)); |
| 161 | + Compare<float>(&scope, ctx); |
| 162 | +} |
| 163 | + |
| 164 | +TEST(gelu_grad, NPU) { |
| 165 | + f::Scope scope; |
| 166 | + p::NPUDeviceContext ctx(p::NPUPlace(0)); |
| 167 | + CompareGrad<float>(&scope, ctx); |
| 168 | +} |
| 169 | + |
0 commit comments