merge_ids_op.h 3.6 KB
Newer Older
Q
qiaolongfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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

S
seiriosPlus 已提交
17 18
#include <tuple>
#include <unordered_map>
Q
qiaolongfei 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include <vector>
#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 <typename DeviceContext, typename T>
class MergeIdsOpKernel : public framework::OpKernel<T> {
 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");
    }

S
seiriosPlus 已提交
36 37 38 39
    const auto ids = ctx.MultiInput<framework::LoDTensor>("Ids");
    const auto row_ids = ctx.MultiInput<framework::LoDTensor>("Rows");
    const auto x_tensors = ctx.MultiInput<framework::LoDTensor>("X");
    auto outs = ctx.MultiOutput<framework::LoDTensor>("Out");
Q
qiaolongfei 已提交
40

S
seiriosPlus 已提交
41 42 43 44
    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");
Q
qiaolongfei 已提交
45

S
seiriosPlus 已提交
46 47 48
    int row_ids_size = 0;
    int row_size = 0;
    int embedding_size = 0;
Q
qiaolongfei 已提交
49

S
seiriosPlus 已提交
50 51 52
    for (int i = 0; i < x_tensors.size(); ++i) {
      const auto *x_tensor = x_tensors[i];
      const auto *row_id = row_ids[i];
Q
qiaolongfei 已提交
53

S
seiriosPlus 已提交
54 55
      if (embedding_size == 0) {
        embedding_size = x_tensor->dims()[1];
Q
qiaolongfei 已提交
56
      }
S
seiriosPlus 已提交
57 58 59 60
      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];
Q
qiaolongfei 已提交
61
    }
S
seiriosPlus 已提交
62

Q
qiaolongfei 已提交
63
    PADDLE_ENFORCE_EQ(
S
seiriosPlus 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        row_size, row_ids_size,
        "the merged X dim[0] and merged Rows dim[0] should be the same");

    std::unordered_map<int64_t, std::tuple<int64_t, int64_t>>
        selected_rows_idx_map;
    for (int i = 0; i < x_tensors.size(); ++i) {
      const auto *row_id = row_ids[i];

      for (int j = 0; j < row_id->numel(); ++j) {
        int64_t key = row_id->data<int64_t>()[j];
        std::tuple<int64_t, int64_t> 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 (int i = 0; i < outs.size(); ++i) {
      auto *out_ids = ids[i];
      auto *out = outs[i];
Q
qiaolongfei 已提交
84

S
seiriosPlus 已提交
85
      out->set_lod(out_ids->lod());
Q
qiaolongfei 已提交
86

S
seiriosPlus 已提交
87
      int nums = static_cast<int>(out_ids->dims()[0]);
Q
qiaolongfei 已提交
88
      auto *out_data = out->mutable_data<T>(
S
seiriosPlus 已提交
89 90 91 92 93 94 95 96 97
          framework::make_ddim({nums, embedding_size}), place);
      for (int j = 0; j < nums; ++j) {
        int id = out_ids->data<int64_t>()[j];
        auto row_tuple = selected_rows_idx_map[id];
        int64_t 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<T>() + row_idx * embedding_size,
Q
qiaolongfei 已提交
98
               sizeof(T) * embedding_size);
Q
qiaolongfei 已提交
99
      }
Q
qiaolongfei 已提交
100 101 102 103 104 105
    }
  }
};

}  // namespace operators
}  // namespace paddle