multiplex_op.h 3.0 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 {

Q
QI JUN 已提交
25
template <typename DeviceContext, 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>();
Q
QI JUN 已提交
38
    platform::CPUPlace place = boost::get<platform::CPUPlace>(ctx.GetPlace());
Y
Yibing Liu 已提交
39
    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
    }
  }
};

Q
QI JUN 已提交
50
template <typename DeviceContext, 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
        auto t = framework::EigenVector<T>::Flatten(*d_ins[i]);
Q
QI JUN 已提交
63 64
        t.device(*ctx.template device_context<DeviceContext>().eigen_device()) =
            t.constant(static_cast<T>(0));
65
      }
Y
Yibing Liu 已提交
66 67
    }

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