batchnorm_kernel.cpp 5.2 KB
Newer Older
H
Hao Han 已提交
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
L
liuruilong 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

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 BATCHNORM_OP

#include "operators/kernel/batchnorm_kernel.h"
S
sharper 已提交
18 19 20 21
#ifdef PADDLE_MOBILE_MALI_GPU
#include "acl_operator.h"
#include "framework/operator.h"
#include "operators/op_param.h"
L
liuruilong 已提交
22 23 24 25

namespace paddle_mobile {
namespace operators {

S
sharper 已提交
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
template <typename DeviceType, typename T>
class AclBatchNormOp : public acl::ACLOperator {
 public:
  AclBatchNormOp() {
    this->force_bypass_acl_path_ = bypass_acl_class_layer & FLAGS_ENABLE_ACL_BN;
  }
  ~AclBatchNormOp() = default;
  AclBatchNormOp(const AclBatchNormOp&) = delete;
  AclBatchNormOp& operator=(const AclBatchNormOp&) = delete;
  AclBatchNormOp(AclBatchNormOp&&) = delete;
  AclBatchNormOp& operator=(AclBatchNormOp&&) = delete;

  acl::AclParameters& getargs() { return args; }
  void InitAclLayer(const BatchNormParam& param) {
    setTargetHint(acl::TargetHint::OPENCL);
    arm_compute::TensorShape input_shape(args.in_cols, args.in_rows,
                                         args.in_depth, args.batch);
    arm_compute::TensorShape output_shape(args.out_cols, args.out_rows,
                                          args.out_depth, args.out_num);

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

    arm_compute::TensorShape mean_shape(args.in_depth);
    arm_compute::TensorShape var_shape = mean_shape;
    arm_compute::TensorShape beta_shape = mean_shape;
    arm_compute::TensorShape gamma_shape = mean_shape;

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

    new_tensor(mean(), mean_shape, args.mean_data);
    new_tensor(var(), var_shape, args.var_data);
    new_tensor(beta(), beta_shape, args.biases_data);
    new_tensor(gamma(), gamma_shape, args.weight_data);

    acl_configure(bn, this, args.epsilon);
  }

  void RunAcl(void* input, void* output) {
    acl::ACLOperator::acl_run(input, output);
  }
  bool Bypass_acl(const BatchNormParam& param) {
    bool bypass_acl = false;
    AclParametersByContext(param);
H
Hao Han 已提交
74
    InitAclLayer(param);
S
sharper 已提交
75 76 77 78 79 80 81 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    // for performance, more groups impact GPU performance
    if (this->force_bypass_acl_path_) {
      bypass_acl = true;
    }

    return bypass_acl;
  }

 private:
  void AclParametersByContext(const BatchNormParam& param) {
    const Tensor* in_x = param.InputX();
    Tensor* out = param.OutputY();
    const Tensor* scale = param.InputScale();
    const Tensor* bias = param.InputBias();
    const Tensor* saved_mean = param.InputMean();
    const Tensor* saved_variance = param.InputVariance();

    const T* input_data = in_x->data<T>();
    T* output_data = out->mutable_data<T>();
    const T* weight_data = scale->data<T>();
    const T* bias_data = bias->data<T>();
    const T* mean_data = saved_mean->data<T>();
    const T* var_data = saved_variance->data<T>();

    float epsilon = param.Epsilon();

    args.input_data = (void*)input_data;
    args.output_data = (void*)output_data;
    // args.weight_data = (void*)weight_data;
    // args.biases_data = (void*)bias_data;
    args.mean_data = (void*)mean_data;
    args.var_data = (void*)var_data;
    args.epsilon = epsilon;

    args.dim = in_x->dims().size();

    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];

    args.out_num = out->dims()[0];
    args.out_depth = out->dims()[1];
    args.out_rows = out->dims()[2];
    args.out_cols = out->dims()[3];

    args.weight_data = (void*)weight_data;
    args.biases_data = (void*)bias_data;

    // std::cout
    //  << "Out C: " <<  args.out_depth
    //  << " H: " << args.out_rows << " W: " << args.out_cols << "\n";
  }
  acl::AclParameters args;
};

L
liuruilong 已提交
131
template <>
E
eclipsess 已提交
132
bool BatchNormKernel<GPU_MALI, float>::Init(BatchNormParam* param) {
S
sharper 已提交
133 134 135 136 137 138
  AclBatchNormOp<GPU_MALI, float>* acl_op =
      reinterpret_cast<AclBatchNormOp<GPU_MALI, float>*>(this->GetAclOp());
  if (acl_op == nullptr) {
    acl_op = new AclBatchNormOp<GPU_MALI, float>();
    this->SetAclOp((void*)acl_op, (void*)this);
  }
H
halsay 已提交
139
  if (acl_op->Bypass_acl(*param)) {
H
Hao Han 已提交
140 141 142
    std::cout << "init acl failed" << std::endl;
    return false;
  }
L
liuruilong 已提交
143 144 145
  return true;
}

L
liuruilong 已提交
146
template <>
L
liuruilong 已提交
147
void BatchNormKernel<GPU_MALI, float>::Compute(
S
sharper 已提交
148 149 150 151 152 153 154 155
    const BatchNormParam& param) const {
  std::cout << "init acl" << std::endl;
  AclBatchNormOp<GPU_MALI, float>* acl_op =
      reinterpret_cast<AclBatchNormOp<GPU_MALI, float>*>(this->GetAclOp());
  if (acl_op == nullptr) {
    return;
  }
  acl::AclParameters& args = acl_op->getargs();
H
Hao Han 已提交
156
  acl_op->RunAcl(args.input_data, args.output_data);
S
sharper 已提交
157
}
L
liuruilong 已提交
158

S
sharper 已提交
159
template class BatchNormKernel<GPU_MALI, float>;
L
liuruilong 已提交
160 161 162 163
}  // namespace operators
}  // namespace paddle_mobile

#endif
S
sharper 已提交
164
#endif