distributed_lookup_table_op.h 5.5 KB
Newer Older
T
tangwei12 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2016 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 <algorithm>
#include <string>
#include <vector>
#include "paddle/fluid/distributed/fleet.h"
Y
yaoxuefeng 已提交
17
#include "paddle/fluid/distributed/service/communicator.h"
T
tangwei12 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/tensor_util.h"
#include "paddle/fluid/operators/math/math_function.h"

namespace paddle {
namespace operators {

template <typename DeviceContext, typename T>
class DistributedLookupTableKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext &context) const override {
    auto &scope = context.scope();

    auto padding_idx = context.Attr<int64_t>("padding_idx");
    auto table_id = context.Attr<int>("table_id");
34
    bool is_test = context.Attr<bool>("is_test");
T
tangwei12 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

    auto embedding_name = context.InputNames("W").front();
    int64_t emb_dim = 0;

    auto *var = scope.FindVar(embedding_name);

    if (var->IsType<framework::LoDTensor>()) {
      emb_dim = var->Get<framework::LoDTensor>().dims()[1];
    } else if (var->IsType<framework::SelectedRows>()) {
      emb_dim = var->Get<framework::SelectedRows>().value().dims()[1];
    } else {
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Expected type of `W` must be Tensor, SelectedRows.But got "
          "unsupport type: %s.",
          framework::ToTypeName(var->Type())));
    }

    auto inputs = context.MultiInput<framework::LoDTensor>("Ids");
    auto outputs = context.MultiOutput<framework::LoDTensor>("Outputs");

Y
yaoxuefeng 已提交
55 56 57
    // auto fleet = distributed::FleetWrapper::GetInstance();
    auto *communicator = (distributed::AsyncCommunicator *)
        distributed::Communicator::GetInstance();
T
tangwei12 已提交
58 59

    if (platform::is_cpu_place(context.GetPlace())) {
Y
yaoxuefeng 已提交
60 61 62 63
      communicator->PullSparseToTensorSync(
          static_cast<uint64_t>(table_id), emb_dim,
          static_cast<uint64_t>(padding_idx), context.GetPlace(), !is_test,
          &inputs, &outputs);
T
tangwei12 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    } else {
      auto inputs_variable = context.MultiInputVar("Ids");
      auto outputs_variable = context.MultiOutputVar("Outputs");
      auto inputs_name = context.InputNames("Ids");
      auto outputs_name = context.OutputNames("Outputs");

      auto cpu_place = platform::CPUPlace();
      framework::Scope *tmp_scope = scope.NewTmpScope().release();

      std::vector<const framework::LoDTensor *> tmp_input_vec;
      auto input_var_size = inputs_variable.size();
      std::vector<framework::LoDTensor *> tmp_output_vec;
      auto output_var_size = outputs_variable.size();

      // create temp input
      for (size_t idx = 0; idx < input_var_size; ++idx) {
        framework::Variable *tmp_input_var = tmp_scope->Var(inputs_name[idx]);
        framework::LoDTensor *tmp_input_tensor =
            tmp_input_var->GetMutable<framework::LoDTensor>();
        framework::TensorCopy(inputs_variable[idx]->Get<framework::LoDTensor>(),
                              cpu_place, context.device_context(),
                              tmp_input_tensor);
        tmp_input_vec.push_back(tmp_input_tensor);
      }

      // create temp output
      for (size_t idx = 0; idx < output_var_size; ++idx) {
        framework::Variable *tmp_output_var = tmp_scope->Var(outputs_name[idx]);
        framework::LoDTensor *tmp_output_tensor =
            tmp_output_var->GetMutable<framework::LoDTensor>();
        tmp_output_tensor->Resize(outputs[idx]->dims());
        tmp_output_vec.push_back(tmp_output_tensor);
      }

      // use fleet->PullSparse
Y
yaoxuefeng 已提交
99 100 101 102
      communicator->PullSparseToTensorSync(
          static_cast<uint64_t>(table_id), emb_dim,
          static_cast<uint64_t>(padding_idx), cpu_place, !is_test,
          &tmp_input_vec, &tmp_output_vec);
T
tangwei12 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

      // cp temp to origin
      for (size_t idx = 0; idx < output_var_size; ++idx) {
        framework::Variable *tmp_output_var = tmp_scope->Var(outputs_name[idx]);
        framework::LoDTensor *tmp_output_tensor =
            tmp_output_var->GetMutable<framework::LoDTensor>();
        framework::TensorCopy(
            *tmp_output_tensor, context.GetPlace(), context.device_context(),
            outputs_variable[idx]->GetMutable<framework::LoDTensor>());
      }
      delete tmp_scope;
    }

    auto id_names = context.InputNames("Ids");
    auto out_names = context.OutputNames("Outputs");
    auto lookup_table_version =
        context.Attr<std::string>("lookup_table_version");

    if (lookup_table_version == "lookup_table_v2") {
      for (size_t i = 0; i < id_names.size(); ++i) {
        auto *id_var = scope.FindVar(id_names[i]);
        auto *out_var = scope.FindVar(out_names[i]);
        auto *id_tensor = id_var->GetMutable<framework::LoDTensor>();
        auto *out_tensor = out_var->GetMutable<framework::LoDTensor>();

        auto id_dims = id_tensor->dims();
        out_tensor->Resize(framework::make_ddim(
            {static_cast<int64_t>(id_dims[0]), static_cast<int64_t>(id_dims[1]),
             static_cast<int64_t>(emb_dim)}));
      }
    }
  }
};

}  // namespace operators
}  // namespace paddle