op_param.h 83.2 KB
Newer Older
W
wangliu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2018 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. */
朔-望's avatar
朔-望 已提交
14

15
#pragma once
朔-望's avatar
朔-望 已提交
16

E
eclipsess 已提交
17
#include <string>
W
wangliu 已提交
18
#include <vector>
L
liuruilong 已提交
19
#include "common/log.h"
朔-望's avatar
朔-望 已提交
20
#include "common/type_define.h"
N
nhzlx 已提交
21
#include "common/types.h"
朔-望's avatar
朔-望 已提交
22 23 24 25
#include "framework/lod_tensor.h"
#include "framework/scope.h"
#include "framework/tensor.h"
#include "framework/variable.h"
Z
zhangyang 已提交
26 27 28 29 30 31 32

#ifdef PADDLE_MOBILE_FPGA_V1
#include "fpga/V1/api.h"
#endif

#ifdef PADDLE_MOBILE_FPGA_V2
#include "fpga/V2/api.h"
Z
zhangyang 已提交
33
#endif
朔-望's avatar
朔-望 已提交
34

L
liuruilong 已提交
35 36
#ifdef PADDLE_MOBILE_CL
#include "framework/cl/cl_image.h"
Z
zhangyang 已提交
37
#endif
朔-望's avatar
朔-望 已提交
38 39

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
40 41
namespace operators {

W
wangliu 已提交
42 43 44 45 46
using framework::Attribute;
using framework::AttributeMap;
using framework::LoDTensor;
using framework::Scope;
using framework::Tensor;
E
eclipsess 已提交
47
using framework::Variable;
W
wangliu 已提交
48 49
using std::string;
using std::vector;
朔-望's avatar
朔-望 已提交
50

N
nhzlx 已提交
51 52 53 54 55 56 57 58 59
template <typename Dtype>
struct DtypeTensorTrait {
  // This is the type we obtained in variable.
  typedef framework::LoDTensor gtype;
  // This type will be the parent class type
  // or the same type.
  typedef framework::Tensor rtype;
};

L
update  
liuruilong 已提交
60
#ifdef PADDLE_MOBILE_CL
L
liuruilong 已提交
61 62 63 64 65 66 67 68
template <>
struct DtypeTensorTrait<GPU_CL> {
  // This is the type we obtained in variable.
  typedef framework::CLImage gtype;
  // This type will be the parent class type
  // or the same type.
  typedef framework::CLImage rtype;
};
L
update  
liuruilong 已提交
69
#endif
L
liuruilong 已提交
70

L
liuruilong 已提交
71
class OpParam {
朔-望's avatar
朔-望 已提交
72
 protected:
xiebaiyuan's avatar
xiebaiyuan 已提交
73 74 75 76
  template <typename T>
  static T *InputH0From(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("H0", inputs, scope);
  }
77 78 79 80 81
  template <typename T>
  static T *InputAlphaFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("Alpha", inputs, scope);
  }

82 83 84 85 86 87 88 89 90
  template <typename T>
  static T *InputFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("Input", inputs, scope);
  }

  template <typename T>
  static T *InputXFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("X", inputs, scope);
  }
91 92 93 94 95
  template <typename T>
  static T *InputOutSizeFrom(const VariableNameMap &inputs,
                             const Scope &scope) {
    return GetVarValue<T>("OutSize", inputs, scope);
  }
xiebaiyuan's avatar
xiebaiyuan 已提交
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

  template <typename T>
  static T *InputWFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("W", inputs, scope);
  }

  template <typename T>
  static T *InputIdsFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("Ids", inputs, scope);
  }

  template <typename T>
  static T *InputEmissionFrom(const VariableNameMap &inputs,
                              const Scope &scope) {
    return GetVarValue<T>("Emission", inputs, scope);
  }

  template <typename T>
  static T *InputTransitionFrom(const VariableNameMap &inputs,
                                const Scope &scope) {
    return GetVarValue<T>("Transition", inputs, scope);
  }
  template <typename T>
  static T *InputLabelFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("Label", inputs, scope);
  }

123 124 125 126
  template <typename T>
  static T *InputXFrom1(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue1<T>("addX", inputs, scope);
  }
127 128 129 130 131 132

  template <typename T>
  static T *InputYFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("Y", inputs, scope);
  }

133 134 135 136 137
  template <typename T>
  static T *InputYFrom1(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue1<T>("Y", inputs, scope);
  }

E
eclipsess 已提交
138 139 140 141 142
  template <typename T>
  static T *InputZFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("Z", inputs, scope);
  }

143 144 145 146 147
  template <typename T>
  static T *InputBiasFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("Bias", inputs, scope);
  }
  template <typename T>
xiebaiyuan's avatar
xiebaiyuan 已提交
148 149 150 151
  static T *InputWeightFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("Weight", inputs, scope);
  }
  template <typename T>
152 153 154 155 156 157 158 159 160 161 162 163
  static T *InputVarianceFrom(const VariableNameMap &inputs,
                              const Scope &scope) {
    return GetVarValue<T>("Variance", inputs, scope);
  }
  template <typename T>
  static T *InputMeanFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("Mean", inputs, scope);
  }
  template <typename T>
  static T *InputScaleFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("Scale", inputs, scope);
  }
E
eclipsess 已提交
164 165 166 167
  template <typename T>
  static T *InputImageFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("Image", inputs, scope);
  }
E
eclipsess 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  template <typename T>
  static T *InputPriorBoxFrom(const VariableNameMap &inputs,
                              const Scope &scope) {
    return GetVarValue<T>("PriorBox", inputs, scope);
  }
  template <typename T>
  static T *InputPriorBoxVarFrom(const VariableNameMap &inputs,
                                 const Scope &scope) {
    return GetVarValue<T>("PriorBoxVar", inputs, scope);
  }
  // LoDTensor but now use Tensor
  template <typename T>
  static T *InputTargetBoxFrom(const VariableNameMap &inputs,
                               const Scope &scope) {
    return GetVarValue<T>("TargetBox", inputs, scope);
  }
184

E
eclipsess 已提交
185 186 187 188 189 190 191 192 193 194
  template <typename T>
  static T *InputBBoxesFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("BBoxes", inputs, scope);
  }

  template <typename T>
  static T *InputScoresFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("Scores", inputs, scope);
  }

E
eclipsess 已提交
195 196 197 198
  template <typename T>
  static T *InputShapeFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("Shape", inputs, scope);
  }
E
eclipsess 已提交
199

200
  template <typename T>
W
wangliu 已提交
201 202
  static vector<T *> InputMultiFrom(const VariableNameMap &inputs,
                                    const Scope &scope) {
203 204 205
    return GetMultiVarValue<T>("X", inputs, scope);
  }

E
eclipsess 已提交
206 207 208 209 210
  static vector<Variable *> InputMultiVarsFrom(const VariableNameMap &inputs,
                                               const Scope &scope) {
    return GetMultiVar("X", inputs, scope);
  }

xiebaiyuan's avatar
xiebaiyuan 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  template <typename T>
  static T *OutputBatchGateFrom(const VariableNameMap &outputs,
                                const Scope &scope) {
    return GetVarValue<T>("BatchGate", outputs, scope);
  }

  template <typename T>
  static T *OutputViterbiPathFrom(const VariableNameMap &outputs,
                                  const Scope &scope) {
    return GetVarValue<T>("ViterbiPath", outputs, scope);
  }
  template <typename T>
  static T *OutputBatchResetHiddenPrevFrom(const VariableNameMap &outputs,
                                           const Scope &scope) {
    return GetVarValue<T>("BatchResetHiddenPrev", outputs, scope);
  }

  template <typename T>
  static T *OutputBatchHiddenFrom(const VariableNameMap &outputs,
                                  const Scope &scope) {
    return GetVarValue<T>("BatchHidden", outputs, scope);
  }

  template <typename T>
  static T *OutputHiddenFrom(const VariableNameMap &outputs,
                             const Scope &scope) {
    return GetVarValue<T>("Hidden", outputs, scope);
  }

240 241 242 243 244
  template <typename T>
  static T *OutputFrom(const VariableNameMap &outputs, const Scope &scope) {
    return GetVarValue<T>("Output", outputs, scope);
  }

E
eclipsess 已提交
245 246 247 248 249
  static Variable *OutVarFrom(const VariableNameMap &outputs,
                              const Scope &scope) {
    return GetVar("Out", outputs, scope);
  }

250 251 252 253 254
  template <typename T>
  static T *OutFrom(const VariableNameMap &outputs, const Scope &scope) {
    return GetVarValue<T>("Out", outputs, scope);
  }

xiebaiyuan's avatar
xiebaiyuan 已提交
255 256 257 258 259 260
  template <typename T>
  static vector<T *> OutMultiFrom(const VariableNameMap &outputs,
                                  const Scope &scope) {
    return GetMultiVarValue<T>("Out", outputs, scope);
  }

261 262 263 264 265
  template <typename T>
  static T *OutputYFrom(const VariableNameMap &outputs, const Scope &scope) {
    return GetVarValue<T>("Y", outputs, scope);
  }

L
lijiancheng0614 已提交
266 267 268 269 270 271
  template <typename T>
  static T *OutputXShapeFrom(const VariableNameMap &outputs,
                             const Scope &scope) {
    return GetVarValue<T>("XShape", outputs, scope);
  }

E
eclipsess 已提交
272 273 274 275 276 277
  template <typename T>
  static T *OutputBoxesFrom(const VariableNameMap &outputs,
                            const Scope &scope) {
    return GetVarValue<T>("Boxes", outputs, scope);
  }

E
eclipsess 已提交
278 279 280 281 282
  template <typename T>
  static T *OutputBoxFrom(const VariableNameMap &outputs, const Scope &scope) {
    return GetVarValue<T>("OutputBox", outputs, scope);
  }

E
eclipsess 已提交
283 284 285 286 287 288
  template <typename T>
  static T *OutputVariancesFrom(const VariableNameMap &outputs,
                                const Scope &scope) {
    return GetVarValue<T>("Variances", outputs, scope);
  }

289 290 291 292 293 294 295 296 297 298 299
  template <typename T>
  static T *MidOutFrom(const VariableNameMap &outputs, const Scope &scope) {
    return GetVarValue<T>("MidOut", outputs, scope);
  }

  template <typename T>
  static T *FilterFrom(const VariableNameMap &inputs, const Scope &scope) {
    return GetVarValue<T>("Filter", inputs, scope);
  }

  template <typename T>
W
wangliu 已提交
300
  static const T GetAttr(const string &key, const AttributeMap &map) {
301 302
    return ((Attribute)map.at(key)).Get<T>();
  }
xiebaiyuan's avatar
xiebaiyuan 已提交
303 304
  static const std::string GetStringAttr(const string &key,
                                         const AttributeMap &map) {
305 306
    return ((Attribute)map.at(key)).GetString();
  }
307

308 309 310 311
  static const bool HasAttr(const string &key, const AttributeMap &map) {
    return map.count(key) > 0;
  }

312
  template <typename T>
W
wangliu 已提交
313
  static T *GetVarValue(const string &key, const VariableNameMap &var_map,
314
                        const Scope &scope) {
W
wangliu 已提交
315 316
    PADDLE_MOBILE_ENFORCE(var_map.count(key) > 0,
                          "%s is not contained in var_map", key.c_str())
317 318 319 320 321 322
    auto var_vec = var_map.at(key);
    if (!var_vec.empty()) {
      auto var = scope.FindVar(var_vec[0]);
      return var->GetMutable<T>();
    } else {
      return nullptr;
朔-望's avatar
朔-望 已提交
323
    }
324
  }
朔-望's avatar
朔-望 已提交
325

E
eclipsess 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338
  static Variable *GetVar(const string &key, const VariableNameMap &var_map,
                          const Scope &scope) {
    PADDLE_MOBILE_ENFORCE(var_map.count(key) > 0,
                          "%s is not contained in var_map", key.c_str())
    auto var_vec = var_map.at(key);
    if (!var_vec.empty()) {
      auto var = scope.FindVar(var_vec[0]);
      return var;
    } else {
      return nullptr;
    }
  }

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
  static std::string getkey(const string &key, const VariableNameMap &var_map,
                            int index) {
    auto var_vec = var_map.at(key);
    return var_vec[index];
  }

  template <typename T>
  static T *GetVarValue1(const string &key, const VariableNameMap &var_map,
                         const Scope &scope) {
    PADDLE_MOBILE_ENFORCE(var_map.count(key) > 0,
                          "%s is not contained in var_map", key.c_str())
    auto var_vec = var_map.at(key);
    if (!var_vec.empty()) {
      auto var = scope.FindVar(var_vec[1]);
      return var->GetMutable<T>();
    } else {
      return nullptr;
    }
  }

359
  template <typename T>
W
wangliu 已提交
360 361 362
  static vector<T *> GetMultiVarValue(const string &key,
                                      const VariableNameMap &var_map,
                                      const Scope &scope) {
363 364
    auto var_vecs = var_map.at(key);
    assert(var_vecs.size() > 1);
W
wangliu 已提交
365
    vector<T *> var_res;
366 367 368
    for (auto &var_vec : var_vecs) {
      auto var = scope.FindVar(var_vec);
      var_res.push_back(var->GetMutable<T>());
朔-望's avatar
朔-望 已提交
369
    }
370 371
    return var_res;
  }
E
eclipsess 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384

  static vector<Variable *> GetMultiVar(const string &key,
                                        const VariableNameMap &var_map,
                                        const Scope &scope) {
    auto var_vecs = var_map.at(key);
    assert(var_vecs.size() > 1);
    vector<Variable *> var_res;
    for (auto &var_vec : var_vecs) {
      auto var = scope.FindVar(var_vec);
      var_res.push_back(var);
    }
    return var_res;
  }
朔-望's avatar
朔-望 已提交
385 386
};

N
nhzlx 已提交
387
template <typename Dtype>
388
class ConvParam : public OpParam {
N
nhzlx 已提交
389 390 391
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

朔-望's avatar
朔-望 已提交
392
 public:
393
  ConvParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
394
            const AttributeMap &attrs, const Scope &scope) {
395 396 397 398 399 400 401 402 403
    filter_ = OpParam::FilterFrom<GType>(inputs, scope);
    input_ = OpParam::InputFrom<GType>(inputs, scope);
    if (outputs.count("Output")) {
      output_ = OpParam::OutputFrom<GType>(outputs, scope);
    }
    strides_ = OpParam::GetAttr<vector<int>>("strides", attrs);
    paddings_ = OpParam::GetAttr<vector<int>>("paddings", attrs);
    dilations_ = OpParam::GetAttr<vector<int>>("dilations", attrs);
    groups = OpParam::GetAttr<int>("groups", attrs);
404
  }
朔-望's avatar
朔-望 已提交
405

N
nhzlx 已提交
406
  const RType *Input() const { return input_; }
朔-望's avatar
朔-望 已提交
407

408
  RType *Filter() const { return filter_; }
朔-望's avatar
朔-望 已提交
409

410
  RType *Output() const { return output_; }
朔-望's avatar
朔-望 已提交
411

W
wangliu 已提交
412
  const vector<int> &Strides() const { return strides_; }
朔-望's avatar
朔-望 已提交
413

W
wangliu 已提交
414
  const vector<int> &Paddings() const { return paddings_; }
朔-望's avatar
朔-望 已提交
415

W
wangliu 已提交
416
  const vector<int> &Dilations() const { return dilations_; }
朔-望's avatar
朔-望 已提交
417

H
hjchen2 已提交
418 419 420 421
  enum ExecMode {
    EXEC_INVALID = 0,
    EXEC_GEMM_FLOAT,
    EXEC_DEPTHWISE3x3S1P1_FLOAT,
422 423
    EXEC_DEPTHWISE3x3S2P0_FLOAT,
    EXEC_DEPTHWISE3x3S2P1_FLOAT,
H
hjchen2 已提交
424 425 426
    EXEC_DEPTHWISE3x3_FLOAT,
    EXEC_WINOGRAD3X3_FLOAT,
    EXEC_WINOGRAD5X5_FLOAT,
427
    EXEC_DEPTHWISE5x5_FLOAT,
H
hjchen2 已提交
428
    EXEC_GEMM_INT8,
H
hjchen2 已提交
429
    EXEC_DEPTHWISE3x3_INT8,
430
    EXEC_DEPTHWISE5x5_INT8,
H
hjchen2 已提交
431 432 433 434
  };

  ExecMode &ExecMode() const { return exec_mode_; }

435
  const int &Groups() const { return groups; }
朔-望's avatar
朔-望 已提交
436

437 438 439 440 441 442 443
#ifdef PADDLE_MOBILE_CL
  int Offset() const { return offset_; }

  int SetOffset(int in_offset) { offset_ = in_offset; }

#endif

H
hjchen2 已提交
444
 public:
N
nhzlx 已提交
445
  RType *input_;
446 447
  RType *output_;
  RType *filter_;
H
hjchen2 已提交
448
  RType *transformed_filter_;
W
wangliu 已提交
449 450 451
  vector<int> strides_;
  vector<int> paddings_;
  vector<int> dilations_;
H
hjchen2 已提交
452
  mutable enum ExecMode exec_mode_;
453
  int groups;
454 455 456 457

#ifdef PADDLE_MOBILE_CL
  int offset_;
#endif
Z
zhangyang 已提交
458 459 460

#ifdef PADDLE_MOBILE_FPGA

H
hjchen2 已提交
461
 public:
Z
zhangyang 已提交
462 463 464 465 466
  fpga::SplitConvArgs fpga_conv_args;

 public:
  const fpga::SplitConvArgs &FpgaArgs() const { return fpga_conv_args; }
  void SetFpgaArgs(const fpga::SplitConvArgs &args) { fpga_conv_args = args; }
467 468 469 470 471 472 473

 public:
  fpga::DWconvArgs fpga_dwconv_args;

 public:
  const fpga::DWconvArgs &FpgaDwconvArgs() const { return fpga_dwconv_args; }
  void SetFpgaArgs(const fpga::DWconvArgs &args) { fpga_dwconv_args = args; }
Z
zhangyang 已提交
474
#endif
朔-望's avatar
朔-望 已提交
475
};
N
nhzlx 已提交
476 477
template <typename Dtype>
Print &operator<<(Print &printer, const ConvParam<Dtype> &conv_param);
朔-望's avatar
朔-望 已提交
478

N
nhzlx 已提交
479
template <typename Dtype>
朔-望's avatar
朔-望 已提交
480
class ElementwiseAddParam : OpParam {
N
nhzlx 已提交
481 482 483
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

朔-望's avatar
朔-望 已提交
484
 public:
485
  ElementwiseAddParam(const VariableNameMap &inputs,
486 487
                      const VariableNameMap &outputs, const AttributeMap &attrs,
                      const Scope &scope) {
N
nhzlx 已提交
488 489 490
    input_x_ = InputXFrom<GType>(inputs, scope);
    input_y_ = InputYFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
491 492 493
    axis_ = GetAttr<int>("axis", attrs);
  }

xiebaiyuan's avatar
xiebaiyuan 已提交
494
  const GType *InputX() const { return input_x_; }
495

xiebaiyuan's avatar
xiebaiyuan 已提交
496
  const GType *InputY() const { return input_y_; }
497

xiebaiyuan's avatar
xiebaiyuan 已提交
498
  GType *Out() const { return out_; }
499 500 501

  const int &Axis() const { return axis_; }

朔-望's avatar
朔-望 已提交
502
 private:
xiebaiyuan's avatar
xiebaiyuan 已提交
503 504 505
  GType *input_x_;
  GType *input_y_;
  GType *out_;
506
  int axis_;
Z
zhangyang 已提交
507 508 509
#ifdef PADDLE_MOBILE_FPGA

 private:
H
hanbuhe 已提交
510
  fpga::EWAddArgs fpga_EW_add_args;
Z
zhangyang 已提交
511 512

 public:
H
hanbuhe 已提交
513 514
  const fpga::EWAddArgs &FpgaArgs() const { return fpga_EW_add_args; }
  void SetFpgaArgs(const fpga::EWAddArgs &args) { fpga_EW_add_args = args; }
Z
zhangyang 已提交
515
#endif
朔-望's avatar
朔-望 已提交
516 517
};

E
eclipsess 已提交
518
#ifdef ELEMENTWISEMUL_OP
E
eclipsess 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
template <typename Dtype>
class ElementwiseMulParam : OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  ElementwiseMulParam(const VariableNameMap &inputs,
                      const VariableNameMap &outputs, const AttributeMap &attrs,
                      const Scope &scope) {
    input_x_ = InputXFrom<GType>(inputs, scope);
    input_y_ = InputYFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
    axis_ = GetAttr<int>("axis", attrs);
  }

  const GType *InputX() const { return input_x_; }

  const GType *InputY() const { return input_y_; }

  GType *Out() const { return out_; }

  const int &Axis() const { return axis_; }

 private:
  GType *input_x_;
  GType *input_y_;
  GType *out_;
  int axis_;
};
S
suiyang 已提交
548
#endif
E
eclipsess 已提交
549

550
#ifdef FUSION_ELEMENTWISEADDRELU_OP
N
nhzlx 已提交
551 552
template <typename Dtype>
using ElementwiseAddReluParam = ElementwiseAddParam<Dtype>;
L
liuruilong 已提交
553 554
#endif

555
#ifdef ELEMENTWISESUB_OP
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
template <typename Dtype>
class ElementwiseSubParam : OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  ElementwiseSubParam(const VariableNameMap &inputs,
                      const VariableNameMap &outputs, const AttributeMap &attrs,
                      const Scope &scope) {
    input_x_ = InputXFrom<GType>(inputs, scope);
    input_y_ = InputYFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
    axis_ = GetAttr<int>("axis", attrs);
  }

  const GType *InputX() const { return input_x_; }

  const GType *InputY() const { return input_y_; }

  GType *Out() const { return out_; }

  const int &Axis() const { return axis_; }

 private:
  GType *input_x_;
  GType *input_y_;
  GType *out_;
  int axis_;
};
585
#endif
586

L
liuruilong 已提交
587
#ifdef MUL_OP
N
nhzlx 已提交
588
template <typename Dtype>
朔-望's avatar
朔-望 已提交
589
class MulParam : OpParam {
N
nhzlx 已提交
590 591 592
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

朔-望's avatar
朔-望 已提交
593
 public:
594
  MulParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
595
           const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
596 597 598
    input_x_ = InputXFrom<GType>(inputs, scope);
    input_y_ = InputYFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
599 600 601
    x_num_col_dims_ = GetAttr<int>("x_num_col_dims", attrs);
    y_num_col_dims_ = GetAttr<int>("y_num_col_dims", attrs);
  }
朔-望's avatar
朔-望 已提交
602

xiebaiyuan's avatar
xiebaiyuan 已提交
603
  const GType *InputX() const { return input_x_; }
朔-望's avatar
朔-望 已提交
604

xiebaiyuan's avatar
xiebaiyuan 已提交
605
  const GType *InputY() const { return input_y_; }
朔-望's avatar
朔-望 已提交
606

xiebaiyuan's avatar
xiebaiyuan 已提交
607
  GType *Out() const { return out_; }
朔-望's avatar
朔-望 已提交
608

609
  const int &XNumColDims() const { return x_num_col_dims_; }
朔-望's avatar
朔-望 已提交
610

611
  const int &YNumColDims() const { return y_num_col_dims_; }
朔-望's avatar
朔-望 已提交
612

朔-望's avatar
朔-望 已提交
613
 private:
xiebaiyuan's avatar
xiebaiyuan 已提交
614 615 616
  GType *input_x_;
  GType *input_y_;
  GType *out_;
617 618
  int x_num_col_dims_;
  int y_num_col_dims_;
朔-望's avatar
朔-望 已提交
619
};
L
liuruilong 已提交
620
#endif
朔-望's avatar
朔-望 已提交
621

L
liuruilong 已提交
622
#ifdef CONCAT_OP
N
nhzlx 已提交
623
template <typename Dtype>
朔-望's avatar
朔-望 已提交
624
class ConcatParam : public OpParam {
N
nhzlx 已提交
625 626 627
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

朔-望's avatar
朔-望 已提交
628
 public:
629
  ConcatParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
630
              const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
631 632
    inputs_ = InputMultiFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
633 634
    axis_ = GetAttr<int>("axis", attrs);
  }
朔-望's avatar
朔-望 已提交
635

N
nhzlx 已提交
636
  vector<GType *> Inputs() const { return inputs_; }
朔-望's avatar
朔-望 已提交
637

xiebaiyuan's avatar
xiebaiyuan 已提交
638
  GType *Out() const { return out_; }
朔-望's avatar
朔-望 已提交
639

640
  const int &Axis() const { return axis_; }
朔-望's avatar
朔-望 已提交
641

朔-望's avatar
朔-望 已提交
642
 private:
N
nhzlx 已提交
643
  vector<GType *> inputs_;
xiebaiyuan's avatar
xiebaiyuan 已提交
644
  GType *out_;
645
  int axis_;
Z
zhangyang 已提交
646 647 648 649 650 651 652 653 654
#ifdef PADDLE_MOBILE_FPGA

 private:
  fpga::ConcatArgs fpga_concat_args;

 public:
  const fpga::ConcatArgs &FpgaArgs() const { return fpga_concat_args; }
  void SetFpgaArgs(const fpga::ConcatArgs &args) { fpga_concat_args = args; }
#endif
朔-望's avatar
朔-望 已提交
655
};
L
liuruilong 已提交
656
#endif
朔-望's avatar
朔-望 已提交
657

E
eclipsess 已提交
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
#ifdef SUM_OP
template <typename Dtype>
class SumParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  SumParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
           const AttributeMap &attrs, const Scope &scope) {
    inputs_vars_ = InputMultiVarsFrom(inputs, scope);
    out_var_ = OutVarFrom(outputs, scope);
    inputs_ = InputMultiFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
  }

  vector<Variable *> InputsVars() const { return inputs_vars_; }

  Variable *OutVar() const { return out_var_; }

  vector<GType *> Inputs() const { return inputs_; }

  GType *Out() const { return out_; }

 private:
  vector<Variable *> inputs_vars_;
  Variable *out_var_;
  vector<GType *> inputs_;
  GType *out_;
};
#endif

L
liuruilong 已提交
689
#ifdef LRN_OP
N
nhzlx 已提交
690
template <typename Dtype>
E
eclipsess 已提交
691
class LrnParam : public OpParam {
N
nhzlx 已提交
692 693 694
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

朔-望's avatar
朔-望 已提交
695
 public:
696
  LrnParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
697
           const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
698 699 700
    input_x_ = InputXFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
    mid_out_ = MidOutFrom<GType>(outputs, scope);
701 702 703 704
    n_ = GetAttr<int>("n", attrs);
    alpha_ = GetAttr<float>("alpha", attrs);
    beta_ = GetAttr<float>("beta", attrs);
    k_ = GetAttr<float>("k", attrs);
705
    data_format_ = GetStringAttr("data_format", attrs);
706
  }
E
eclipsess 已提交
707

N
nhzlx 已提交
708
  const RType *InputX() const { return input_x_; }
E
eclipsess 已提交
709

N
nhzlx 已提交
710
  RType *Out() const { return out_; }
E
eclipsess 已提交
711

N
nhzlx 已提交
712
  RType *MidOut() const { return mid_out_; }
E
eclipsess 已提交
713

714
  const int &N() const { return n_; }
E
eclipsess 已提交
715

716
  const float &Alpha() const { return alpha_; }
E
eclipsess 已提交
717

718
  const float &Beta() const { return beta_; }
E
eclipsess 已提交
719

720
  const float &K() const { return k_; }
E
eclipsess 已提交
721

W
wangliu 已提交
722
  const string &DataFormat() const { return data_format_; }
E
eclipsess 已提交
723

朔-望's avatar
朔-望 已提交
724
 private:
N
nhzlx 已提交
725 726 727
  RType *input_x_;
  RType *out_;
  RType *mid_out_;
728 729 730 731
  int n_;
  float alpha_;
  float beta_;
  float k_;
W
wangliu 已提交
732
  string data_format_;
E
eclipsess 已提交
733
};
L
liuruilong 已提交
734 735 736
#endif

#ifdef BATCHNORM_OP
N
nhzlx 已提交
737
template <typename Dtype>
E
eclipsess 已提交
738
class BatchNormParam : OpParam {
N
nhzlx 已提交
739 740 741
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

朔-望's avatar
朔-望 已提交
742
 public:
743
  BatchNormParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
744
                 const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
745 746 747 748 749 750
    input_x_ = InputXFrom<GType>(inputs, scope);
    output_y_ = OutputYFrom<GType>(outputs, scope);
    input_bias_ = InputBiasFrom<GType>(inputs, scope);
    input_mean_ = InputMeanFrom<GType>(inputs, scope);
    input_scale_ = InputScaleFrom<GType>(inputs, scope);
    input_variance_ = InputVarianceFrom<GType>(inputs, scope);
751 752
    epsilon_ = GetAttr<float>("epsilon", attrs);
    momentum_ = GetAttr<float>("momentum", attrs);
L
liuruilong 已提交
753
    //    is_test_ = GetAttr<bool>("is_test", attrs);
754
  }
E
eclipsess 已提交
755

N
nhzlx 已提交
756
  const RType *InputX() const { return input_x_; }
E
eclipsess 已提交
757

N
nhzlx 已提交
758
  RType *OutputY() const { return output_y_; }
E
eclipsess 已提交
759

N
nhzlx 已提交
760
  const RType *InputBias() const { return input_bias_; }
E
eclipsess 已提交
761

N
nhzlx 已提交
762
  const RType *InputMean() const { return input_mean_; }
E
eclipsess 已提交
763

N
nhzlx 已提交
764
  const RType *InputScale() const { return input_scale_; }
E
eclipsess 已提交
765

N
nhzlx 已提交
766
  const RType *InputVariance() const { return input_variance_; }
E
eclipsess 已提交
767

768
  const float &Epsilon() const { return epsilon_; }
E
eclipsess 已提交
769

770
  const float &Momentum() const { return momentum_; }
E
eclipsess 已提交
771

772
  const bool &IsTest() const { return is_test_; }
E
eclipsess 已提交
773

W
wangliu 已提交
774
  const string &DataFormat() const { return data_format_; }
E
eclipsess 已提交
775

776 777 778 779 780 781 782 783
  void SetNewScale(RType *new_scale) { new_scale_ = new_scale; }

  void SetNewBias(RType *new_bias) { new_bias_ = new_bias; }

  const RType *NewScale() const { return new_scale_; }

  const RType *NewBias() const { return new_bias_; }

朔-望's avatar
朔-望 已提交
784
 private:
N
nhzlx 已提交
785 786 787 788 789 790
  RType *input_x_;
  RType *output_y_;
  RType *input_bias_;
  RType *input_mean_;
  RType *input_scale_;
  RType *input_variance_;
791 792 793
  float epsilon_;
  float momentum_;
  bool is_test_;
W
wangliu 已提交
794
  string data_format_;
795 796
  RType *new_bias_;
  RType *new_scale_;
E
eclipsess 已提交
797
};
L
liuruilong 已提交
798 799 800
#endif

#ifdef POOL_OP
N
nhzlx 已提交
801
template <typename Dtype>
802
class PoolParam : public OpParam {
N
nhzlx 已提交
803 804 805
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

朔-望's avatar
朔-望 已提交
806
 public:
807
  PoolParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
808
            const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
809
    input_ = InputXFrom<GType>(inputs, scope);
810

N
nhzlx 已提交
811
    output_ = OutFrom<GType>(outputs, scope);
812
    pooling_type_ = GetStringAttr("pooling_type", attrs);
W
wangliu 已提交
813 814 815
    ksize_ = GetAttr<vector<int>>("ksize", attrs);
    strides_ = GetAttr<vector<int>>("strides", attrs);
    paddings_ = GetAttr<vector<int>>("paddings", attrs);
816
    ceil_mode_ = GetAttr<bool>("ceil_mode", attrs);
817
    global_pooling_ = GetAttr<bool>("global_pooling", attrs);
818
  }
819

N
nhzlx 已提交
820
  const RType *Input() const { return input_; }
821

N
nhzlx 已提交
822
  RType *Output() const { return output_; }
823

W
wangliu 已提交
824
  const string &PoolingType() const { return pooling_type_; }
825

W
wangliu 已提交
826
  const vector<int> &Ksize() const { return ksize_; }
827

W
wangliu 已提交
828
  const vector<int> &Strides() const { return strides_; }
829

W
wangliu 已提交
830
  const vector<int> &Paddings() const { return paddings_; }
831

832
  bool isCeilMode() const { return ceil_mode_; }
833

Z
zhangyang 已提交
834
  bool isGlobalPooling() const { return global_pooling_; }
835

朔-望's avatar
朔-望 已提交
836
 private:
N
nhzlx 已提交
837 838
  RType *input_;
  RType *output_;
W
wangliu 已提交
839 840 841 842
  string pooling_type_;
  vector<int> ksize_;
  vector<int> strides_;
  vector<int> paddings_;
843
  bool ceil_mode_;
844
  bool global_pooling_ = false;
Z
zhangyang 已提交
845
#ifdef PADDLE_MOBILE_FPGA
846 847

 private:
H
hanbuhe 已提交
848
  fpga::PoolingArgs fpga_pool_args;
Z
zhangyang 已提交
849 850

 public:
H
hanbuhe 已提交
851 852
  const fpga::PoolingArgs &FpgaArgs() const { return fpga_pool_args; }
  void SetFpgaArgs(const fpga::PoolingArgs &args) { fpga_pool_args = args; }
Z
zhangyang 已提交
853
#endif
854
};
L
liuruilong 已提交
855 856 857
#endif

#ifdef PRIORBOX_OP
N
nhzlx 已提交
858
template <typename Dtype>
E
eclipsess 已提交
859
class PriorBoxParam : public OpParam {
N
nhzlx 已提交
860 861 862
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

E
eclipsess 已提交
863 864
 public:
  PriorBoxParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
865
                const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
866 867 868 869
    input_ = InputFrom<GType>(inputs, scope);
    input_image_ = InputImageFrom<GType>(inputs, scope);
    output_boxes_ = OutputBoxesFrom<GType>(outputs, scope);
    output_variances_ = OutputVariancesFrom<GType>(outputs, scope);
W
wangliu 已提交
870 871 872 873
    min_sizes_ = GetAttr<vector<float>>("min_sizes", attrs);
    max_sizes_ = GetAttr<vector<float>>("max_sizes", attrs);
    aspect_ratios_ = GetAttr<vector<float>>("aspect_ratios", attrs);
    variances_ = GetAttr<vector<float>>("variances", attrs);
874 875 876 877

    if (HasAttr("min_max_aspect_ratios_order", attrs)) {
      min_max_aspect_ratios_order_ =
          GetAttr<bool>("min_max_aspect_ratios_order", attrs);
Y
yangfei 已提交
878 879
    } else {
      min_max_aspect_ratios_order_ = false;
880
    }
E
eclipsess 已提交
881 882 883 884 885 886
    flip_ = GetAttr<bool>("flip", attrs);
    clip_ = GetAttr<bool>("clip", attrs);
    step_w_ = GetAttr<float>("step_w", attrs);
    step_h_ = GetAttr<float>("step_h", attrs);
    offset_ = GetAttr<float>("offset", attrs);
  }
N
nhzlx 已提交
887
  const RType *Input() const { return input_; }
E
eclipsess 已提交
888

N
nhzlx 已提交
889
  const RType *InputImage() const { return input_image_; }
E
eclipsess 已提交
890

N
nhzlx 已提交
891
  RType *OutputBoxes() const { return output_boxes_; }
E
eclipsess 已提交
892

N
nhzlx 已提交
893
  RType *OutputVariances() const { return output_variances_; }
E
eclipsess 已提交
894

W
wangliu 已提交
895
  const vector<float> &MinSizes() const { return min_sizes_; }
E
eclipsess 已提交
896

W
wangliu 已提交
897
  const vector<float> &MaxSizes() const { return max_sizes_; }
E
eclipsess 已提交
898

W
wangliu 已提交
899
  const vector<float> &AspectRatios() const { return aspect_ratios_; }
E
eclipsess 已提交
900

W
wangliu 已提交
901
  const vector<float> &Variances() const { return variances_; }
E
eclipsess 已提交
902 903 904 905 906 907 908 909 910 911 912

  const bool &Flip() const { return flip_; }

  const bool &Clip() const { return clip_; }

  const float &StepW() const { return step_w_; }

  const float &StepH() const { return step_h_; }

  const float &Offset() const { return offset_; }

913 914 915 916
  const bool &MinMaxAspectRatiosOrder() const {
    return min_max_aspect_ratios_order_;
  }

E
eclipsess 已提交
917
 private:
N
nhzlx 已提交
918 919 920 921
  RType *input_;
  RType *input_image_;
  RType *output_boxes_;
  RType *output_variances_;
W
wangliu 已提交
922 923 924 925
  vector<float> min_sizes_;
  vector<float> max_sizes_;
  vector<float> aspect_ratios_;
  vector<float> variances_;
E
eclipsess 已提交
926 927 928 929 930
  bool flip_;
  bool clip_;
  float step_w_;
  float step_h_;
  float offset_;
931
  bool min_max_aspect_ratios_order_;
E
eclipsess 已提交
932
};
L
liuruilong 已提交
933
#endif
E
eclipsess 已提交
934

L
liuruilong 已提交
935
#ifdef BOXCODER_OP
N
nhzlx 已提交
936
template <typename Dtype>
E
eclipsess 已提交
937
class BoxCoderParam : public OpParam {
N
nhzlx 已提交
938 939 940
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

E
eclipsess 已提交
941 942
 public:
  BoxCoderParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
943
                const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
944 945 946 947
    input_priorbox_ = InputPriorBoxFrom<GType>(inputs, scope);
    input_priorboxvar_ = InputPriorBoxVarFrom<GType>(inputs, scope);
    input_targetbox_ = InputTargetBoxFrom<GType>(inputs, scope);
    output_box_ = OutputBoxFrom<GType>(outputs, scope);
948
    code_type_ = GetStringAttr("code_type", attrs);
E
eclipsess 已提交
949
  }
N
nhzlx 已提交
950
  const RType *InputPriorBox() const { return input_priorbox_; }
E
eclipsess 已提交
951

N
nhzlx 已提交
952
  const RType *InputPriorBoxVar() const { return input_priorboxvar_; }
E
eclipsess 已提交
953

N
nhzlx 已提交
954
  const RType *InputTargetBox() const { return input_targetbox_; }
E
eclipsess 已提交
955

N
nhzlx 已提交
956
  RType *OutputBox() const { return output_box_; }
E
eclipsess 已提交
957 958 959 960

  const std::string &CodeType() const { return code_type_; }

 private:
N
nhzlx 已提交
961 962 963 964
  RType *input_priorbox_;
  RType *input_priorboxvar_;
  RType *input_targetbox_;
  RType *output_box_;
E
eclipsess 已提交
965 966
  std::string code_type_;
};
L
liuruilong 已提交
967
#endif
W
wangliu 已提交
968

L
liuruilong 已提交
969
#ifdef SOFTMAX_OP
N
nhzlx 已提交
970
template <typename Dtype>
W
wangliu 已提交
971
class SoftmaxParam : public OpParam {
N
nhzlx 已提交
972 973 974
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

W
wangliu 已提交
975 976
 public:
  SoftmaxParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
977
               const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
978 979
    input_x_ = InputXFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
W
wangliu 已提交
980
  }
H
hjchen2 已提交
981 982
  const GType *InputX() const { return input_x_; }
  GType *Out() const { return out_; }
W
wangliu 已提交
983 984

 private:
H
hjchen2 已提交
985 986
  GType *input_x_;
  GType *out_;
H
hanbuhe 已提交
987 988 989 990

#ifdef PADDLE_MOBILE_FPGA

 private:
N
nhzlx 已提交
991
  std::shared_ptr<RType> float_input_x_;
H
hanbuhe 已提交
992 993 994
  fpga::BypassArgs fpga_bypass_args;

 public:
995
  RType *FloatInput() const {
H
hanbuhe 已提交
996 997 998 999 1000 1001
    return float_input_x_ == nullptr ? input_x_ : float_input_x_.get();
  }
  void SetFloatInput(Tensor *input) { float_input_x_.reset(input); }
  const fpga::BypassArgs &FpgaArgs() const { return fpga_bypass_args; }
  void SetFpgaArgs(const fpga::BypassArgs &args) { fpga_bypass_args = args; }
#endif
W
wangliu 已提交
1002
};
L
liuruilong 已提交
1003
#endif
W
wangliu 已提交
1004

L
liuruilong 已提交
1005
#ifdef SIGMOID_OP
N
nhzlx 已提交
1006
template <typename Dtype>
W
wangliu 已提交
1007
class SigmoidParam : public OpParam {
N
nhzlx 已提交
1008 1009 1010
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

W
wangliu 已提交
1011 1012
 public:
  SigmoidParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
1013
               const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
1014 1015
    input_x_ = InputXFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
W
wangliu 已提交
1016
  }
N
nhzlx 已提交
1017 1018
  const RType *InputX() const { return input_x_; }
  RType *Out() const { return out_; }
W
wangliu 已提交
1019 1020

 private:
N
nhzlx 已提交
1021 1022
  RType *input_x_;
  RType *out_;
W
wangliu 已提交
1023
};
L
liuruilong 已提交
1024 1025 1026
#endif

#ifdef MULTICLASSNMS_OP
N
nhzlx 已提交
1027
template <typename Dtype>
E
eclipsess 已提交
1028
class MultiClassNMSParam : public OpParam {
N
nhzlx 已提交
1029 1030 1031
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

E
eclipsess 已提交
1032 1033 1034 1035
 public:
  MultiClassNMSParam(const VariableNameMap &inputs,
                     const VariableNameMap &outputs, const AttributeMap &attrs,
                     const Scope &scope) {
N
nhzlx 已提交
1036 1037 1038
    input_bboxes_ = InputBBoxesFrom<GType>(inputs, scope);
    input_scores_ = InputScoresFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
E
eclipsess 已提交
1039 1040 1041 1042 1043 1044 1045 1046
    background_label_ = GetAttr<int>("background_label", attrs);
    nms_top_k_ = GetAttr<int>("nms_top_k", attrs);
    keep_top_k_ = GetAttr<int>("keep_top_k", attrs);
    nms_threshold_ = GetAttr<float>("nms_threshold", attrs);
    nms_eta_ = GetAttr<float>("nms_eta", attrs);
    score_threshold_ = GetAttr<float>("score_threshold", attrs);
  }

Y
yangfei 已提交
1047
  RType *InputBBoxes() const { return input_bboxes_; }
E
eclipsess 已提交
1048

Y
yangfei 已提交
1049
  RType *InputScores() const { return input_scores_; }
E
eclipsess 已提交
1050

N
nhzlx 已提交
1051
  RType *Out() const { return out_; }
E
eclipsess 已提交
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065

  const int &BackGroundLabel() const { return background_label_; }

  const int &NMSTopK() const { return nms_top_k_; }

  const int &KeepTopK() const { return keep_top_k_; }

  const float &NMSThreshold() const { return nms_threshold_; }

  const float &NMSEta() const { return nms_eta_; }

  const float &ScoreThreshold() const { return score_threshold_; }

 private:
N
nhzlx 已提交
1066 1067 1068
  RType *input_bboxes_;
  RType *input_scores_;
  RType *out_;
E
eclipsess 已提交
1069 1070 1071 1072 1073 1074 1075
  int background_label_;
  int nms_top_k_;
  int keep_top_k_;
  float nms_threshold_;
  float nms_eta_;
  float score_threshold_;
};
L
liuruilong 已提交
1076
#endif
W
wangliu 已提交
1077

L
lijiancheng0614 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
#ifdef POLYGONBOXTRANSFORM_OP
template <typename Dtype>
class PolygonBoxTransformParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  PolygonBoxTransformParam(const VariableNameMap &inputs,
                           const VariableNameMap &outputs,
                           const AttributeMap &attrs, const Scope &scope) {
    input_ = InputFrom<GType>(inputs, scope);
    output_ = OutputFrom<GType>(outputs, scope);
  }
  const RType *Input() const { return input_; }
  RType *Output() const { return output_; }

 private:
  RType *input_;
  RType *output_;
};
#endif

N
nhzlx 已提交
1100
template <typename Dtype>
L
liuruilong 已提交
1101
class FeedParam : public OpParam {
N
nhzlx 已提交
1102 1103 1104
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

L
liuruilong 已提交
1105 1106
 public:
  FeedParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
Y
yangfei 已提交
1107 1108 1109 1110
            const AttributeMap &attrs, const Scope &scope) {
    input_x_ = InputXFrom<LoDTensor>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
    auto var = scope.FindVar("batch_size");
W
wangliu 已提交
1111
    batch_size = var->GetValue<int>();
L
liuruilong 已提交
1112
  }
Y
yangfei 已提交
1113
  const LoDTensor *InputX() const { return input_x_; }
xiebaiyuan's avatar
xiebaiyuan 已提交
1114
  GType *Out() const { return out_; }
W
wangliu 已提交
1115
  const int BatchSize() const { return batch_size; }
L
liuruilong 已提交
1116

L
liuruilong 已提交
1117
 private:
Y
yangfei 已提交
1118
  LoDTensor *input_x_;
xiebaiyuan's avatar
xiebaiyuan 已提交
1119
  GType *out_;
W
wangliu 已提交
1120
  int batch_size;
L
liuruilong 已提交
1121 1122
};

N
nhzlx 已提交
1123
template <typename Dtype>
L
liuruilong 已提交
1124
class FetchParam : public OpParam {
N
nhzlx 已提交
1125 1126 1127
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

L
liuruilong 已提交
1128 1129
 public:
  FetchParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
1130
             const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
1131
    input_x_ = InputXFrom<GType>(inputs, scope);
1132
    out_ = OutFrom(outputs, scope);
L
liuruilong 已提交
1133
  }
L
liuruilong 已提交
1134

N
nhzlx 已提交
1135
  const RType *InputX() const { return input_x_; }
1136 1137 1138
  Tensor *Out() const { return out_; }

  static Tensor *OutFrom(const VariableNameMap &outputs, const Scope &scope) {
Z
zhaojiaying01 已提交
1139
    return GetVarValue<LoDTensor>("Out", outputs, scope);
1140
  }
L
liuruilong 已提交
1141

L
liuruilong 已提交
1142
 private:
N
nhzlx 已提交
1143
  RType *input_x_;
Y
yangfei 已提交
1144
  Tensor *out_;
L
liuruilong 已提交
1145 1146
};

L
lijiancheng0614 已提交
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
#ifdef FILL_CONSTANT_OP
template <typename Dtype>
class FillConstantParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  FillConstantParam(const VariableNameMap &inputs,
                    const VariableNameMap &outputs, const AttributeMap &attrs,
                    const Scope &scope) {
    out_var_ = OutVarFrom(outputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
    dtype_ = GetAttr<int>("dtype", attrs);
    shape_ = GetAttr<vector<int>>("shape", attrs);
    value_ = GetAttr<float>("value", attrs);
  }

  Variable *OutVar() const { return out_var_; }

  RType *Out() const { return out_; }

  const int &DataDtype() const { return dtype_; }

  const vector<int> &Shape() const { return shape_; }

  const float &Value() const { return value_; }

 private:
  Variable *out_var_;
  RType *out_;
  int dtype_;
  vector<int> shape_;
  float value_;
};
#endif

L
liuruilong 已提交
1183
#ifdef TRANSPOSE_OP
N
nhzlx 已提交
1184
template <typename Dtype>
E
eclipsess 已提交
1185
class TransposeParam : public OpParam {
N
nhzlx 已提交
1186 1187 1188
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

E
eclipsess 已提交
1189 1190 1191
 public:
  TransposeParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
                 const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
1192 1193
    input_x_ = InputXFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
E
eclipsess 已提交
1194 1195 1196
    axis_ = GetAttr<vector<int>>("axis", attrs);
  }

N
nhzlx 已提交
1197
  const RType *InputX() const { return input_x_; }
E
eclipsess 已提交
1198

N
nhzlx 已提交
1199
  RType *Out() const { return out_; }
E
eclipsess 已提交
1200 1201 1202 1203

  const vector<int> &Axis() const { return axis_; }

 private:
N
nhzlx 已提交
1204 1205
  RType *input_x_;
  RType *out_;
E
eclipsess 已提交
1206 1207
  vector<int> axis_;
};
L
liuruilong 已提交
1208
#endif
E
eclipsess 已提交
1209

L
lijiancheng0614 已提交
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
#ifdef TRANSPOSE2_OP
template <typename Dtype>
class Transpose2Param : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  Transpose2Param(const VariableNameMap &inputs, const VariableNameMap &outputs,
                  const AttributeMap &attrs, const Scope &scope) {
    input_x_ = InputXFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
    output_xshape_ = OutputXShapeFrom<GType>(outputs, scope);
    axis_ = GetAttr<vector<int>>("axis", attrs);
  }

  const RType *InputX() const { return input_x_; }

  RType *Out() const { return out_; }

  RType *OutputXShape() const { return output_xshape_; }

  const vector<int> &Axis() const { return axis_; }

 private:
  RType *input_x_;
  RType *out_;
  RType *output_xshape_;
  vector<int> axis_;
};
#endif

xiebaiyuan's avatar
xiebaiyuan 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
#ifdef LOOKUP_OP
template <typename Dtype>
class LookupParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  LookupParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
              const AttributeMap &attrs, const Scope &scope) {
    input_w_ = InputWFrom<GType>(inputs, scope);
    input_ids_ = InputIdsFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
    padding_idx_ = GetAttr<int64_t>("padding_idx", attrs);
  }

  const GType *InputW() const { return input_w_; }
  const GType *InputIds() const { return input_ids_; }
  GType *Out() const { return out_; }
  int64_t PaddingIdx() const { return padding_idx_; }

 private:
  GType *input_w_;
  GType *input_ids_;
  GType *out_;
  int64_t padding_idx_;
};
#endif

#ifdef CRF_OP
template <typename Dtype>
class CrfParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  //    {G_OP_TYPE_CRF, {{"Emission", "Transition", "Label"}, {"ViterbiPath"}}},

  CrfParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
           const AttributeMap &attrs, const Scope &scope) {
    // todo crf params
    input_emission_ = InputEmissionFrom<GType>(inputs, scope);
    input_transition_ = InputTransitionFrom<GType>(inputs, scope);
    input_label_ = InputLabelFrom<GType>(inputs, scope);
    output_viterbipath_ = OutputViterbiPathFrom<GType>(outputs, scope);
    //    padding_idx_ = GetAttr<int64_t>("padding_idx", attrs);
  }
  const GType *InputEmission() const { return input_emission_; }
  const GType *InputTransition() const { return input_transition_; }
  const GType *InputLabel() const { return input_label_; }
  GType *outputVBP() const { return output_viterbipath_; }
  //  const RType *InputIds() const { return input_ids_; }
  //  RType *Out() const { return out_; }
  //  int64_t PaddingIdx() const { return padding_idx_; }

 private:
  GType *input_emission_;
  GType *input_transition_;
  GType *input_label_;
  GType *output_viterbipath_;

  //  RType *input_ids_;
  //  RType *out_;
  //  int64_t padding_idx_;
};
#endif

L
liuruilong 已提交
1307
#ifdef RESHAPE_OP
N
nhzlx 已提交
1308
template <typename Dtype>
E
eclipsess 已提交
1309
class ReshapeParam : public OpParam {
N
nhzlx 已提交
1310 1311 1312
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

E
eclipsess 已提交
1313 1314 1315
 public:
  ReshapeParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
               const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
1316 1317 1318
    input_x_ = InputXFrom<GType>(inputs, scope);
    input_shape_ = InputShapeFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
E
eclipsess 已提交
1319
    shape_ = GetAttr<vector<int>>("shape", attrs);
1320 1321 1322 1323 1324 1325 1326

    if (HasAttr("inplace", attrs)) {
      inplace_ = GetAttr<bool>("inplace", attrs);
    } else {
      inplace_ = false;
      DLOG << "ReshapeParam lost inplace params. maybe fluid updated";
    }
E
eclipsess 已提交
1327 1328
  }

N
nhzlx 已提交
1329
  const RType *InputX() const { return input_x_; }
E
eclipsess 已提交
1330

N
nhzlx 已提交
1331
  const RType *InputShape() const { return input_shape_; }
E
eclipsess 已提交
1332

N
nhzlx 已提交
1333
  RType *Out() const { return out_; }
E
eclipsess 已提交
1334 1335 1336 1337 1338 1339

  const vector<int> &Shape() const { return shape_; }

  const bool &Inplace() const { return inplace_; }

 private:
N
nhzlx 已提交
1340 1341 1342
  RType *input_x_;
  RType *input_shape_;
  RType *out_;
E
eclipsess 已提交
1343 1344 1345
  vector<int> shape_;
  bool inplace_;
};
L
liuruilong 已提交
1346
#endif
E
eclipsess 已提交
1347

L
lijiancheng0614 已提交
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
#ifdef RESHAPE2_OP
template <typename Dtype>
class Reshape2Param : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  Reshape2Param(const VariableNameMap &inputs, const VariableNameMap &outputs,
                const AttributeMap &attrs, const Scope &scope) {
    input_x_ = InputXFrom<GType>(inputs, scope);
    input_shape_ = InputShapeFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
    output_xshape_ = OutputXShapeFrom<GType>(outputs, scope);
    shape_ = GetAttr<vector<int>>("shape", attrs);
    if (HasAttr("inplace", attrs)) {
      inplace_ = GetAttr<bool>("inplace", attrs);
    } else {
      inplace_ = false;
    }
  }

E
eclipsess 已提交
1369
  const GType *InputX() const { return input_x_; }
L
lijiancheng0614 已提交
1370

E
eclipsess 已提交
1371
  const GType *InputShape() const { return input_shape_; }
L
lijiancheng0614 已提交
1372

E
eclipsess 已提交
1373
  GType *Out() const { return out_; }
L
lijiancheng0614 已提交
1374

E
eclipsess 已提交
1375
  GType *OutputXShape() const { return output_xshape_; }
L
lijiancheng0614 已提交
1376 1377 1378 1379 1380 1381

  const vector<int> &Shape() const { return shape_; }

  const bool &Inplace() const { return inplace_; }

 private:
E
eclipsess 已提交
1382 1383 1384 1385
  GType *input_x_;
  GType *input_shape_;
  GType *out_;
  GType *output_xshape_;
L
lijiancheng0614 已提交
1386 1387 1388 1389 1390
  vector<int> shape_;
  bool inplace_;
};
#endif

T
Tian 已提交
1391
#ifdef SCALE_OP
N
nhzlx 已提交
1392
template <typename Dtype>
I
itminner 已提交
1393
class ScaleParam : public OpParam {
N
nhzlx 已提交
1394 1395 1396
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

I
itminner 已提交
1397 1398 1399
 public:
  ScaleParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
             const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
1400 1401 1402
    input_x_ = InputXFrom<GType>(inputs, scope);
    input_bias_ = InputBiasFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
I
itminner 已提交
1403 1404 1405 1406 1407 1408
    inplace_ = GetAttr<bool>("inplace", attrs);
    has_bias_ = GetAttr<bool>("has_bias", attrs);
    scales_ = GetAttr<vector<float>>("scales", attrs);
    biases_ = GetAttr<vector<float>>("biases", attrs);
  }

N
nhzlx 已提交
1409
  const RType *InputX() const { return input_x_; }
I
itminner 已提交
1410

N
nhzlx 已提交
1411
  const RType *InputBias() const { return input_bias_; }
I
itminner 已提交
1412

N
nhzlx 已提交
1413
  RType *Out() const { return out_; }
I
itminner 已提交
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423

  const bool &Inplace() const { return inplace_; }

  const bool &HasBias() const { return has_bias_; }

  const vector<float> &Scales() const { return scales_; }

  const vector<float> &Biases() const { return biases_; }

 private:
N
nhzlx 已提交
1424 1425 1426
  RType *input_x_;
  RType *input_bias_;
  RType *out_;
I
itminner 已提交
1427 1428 1429 1430 1431
  bool inplace_;
  bool has_bias_;
  vector<float> scales_;
  vector<float> biases_;
};
T
Tian 已提交
1432 1433 1434
#endif

#ifdef SLICE_OP
N
nhzlx 已提交
1435
template <typename Dtype>
I
itminner 已提交
1436
class SliceParam : public OpParam {
N
nhzlx 已提交
1437 1438 1439
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

I
itminner 已提交
1440 1441 1442
 public:
  SliceParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
             const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
1443 1444 1445
    input_x_ = InputXFrom<GType>(inputs, scope);
    input_shape_ = InputShapeFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
I
itminner 已提交
1446 1447 1448 1449 1450
    axis_ = GetAttr<int>("axis", attrs);
    slice_points_ = GetAttr<vector<int>>("slice_points", attrs);
    inplace_ = GetAttr<bool>("inplace", attrs);
  }

N
nhzlx 已提交
1451
  const RType *InputX() const { return input_x_; }
I
itminner 已提交
1452

N
nhzlx 已提交
1453
  const RType *InputShape() const { return input_shape_; }
I
itminner 已提交
1454

N
nhzlx 已提交
1455
  RType *Out() const { return out_; }
I
itminner 已提交
1456 1457 1458 1459 1460 1461 1462 1463

  const int &Axis() const { return axis_; }

  const vector<int> &SlicePoints() const { return slice_points_; }

  const bool &Inplace() const { return inplace_; }

 private:
N
nhzlx 已提交
1464 1465 1466
  RType *input_x_;
  RType *input_shape_;
  RType *out_;
I
itminner 已提交
1467 1468 1469 1470
  int axis_;
  vector<int> slice_points_;
  bool inplace_;
};
T
Tian 已提交
1471 1472 1473
#endif

#ifdef RESIZE_OP
N
nhzlx 已提交
1474
template <typename Dtype>
T
Tian 已提交
1475
class ResizeParam : public OpParam {
N
nhzlx 已提交
1476 1477 1478
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

I
itminner 已提交
1479 1480 1481
 public:
  ResizeParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
              const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
1482 1483 1484
    input_x_ = InputXFrom<GType>(inputs, scope);
    input_shape_ = InputShapeFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
I
itminner 已提交
1485 1486 1487 1488 1489 1490
    is_pyramid_test_ = GetAttr<bool>("is_pyramid_test", attrs);
    height_ = GetAttr<int>("height", attrs);
    width_ = GetAttr<int>("width", attrs);
    out_height_scale_ = GetAttr<float>("out_height_scale", attrs);
    out_width_scale_ = GetAttr<float>("out_width_scale", attrs);
  }
T
Tian 已提交
1491

N
nhzlx 已提交
1492
  const RType *InputX() const { return input_x_; }
T
Tian 已提交
1493

N
nhzlx 已提交
1494
  const RType *InputShape() const { return input_shape_; }
T
Tian 已提交
1495

N
nhzlx 已提交
1496
  RType *Out() const { return out_; }
T
Tian 已提交
1497

I
itminner 已提交
1498
  const bool &IsPyramidTest() const { return is_pyramid_test_; }
T
Tian 已提交
1499

I
itminner 已提交
1500
  const int &Height() const { return height_; }
T
Tian 已提交
1501

I
itminner 已提交
1502
  const int &Width() const { return width_; }
T
Tian 已提交
1503

I
itminner 已提交
1504
  const float &OutHeightScale() const { return out_height_scale_; }
T
Tian 已提交
1505

I
itminner 已提交
1506
  const float &OutWidthScale() const { return out_width_scale_; }
T
Tian 已提交
1507

I
itminner 已提交
1508
 private:
N
nhzlx 已提交
1509 1510 1511
  RType *input_x_;
  RType *input_shape_;
  RType *out_;
I
itminner 已提交
1512 1513 1514 1515 1516
  bool is_pyramid_test_;
  int height_;
  int width_;
  float out_height_scale_;
  float out_width_scale_;
T
Tian 已提交
1517 1518 1519
};
#endif

L
liuruilong 已提交
1520
#ifdef RELU_OP
L
liuruilong 已提交
1521 1522 1523
/*
 * @b op 层实例化好这个 param 传递给 kernel 层使用
 * */
N
nhzlx 已提交
1524
template <typename Dtype>
D
relu  
dolphin8 已提交
1525
class ReluParamBase : public OpParam {
N
nhzlx 已提交
1526 1527 1528
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

E
eclipsess 已提交
1529
 public:
D
relu  
dolphin8 已提交
1530
  ReluParamBase(const VariableNameMap &inputs, const VariableNameMap &outputs,
Y
yangfei 已提交
1531
                const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
1532 1533
    input_x_ = InputXFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
E
eclipsess 已提交
1534 1535
  }

N
nhzlx 已提交
1536
  const RType *InputX() const { return input_x_; }
E
eclipsess 已提交
1537

N
nhzlx 已提交
1538
  RType *Out() const { return out_; }
E
eclipsess 已提交
1539 1540

 private:
N
nhzlx 已提交
1541 1542
  RType *input_x_;
  RType *out_;
E
eclipsess 已提交
1543
};
D
relu  
dolphin8 已提交
1544 1545 1546

template <typename Dtype>
class ReluParam : public ReluParamBase<Dtype> {
Y
yangfei 已提交
1547
 public:
D
relu  
dolphin8 已提交
1548 1549 1550
  using ReluParamBase<Dtype>::ReluParamBase;
};

Y
yangfei 已提交
1551
#ifdef PADDLE_MOBILE_CL
D
relu  
dolphin8 已提交
1552 1553
template <>
class ReluParam<GPU_CL> : public ReluParamBase<GPU_CL> {
Y
yangfei 已提交
1554
 public:
D
relu  
dolphin8 已提交
1555
  using ReluParamBase<GPU_CL>::ReluParamBase;
Y
yangfei 已提交
1556 1557 1558
  framework::CLImage &getMidImage() { return midImage; }

 private:
D
relu  
dolphin8 已提交
1559 1560
  framework::CLImage midImage;
};
Y
yangfei 已提交
1561
#endif
D
relu  
dolphin8 已提交
1562

L
liuruilong 已提交
1563
#endif
E
eclipsess 已提交
1564

Z
zhangyang 已提交
1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
#ifdef TANH_OP
template <typename Dtype>
class TanhParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  TanhParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
            const AttributeMap &attrs, const Scope &scope) {
    input_x_ = InputXFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
  }
  const RType *InputX() const { return input_x_; }
  RType *Out() const { return out_; }

 private:
  RType *input_x_;
  RType *out_;
qnqinan's avatar
qnqinan 已提交
1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
#ifdef PADDLE_MOBILE_FPGA

 private:
  std::shared_ptr<RType> float_input_x_;
  fpga::BypassArgs fpga_bypass_args;

 public:
  RType *FloatInput() const {
    return float_input_x_ == nullptr ? input_x_ : float_input_x_.get();
  }
  void SetFloatInput(Tensor *input) { float_input_x_.reset(input); }
  const fpga::BypassArgs &FpgaArgs() const { return fpga_bypass_args; }
  void SetFpgaArgs(const fpga::BypassArgs &args) { fpga_bypass_args = args; }
#endif
Z
zhangyang 已提交
1597
};
L
liuruilong 已提交
1598
#endif
E
eclipsess 已提交
1599

T
Tian 已提交
1600
#ifdef PRELU_OP
N
nhzlx 已提交
1601
template <typename Dtype>
T
Tian 已提交
1602
class PReluParam : public OpParam {
N
nhzlx 已提交
1603 1604 1605
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

I
itminner 已提交
1606 1607 1608
 public:
  PReluParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
             const AttributeMap &attrs, const Scope &scope) {
1609
    DLOG << "PReluParam inputs before";
N
nhzlx 已提交
1610
    input_x_ = InputXFrom<GType>(inputs, scope);
N
nhzlx 已提交
1611
    alpha_ = InputAlphaFrom<GType>(inputs, scope);
1612
    framework::DDim dims = alpha_->dims();
N
nhzlx 已提交
1613
    out_ = OutFrom<GType>(outputs, scope);
1614
    mode_ = GetStringAttr("mode", attrs);
1615
    DLOG << "PReluParam mode after" << mode_;
I
itminner 已提交
1616
  }
N
nhzlx 已提交
1617
  const RType *InputX() const { return input_x_; }
N
nhzlx 已提交
1618
  const RType *InputAlpha() const { return alpha_; }
N
nhzlx 已提交
1619
  RType *Out() const { return out_; }
1620
  const std::string &Mode() const { return mode_; }
T
Tian 已提交
1621

I
itminner 已提交
1622
 private:
N
nhzlx 已提交
1623 1624
  RType *input_x_;
  RType *out_;
N
nhzlx 已提交
1625
  RType *alpha_;
1626
  std::string mode_;
T
Tian 已提交
1627 1628 1629
};
#endif

N
nhzlx 已提交
1630
template <typename Dtype>
L
liuruilong 已提交
1631
class FusionFcParam : public OpParam {
N
nhzlx 已提交
1632 1633 1634
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

E
eclipsess 已提交
1635
 public:
L
liuruilong 已提交
1636
  FusionFcParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
L
liuruilong 已提交
1637
                const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
1638 1639 1640 1641
    input_x_ = InputXFrom<GType>(inputs, scope);
    input_y_ = InputYFrom<GType>(inputs, scope);
    input_z_ = InputZFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
E
eclipsess 已提交
1642 1643 1644 1645
    x_num_col_dims_ = GetAttr<int>("x_num_col_dims", attrs);
    y_num_col_dims_ = GetAttr<int>("y_num_col_dims", attrs);
    axis_ = GetAttr<int>("axis", attrs);
  }
Y
yangfei 已提交
1646
  GType *InputX() const { return input_x_; }
E
eclipsess 已提交
1647

Y
yangfei 已提交
1648
  RType *InputY() const { return input_y_; }
E
eclipsess 已提交
1649

Y
yangfei 已提交
1650
  RType *InputZ() const { return input_z_; }
E
eclipsess 已提交
1651

xiebaiyuan's avatar
xiebaiyuan 已提交
1652
  GType *Out() const { return out_; }
E
eclipsess 已提交
1653 1654 1655 1656 1657 1658 1659 1660

  const int &XNumColDims() const { return x_num_col_dims_; }

  const int &YNumColDims() const { return y_num_col_dims_; }

  const int &Axis() const { return axis_; }

 private:
xiebaiyuan's avatar
xiebaiyuan 已提交
1661
  GType *input_x_;
N
nhzlx 已提交
1662 1663
  RType *input_y_;
  RType *input_z_;
xiebaiyuan's avatar
xiebaiyuan 已提交
1664
  GType *out_;
E
eclipsess 已提交
1665 1666 1667
  int x_num_col_dims_;
  int y_num_col_dims_;
  int axis_;
Z
zhangyang 已提交
1668

Z
ZhenWang 已提交
1669
#ifdef PADDLE_MOBILE_FPGA
1670
 private:  // NOLINT
Z
zhangyang 已提交
1671
  fpga::SplitConvArgs fpga_conv_args;
Z
zhangyang 已提交
1672 1673

 public:
Z
zhangyang 已提交
1674 1675
  const fpga::SplitConvArgs &FpgaArgs() const { return fpga_conv_args; }
  void SetFpgaArgs(const fpga::SplitConvArgs &args) { fpga_conv_args = args; }
Z
zhangyang 已提交
1676
#endif
E
eclipsess 已提交
1677
};
1678 1679

#ifdef FUSION_FCRELU_OP
N
nhzlx 已提交
1680 1681
template <typename DeviceType>
using FusionFcReluParam = FusionFcParam<DeviceType>;
L
liuruilong 已提交
1682
#endif
E
eclipsess 已提交
1683

N
nhzlx 已提交
1684
template <typename Dtype>
1685
class FusionConvAddParam : public ConvParam<Dtype> {
N
nhzlx 已提交
1686 1687 1688
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

W
wangliu 已提交
1689
 public:
L
liuruilong 已提交
1690
  FusionConvAddParam(const VariableNameMap &inputs,
L
liuruilong 已提交
1691
                     const VariableNameMap &outputs, const AttributeMap &attrs,
1692 1693 1694 1695 1696
                     const Scope &scope)
      : ConvParam<Dtype>(inputs, outputs, attrs, scope) {
    bias_ = OpParam::InputYFrom<GType>(inputs, scope);
    axis_ = OpParam::GetAttr<int>("axis", attrs);
    output_ = OpParam::OutFrom<GType>(outputs, scope);
W
wangliu 已提交
1697
  }
N
nhzlx 已提交
1698
  RType *Bias() const { return bias_; }
W
wangliu 已提交
1699 1700 1701

  const int &Axis() const { return axis_; }

N
nhzlx 已提交
1702
  RType *Output() const { return output_; }
W
wangliu 已提交
1703

L
liuruilong 已提交
1704
 protected:
N
nhzlx 已提交
1705
  RType *bias_;
W
wangliu 已提交
1706
  int axis_;
N
nhzlx 已提交
1707
  RType *output_;
W
wangliu 已提交
1708 1709
};

N
nhzlx 已提交
1710 1711
template <typename Dtype>
Print &operator<<(Print &printer, const FusionConvAddParam<Dtype> &conv_param);
W
wangliu 已提交
1712

Z
zhangyang 已提交
1713
#ifdef FUSION_CONVADDRELU_OP
N
nhzlx 已提交
1714 1715
template <typename DeviceType>
class FusionConvAddReluParam : public FusionConvAddParam<DeviceType> {
L
liuruilong 已提交
1716
 public:
L
liuruilong 已提交
1717
  FusionConvAddReluParam(const VariableNameMap &inputs,
L
liuruilong 已提交
1718 1719
                         const VariableNameMap &outputs,
                         const AttributeMap &attrs, const Scope &scope)
1720
      : FusionConvAddParam<DeviceType>(inputs, outputs, attrs, scope) {}
L
liuruilong 已提交
1721 1722 1723
};
#endif

1724
#ifdef FUSION_CONVADDPRELU_OP
1725 1726 1727 1728
template <typename Dtype>
class FusionConvAddPReluParam : public ConvParam<Dtype> {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;
1729 1730 1731 1732

 public:
  FusionConvAddPReluParam(const VariableNameMap &inputs,
                          const VariableNameMap &outputs,
1733 1734 1735
                          const AttributeMap &attrs, const Scope &scope)
      : ConvParam<Dtype>(inputs, outputs, attrs, scope) {
    alpha_ = OpParam::InputAlphaFrom<GType>(inputs, scope);
1736
    mode_ = OpParam::GetStringAttr("mode", attrs);
1737
    framework::DDim dims = alpha_->dims();
1738 1739 1740
    bias_ = OpParam::InputYFrom<GType>(inputs, scope);
    axis_ = OpParam::GetAttr<int>("axis", attrs);
    output_ = OpParam::OutFrom<GType>(outputs, scope);
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757
  }
  const RType *InputAlpha() const { return alpha_; }
  const std::string &Mode() const { return mode_; }
  RType *Bias() const { return bias_; }
  const int &Axis() const { return axis_; }
  RType *Output() const { return output_; }

 protected:
  RType *bias_;
  int axis_;
  RType *output_;
  RType *alpha_;
  std::string mode_;
};
#endif

#ifdef FUSION_CONVADDADDPRELU_OP
1758 1759 1760 1761
template <typename Dtype>
class FusionConvAddAddPReluParam : public ConvParam<Dtype> {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;
1762 1763 1764 1765

 public:
  FusionConvAddAddPReluParam(const VariableNameMap &inputs,
                             const VariableNameMap &outputs,
1766 1767 1768 1769
                             const AttributeMap &attrs, const Scope &scope)
      : ConvParam<Dtype>(inputs, outputs, attrs, scope) {
    bias1_ = OpParam::InputYFrom1<GType>(inputs, scope);
    alpha_ = OpParam::InputAlphaFrom<GType>(inputs, scope);
1770
    mode_ = OpParam::GetStringAttr("mode", attrs);
1771
    framework::DDim dims = alpha_->dims();
1772 1773 1774 1775 1776 1777
    bias_ = OpParam::InputYFrom<GType>(inputs, scope);
    output_ = OpParam::OutFrom<GType>(outputs, scope);
    axis_ = OpParam::GetAttr<int>("axis", attrs);
    keyOutput_ = OpParam::getkey("addOut", inputs, 0);
    keyX1_ = OpParam::getkey("addX", inputs, 1);
    keyY1_ = OpParam::getkey("Y", inputs, 1);
1778
    if (keyX1_ == keyOutput_) {
1779
      bias1_ = OpParam::InputYFrom1<GType>(inputs, scope);
1780
    } else if (keyY1_ == keyOutput_) {
1781
      bias1_ = OpParam::InputXFrom1<GType>(inputs, scope);
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805
    }
  }
  const RType *InputAlpha() const { return alpha_; }
  const std::string &Mode() const { return mode_; }
  const RType *Bias1() const { return bias1_; }

  RType *Bias() const { return bias_; }

  const int &Axis() const { return axis_; }
  RType *Output() const { return output_; }

 protected:
  RType *bias_;
  int axis_;
  RType *output_;
  RType *alpha_;
  std::string mode_;
  RType *bias1_;
  std::string keyOutput_;
  std::string keyX1_;
  std::string keyY1_;
};
#endif

E
eclipsess 已提交
1806
#ifdef FUSION_CONVADDBNRELU_OP
N
nhzlx 已提交
1807
template <typename Dtype>
1808
class FusionConvAddBNReluParam : public ConvParam<Dtype> {
N
nhzlx 已提交
1809 1810 1811
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

E
eclipsess 已提交
1812 1813 1814
 public:
  FusionConvAddBNReluParam(const VariableNameMap &inputs,
                           const VariableNameMap &outputs,
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
                           const AttributeMap &attrs, const Scope &scope)
      : ConvParam<Dtype>(inputs, outputs, attrs, scope) {
    bias_ = OpParam::InputYFrom<GType>(inputs, scope);
    axis_ = OpParam::GetAttr<int>("axis", attrs);
    output_ = OpParam::OutFrom<GType>(outputs, scope);
    input_bias_ = OpParam::InputBiasFrom<GType>(inputs, scope);
    input_mean_ = OpParam::InputMeanFrom<GType>(inputs, scope);
    input_scale_ = OpParam::InputScaleFrom<GType>(inputs, scope);
    input_variance_ = OpParam::InputVarianceFrom<GType>(inputs, scope);
    epsilon_ = OpParam::GetAttr<float>("epsilon", attrs);
    momentum_ = OpParam::GetAttr<float>("momentum", attrs);
    //    is_test_ = OpParam::GetAttr<bool>("is_test", attrs);
E
eclipsess 已提交
1827
  }
N
nhzlx 已提交
1828
  RType *Bias() const { return bias_; }
E
eclipsess 已提交
1829 1830 1831

  const int &Axis() const { return axis_; }

N
nhzlx 已提交
1832
  RType *Output() const { return output_; }
E
eclipsess 已提交
1833

N
nhzlx 已提交
1834
  const RType *InputBias() const { return input_bias_; }
E
eclipsess 已提交
1835

N
nhzlx 已提交
1836
  const RType *InputMean() const { return input_mean_; }
E
eclipsess 已提交
1837

N
nhzlx 已提交
1838
  const RType *InputScale() const { return input_scale_; }
E
eclipsess 已提交
1839

N
nhzlx 已提交
1840
  const RType *InputVariance() const { return input_variance_; }
E
eclipsess 已提交
1841 1842 1843 1844 1845 1846 1847

  const float &Epsilon() const { return epsilon_; }

  const float &Momentum() const { return momentum_; }

  const bool &IsTest() const { return is_test_; }

N
nhzlx 已提交
1848
  void SetNewScale(RType *new_scale) { new_scale_ = new_scale; }
E
eclipsess 已提交
1849

N
nhzlx 已提交
1850
  void SetNewBias(RType *new_bias) { new_bias_ = new_bias; }
E
eclipsess 已提交
1851

N
nhzlx 已提交
1852
  const RType *NewScale() const { return new_scale_; }
E
eclipsess 已提交
1853

N
nhzlx 已提交
1854
  const RType *NewBias() const { return new_bias_; }
E
eclipsess 已提交
1855 1856

 protected:
N
nhzlx 已提交
1857
  RType *bias_;
E
eclipsess 已提交
1858
  int axis_;
N
nhzlx 已提交
1859 1860 1861 1862 1863
  RType *output_;
  RType *input_bias_;
  RType *input_mean_;
  RType *input_scale_;
  RType *input_variance_;
E
eclipsess 已提交
1864 1865 1866
  float epsilon_;
  float momentum_;
  bool is_test_;
N
nhzlx 已提交
1867 1868
  RType *new_bias_;
  RType *new_scale_;
1869 1870 1871 1872 1873
};
#endif

#ifdef FUSION_CONVBNADDRELU_OP
template <typename Dtype>
1874
class FusionConvBNAddReluParam : public ConvParam<Dtype> {
1875 1876 1877 1878 1879 1880
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  FusionConvBNAddReluParam(const VariableNameMap &inputs,
                           const VariableNameMap &outputs,
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
                           const AttributeMap &attrs, const Scope &scope)
      : ConvParam<Dtype>(inputs, outputs, attrs, scope) {
    bias_ = OpParam::InputYFrom<GType>(inputs, scope);
    axis_ = OpParam::GetAttr<int>("axis", attrs);
    output_ = OpParam::OutFrom<GType>(outputs, scope);
    input_bias_ = OpParam::InputBiasFrom<GType>(inputs, scope);
    input_mean_ = OpParam::InputMeanFrom<GType>(inputs, scope);
    input_scale_ = OpParam::InputScaleFrom<GType>(inputs, scope);
    input_variance_ = OpParam::InputVarianceFrom<GType>(inputs, scope);
    epsilon_ = OpParam::GetAttr<float>("epsilon", attrs);
    momentum_ = OpParam::GetAttr<float>("momentum", attrs);
    keyBNY_ = OpParam::getkey("BNY", inputs, 0);
    keyX_ = OpParam::getkey("X", inputs, 0);
    keyY_ = OpParam::getkey("Y", inputs, 0);
1895
    if (keyX_ == keyBNY_) {
1896
      bias_ = OpParam::InputYFrom<GType>(inputs, scope);
1897
    } else if (keyY_ == keyBNY_) {
1898
      bias_ = OpParam::InputXFrom<GType>(inputs, scope);
1899
    }
1900
    //    is_test_ = OpParam::GetAttr<bool>("is_test", attrs);
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
  }
  RType *Bias() const { return bias_; }

  const int &Axis() const { return axis_; }

  RType *Output() const { return output_; }

  const RType *InputBias() const { return input_bias_; }

  const RType *InputMean() const { return input_mean_; }

  const RType *InputScale() const { return input_scale_; }

  const RType *InputVariance() const { return input_variance_; }

  const float &Epsilon() const { return epsilon_; }

  const float &Momentum() const { return momentum_; }

  const bool &IsTest() const { return is_test_; }

  void SetNewScale(RType *new_scale) { new_scale_ = new_scale; }

  void SetNewBias(RType *new_bias) { new_bias_ = new_bias; }

  const RType *NewScale() const { return new_scale_; }

  const RType *NewBias() const { return new_bias_; }

 protected:
  RType *bias_;
  int axis_;
  RType *output_;
  RType *input_bias_;
  RType *input_mean_;
  RType *input_scale_;
  RType *input_variance_;
  float epsilon_;
  float momentum_;
  bool is_test_;
  RType *new_bias_;
  RType *new_scale_;
  std::string keyBNY_;
  std::string keyX_;
  std::string keyY_;
E
eclipsess 已提交
1946
};
1947
#endif
E
eclipsess 已提交
1948

Z
zhangyang 已提交
1949
#ifdef FUSION_CONVBN_OP
N
nhzlx 已提交
1950
template <typename Dtype>
1951
class FusionConvBNParam : public ConvParam<Dtype> {
N
nhzlx 已提交
1952 1953 1954
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

Z
zhangyang 已提交
1955 1956 1957
 public:
  FusionConvBNParam(const VariableNameMap &inputs,
                    const VariableNameMap &outputs, const AttributeMap &attrs,
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
                    const Scope &scope)
      : ConvParam<Dtype>(inputs, outputs, attrs, scope) {
    output_y_ = OpParam::OutputYFrom<GType>(outputs, scope);
    input_bias_ = OpParam::InputBiasFrom<GType>(inputs, scope);
    input_mean_ = OpParam::InputMeanFrom<GType>(inputs, scope);
    input_scale_ = OpParam::InputScaleFrom<GType>(inputs, scope);
    input_variance_ = OpParam::InputVarianceFrom<GType>(inputs, scope);
    epsilon_ = OpParam::GetAttr<float>("epsilon", attrs);
    momentum_ = OpParam::GetAttr<float>("momentum", attrs);
    //    is_test_ = OpParam::GetAttr<bool>("is_test", attrs);
Z
zhangyang 已提交
1968
  }
N
nhzlx 已提交
1969
  RType *Output() const { return output_y_; }
Z
zhangyang 已提交
1970

N
nhzlx 已提交
1971
  const RType *InputBias() const { return input_bias_; }
Z
zhangyang 已提交
1972

N
nhzlx 已提交
1973
  const RType *InputMean() const { return input_mean_; }
Z
zhangyang 已提交
1974

N
nhzlx 已提交
1975
  const RType *InputScale() const { return input_scale_; }
Z
zhangyang 已提交
1976

N
nhzlx 已提交
1977
  const RType *InputVariance() const { return input_variance_; }
Z
zhangyang 已提交
1978 1979 1980 1981 1982 1983 1984

  const float &Epsilon() const { return epsilon_; }

  const float &Momentum() const { return momentum_; }

  const bool &IsTest() const { return is_test_; }

N
nhzlx 已提交
1985
  void SetNewScale(RType *new_scale) { new_scale_ = new_scale; }
Z
zhangyang 已提交
1986

N
nhzlx 已提交
1987
  void SetNewBias(RType *new_bias) { new_bias_ = new_bias; }
Z
zhangyang 已提交
1988

N
nhzlx 已提交
1989
  const RType *NewScale() const { return new_scale_; }
Z
zhangyang 已提交
1990

N
nhzlx 已提交
1991
  const RType *NewBias() const { return new_bias_; }
Z
zhangyang 已提交
1992 1993

 protected:
N
nhzlx 已提交
1994 1995 1996 1997 1998
  RType *output_y_;
  RType *input_bias_;
  RType *input_mean_;
  RType *input_scale_;
  RType *input_variance_;
Z
zhangyang 已提交
1999 2000 2001
  float epsilon_;
  float momentum_;
  bool is_test_;
N
nhzlx 已提交
2002 2003
  RType *new_bias_;
  RType *new_scale_;
Z
zhangyang 已提交
2004 2005 2006
};
#endif

2007
#ifdef FUSION_CONVADDBN_OP
N
nhzlx 已提交
2008
template <typename Dtype>
2009
class FusionConvAddBNParam : public ConvParam<Dtype> {
N
nhzlx 已提交
2010 2011 2012
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

2013 2014 2015
 public:
  FusionConvAddBNParam(const VariableNameMap &inputs,
                       const VariableNameMap &outputs,
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
                       const AttributeMap &attrs, const Scope &scope)
      : ConvParam<Dtype>(inputs, outputs, attrs, scope) {
    bias_ = OpParam::InputYFrom<GType>(inputs, scope);
    axis_ = OpParam::GetAttr<int>("axis", attrs);
    output_y_ = OpParam::OutputYFrom<GType>(outputs, scope);
    input_bias_ = OpParam::InputBiasFrom<GType>(inputs, scope);
    input_mean_ = OpParam::InputMeanFrom<GType>(inputs, scope);
    input_scale_ = OpParam::InputScaleFrom<GType>(inputs, scope);
    input_variance_ = OpParam::InputVarianceFrom<GType>(inputs, scope);
    epsilon_ = OpParam::GetAttr<float>("epsilon", attrs);
    momentum_ = OpParam::GetAttr<float>("momentum", attrs);
    //    is_test_ = OpParam::GetAttr<bool>("is_test", attrs);
2028
  }
N
nhzlx 已提交
2029
  RType *Bias() const { return bias_; }
2030 2031 2032

  const int &Axis() const { return axis_; }

N
nhzlx 已提交
2033
  RType *Output() const { return output_y_; }
2034

N
nhzlx 已提交
2035
  const RType *InputBias() const { return input_bias_; }
2036

N
nhzlx 已提交
2037
  const RType *InputMean() const { return input_mean_; }
2038

N
nhzlx 已提交
2039
  const RType *InputScale() const { return input_scale_; }
2040

N
nhzlx 已提交
2041
  const RType *InputVariance() const { return input_variance_; }
2042 2043 2044 2045 2046 2047 2048

  const float &Epsilon() const { return epsilon_; }

  const float &Momentum() const { return momentum_; }

  const bool &IsTest() const { return is_test_; }

N
nhzlx 已提交
2049
  void SetNewScale(RType *new_scale) { new_scale_ = new_scale; }
2050

N
nhzlx 已提交
2051
  void SetNewBias(RType *new_bias) { new_bias_ = new_bias; }
2052

N
nhzlx 已提交
2053
  const RType *NewScale() const { return new_scale_; }
2054

N
nhzlx 已提交
2055
  const RType *NewBias() const { return new_bias_; }
2056 2057

 protected:
N
nhzlx 已提交
2058
  RType *bias_;
2059
  int axis_;
N
nhzlx 已提交
2060 2061 2062 2063 2064
  RType *output_y_;
  RType *input_bias_;
  RType *input_mean_;
  RType *input_scale_;
  RType *input_variance_;
2065 2066 2067
  float epsilon_;
  float momentum_;
  bool is_test_;
N
nhzlx 已提交
2068 2069
  RType *new_bias_;
  RType *new_scale_;
2070
};
E
eclipsess 已提交
2071
#endif
Y
Yao,kun 已提交
2072

E
eclipsess 已提交
2073
#ifdef FUSION_DWCONVBNRELU_OP
N
nhzlx 已提交
2074
template <typename Dtype>
2075
class FusionDWConvBNReluParam : public ConvParam<Dtype> {
N
nhzlx 已提交
2076 2077 2078
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

E
eclipsess 已提交
2079 2080 2081
 public:
  FusionDWConvBNReluParam(const VariableNameMap &inputs,
                          const VariableNameMap &outputs,
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
                          const AttributeMap &attrs, const Scope &scope)
      : ConvParam<Dtype>(inputs, outputs, attrs, scope) {
    output_ = OpParam::OutFrom<GType>(outputs, scope);
    input_bias_ = OpParam::InputBiasFrom<GType>(inputs, scope);
    input_mean_ = OpParam::InputMeanFrom<GType>(inputs, scope);
    input_scale_ = OpParam::InputScaleFrom<GType>(inputs, scope);
    input_variance_ = OpParam::InputVarianceFrom<GType>(inputs, scope);
    epsilon_ = OpParam::GetAttr<float>("epsilon", attrs);
    momentum_ = OpParam::GetAttr<float>("momentum", attrs);
    //    is_test_ = OpParam::GetAttr<bool>("is_test", attrs);
E
eclipsess 已提交
2092
  }
N
nhzlx 已提交
2093
  RType *Output() const { return output_; }
E
eclipsess 已提交
2094

N
nhzlx 已提交
2095
  const RType *InputBias() const { return input_bias_; }
E
eclipsess 已提交
2096

N
nhzlx 已提交
2097
  const RType *InputMean() const { return input_mean_; }
E
eclipsess 已提交
2098

N
nhzlx 已提交
2099
  const RType *InputScale() const { return input_scale_; }
E
eclipsess 已提交
2100

N
nhzlx 已提交
2101
  const RType *InputVariance() const { return input_variance_; }
E
eclipsess 已提交
2102 2103 2104 2105 2106 2107 2108

  const float &Epsilon() const { return epsilon_; }

  const float &Momentum() const { return momentum_; }

  const bool &IsTest() const { return is_test_; }

N
nhzlx 已提交
2109
  void SetNewScale(RType *new_scale) { new_scale_ = new_scale; }
E
eclipsess 已提交
2110

N
nhzlx 已提交
2111
  void SetNewBias(RType *new_bias) { new_bias_ = new_bias; }
E
eclipsess 已提交
2112

N
nhzlx 已提交
2113
  const RType *NewScale() const { return new_scale_; }
E
eclipsess 已提交
2114

N
nhzlx 已提交
2115
  const RType *NewBias() const { return new_bias_; }
E
eclipsess 已提交
2116 2117

 protected:
N
nhzlx 已提交
2118 2119 2120 2121 2122
  RType *output_;
  RType *input_bias_;
  RType *input_mean_;
  RType *input_scale_;
  RType *input_variance_;
E
eclipsess 已提交
2123 2124 2125
  float epsilon_;
  float momentum_;
  bool is_test_;
N
nhzlx 已提交
2126 2127
  RType *new_bias_;
  RType *new_scale_;
E
eclipsess 已提交
2128 2129 2130 2131
};

#endif

2132
#ifdef FUSION_CONVBNRELU_OP
N
nhzlx 已提交
2133
template <typename Dtype>
2134
class FusionConvBNReluParam : public ConvParam<Dtype> {
N
nhzlx 已提交
2135 2136 2137
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

2138 2139 2140
 public:
  FusionConvBNReluParam(const VariableNameMap &inputs,
                        const VariableNameMap &outputs,
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150
                        const AttributeMap &attrs, const Scope &scope)
      : ConvParam<Dtype>(inputs, outputs, attrs, scope) {
    output_ = OpParam::OutFrom<GType>(outputs, scope);
    input_bias_ = OpParam::InputBiasFrom<GType>(inputs, scope);
    input_mean_ = OpParam::InputMeanFrom<GType>(inputs, scope);
    input_scale_ = OpParam::InputScaleFrom<GType>(inputs, scope);
    input_variance_ = OpParam::InputVarianceFrom<GType>(inputs, scope);
    epsilon_ = OpParam::GetAttr<float>("epsilon", attrs);
    momentum_ = OpParam::GetAttr<float>("momentum", attrs);
    //    is_test_ = OpParam::GetAttr<bool>("is_test", attrs);
2151
  }
N
nhzlx 已提交
2152
  RType *Output() const { return output_; }
2153

N
nhzlx 已提交
2154
  const RType *InputBias() const { return input_bias_; }
2155

N
nhzlx 已提交
2156
  const RType *InputMean() const { return input_mean_; }
2157

N
nhzlx 已提交
2158
  const RType *InputScale() const { return input_scale_; }
2159

N
nhzlx 已提交
2160
  const RType *InputVariance() const { return input_variance_; }
2161 2162 2163 2164 2165 2166 2167

  const float &Epsilon() const { return epsilon_; }

  const float &Momentum() const { return momentum_; }

  const bool &IsTest() const { return is_test_; }

N
nhzlx 已提交
2168
  void SetNewScale(RType *new_scale) { new_scale_ = new_scale; }
2169

N
nhzlx 已提交
2170
  void SetNewBias(RType *new_bias) { new_bias_ = new_bias; }
2171

N
nhzlx 已提交
2172
  const RType *NewScale() const { return new_scale_; }
2173

N
nhzlx 已提交
2174
  const RType *NewBias() const { return new_bias_; }
2175 2176

 protected:
N
nhzlx 已提交
2177 2178 2179 2180 2181
  RType *output_;
  RType *input_bias_;
  RType *input_mean_;
  RType *input_scale_;
  RType *input_variance_;
2182 2183 2184
  float epsilon_;
  float momentum_;
  bool is_test_;
N
nhzlx 已提交
2185 2186
  RType *new_bias_;
  RType *new_scale_;
2187 2188 2189
};
#endif

Y
Yao,kun 已提交
2190
#ifdef IM2SEQUENCE_OP
N
nhzlx 已提交
2191
template <typename Dtype>
Y
Yao,kun 已提交
2192
class Im2SequenceParam : public OpParam {
N
nhzlx 已提交
2193 2194 2195
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

Y
Yao,kun 已提交
2196 2197 2198 2199
 public:
  Im2SequenceParam(const VariableNameMap &inputs,
                   const VariableNameMap &outputs, const AttributeMap &attrs,
                   const Scope &scope) {
N
nhzlx 已提交
2200 2201
    input_x_ = InputXFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
Y
Yao,kun 已提交
2202 2203 2204 2205 2206
    kernels_ = GetAttr<vector<int>>("kernels", attrs);
    strides_ = GetAttr<vector<int>>("strides", attrs);
    paddings_ = GetAttr<vector<int>>("paddings", attrs);
  }

E
eclipsess 已提交
2207
  const GType *Input() const { return input_x_; }
Y
Yao,kun 已提交
2208

E
eclipsess 已提交
2209
  GType *Output() const { return out_; }
Y
Yao,kun 已提交
2210 2211 2212 2213 2214 2215 2216 2217

  const vector<int> &Kernels() const { return kernels_; }

  const vector<int> &Strides() const { return strides_; }

  const vector<int> &Paddings() const { return paddings_; }

 private:
E
eclipsess 已提交
2218 2219
  GType *input_x_;
  GType *out_;
Y
Yao,kun 已提交
2220 2221 2222 2223
  vector<int> kernels_;
  vector<int> strides_;
  vector<int> paddings_;
};
2224
#endif
Y
Yao,kun 已提交
2225

2226
#ifdef DROPOUT_OP
N
nhzlx 已提交
2227
template <typename Dtype>
Y
Yao,kun 已提交
2228
class DropoutParam : public OpParam {
N
nhzlx 已提交
2229 2230 2231
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

Y
Yao,kun 已提交
2232 2233 2234
 public:
  DropoutParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
               const AttributeMap &attrs, const Scope &scope) {
N
nhzlx 已提交
2235 2236
    input_x_ = InputXFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
Y
yangfei 已提交
2237 2238

    dropout_prob_ = GetAttr<float>("dropout_prob", attrs);
Y
Yao,kun 已提交
2239 2240
  }

N
nhzlx 已提交
2241
  const RType *InputX() const { return input_x_; }
Y
Yao,kun 已提交
2242

N
nhzlx 已提交
2243
  RType *Out() const { return out_; }
Y
Yao,kun 已提交
2244

Y
yangfei 已提交
2245 2246
  float DropoutProb() const { return dropout_prob_; }

Y
Yao,kun 已提交
2247
 private:
N
nhzlx 已提交
2248 2249
  RType *input_x_;
  RType *out_;
Y
yangfei 已提交
2250
  float dropout_prob_;
Y
Yao,kun 已提交
2251
};
2252
#endif
Y
Yao,kun 已提交
2253

N
nhzlx 已提交
2254
template <typename Dtype>
L
liuruilong 已提交
2255
class ConvTransposeParam : public OpParam {
N
nhzlx 已提交
2256 2257 2258
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

L
liuruilong 已提交
2259 2260 2261 2262
 public:
  ConvTransposeParam(const VariableNameMap &inputs,
                     const VariableNameMap &outputs, const AttributeMap &attrs,
                     const Scope &scope) {
N
nhzlx 已提交
2263 2264
    filter_ = FilterFrom<GType>(inputs, scope);
    input_ = InputFrom<GType>(inputs, scope);
2265
    // output_ = OutputFrom<GType>(outputs, scope);
qnqinan's avatar
qnqinan 已提交
2266
    if (outputs.count("Output")) {
2267
      output_ = OpParam::OutputFrom<GType>(outputs, scope);
qnqinan's avatar
qnqinan 已提交
2268
    }
L
liuruilong 已提交
2269 2270 2271 2272 2273 2274
    strides_ = GetAttr<vector<int>>("strides", attrs);
    paddings_ = GetAttr<vector<int>>("paddings", attrs);
    dilations_ = GetAttr<vector<int>>("dilations", attrs);
    groups = GetAttr<int>("groups", attrs);
  }

N
nhzlx 已提交
2275
  const RType *Input() const { return input_; }
L
liuruilong 已提交
2276

N
nhzlx 已提交
2277
  const RType *Filter() const { return filter_; }
L
liuruilong 已提交
2278

N
nhzlx 已提交
2279
  RType *Output() const { return output_; }
L
liuruilong 已提交
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289

  const vector<int> &Strides() const { return strides_; }

  const vector<int> &Paddings() const { return paddings_; }

  const vector<int> &Dilations() const { return dilations_; }

  const int &Groups() const { return groups; }

 private:
N
nhzlx 已提交
2290 2291 2292
  RType *input_;
  RType *output_;
  RType *filter_;
L
liuruilong 已提交
2293 2294 2295 2296
  vector<int> strides_;
  vector<int> paddings_;
  vector<int> dilations_;
  int groups;
Z
zhangyang 已提交
2297 2298 2299 2300 2301 2302 2303 2304 2305 2306

#ifdef PADDLE_MOBILE_FPGA

 private:
  fpga::DeconvArgs fpga_conv_args;

 public:
  const fpga::DeconvArgs &FpgaArgs() const { return fpga_conv_args; }
  void SetFpgaArgs(const fpga::DeconvArgs &args) { fpga_conv_args = args; }
#endif
L
liuruilong 已提交
2307
};
Z
zhangyang 已提交
2308

qnqinan's avatar
qnqinan 已提交
2309 2310 2311 2312 2313
#ifdef FUSION_DECONVADD_OP
template <typename Dtype>
class FusionDeconvAddParam : public ConvTransposeParam<Dtype> {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;
2314 2315

 public:
qnqinan's avatar
qnqinan 已提交
2316
  FusionDeconvAddParam(const VariableNameMap &inputs,
2317 2318 2319
                       const VariableNameMap &outputs,
                       const AttributeMap &attrs, const Scope &scope)
      : ConvTransposeParam<Dtype>(inputs, outputs, attrs, scope) {
qnqinan's avatar
qnqinan 已提交
2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340
    bias_ = OpParam::InputYFrom<GType>(inputs, scope);
    axis_ = OpParam::GetAttr<int>("axis", attrs);
    output_ = OpParam::OutFrom<GType>(outputs, scope);
  }
  RType *Bias() const { return bias_; }

  const int &Axis() const { return axis_; }

  RType *Output() const { return output_; }

 protected:
  RType *bias_;
  int axis_;
  RType *output_;
};
#endif

#ifdef FUSION_DECONVADDRELU_OP
template <typename Dtype>
using FusionDeconvAddReluParam = FusionDeconvAddParam<Dtype>;
#endif
L
liuruilong 已提交
2341

Z
zhangyang 已提交
2342 2343 2344 2345 2346
#ifdef FUSION_DECONVRELU_OP
template <typename Dtype>
using FusionDeconvReluParam = ConvTransposeParam<Dtype>;
#endif

xiebaiyuan's avatar
xiebaiyuan 已提交
2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371
#ifdef GRU_OP
template <typename Dtype>
class GruParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;

 public:
  /**
   *
   * @param inputs
   * @param outputs
   * @param attrs
   * @param scope
   * */
  GruParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
           const AttributeMap &attrs, const Scope &scope) {
    input_input_ = InputFrom<GType>(inputs, scope);
    input_h0_ = InputH0From<GType>(inputs, scope);
    input_bias_ = InputBiasFrom<GType>(inputs, scope);
    input_weight_ = InputWeightFrom<GType>(inputs, scope);

    output_batch_gate_ = OutputBatchGateFrom<GType>(outputs, scope);
    output_batch_reset_hidden_prev_ =
        OutputBatchResetHiddenPrevFrom<GType>(outputs, scope);
    output_batch_hidden_ = OutputBatchHiddenFrom<GType>(outputs, scope);
    output_hidden_ = OutputHiddenFrom<GType>(outputs, scope);
2372 2373
    activation_ = GetStringAttr("activation", attrs);
    gate_activation_ = GetStringAttr("gate_activation", attrs);
xiebaiyuan's avatar
xiebaiyuan 已提交
2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
    is_reverse_ = GetAttr<bool>("is_reverse", attrs);
  }
  const GType *InputInput() const { return input_input_; }
  const GType *InputWeight() const { return input_weight_; }
  const GType *InputH0() const { return input_h0_; }
  const GType *InputBias() const { return input_bias_; }
  const std::string &Activation() const { return activation_; }
  const std::string &GateActivation() const { return gate_activation_; }
  const bool &IsReverse() const { return is_reverse_; }

  GType *OutBatchGate() const { return output_batch_gate_; }
  GType *OutBatchResetHiddenPrev() const {
    return output_batch_reset_hidden_prev_;
  }
  GType *OutBatchHidden() const { return output_batch_hidden_; }
  GType *OutHidden() const { return output_hidden_; }

 private:
  GType *input_input_;
  GType *input_h0_;
  GType *input_bias_;
  GType *input_weight_;

  GType *output_batch_gate_;
  GType *output_batch_reset_hidden_prev_;
  GType *output_batch_hidden_;
  GType *output_hidden_;
  std::string activation_;
  std::string gate_activation_;
  bool is_reverse_;
};
#endif

2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417
#ifdef FLATTEN_OP
template <typename Dtype>
class FlattenParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  FlattenParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
               const AttributeMap &attrs, const Scope &scope) {
    input_x_ = InputXFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
xiebaiyuan's avatar
xiebaiyuan 已提交
2418
    axis = GetAttr<int>("axis", attrs);
2419 2420 2421
  }
  const RType *InputX() const { return input_x_; }
  RType *Out() const { return out_; }
xiebaiyuan's avatar
xiebaiyuan 已提交
2422
  const int &Axis() const { return axis; }
2423 2424 2425 2426

 private:
  RType *input_x_;
  RType *out_;
xiebaiyuan's avatar
xiebaiyuan 已提交
2427
  int axis;
2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
};
#endif

#ifdef SPLIT_OP
template <typename Dtype>
class SplitParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  SplitParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
             const AttributeMap &attrs, const Scope &scope) {
    input_x_ = InputXFrom<GType>(inputs, scope);
xiebaiyuan's avatar
xiebaiyuan 已提交
2441
    outs_ = OutMultiFrom<GType>(outputs, scope);
xiebaiyuan's avatar
xiebaiyuan 已提交
2442
    axis = GetAttr<int>("axis", attrs);
xiebaiyuan's avatar
xiebaiyuan 已提交
2443 2444 2445 2446 2447 2448
    num = GetAttr<int>("num", attrs);
    sections = GetAttr<std::vector<int>>("sections", attrs);

    //    for (int i = 0; i < outs_.size(); ++i) {
    //      out_ts_.push_back(*scope.FindVar(outs_[i])->GetMutable());
    //    }
2449 2450
  }
  const RType *InputX() const { return input_x_; }
xiebaiyuan's avatar
xiebaiyuan 已提交
2451 2452 2453 2454 2455
  std::vector<GType *> Outs() const { return outs_; }
  int Axis() const { return axis; }
  int Num() const { return num; }
  std::vector<int> Sections() const { return sections; }
  //  std::vector<GType> OutTs() const { return out_ts_; }
2456 2457 2458

 private:
  RType *input_x_;
xiebaiyuan's avatar
xiebaiyuan 已提交
2459
  std::vector<GType *> outs_;
xiebaiyuan's avatar
xiebaiyuan 已提交
2460
  int axis;
xiebaiyuan's avatar
xiebaiyuan 已提交
2461 2462 2463
  int num;
  std::vector<int> sections;
  //  std::vector<GType> out_ts_;
2464 2465 2466 2467 2468 2469 2470 2471 2472
#ifdef PADDLE_MOBILE_FPGA

 private:
  fpga::SplitArgs fpga_split_args;

 public:
  const fpga::SplitArgs &FpgaArgs() const { return fpga_split_args; }
  void SetFpgaArgs(const fpga::SplitArgs &args) { fpga_split_args = args; }
#endif
2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488
};
#endif

#ifdef BILINEAR_INTERP_OP
template <typename Dtype>
class BilinearInterpParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  BilinearInterpParam(const VariableNameMap &inputs,
                      const VariableNameMap &outputs, const AttributeMap &attrs,
                      const Scope &scope) {
    input_x_ = InputXFrom<GType>(inputs, scope);
    input_outsize_ = InputOutSizeFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
xiebaiyuan's avatar
xiebaiyuan 已提交
2489 2490
    out_h_ = GetAttr<int>("out_h", attrs);
    out_w_ = GetAttr<int>("out_w", attrs);
2491 2492
  }
  const RType *InputX() const { return input_x_; }
xiebaiyuan's avatar
xiebaiyuan 已提交
2493
  const RType *InputOutPutSize() const { return input_outsize_; }
2494
  RType *Out() const { return out_; }
xiebaiyuan's avatar
xiebaiyuan 已提交
2495 2496
  int OutH() const { return out_h_; }
  int OutW() const { return out_w_; }
2497 2498 2499 2500 2501

 private:
  RType *input_x_;
  RType *input_outsize_;
  RType *out_;
xiebaiyuan's avatar
xiebaiyuan 已提交
2502 2503
  int out_h_;
  int out_w_;
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518
};
#endif

#ifdef SHAPE_OP
template <typename Dtype>
class ShapeParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  ShapeParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
             const AttributeMap &attrs, const Scope &scope) {
    input_ = InputFrom<GType>(inputs, scope);
    out_ = OutFrom<GType>(outputs, scope);
  }
xiebaiyuan's avatar
xiebaiyuan 已提交
2519
  const RType *Input() const { return input_; }
2520 2521 2522 2523 2524 2525 2526 2527
  RType *Out() const { return out_; }

 private:
  RType *input_;
  RType *out_;
};
#endif

H
hjchen2 已提交
2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
#ifdef TOP_K_OP
template <typename Dtype>
class TopKParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  TopKParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
            const AttributeMap &attrs, const Scope &scope) {
    input_ = OpParam::GetVarValue<GType>("X", inputs, scope);
    output_ = OpParam::GetVarValue<GType>("Out", outputs, scope);
    indices_ = OpParam::GetVarValue<GType>("Indices", outputs, scope);
    k_ = OpParam::GetAttr<int>("k", attrs);
  }

 public:
  RType *input_;
  RType *output_;
  RType *indices_;
  int k_;
};
#endif  // TOP_K_OP

#ifdef CAST_OP
template <typename Dtype>
class CastParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  CastParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
            const AttributeMap &attrs, const Scope &scope) {
    input_ = OpParam::GetVarValue<GType>("X", inputs, scope);
    output_ = OpParam::GetVarValue<GType>("Out", outputs, scope);
    input_type_ = OpParam::GetAttr<int>("in_dtype", attrs);
    output_type_ = OpParam::GetAttr<int>("out_dtype", attrs);
  }

 public:
  RType *input_;
  RType *output_;
  int input_type_;
  int output_type_;
};
#endif  // CAST_OP

2574
#ifdef QUANT_OP
2575
template <typename Dtype>
2576 2577 2578 2579 2580
class QuantizeParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
2581 2582
  QuantizeParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
                const AttributeMap &attrs, const Scope &scope) {
2583
    input_ = InputXFrom<GType>(inputs, scope);
H
hjchen2 已提交
2584
    output_ = OutFrom<GType>(outputs, scope);
2585 2586
    // online
    // scale = max(abs(x))
H
hjchen2 已提交
2587
    online_scale_ = OpParam::GetVarValue<GType>("OutScale", outputs, scope);
2588
    // offline
2589
    if (inputs.count("InScale")) {
2590 2591
      offline_ = true;
      offline_scale_ = OpParam::GetVarValue<GType>("InScale", inputs, scope);
2592 2593
    }
    // x = round(scale * x)
2594 2595
    if (OpParam::HasAttr("round_type", attrs)) {
      round_type_ = OpParam::GetAttr<RoundType>("round_type", attrs);
H
hjchen2 已提交
2596
    }
2597 2598 2599 2600
  }

 public:
  // op input
2601
  GType *input_;
2602
  // op output
2603
  GType *output_;
2604
  RType *online_scale_;
2605 2606 2607 2608
  // quantize offline scale
  RType *offline_scale_;
  // if offine scale or not
  bool offline_ = false;
2609
  // round method type
2610 2611
  RoundType round_type_ = ROUND_NEAREST_AWAY_ZERO;
  // RoundType round_type_ = ROUND_NEAREST_TOWARDS_ZERO;
2612
};
2613
#endif
2614

2615
#ifdef DEQUANT_OP
2616
template <typename Dtype>
2617 2618 2619 2620 2621
class DequantizeParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
2622 2623
  DequantizeParam(const VariableNameMap &inputs, const VariableNameMap &outputs,
                  const AttributeMap &attrs, const Scope &scope) {
2624
    input_ = InputXFrom<GType>(inputs, scope);
2625
    output_ = OutFrom<GType>(outputs, scope);
H
hjchen2 已提交
2626
    activation_scale_ = OpParam::GetVarValue<GType>("Scale", inputs, scope);
2627
    // dequantization is performed as x = x / static_scale / online_scale
2628 2629
    if (OpParam::HasAttr("weight_scale", attrs)) {
      weight_scale_ = OpParam::GetAttr<float>("weight_scale", attrs);
2630
    } else {
2631
      weight_scale_ = OpParam::GetAttr<float>("max_range", attrs);
2632 2633 2634 2635 2636
    }
  }

 public:
  // op input
2637
  GType *input_;
2638
  // op output
2639
  GType *output_;
2640 2641 2642
  RType *activation_scale_;
  float weight_scale_;
};
2643
#endif
2644

2645 2646 2647 2648
#if defined(FUSION_DEQUANT_BN_OP) || defined(FUSION_DEQUANT_ADD_BN_OP) || \
    defined(FUSION_DEQUANT_ADD_BN_RELU_OP) ||                             \
    defined(FUSION_DEQUANT_BN_RELU_OP) ||                                 \
    defined(FUSION_DEQUANT_ADD_BN_QUANT_OP) ||                            \
2649
    defined(FUSION_DEQUANT_ADD_BN_RELU_QUANT_OP)
H
hjchen2 已提交
2650
template <typename Dtype>
2651
class FusionDequantBNParam : public DequantizeParam<Dtype> {
H
hjchen2 已提交
2652 2653 2654 2655
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
2656 2657 2658
  FusionDequantBNParam(const VariableNameMap &inputs,
                       const VariableNameMap &outputs,
                       const AttributeMap &attrs, const Scope &scope)
H
hjchen2 已提交
2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674
      : DequantizeParam<Dtype>(inputs, outputs, attrs, scope) {
    // batch norm params
    bn_mean_ = OpParam::GetVarValue<GType>("BNMean", inputs, scope);
    bn_variance_ = OpParam::GetVarValue<GType>("BNVariance", inputs, scope);
    bn_scale_ = OpParam::GetVarValue<GType>("BNScale", inputs, scope);
    bn_bias_ = OpParam::GetVarValue<GType>("BNBias", inputs, scope);
    epsilon_ = OpParam::GetAttr<float>("epsilon", attrs);
  }

 public:
  // batch norm
  RType *bn_mean_;
  RType *bn_variance_;
  RType *bn_scale_;
  RType *bn_bias_;
  float epsilon_;
2675 2676 2677
};
#endif

2678 2679 2680 2681
#if defined(FUSION_DEQUANT_ADD_BN_RELU_OP) ||  \
    defined(FUSION_DEQUANT_ADD_BN_OP) ||       \
    defined(FUSION_DEQUANT_ADD_BN_QUANT_OP) || \
    defined(FUSION_DEQUANT_ADD_BN_RELU_QUANT_OP)
2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703
template <typename Dtype>
class FusionDequantAddBNParam : public FusionDequantBNParam<Dtype> {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  FusionDequantAddBNParam(const VariableNameMap &inputs,
                          const VariableNameMap &outputs,
                          const AttributeMap &attrs, const Scope &scope)
      : FusionDequantBNParam<Dtype>(inputs, outputs, attrs, scope) {
    // element wise add params
    axis_ = OpParam::GetAttr<int>("axis", attrs);
    bias_ = OpParam::InputYFrom<GType>(inputs, scope);
  }

 public:
  // elementwise add
  int axis_;
  RType *bias_;
};
#endif

2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717
#ifdef FUSION_DEQUANT_ADD_BN_QUANT_OP
template <typename Dtype>
class FusionDequantAddBNQuantParam : public FusionDequantAddBNParam<Dtype> {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  FusionDequantAddBNQuantParam(const VariableNameMap &inputs,
                               const VariableNameMap &outputs,
                               const AttributeMap &attrs, const Scope &scope)
      : FusionDequantAddBNParam<Dtype>(inputs, outputs, attrs, scope) {
    // scale output
    online_scale_ = OpParam::GetVarValue<GType>("OutScale", outputs, scope);
    // offline
2718 2719 2720
    if (inputs.count("InScale")) {
      offline_ = true;
      offline_scale_ = OpParam::GetVarValue<GType>("InScale", inputs, scope);
2721 2722 2723 2724 2725 2726 2727 2728 2729
    }
    // x = round(scale * x)
    if (OpParam::HasAttr("round_type", attrs)) {
      round_type_ = OpParam::GetAttr<RoundType>("round_type", attrs);
    }
  }

 public:
  RType *online_scale_;
2730 2731 2732 2733
  // quantize offline scale
  RType *offline_scale_;
  // if offine scale or not
  bool offline_ = false;
2734 2735 2736 2737 2738 2739
  // round method type
  // RoundType round_type_ = ROUND_NEAREST_AWAY_ZERO;
  RoundType round_type_ = ROUND_NEAREST_TOWARDS_ZERO;
};
#endif

2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780
#ifdef SEQUENCE_EXPAND_OP
template <typename Dtype>
class SequenceExpandParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  SequenceExpandParam(const VariableNameMap &inputs,
                      const VariableNameMap &outputs, const AttributeMap &attrs,
                      const Scope &scope) {
    input_x_ = InputXFrom<GType>(inputs, scope);
    input_y_ = InputYFrom<GType>(inputs, scope);
    output_ = OutFrom<GType>(outputs, scope);
    ref_level_ = -1;
    if (OpParam::HasAttr("ref_level", attrs)) {
      ref_level_ = OpParam::GetAttr<int>("ref_level", attrs);
    }
  }

 public:
  GType *input_x_;
  GType *input_y_;
  GType *output_;
  int ref_level_;
};
#endif  // SEQUENCE_EXPAND_OP

#ifdef SEQUENCE_POOL_OP
template <typename Dtype>
class SequencePoolParam : public OpParam {
  typedef typename DtypeTensorTrait<Dtype>::gtype GType;
  typedef typename DtypeTensorTrait<Dtype>::rtype RType;

 public:
  SequencePoolParam(const VariableNameMap &inputs,
                    const VariableNameMap &outputs, const AttributeMap &attrs,
                    const Scope &scope) {
    input_ = InputXFrom<GType>(inputs, scope);
    output_ = OutFrom<GType>(outputs, scope);
    pool_type_ = "MAX";
    if (OpParam::HasAttr("pooltype", attrs)) {
H
hjchen2 已提交
2781
      pool_type_ = OpParam::GetStringAttr("pooltype", attrs);
2782 2783 2784 2785 2786 2787 2788 2789 2790 2791
    }
  }

 public:
  GType *input_;
  GType *output_;
  std::string pool_type_;
};
#endif  // SEQUENCE_EXPAND_OP

朔-望's avatar
朔-望 已提交
2792 2793
}  // namespace operators
}  // namespace paddle_mobile