diff --git a/paddle/fluid/operators/pull_sparse_v2_op.cc b/paddle/fluid/operators/pull_sparse_v2_op.cc deleted file mode 100644 index 05875ff7d1d49f..00000000000000 --- a/paddle/fluid/operators/pull_sparse_v2_op.cc +++ /dev/null @@ -1,141 +0,0 @@ -// Copyright (c) 2020 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/operators/pull_sparse_v2_op.h" - -#include - -namespace paddle { -namespace operators { - -class PullSparseV2Op : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - void InferShape(framework::InferShapeContext* ctx) const override { - PADDLE_ENFORCE_GE(ctx->Inputs("Ids").size(), - 1UL, - common::errors::InvalidArgument( - "Input(Ids) of PullSparseV2Op can not be null")); - PADDLE_ENFORCE_GE(ctx->Outputs("Out").size(), - 1UL, - common::errors::InvalidArgument( - "Output(Out) of PullSparseV2Op can not be null")); - - auto hidden_size = - static_cast(ctx->Attrs().Get("EmbeddingDim")); - auto all_ids_dim = ctx->GetInputsDim("Ids"); - const size_t n_ids = all_ids_dim.size(); - std::vector outs_dims; - outs_dims.resize(n_ids); - for (size_t i = 0; i < n_ids; ++i) { - const auto ids_dims = all_ids_dim[i]; - auto out_dim = common::vectorize(ids_dims); - out_dim.push_back(hidden_size); - outs_dims[i] = common::make_ddim(out_dim); - } - ctx->SetOutputsDim("Out", outs_dims); - for (size_t i = 0; i < n_ids; ++i) { - ctx->ShareLoD("Ids", "Out", i, i); - } - } - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext& ctx) const override { - return phi::KernelKey(framework::proto::VarType::FP32, ctx.GetPlace()); - } -}; - -class PullSparseV2OpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput("Ids", - "Input tensors with type int64 contains " - "the ids to be looked up in PSLib. ") - .AsDuplicable(); - AddInput("W", "The lookup table tensors.").AsDuplicable(); - AddOutput("Out", "The lookup results tensors.").AsDuplicable(); - AddAttr("EmbeddingDim", "(int, the embedding hidden size") - .SetDefault(11); - AddAttr("TableId", "(int, the table id of this embedding") - .SetDefault(0); - AddAttr("AccessorClass", "(string, the class name of accessor") - .SetDefault(""); - AddAttr("CtrLabelName", "(string, ctr label name") - .SetDefault(""); - AddAttr("PaddingId", "(int, the padding id of this embedding") - .SetDefault(0); - AddAttr("ScaleSparseGrad", - "(bool, whether scale sparse gradient with batch size") - .SetDefault(true); - AddAttr>("InputNames", "(vector, slot names") - .SetDefault(std::vector()); - AddAttr("is_distributed", "(bool, it must be true").SetDefault(true); - AddComment(R"DOC( -Pull Sparse V2 Operator. - -This operator is used to perform lookups on the PSLib -then concatenated into a dense tensor. - -The input Ids can carry the LoD (Level of Details) information, -or not. And the output only shares the LoD information with input Ids. - -)DOC"); - } -}; - -template -class PushSparseV2OpMaker : public framework::SingleGradOpMaker { - public: - using framework::SingleGradOpMaker::SingleGradOpMaker; - - protected: - void Apply(GradOpPtr retv) const override { - retv->SetType("push_sparse_v2"); - retv->SetInput("Ids", this->Input("Ids")); - retv->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out")); - retv->SetInput("W", this->Input("W")); - retv->SetOutput(framework::GradVarName("Out"), this->OutputGrad("Out")); - retv->SetAttrMap(this->Attrs()); - } -}; - -class PushSparseV2Op : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - void InferShape(framework::InferShapeContext* ctx) const override {} - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext& ctx) const override { - return phi::KernelKey(OperatorWithKernel::IndicateVarDataType( - ctx, framework::GradVarName("Out")), - ctx.GetPlace()); - } -}; -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; -REGISTER_OPERATOR(pull_sparse_v2, - ops::PullSparseV2Op, - ops::PullSparseV2OpMaker, - ops::PushSparseV2OpMaker, - ops::PushSparseV2OpMaker); -REGISTER_OPERATOR(push_sparse_v2, ops::PushSparseV2Op); -PD_REGISTER_STRUCT_KERNEL( - pull_sparse_v2, CPU, ALL_LAYOUT, ops::PullSparseV2CPUKernel, float) {} -PD_REGISTER_STRUCT_KERNEL( - push_sparse_v2, CPU, ALL_LAYOUT, ops::PushSparseV2CPUKernel, float) {} diff --git a/paddle/fluid/operators/pull_sparse_v2_op.h b/paddle/fluid/operators/pull_sparse_v2_op.h deleted file mode 100644 index 6dbf7f3bcb5e0f..00000000000000 --- a/paddle/fluid/operators/pull_sparse_v2_op.h +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright (c) 2020 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 -#include -#include - -#include "paddle/fluid/framework/fleet/fleet_wrapper.h" -#include "paddle/fluid/framework/op_registry.h" -#include "paddle/fluid/framework/tensor.h" - -namespace paddle { -namespace operators { - -template -void PullSparseFunctor(const framework::ExecutionContext& ctx) { - auto inputs = ctx.MultiInput("Ids"); - auto outputs = ctx.MultiOutput("Out"); - uint32_t fea_dim = static_cast(ctx.Attr("EmbeddingDim")); - uint64_t padding_id = static_cast(ctx.Attr("PaddingId")); - auto table_id = static_cast(ctx.Attr("TableId")); - // note: GetInstance() is not thread-safe - // we assume FleetWrapper has been already initialized - auto fleet_ptr = framework::FleetWrapper::GetInstance(); - fleet_ptr->PullSparseToTensorSync( - table_id, fea_dim, padding_id, ctx.GetPlace(), &inputs, &outputs); -} - -template -void PushSparseFunctor(const framework::ExecutionContext& ctx) { - auto inputs = ctx.MultiInput("Ids"); - auto grads = ctx.MultiInput(framework::GradVarName("Out")); - uint32_t fea_dim = static_cast(ctx.Attr("EmbeddingDim")); - std::string accessor = ctx.Attr("AccessorClass"); - bool scale_sparse = ctx.Attr("ScaleSparseGrad"); - uint64_t padding_id = static_cast(ctx.Attr("PaddingId")); - const std::string& label_name = ctx.Attr("CtrLabelName"); - const framework::Scope& scope = ctx.scope(); - auto input_names = ctx.Attr>("InputNames"); - auto table_id = static_cast(ctx.Attr("TableId")); - // note: GetInstance() is not thread-safe - // we assume FleetWrapper has been already initialized - auto fleet_ptr = framework::FleetWrapper::GetInstance(); - fleet_ptr->PushSparseFromTensorWithLabelAsync(scope, - table_id, - fea_dim, - padding_id, - scale_sparse, - accessor, - label_name, - ctx.GetPlace(), - input_names, - &inputs, - &grads); -} - -template -class PullSparseV2CPUKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext& ctx) const override { - PullSparseFunctor(ctx); - } -}; - -template -class PushSparseV2CPUKernel : public framework::OpKernel { - public: - void Compute(const framework::ExecutionContext& ctx) const override { - PushSparseFunctor(ctx); - } -}; -} // namespace operators -} // namespace paddle diff --git a/test/ir/pir/translator/CMakeLists.txt b/test/ir/pir/translator/CMakeLists.txt index 8d7ac2c33e42ae..e0325e5b5672ae 100644 --- a/test/ir/pir/translator/CMakeLists.txt +++ b/test/ir/pir/translator/CMakeLists.txt @@ -28,7 +28,6 @@ list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_prune_gate_by_capacity_translator) list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_pull_gpups_sparse_translator) -list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_pull_sparse_v2_translator) list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_random_routing_translator) list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_send_and_recv_translator) list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_limit_by_capacity_translator) diff --git a/test/ir/pir/translator/test_pull_sparse_v2_translator.py b/test/ir/pir/translator/test_pull_sparse_v2_translator.py deleted file mode 100644 index f91bb3ccc2f900..00000000000000 --- a/test/ir/pir/translator/test_pull_sparse_v2_translator.py +++ /dev/null @@ -1,55 +0,0 @@ -# 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. - -import unittest - -import test_op_translator - -import paddle -from paddle.base import core -from paddle.base.layer_helper import LayerHelper - -paddle.pir_utils._switch_to_old_ir_() - - -class TestPullSparseV2OpTranslator( - test_op_translator.TestOpWithBackwardTranslator -): - def setUp(self): - self.place = core.Place() - self.place.set_place(paddle.CPUPlace()) - self.new_scope = paddle.static.Scope() - self.main_program = paddle.static.Program() - self.forward_op_type = "pull_sparse_v2" - self.backward_op_type = "push_sparse_v2" - - def append_op(self): - self.op_type = "pull_sparse_v2" - ids = paddle.ones(shape=(1,), dtype='int64') - w = paddle.ones(shape=(1,), dtype='int64') - out = paddle.ones(shape=(1,), dtype='int64') - helper = LayerHelper(self.op_type) - helper.append_op( - type=self.op_type, - inputs={"Ids": [ids], "W": [w]}, - outputs={"Out": [out]}, - ) - return out - - def test_translator(self): - self.check() - - -if __name__ == "__main__": - unittest.main()