custom_device_test.cc 9.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2022 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>
16

17 18 19 20 21
#include <string>

#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/framework/tensor_util.h"
#include "paddle/fluid/platform/device_context.h"
22 23
#include "paddle/phi/backends/custom/fake_cpu_device.h"
#include "paddle/phi/backends/device_manager.h"
24 25 26 27 28 29 30 31 32 33

void RegisterDevice() {
  CustomRuntimeParams runtime_params;
  runtime_params.size = sizeof(CustomRuntimeParams);
  auto device_interface = std::make_unique<C_DeviceInterface>();
  runtime_params.interface = device_interface.get();
  std::memset(runtime_params.interface, 0, sizeof(C_DeviceInterface));
  runtime_params.interface->size = sizeof(C_DeviceInterface);

  InitFakeCPUDevice(&runtime_params);
34
  phi::LoadCustomRuntimeLib(
35
      runtime_params, std::move(device_interface), "", nullptr);
36 37 38 39
}

void InitDevice() {
  RegisterDevice();
40
  EXPECT_GT(static_cast<int>(phi::DeviceManager::GetAllDeviceTypes().size()),
41 42
            0);
  auto place = paddle::platform::CustomPlace(DEVICE_TYPE, 0);
43
  auto device = phi::DeviceManager::GetDeviceWithPlace(place);
44 45 46
  EXPECT_NE(device, nullptr);

  std::vector<paddle::platform::Place> places;
47
  auto device_types = phi::DeviceManager::GetAllDeviceTypes();
48
  for (auto dev_type : device_types) {
49
    auto devices = phi::DeviceManager::GetDeviceList(dev_type);
50 51 52 53 54 55 56 57 58 59 60 61 62
    for (auto dev_id : devices) {
      places.push_back(
          paddle::platform::PlaceHelper::CreatePlace(dev_type, dev_id));
    }
  }
  EXPECT_GT(static_cast<int>(places.size()), 0);

  paddle::platform::DeviceContextPool::Init(places);
}

void TestDeviceInterface(const paddle::platform::Place& place) {
  std::cout << "TestDeviceInterface on " << place << std::endl;
  if (paddle::platform::is_custom_place(place)) {
63
    auto device = phi::DeviceManager::GetDeviceWithPlace(place);
64
    auto dev_type = paddle::platform::PlaceHelper::GetDeviceType(place);
65 66
    auto p1 =
        device->MemoryAllocate(phi::DeviceManager::GetMinChunkSize(place));
67 68
    EXPECT_NE(p1, nullptr);

69 70
    phi::DeviceManager::SetDevice(place);
    auto dev_id = phi::DeviceManager::GetDevice(dev_type);
71 72 73 74 75 76 77 78 79 80
    EXPECT_EQ(dev_id, place.GetDeviceId());
  }
}

void TestTensorMutableData(const paddle::platform::Place& place) {
  std::cout << "TestTensorInitialization on " << place << std::endl;
  paddle::framework::Tensor src_tensor;
  float* p1 = nullptr;
  float* p2 = nullptr;
  // initialization
81
  p1 = src_tensor.mutable_data<float>(phi::make_ddim({1, 2, 3}), place);
82 83 84 85
  auto p1_holder = src_tensor.Holder();
  EXPECT_NE(p1, nullptr);
  // set src_tensor a new dim with large size
  // momery is supposed to be re-allocated
86
  p2 = src_tensor.mutable_data<float>(phi::make_ddim({3, 1024}), place);
87 88 89 90 91
  auto p2_holder = src_tensor.Holder();
  EXPECT_NE(p2, nullptr);
  EXPECT_NE(p1_holder.get(), p2_holder.get());
  // set src_tensor a new dim with same size
  // momery block is supposed to be unchanged
92
  p1 = src_tensor.mutable_data<float>(phi::make_ddim({2, 2, 3}), place);
93 94 95
  EXPECT_EQ(p1, p2);
  // set src_tensor a new dim with smaller size
  // momery block is supposed to be unchanged
96
  p2 = src_tensor.mutable_data<float>(phi::make_ddim({2, 2}), place);
97 98 99 100 101 102 103
  EXPECT_EQ(p1, p2);
}

void TestTensorShareDataWith(const paddle::platform::Place& place) {
  std::cout << "TestTensorShareDataWith on " << place << std::endl;
  paddle::framework::Tensor src_tensor;
  paddle::framework::Tensor dst_tensor;
104
  src_tensor.mutable_data<int>(phi::make_ddim({2, 3, 4}), place);
105 106 107 108 109
  dst_tensor.ShareDataWith(src_tensor);
  ASSERT_EQ(src_tensor.data<int>(), dst_tensor.data<int>());
}

void TestTensorUtils(const paddle::platform::Place& place) {
110
  std::cout << "TestTensorUtils on " << place << std::endl;
111 112 113 114 115 116 117
  if (paddle::platform::is_custom_place(place) == false) {
    return;
  }
  paddle::framework::Tensor src_tensor;
  paddle::framework::Tensor gpu_tensor;
  paddle::framework::Tensor dst_tensor;

118
  int* src_ptr = src_tensor.mutable_data<int>(phi::make_ddim({3, 3}),
119
                                              paddle::platform::CPUPlace());
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 167 168 169

  int arr[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  memcpy(src_ptr, arr, 9 * sizeof(int));

  // CPU Tensor to GPU Tensor
  paddle::platform::CustomDeviceContext gpu_ctx(place);
  paddle::framework::TensorCopy(src_tensor, place, gpu_ctx, &gpu_tensor);
#if 0
  // GPU Tensor to CPU Tensor
  auto cpu_place = new paddle::platform::CPUPlace();
  paddle::framework::TensorCopy(gpu_tensor, *cpu_place, gpu_ctx, &dst_tensor);

  // Sync before Compare Tensors
  gpu_ctx.Wait();
  const int* dst_ptr = dst_tensor.data<int>();
  EXPECT_NE(src_ptr, dst_ptr);
  for (size_t i = 0; i < 9; ++i) {
    EXPECT_EQ(src_ptr[i], dst_ptr[i]);
  }

  // Copy the same tensor
  paddle::framework::TensorCopy(gpu_tensor, place, gpu_ctx, &gpu_tensor);
  gpu_ctx.Wait();
  const int* dst_ptr_tmp = dst_tensor.data<int>();
  EXPECT_NE(src_ptr, dst_ptr_tmp);
  for (size_t i = 0; i < 9; ++i) {
    EXPECT_EQ(src_ptr[i], dst_ptr_tmp[i]);
  }

  paddle::framework::Tensor slice_tensor = src_tensor.Slice(1, 2);

  // CPU Slice Tensor to GPU Tensor
  paddle::framework::TensorCopy(slice_tensor, place, gpu_ctx, &gpu_tensor);

  // GPU Tensor to CPU Tensor
  paddle::framework::TensorCopy(gpu_tensor, *cpu_place, gpu_ctx, &dst_tensor);

  // Sync before Compare Slice Tensors
  gpu_ctx.Wait();
  const int* slice_ptr = slice_tensor.data<int>();
  dst_ptr = dst_tensor.data<int>();
  EXPECT_NE(dst_ptr, slice_ptr);
  for (size_t i = 0; i < 3; ++i) {
    EXPECT_EQ(dst_ptr[i], slice_ptr[i]);
  }

  EXPECT_TRUE(dst_tensor.layout() == src_tensor.layout());
#endif
}

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
void TestCustomCCL(const paddle::platform::Place& place) {
  std::cout << "TestCustomCCL on " << place << std::endl;
  if (paddle::platform::is_custom_place(place) == false) {
    return;
  }
  std::string dev_type = place.GetDeviceType();
  phi::ccl::CCLComm comm;
  phi::stream::Stream stream(place, nullptr);
  phi::ccl::CCLRootId root_id;

  phi::DeviceManager::CCLDestroyComm(dev_type, nullptr);
  phi::DeviceManager::CCLGetUniqueId(dev_type, &root_id);
  phi::DeviceManager::CCLCommInitRank(dev_type, 0, &root_id, 0, nullptr);
  phi::DeviceManager::CCLBroadcast(dev_type,
                                   nullptr,
                                   0,
                                   phi::ccl::CCLDataType::CCL_DATA_TYPE_FP32,
                                   0,
                                   comm,
                                   stream);
  phi::DeviceManager::CCLAllReduce(dev_type,
                                   nullptr,
                                   nullptr,
                                   0,
                                   phi::ccl::CCLDataType::CCL_DATA_TYPE_FP32,
                                   phi::ccl::CCLReduceOp::SUM,
                                   comm,
                                   stream);
  phi::DeviceManager::CCLReduce(dev_type,
                                nullptr,
                                nullptr,
                                0,
                                phi::ccl::CCLDataType::CCL_DATA_TYPE_FP32,
                                phi::ccl::CCLReduceOp::SUM,
                                comm,
                                stream);
  phi::DeviceManager::CCLAllGather(dev_type,
                                   nullptr,
                                   nullptr,
                                   0,
                                   phi::ccl::CCLDataType::CCL_DATA_TYPE_FP32,
                                   comm,
                                   stream);
  phi::DeviceManager::CCLReduceScatter(
      dev_type,
      nullptr,
      nullptr,
      0,
      phi::ccl::CCLDataType::CCL_DATA_TYPE_FP32,
      phi::ccl::CCLReduceOp::SUM,
      comm,
      stream);
  phi::DeviceManager::CCLGroupStart(dev_type);
  phi::DeviceManager::CCLGroupEnd(dev_type);
  phi::DeviceManager::CCLSend(dev_type,
                              nullptr,
                              0,
                              phi::ccl::CCLDataType::CCL_DATA_TYPE_FP32,
                              0,
                              comm,
                              stream);
  phi::DeviceManager::CCLRecv(dev_type,
                              nullptr,
                              0,
                              phi::ccl::CCLDataType::CCL_DATA_TYPE_FP32,
                              0,
                              comm,
                              stream);
}

240 241
TEST(CustomDevice, Tensor) {
  InitDevice();
242
  auto dev_types = phi::DeviceManager::GetAllDeviceTypes();
243 244
  for (const auto& dev_type : dev_types) {
    std::cout << "Test on " << dev_type << std::endl;
245
    EXPECT_GT(static_cast<int>(phi::DeviceManager::GetDeviceCount(dev_type)),
246 247 248 249 250 251 252
              0);
    auto place = paddle::platform::PlaceHelper::CreatePlace(dev_type);

    TestDeviceInterface(place);
    TestTensorMutableData(place);
    TestTensorShareDataWith(place);
    TestTensorUtils(place);
253
    TestCustomCCL(place);
254 255 256 257 258 259 260
  }
}

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}