helper.cc 2.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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. */

#include "paddle/fluid/operators/jit/helper.h"
T
tensor-tang 已提交
16
#include <algorithm>  // tolower
17
#include "paddle/fluid/platform/enforce.h"
18 19 20 21 22

namespace paddle {
namespace operators {
namespace jit {

23 24 25 26
#define ONE_CASE(key) \
  case key:           \
    return #key

27 28
const char* to_string(KernelType kt) {
  switch (kt) {
29
    ONE_CASE(kNone);
T
tensor-tang 已提交
30 31 32 33 34 35 36 37 38
    ONE_CASE(kVMul);
    ONE_CASE(kVAdd);
    ONE_CASE(kVAddRelu);
    ONE_CASE(kVSub);
    ONE_CASE(kVScal);
    ONE_CASE(kVAddBias);
    ONE_CASE(kVRelu);
    ONE_CASE(kVIdentity);
    ONE_CASE(kVExp);
T
tensor-tang 已提交
39
    ONE_CASE(kVSquare);
T
tensor-tang 已提交
40 41 42 43 44 45 46 47 48 49
    ONE_CASE(kVSigmoid);
    ONE_CASE(kVTanh);
    ONE_CASE(kLSTMCtHt);
    ONE_CASE(kLSTMC1H1);
    ONE_CASE(kGRUH1);
    ONE_CASE(kGRUHtPart1);
    ONE_CASE(kGRUHtPart2);
    ONE_CASE(kCRFDecoding);
    ONE_CASE(kLayerNorm);
    ONE_CASE(kNCHW16CMulNC);
50
    ONE_CASE(kSeqPool);
T
tensor-tang 已提交
51
    ONE_CASE(kMatMul);
52 53 54
    ONE_CASE(kHMax);
    ONE_CASE(kHSum);
    ONE_CASE(kSoftmax);
55
    default:
T
tensor-tang 已提交
56
      PADDLE_THROW("Not support type: %d, or forget to add it.", kt);
57 58 59 60
      return "NOT JITKernel";
  }
  return nullptr;
}
61 62 63 64 65 66 67 68 69 70 71 72 73

const char* to_string(SeqPoolType tp) {
  switch (tp) {
    ONE_CASE(kNonePoolType);
    ONE_CASE(kSum);
    ONE_CASE(kAvg);
    ONE_CASE(kSqrt);
    default:
      PADDLE_THROW("Not support type: %d, or forget to add it.", tp);
      return "NOT PoolType";
  }
  return nullptr;
}
74
#undef ONE_CASE
75

T
tensor-tang 已提交
76 77 78 79
KernelType to_kerneltype(const std::string& act) {
  std::string lower = act;
  std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
  if (lower == "relu" || lower == "vrelu") {
T
tensor-tang 已提交
80
    return kVRelu;
T
tensor-tang 已提交
81
  } else if (lower == "identity" || lower == "videntity" || lower == "") {
T
tensor-tang 已提交
82
    return kVIdentity;
T
tensor-tang 已提交
83
  } else if (lower == "exp" || lower == "vexp") {
T
tensor-tang 已提交
84
    return kVExp;
T
tensor-tang 已提交
85
  } else if (lower == "sigmoid" || lower == "vsigmoid") {
T
tensor-tang 已提交
86
    return kVSigmoid;
T
tensor-tang 已提交
87
  } else if (lower == "tanh" || lower == "vtanh") {
T
tensor-tang 已提交
88
    return kVTanh;
T
tensor-tang 已提交
89
  }
90
  PADDLE_THROW("Not support type: %s, or forget to add this case", act);
T
tensor-tang 已提交
91
  return kNone;
T
tensor-tang 已提交
92 93
}

94 95 96
}  // namespace jit
}  // namespace operators
}  // namespace paddle