/* Copyright (c) 2016 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 #include #include #include #include #include "paddle/fluid/framework/data_type.h" #include "paddle/fluid/framework/dlpack_tensor.h" #include "paddle/fluid/framework/tensor.h" #include "paddle/phi/core/memory/allocation/allocator_facade.h" #include "paddle/phi/core/platform/device_context.h" #include "paddle/phi/core/vocab/string_array.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/memory/memory.h" namespace paddle { namespace framework { class PrintOptions { public: static PrintOptions& Instance() { static PrintOptions instance; return instance; } ~PrintOptions() {} PrintOptions(const PrintOptions& o) = delete; const PrintOptions& operator=(const PrintOptions& o) = delete; int precision = 8; int threshold = 1000; int edgeitems = 3; int linewidth = 75; bool sci_mode = false; private: PrintOptions() {} }; phi::DataType ConvertToPDDataType(const std::string& typestr); TEST_API void TensorToStream(std::ostream& os, const phi::DenseTensor& tensor, const phi::DeviceContext& dev_ctx); TEST_API void TensorFromStream(std::istream& is, phi::DenseTensor* tensor, const phi::DeviceContext& dev_ctx); void TensorFromStream(std::istream& is, phi::DenseTensor* tensor, const phi::DeviceContext& dev_ctx, const size_t& seek, const std::vector& shape); // NOTE(zcd): Because TensorCopy is an async operation, when the src_place // and dst_place are two different GPU, to ensure that the operation can // be carried out correctly, there is a src_ctx wait operation in TensorCopy. // If ctx_place and src_place are the same, src_ctx.Wait() is added // after memory::Copy; if ctx_place and dst_place are the same, // src_ctx.Wait() is added before memory::Copy. TEST_API void TensorCopy(const phi::DenseTensor& src, const phi::Place& dst_place, const phi::DeviceContext& ctx, phi::DenseTensor* dst); // NOTE(zcd): If the src.place() and dst_place are two different GPU, // the copy operation is carried out on the dst_place's stream. This is // very important, because TensorCopy is an async operator, and in most // case, once this copy operator returns, dst is to be used in dst_place's // stream, if this copy operation is carried out on the src_place's stream, // when dst is used in dst_place's stream the copy operation may be // not completed. TEST_API void TensorCopy(const phi::DenseTensor& src, const phi::Place& dst_place, phi::DenseTensor* dst); TEST_API void TensorCopySync(const phi::DenseTensor& src, const phi::Place& dst_place, phi::DenseTensor* dst); template void TensorFromVector(const std::vector& src, const phi::DeviceContext& ctx, phi::DenseTensor* dst); template void TensorFromVector(const std::vector& src, phi::DenseTensor* dst); template void TensorToVector(const phi::DenseTensor& src, const phi::DeviceContext& ctx, std::vector* dst); template void TensorToVector(const phi::DenseTensor& src, std::vector* dst); TEST_API DenseTensor TensorFromDLPack(DLManagedTensor* src); TEST_API DenseTensor TensorFromDLPack(DLManagedTensorVersioned* src); // The implementation of template functions. // template void TensorFromArray(const T* src, const size_t& array_size, const phi::DeviceContext& ctx, phi::DenseTensor* dst) { auto dst_place = ctx.GetPlace(); auto src_ptr = static_cast(src); CPUPlace src_place; dst->Resize({static_cast(array_size)}); auto dst_ptr = static_cast(dst->mutable_data(dst_place)); auto size = array_size * sizeof(T); if (phi::is_cpu_place(dst_place)) { memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size); } #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) else if (phi::is_gpu_place(dst_place)) { // NOLINT memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size, reinterpret_cast(ctx).stream()); } #endif #ifdef PADDLE_WITH_CUSTOM_DEVICE else if (phi::is_custom_place(dst_place)) { // NOLINT memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size, reinterpret_cast(ctx).stream()); } #endif #ifdef PADDLE_WITH_XPU else if (phi::is_xpu_place(dst_place)) { // NOLINT memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size); } #endif else { // NOLINT PADDLE_THROW(common::errors::Unimplemented( "TensorFromArray on %s is not supported.", dst_place)); } } template void TensorFromVector(const std::vector& src, const phi::DeviceContext& ctx, phi::DenseTensor* dst) { auto dst_place = ctx.GetPlace(); auto src_ptr = static_cast(src.data()); CPUPlace src_place; dst->Resize({static_cast(src.size())}); auto dst_ptr = static_cast(dst->mutable_data(dst_place)); auto size = src.size() * sizeof(T); if (phi::is_cpu_place(dst_place)) { memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size); } #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) else if (phi::is_gpu_place(dst_place)) { // NOLINT memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size, reinterpret_cast(ctx).stream()); } #endif #ifdef PADDLE_WITH_CUSTOM_DEVICE else if (phi::is_custom_place(dst_place)) { // NOLINT memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size, reinterpret_cast(ctx).stream()); } #endif #ifdef PADDLE_WITH_XPU else if (phi::is_xpu_place(dst_place)) { // NOLINT memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size); } #endif else { // NOLINT PADDLE_THROW(common::errors::Unimplemented( "TensorFromVector on %s is not supported.", dst_place)); } } // The fully specialized function should be inline to avoid // multi-definition. template <> inline void TensorFromVector(const std::vector& src, const phi::DeviceContext& ctx, phi::DenseTensor* dst) { // vector has no data() member, use array instead. // See details: // https://stackoverflow.com/questions/46115669/why-does-stdvectorbool-have-no-data/46115714 bool* array = new bool[src.size()]; for (unsigned int i = 0; i < src.size(); i++) { array[i] = static_cast(src[i]); } auto dst_place = ctx.GetPlace(); auto src_ptr = static_cast(array); CPUPlace src_place; dst->Resize({static_cast(src.size())}); auto dst_ptr = static_cast(dst->mutable_data(dst_place)); auto size = src.size() * sizeof(bool); if (phi::is_cpu_place(dst_place)) { memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size); } #ifdef PADDLE_WITH_CUDA else if (phi::is_gpu_place(dst_place)) { // NOLINT memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size, reinterpret_cast(ctx).stream()); } #endif #ifdef PADDLE_WITH_CUSTOM_DEVICE else if (phi::is_custom_place(dst_place)) { // NOLINT auto stream = reinterpret_cast(ctx).stream(); memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size, stream); } #endif #ifdef PADDLE_WITH_XPU else if (phi::is_xpu_place(dst_place)) { // NOLINT memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size); } #endif else { // NOLINT PADDLE_THROW(common::errors::Unimplemented( "TensorFromVector on %s is not supported.", dst_place)); } delete[] array; } template void TensorFromVector(const std::vector& src, phi::DenseTensor* dst) { CPUPlace dst_place = CPUPlace(); auto src_ptr = static_cast(src.data()); CPUPlace src_place; dst->Resize({static_cast(src.size())}); auto dst_ptr = static_cast(dst->mutable_data(dst_place)); auto size = src.size() * sizeof(T); memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size); } template <> inline void TensorFromVector(const std::vector& src, phi::DenseTensor* dst) { bool* array = new bool[src.size()]; for (unsigned int i = 0; i < src.size(); i++) { array[i] = static_cast(src[i]); } CPUPlace dst_place = CPUPlace(); auto src_ptr = static_cast(array); CPUPlace src_place; dst->Resize({static_cast(src.size())}); auto dst_ptr = static_cast(dst->mutable_data(dst_place)); auto size = src.size() * sizeof(bool); memory::Copy(dst_place, dst_ptr, src_place, src_ptr, size); delete[] array; } template void TensorToVector(const phi::DenseTensor& src, const phi::DeviceContext& ctx, std::vector* dst) { auto src_ptr = static_cast(src.data()); auto size = src.numel() * sizeof(T); CPUPlace dst_place; dst->resize(src.numel()); auto dst_ptr = static_cast(dst->data()); if (phi::is_cpu_place(src.place())) { memory::Copy(dst_place, dst_ptr, src.place(), src_ptr, size); } #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) else if (phi::is_gpu_place(src.place())) { // NOLINT memory::Copy(dst_place, dst_ptr, src.place(), src_ptr, size, reinterpret_cast(ctx).stream()); } #endif #if defined(PADDLE_WITH_XPU) else if (phi::is_xpu_place(src.place())) { // NOLINT memory::Copy(dst_place, dst_ptr, src.place(), src_ptr, size); } #endif #ifdef PADDLE_WITH_CUSTOM_DEVICE else if (phi::is_custom_place(src.place())) { // NOLINT memory::Copy(dst_place, dst_ptr, src.place(), src_ptr, size, nullptr); } #endif else { // NOLINT PADDLE_THROW(common::errors::Unimplemented( "TensorToVector on %s is not supported.", src.place())); } } template <> inline void TensorToVector(const phi::DenseTensor& src, const phi::DeviceContext& ctx, std::vector* dst) { auto src_ptr = static_cast(src.data()); auto size = src.numel() * sizeof(bool); bool* array = new bool[src.numel()]; CPUPlace dst_place; dst->resize(src.numel()); auto dst_ptr = static_cast(array); if (phi::is_cpu_place(src.place())) { memory::Copy(dst_place, dst_ptr, src.place(), src_ptr, size); } #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) else if (phi::is_gpu_place(src.place())) { // NOLINT memory::Copy(dst_place, dst_ptr, src.place(), src_ptr, size, reinterpret_cast(ctx).stream()); } #endif #if defined(PADDLE_WITH_XPU) else if (phi::is_xpu_place(src.place())) { // NOLINT memory::Copy(dst_place, dst_ptr, src.place(), src_ptr, size); } #endif #ifdef PADDLE_WITH_CUSTOM_DEVICE else if (phi::is_custom_place(src.place())) { // NOLINT memory::Copy(dst_place, dst_ptr, src.place(), src_ptr, size, nullptr); } #endif for (int64_t i = 0; i < src.numel(); i++) { (*dst)[i] = static_cast(array[i]); } delete[] array; } template void TensorToVector(const phi::DenseTensor& src, std::vector* dst) { auto src_ptr = static_cast(src.data()); auto size = src.numel() * sizeof(T); CPUPlace dst_place; dst->resize(src.numel()); auto dst_ptr = static_cast(dst->data()); PADDLE_ENFORCE_EQ( phi::is_cpu_place(src.place()), true, common::errors::InvalidArgument( "The input tensor should be CPU device, but actually it is in %s.", src.place())); memory::Copy(dst_place, dst_ptr, src.place(), src_ptr, size); } template <> inline void TensorToVector(const phi::DenseTensor& src, std::vector* dst) { auto src_ptr = static_cast(src.data()); auto size = src.numel() * sizeof(bool); bool* array = new bool[src.numel()]; CPUPlace dst_place; dst->resize(src.numel()); auto dst_ptr = static_cast(array); PADDLE_ENFORCE_EQ( phi::is_cpu_place(src.place()), true, common::errors::InvalidArgument( "The input tensor should be CPU device, but actually it is in %s.", src.place())); memory::Copy(dst_place, dst_ptr, src.place(), src_ptr, size); for (int64_t i = 0; i < src.numel(); i++) { (*dst)[i] = static_cast(array[i]); } delete[] array; } std::ostream& operator<<(std::ostream& os, const LegacyLoD& lod); template inline T GetValue(const phi::DenseTensor* x) { T value = static_cast(0); if (!phi::is_cpu_place(x->place())) { DenseTensor cpu_x; framework::TensorCopy(*x, CPUPlace(), &cpu_x); value = cpu_x.data()[0]; } else { value = x->data()[0]; } return value; } } // namespace framework } // namespace paddle namespace phi { TEST_API std::ostream& operator<<(std::ostream& os, const DenseTensor& t); }