merge_ids_op.h 3.9 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
    PADDLE_ENFORCE_EQ(row_ids.size(), x_tensors.size(),
42 43
                      platform::errors::InvalidArgument(
                          "the number of Rows and X should be the same"));
S
seiriosPlus 已提交
44
    PADDLE_ENFORCE_EQ(ids.size(), outs.size(),
45 46
                      platform::errors::InvalidArgument(
                          "the number of Ids and Out should be the same"));
Q
qiaolongfei 已提交
47

T
tangwei12 已提交
48 49 50
    int64_t row_ids_size = 0;
    int64_t row_size = 0;
    int64_t embedding_size = 0;
Q
qiaolongfei 已提交
51

T
Tao Luo 已提交
52
    for (size_t i = 0; i < x_tensors.size(); ++i) {
S
seiriosPlus 已提交
53 54
      const auto *x_tensor = x_tensors[i];
      const auto *row_id = row_ids[i];
Q
qiaolongfei 已提交
55

S
seiriosPlus 已提交
56 57
      if (embedding_size == 0) {
        embedding_size = x_tensor->dims()[1];
Q
qiaolongfei 已提交
58
      }
S
seiriosPlus 已提交
59
      PADDLE_ENFORCE_EQ(embedding_size, x_tensor->dims()[1],
60 61
                        platform::errors::InvalidArgument(
                            "embedding size of all input should be the same"));
S
seiriosPlus 已提交
62 63
      row_size += x_tensor->dims()[0];
      row_ids_size += row_id->dims()[0];
Q
qiaolongfei 已提交
64
    }
S
seiriosPlus 已提交
65

Q
qiaolongfei 已提交
66
    PADDLE_ENFORCE_EQ(
S
seiriosPlus 已提交
67
        row_size, row_ids_size,
68 69
        platform::errors::InvalidArgument(
            "the merged X dim[0] and merged Rows dim[0] should be the same"));
S
seiriosPlus 已提交
70 71 72

    std::unordered_map<int64_t, std::tuple<int64_t, int64_t>>
        selected_rows_idx_map;
T
Tao Luo 已提交
73
    for (size_t i = 0; i < x_tensors.size(); ++i) {
S
seiriosPlus 已提交
74 75
      const auto *row_id = row_ids[i];

T
tangwei12 已提交
76
      for (auto j = 0; j < row_id->numel(); ++j) {
S
seiriosPlus 已提交
77 78 79 80 81 82
        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(),
83 84
                      platform::errors::InvalidArgument(
                          "the rows and tensor map size should be the same"));
S
seiriosPlus 已提交
85

T
Tao Luo 已提交
86
    for (size_t i = 0; i < outs.size(); ++i) {
S
seiriosPlus 已提交
87 88
      auto *out_ids = ids[i];
      auto *out = outs[i];
Q
qiaolongfei 已提交
89

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

T
tangwei12 已提交
92
      auto nums = out_ids->dims()[0];
Q
qiaolongfei 已提交
93
      auto *out_data = out->mutable_data<T>(
S
seiriosPlus 已提交
94
          framework::make_ddim({nums, embedding_size}), place);
T
tangwei12 已提交
95 96 97 98
      for (auto j = 0; j < nums; ++j) {
        auto id = out_ids->data<int64_t>()[j];
        auto row_tuple = selected_rows_idx_map.at(id);
        auto row_idx = std::get<1>(row_tuple);
S
seiriosPlus 已提交
99 100 101 102
        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 已提交
103
               sizeof(T) * embedding_size);
Q
qiaolongfei 已提交
104
      }
Q
qiaolongfei 已提交
105 106 107 108 109 110
    }
  }
};

}  // namespace operators
}  // namespace paddle