|
| 1 | +/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +Unless required by applicable law or agreed to in writing, software |
| 7 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +See the License for the specific language governing permissions and |
| 10 | +limitations under the License. */ |
| 11 | + |
| 12 | +#ifndef _WIN32 |
| 13 | +#include <unistd.h> |
| 14 | +#endif |
| 15 | + |
| 16 | +#include <string> |
| 17 | +#include <cmath> |
| 18 | +#include <thread> // NOLINT |
| 19 | +#include <vector> |
| 20 | +#include <numeric> |
| 21 | +#include <iostream> |
| 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(transpose); |
| 36 | +USE_OP_DEVICE_KERNEL(transpose, NPU); |
| 37 | + |
| 38 | + |
| 39 | +template <typename T> |
| 40 | +void Compare(f::Scope* scope, const p::DeviceContext& ctx) { |
| 41 | + // init |
| 42 | + auto x = scope->Var("X"); |
| 43 | + auto out = scope->Var("Out"); |
| 44 | + auto* x_t = x->GetMutable<f::LoDTensor>(); |
| 45 | + auto* out_t = out->GetMutable<f::LoDTensor>(); |
| 46 | + auto place = ctx.GetPlace(); |
| 47 | + |
| 48 | + int dim0=2; |
| 49 | + int dim1=2; |
| 50 | + TensorFromVector(std::vector<T>({0,1,2,3}), ctx, x_t); |
| 51 | + ctx.Wait(); |
| 52 | + x_t->Resize({dim0, dim1}); |
| 53 | + out_t->Resize({dim0, dim1}); |
| 54 | + ctx.Wait(); |
| 55 | + out_t->mutable_data<T>(place); |
| 56 | + ctx.Wait(); |
| 57 | + |
| 58 | + f::AttributeMap attrs = { |
| 59 | + {"axis", std::vector<int>({1, 0})}, |
| 60 | + {"data_format", std::string("AnyLayout")} |
| 61 | + }; |
| 62 | + auto op = f::OpRegistry::CreateOp("transpose", {{"X", {"X"}}}, |
| 63 | + {{"Out", {"Out"}}}, attrs); |
| 64 | + ctx.Wait(); |
| 65 | + op->Run(*scope, place); |
| 66 | + ctx.Wait(); |
| 67 | + std::vector<T> out_v; |
| 68 | + TensorToVector(*out_t, ctx, &out_v); |
| 69 | + ctx.Wait(); |
| 70 | + |
| 71 | + EXPECT_EQ(out_t->numel(), dim0 * dim1); |
| 72 | + EXPECT_EQ(out_v[0], 0); |
| 73 | + EXPECT_EQ(out_v[1], 2); |
| 74 | + EXPECT_EQ(out_v[2], 1); |
| 75 | + EXPECT_EQ(out_v[3], 3); |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +template <typename T> |
| 80 | +void CompareGrad(f::Scope* scope, const p::DeviceContext& ctx) { |
| 81 | + // init |
| 82 | + std::cout<<"run grad test"<<std::endl; |
| 83 | + auto x = scope->Var("X"); |
| 84 | + auto x_grad = scope->Var("X@GRAD"); |
| 85 | + auto out = scope->Var("Out"); |
| 86 | + auto out_grad = scope->Var("Out@GRAD"); |
| 87 | + |
| 88 | + auto* x_grad_t = x_grad->GetMutable<f::LoDTensor>(); |
| 89 | + auto* x_t = x->GetMutable<f::LoDTensor>(); |
| 90 | + auto* out_grad_t = out_grad->GetMutable<f::LoDTensor>(); |
| 91 | + auto* out_t = out->GetMutable<f::LoDTensor>(); |
| 92 | + int dim0=2; |
| 93 | + int dim1=2; |
| 94 | + auto place = ctx.GetPlace(); |
| 95 | + |
| 96 | + std::cout<<"build up tensor"<<std::endl; |
| 97 | + TensorFromVector(std::vector<T>({0,1,2,3}), ctx, out_grad_t); |
| 98 | + TensorFromVector(std::vector<T>({0,1,2,3}), ctx, x_t); |
| 99 | + ctx.Wait(); |
| 100 | + x_grad_t->Resize({dim0, dim1}); |
| 101 | + x_t->Resize({dim0, dim1}); |
| 102 | + out_grad_t->Resize({dim0, dim1}); |
| 103 | + out_t->Resize({dim0, dim1}); |
| 104 | + |
| 105 | + //out_grad_t->mutable_data<T>(place); |
| 106 | + x_grad_t->mutable_data<T>(place); |
| 107 | + out_t->mutable_data<T>(place); |
| 108 | + ctx.Wait(); |
| 109 | + |
| 110 | + std::cout<<"build op"<<std::endl; |
| 111 | + f::AttributeMap attrs = { |
| 112 | + {"axis", std::vector<int>({1, 0})}, |
| 113 | + {"data_format", std::string("AnyLayout")} |
| 114 | + }; |
| 115 | + /* |
| 116 | + {"mkldnn_data_type", "float32"}, |
| 117 | + {"use_mkldnn", false}, |
| 118 | + {"use_quantizer", false}, |
| 119 | + */ |
| 120 | + auto op = f::OpRegistry::CreateOp("transpose_grad", {{"Out@GRAD", {"Out@GRAD"}}, {"X", {"X"}}, {"Out", {"Out"}}}, |
| 121 | + {{"X@GRAD", {"X@GRAD"}}}, attrs); |
| 122 | + std::cout<<"run op"<<std::endl; |
| 123 | + op->Run(*scope, place); |
| 124 | + ctx.Wait(); |
| 125 | + std::cout<<"build res"<<std::endl; |
| 126 | + std::vector<T> out_v; |
| 127 | + TensorToVector(*x_grad_t, ctx, &out_v); |
| 128 | + ctx.Wait(); |
| 129 | + |
| 130 | + EXPECT_EQ(x_grad_t->numel(), dim0 * dim1); |
| 131 | + EXPECT_EQ(out_v[0], 0); |
| 132 | + EXPECT_EQ(out_v[1], 2); |
| 133 | + EXPECT_EQ(out_v[2], 1); |
| 134 | + EXPECT_EQ(out_v[3], 3); |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +TEST(transpose, NPU_fp32) { |
| 139 | + f::Scope scope; |
| 140 | + p::NPUDeviceContext ctx(p::NPUPlace(0)); |
| 141 | + Compare<float>(&scope, ctx); |
| 142 | +} |
| 143 | + |
| 144 | +TEST(transpose_grad, NPU_fp32) { |
| 145 | + f::Scope scope; |
| 146 | + p::NPUDeviceContext ctx(p::NPUPlace(0)); |
| 147 | + CompareGrad<float>(&scope, ctx); |
| 148 | +} |
| 149 | + |
0 commit comments