diff --git a/paddle/pten/CMakeLists.txt b/paddle/pten/CMakeLists.txt index 2c2004162452c5da06a1a413d33737b236ea4516..448bcb80aa678dc33a1bcc27d640b9417a79aae0 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/api/lib/utils/CMakeLists.txt b/paddle/pten/api/lib/utils/CMakeLists.txt index 34f8da94c661151494715e8819c002fb5594ecb6..06178dad43767df13f74ea08ec4f6a5e044454d6 100644 --- a/paddle/pten/api/lib/utils/CMakeLists.txt +++ b/paddle/pten/api/lib/utils/CMakeLists.txt @@ -1 +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) +cc_library(pten_api_utils SRCS allocator.cc storage.cc tensor_utils.cc place_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 new file mode 100644 index 0000000000000000000000000000000000000000..af4f84b1ad83635a39cf26446aaf2fba998176d3 --- /dev/null +++ b/paddle/pten/api/lib/utils/place_utils.cc @@ -0,0 +1,62 @@ +/* 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 new file mode 100644 index 0000000000000000000000000000000000000000..9ac10158040b2c1f63dee4eb8828524750027aa0 --- /dev/null +++ b/paddle/pten/api/lib/utils/place_utils.h @@ -0,0 +1,28 @@ +/* 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 new file mode 100644 index 0000000000000000000000000000000000000000..c4083d7f0d75641b147fabe9177c2066ff974daa --- /dev/null +++ b/paddle/pten/common/CMakeLists.txt @@ -0,0 +1 @@ +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 new file mode 100644 index 0000000000000000000000000000000000000000..9583b521d9123975ceb3526caf08096f631851de --- /dev/null +++ b/paddle/pten/common/device.cc @@ -0,0 +1,65 @@ +/* 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 "kUndef"; + 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 new file mode 100644 index 0000000000000000000000000000000000000000..eddb71bce16da991b1e21a045df6c99c3b7b6ec2 --- /dev/null +++ b/paddle/pten/common/device.h @@ -0,0 +1,70 @@ +/* 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 new file mode 100644 index 0000000000000000000000000000000000000000..ba34c5d0f92225fd967e492ddcbc105a3c7d35c3 --- /dev/null +++ b/paddle/pten/common/place.cc @@ -0,0 +1,26 @@ +/* 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 new file mode 100644 index 0000000000000000000000000000000000000000..fdc948734934bfbd8703e3585d188aefe4dc2a1f --- /dev/null +++ b/paddle/pten/common/place.h @@ -0,0 +1,67 @@ +/* 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/tests/api/CMakeLists.txt b/paddle/pten/tests/api/CMakeLists.txt index 9acf39f7c2bdceec675c842a9b9ff0916a6bb65a..a5f6aa73cade5a1972d5e253621aa55f6ea606d2 100644 --- a/paddle/pten/tests/api/CMakeLists.txt +++ b/paddle/pten/tests/api/CMakeLists.txt @@ -7,6 +7,8 @@ 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) cc_test(test_matmul_api SRCS test_matmul_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 new file mode 100644 index 0000000000000000000000000000000000000000..4db1f59d8378638276a9e8506e221c539a5ccc1a --- /dev/null +++ b/paddle/pten/tests/api/test_place_utils.cc @@ -0,0 +1,77 @@ +/* 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