-
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】Add dot and matmul grad kernel in pten #38713
Merged
Merged
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
76608b5
refactor matmul directory in pten
zyfncg beecda8
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 43b3267
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 63d1681
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 8cfb873
fix merge conflict
zyfncg 2dffa46
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 0f3c6d1
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 2f077b4
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg d6217d2
add dot_grad kernel
zyfncg dc01e07
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg a3658f5
add dot_grad kernel in pten
zyfncg 41c0b85
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 682059d
add matmul_grad kernel
zyfncg 6a5c6b9
Merge commit 'refs/pull/38441/head' of https://github.com/PaddlePaddl…
zyfncg a8830fc
Merge commit 'refs/pull/38227/head' of https://github.com/PaddlePaddl…
zyfncg 0846c48
update the code
zyfncg 83d7207
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 0d0f654
delete useless code in fluid
zyfncg e2e4af0
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 854c6fb
fix some bug of running matmul grad kernel
zyfncg 8b60eaa
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg 3c955bd
fix merge conflict
zyfncg ab5c095
refactor some code
zyfncg bc8a80b
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
zyfncg c113ab5
refactor code
zyfncg fc13a47
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
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 |
---|---|---|
|
@@ -338,19 +338,41 @@ static void BuildDygraphPtenKernelContext( | |
|
||
for (size_t i = 0; i < output_names.size(); ++i) { | ||
auto& out_def = output_defs.at(i); | ||
auto& outs_vector = outs.at(output_names[i]); | ||
|
||
size_t start_idx = (i == 0 ? 0 : kernel_ctx->OutputRangeAt(i - 1).second); | ||
size_t end_idx = start_idx + outs_vector.size(); | ||
auto current_vector_size = kernel_ctx->OutputsSize(); | ||
|
||
auto iter = outs.find(output_names[i]); | ||
if (iter == outs.end()) { | ||
if (current_vector_size > start_idx) { | ||
kernel_ctx->SetOutputWithoutSetRange(start_idx, {nullptr}); | ||
} else { | ||
kernel_ctx->EmplaceBackOutputWithoutSetRange({nullptr}); | ||
} | ||
kernel_ctx->AssignOutputRange(std::make_pair(start_idx, start_idx + 1), | ||
i); | ||
continue; | ||
} | ||
|
||
auto& outs_vector = iter->second; | ||
size_t end_idx = start_idx + outs_vector.size(); | ||
|
||
// If the memory needed is less than the current memory allocated, we will | ||
// reuse the current memory by using ReMakePtenDenseTensorFromVar. | ||
// Otherwise,we will create new storage. | ||
for (size_t offset = 0; offset < outs_vector.size(); ++offset) { | ||
if (current_vector_size > start_idx + offset) { | ||
experimental::ReMakePtenDenseTensorFromVar( | ||
outs_vector[offset]->MutableVar(), out_def, | ||
kernel_ctx->MutableOutputAt<pten::DenseTensor>(start_idx + offset)); | ||
auto* buffer_tensor = | ||
kernel_ctx->MutableOutputAt<pten::DenseTensor>(start_idx + offset); | ||
if (buffer_tensor) { | ||
experimental::ReMakePtenDenseTensorFromVar( | ||
outs_vector[offset]->MutableVar(), out_def, buffer_tensor); | ||
} else { | ||
kernel_ctx->SetOutputWithoutSetRange( | ||
start_idx + offset, | ||
experimental::MakePtenTensorBaseFromVar( | ||
outs_vector[offset]->MutableVar(), out_def)); | ||
} | ||
} else { | ||
kernel_ctx->EmplaceBackOutputWithoutSetRange( | ||
experimental::MakePtenTensorBaseFromVar( | ||
|
@@ -465,15 +487,18 @@ static void WriteBackToOutputs( | |
auto& output_names = std::get<2>(pt_kernel_signature.args); | ||
|
||
for (size_t i = 0; i < output_names.size(); ++i) { | ||
auto& outs_vector = outs.at(output_names[i]); | ||
auto iter = outs.find(output_names[i]); | ||
if (iter != outs.end()) { | ||
auto& outs_vector = iter->second; | ||
|
||
auto& range_pair = kernel_ctx->OutputRangeAt(i); | ||
auto pten_outs = kernel_ctx->MutableOutputBetween<pten::DenseTensor>( | ||
range_pair.first, range_pair.second); | ||
auto& range_pair = kernel_ctx->OutputRangeAt(i); | ||
auto pten_outs = kernel_ctx->MutableOutputBetween<pten::DenseTensor>( | ||
range_pair.first, range_pair.second); | ||
|
||
for (size_t j = 0; j < pten_outs.size(); ++j) { | ||
experimental::MakeVariableFromPtenTensor(pten_outs[j], | ||
outs_vector[j]->MutableVar()); | ||
for (size_t j = 0; j < pten_outs.size(); ++j) { | ||
experimental::MakeVariableFromPtenTensor(pten_outs[j], | ||
outs_vector[j]->MutableVar()); | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -530,6 +555,7 @@ static void PreparedOpRunImpl( | |
template <typename VarType> | ||
static void PreparedOpRunPtImpl( | ||
const framework::OperatorBase& op, | ||
const framework::OpKernelType& kernel_type, | ||
const framework::KernelSignature& pt_kernel_signature, | ||
const pten::Kernel& pt_kernel, pten::KernelContext* pt_kernel_context, | ||
platform::DeviceContext* dev_ctx, const NameVarMap<VarType>& ins, | ||
|
@@ -560,17 +586,19 @@ static void PreparedOpRunPtImpl( | |
pt_kernel_context->ClearData(); | ||
|
||
// TODO(chenweihang): add debug flags later | ||
// TODO(chenweihang): deal with complex cases later | ||
if (framework::IsComplexType(kernel_type.data_type_)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里是否可以使用pten_kernel的data type There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 由于传入的 |
||
HandleComplexGradToRealGrad<VarType>(outs); | ||
} | ||
} | ||
|
||
void PreparedOp::Run(const NameVarMap<VarBase>& ins, | ||
const NameVarMap<VarBase>& outs, | ||
const framework::AttributeMap& attrs, | ||
const framework::AttributeMap& default_attrs) { | ||
if (run_pten_kernel_) { | ||
PreparedOpRunPtImpl<VarBase>(op_, pt_kernel_signature_, pt_kernel_, | ||
pt_kernel_context_, dev_ctx_, ins, outs, attrs, | ||
default_attrs); | ||
PreparedOpRunPtImpl<VarBase>(op_, kernel_type_, pt_kernel_signature_, | ||
pt_kernel_, pt_kernel_context_, dev_ctx_, ins, | ||
outs, attrs, default_attrs); | ||
} else { | ||
PreparedOpRunImpl<VarBase>(op_, ctx_, kernel_type_, func_, dev_ctx_, ins, | ||
outs, attrs, default_attrs); | ||
|
@@ -582,9 +610,9 @@ void PreparedOp::Run(const NameVarMap<VariableWrapper>& ins, | |
const framework::AttributeMap& attrs, | ||
const framework::AttributeMap& default_attrs) { | ||
if (run_pten_kernel_) { | ||
PreparedOpRunPtImpl<VariableWrapper>(op_, pt_kernel_signature_, pt_kernel_, | ||
pt_kernel_context_, dev_ctx_, ins, | ||
outs, attrs, default_attrs); | ||
PreparedOpRunPtImpl<VariableWrapper>( | ||
op_, kernel_type_, pt_kernel_signature_, pt_kernel_, pt_kernel_context_, | ||
dev_ctx_, ins, outs, attrs, default_attrs); | ||
} else { | ||
PreparedOpRunImpl<VariableWrapper>(op_, ctx_, kernel_type_, func_, dev_ctx_, | ||
ins, outs, attrs, default_attrs); | ||
|
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.
Done