lrn_kernel.cpp 4.5 KB
Newer Older
H
Hao Han 已提交
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
S
sharper 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

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. */

#ifdef LRN_OP

#pragma once

#include "operators/kernel/lrn_kernel.h"
#ifdef PADDLE_MOBILE_MALI_GPU
#include "acl_operator.h"
#include "framework/operator.h"
H
halsay 已提交
23
#include "operators/kernel/central-arm-func/lrn_arm_func.h"
S
sharper 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include "operators/op_param.h"

namespace paddle_mobile {
namespace operators {

template <typename DeviceType, typename T>
class AclLrnOp : public acl::ACLOperator {
 public:
  AclLrnOp() {
    this->force_bypass_acl_path_ =
        bypass_acl_class_layer & FLAGS_ENABLE_ACL_LRN;
  }
  ~AclLrnOp() = default;
  AclLrnOp(const AclLrnOp&) = delete;
  AclLrnOp& operator=(const AclLrnOp&) = delete;
  AclLrnOp(AclLrnOp&&) = delete;
  AclLrnOp& operator=(AclLrnOp&&) = delete;

  acl::AclParameters& getargs() { return args; }
N
nhzlx 已提交
43
  void InitAclLayer(const LrnParam<DeviceType>& param) {
S
sharper 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    setTargetHint(acl::TargetHint::OPENCL);
    arm_compute::TensorShape shape(args.in_cols, args.in_rows, args.in_depth);

    if (is_operator_init_done(shape)) return;
    set_operator_init_done();
    this->force_bypass_acl_path_ = false;

    arm_compute::NormalizationLayerInfo norm_info(
        arm_compute::NormType::CROSS_MAP, args.nsize, args.alpha, args.beta,
        args.knorm);

    //[width, height, IFM]
    new_tensor(input(), shape, args.input_data);
    //[width, height, OFM]
    new_tensor(output(), shape, args.output_data);

    acl_configure(lrn, this, norm_info);
  }

H
halsay 已提交
63 64
  void Set_bypass(bool bypass) { args.is_bypass = bypass; }

S
sharper 已提交
65 66 67
  void RunAcl(void* input, void* output) {
    acl::ACLOperator::acl_run(input, output);
  }
N
nhzlx 已提交
68
  bool Bypass_acl(const LrnParam<DeviceType>& param) {
S
sharper 已提交
69 70
    bool bypass_acl = false;
    AclParametersByContext(param);
H
halsay 已提交
71
    InitAclLayer(param);
S
sharper 已提交
72 73 74 75 76 77 78 79 80
    // for performance, more groups impact GPU performance
    if (this->force_bypass_acl_path_) {
      bypass_acl = true;
    }

    return bypass_acl;
  }

 private:
N
nhzlx 已提交
81
  void AclParametersByContext(const LrnParam<DeviceType>& param) {
S
sharper 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    const Tensor* in_x = param.InputX();
    Tensor* out = param.Out();

    int n = param.N();
    T alpha = param.Alpha();
    T beta = param.Beta();
    T k = param.K();

    const T* input_data = in_x->data<T>();
    T* output_data = out->mutable_data<T>();

    args.input_data = (void*)input_data;
    args.output_data = (void*)output_data;

    args.nsize = n;
    args.alpha = alpha;
    args.beta = beta;
    args.knorm = k;

    // NCHW
    args.batch = in_x->dims()[0];
    args.in_depth = in_x->dims()[1];
    args.in_rows = in_x->dims()[2];
    args.in_cols = in_x->dims()[3];
    // std::cout
    //  << "Out C: " <<  args.out_depth
    //  << " H: " << args.out_rows << " W: " << args.out_cols << "\n";
  }
  acl::AclParameters args;
};

template <>
N
nhzlx 已提交
114
bool LrnKernel<GPU_MALI, float>::Init(LrnParam<GPU_MALI>* param) {
S
sharper 已提交
115 116 117 118 119 120
  AclLrnOp<GPU_MALI, float>* acl_op =
      reinterpret_cast<AclLrnOp<GPU_MALI, float>*>(this->GetAclOp());
  if (acl_op == nullptr) {
    acl_op = new AclLrnOp<GPU_MALI, float>();
    this->SetAclOp((void*)acl_op, (void*)this);
  }
H
halsay 已提交
121 122 123 124 125
  if (acl_op->Bypass_acl(*param)) {
    acl_op->Set_bypass(true);
    std::cout << "init acl failed" << std::endl;
    return true;
  }
S
sharper 已提交
126 127 128 129
  return true;
}

template <>
N
nhzlx 已提交
130
void LrnKernel<GPU_MALI, float>::Compute(
L
liuruilong 已提交
131
    const LrnParam<GPU_MALI>& param) {
S
sharper 已提交
132 133 134 135 136 137
  std::cout << "init acl" << std::endl;
  AclLrnOp<GPU_MALI, float>* acl_op =
      reinterpret_cast<AclLrnOp<GPU_MALI, float>*>(this->GetAclOp());
  if (acl_op == nullptr) {
    return;
  }
H
halsay 已提交
138 139 140 141
  acl::AclParameters& args = acl_op->getargs();
  if (args.is_bypass) {
    std::cout << "bypass op" << std::endl;
    LrnCompute<float>(param);
S
sharper 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    return;
  }
  const float* input_data = (const float*)args.input_data;
  const float* output_data = (const float*)args.output_data;
  for (int n = 0; n < args.batch; ++n) {
    acl_op->RunAcl((void*)input_data, (void*)output_data);
    input_data += args.in_depth * args.in_cols * args.in_rows;
    output_data += args.in_depth * args.in_cols * args.in_rows;
  }
}

template class LrnKernel<GPU_MALI, float>;
}  // namespace operators
}  // namespace paddle_mobile

#endif
#endif