-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[XPU] ernie3.0 support, optimize xpu memory usage when sharing weights on __xpu__fc op #7966
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
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
80ebf6e
[XPU] ernie3.0 support, optimize xpu memory usage when sharing weight…
mayang002 f04726c
[XPU] remove some debug code, test=xpu, test=develop
mayang002 0f37c04
[XPU] refine quant codes to xpu_quant.h/.cc, test=develop, test=xpu
mayang002 a5d6352
[XPU] refactor xpu memory related code, test=develop, test=xpu
mayang002 c7cba33
[XPU] use shared_ptr to avoid memory leak and typo fix, test=develop,…
mayang002 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
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,229 @@ | ||
| // Copyright (c) 2019 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 "lite/backends/xpu/xpu_quantizer.h" | ||
| #include <algorithm> | ||
| #include <string> | ||
| #include "lite/backends/xpu/math.h" | ||
|
|
||
| namespace paddle { | ||
| namespace lite { | ||
|
|
||
| template <typename T> | ||
| static inline size_t hash_combine(size_t seed, const T& v) { | ||
mayang002 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| std::hash<T> hasher; | ||
| seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); | ||
| return seed; | ||
| } | ||
|
|
||
| static size_t Hashed(const float* cpu_data, | ||
| int numel, | ||
| const std::string& precision, | ||
| bool trans) { | ||
| std::hash<const float*> ptr_hasher; | ||
| auto hash_res = ptr_hasher(cpu_data); | ||
| hash_res = hash_combine(hash_res, numel); | ||
| hash_res = hash_combine(hash_res, precision); | ||
| hash_res = hash_combine(hash_res, trans); | ||
| return hash_res; | ||
| } | ||
|
|
||
| template <typename T> | ||
| static inline const std::string CppTypeToString() { | ||
| return "unkown"; | ||
| } | ||
| template <> | ||
| inline const std::string CppTypeToString<float>() { | ||
| return "float"; | ||
| } | ||
| template <> | ||
| inline const std::string CppTypeToString<float16>() { | ||
| return "float16"; | ||
| } | ||
| template <> | ||
| inline const std::string CppTypeToString<int64_t>() { | ||
| return "int64_t"; | ||
| } | ||
| template <> | ||
| inline const std::string CppTypeToString<int>() { | ||
| return "int"; | ||
| } | ||
| template <> | ||
| inline const std::string CppTypeToString<int16_t>() { | ||
| return "int16_t"; | ||
| } | ||
| template <> | ||
| inline const std::string CppTypeToString<int8_t>() { | ||
| return "int8_t"; | ||
| } | ||
|
|
||
| template <typename T> | ||
| static void QuantFP32ToIntX(const float* src_ptr, | ||
| T* dst_ptr, | ||
| float max_val, | ||
| int numel) { | ||
| CHECK(false) << "Not support for T is " << CppTypeToString<T>(); | ||
mayang002 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| template <> | ||
| void QuantFP32ToIntX<float>(const float* src_ptr, | ||
| float* dst_ptr, | ||
| float max_val, | ||
| int numel) { | ||
| std::copy(src_ptr, src_ptr + numel, dst_ptr); | ||
| } | ||
| template <> | ||
| void QuantFP32ToIntX<int16_t>(const float* src_ptr, | ||
| int16_t* dst_ptr, | ||
| float max_val, | ||
| int numel) { | ||
| paddle::lite::xpu::math::ConvertFP32ToInt16(src_ptr, dst_ptr, max_val, numel); | ||
| } | ||
| template <> | ||
| void QuantFP32ToIntX<int8_t>(const float* src_ptr, | ||
| int8_t* dst_ptr, | ||
| float max_val, | ||
| int numel) { | ||
| paddle::lite::xpu::math::ConvertFP32ToInt8(src_ptr, dst_ptr, max_val, numel); | ||
| } | ||
|
|
||
| template <typename Tcpu, | ||
| typename Txpu, | ||
| typename std::enable_if<!std::is_same<Tcpu, float>::value, | ||
| Tcpu>::type* ptr = nullptr> | ||
| void ConvertWithQuant( | ||
| const Tcpu* cpu_data, | ||
| const DDimLite& dims, | ||
| bool data_transpose, | ||
| std::unordered_map<size_t, | ||
| std::pair<XPUScratchPadGuard, XPUScratchPadGuard>>& | ||
| weight_cache_, | ||
| size_t hashed_key) { | ||
| CHECK(false) << "Not support for Tcpu is " << CppTypeToString<Tcpu>(); | ||
mayang002 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| template <typename Tcpu, | ||
| typename Txpu, | ||
| typename std::enable_if<std::is_same<Tcpu, float>::value, Tcpu>::type* | ||
| ptr = nullptr> | ||
| void ConvertWithQuant( | ||
| const Tcpu* cpu_data, | ||
| const DDimLite& dims, | ||
| bool data_transpose, | ||
| std::unordered_map<size_t, | ||
| std::pair<XPUScratchPadGuard, XPUScratchPadGuard>>& | ||
| weight_cache_, | ||
| size_t hashed_key) { | ||
| // transpose | ||
| const Tcpu* cpu_ptr = nullptr; | ||
| int numel = dims.production(); | ||
| std::vector<Tcpu> transpose_data(numel, 0); | ||
| if (data_transpose) { | ||
| CHECK(dims.size() == 2) << "Not support: dims.size = " << dims.size(); | ||
mayang002 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| paddle::lite::xpu::math::Transpose<Tcpu>( | ||
| cpu_data, transpose_data.data(), dims[0], dims[1]); | ||
| cpu_ptr = transpose_data.data(); | ||
| } else { | ||
| cpu_ptr = cpu_data; | ||
| } | ||
| // findmax | ||
| XPUScratchPadGuard weight_max_guard; | ||
| XPUScratchPadGuard quant_weight_guard; | ||
| float max_val = paddle::lite::xpu::math::FindMaxAbs(cpu_ptr, numel); | ||
| int max_ptr_size = xdnn::get_max_ptr_size(TargetWrapperXPU::GetRawContext()); | ||
| std::vector<float> max_vec(max_ptr_size, max_val); | ||
| weight_max_guard = std::move( | ||
| TargetWrapperXPU::MallocScratchPad(max_ptr_size * sizeof(float))); | ||
| TargetWrapperXPU::MemcpySync(weight_max_guard->addr_, | ||
| max_vec.data(), | ||
| max_ptr_size * sizeof(float), | ||
| IoDirection::HtoD); | ||
| // quant | ||
| quant_weight_guard = | ||
| std::move(TargetWrapperXPU::MallocScratchPad(numel * sizeof(Txpu))); | ||
| std::vector<int16_t> quant_data_cpu(numel, 0); | ||
| QuantFP32ToIntX<Txpu>(cpu_ptr, quant_data_cpu.data(), max_val, numel); | ||
| TargetWrapperXPU::MemcpySync(quant_weight_guard->addr_, | ||
| quant_data_cpu.data(), | ||
| numel * sizeof(Txpu), | ||
| IoDirection::HtoD); | ||
| // add to cache | ||
| weight_cache_[hashed_key] = std::make_pair(std::move(weight_max_guard), | ||
| std::move(quant_weight_guard)); | ||
| } | ||
|
|
||
| template <typename T> | ||
| void ConvertWithoutQuant( | ||
| const T* cpu_data, | ||
| const DDimLite& dims, | ||
| bool data_transpose, | ||
| std::unordered_map<size_t, | ||
| std::pair<XPUScratchPadGuard, XPUScratchPadGuard>>& | ||
| weight_cache_, | ||
| size_t hashed_key) { | ||
| // transpose | ||
| const T* cpu_ptr = nullptr; | ||
| int numel = dims.production(); | ||
| std::vector<T> transpose_data(numel, 0); | ||
| if (data_transpose) { | ||
| CHECK(dims.size() == 2) << "Not support: dims.size = " << dims.size(); | ||
| paddle::lite::xpu::math::Transpose<T>( | ||
| cpu_data, transpose_data.data(), dims[0], dims[1]); | ||
| cpu_ptr = transpose_data.data(); | ||
| } else { | ||
| cpu_ptr = cpu_data; | ||
| } | ||
| // copy to XPU | ||
| XPUScratchPadGuard weight_max_guard(new XPUScratchPad(nullptr, 0)); | ||
| XPUScratchPadGuard quant_weight_guard; | ||
| quant_weight_guard = | ||
| std::move(TargetWrapperXPU::MallocScratchPad(numel * sizeof(T))); | ||
| TargetWrapperXPU::MemcpySync( | ||
| quant_weight_guard->addr_, cpu_ptr, numel * sizeof(T), IoDirection::HtoD); | ||
| // add to cache | ||
| weight_cache_[hashed_key] = std::make_pair(std::move(weight_max_guard), | ||
| std::move(quant_weight_guard)); | ||
| } | ||
|
|
||
| template <typename Tcpu, typename Txpu> | ||
| XPUQuantData XPUQuantizer::quant(const Tcpu* cpu_data, | ||
| const DDimLite& dims, | ||
| bool data_transpose) { | ||
| int numel = dims.production(); | ||
| const std::string cpu_dtype = CppTypeToString<Tcpu>(); | ||
| const std::string xpu_dtype = CppTypeToString<Txpu>(); | ||
| const std::string precision = cpu_dtype + xpu_dtype; | ||
| auto hashed_key = Hashed(cpu_data, numel, precision, data_transpose); | ||
| VLOG(3) << "cpu_data=" << cpu_data << ", numel=" << numel | ||
| << ", precision=" << precision << ", transpose=" << data_transpose | ||
| << ", hashed_key=" << hashed_key; | ||
| if (weight_cache_.find(hashed_key) == weight_cache_.end()) { | ||
| bool need_quant = !std::is_same<Tcpu, Txpu>::value; | ||
| if (need_quant) { | ||
| ConvertWithQuant<Tcpu, Txpu>( | ||
| cpu_data, dims, data_transpose, weight_cache_, hashed_key); | ||
| } else { | ||
| ConvertWithoutQuant<Tcpu>( | ||
| cpu_data, dims, data_transpose, weight_cache_, hashed_key); | ||
| } | ||
| } | ||
|
|
||
| float* max_ptr = | ||
| reinterpret_cast<float*>(weight_cache_[hashed_key].first->addr_); | ||
| void* qdata_ptr = weight_cache_[hashed_key].second->addr_; | ||
| XPUQuantData xpu_qdata(max_ptr, qdata_ptr); | ||
| return xpu_qdata; | ||
| } | ||
|
|
||
| } // namespace lite | ||
| } // 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // 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 <unordered_map> | ||
| #include <utility> | ||
| #include <vector> | ||
| #include "lite/backends/xpu/xpu_header_sitter.h" | ||
| #include "lite/backends/xpu/xpu_scratch.h" | ||
| #include "lite/core/dim.h" | ||
| #include "lite/utils/macros.h" | ||
|
|
||
| namespace paddle { | ||
| namespace lite { | ||
|
|
||
| struct XPUQuantData { | ||
| XPUQuantData() : data_ptr_(nullptr), max_ptr_(nullptr) {} | ||
| XPUQuantData(float* max_ptr, void* data_ptr) | ||
| : data_ptr_(data_ptr), max_ptr_(max_ptr) {} | ||
| void* data_ptr_{nullptr}; | ||
| float* max_ptr_{nullptr}; | ||
| }; | ||
|
|
||
| class XPUQuantizer { | ||
| public: | ||
| template <typename Tcpu, typename Txpu> | ||
| XPUQuantData quant(const Tcpu* cpu_data, | ||
| const DDimLite& dims, | ||
| bool data_transpose); | ||
|
|
||
| private: | ||
| // cpu data to xpu quant data | ||
| std::unordered_map<size_t, std::pair<XPUScratchPadGuard, XPUScratchPadGuard>> | ||
| weight_cache_; | ||
| }; | ||
|
|
||
| } // namespace lite | ||
| } // namespace paddle |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.