-
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
[phi decoupling] remove variable.h in phi #50407
Merged
Merged
Changes from 17 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
64861e6
move variable_utils from phi_api_utils to fluid
huangjiyi 62b6484
fix coment
huangjiyi 2ecd4e8
update include
huangjiyi 4c5c360
fix bugs
huangjiyi dcbe0f8
fix bugs
huangjiyi 4a84302
fix bugs
huangjiyi 3a331d8
fix bugs
huangjiyi feb1d06
fix bugs
huangjiyi f279175
update
huangjiyi 1e6b63c
update
huangjiyi 510ccbc
fix CI-Windows-OpenBLAS
huangjiyi 274c036
fix bugs
huangjiyi cd5154a
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
huangjiyi ef8d31f
fix bugs
huangjiyi d6ca1ab
fix bugs
huangjiyi d6417c9
Merge branch 'move_variable' of https://github.com/huangjiyi/Paddle i…
huangjiyi bee9944
update include
huangjiyi 71d5bc9
move variable_utils to phi_utils
huangjiyi 8211a96
fix namespace
huangjiyi ba10b39
Merge branch 'PaddlePaddle:develop' into move_variable
huangjiyi 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
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,122 @@ | ||
// Copyright (c) 2023 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/fluid/framework/variable_utils.h" | ||
|
||
#include "paddle/fluid/framework/variable.h" | ||
#include "paddle/phi/api/lib/utils/allocator.h" | ||
#include "paddle/phi/core/compat/convert_utils.h" | ||
#include "paddle/phi/core/kernel_factory.h" | ||
#include "paddle/phi/core/tensor_utils.h" | ||
|
||
namespace paddle { | ||
namespace experimental { | ||
|
||
phi::Scalar MakePhiScalarFromVar(const framework::Variable& variable) { | ||
auto expected_place = phi::TransToPhiPlace(phi::Backend::CPU); | ||
if (variable.IsType<phi::DenseTensor>()) { | ||
const auto& tensor = variable.Get<phi::DenseTensor>(); | ||
PADDLE_ENFORCE_EQ( | ||
tensor.numel(), | ||
1UL, | ||
platform::errors::InvalidArgument("The DenseTensor used to construct " | ||
"the Scalar contains more than 1 " | ||
"value, it contains `%d` values.", | ||
tensor.numel())); | ||
if (!platform::is_same_place(tensor.place(), expected_place)) { | ||
phi::DenseTensor tmp_tensor; | ||
framework::TensorCopySync(tensor, expected_place, &tmp_tensor); | ||
return {tmp_tensor}; | ||
} else { | ||
return {tensor}; | ||
} | ||
} else { | ||
PADDLE_THROW(platform::errors::Unimplemented( | ||
"Unsupport casting input `%s` type to Scalar when call pt " | ||
"kernel.", | ||
framework::ToTypeName(variable.Type()))); | ||
} | ||
} | ||
|
||
phi::IntArray MakePhiIntArrayFromVar(const framework::Variable& variable) { | ||
if (variable.IsType<phi::DenseTensor>()) { | ||
const auto& tensor = variable.Get<phi::DenseTensor>(); | ||
return MakePhiIntArray(tensor); | ||
} else { | ||
PADDLE_THROW(platform::errors::Unimplemented( | ||
"Unsupport casting input `%s` type to IntArray when call pt " | ||
"kernel.", | ||
framework::ToTypeName(variable.Type()))); | ||
} | ||
} | ||
|
||
// TODO(chentianyu03): Inplace with IntArray constructor | ||
phi::IntArray MakePhiIntArrayFromVarList( | ||
const std::vector<framework::Variable*>& variable_list) { | ||
if (variable_list.size() == 0) { | ||
return phi::IntArray(); | ||
} | ||
auto expected_place = phi::TransToPhiPlace(phi::Backend::CPU); | ||
|
||
std::vector<int64_t> vector_data; | ||
vector_data.reserve(variable_list.size()); | ||
|
||
for (auto* var : variable_list) { | ||
paddle::experimental::DataType data_type; | ||
if (var->IsType<phi::DenseTensor>()) { | ||
const auto& tensor = var->Get<phi::DenseTensor>(); | ||
data_type = tensor.dtype(); | ||
if (data_type == paddle::experimental::DataType::INT64) { | ||
const auto& tensor = var->Get<phi::DenseTensor>(); | ||
if (tensor.IsInitialized() && | ||
!platform::is_same_place(tensor.place(), expected_place)) { | ||
phi::DenseTensor tmp_tensor; | ||
framework::TensorCopySync(tensor, expected_place, &tmp_tensor); | ||
vector_data.push_back(*tmp_tensor.data<int64_t>()); | ||
} else { | ||
vector_data.push_back(*tensor.data<int64_t>()); | ||
} | ||
} else if (data_type == paddle::experimental::DataType::INT32) { | ||
const auto& tensor = var->Get<phi::DenseTensor>(); | ||
if (tensor.IsInitialized() && | ||
!platform::is_same_place(tensor.place(), expected_place)) { | ||
phi::DenseTensor tmp_tensor; | ||
framework::TensorCopySync(tensor, expected_place, &tmp_tensor); | ||
vector_data.push_back(*tmp_tensor.data<int32_t>()); | ||
} else { | ||
vector_data.push_back(*tensor.data<int32_t>()); | ||
} | ||
} else { | ||
PADDLE_THROW(phi::errors::InvalidArgument( | ||
"Data type error. When cast a LoDTensor to VectorTensor, " | ||
"the data type of LoDTensor must be int32 or int64, " | ||
"but now data type is %s.", | ||
data_type)); | ||
} | ||
} else { | ||
PADDLE_THROW(phi::errors::Unimplemented( | ||
"Unsupport casting input `%s` type to VectorTensor when call pt " | ||
"kernel.", | ||
framework::ToTypeName(var->Type()))); | ||
} | ||
} | ||
|
||
phi::IntArray result{vector_data}; | ||
result.SetFromTensor(true); | ||
|
||
return result; | ||
} | ||
|
||
} // 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,32 @@ | ||
// Copyright (c) 2023 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/fluid/framework/variable.h" | ||
#include "paddle/phi/api/lib/utils/tensor_utils.h" | ||
#include "paddle/phi/common/scalar.h" | ||
|
||
namespace paddle { | ||
namespace experimental { | ||
|
||
phi::Scalar MakePhiScalarFromVar(const framework::Variable& variable); | ||
|
||
phi::IntArray MakePhiIntArrayFromVar(const framework::Variable& variable); | ||
|
||
// TODO(chentianyu03): Inplace with IntArray constructor | ||
phi::IntArray MakePhiIntArrayFromVarList( | ||
const std::vector<framework::Variable*>& variable_list); | ||
|
||
} // 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
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
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
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.
把这个文件尝试合入到phi_utils.h里吧,感觉又多了一个utils文件有点重复了
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.
Done