From e9aebc1d9e51d4b9e41215d55203b011fb43f260 Mon Sep 17 00:00:00 2001 From: Difers <707065510@qq.com> Date: Sun, 31 Dec 2023 14:43:28 +0800 Subject: [PATCH 1/7] reg partial_send --- .../pir/dialect/op_generator/ops_api_gen.py | 1 + paddle/fluid/pir/dialect/operator/ir/ops.yaml | 10 +++++ .../fluid/pir/dialect/operator/utils/utils.cc | 3 +- paddle/phi/api/yaml/op_compat.yaml | 6 +++ paddle/phi/infermeta/unary.cc | 31 ++++++++++++++ paddle/phi/infermeta/unary.h | 8 ++++ .../translator/test_partial_send_translate.py | 42 +++++++++++++++++++ 7 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 test/ir/pir/translator/test_partial_send_translate.py diff --git a/paddle/fluid/pir/dialect/op_generator/ops_api_gen.py b/paddle/fluid/pir/dialect/op_generator/ops_api_gen.py index 13c656207f1b8..8328e406ae0e6 100644 --- a/paddle/fluid/pir/dialect/op_generator/ops_api_gen.py +++ b/paddle/fluid/pir/dialect/op_generator/ops_api_gen.py @@ -156,6 +156,7 @@ 'c_reduce_min_', 'push_sparse_v2', 'push_sparse_v2_', + 'partial_send', ] diff --git a/paddle/fluid/pir/dialect/operator/ir/ops.yaml b/paddle/fluid/pir/dialect/operator/ir/ops.yaml index d32c1d8b7a6bd..c3338db039259 100644 --- a/paddle/fluid/pir/dialect/operator/ir/ops.yaml +++ b/paddle/fluid/pir/dialect/operator/ir/ops.yaml @@ -1654,6 +1654,16 @@ kernel: func: onednn_to_paddle_layout +- op: partial_send + args: (Tensor x, int ring_id = 0, int peer = 0, bool use_calc_stream = false, int num = 1, int id = 0) + output: Tensor(out) + infer_meta: + func: PartialSendInferMeta + param: [x, ring_id, peer, use_calc_stream, num, id] + kernel: + func: partial_send + optional : out + - op: sparse_momentum args: (Tensor param, Tensor grad, Tensor velocity, Tensor index, Tensor learning_rate, Tensor master_param,float mu, Scalar axis=0, bool use_nesterov=false,str regularization_method="", float regularization_coeff=0.0f, bool multi_precision=false, float rescale_grad=1.0f) output: Tensor(param_out), Tensor(velocity_out), Tensor(master_param_out) diff --git a/paddle/fluid/pir/dialect/operator/utils/utils.cc b/paddle/fluid/pir/dialect/operator/utils/utils.cc index ea8002c1c842f..b4bad427567b7 100644 --- a/paddle/fluid/pir/dialect/operator/utils/utils.cc +++ b/paddle/fluid/pir/dialect/operator/utils/utils.cc @@ -80,7 +80,8 @@ const std::unordered_set LegacyOpList = { paddle::onednn::dialect::MultiGruOp::name(), #endif CReduceMinOp::name(), - PushSparseV2Op::name()}; + PushSparseV2Op::name(), + PartialSendOp::name()}; enum class AttrType { UNDEFINED = 0, diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 237724dabe69f..e8ad991453836 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -3653,6 +3653,12 @@ outputs : out : Out +- op: partial_send + inputs : + x : X + outputs : + out : Out + - op: read_from_array inputs: array : X diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 611b5239dccdf..11d7f0b46b3c3 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -2917,6 +2917,37 @@ void Pad3dInferMeta(const MetaTensor& x, out->share_lod(x); } +void PartialSendInferMeta(const MetaTensor& x, + int ring_id, + int peer, + bool use_calc_stream, + int num, + int id, + MetaTensor* out) { + PADDLE_ENFORCE_GE( + peer, + 0, + phi::errors::InvalidArgument( + "The peer (%d) for partial_send op must be non-negative.", peer)); + PADDLE_ENFORCE_GE( + ring_id, + 0, + phi::errors::InvalidArgument( + "The ring_id (%d) for partial_send op must be non-negative.", + ring_id)); + PADDLE_ENFORCE_GE(num, + 1, + phi::errors::InvalidArgument( + "The num (%d) for partial_send op must >=1", num)); + PADDLE_ENFORCE_EQ( + (id >= 0 && id < num), + true, + phi::errors::InvalidArgument( + "The id (%d) for partial_send op must >=0 and set_dtype(x.dtype()); + out->set_dims(x.dims()); +} + void PixelShuffleInferMeta(const MetaTensor& x, int upscale_factor, const std::string& data_format, diff --git a/paddle/phi/infermeta/unary.h b/paddle/phi/infermeta/unary.h index 5f587e27cf348..24a05ea3a24ac 100644 --- a/paddle/phi/infermeta/unary.h +++ b/paddle/phi/infermeta/unary.h @@ -434,6 +434,14 @@ void Pad3dInferMeta(const MetaTensor& x, MetaTensor* out, MetaConfig config = MetaConfig()); +void PartialSendInferMeta(const MetaTensor& x, + int ring_id, + int peer, + bool use_calc_stream, + int num, + int id, + MetaTensor* out); + void PixelShuffleInferMeta(const MetaTensor& x, int upscale_factor, const std::string& data_format, diff --git a/test/ir/pir/translator/test_partial_send_translate.py b/test/ir/pir/translator/test_partial_send_translate.py new file mode 100644 index 0000000000000..38e105299f822 --- /dev/null +++ b/test/ir/pir/translator/test_partial_send_translate.py @@ -0,0 +1,42 @@ +# 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. + +import unittest + +import test_op_transcriber + +import paddle +from paddle.base.layer_helper import LayerHelper + + +class TestCReduceMinOpTranscriber(test_op_transcriber.TestOpTranscriber): + def append_op(self): + self.op_type = "partial_send" + x = paddle.ones(shape=(100, 2, 3), dtype='float32') + y = x + attrs = {'ring_id': 0, 'root_id': 0, 'use_calc_stream': False} + helper = LayerHelper(self.op_type) + helper.append_op( + type=self.op_type, + inputs={"X": x}, + outputs={"Out": y}, + attrs=attrs, + ) + + def test_translator(self): + self.check() + + +if __name__ == "__main__": + unittest.main() From d57e49b3134a8551af4b7dfee768352c348c3f9f Mon Sep 17 00:00:00 2001 From: Difers <707065510@qq.com> Date: Sat, 6 Jan 2024 10:29:47 +0800 Subject: [PATCH 2/7] fix cmake --- test/ir/pir/translator/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/ir/pir/translator/CMakeLists.txt b/test/ir/pir/translator/CMakeLists.txt index fe9a5ccd74115..ab5c32ed14378 100644 --- a/test/ir/pir/translator/CMakeLists.txt +++ b/test/ir/pir/translator/CMakeLists.txt @@ -7,8 +7,8 @@ string(REPLACE ".py" "" TEST_INTERP_CASES "${TEST_INTERP_CASES}") set(DISTRIBUTED_OP_TRANSLATOR_TEST test_c_reduce_min_translator) list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_c_allreduce_min_translator) list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_c_allreduce_prod_translator) -list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST - test_distributed_lookup_table_translate) +list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_distributed_lookup_table_translate) +list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_partial_send_translator) if(NOT WITH_DISTRIBUTE) list(REMOVE_ITEM TEST_INTERP_CASES ${DISTRIBUTED_OP_TRANSLATOR_TEST}) From 537cfbb321f946ef39e289af8957b97256b3b619 Mon Sep 17 00:00:00 2001 From: Difers <707065510@qq.com> Date: Sun, 7 Jan 2024 10:32:42 +0800 Subject: [PATCH 3/7] fix import --- test/ir/pir/translator/test_partial_send_translate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/ir/pir/translator/test_partial_send_translate.py b/test/ir/pir/translator/test_partial_send_translate.py index 38e105299f822..6d1d6b39ebd13 100644 --- a/test/ir/pir/translator/test_partial_send_translate.py +++ b/test/ir/pir/translator/test_partial_send_translate.py @@ -14,13 +14,13 @@ import unittest -import test_op_transcriber +import test_op_translator import paddle from paddle.base.layer_helper import LayerHelper -class TestCReduceMinOpTranscriber(test_op_transcriber.TestOpTranscriber): +class TestCPartialSendTranslator(test_op_translator.TestOpTranslator): def append_op(self): self.op_type = "partial_send" x = paddle.ones(shape=(100, 2, 3), dtype='float32') From 365e1cc48caa1f23b314c907ee3a602f42f9ae73 Mon Sep 17 00:00:00 2001 From: Difers <707065510@qq.com> Date: Wed, 10 Jan 2024 19:15:56 +0800 Subject: [PATCH 4/7] fix cmake --- ...artial_send_translate.py => test_partial_send_translator.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename test/ir/pir/translator/{test_partial_send_translate.py => test_partial_send_translator.py} (94%) diff --git a/test/ir/pir/translator/test_partial_send_translate.py b/test/ir/pir/translator/test_partial_send_translator.py similarity index 94% rename from test/ir/pir/translator/test_partial_send_translate.py rename to test/ir/pir/translator/test_partial_send_translator.py index 6d1d6b39ebd13..b1e490c793249 100644 --- a/test/ir/pir/translator/test_partial_send_translate.py +++ b/test/ir/pir/translator/test_partial_send_translator.py @@ -20,7 +20,7 @@ from paddle.base.layer_helper import LayerHelper -class TestCPartialSendTranslator(test_op_translator.TestOpTranslator): +class TestPartialSendTranslator(test_op_translator.TestOpTranslator): def append_op(self): self.op_type = "partial_send" x = paddle.ones(shape=(100, 2, 3), dtype='float32') From c5c0eaddcf74e674fd30169d2676eec7d12e5d72 Mon Sep 17 00:00:00 2001 From: Difers <707065510@qq.com> Date: Wed, 7 Feb 2024 11:07:13 +0800 Subject: [PATCH 5/7] try to remove out --- paddle/fluid/pir/dialect/operator/ir/ops.yaml | 1 - paddle/phi/api/yaml/op_compat.yaml | 2 -- paddle/phi/infermeta/unary.cc | 5 +---- paddle/phi/infermeta/unary.h | 3 +-- test/ir/pir/translator/test_partial_send_translator.py | 2 -- 5 files changed, 2 insertions(+), 11 deletions(-) diff --git a/paddle/fluid/pir/dialect/operator/ir/ops.yaml b/paddle/fluid/pir/dialect/operator/ir/ops.yaml index c3338db039259..866fb0690b3be 100644 --- a/paddle/fluid/pir/dialect/operator/ir/ops.yaml +++ b/paddle/fluid/pir/dialect/operator/ir/ops.yaml @@ -1662,7 +1662,6 @@ param: [x, ring_id, peer, use_calc_stream, num, id] kernel: func: partial_send - optional : out - op: sparse_momentum args: (Tensor param, Tensor grad, Tensor velocity, Tensor index, Tensor learning_rate, Tensor master_param,float mu, Scalar axis=0, bool use_nesterov=false,str regularization_method="", float regularization_coeff=0.0f, bool multi_precision=false, float rescale_grad=1.0f) diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index e8ad991453836..53e0cea953b87 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -3656,8 +3656,6 @@ - op: partial_send inputs : x : X - outputs : - out : Out - op: read_from_array inputs: diff --git a/paddle/phi/infermeta/unary.cc b/paddle/phi/infermeta/unary.cc index 11d7f0b46b3c3..a742ad2a83d6e 100644 --- a/paddle/phi/infermeta/unary.cc +++ b/paddle/phi/infermeta/unary.cc @@ -2922,8 +2922,7 @@ void PartialSendInferMeta(const MetaTensor& x, int peer, bool use_calc_stream, int num, - int id, - MetaTensor* out) { + int id) { PADDLE_ENFORCE_GE( peer, 0, @@ -2944,8 +2943,6 @@ void PartialSendInferMeta(const MetaTensor& x, true, phi::errors::InvalidArgument( "The id (%d) for partial_send op must >=0 and set_dtype(x.dtype()); - out->set_dims(x.dims()); } void PixelShuffleInferMeta(const MetaTensor& x, diff --git a/paddle/phi/infermeta/unary.h b/paddle/phi/infermeta/unary.h index 24a05ea3a24ac..a9f5f2eb1a13c 100644 --- a/paddle/phi/infermeta/unary.h +++ b/paddle/phi/infermeta/unary.h @@ -439,8 +439,7 @@ void PartialSendInferMeta(const MetaTensor& x, int peer, bool use_calc_stream, int num, - int id, - MetaTensor* out); + int id); void PixelShuffleInferMeta(const MetaTensor& x, int upscale_factor, diff --git a/test/ir/pir/translator/test_partial_send_translator.py b/test/ir/pir/translator/test_partial_send_translator.py index b1e490c793249..9f133f5274969 100644 --- a/test/ir/pir/translator/test_partial_send_translator.py +++ b/test/ir/pir/translator/test_partial_send_translator.py @@ -24,13 +24,11 @@ class TestPartialSendTranslator(test_op_translator.TestOpTranslator): def append_op(self): self.op_type = "partial_send" x = paddle.ones(shape=(100, 2, 3), dtype='float32') - y = x attrs = {'ring_id': 0, 'root_id': 0, 'use_calc_stream': False} helper = LayerHelper(self.op_type) helper.append_op( type=self.op_type, inputs={"X": x}, - outputs={"Out": y}, attrs=attrs, ) From 8e61e111a92697a9476dc18d7acc00d56c5cfd52 Mon Sep 17 00:00:00 2001 From: Difers <707065510@qq.com> Date: Thu, 22 Feb 2024 19:15:10 +0800 Subject: [PATCH 6/7] add partitial_send to prim gen blacklist --- paddle/fluid/pir/dialect/operator/ir/ops.yaml | 3 ++- paddle/fluid/primitive/codegen/gen.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/pir/dialect/operator/ir/ops.yaml b/paddle/fluid/pir/dialect/operator/ir/ops.yaml index 866fb0690b3be..4fcd90c99fe0a 100644 --- a/paddle/fluid/pir/dialect/operator/ir/ops.yaml +++ b/paddle/fluid/pir/dialect/operator/ir/ops.yaml @@ -1656,12 +1656,13 @@ - op: partial_send args: (Tensor x, int ring_id = 0, int peer = 0, bool use_calc_stream = false, int num = 1, int id = 0) - output: Tensor(out) + output : infer_meta: func: PartialSendInferMeta param: [x, ring_id, peer, use_calc_stream, num, id] kernel: func: partial_send + param: [x, ring_id, peer, use_calc_stream, num, id] - op: sparse_momentum args: (Tensor param, Tensor grad, Tensor velocity, Tensor index, Tensor learning_rate, Tensor master_param,float mu, Scalar axis=0, bool use_nesterov=false,str regularization_method="", float regularization_coeff=0.0f, bool multi_precision=false, float rescale_grad=1.0f) diff --git a/paddle/fluid/primitive/codegen/gen.py b/paddle/fluid/primitive/codegen/gen.py index 3c6791a344a8b..fb1579968423a 100644 --- a/paddle/fluid/primitive/codegen/gen.py +++ b/paddle/fluid/primitive/codegen/gen.py @@ -52,6 +52,7 @@ "embedding_sparse_grad", "embedding_grad", "full", + "partial_send", ] # prim op with one input and one output, with no attribute From 1e1f5fb5fe94b84bf1ff4773a60098e3a631e84f Mon Sep 17 00:00:00 2001 From: Difers <707065510@qq.com> Date: Thu, 22 Feb 2024 20:23:10 +0800 Subject: [PATCH 7/7] fix typo --- test/ir/pir/translator/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/ir/pir/translator/CMakeLists.txt b/test/ir/pir/translator/CMakeLists.txt index ab5c32ed14378..2dd89d3406c92 100644 --- a/test/ir/pir/translator/CMakeLists.txt +++ b/test/ir/pir/translator/CMakeLists.txt @@ -7,7 +7,8 @@ string(REPLACE ".py" "" TEST_INTERP_CASES "${TEST_INTERP_CASES}") set(DISTRIBUTED_OP_TRANSLATOR_TEST test_c_reduce_min_translator) list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_c_allreduce_min_translator) list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_c_allreduce_prod_translator) -list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_distributed_lookup_table_translate) +list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST + test_distributed_lookup_table_translate) list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_partial_send_translator) if(NOT WITH_DISTRIBUTE)