/* Copyright (c) 2018 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/op_registry.h" #include "paddle/fluid/framework/tensor_util.h" #include "paddle/fluid/operators/math/selected_rows_functor.h" namespace paddle { namespace operators { template class MergeIdsOpKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &ctx) const override { auto place = ctx.GetPlace(); if (!platform::is_cpu_place(place)) { PADDLE_THROW("MergeIds do not support GPU kernel"); } const auto ids = ctx.MultiInput("Ids"); const auto row_ids = ctx.MultiInput("Rows"); const auto x_tensors = ctx.MultiInput("X"); auto outs = ctx.MultiOutput("Out"); PADDLE_ENFORCE_EQ(row_ids.size(), x_tensors.size(), "the number of Rows and X should be the same"); PADDLE_ENFORCE_EQ(ids.size(), outs.size(), "the number of Ids and Out should be the same"); int64_t row_ids_size = 0; int64_t row_size = 0; int64_t embedding_size = 0; for (size_t i = 0; i < x_tensors.size(); ++i) { const auto *x_tensor = x_tensors[i]; const auto *row_id = row_ids[i]; if (embedding_size == 0) { embedding_size = x_tensor->dims()[1]; } PADDLE_ENFORCE_EQ(embedding_size, x_tensor->dims()[1], "embedding size of all input should be the same"); row_size += x_tensor->dims()[0]; row_ids_size += row_id->dims()[0]; } PADDLE_ENFORCE_EQ( row_size, row_ids_size, "the merged X dim[0] and merged Rows dim[0] should be the same"); std::unordered_map> selected_rows_idx_map; for (size_t i = 0; i < x_tensors.size(); ++i) { const auto *row_id = row_ids[i]; for (auto j = 0; j < row_id->numel(); ++j) { int64_t key = row_id->data()[j]; std::tuple val = std::make_tuple(i, j); selected_rows_idx_map.insert(std::make_pair(key, val)); } } PADDLE_ENFORCE_EQ(row_ids_size, selected_rows_idx_map.size(), "the rows and tensor map size should be the same"); for (size_t i = 0; i < outs.size(); ++i) { auto *out_ids = ids[i]; auto *out = outs[i]; out->set_lod(out_ids->lod()); auto nums = out_ids->dims()[0]; auto *out_data = out->mutable_data( framework::make_ddim({nums, embedding_size}), place); for (auto j = 0; j < nums; ++j) { auto id = out_ids->data()[j]; auto row_tuple = selected_rows_idx_map.at(id); auto row_idx = std::get<1>(row_tuple); const auto *x_tensor = x_tensors[std::get<0>(row_tuple)]; memcpy(out_data + embedding_size * j, x_tensor->data() + row_idx * embedding_size, sizeof(T) * embedding_size); } } } }; } // namespace operators } // namespace paddle