multiplex_op.h 2.9 KB
Newer Older
Y
Yibing Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   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 "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
20
#include "paddle/memory/memcpy.h"
Y
Yibing Liu 已提交
21 22 23 24

namespace paddle {
namespace operators {

25
template <typename Place, typename T>
Y
Yu Yang 已提交
26
class MultiplexCPUKernel : public framework::OpKernel<T> {
Y
Yibing Liu 已提交
27 28 29
 public:
  void Compute(const framework::ExecutionContext& ctx) const {
    auto ins = ctx.MultiInput<framework::Tensor>("X");
30
    auto ids = ctx.Input<framework::Tensor>("Ids");
Y
Yibing Liu 已提交
31
    auto* out = ctx.Output<framework::Tensor>("Out");
32

Y
Yibing Liu 已提交
33 34
    out->mutable_data<T>(ctx.GetPlace());

35
    auto rows = ins[0]->dims()[0];
36
    auto cols = ins[0]->numel() / rows;
37
    auto index = ids->data<int32_t>();
Y
Yibing Liu 已提交
38 39
    Place place = boost::get<Place>(ctx.GetPlace());
    for (auto i = 0; i < rows; i++) {
40 41
      int32_t k = index[i];
      PADDLE_ENFORCE_GE(k, 0, "index must be nonnegative.");
Q
qiaolongfei 已提交
42
      PADDLE_ENFORCE_LT(static_cast<size_t>(k), ins.size(),
Y
Yibing Liu 已提交
43 44 45
                        "index exceeds the number of candidate tensors.");
      memory::Copy(place, out->data<T>() + i * cols, place,
                   ins[k]->data<T>() + i * cols, cols * sizeof(T));
Y
Yibing Liu 已提交
46 47 48 49
    }
  }
};

50
template <typename Place, typename T>
Y
Yu Yang 已提交
51
class MultiplexGradCPUKernel : public framework::OpKernel<T> {
Y
Yibing Liu 已提交
52 53 54
 public:
  void Compute(const framework::ExecutionContext& ctx) const {
    auto* d_out = ctx.Input<framework::Tensor>(framework::GradVarName("Out"));
55
    auto* ids = ctx.Input<framework::Tensor>("Ids");
Y
Yibing Liu 已提交
56 57 58
    auto ins = ctx.MultiInput<framework::Tensor>("X");
    auto d_ins =
        ctx.MultiOutput<framework::Tensor>(framework::GradVarName("X"));
59
    for (size_t i = 0; i < d_ins.size(); i++) {
60 61
      if (d_ins[i]) {
        d_ins[i]->mutable_data<T>(ctx.GetPlace());
62 63
        auto t = framework::EigenVector<T>::Flatten(*d_ins[i]);
        t.device(ctx.GetEigenDevice<Place>()) = t.constant(static_cast<T>(0));
64
      }
Y
Yibing Liu 已提交
65 66
    }

67
    auto rows = ins[0]->dims()[0];
68
    auto cols = ins[0]->numel() / rows;
69
    auto* index = ids->data<int32_t>();
Y
Yibing Liu 已提交
70 71
    Place place = boost::get<Place>(ctx.GetPlace());
    for (auto i = 0; i < rows; i++) {
72
      size_t k = static_cast<size_t>(index[i]);
Y
Yibing Liu 已提交
73 74 75
      if (d_ins[k]) {
        memory::Copy(place, d_ins[k]->data<T>() + i * cols, place,
                     d_out->data<T>() + i * cols, cols * sizeof(T));
76
      }
Y
Yibing Liu 已提交
77 78 79
    }
  }
};
Q
qiaolongfei 已提交
80 81
}  // namespace operators
}  // namespace paddle