Skip to content
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 average accumulates migration #44554

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def SkipAPIGeneration(forward_api_name):
'adam',
'adamw_',
'adamw',
'average_accumulates',
'average_accumulates_',
'decayed_adagrad_',
'decayed_adagrad',
'dgc_momentum_',
Expand Down
100 changes: 11 additions & 89 deletions paddle/fluid/operators/average_accumulates_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,99 +12,19 @@ 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/operators/average_accumulates_op.h"
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"

#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/phi/infermeta/multiary.h"

namespace paddle {
namespace operators {

template <>
void GetAccumulators<phi::CPUContext>(const framework::ExecutionContext& ctx,
int64_t* num_updates,
int64_t* num_accumulates,
int64_t* old_num_accumulates) {
auto* in_old_num_accumulates = ctx.Input<Tensor>("in_old_num_accumulates");
auto* in_num_accumulates = ctx.Input<Tensor>("in_num_accumulates");
auto* in_num_updates = ctx.Input<Tensor>("in_num_updates");

*old_num_accumulates = in_old_num_accumulates->data<int64_t>()[0];
*num_accumulates = in_num_accumulates->data<int64_t>()[0];
*num_updates = in_num_updates->data<int64_t>()[0];
}

template <>
void SetAccumulators<phi::CPUContext>(const framework::ExecutionContext& ctx,
int64_t num_updates,
int64_t num_accumulates,
int64_t old_num_accumulates) {
auto* out_old_num_accumulates = ctx.Output<Tensor>("out_old_num_accumulates");
auto* out_num_accumulates = ctx.Output<Tensor>("out_num_accumulates");
auto* out_num_updates = ctx.Output<Tensor>("out_num_updates");

out_old_num_accumulates->data<int64_t>()[0] = old_num_accumulates;
out_num_accumulates->data<int64_t>()[0] = num_accumulates;
out_num_updates->data<int64_t>()[0] = num_updates;
}

class AverageAccumulatesOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;

void InferShape(framework::InferShapeContext* ctx) const override {
OP_INOUT_CHECK(
ctx->HasInput("param"), "Input", "param", "AverageAccumulates");
OP_INOUT_CHECK(
ctx->HasInput("in_sum_1"), "Input", "in_sum_1", "AverageAccumulates");
OP_INOUT_CHECK(
ctx->HasInput("in_sum_2"), "Input", "in_sum_2", "AverageAccumulates");
OP_INOUT_CHECK(
ctx->HasInput("in_sum_3"), "Input", "in_sum_3", "AverageAccumulates");
OP_INOUT_CHECK(ctx->HasInput("in_num_accumulates"),
"Input",
"in_num_accumulates",
"AverageAccumulates");
OP_INOUT_CHECK(ctx->HasInput("in_old_num_accumulates"),
"Input",
"in_old_num_accumulates",
"AverageAccumulates");
OP_INOUT_CHECK(ctx->HasInput("in_num_updates"),
"Input",
"in_num_updates",
"AverageAccumulates");

OP_INOUT_CHECK(ctx->HasOutput("out_sum_1"),
"Output",
"out_sum_1",
"AverageAccumulates");
OP_INOUT_CHECK(ctx->HasOutput("out_sum_2"),
"Output",
"out_sum_2",
"AverageAccumulates");
OP_INOUT_CHECK(ctx->HasOutput("out_sum_3"),
"Output",
"out_sum_3",
"AverageAccumulates");
OP_INOUT_CHECK(ctx->HasOutput("out_num_accumulates"),
"Output",
"out_num_accumulates",
"AverageAccumulates");
OP_INOUT_CHECK(ctx->HasOutput("out_old_num_accumulates"),
"Output",
"out_old_num_accumulates",
"AverageAccumulates");
OP_INOUT_CHECK(ctx->HasOutput("out_num_updates"),
"Output",
"out_num_updates",
"AverageAccumulates");
auto in_dim = ctx->GetInputDim("param");

ctx->SetOutputDim("out_sum_1", in_dim);
ctx->SetOutputDim("out_sum_2", in_dim);
ctx->SetOutputDim("out_sum_3", in_dim);
ctx->SetOutputDim("out_num_accumulates", {1});
ctx->SetOutputDim("out_old_num_accumulates", {1});
ctx->SetOutputDim("out_num_updates", {1});
}

protected:
framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext& ctx) const override {
Expand Down Expand Up @@ -209,12 +129,14 @@ And for a mini-batch in training, accumulators were computed as below steps:
} // namespace paddle

namespace ops = paddle::operators;
DECLARE_INFER_SHAPE_FUNCTOR(average_accumulates,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fluid下的kernel和infershape为啥没有删掉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

删除fluid下kernel和infershape后不知道为何会报编译错误,我可以再试一下

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已删除fluid下的kernel和infershape

AverageAccumulatesInferShapeFunctor,
PD_INFER_META(phi::AverageAccumulatesInferMeta));

REGISTER_OPERATOR(
average_accumulates,
ops::AverageAccumulatesOp,
ops::AverageAccumulatesOpMaker,
paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);
REGISTER_OP_CPU_KERNEL(average_accumulates,
ops::AverageAccumulatesKernel<phi::CPUContext, float>,
ops::AverageAccumulatesKernel<phi::CPUContext, double>);
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
AverageAccumulatesInferShapeFunctor);
90 changes: 0 additions & 90 deletions paddle/fluid/operators/average_accumulates_op.cu

This file was deleted.

119 changes: 0 additions & 119 deletions paddle/fluid/operators/average_accumulates_op.h

This file was deleted.

11 changes: 11 additions & 0 deletions paddle/phi/api/yaml/legacy_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@
kernel :
func : auc

#average_accumulates
- api : average_accumulates_
args : (Tensor param, Tensor in_sum_1, Tensor in_sum_2, Tensor in_sum_3, Tensor in_num_accumulates, Tensor in_old_num_accumulates, Tensor in_num_updates, float average_window, int64_t max_average_window, int64_t min_average_window)
output : Tensor(out_sum_1), Tensor(out_sum_2), Tensor(out_sum_3), Tensor(out_num_accumulates), Tensor(out_old_num_accumulates), Tensor(out_num_updates)
infer_meta:
func : AverageAccumulatesInferMeta
kernel :
func : average_accumulates {dense, dense, dense, dense, dense ,dense, dense -> dense, dense, dense, dense, dense, dense}
data_type : param
inplace : (in_sum_1 -> out_sum_1), (in_sum_2 -> out_sum_2), (in_sum_3 -> out_sum_3), (in_num_accumulates -> out_num_accumulates), (in_old_num_accumulates -> out_old_num_accumulates), (in_num_updates -> out_num_updates)

# batch_norm
- api : batch_norm
args : (Tensor x, Tensor scale, Tensor bias, Tensor mean, Tensor variance, float momentum, float epsilon, str data_layout, bool is_test, bool use_global_stats, bool trainable_statistics, bool fuse_with_relu)
Expand Down
Loading