/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. Copyright (c) 2022 NVIDIA Corporation. 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/fluid/platform/device_context.h" #include #include #include #include "glog/logging.h" #include "paddle/fluid/platform/device/device_wrapper.h" #include "paddle/fluid/platform/place.h" #include "paddle/fluid/platform/profiler.h" #include "paddle/fluid/platform/profiler/event_tracing.h" #include "paddle/phi/core/allocator.h" #include "paddle/phi/core/expect.h" #include "paddle/phi/core/generator.h" #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) #include "paddle/fluid/memory/allocation/cuda_device_context_allocator.h" #include "paddle/fluid/platform/cuda_device_guard.h" #include "paddle/phi/backends/gpu/gpu_context.h" #endif #ifdef PADDLE_WITH_MLU #include "paddle/fluid/platform/device/mlu/device_context.h" #include "paddle/fluid/platform/device/mlu/device_context_allocator.h" #endif namespace paddle { namespace platform { DeviceType Place2DeviceType(const platform::Place& place) { if (platform::is_cpu_place(place)) { return platform::DeviceType::CPU; } else if (platform::is_gpu_place(place)) { return platform::DeviceType::CUDA; } else if (platform::is_xpu_place(place)) { return platform::DeviceType::XPU; } else if (platform::is_ipu_place(place)) { return platform::DeviceType::IPU; } else if (platform::is_npu_place(place)) { return platform::DeviceType::NPU; } else if (platform::is_mlu_place(place)) { return platform::DeviceType::MLU; } else if (platform::is_custom_place(place)) { return platform::DeviceType::CUSTOM_DEVICE; } else { PADDLE_THROW(platform::errors::Unavailable( "Unsupported place %s to convert into platform::DeviceType.", place)); } } #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) template typename std::enable_if::value, DevCtx*>::type ConstructDevCtx(const phi::Place& p, /*unused*/ int stream_priority = 0) { return new DevCtx(p); } template typename std::enable_if::value, DevCtx*>::type ConstructDevCtx(const phi::Place& p, int stream_priority) { return new DevCtx(p, /*init=*/true, stream_priority); } #else template DevCtx* ConstructDevCtx(const phi::Place& p, /*unused*/ int stream_priority) { return new DevCtx(p); } #endif template inline std::unique_ptr CreateDeviceContext( const phi::Place& p, bool disable_setting_default_stream_for_allocator, int stream_priority) { using PtrType = std::unique_ptr; DevCtx* dev_ctx = ConstructDevCtx(p, stream_priority); auto& instance = paddle::memory::allocation::AllocatorFacade::Instance(); if (p.GetType() == phi::AllocationType::GPU) { #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) auto* cuda_ctx = dynamic_cast(dev_ctx); PADDLE_ENFORCE_NOT_NULL( cuda_ctx, phi::errors::InvalidArgument( "Failed to dynamic_cast dev_ctx into phi::GPUContext.")); if (!disable_setting_default_stream_for_allocator) { instance.SetDefaultStream(GPUPlace(p.GetDeviceId()), cuda_ctx->stream()); } dev_ctx->SetAllocator(instance.GetAllocator(p, cuda_ctx->stream()).get()); dev_ctx->SetPinnedAllocator( instance.GetAllocator(phi::GPUPinnedPlace()).get()); cuda_ctx->PartialInitWithAllocator(); dev_ctx->SetGenerator(phi::DefaultCUDAGenerator(p.GetDeviceId()).get()); #endif } else if (p.GetType() == phi::AllocationType::XPU) { #if defined(PADDLE_WITH_XPU) dev_ctx->SetAllocator(instance.GetAllocator(p).get()); dev_ctx->SetGenerator(phi::DefaultXPUGenerator(p.GetDeviceId()).get()); #endif } else { dev_ctx->SetAllocator(instance.GetAllocator(p).get()); dev_ctx->SetGenerator(phi::DefaultCPUGenerator().get()); } dev_ctx->SetHostGenerator(phi::DefaultCPUGenerator().get()); dev_ctx->SetHostAllocator(instance.GetAllocator(phi::CPUPlace()).get()); dev_ctx->SetZeroAllocator(instance.GetZeroAllocator(p).get()); dev_ctx->SetHostZeroAllocator( instance.GetZeroAllocator(phi::CPUPlace()).get()); return PtrType(dev_ctx); } template inline void EmplaceDeviceContext( std::map>>* place_to_device_context, const phi::Place& place, bool disable_setting_default_stream_for_allocator, int stream_priority) { // lazy evaluation. i.e., only create device context at first `Get` place_to_device_context->emplace( place, std::async(std::launch::deferred, CreateDeviceContext, place, disable_setting_default_stream_for_allocator, stream_priority)); } void EmplaceDeviceContexts( std::map>>* place_to_device_context, const std::vector& places, bool disable_setting_default_stream_for_allocator, int stream_priority) { PADDLE_ENFORCE_GT( places.size(), 0, phi::errors::InvalidArgument("The number of platform places should " "be larger than 0. But received %d.", places.size())); std::set set; for (auto& p : places) { set.insert(p); } for (auto& place : set) { if (place.GetType() == phi::AllocationType::CPU) { #ifdef PADDLE_WITH_MKLDNN EmplaceDeviceContext( place_to_device_context, place, disable_setting_default_stream_for_allocator, /*unused*/ stream_priority); #else EmplaceDeviceContext( place_to_device_context, place, disable_setting_default_stream_for_allocator, /*unused*/ stream_priority); #endif } else if (place.GetType() == phi::AllocationType::GPU) { #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) EmplaceDeviceContext( place_to_device_context, place, disable_setting_default_stream_for_allocator, stream_priority); #else PADDLE_THROW( phi::errors::Unimplemented("GPUPlace is not supported. Please " "re-compile with WITH_GPU option.")); #endif } else if (place.GetType() == phi::AllocationType::XPU) { #ifdef PADDLE_WITH_XPU EmplaceDeviceContext( place_to_device_context, place, disable_setting_default_stream_for_allocator, /*unused*/ stream_priority); #else PADDLE_THROW( phi::errors::Unimplemented("XPUPlace is not supported. Please " "re-compile with WITH_XPU option.")); #endif } else if (place.GetType() == phi::AllocationType::CUSTOM) { #ifdef PADDLE_WITH_CUSTOM_DEVICE EmplaceDeviceContext( place_to_device_context, place, disable_setting_default_stream_for_allocator, /*unused*/ stream_priority); #else PADDLE_THROW(phi::errors::Unimplemented( "CustomPlace is not supported. Please re-compile with " "WITH_CUSTOM_DEVICE " "option.")); #endif } else if (platform::is_cuda_pinned_place(place)) { #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) EmplaceDeviceContext( place_to_device_context, place, disable_setting_default_stream_for_allocator, /*unused*/ stream_priority); #else PADDLE_THROW(platform::errors::Unimplemented( "CUDAPlace is not supported. Please re-compile with WITH_GPU " "option.")); #endif } else if (platform::is_mlu_place(place)) { #ifdef PADDLE_WITH_MLU EmplaceDeviceContext( place_to_device_context, place, disable_setting_default_stream_for_allocator, /*unused*/ stream_priority); #else PADDLE_THROW( platform::errors::Unimplemented("MLUPlace is not supported. Please " "re-compile with WITH_MLU option.")); #endif } else if (platform::is_ipu_place(place)) { #ifdef PADDLE_WITH_IPU EmplaceDeviceContext( place_to_device_context, place, disable_setting_default_stream_for_allocator, /*unused*/ stream_priority); #else PADDLE_THROW( platform::errors::Unimplemented("IPUPlace is not supported. Please " "re-compile with WITH_IPU option.")); #endif } else if (platform::is_npu_place(place)) { #ifdef PADDLE_WITH_ASCEND_CL EmplaceDeviceContext( place_to_device_context, place, disable_setting_default_stream_for_allocator, /*unused*/ stream_priority); #else PADDLE_THROW(platform::errors::Unimplemented( "NPUPlace is not supported. Please " "re-compile with WITH_ASCEND_CL option.")); #endif } else if (platform::is_npu_pinned_place(place)) { #ifdef PADDLE_WITH_ASCEND_CL EmplaceDeviceContext( place_to_device_context, place, disable_setting_default_stream_for_allocator, /*unused*/ stream_priority); #else PADDLE_THROW(platform::errors::Unimplemented( "NPUPinnedPlace is not supported. Please re-compile with " "WITH_ASCEND_CL " "option.")); #endif } } } #ifdef PADDLE_WITH_IPU IPUDeviceContext::IPUDeviceContext(IPUPlace place) : place_(place) {} const Place& IPUDeviceContext::GetPlace() const { return place_; } void IPUDeviceContext::Wait() const { /*! \brief Wait for all operations completion in the stream. */ } IPUDeviceContext::~IPUDeviceContext() {} #endif #ifdef PADDLE_WITH_ASCEND_CL NPUDeviceContext::NPUDeviceContext(NPUPlace place) : place_(place) { NPUDeviceGuard guard(place_.device); // PADDLE_ENFORCE_NPU_SUCCESS(aclrtCreateContext(&context_, place_.device)); // NOTE(zhiqiu): Usually, no need to create context explicitly, // ACL creates a default context which contains 1 default stream // and 1 sync strean after aclrtSetDevice. platform::GetCurrentNPUContext(&context_); stream_.reset(new stream::NPUStream(place)); } NPUDeviceContext::~NPUDeviceContext() { // NPUDeviceGuard guard(place_.device); // PADDLE_ENFORCE_NPU_SUCCESS(aclrtDestroyContext(context_)); } void NPUDeviceContext::Wait() const { platform::RecordEvent record_event( "NPUDeviceContext/wait", platform::TracerEventType::UserDefined, 2); VLOG(4) << "NPU context(" << this << ") Wait"; stream_->Wait(); } aclrtStream NPUDeviceContext::stream() const { return stream_->raw_stream(); } const Place& NPUDeviceContext::GetPlace() const { return place_; } aclrtContext NPUDeviceContext::context() const { return context_; } NPUPinnedDeviceContext::NPUPinnedDeviceContext() { eigen_device_.reset(new Eigen::DefaultDevice()); } NPUPinnedDeviceContext::NPUPinnedDeviceContext(NPUPinnedPlace place) : place_(place) { eigen_device_.reset(new Eigen::DefaultDevice()); } Eigen::DefaultDevice* NPUPinnedDeviceContext::eigen_device() const { return eigen_device_.get(); } const Place& NPUPinnedDeviceContext::GetPlace() const { return place_; } #endif } // namespace platform } // namespace paddle