multiplex_op.h 2.6 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
Yibing Liu 已提交
26
class MultiplexCPUKernel : public framework::OpKernel {
Y
Yibing Liu 已提交
27 28 29
 public:
  void Compute(const framework::ExecutionContext& ctx) const {
    auto ins = ctx.MultiInput<framework::Tensor>("X");
Y
Yibing Liu 已提交
30
    auto* out = ctx.Output<framework::Tensor>("Out");
31

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

    auto rows = ins[1]->dims()[0];
    auto cols = ins[1]->dims()[1];
Y
Yibing Liu 已提交
36 37 38
    auto* index = ins[0]->data<T>();
    Place place = boost::get<Place>(ctx.GetPlace());
    for (auto i = 0; i < rows; i++) {
Y
Yibing Liu 已提交
39
      size_t k = (size_t)index[i] + 1;
Y
Yibing Liu 已提交
40 41 42 43
      PADDLE_ENFORCE_LT(k, ins.size(),
                        "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 已提交
44 45 46 47
    }
  }
};

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

    auto rows = ins[1]->dims()[0];
    auto cols = ins[1]->dims()[1];
Y
Yibing Liu 已提交
66 67 68
    auto* index = ins[0]->data<T>();
    Place place = boost::get<Place>(ctx.GetPlace());
    for (auto i = 0; i < rows; i++) {
Y
Yibing Liu 已提交
69
      size_t k = (size_t)index[i] + 1;
Y
Yibing Liu 已提交
70 71 72
      if (d_ins[k]) {
        memory::Copy(place, d_ins[k]->data<T>() + i * cols, place,
                     d_out->data<T>() + i * cols, cols * sizeof(T));
73
      }
Y
Yibing Liu 已提交
74 75 76 77 78
    }
  }
};
}
}