|
| 1 | +// Copyright (c) 2024 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 | +#include "gtest/gtest.h" |
| 16 | +#include "paddle/fluid/framework/op_registry.h" |
| 17 | +#include "paddle/fluid/platform/float16.h" |
| 18 | +#include "paddle/phi/core/kernel_registry.h" |
| 19 | + |
| 20 | +template <typename Place, typename T> |
| 21 | +int SaveLoadOpTest(Place place, int dim_1, int dim_2) { |
| 22 | + // use cpu place for ground truth |
| 23 | + paddle::platform::CPUPlace cpu_place; |
| 24 | + std::vector<T> ground_truth_cpu(dim_1 * dim_2); |
| 25 | + for (int i = 0; i < dim_1 * dim_2; i++) { |
| 26 | + ground_truth_cpu[i] = static_cast<T>(i); |
| 27 | + } |
| 28 | + |
| 29 | + // scope, var, tensor and lod |
| 30 | + paddle::framework::Scope scope; |
| 31 | + auto var = scope.Var("test_var"); |
| 32 | + auto tensor = var->GetMutable<phi::DenseTensor>(); |
| 33 | + tensor->Resize({dim_1, dim_2}); |
| 34 | + paddle::framework::LoD expect_lod; |
| 35 | + expect_lod.resize(1); |
| 36 | + for (int i = 0; i < dim_1; i++) { |
| 37 | + expect_lod[0].push_back(i); |
| 38 | + } |
| 39 | + tensor->set_lod(expect_lod); |
| 40 | + T* src_mutable = tensor->mutable_data<T>(place); |
| 41 | + // copy cpu data to tensor |
| 42 | + paddle::memory::Copy(place, |
| 43 | + src_mutable, |
| 44 | + cpu_place, |
| 45 | + ground_truth_cpu.data(), |
| 46 | + sizeof(T) * ground_truth_cpu.size()); |
| 47 | + |
| 48 | + // run save op |
| 49 | + paddle::framework::AttributeMap attrs; |
| 50 | + attrs.insert({"file_path", std::string("tensor.save")}); |
| 51 | + auto save_op = paddle::framework::OpRegistry::CreateOp( |
| 52 | + "save", {{"X", {"test_var"}}}, {}, attrs); |
| 53 | + save_op->Run(scope, place); |
| 54 | + |
| 55 | + // result var and tensor |
| 56 | + auto load_var = scope.Var("out_var"); |
| 57 | + auto target = load_var->GetMutable<phi::DenseTensor>(); |
| 58 | + |
| 59 | + // run load op |
| 60 | + auto load_op = paddle::framework::OpRegistry::CreateOp( |
| 61 | + "load", {}, {{"Out", {"out_var"}}}, attrs); |
| 62 | + load_op->Run(scope, place); |
| 63 | + |
| 64 | + // copy result tensor data to cpu |
| 65 | + T* actual = target->data<T>(); |
| 66 | + std::vector<T> actual_cpu(dim_1 * dim_2); |
| 67 | + paddle::memory::Copy(cpu_place, |
| 68 | + actual_cpu.data(), |
| 69 | + place, |
| 70 | + actual, |
| 71 | + sizeof(T) * ground_truth_cpu.size()); |
| 72 | + |
| 73 | + // check result: data |
| 74 | + for (int i = 0; i < dim_1 * dim_2; i++) { |
| 75 | + if (actual_cpu[i] != ground_truth_cpu[i]) { |
| 76 | + return 1; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // check result: lod |
| 81 | + auto& actual_lod = target->lod(); |
| 82 | + if (expect_lod.size() != actual_lod.size()) { |
| 83 | + return 1; |
| 84 | + } |
| 85 | + for (size_t i = 0; i < expect_lod.size(); ++i) { // NOLINT |
| 86 | + for (size_t j = 0; j < expect_lod[i].size(); ++j) { |
| 87 | + if (expect_lod[i][j] != actual_lod[i][j]) { |
| 88 | + return 1; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +TEST(SaveLoadOp, XPU) { |
| 96 | + paddle::platform::XPUPlace xpu_place(0); |
| 97 | + paddle::platform::CPUPlace cpu_place; |
| 98 | + int r = 0; |
| 99 | + |
| 100 | + r = SaveLoadOpTest<paddle::platform::XPUPlace, float>(xpu_place, 3, 10); |
| 101 | + EXPECT_EQ(r, 0); |
| 102 | + r = SaveLoadOpTest<paddle::platform::CPUPlace, float>(cpu_place, 3, 10); |
| 103 | + EXPECT_EQ(r, 0); |
| 104 | + |
| 105 | + r = SaveLoadOpTest<paddle::platform::XPUPlace, int>(xpu_place, 2, 128); |
| 106 | + EXPECT_EQ(r, 0); |
| 107 | + r = SaveLoadOpTest<paddle::platform::CPUPlace, int>(cpu_place, 2, 128); |
| 108 | + EXPECT_EQ(r, 0); |
| 109 | + |
| 110 | + r = SaveLoadOpTest<paddle::platform::XPUPlace, paddle::platform::float16>( |
| 111 | + xpu_place, 2, 128); |
| 112 | + EXPECT_EQ(r, 0); |
| 113 | + r = SaveLoadOpTest<paddle::platform::CPUPlace, paddle::platform::float16>( |
| 114 | + cpu_place, 2, 128); |
| 115 | + EXPECT_EQ(r, 0); |
| 116 | + |
| 117 | + r = SaveLoadOpTest<paddle::platform::XPUPlace, paddle::platform::bfloat16>( |
| 118 | + xpu_place, 4, 32); |
| 119 | + EXPECT_EQ(r, 0); |
| 120 | + r = SaveLoadOpTest<paddle::platform::CPUPlace, paddle::platform::bfloat16>( |
| 121 | + cpu_place, 4, 32); |
| 122 | + EXPECT_EQ(r, 0); |
| 123 | +} |
0 commit comments