- Notifications
You must be signed in to change notification settings - Fork 5.9k
[Phi] Add phi device context pool #40635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chenwhql merged 7 commits into PaddlePaddle:develop from chenwhql:phi/add_device_context_pool Mar 21, 2022
Merged
Changes from 2 commits
Commits
Show all changes
7 commits Select commit Hold shift + click to select a range
2ab9a2c add phi device context pool
chenwhql a89bc8c change year
chenwhql 1624de9 fix compile error
chenwhql 858534e fix operator = error
chenwhql 062c126 refine init impl
chenwhql 7056814 polish details
chenwhql 4c4e9aa refine init impl
chenwhql File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* 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 "paddle/phi/common/place.h" | ||
| #include "paddle/phi/core/macros.h" | ||
| #include "paddle/utils/flat_hash_map.h" | ||
| | ||
| namespace phi { | ||
| class DeviceContext; | ||
| class CPUContext; | ||
| class GPUContext; | ||
| } // namespace phi | ||
| | ||
| namespace paddle { | ||
| namespace experimental { | ||
| | ||
| template <AllocationType T> | ||
| struct DefaultDeviceContextType; | ||
| | ||
| template <> | ||
| struct DefaultDeviceContextType<AllocationType::CPU> { | ||
| using TYPE = phi::CPUContext; | ||
| }; | ||
| | ||
| template <> | ||
| struct DefaultDeviceContextType<AllocationType::GPU> { | ||
| using TYPE = phi::GPUContext; | ||
| }; | ||
| | ||
| /** | ||
| * The DeviceContextPool here is just a mirror of the DeviceContextPool in | ||
| * fluid, and does not manage the life cycle of the DeviceContext. | ||
| * It is mainly used for external custom operator calls and high-performance | ||
| * C++ APIs. | ||
| * | ||
| * Since DeviceContextPool in fluid is a global singleton, it always exists | ||
| * in program running, so DeviceContextPool here can always access the correct | ||
| * DeviceContext pointer. | ||
| * | ||
| * In order not to depend on the fluid's DeviceContextPool, | ||
| * the DeviceContextPool here needs to be initialized in the fluid, and cannot | ||
| * be initialized by itself. | ||
| */ | ||
| class DeviceContextPool { | ||
| public: | ||
| static DeviceContextPool& Instance(); | ||
| | ||
| const phi::DeviceContext* Get(const Place& place) const; | ||
| | ||
| phi::DeviceContext* GetMutable(const Place& place); | ||
| | ||
| template <AllocationType T> | ||
| const typename DefaultDeviceContextType<T>::TYPE* Get( | ||
| const Place& place) const { | ||
| return reinterpret_cast<const typename DefaultDeviceContextType<T>::TYPE*>( | ||
| Get(place)); | ||
| } | ||
| | ||
| void Insert(const Place& place, const phi::DeviceContext* dev_ctx); | ||
| | ||
| private: | ||
| DeviceContextPool() = default; | ||
| paddle::flat_hash_map<Place, const phi::DeviceContext*, Place::Hash> | ||
| context_map_; | ||
| | ||
| DISABLE_COPY_AND_ASSIGN(DeviceContextPool); | ||
| }; | ||
| | ||
| } // namespace experimental | ||
| } // namespace paddle | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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/phi/api/include/context_pool.h" | ||
| | ||
| #include "paddle/phi/backends/cpu/cpu_context.h" | ||
| #include "paddle/phi/backends/gpu/gpu_context.h" | ||
| #include "paddle/phi/core/enforce.h" | ||
| | ||
| namespace paddle { | ||
| namespace experimental { | ||
| | ||
| DeviceContextPool& DeviceContextPool::Instance() { | ||
| static DeviceContextPool g_device_context_pool; | ||
| return g_device_context_pool; | ||
| } | ||
| | ||
| const phi::DeviceContext* DeviceContextPool::Get(const Place& place) const { | ||
| auto it = context_map_.find(place); | ||
| PADDLE_ENFORCE_NE( | ||
| it, | ||
| context_map_.end(), | ||
| phi::errors::NotFound("The DeviceContext of %s does not exists.", place)); | ||
| return it->second; | ||
| } | ||
| | ||
| phi::DeviceContext* DeviceContextPool::GetMutable(const Place& place) { | ||
| return const_cast<phi::DeviceContext*>(Get(place)); | ||
| } | ||
| | ||
| void DeviceContextPool::Insert(const Place& place, | ||
| const phi::DeviceContext* dev_ctx) { | ||
| auto it = context_map_.find(place); | ||
| PADDLE_ENFORCE_EQ(it, | ||
| context_map_.end(), | ||
| phi::errors::AlreadyExists( | ||
| "The DeviceContext of %s already exists.", place)); | ||
| context_map_[place] = dev_ctx; | ||
| } | ||
| | ||
| } // namespace experimental | ||
| } // namespace paddle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
中间实现有所调整,这里注释也需要调整下,下个pr完善下