From e3640af3476f9149eebbb70a8d2bc247765d3401 Mon Sep 17 00:00:00 2001 From: jiweibo Date: Mon, 10 Jan 2022 11:20:42 +0000 Subject: [PATCH 1/4] add pten::Place data structure. --- paddle/pten/CMakeLists.txt | 3 - paddle/pten/api/lib/utils/CMakeLists.txt | 2 +- paddle/pten/api/lib/utils/place_utils.cc | 62 ------------- paddle/pten/api/lib/utils/place_utils.h | 28 ------ paddle/pten/common/CMakeLists.txt | 1 - paddle/pten/common/device.cc | 65 ------------- paddle/pten/common/device.h | 70 -------------- paddle/pten/common/place.cc | 26 ------ paddle/pten/common/place.h | 67 -------------- paddle/pten/core/CMakeLists.txt | 1 + paddle/pten/core/place.cc | 60 ++++++++++++ paddle/pten/core/place.h | 112 +++++++++++++++++++++++ paddle/pten/tests/core/CMakeLists.txt | 1 + paddle/pten/tests/core/test_place.cc | 51 +++++++++++ 14 files changed, 226 insertions(+), 323 deletions(-) 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/CMakeLists.txt delete mode 100644 paddle/pten/common/device.cc delete mode 100644 paddle/pten/common/device.h delete mode 100644 paddle/pten/common/place.cc delete mode 100644 paddle/pten/common/place.h create mode 100644 paddle/pten/core/place.cc create mode 100644 paddle/pten/core/place.h create mode 100644 paddle/pten/tests/core/test_place.cc diff --git a/paddle/pten/CMakeLists.txt b/paddle/pten/CMakeLists.txt index 05b321c50c1c4..97875b8632adf 100644 --- a/paddle/pten/CMakeLists.txt +++ b/paddle/pten/CMakeLists.txt @@ -3,9 +3,6 @@ # float16.h/complex.h/bfloat16.h into pten include_directories(${PADDLE_SOURCE_DIR}/paddle/fluid/platform) -# paddle experimental common components -add_subdirectory(common) - # pten (low level) api headers: include # pten (high level) api add_subdirectory(api) diff --git a/paddle/pten/api/lib/utils/CMakeLists.txt b/paddle/pten/api/lib/utils/CMakeLists.txt index 06178dad43767..0650c628dc7f3 100644 --- a/paddle/pten/api/lib/utils/CMakeLists.txt +++ b/paddle/pten/api/lib/utils/CMakeLists.txt @@ -1,2 +1,2 @@ -cc_library(pten_api_utils SRCS allocator.cc storage.cc tensor_utils.cc place_utils.cc DEPS +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 pten_common) 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 af4f84b1ad836..0000000000000 --- a/paddle/pten/api/lib/utils/place_utils.cc +++ /dev/null @@ -1,62 +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/api/lib/utils/place_utils.h" -#include "paddle/pten/api/ext/exception.h" - -namespace paddle { -namespace experimental { - -Place ConvertToPtenPlace(const platform::Place& src) { - Place place; - if (platform::is_cpu_place(src)) { - place.Reset(Device(DeviceType::kHost, 0)); - } else if (platform::is_gpu_place(src)) { - place.Reset( - Device(DeviceType::kCuda, - BOOST_GET_CONST(platform::CUDAPlace, src).GetDeviceId())); - } else if (platform::is_cuda_pinned_place(src)) { - place.Reset(Device(DeviceType::kCuda, 0), true); - } else if (platform::is_xpu_place(src)) { - place.Reset(Device(DeviceType::kXpu, - BOOST_GET_CONST(platform::XPUPlace, src).GetDeviceId())); - } else { - PD_THROW("Invalid platform place type."); - } - return place; -} - -platform::Place ConvertToPlatformPlace(const Place& src) { - switch (src.device().type()) { - case DeviceType::kHost: { - return platform::CPUPlace(); - } - case DeviceType::kCuda: { - if (src.is_pinned()) { - return platform::CUDAPinnedPlace(); - } else { - return platform::CUDAPlace(src.device().id()); - } - } - case DeviceType::kXpu: { - return platform::XPUPlace(src.device().id()); - } - default: - PD_THROW("Invalid pten place type."); - } - return {}; -} - -} // namespace experimental -} // namespace paddle 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 9ac10158040b2..0000000000000 --- a/paddle/pten/api/lib/utils/place_utils.h +++ /dev/null @@ -1,28 +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. */ - -#pragma once - -#include "paddle/fluid/platform/place.h" -#include "paddle/pten/common/place.h" - -namespace paddle { -namespace experimental { - -Place ConvertToPtenPlace(const platform::Place& src); - -platform::Place ConvertToPlatformPlace(const Place& src); - -} // namespace experimental -} // namespace paddle diff --git a/paddle/pten/common/CMakeLists.txt b/paddle/pten/common/CMakeLists.txt deleted file mode 100644 index c4083d7f0d756..0000000000000 --- a/paddle/pten/common/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -cc_library(pten_common SRCS device.cc place.cc DEPS enforce) diff --git a/paddle/pten/common/device.cc b/paddle/pten/common/device.cc deleted file mode 100644 index 55130067ae200..0000000000000 --- 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 eddb71bce16da..0000000000000 --- a/paddle/pten/common/device.h +++ /dev/null @@ -1,70 +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. */ - -#pragma once - -#include -#include - -namespace paddle { -namespace experimental { - -enum class DeviceType : int8_t { - kUndef = 0, - kHost = 1, - kXpu = 2, - kCuda = 3, - kHip = 4, - kNpu = 5, -}; - -const char* DeviceTypeStr(DeviceType type); - -/// \brief The device is used to store hardware information. It has not yet -/// stored information related to the math acceleration library. -struct Device final { - public: - Device() = default; - - Device(DeviceType type, int8_t id); - - Device(DeviceType type); - - DeviceType type() const noexcept { return type_; } - - /// \brief Returns the index of the device. Here, -1 is used to indicate an - /// invalid value, and 0 to indicate a default value. - /// \return The index of the device. - int8_t id() const noexcept { return id_; } - - void set_type(DeviceType type) noexcept { type_ = type; } - - void set_id(int8_t id) noexcept { id_ = id; } - - std::string DebugString() const; - - private: - friend bool operator==(const Device&, const Device&) noexcept; - - private: - DeviceType type_{DeviceType::kUndef}; - int8_t id_{-1}; -}; - -inline bool operator==(const Device& lhs, const Device& rhs) noexcept { - return (lhs.type_ == rhs.type_) && (lhs.id_ == rhs.id_); -} - -} // namespace experimental -} // namespace paddle diff --git a/paddle/pten/common/place.cc b/paddle/pten/common/place.cc deleted file mode 100644 index ba34c5d0f9222..0000000000000 --- a/paddle/pten/common/place.cc +++ /dev/null @@ -1,26 +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/place.h" -#include "paddle/fluid/platform/enforce.h" - -namespace paddle { -namespace experimental { - -std::string Place::DebugString() const { - return device_.DebugString() + ", is_pinned: " + std::to_string(is_pinned_); -} - -} // namespace experimental -} // namespace paddle diff --git a/paddle/pten/common/place.h b/paddle/pten/common/place.h deleted file mode 100644 index fdc948734934b..0000000000000 --- a/paddle/pten/common/place.h +++ /dev/null @@ -1,67 +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. */ - -#pragma once - -#include - -#include "paddle/pten/common/device.h" - -namespace paddle { -namespace experimental { - -/// \brief The place is used to specify where the data is stored. -class Place final { - public: - Place() = default; - - explicit Place(const Device& device) : device_(device) {} - - Place(DeviceType type, int8_t id) : device_(type, id) {} - - Place(DeviceType type) : device_(type) {} - - Place(const Device& device, bool is_pinned) noexcept : device_(device), - is_pinned_(is_pinned) { - } - - const Device& device() const noexcept { return device_; } - - /// \brief Returns whether the memory is a locked page. The page lock - /// memory is actually located in the host memory, but it can only be - /// used by certain devices and can be directly transferred by DMA. - /// \return Whether the memory is a locked page. - bool is_pinned() const noexcept { return is_pinned_; } - - void Reset(const Device& device, bool is_pinned = false) noexcept { - device_ = device; - is_pinned_ = is_pinned; - } - - std::string DebugString() const; - - private: - friend bool operator==(const Place&, const Place&) noexcept; - - private: - Device device_; - bool is_pinned_{false}; -}; - -inline bool operator==(const Place& lhs, const Place& rhs) noexcept { - return (lhs.device_ == rhs.device_) && (lhs.is_pinned_ == rhs.is_pinned_); -} - -} // namespace experimental -} // namespace paddle diff --git a/paddle/pten/core/CMakeLists.txt b/paddle/pten/core/CMakeLists.txt index 87c3612e35424..6c1487147992c 100644 --- a/paddle/pten/core/CMakeLists.txt +++ b/paddle/pten/core/CMakeLists.txt @@ -6,6 +6,7 @@ else() cc_library(convert_utils SRCS convert_utils.cc DEPS data_type place) endif() +cc_library(pten_place SRCS place.cc) cc_library(kernel_factory SRCS kernel_factory.cc DEPS enforce convert_utils) cc_library(kernel_context SRCS kernel_context.cc DEPS enforce pten_context) cc_library(tensor_base SRCS tensor_base.cc allocator.cc storage.cc DEPS enforce) diff --git a/paddle/pten/core/place.cc b/paddle/pten/core/place.cc new file mode 100644 index 0000000000000..29c0fbe837b0b --- /dev/null +++ b/paddle/pten/core/place.cc @@ -0,0 +1,60 @@ +/* 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/core/place.h" + +#include + +#include "paddle/pten/api/ext/exception.h" + +namespace pten { + +const char *AllocationTypeStr(AllocationType type) { + switch (type) { + case AllocationType::kUndef: + return "kUndef"; + case AllocationType::kCpu: + return "kCpu"; + case AllocationType::kGpu: + return "kGpu"; + case AllocationType::kGpuPinned: + return "kGpuPinned"; + case AllocationType::kXpu: + return "kXpu"; + case AllocationType::kNpu: + return "kNpu"; + case AllocationType::kNpuPinned: + return "kNpuPinned"; + case AllocationType::kIpu: + return "kIpu"; + case AllocationType::kMlu: + return "kMlu"; + default: + PD_THROW("Invalid pten device type."); + return {}; + } +} + +std::string Place::DebugString() const { + std::string str{"Place:{type = "}; + return str + AllocationTypeStr(alloc_type_) + ", id = " + + std::to_string(device) + "}"; +} + +std::ostream &operator<<(std::ostream &os, const Place &p) { + os << p.DebugString(); + return os; +} + +} // namespace pten diff --git a/paddle/pten/core/place.h b/paddle/pten/core/place.h new file mode 100644 index 0000000000000..92dab16a5ae62 --- /dev/null +++ b/paddle/pten/core/place.h @@ -0,0 +1,112 @@ +/* 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. */ + +#pragma once + +#include + +namespace pten { + +enum class AllocationType : int8_t { + kUndef = 0, + kCpu = 1, + kGpu = 2, + kGpuPinned = 3, + kXpu = 4, + kNpu = 5, + kNpuPinned = 6, + kIpu = 7, + kMlu = 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::kUndef) {} + + 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::kCpu, 0) {} +}; + +class GPUPlace : public Place { + public: + GPUPlace() : Place(AllocationType::kGpu, 0) {} + explicit GPUPlace(int device_id) : Place(AllocationType::kGpu, device_id) {} +}; + +class GPUPinnedPlace : public Place { + public: + GPUPinnedPlace() : Place(AllocationType::kGpuPinned) {} +}; + +class XPUPlace : public Place { + public: + XPUPlace() : Place(AllocationType::kXpu, 0) {} + explicit XPUPlace(int device_id) : Place(AllocationType::kXpu, device_id) {} +}; + +class NPUPlace : public Place { + public: + NPUPlace() : Place(AllocationType::kNpu, 0) {} + explicit NPUPlace(int device_id) : Place(AllocationType::kXpu, device_id) {} +}; + +class NPUPinnedPlace : public Place { + public: + NPUPinnedPlace() : Place(AllocationType::kNpuPinned) {} +}; + +class IPUPlace : public Place { + public: + IPUPlace() : Place(AllocationType::kXpu, 0) {} + explicit IPUPlace(int device_id) : Place(AllocationType::kXpu, device_id) {} +}; + +class MLUPlace : public Place { + public: + MLUPlace() : Place(AllocationType::kMlu, 0) {} + explicit MLUPlace(int device_id) : Place(AllocationType::kMlu, device_id) {} +}; + +std::ostream &operator<<(std::ostream &, const Place &); + +} // namespace pten diff --git a/paddle/pten/tests/core/CMakeLists.txt b/paddle/pten/tests/core/CMakeLists.txt index 9a5cfecc2917b..ec4fc698090c9 100644 --- a/paddle/pten/tests/core/CMakeLists.txt +++ b/paddle/pten/tests/core/CMakeLists.txt @@ -5,3 +5,4 @@ cc_test(test_intrusive_ptr SRCS test_intrusive_ptr.cc) cc_test(test_type_info SRCS test_type_info.cc) cc_test(test_convert_utils SRCS test_convert_utils.cc DEPS convert_utils) cc_test(test_kernel_factory SRCS test_kernel_factory.cc DEPS kernel_factory) +cc_test(test_pten_place SRCS test_place.cc DEPS pten_place) diff --git a/paddle/pten/tests/core/test_place.cc b/paddle/pten/tests/core/test_place.cc new file mode 100644 index 0000000000000..99788a97e8a99 --- /dev/null +++ b/paddle/pten/tests/core/test_place.cc @@ -0,0 +1,51 @@ +/* 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/core/place.h" + +#include "gtest/gtest.h" + +namespace pten { +namespace tests { + +TEST(PtenPlace, place) { + pten::Place place; + EXPECT_EQ(place.GetType(), pten::AllocationType::kUndef); + + place.Reset(pten::AllocationType::kGpu, 1); + EXPECT_EQ(place.GetType(), pten::AllocationType::kGpu); + EXPECT_EQ(place.GetDeviceId(), 1); +} + +TEST(Place, cpu_place) { + pten::CPUPlace place; + EXPECT_EQ(place.GetType(), pten::AllocationType::kCpu); +} + +TEST(Place, gpu_place) { + pten::GPUPlace place; + EXPECT_EQ(place.GetType(), pten::AllocationType::kGpu); + EXPECT_EQ(place.GetDeviceId(), 0); + + pten::GPUPlace place1(2); + EXPECT_EQ(place1.GetType(), pten::AllocationType::kGpu); + EXPECT_EQ(place1.GetDeviceId(), 2); + std::cout << "place string repr: " << place1 << std::endl; + + pten::GPUPinnedPlace place2; + EXPECT_EQ(place2.GetType(), pten::AllocationType::kGpuPinned); +} + +} // namespace tests +} // namespace pten From 212ea968df8ad730c82cc89e6e40181070268808 Mon Sep 17 00:00:00 2001 From: jiweibo Date: Mon, 10 Jan 2022 11:28:38 +0000 Subject: [PATCH 2/4] update ci problem --- paddle/pten/api/lib/utils/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/pten/api/lib/utils/CMakeLists.txt b/paddle/pten/api/lib/utils/CMakeLists.txt index 0650c628dc7f3..4a44ad7758b56 100644 --- a/paddle/pten/api/lib/utils/CMakeLists.txt +++ b/paddle/pten/api/lib/utils/CMakeLists.txt @@ -1,2 +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 pten_common) +tensor_base convert_utils dense_tensor lod_tensor selected_rows place var_type_traits) From 08a02635a54fb7def70c2fd4eff10b7040846160 Mon Sep 17 00:00:00 2001 From: jiweibo Date: Mon, 10 Jan 2022 11:52:14 +0000 Subject: [PATCH 3/4] fix ci problem --- paddle/pten/tests/api/CMakeLists.txt | 1 - paddle/pten/tests/api/test_place_utils.cc | 77 ----------------------- 2 files changed, 78 deletions(-) delete mode 100644 paddle/pten/tests/api/test_place_utils.cc diff --git a/paddle/pten/tests/api/CMakeLists.txt b/paddle/pten/tests/api/CMakeLists.txt index bb1eab2c09551..ffbc551843148 100644 --- a/paddle/pten/tests/api/CMakeLists.txt +++ b/paddle/pten/tests/api/CMakeLists.txt @@ -7,7 +7,6 @@ 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_framework_place_utils storage SRCS test_place_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) 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 4db1f59d83786..0000000000000 --- a/paddle/pten/tests/api/test_place_utils.cc +++ /dev/null @@ -1,77 +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 "gtest/gtest.h" - -#include "paddle/pten/api/lib/utils/place_utils.h" - -namespace paddle { -namespace experimental { -namespace tests { - -TEST(place_utils, cpu_place) { - auto pd_place = platform::CPUPlace(); - Place pten_place = ConvertToPtenPlace(pd_place); - CHECK_EQ(pten_place.device().id(), 0); - CHECK(pten_place.device().type() == DeviceType::kHost); - CHECK(pten_place.is_pinned() == false); - - auto pd_place_1 = ConvertToPlatformPlace(pten_place); - CHECK(platform::is_cpu_place(pd_place_1)); - CHECK(pd_place == BOOST_GET_CONST(platform::CPUPlace, pd_place_1)); - CHECK(pten_place == ConvertToPtenPlace(pd_place_1)); -} - -TEST(place_utils, cuda_place) { - auto pd_place = platform::CUDAPlace(1); - Place pten_place = ConvertToPtenPlace(pd_place); - CHECK_EQ(pten_place.device().id(), 1); - CHECK(pten_place.device().type() == DeviceType::kCuda); - CHECK(pten_place.is_pinned() == false); - - auto pd_place_1 = ConvertToPlatformPlace(pten_place); - CHECK(platform::is_gpu_place(pd_place_1)); - CHECK(pd_place == BOOST_GET_CONST(platform::CUDAPlace, pd_place_1)); - CHECK(pten_place == ConvertToPtenPlace(pd_place_1)); -} - -TEST(place_utils, cuda_pinned_place) { - auto pd_place = platform::CUDAPinnedPlace(); - Place pten_place = ConvertToPtenPlace(pd_place); - CHECK_EQ(pten_place.device().id(), 0); - CHECK(pten_place.device().type() == DeviceType::kCuda); - CHECK(pten_place.is_pinned() == true); - - auto pd_place_1 = ConvertToPlatformPlace(pten_place); - CHECK(platform::is_cuda_pinned_place(pd_place_1)); - CHECK(pd_place == BOOST_GET_CONST(platform::CUDAPinnedPlace, pd_place_1)); - CHECK(pten_place == ConvertToPtenPlace(pd_place_1)); -} - -TEST(place_utils, xpu_place) { - auto pd_place = platform::XPUPlace(1); - Place pten_place = ConvertToPtenPlace(pd_place); - CHECK_EQ(pten_place.device().id(), 1); - CHECK(pten_place.device().type() == DeviceType::kXpu); - CHECK(pten_place.is_pinned() == false); - - auto pd_place_1 = ConvertToPlatformPlace(pten_place); - CHECK(platform::is_xpu_place(pd_place_1)); - CHECK(pd_place == BOOST_GET_CONST(platform::XPUPlace, pd_place_1)); - CHECK(pten_place == ConvertToPtenPlace(pd_place_1)); -} - -} // namespace tests -} // namespace experimental -} // namespace paddle From 30d84e7f60694f936c394cfef2b0708c35f9f0e1 Mon Sep 17 00:00:00 2001 From: jiweibo Date: Mon, 10 Jan 2022 12:53:41 +0000 Subject: [PATCH 4/4] update --- paddle/pten/CMakeLists.txt | 3 + paddle/pten/common/CMakeLists.txt | 1 + paddle/pten/{core => common}/place.cc | 55 +++++++++++-------- paddle/pten/{core => common}/place.h | 48 ++++++++-------- paddle/pten/core/CMakeLists.txt | 1 - paddle/pten/tests/common/CMakeLists.txt | 1 + .../pten/tests/{core => common}/test_place.cc | 22 ++++---- paddle/pten/tests/core/CMakeLists.txt | 1 - 8 files changed, 73 insertions(+), 59 deletions(-) create mode 100644 paddle/pten/common/CMakeLists.txt rename paddle/pten/{core => common}/place.cc (50%) rename paddle/pten/{core => common}/place.h (63%) rename paddle/pten/tests/{core => common}/test_place.cc (58%) diff --git a/paddle/pten/CMakeLists.txt b/paddle/pten/CMakeLists.txt index 97875b8632adf..05b321c50c1c4 100644 --- a/paddle/pten/CMakeLists.txt +++ b/paddle/pten/CMakeLists.txt @@ -3,6 +3,9 @@ # float16.h/complex.h/bfloat16.h into pten include_directories(${PADDLE_SOURCE_DIR}/paddle/fluid/platform) +# paddle experimental common components +add_subdirectory(common) + # pten (low level) api headers: include # pten (high level) api add_subdirectory(api) diff --git a/paddle/pten/common/CMakeLists.txt b/paddle/pten/common/CMakeLists.txt new file mode 100644 index 0000000000000..feaf0e12bdb16 --- /dev/null +++ b/paddle/pten/common/CMakeLists.txt @@ -0,0 +1 @@ +cc_library(pten_place SRCS place.cc) diff --git a/paddle/pten/core/place.cc b/paddle/pten/common/place.cc similarity index 50% rename from paddle/pten/core/place.cc rename to paddle/pten/common/place.cc index 29c0fbe837b0b..2d33bb508af44 100644 --- a/paddle/pten/core/place.cc +++ b/paddle/pten/common/place.cc @@ -1,4 +1,4 @@ -/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +/* 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. @@ -12,8 +12,9 @@ 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/core/place.h" +#include "paddle/pten/common/place.h" +#include #include #include "paddle/pten/api/ext/exception.h" @@ -22,24 +23,24 @@ namespace pten { const char *AllocationTypeStr(AllocationType type) { switch (type) { - case AllocationType::kUndef: - return "kUndef"; - case AllocationType::kCpu: - return "kCpu"; - case AllocationType::kGpu: - return "kGpu"; - case AllocationType::kGpuPinned: - return "kGpuPinned"; - case AllocationType::kXpu: - return "kXpu"; - case AllocationType::kNpu: - return "kNpu"; - case AllocationType::kNpuPinned: - return "kNpuPinned"; - case AllocationType::kIpu: - return "kIpu"; - case AllocationType::kMlu: - return "kMlu"; + 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 {}; @@ -47,9 +48,17 @@ const char *AllocationTypeStr(AllocationType type) { } std::string Place::DebugString() const { - std::string str{"Place:{type = "}; - return str + AllocationTypeStr(alloc_type_) + ", id = " + - std::to_string(device) + "}"; + 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) { diff --git a/paddle/pten/core/place.h b/paddle/pten/common/place.h similarity index 63% rename from paddle/pten/core/place.h rename to paddle/pten/common/place.h index 92dab16a5ae62..24d24305202cf 100644 --- a/paddle/pten/core/place.h +++ b/paddle/pten/common/place.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +/* 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. @@ -19,15 +19,15 @@ limitations under the License. */ namespace pten { enum class AllocationType : int8_t { - kUndef = 0, - kCpu = 1, - kGpu = 2, - kGpuPinned = 3, - kXpu = 4, - kNpu = 5, - kNpuPinned = 6, - kIpu = 7, - kMlu = 8, + UNDEF = 0, + CPU = 1, + GPU = 2, + GPUPINNED = 3, + XPU = 4, + NPU = 5, + NPUPINNED = 6, + IPU = 7, + MLU = 8, }; const char *AllocationTypeStr(AllocationType type); @@ -35,7 +35,7 @@ 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::kUndef) {} + Place() : device(0), alloc_type_(AllocationType::UNDEF) {} explicit Place(AllocationType type, int8_t id) : device(id), alloc_type_(type) {} @@ -64,47 +64,47 @@ class Place { class CPUPlace : public Place { public: - CPUPlace() : Place(AllocationType::kCpu, 0) {} + CPUPlace() : Place(AllocationType::CPU, 0) {} }; class GPUPlace : public Place { public: - GPUPlace() : Place(AllocationType::kGpu, 0) {} - explicit GPUPlace(int device_id) : Place(AllocationType::kGpu, device_id) {} + GPUPlace() : Place(AllocationType::GPU, 0) {} + explicit GPUPlace(int device_id) : Place(AllocationType::GPU, device_id) {} }; class GPUPinnedPlace : public Place { public: - GPUPinnedPlace() : Place(AllocationType::kGpuPinned) {} + GPUPinnedPlace() : Place(AllocationType::GPUPINNED) {} }; class XPUPlace : public Place { public: - XPUPlace() : Place(AllocationType::kXpu, 0) {} - explicit XPUPlace(int device_id) : Place(AllocationType::kXpu, device_id) {} + XPUPlace() : Place(AllocationType::XPU, 0) {} + explicit XPUPlace(int device_id) : Place(AllocationType::XPU, device_id) {} }; class NPUPlace : public Place { public: - NPUPlace() : Place(AllocationType::kNpu, 0) {} - explicit NPUPlace(int device_id) : Place(AllocationType::kXpu, device_id) {} + NPUPlace() : Place(AllocationType::NPU, 0) {} + explicit NPUPlace(int device_id) : Place(AllocationType::XPU, device_id) {} }; class NPUPinnedPlace : public Place { public: - NPUPinnedPlace() : Place(AllocationType::kNpuPinned) {} + NPUPinnedPlace() : Place(AllocationType::NPUPINNED) {} }; class IPUPlace : public Place { public: - IPUPlace() : Place(AllocationType::kXpu, 0) {} - explicit IPUPlace(int device_id) : Place(AllocationType::kXpu, device_id) {} + IPUPlace() : Place(AllocationType::XPU, 0) {} + explicit IPUPlace(int device_id) : Place(AllocationType::XPU, device_id) {} }; class MLUPlace : public Place { public: - MLUPlace() : Place(AllocationType::kMlu, 0) {} - explicit MLUPlace(int device_id) : Place(AllocationType::kMlu, device_id) {} + MLUPlace() : Place(AllocationType::MLU, 0) {} + explicit MLUPlace(int device_id) : Place(AllocationType::MLU, device_id) {} }; std::ostream &operator<<(std::ostream &, const Place &); diff --git a/paddle/pten/core/CMakeLists.txt b/paddle/pten/core/CMakeLists.txt index 6c1487147992c..87c3612e35424 100644 --- a/paddle/pten/core/CMakeLists.txt +++ b/paddle/pten/core/CMakeLists.txt @@ -6,7 +6,6 @@ else() cc_library(convert_utils SRCS convert_utils.cc DEPS data_type place) endif() -cc_library(pten_place SRCS place.cc) cc_library(kernel_factory SRCS kernel_factory.cc DEPS enforce convert_utils) cc_library(kernel_context SRCS kernel_context.cc DEPS enforce pten_context) cc_library(tensor_base SRCS tensor_base.cc allocator.cc storage.cc DEPS enforce) diff --git a/paddle/pten/tests/common/CMakeLists.txt b/paddle/pten/tests/common/CMakeLists.txt index c0a5414d53e47..f54b37cb976c5 100644 --- a/paddle/pten/tests/common/CMakeLists.txt +++ b/paddle/pten/tests/common/CMakeLists.txt @@ -1,3 +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/core/test_place.cc b/paddle/pten/tests/common/test_place.cc similarity index 58% rename from paddle/pten/tests/core/test_place.cc rename to paddle/pten/tests/common/test_place.cc index 99788a97e8a99..0bbd8f1d42273 100644 --- a/paddle/pten/tests/core/test_place.cc +++ b/paddle/pten/tests/common/test_place.cc @@ -1,4 +1,4 @@ -/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +/* 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. @@ -12,7 +12,7 @@ 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/core/place.h" +#include "paddle/pten/common/place.h" #include "gtest/gtest.h" @@ -21,30 +21,32 @@ namespace tests { TEST(PtenPlace, place) { pten::Place place; - EXPECT_EQ(place.GetType(), pten::AllocationType::kUndef); + EXPECT_EQ(place.GetType(), pten::AllocationType::UNDEF); - place.Reset(pten::AllocationType::kGpu, 1); - EXPECT_EQ(place.GetType(), pten::AllocationType::kGpu); + 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::kCpu); + 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::kGpu); + EXPECT_EQ(place.GetType(), pten::AllocationType::GPU); EXPECT_EQ(place.GetDeviceId(), 0); pten::GPUPlace place1(2); - EXPECT_EQ(place1.GetType(), pten::AllocationType::kGpu); + EXPECT_EQ(place1.GetType(), pten::AllocationType::GPU); EXPECT_EQ(place1.GetDeviceId(), 2); - std::cout << "place string repr: " << place1 << std::endl; + std::cout << "gpu place repr: " << place1 << std::endl; pten::GPUPinnedPlace place2; - EXPECT_EQ(place2.GetType(), pten::AllocationType::kGpuPinned); + EXPECT_EQ(place2.GetType(), pten::AllocationType::GPUPINNED); + std::cout << "gpu pinned place repr: " << place2 << std::endl; } } // namespace tests diff --git a/paddle/pten/tests/core/CMakeLists.txt b/paddle/pten/tests/core/CMakeLists.txt index ec4fc698090c9..9a5cfecc2917b 100644 --- a/paddle/pten/tests/core/CMakeLists.txt +++ b/paddle/pten/tests/core/CMakeLists.txt @@ -5,4 +5,3 @@ cc_test(test_intrusive_ptr SRCS test_intrusive_ptr.cc) cc_test(test_type_info SRCS test_type_info.cc) cc_test(test_convert_utils SRCS test_convert_utils.cc DEPS convert_utils) cc_test(test_kernel_factory SRCS test_kernel_factory.cc DEPS kernel_factory) -cc_test(test_pten_place SRCS test_place.cc DEPS pten_place)