merge_ids_op.h 4.0 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>
19
#include <utility>
Q
qiaolongfei 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33
#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)) {
34 35
      PADDLE_THROW(platform::errors::InvalidArgument(
          "MergeIds do not support GPU kernel"));
Q
qiaolongfei 已提交
36 37
    }

S
seiriosPlus 已提交
38 39 40 41
    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 已提交
42

S
seiriosPlus 已提交
43
    PADDLE_ENFORCE_EQ(row_ids.size(), x_tensors.size(),
44 45
                      platform::errors::InvalidArgument(
                          "the number of Rows and X should be the same"));
S
seiriosPlus 已提交
46
    PADDLE_ENFORCE_EQ(ids.size(), outs.size(),
47 48
                      platform::errors::InvalidArgument(
                          "the number of Ids and Out should be the same"));
Q
qiaolongfei 已提交
49

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

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

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

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

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

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

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

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

T
tangwei12 已提交
94
      auto nums = out_ids->dims()[0];
Q
qiaolongfei 已提交
95
      auto *out_data = out->mutable_data<T>(
S
seiriosPlus 已提交
96
          framework::make_ddim({nums, embedding_size}), place);
T
tangwei12 已提交
97 98 99 100
      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 已提交
101 102 103 104
        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 已提交
105
               sizeof(T) * embedding_size);
Q
qiaolongfei 已提交
106
      }
Q
qiaolongfei 已提交
107 108 109 110 111 112
    }
  }
};

}  // namespace operators
}  // namespace paddle