-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
【Pten】Support data transform in C++ API #39263
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
793663d
add data_transform in pten api
zyfncg 380ebc4
Merge branch 'develop' of /~https://github.com/PaddlePaddle/Paddle into…
zyfncg 42017ae
Merge branch 'develop' of /~https://github.com/PaddlePaddle/Paddle into…
zyfncg 43cf904
support GetKernelTypeForVar
zyfncg 9ee6fe5
Merge branch 'develop' of /~https://github.com/PaddlePaddle/Paddle into…
zyfncg a00d452
fix complie problem of bfloat16
zyfncg e2ae6cf
change error namespace
zyfncg 45e4f4a
add complex type transform unittest
zyfncg d94aa44
Merge branch 'develop' of /~https://github.com/PaddlePaddle/Paddle into…
zyfncg ab1b225
fix merge conflict
zyfncg 0c55f17
Merge branch 'develop' of /~https://github.com/PaddlePaddle/Paddle into…
zyfncg 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 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 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 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,232 @@ | ||
/* 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/pten/api/lib/data_transform.h" | ||
|
||
#include "paddle/pten/api/ext/dispatch.h" | ||
#include "paddle/pten/api/lib/kernel_dispatch.h" | ||
#include "paddle/pten/backends/all_context.h" | ||
#include "paddle/pten/kernels/cast_kernel.h" | ||
#include "paddle/pten/kernels/transfer_layout_kernel.h" | ||
|
||
#include "paddle/fluid/framework/data_device_transform.h" | ||
|
||
namespace paddle { | ||
namespace experimental { | ||
|
||
inline bool NeedTransformDataType(const DataType& input, | ||
const DataType& target, | ||
const TransformFlag& transform_flag) { | ||
return input != target && | ||
(transform_flag.need_trans_data_type() || | ||
target == DataType::COMPLEX64 || target == DataType::COMPLEX128); | ||
} | ||
|
||
inline bool NeedTransformPlace(const paddle::platform::Place& input, | ||
const Backend& target, | ||
const TransformFlag& transform_flag) { | ||
bool ret = transform_flag.need_trans_backend() && | ||
target != Backend::ALL_BACKEND && | ||
!platform::is_same_place(input, pten::TransToFluidPlace(target)); | ||
return ret; | ||
} | ||
|
||
inline bool NeedTransformLayout(const DataLayout& input, | ||
const DataLayout& target, | ||
const TransformFlag& transform_flag) { | ||
bool ret = transform_flag.need_trans_layout() && | ||
(input != DataLayout::ALL_LAYOUT && | ||
target != DataLayout::ALL_LAYOUT && input != target); | ||
return ret; | ||
} | ||
|
||
inline pten::DenseTensor TransDataLayout(const pten::DenseTensor& tensor, | ||
DataLayout layout) { | ||
auto& pool = paddle::platform::DeviceContextPool::Instance(); | ||
VLOG(3) << "DataLayoutTransform src_layout: " << tensor.layout() | ||
<< " dst_layout: " << layout; | ||
if (platform::is_cpu_place(tensor.place())) { | ||
auto* dev_ctx = static_cast<pten::CPUContext*>(pool.Get(tensor.place())); | ||
return pten::TransferLayout(*dev_ctx, tensor, layout); | ||
} else { | ||
PADDLE_THROW(pten::errors::PreconditionNotMet( | ||
"Unsupported data layout cast from CPU to GPU.")); | ||
} | ||
} | ||
|
||
template <typename Context> | ||
pten::DenseTensor CastDateType(const Context& dev_ctx, | ||
const pten::DenseTensor& tensor, | ||
DataType dtype) { | ||
switch (tensor.dtype()) { | ||
case DataType::FLOAT32: | ||
return pten::Cast<float>(dev_ctx, tensor, dtype); | ||
case DataType::FLOAT64: | ||
return pten::Cast<double>(dev_ctx, tensor, dtype); | ||
case DataType::INT32: | ||
return pten::Cast<int32_t>(dev_ctx, tensor, dtype); | ||
case DataType::INT64: | ||
return pten::Cast<int64_t>(dev_ctx, tensor, dtype); | ||
case DataType::FLOAT16: | ||
return pten::Cast<pten::dtype::float16>(dev_ctx, tensor, dtype); | ||
case DataType::BFLOAT16: | ||
return pten::Cast<pten::dtype::bfloat16>(dev_ctx, tensor, dtype); | ||
case DataType::BOOL: | ||
return pten::Cast<bool>(dev_ctx, tensor, dtype); | ||
case DataType::INT16: | ||
return pten::Cast<int16_t>(dev_ctx, tensor, dtype); | ||
case DataType::UINT8: | ||
return pten::Cast<uint8_t>(dev_ctx, tensor, dtype); | ||
default: | ||
PADDLE_THROW(pten::errors::Unimplemented( | ||
"Data type (%s) is not supported when casting data type.", | ||
tensor.dtype())); | ||
} | ||
} | ||
|
||
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) | ||
pten::DenseTensor CastDateType(const pten::GPUContext& dev_ctx, | ||
const pten::DenseTensor& tensor, | ||
DataType dtype) { | ||
switch (tensor.dtype()) { | ||
case DataType::FLOAT32: | ||
return pten::Cast<float>(dev_ctx, tensor, dtype); | ||
case DataType::FLOAT64: | ||
return pten::Cast<double>(dev_ctx, tensor, dtype); | ||
case DataType::INT32: | ||
return pten::Cast<int32_t>(dev_ctx, tensor, dtype); | ||
case DataType::INT64: | ||
return pten::Cast<int64_t>(dev_ctx, tensor, dtype); | ||
case DataType::FLOAT16: | ||
return pten::Cast<pten::dtype::float16>(dev_ctx, tensor, dtype); | ||
case DataType::BOOL: | ||
return pten::Cast<bool>(dev_ctx, tensor, dtype); | ||
case DataType::INT16: | ||
return pten::Cast<int16_t>(dev_ctx, tensor, dtype); | ||
case DataType::UINT8: | ||
return pten::Cast<uint8_t>(dev_ctx, tensor, dtype); | ||
default: | ||
PADDLE_THROW(pten::errors::Unimplemented( | ||
"Data type (%s) is not supported when casting data type.", | ||
tensor.dtype())); | ||
} | ||
} | ||
#endif | ||
|
||
inline pten::DenseTensor TransDataType(const pten::DenseTensor& tensor, | ||
DataType dtype) { | ||
auto& pool = paddle::platform::DeviceContextPool::Instance(); | ||
|
||
VLOG(3) << "DataTypeTransform src_dtype: " << tensor.dtype() | ||
<< " dst_dtype: " << dtype; | ||
|
||
pten::DenseTensor out( | ||
pten::make_intrusive<paddle::experimental::SharedStorage>(tensor.place()), | ||
{dtype, tensor.dims(), tensor.layout()}); | ||
|
||
if (platform::is_cpu_place(tensor.place())) { | ||
auto* dev_ctx = static_cast<pten::CPUContext*>(pool.Get(tensor.place())); | ||
return CastDateType(*dev_ctx, tensor, dtype); | ||
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) | ||
} else if (platform::is_gpu_place(tensor.place())) { | ||
auto* dev_ctx = static_cast<pten::GPUContext*>(pool.Get(tensor.place())); | ||
return CastDateType(*dev_ctx, tensor, dtype); | ||
#endif | ||
} else { | ||
PADDLE_THROW(pten::errors::Unimplemented( | ||
"Place type is not supported when casting data type.")); | ||
} | ||
return out; | ||
} | ||
|
||
pten::DenseTensor TransformData(const pten::DenseTensor& tensor, | ||
const pten::TensorArgDef& target_args_def, | ||
const TransformFlag& transform_flag) { | ||
pten::DenseTensor out = tensor; | ||
if (NeedTransformLayout( | ||
tensor.layout(), target_args_def.layout, transform_flag)) { | ||
out = TransDataLayout(out, target_args_def.layout); | ||
} | ||
|
||
if (NeedTransformDataType( | ||
tensor.dtype(), target_args_def.dtype, transform_flag)) { | ||
out = TransDataType(out, target_args_def.dtype); | ||
} | ||
|
||
if (NeedTransformPlace( | ||
out.place(), target_args_def.backend, transform_flag)) { | ||
pten::DenseTensor result( | ||
pten::make_intrusive<paddle::experimental::SharedStorage>( | ||
pten::TransToFluidPlace(target_args_def.backend)), | ||
{out.dtype(), out.dims(), out.layout()}); | ||
framework::TransDataDevice( | ||
out, pten::TransToFluidPlace(target_args_def.backend), &result); | ||
out = result; | ||
} | ||
return out; | ||
} | ||
|
||
std::shared_ptr<pten::DenseTensor> PrepareData( | ||
const Tensor& input, | ||
const pten::TensorArgDef& target_args_def, | ||
const TransformFlag& transform_flag) { | ||
const auto& tensor_in = input.impl(); | ||
if (!transform_flag.NeedTransform() || !tensor_in->initialized() || | ||
(!NeedTransformPlace( | ||
tensor_in->place(), target_args_def.backend, transform_flag) && | ||
!NeedTransformDataType( | ||
tensor_in->dtype(), target_args_def.dtype, transform_flag) && | ||
!NeedTransformLayout( | ||
tensor_in->layout(), target_args_def.layout, transform_flag))) { | ||
return std::dynamic_pointer_cast<pten::DenseTensor>(tensor_in); | ||
} | ||
|
||
pten::DenseTensor out = | ||
TransformData(*(static_cast<pten::DenseTensor*>(tensor_in.get())), | ||
target_args_def, | ||
transform_flag); | ||
return std::make_shared<pten::DenseTensor>(out); | ||
} | ||
|
||
std::unique_ptr<std::vector<pten::DenseTensor>> PrepareData( | ||
const std::vector<Tensor>& inputs, | ||
const pten::TensorArgDef& target_args_def, | ||
const TransformFlag& transform_flag) { | ||
auto pt_tensors = std::make_unique<std::vector<pten::DenseTensor>>(); | ||
pt_tensors->reserve(inputs.size()); | ||
|
||
for (const auto& input : inputs) { | ||
const auto& tensor_in = input.impl(); | ||
if (!transform_flag.NeedTransform() || !tensor_in->initialized() || | ||
(!NeedTransformPlace( | ||
tensor_in->place(), target_args_def.backend, transform_flag) && | ||
!NeedTransformDataType( | ||
tensor_in->dtype(), target_args_def.dtype, transform_flag) && | ||
!NeedTransformLayout( | ||
tensor_in->layout(), target_args_def.layout, transform_flag))) { | ||
pt_tensors->emplace_back( | ||
*std::dynamic_pointer_cast<pten::DenseTensor>(tensor_in)); | ||
} else { | ||
pt_tensors->emplace_back( | ||
TransformData(*(static_cast<pten::DenseTensor*>(tensor_in.get())), | ||
target_args_def, | ||
transform_flag)); | ||
} | ||
} | ||
|
||
return std::move(pt_tensors); | ||
} | ||
|
||
} // namespace experimental | ||
} // namespace paddle |
This file contains 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,75 @@ | ||
/* 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/pten/api/include/tensor.h" | ||
#include "paddle/pten/core/kernel_factory.h" | ||
|
||
namespace paddle { | ||
namespace experimental { | ||
|
||
class TransformFlag { | ||
public: | ||
TransformFlag(bool stop_transform = false, | ||
bool trans_dtype = false, | ||
bool trans_backend = true, | ||
bool trans_layout = true) | ||
: stop_transform_(stop_transform), | ||
trans_data_type_(trans_dtype), | ||
trans_backend_(trans_backend), | ||
trans_layout_(trans_layout) {} | ||
|
||
bool NeedTransform() const { | ||
return !stop_transform_ && | ||
(trans_data_type_ || trans_backend_ || trans_layout_); | ||
} | ||
|
||
bool need_trans_data_type() const { | ||
return !stop_transform_ && trans_data_type_; | ||
} | ||
|
||
bool need_trans_backend() const { return !stop_transform_ && trans_backend_; } | ||
|
||
bool need_trans_layout() const { return !stop_transform_ && trans_layout_; } | ||
|
||
private: | ||
// This is the highest priority in flags, | ||
// and can be setted by api[data_transform->skip_transform] in the yaml file. | ||
bool stop_transform_ = false; | ||
|
||
// trans_data_type_ can be setted by api[data_transform->support_trans_dtype] | ||
// in the yaml file. | ||
// trans_data_type_ only affect the non complex types, | ||
// the complex is always transferd, except stop_transform_ is true. | ||
bool trans_data_type_ = false; | ||
|
||
// trans_backend_ and trans_layout_ are true defalutly, | ||
// and they can only be setted by global flag. | ||
bool trans_backend_ = true; | ||
bool trans_layout_ = true; | ||
}; | ||
|
||
std::shared_ptr<pten::DenseTensor> PrepareData( | ||
const Tensor& input, | ||
const pten::TensorArgDef& target_args_def, | ||
const TransformFlag& transform_flag); | ||
|
||
std::unique_ptr<std::vector<pten::DenseTensor>> PrepareData( | ||
const std::vector<Tensor>& inputs, | ||
const pten::TensorArgDef& target_args_def, | ||
const TransformFlag& transform_flag); | ||
|
||
} // namespace experimental | ||
} // namespace paddle |
This file contains 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 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 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 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
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.
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.
这里为什么需要单独重载一个函数
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.
GPU的
Cast
不支持bfloat16类型,所以单独分出来处理了