-
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
add inplace api transpose_, t_, normal_,cauchy_, geometric_ #57093
Merged
jeff41404
merged 17 commits into
PaddlePaddle:develop
from
GGBond8488:add_transpose_cauchy_geometric_inpalce_api
Sep 22, 2023
Merged
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
50d23ae
add inplace
GGBond8488 f8d6536
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
GGBond8488 bf46171
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
GGBond8488 f3e3592
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
GGBond8488 4552629
fix transpose inpalce error
GGBond8488 1acd8e8
fix error
GGBond8488 c4741c1
fix
GGBond8488 57f61e1
fix
GGBond8488 3c7b738
add gaussian inpalce kernel
GGBond8488 3319bb9
change cauchy_ gepmetric impl
GGBond8488 be7e52d
fix typro
GGBond8488 abcca20
add test
GGBond8488 0064de3
remove gaussian test
GGBond8488 1b1b1e5
fix sample code error
GGBond8488 b364654
fix sample code
GGBond8488 ec79a41
fix sample code error
GGBond8488 bec77c2
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
GGBond8488 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,41 @@ | ||
/* 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/phi/kernels/gaussian_inplace_grad_kernel.h" | ||
|
||
#include "paddle/phi/core/kernel_registry.h" | ||
|
||
namespace phi { | ||
|
||
template <typename T, typename Context> | ||
void GaussianInplaceGradKernel(const Context& ctx, | ||
const DenseTensor& out_grad UNUSED, | ||
float mean UNUSED, | ||
float std UNUSED, | ||
int seed UNUSED, | ||
DenseTensor* x_grad) { | ||
if (x_grad) { | ||
auto* data = ctx.template Alloc<T>(x_grad); | ||
std::fill(data, data + x_grad->numel(), T(0)); | ||
} | ||
} | ||
|
||
} // namespace phi | ||
|
||
PD_REGISTER_KERNEL(gaussian_inplace_grad, | ||
CPU, | ||
ALL_LAYOUT, | ||
phi::GaussianInplaceGradKernel, | ||
float, | ||
double) {} |
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 |
---|---|---|
|
@@ -48,7 +48,38 @@ void GaussianKernel(const Context& dev_ctx, | |
} | ||
} | ||
|
||
template <typename T, typename Context> | ||
void GaussianInplaceKernel(const Context& dev_ctx, | ||
const DenseTensor& x, | ||
float mean, | ||
float std, | ||
Comment on lines
+54
to
+55
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. if dtype of 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. As above |
||
int seed, | ||
DenseTensor* out) { | ||
T* data = dev_ctx.template Alloc<T>(out); | ||
std::normal_distribution<T> dist(mean, std); | ||
|
||
int64_t size = out->numel(); | ||
std::shared_ptr<std::mt19937_64> engine; | ||
if (seed) { | ||
engine = std::make_shared<std::mt19937_64>(); | ||
engine->seed(seed); | ||
} else { | ||
engine = dev_ctx.GetGenerator()->GetCPUEngine(); | ||
} | ||
|
||
for (int64_t i = 0; i < size; ++i) { | ||
data[i] = dist(*engine); | ||
} | ||
} | ||
|
||
} // namespace phi | ||
|
||
PD_REGISTER_KERNEL( | ||
gaussian, CPU, ALL_LAYOUT, phi::GaussianKernel, float, double) {} | ||
|
||
PD_REGISTER_KERNEL(gaussian_inplace, | ||
CPU, | ||
ALL_LAYOUT, | ||
phi::GaussianInplaceKernel, | ||
float, | ||
double) {} |
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,29 @@ | ||
// 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/phi/core/dense_tensor.h" | ||
|
||
namespace phi { | ||
|
||
template <typename T, typename Context> | ||
void GaussianInplaceGradKernel(const Context& ctx, | ||
const DenseTensor& out_grad, | ||
float mean, | ||
float std, | ||
int seed, | ||
DenseTensor* x_grad); | ||
|
||
} // namespace phi |
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,44 @@ | ||
/* 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. */ | ||
|
||
#include "paddle/phi/kernels/gaussian_inplace_grad_kernel.h" | ||
|
||
#include "paddle/phi/common/amp_type_traits.h" | ||
#include "paddle/phi/core/kernel_registry.h" | ||
#include "paddle/phi/kernels/full_kernel.h" | ||
|
||
namespace phi { | ||
|
||
template <typename T, typename Context> | ||
void GaussianInplaceGradKernel(const Context& ctx, | ||
const DenseTensor& out_grad, | ||
float mean, | ||
float std, | ||
int seed, | ||
DenseTensor* x_grad) { | ||
auto dims = vectorize(x_grad->dims()); | ||
float value = static_cast<float>(0.0f); | ||
phi::FullKernel<T>(ctx, dims, value, phi::DataType::UNDEFINED, x_grad); | ||
} | ||
|
||
} // namespace phi | ||
|
||
PD_REGISTER_KERNEL(gaussian_inplace_grad, | ||
GPU, | ||
ALL_LAYOUT, | ||
phi::GaussianInplaceGradKernel, | ||
float, | ||
double, | ||
phi::dtype::float16, | ||
phi::dtype::bfloat16) {} |
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.
为什么需要新增单独的inplace算子来着?
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.
原有的gassian算子没有输入tensor,如果用gaussian+assign的方式梯度计算会不对
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.
现有的uniform_inplace也是新增的uniform_inpalce算子, uniform+gaussian 可以组合出所有其他的分布,后续用应该也不需要新增其他的这种算子了