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

【PIR Dist Op Reg No.19】 reg pull_box_sparse #62982

Merged
merged 23 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions paddle/fluid/pir/dialect/op_generator/ops_api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@
'push_dense',
'limit_by_capacity',
'global_scatter',
'pull_box_sparse',
'pull_box_sparse_',
]


Expand Down
9 changes: 9 additions & 0 deletions paddle/fluid/pir/dialect/operator/ir/ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,15 @@
func : prune_gate_by_capacity
data_type : gate_idx

- op : pull_box_sparse
args : (Tensor w, Tensor[] ids, bool is_sparse = false, bool is_distributed = false, int size = 1)
enkilee marked this conversation as resolved.
Show resolved Hide resolved
output : Tensor[](out){ids.size()}
infer_meta :
func : PullBoxSparseInferMeta
kernel :
func : pull_box_sparse
data_type : ids

- op : push_dense
args : (Tensor[] ids, int table_id = -1, float scale_data_norm = -1.0f, str[] input_names = {})
output :
Expand Down
1 change: 1 addition & 0 deletions paddle/fluid/pir/dialect/operator/utils/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const std::unordered_set<std::string> LegacyOpList = {
CReduceMinOp::name(),
CReduceProdOp::name(),
CScatterOp::name(),
PullBoxSparseOp::name(),
PushSparseV2Op::name(),
PartialSendOp::name(),
PartialRecvOp::name()};
Expand Down
10 changes: 10 additions & 0 deletions paddle/phi/api/yaml/op_compat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2641,6 +2641,16 @@
outputs :
out : Out

- op : pull_box_sparse
inputs :
{ w : W, ids: Ids}
outputs :
out : Out
attrs :
sparse : is_sparse
extra :
attrs : [bool is_sparse = false, bool is_distributed = false, int size = 1]

- op : push_dense
inputs :
ids : Ids
Expand Down
26 changes: 26 additions & 0 deletions paddle/phi/infermeta/binary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2893,6 +2893,32 @@ void PruneGateByCapacityInferMeta(const MetaTensor& gate_idx,
new_gate_idx->set_dtype(gate_idx.dtype());
}

void PullBoxSparseInferMeta(const MetaTensor& w,
const std::vector<const MetaTensor*>& ids,
bool is_sparse,
bool is_distributed,
int size,
std::vector<MetaTensor*> out) {
auto hidden_size = static_cast<int64_t>(size);
const size_t n_ids = ids.size();
for (size_t i = 0; i < n_ids; ++i) {
MetaTensor* output = out[i];
auto ids_dims = ids[i]->dims();
int ids_rank = ids_dims.size();
PADDLE_ENFORCE_EQ(ids_dims[ids_rank - 1],
1UL,
phi::errors::InvalidArgument(
"Shape error in %lu id, the last dimension of the "
"'Ids' tensor must be 1.",
i));
auto out_dim =
common::vectorize(common::slice_ddim(ids_dims, 0, ids_rank - 1));
out_dim.push_back(hidden_size);
output->set_dims(common::make_ddim(out_dim));
output->share_lod(*ids[i]);
enkilee marked this conversation as resolved.
Show resolved Hide resolved
}
}

void RepeatInterleaveWithTensorIndexInferMeta(const MetaTensor& x,
const MetaTensor& repeats,
int dim,
Expand Down
7 changes: 7 additions & 0 deletions paddle/phi/infermeta/binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,13 @@ void PReluInferMeta(const MetaTensor& x,
MetaTensor* out,
MetaConfig config = MetaConfig());

void PullBoxSparseInferMeta(const MetaTensor& w,
const std::vector<const MetaTensor*>& ids,
bool is_sparse,
bool is_distributed,
int size,
std::vector<MetaTensor*> out);

void RepeatInterleaveWithTensorIndexInferMeta(const MetaTensor& x,
const MetaTensor& repeats,
int dim,
Expand Down
1 change: 1 addition & 0 deletions test/ir/pir/translator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_push_dense_translator)
list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_partial_recv_translator)
list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST
test_prune_gate_by_capacity_translator)
list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_pull_box_sparse_translator)
list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_random_routing_translator)
list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_limit_by_capacity_translator)
list(APPEND DISTRIBUTED_OP_TRANSLATOR_TEST test_global_scatter_translator)
Expand Down
47 changes: 47 additions & 0 deletions test/ir/pir/translator/test_pull_box_sparse_translator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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.layer_helper import LayerHelper


class TestPullBoxSparseOpTranslator(test_op_translator.TestOpTranslator):
enkilee marked this conversation as resolved.
Show resolved Hide resolved
enkilee marked this conversation as resolved.
Show resolved Hide resolved
def append_op(self):
self.op_type = "pull_box_sparse"
ids = paddle.ones(shape=(1, 1), dtype='float32')
w = paddle.ones(shape=(1, 1), dtype='float32')
out = paddle.ones(shape=(1, 1), dtype='float32')
attrs = {
'is_sparse': False,
'is_distributed': False,
'size': 1,
}
helper = LayerHelper(self.op_type)
helper.append_op(
type=self.op_type,
inputs={"W": w, "Ids": [ids]},
outputs={"Out": [out]},
attrs=attrs,
)

enkilee marked this conversation as resolved.
Show resolved Hide resolved
def test_translator(self):
self.check()


if __name__ == "__main__":
unittest.main()