split_pe.hpp 4.8 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2019 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>

19 20
#include "lite/backends/fpga/KD/pe.hpp"
#include "lite/backends/fpga/KD/pe_params.hpp"
Y
Yan Chunwei 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
namespace paddle {
namespace zynqmp {

class SplitPE : public PE {
 public:
  bool init() {
    std::vector<Tensor*> outputs = param_.outputs;
    for (size_t i = 0; i < outputs.size(); i++) {
      Tensor* out = outputs[i];
      out->setAligned(false);
      out->setDataLocation(CPU);
    }
    return true;
  }

  std::vector<int> stride_numel(std::vector<int> ddim) {
    std::vector<int> strides(ddim.size());
    strides[ddim.size() - 1] = ddim[ddim.size() - 1];
    for (int i = ddim.size() - 2; i >= 0; --i) {
      strides[i] = strides[i + 1] * ddim[i];
    }
    return strides;
  }

  template <typename T>
  inline void StridedNumelCopyWithAxis(int64_t axis,
                                       T* dst,
                                       const std::vector<int>& dst_stride_numel,
                                       T* src,
                                       const std::vector<int>& src_stride_numel,
                                       int64_t size) {
    int64_t before = dst_stride_numel[0] / dst_stride_numel[axis];
    int64_t src_after = src_stride_numel[axis];
    int64_t dst_after = dst_stride_numel[axis];

56 57 58 59
    // PADDLE_MOBILE_ENFORCE(src_stride_numel.size() == dst_stride_numel.size(),
    //                       "src and dst tensor should have the same dims
    //                       size.");

Y
Yan Chunwei 已提交
60 61
    for (int64_t i = 0; i < axis; ++i) {
      if (i < axis) {
62 63 64 65 66
        // PADDLE_MOBILE_ENFORCE(src_stride_numel[i] / src_stride_numel[axis] ==
        //                           dst_stride_numel[i] /
        //                           dst_stride_numel[axis],
        //                       "src and dst should have the same elements "
        //                       "except the specified axis.");
Y
Yan Chunwei 已提交
67 68 69
      } else if (i == axis) {
        continue;
      } else {
70 71 72
        // PADDLE_MOBILE_ENFORCE(src_stride_numel[i] == dst_stride_numel[i],
        //                       "src and dst should have the same elements "
        //                       "except the specified axis.");
Y
Yan Chunwei 已提交
73 74 75 76
      }
    }

    for (int64_t i = 0; i < before; ++i) {
77
      memcpy(dst + i * dst_after, src + i * src_after, sizeof(T) * size);
Y
Yan Chunwei 已提交
78 79 80
    }
  }

81 82 83 84 85 86
  void split3D() {
    int axis = param_.axis;
    // float16* dst = param_.output->data<float16>();
    // std::vector<int>& dst_dims = ;
    // StridedNumelCopyWithAxis();
  }
Y
Yan Chunwei 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

  bool dispatch() {
    Tensor* input = param_.input;
    input->syncToCPU();
    if (input->shape().dimSize() <= 3) {
      auto in_stride = stride_numel(input->shape().dims());
      int64_t axis = param_.axis;
      size_t input_offset = 0;
      float16* in_data = input->data<float16>();

      for (auto& out : param_.outputs) {
        float16* out_data = out->mutableData<float16>();
        auto out_stride = stride_numel(out->shape().dims());

        StridedNumelCopyWithAxis<float16>(axis,
                                          out_data,
                                          out_stride,
                                          in_data + input_offset,
                                          in_stride,
                                          out_stride[axis]);
        input_offset += out_stride[axis];
108
        // out->flush();
Y
Yan Chunwei 已提交
109 110 111 112 113 114 115
      }
      return true;
    }

    std::vector<Tensor*> outputs = param_.outputs;

    int in_channel = input->shape().channel();
116
    // int split_channel = input->shape().channel() / param_.num;
Y
Yan Chunwei 已提交
117 118 119
    int hw = input->shape().height() * input->shape().width();

    float16* in_data = input->data<float16>();
120

Y
Yan Chunwei 已提交
121
    for (int i = 0; i < hw; i++) {
122
      int channel_stride = 0;
Y
Yan Chunwei 已提交
123 124 125
      for (int n = 0; n < outputs.size(); n++) {
        Tensor* out = outputs[n];
        float16* out_data = out->data<float16>();
126 127 128 129
        memcpy(out_data + i * out->shape().channel(),
               in_data + i * in_channel + channel_stride,
               out->shape().channel() * sizeof(float16));
        channel_stride += out->shape().channel();
Y
Yan Chunwei 已提交
130 131
      }
    }
132

Y
Yan Chunwei 已提交
133 134
    for (int n = 0; n < outputs.size(); n++) {
      Tensor* out = outputs[n];
135
      out->flush();
Y
Yan Chunwei 已提交
136 137 138 139 140 141 142 143 144 145
      out->copyScaleFrom(input);
    }
    return true;
  }

  SplitParam& param() { return param_; }

 private:
  SplitParam param_;
};
146

Y
Yan Chunwei 已提交
147 148
}  // namespace zynqmp
}  // namespace paddle