Skip to content

Commit

Permalink
【Hackathon 6th Fundable Projects 3 No.241】 [fluid_ops] straight_throu…
Browse files Browse the repository at this point in the history
…gh_estimator_grad (#67299)
  • Loading branch information
co63oc authored Aug 12, 2024
1 parent d483bf8 commit d815238
Show file tree
Hide file tree
Showing 16 changed files with 278 additions and 164 deletions.
10 changes: 0 additions & 10 deletions paddle/fluid/operators/fake_quantize_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,9 @@ REGISTER_OPERATOR(
ops::MovingAverageAbsMaxScaleOpMaker,
ops::StraightThroughEstimatorMaker<paddle::framework::OpDesc>,
ops::StraightThroughEstimatorMaker<paddle::imperative::OpBase>);
PD_REGISTER_STRUCT_KERNEL(moving_average_abs_max_scale,
CPU,
ALL_LAYOUT,
ops::MovingAverageAbsMaxScaleKernel,
float) {}

REGISTER_OPERATOR(straight_through_estimator_grad,
ops::StraightThroughEstimatorGradOp);
PD_REGISTER_STRUCT_KERNEL(straight_through_estimator_grad,
CPU,
ALL_LAYOUT,
ops::StraightThroughEstimatorGradKernel,
float) {}

REGISTER_OP_VERSION(fake_channel_wise_quantize_abs_max)
.AddCheckpoint(
Expand Down
32 changes: 0 additions & 32 deletions paddle/fluid/operators/fake_quantize_op.cu

This file was deleted.

120 changes: 0 additions & 120 deletions paddle/fluid/operators/fake_quantize_op.cu.h

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2024 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/core/compat/op_utils.h"

namespace phi {

// we have to return every specific KernelSignature for inference now
KernelSignature MovingAverageAbsMaxScaleOpArgumentMapping(
const ArgumentMappingContext& ctx) {
return KernelSignature("moving_average_abs_max_scale",
{"X", "InAccum", "InState"},
{"moving_rate", "is_test"},
{"Out", "OutScale", "OutState", "OutAccum"});
}

} // namespace phi

PD_REGISTER_ARG_MAPPING_FN(moving_average_abs_max_scale,
phi::MovingAverageAbsMaxScaleOpArgumentMapping);
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2024 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/core/compat/op_utils.h"

namespace phi {

KernelSignature StraightThroughEstimatorGradOpArgumentMapping(
const ArgumentMappingContext& ctx) {
return KernelSignature(
"straight_through_estimator_grad", {"Out@GRAD"}, {}, {"X@GRAD"});
}

} // namespace phi

PD_REGISTER_ARG_MAPPING_FN(straight_through_estimator_grad,
phi::StraightThroughEstimatorGradOpArgumentMapping);
2 changes: 0 additions & 2 deletions paddle/fluid/pir/dialect/operator/utils/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ const std::unordered_set<std::string> LegacyOpList = {
PushDenseOp::name(),
SoftReluOp::name(),
SoftReluGradOp::name(),
MovingAverageAbsMaxScaleOp::name(),
MovingAverageAbsMaxScale_Op::name(),
CReduceAvgOp::name(),
CReduceAvg_Op::name(),
CReduceMaxOp::name(),
Expand Down
6 changes: 6 additions & 0 deletions paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5990,6 +5990,12 @@ void StridedUnChangedInferMeta(const MetaTensor& x, MetaTensor* out) {
out->set_strides(x.strides());
}

void StraightThroughEstimatorInferMeta(const MetaTensor& out_grad,
MetaTensor* x_grad) {
x_grad->set_dims(out_grad.dims());
x_grad->set_dtype(out_grad.dtype());
}

void NumberCountInferMeta(const MetaTensor& x,
int upper_range,
MetaTensor* out) {
Expand Down
3 changes: 3 additions & 0 deletions paddle/phi/infermeta/unary.h
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,9 @@ void NumberCountInferMeta(const MetaTensor& x,

void StridedUnChangedInferMeta(const MetaTensor& x, MetaTensor* out);

void StraightThroughEstimatorInferMeta(const MetaTensor& out_grad,
MetaTensor* x_grad);

void LrnInferMeta(const MetaTensor& x,
int n,
MetaTensor* out,
Expand Down
22 changes: 22 additions & 0 deletions paddle/phi/kernels/cpu/moving_average_abs_max_scale_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2024 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/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/moving_average_abs_max_scale_kernel_impl.h"

PD_REGISTER_KERNEL(moving_average_abs_max_scale,
CPU,
ALL_LAYOUT,
phi::MovingAverageAbsMaxScaleKernel,
float) {}
21 changes: 21 additions & 0 deletions paddle/phi/kernels/cpu/straight_through_estimator_grad_kernel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2024 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/impl/straight_through_estimator_grad_kernel_impl.h"

PD_REGISTER_KERNEL(straight_through_estimator_grad,
CPU,
ALL_LAYOUT,
phi::StraightThroughEstimatorGradKernel,
float) {}
23 changes: 23 additions & 0 deletions paddle/phi/kernels/gpu/moving_average_abs_max_scale_kernel.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2024 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/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/moving_average_abs_max_scale_kernel_impl.h"

PD_REGISTER_KERNEL(moving_average_abs_max_scale,
GPU,
ALL_LAYOUT,
phi::MovingAverageAbsMaxScaleKernel,
float,
phi::dtype::float16) {}
22 changes: 22 additions & 0 deletions paddle/phi/kernels/gpu/straight_through_estimator_grad_kernel.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2024 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/impl/straight_through_estimator_grad_kernel_impl.h"

PD_REGISTER_KERNEL(straight_through_estimator_grad,
GPU,
ALL_LAYOUT,
phi::StraightThroughEstimatorGradKernel,
float,
phi::dtype::float16) {}
Loading

0 comments on commit d815238

Please sign in to comment.