split_ids_op.h 2.0 KB
Newer Older
Q
Qiao Longfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/* 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 <vector>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/selected_rows_functor.h"

namespace paddle {
namespace operators {

template <typename DeviceContext, typename T>
class SplitIdsOpKernel : 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("SplitIds do not support GPU kernel");
    }

33 34
    auto& ids_dims = ctx.Input<framework::LoDTensor>("Ids")->dims();
    const T* ids = ctx.Input<framework::LoDTensor>("Ids")->data<T>();
Q
Qiao Longfei 已提交
35 36 37 38 39 40 41
    auto outs = ctx.MultiOutput<framework::LoDTensor>("Out");
    const size_t shard_num = outs.size();

    std::vector<std::vector<T>> out_ids;
    out_ids.resize(outs.size());

    // split id by their shard_num.
42
    for (int i = 0; i < ids_dims[0]; ++i) {
Q
Qiao Longfei 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
      T id = ids[i];
      size_t shard_id = static_cast<size_t>(id) % shard_num;
      out_ids[shard_id].push_back(id);
    }

    // create tensor for each shard and send to parameter server
    for (size_t i = 0; i < out_ids.size(); ++i) {
      auto* shard_t = outs[i];
      std::vector<T> ids = out_ids[i];
      auto* shard_data = shard_t->mutable_data<T>(
          framework::make_ddim({static_cast<int64_t>(ids.size()), 1}), place);
      for (size_t i = 0; i < ids.size(); ++i) {
        shard_data[i] = ids[i];
      }
    }
  }
};

}  // namespace operators
}  // namespace paddle