layout_compute.h 4.9 KB
Newer Older
J
jackzhang235 已提交
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
// 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 <Eigen/Core>
#include <string>
#include <vector>
#include "lite/backends/x86/math/math_function.h"
#include "lite/core/kernel.h"
#include "lite/core/op_lite.h"
#include "lite/core/op_registry.h"
#include "lite/core/type_system.h"
#include "lite/operators/layout_op.h"

namespace paddle {
namespace lite {
namespace kernels {
namespace mlu {

J
jackzhang235 已提交
32 33 34 35 36
template <paddle::lite_api::PrecisionType>
struct FPTypeTraits {};

template <>
struct FPTypeTraits<paddle::lite_api::PrecisionType::kFloat> {
37
  using type = float;
J
jackzhang235 已提交
38 39 40 41
};

template <>
struct FPTypeTraits<paddle::lite_api::PrecisionType::kFP16> {
42
  using type = paddle::lite::fluid::float16;
J
jackzhang235 已提交
43 44 45 46
};

template <>
struct FPTypeTraits<paddle::lite_api::PrecisionType::kInt8> {
47
  using type = int8_t;
J
jackzhang235 已提交
48 49
};

J
jackzhang235 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
template <lite::TargetType Target, typename T>
inline void LayoutTransCompute(const int dim,
                               const lite::Context<Target>& context,
                               const lite::Tensor& in,
                               lite::Tensor* out,
                               const std::vector<int>& axis) {
  switch (dim) {
    case 2:
      paddle::lite::x86::math::Transpose<lite::TargetType::kX86, T, 2> trans2;
      trans2(context, in, out, axis);
      break;
    case 3:
      paddle::lite::x86::math::Transpose<lite::TargetType::kX86, T, 3> trans3;
      trans3(context, in, out, axis);
      break;
    case 4:
      paddle::lite::x86::math::Transpose<lite::TargetType::kX86, T, 4> trans4;
      trans4(context, in, out, axis);
      break;
    default:
      CHECK(0) << ("Unsupport dim in mlu layout");
  }
}

template <PrecisionType Precision>
class LayoutNchwToNhwcCompute
J
jackzhang235 已提交
76
    : public KernelLite<TARGET(kX86), Precision, DATALAYOUT(kNCHW)> {
J
jackzhang235 已提交
77 78 79 80 81 82 83
 public:
  using param_t = operators::LayoutParam;

  void Run() override {
    auto& param = this->template Param<param_t>();
    auto* x = param.x;
    auto* out = param.y;
84 85
    out->template mutable_data<typename FPTypeTraits<Precision>::type>();
    auto x_ndims = param.x->dims().size();
J
jackzhang235 已提交
86 87
    auto& context = this->ctx_->template As<X86Context>();

88 89
    const auto origin_dims = out->dims().Vectorize();

J
jackzhang235 已提交
90
    std::vector<int> axis;
91
    switch (x_ndims) {
J
jackzhang235 已提交
92 93 94 95 96 97
      case 2:
        axis = {0, 1};
        break;
      case 3:
        axis = {0, 2, 1};
        out->Resize(std::vector<int64_t>{
98
            origin_dims[0], origin_dims[2], origin_dims[1]});
J
jackzhang235 已提交
99 100 101 102
        break;
      case 4:
        axis = {0, 2, 3, 1};
        out->Resize(std::vector<int64_t>{
103
            origin_dims[0], origin_dims[2], origin_dims[3], origin_dims[1]});
J
jackzhang235 已提交
104 105 106 107 108
        break;
      default:
        CHECK(0) << "Unsupport dim in mlu layout nchw to nhwc";
    }

J
jackzhang235 已提交
109
    LayoutTransCompute<lite::TargetType::kX86,
110 111
                       typename FPTypeTraits<Precision>::type>(
        x_ndims, context, *x, out, axis);
112

113
    if (x_ndims > 2) {
114 115
      out->Resize(origin_dims);
    }
J
jackzhang235 已提交
116 117 118 119 120 121 122 123 124
  }

  std::string doc() const override {
    return "Mlu layout transform nchw to nhwc";
  }
};

template <PrecisionType Precision>
class LayoutNhwcToNchwCompute
J
jackzhang235 已提交
125
    : public KernelLite<TARGET(kX86), Precision, DATALAYOUT(kNCHW)> {
J
jackzhang235 已提交
126 127 128 129 130 131 132
 public:
  using param_t = operators::LayoutParam;

  void Run() override {
    auto& param = this->template Param<param_t>();
    auto* x = param.x;
    auto* out = param.y;
133
    out->template mutable_data<typename FPTypeTraits<Precision>::type>();
J
jackzhang235 已提交
134 135
    auto& context = this->ctx_->template As<X86Context>();

136 137
    TensorLite tmp_t;
    tmp_t.ShareDataWith(*x);
138

139 140
    const auto x_dims = x->dims().Vectorize();
    auto x_ndims = param.x->dims().size();
J
jackzhang235 已提交
141
    std::vector<int> axis;
142
    switch (x_ndims) {
J
jackzhang235 已提交
143 144 145 146
      case 2:
        axis = {0, 1};
        break;
      case 3:
147
        tmp_t.Resize(std::vector<int64_t>{x_dims[0], x_dims[2], x_dims[1]});
148
        axis = {0, 2, 1};
J
jackzhang235 已提交
149 150
        break;
      case 4:
151 152
        tmp_t.Resize(
            std::vector<int64_t>{x_dims[0], x_dims[2], x_dims[3], x_dims[1]});
153
        axis = {0, 3, 1, 2};
J
jackzhang235 已提交
154 155 156 157 158
        break;
      default:
        CHECK(0) << "Unsupport dim in mlu layout nhwc to nchw";
    }

J
jackzhang235 已提交
159
    LayoutTransCompute<lite::TargetType::kX86,
160 161
                       typename FPTypeTraits<Precision>::type>(
        x_ndims, context, tmp_t, out, axis);
J
jackzhang235 已提交
162 163 164 165 166 167 168 169 170 171 172
  }

  std::string doc() const override {
    return "Mlu layout transform nhwc to nchw";
  }
};

}  // namespace mlu
}  // namespace kernels
}  // namespace lite
}  // namespace paddle