|
| 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(unsqueeze); |
| 36 | +USE_OP_DEVICE_KERNEL(unsqueeze, 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 | + int dim0 = 5; |
| 45 | + int dim1 = 10; |
| 46 | + |
| 47 | + std::vector<T> init; |
| 48 | + for (int64_t i = 0; i < dim0 * dim1; ++i) { |
| 49 | + init.push_back(static_cast<T>(0.1)); |
| 50 | + } |
| 51 | + |
| 52 | + TensorFromVector(init, ctx, tensor_x); |
| 53 | + tensor_x->Resize({dim0, dim1}); |
| 54 | + |
| 55 | + ctx.Wait(); |
| 56 | + |
| 57 | + // run |
| 58 | + auto place = ctx.GetPlace(); |
| 59 | + auto out = scope->Var("Out"); |
| 60 | + auto tensor_out = out->GetMutable<f::LoDTensor>(); |
| 61 | + |
| 62 | + std::vector<int> axis; |
| 63 | + axis.push_back(1); |
| 64 | + f::AttributeMap attrs = {{"axes", axis}}; |
| 65 | + |
| 66 | + auto op = |
| 67 | + f::OpRegistry::CreateOp("unsqueeze", {{"X", {"X"}}}, |
| 68 | + {{"Out", {"Out"}}}, attrs); |
| 69 | + |
| 70 | + op->Run(*scope, place); |
| 71 | + ctx.Wait(); |
| 72 | + |
| 73 | + EXPECT_EQ((uint32_t)tensor_out->dims().size(), uint32_t(3)); |
| 74 | + EXPECT_EQ((uint32_t)tensor_out->dims()[0], uint32_t(5)); |
| 75 | + EXPECT_EQ((uint32_t)tensor_out->dims()[1], uint32_t(1)); |
| 76 | + EXPECT_EQ((uint32_t)tensor_out->dims()[2], uint32_t(10)); |
| 77 | + |
| 78 | + std::vector<T> out_vec; |
| 79 | + TensorToVector(*tensor_out, ctx, &out_vec); |
| 80 | + for (uint32_t i = 0; i < out_vec.size(); i++) { |
| 81 | + EXPECT_EQ(out_vec[i], static_cast<T>(0.1)); |
| 82 | + } |
| 83 | + |
| 84 | + ctx.Wait(); |
| 85 | +} |
| 86 | + |
| 87 | +TEST(unsqueeze, NPU_fp32) { |
| 88 | + f::Scope scope; |
| 89 | + p::NPUDeviceContext ctx(p::NPUPlace(0)); |
| 90 | + Compare<float>(&scope, ctx); |
| 91 | +} |
| 92 | + |
0 commit comments