From 6cff41764e1ccddcd1f4bdb3b5f8ba42c0bc2fbe Mon Sep 17 00:00:00 2001 From: PaddlePaddle-Gardener Date: Wed, 12 Jan 2022 14:44:23 +0800 Subject: [PATCH] mirgate_38844 --- paddle/pten/api/lib/utils/CMakeLists.txt | 2 + paddle/pten/api/lib/utils/place_utils.cc | 0 paddle/pten/api/lib/utils/place_utils.h | 0 paddle/pten/common/CMakeLists.txt | 1 + paddle/pten/common/device.cc | 65 ------------- paddle/pten/common/device.h | 0 paddle/pten/common/place.cc | 69 +++++++++++++ paddle/pten/common/place.h | 112 ++++++++++++++++++++++ paddle/pten/tests/api/CMakeLists.txt | 25 +++++ paddle/pten/tests/api/test_place_utils.cc | 0 paddle/pten/tests/common/CMakeLists.txt | 4 + paddle/pten/tests/common/test_place.cc | 53 ++++++++++ 12 files changed, 266 insertions(+), 65 deletions(-) create mode 100644 paddle/pten/api/lib/utils/CMakeLists.txt delete mode 100644 paddle/pten/api/lib/utils/place_utils.cc delete mode 100644 paddle/pten/api/lib/utils/place_utils.h delete mode 100644 paddle/pten/common/device.cc delete mode 100644 paddle/pten/common/device.h delete mode 100644 paddle/pten/tests/api/test_place_utils.cc create mode 100644 paddle/pten/tests/common/test_place.cc diff --git a/paddle/pten/api/lib/utils/CMakeLists.txt b/paddle/pten/api/lib/utils/CMakeLists.txt new file mode 100644 index 0000000000..4a44ad7758 --- /dev/null +++ b/paddle/pten/api/lib/utils/CMakeLists.txt @@ -0,0 +1,2 @@ +cc_library(pten_api_utils SRCS allocator.cc storage.cc tensor_utils.cc DEPS +tensor_base convert_utils dense_tensor lod_tensor selected_rows place var_type_traits) diff --git a/paddle/pten/api/lib/utils/place_utils.cc b/paddle/pten/api/lib/utils/place_utils.cc deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/paddle/pten/api/lib/utils/place_utils.h b/paddle/pten/api/lib/utils/place_utils.h deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/paddle/pten/common/CMakeLists.txt b/paddle/pten/common/CMakeLists.txt index e69de29bb2..feaf0e12bd 100644 --- a/paddle/pten/common/CMakeLists.txt +++ b/paddle/pten/common/CMakeLists.txt @@ -0,0 +1 @@ +cc_library(pten_place SRCS place.cc) diff --git a/paddle/pten/common/device.cc b/paddle/pten/common/device.cc deleted file mode 100644 index 55130067ae..0000000000 --- a/paddle/pten/common/device.cc +++ /dev/null @@ -1,65 +0,0 @@ -/* Copyright (c) 2021 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 "paddle/pten/common/device.h" -#include "paddle/fluid/platform/enforce.h" -#include "paddle/pten/api/ext/exception.h" - -namespace paddle { -namespace experimental { - -const char* DeviceTypeStr(DeviceType type) { - switch (type) { - case DeviceType::kUndef: - return "kUndef"; - case DeviceType::kHost: - return "kHost"; - case DeviceType::kXpu: - return "kXpu"; - case DeviceType::kCuda: - return "kCuda"; - case DeviceType::kHip: - return "kHip"; - case DeviceType::kNpu: - return "kNpu"; - default: - PD_THROW("Invalid pten device type."); - } - return {}; -} - -Device::Device(DeviceType type, int8_t id) : type_(type), id_(id) { - PADDLE_ENFORCE_GE( - id, - 0, - platform::errors::InvalidArgument( - "The device id needs to start from zero, but you passed in %d.", id)); -} - -Device::Device(DeviceType type) : type_(type), id_(0) { - PADDLE_ENFORCE_EQ( - type, - DeviceType::kHost, - platform::errors::InvalidArgument( - "The device id needs to start from zero, but you passed in %s.", - DeviceTypeStr(type))); -} - -std::string Device::DebugString() const { - std::string str{"DeviceType:"}; - return str + DeviceTypeStr(type_) + ", id: " + std::to_string(id_); -} - -} // namespace experimental -} // namespace paddle diff --git a/paddle/pten/common/device.h b/paddle/pten/common/device.h deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/paddle/pten/common/place.cc b/paddle/pten/common/place.cc index e69de29bb2..2d33bb508a 100644 --- a/paddle/pten/common/place.cc +++ b/paddle/pten/common/place.cc @@ -0,0 +1,69 @@ +/* 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 "paddle/pten/common/place.h" + +#include +#include + +#include "paddle/pten/api/ext/exception.h" + +namespace pten { + +const char *AllocationTypeStr(AllocationType type) { + switch (type) { + case AllocationType::UNDEF: + return "undef"; + case AllocationType::CPU: + return "cpu"; + case AllocationType::GPU: + return "gpu"; + case AllocationType::GPUPINNED: + return "gpu pinned"; + case AllocationType::XPU: + return "xpu"; + case AllocationType::NPU: + return "npu"; + case AllocationType::NPUPINNED: + return "npu pinned"; + case AllocationType::IPU: + return "ipu"; + case AllocationType::MLU: + return "mlu"; + default: + PD_THROW("Invalid pten device type."); + return {}; + } +} + +std::string Place::DebugString() const { + std::ostringstream os; + os << "Place("; + os << AllocationTypeStr(alloc_type_); + if (alloc_type_ == AllocationType::GPUPINNED || + alloc_type_ == AllocationType::NPUPINNED || + alloc_type_ == AllocationType::CPU) { + os << ")"; + } else { + os << ":" << std::to_string(device) << ")"; + } + return os.str(); +} + +std::ostream &operator<<(std::ostream &os, const Place &p) { + os << p.DebugString(); + return os; +} + +} // namespace pten diff --git a/paddle/pten/common/place.h b/paddle/pten/common/place.h index e69de29bb2..24d2430520 100644 --- a/paddle/pten/common/place.h +++ b/paddle/pten/common/place.h @@ -0,0 +1,112 @@ +/* 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. */ + +#pragma once + +#include + +namespace pten { + +enum class AllocationType : int8_t { + UNDEF = 0, + CPU = 1, + GPU = 2, + GPUPINNED = 3, + XPU = 4, + NPU = 5, + NPUPINNED = 6, + IPU = 7, + MLU = 8, +}; + +const char *AllocationTypeStr(AllocationType type); + +/// \brief The place is used to specify where the data is stored. +class Place { + public: + Place() : device(0), alloc_type_(AllocationType::UNDEF) {} + + explicit Place(AllocationType type, int8_t id) + : device(id), alloc_type_(type) {} + + explicit Place(AllocationType type) : device(0), alloc_type_(type) {} + + void Reset(AllocationType type, int8_t device_id = 0) noexcept { + alloc_type_ = type; + device = device_id; + } + + AllocationType GetType() const { return alloc_type_; } + + int8_t GetDeviceId() const { return device; } + + std::string DebugString() const; + + public: + // TODO(wilber): Just because of backward compatibility, it needs to be + // changed to private in the future. + int8_t device; + + private: + AllocationType alloc_type_; +}; + +class CPUPlace : public Place { + public: + CPUPlace() : Place(AllocationType::CPU, 0) {} +}; + +class GPUPlace : public Place { + public: + GPUPlace() : Place(AllocationType::GPU, 0) {} + explicit GPUPlace(int device_id) : Place(AllocationType::GPU, device_id) {} +}; + +class GPUPinnedPlace : public Place { + public: + GPUPinnedPlace() : Place(AllocationType::GPUPINNED) {} +}; + +class XPUPlace : public Place { + public: + XPUPlace() : Place(AllocationType::XPU, 0) {} + explicit XPUPlace(int device_id) : Place(AllocationType::XPU, device_id) {} +}; + +class NPUPlace : public Place { + public: + NPUPlace() : Place(AllocationType::NPU, 0) {} + explicit NPUPlace(int device_id) : Place(AllocationType::XPU, device_id) {} +}; + +class NPUPinnedPlace : public Place { + public: + NPUPinnedPlace() : Place(AllocationType::NPUPINNED) {} +}; + +class IPUPlace : public Place { + public: + IPUPlace() : Place(AllocationType::XPU, 0) {} + explicit IPUPlace(int device_id) : Place(AllocationType::XPU, device_id) {} +}; + +class MLUPlace : public Place { + public: + MLUPlace() : Place(AllocationType::MLU, 0) {} + explicit MLUPlace(int device_id) : Place(AllocationType::MLU, device_id) {} +}; + +std::ostream &operator<<(std::ostream &, const Place &); + +} // namespace pten diff --git a/paddle/pten/tests/api/CMakeLists.txt b/paddle/pten/tests/api/CMakeLists.txt index e69de29bb2..ffbc551843 100644 --- a/paddle/pten/tests/api/CMakeLists.txt +++ b/paddle/pten/tests/api/CMakeLists.txt @@ -0,0 +1,25 @@ +if(WITH_ROCM) + hip_test(test_pten_tensor SRCS test_pten_tensor.cc DEPS pten_tensor pten_function_api utils_api glog) +else() + cc_test(test_pten_tensor SRCS test_pten_tensor.cc DEPS pten_tensor pten_function_api utils_api glog) +endif() + +cc_test(test_pten_exception SRCS test_pten_exception.cc DEPS gtest) +cc_test(test_framework_storage SRCS test_storage.cc DEPS pten_api_utils) +cc_test(test_framework_tensor_utils SRCS test_tensor_utils.cc DEPS pten_api_utils) + +cc_test(test_mean_api SRCS test_mean_api.cc DEPS pten_tensor pten_api pten_api_utils) +cc_test(test_dot_api SRCS test_dot_api.cc DEPS pten_tensor pten_api pten_api_utils) +cc_test(test_matmul_api SRCS test_matmul_api.cc DEPS pten_tensor pten_api pten_api_utils) +cc_test(test_empty_api SRCS test_empty_api.cc DEPS pten_tensor pten_api pten_api_utils) +cc_test(test_fill_api SRCS test_fill_api.cc DEPS pten_tensor pten_api pten_api_utils) +cc_test(test_flatten_api SRCS test_flatten_api.cc DEPS pten_tensor pten_api pten_api_utils) +cc_test(test_elementwise_api SRCS test_elementwise_api.cc DEPS pten_tensor pten_api pten_api_utils) +cc_test(test_cast_api SRCS test_cast_api.cc DEPS pten_tensor pten_api pten_api_utils) +cc_test(test_reshape_api SRCS test_reshape_api.cc DEPS pten_tensor pten_api pten_api_utils) +cc_test(test_to_api SRCS test_to_api.cc DEPS pten_tensor pten_api pten_api_utils) +cc_test(test_slice_api SRCS test_slice_api.cc DEPS pten_tensor pten_api pten_api_utils) +cc_test(test_sum_api SRCS test_sum_api.cc DEPS pten_tensor pten_api pten_api_utils) +cc_test(test_scale_api SRCS test_scale_api.cc DEPS pten_tensor pten_api pten_api_utils) +cc_test(test_scale_benchmark SRCS test_scale_benchmark.cc DEPS pten_tensor pten_api pten_api_utils) +cc_test(test_conj_api SRCS test_conj_api.cc DEPS pten_tensor pten_api pten_api_utils) diff --git a/paddle/pten/tests/api/test_place_utils.cc b/paddle/pten/tests/api/test_place_utils.cc deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/paddle/pten/tests/common/CMakeLists.txt b/paddle/pten/tests/common/CMakeLists.txt index e69de29bb2..f54b37cb97 100644 --- a/paddle/pten/tests/common/CMakeLists.txt +++ b/paddle/pten/tests/common/CMakeLists.txt @@ -0,0 +1,4 @@ +cc_test(pten_test_backend SRCS test_backend.cc DEPS gtest) +cc_test(pten_test_data_layout SRCS test_data_layout.cc DEPS gtest) +cc_test(pten_test_data_type SRCS test_data_type.cc DEPS gtest) +cc_test(pten_test_place SRCS test_place.cc DEPS pten_place) diff --git a/paddle/pten/tests/common/test_place.cc b/paddle/pten/tests/common/test_place.cc new file mode 100644 index 0000000000..0bbd8f1d42 --- /dev/null +++ b/paddle/pten/tests/common/test_place.cc @@ -0,0 +1,53 @@ +/* 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 "paddle/pten/common/place.h" + +#include "gtest/gtest.h" + +namespace pten { +namespace tests { + +TEST(PtenPlace, place) { + pten::Place place; + EXPECT_EQ(place.GetType(), pten::AllocationType::UNDEF); + + place.Reset(pten::AllocationType::GPU, 1); + EXPECT_EQ(place.GetType(), pten::AllocationType::GPU); + EXPECT_EQ(place.GetDeviceId(), 1); +} + +TEST(Place, cpu_place) { + pten::CPUPlace place; + EXPECT_EQ(place.GetType(), pten::AllocationType::CPU); + std::cout << "cpu place repr: " << place << std::endl; +} + +TEST(Place, gpu_place) { + pten::GPUPlace place; + EXPECT_EQ(place.GetType(), pten::AllocationType::GPU); + EXPECT_EQ(place.GetDeviceId(), 0); + + pten::GPUPlace place1(2); + EXPECT_EQ(place1.GetType(), pten::AllocationType::GPU); + EXPECT_EQ(place1.GetDeviceId(), 2); + std::cout << "gpu place repr: " << place1 << std::endl; + + pten::GPUPinnedPlace place2; + EXPECT_EQ(place2.GetType(), pten::AllocationType::GPUPINNED); + std::cout << "gpu pinned place repr: " << place2 << std::endl; +} + +} // namespace tests +} // namespace pten -- Gitee