types.cc 1.9 KB
Newer Older
L
Liangliang He 已提交
1
// Copyright 2018 The MACE Authors. All Rights Reserved.
L
liuqi 已提交
2
//
L
Liangliang He 已提交
3 4 5
// 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
L
liuqi 已提交
6
//
L
Liangliang He 已提交
7 8 9 10 11 12 13
//     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.
L
liuqi 已提交
14

15
#include <cstdint>
16
#include <map>
17

L
liuqi 已提交
18
#include "mace/core/types.h"
19
#include "mace/utils/logging.h"
L
liuqi 已提交
20 21 22 23 24 25 26

namespace mace {

bool DataTypeCanUseMemcpy(DataType dt) {
  switch (dt) {
    case DT_FLOAT:
    case DT_UINT8:
27
    case DT_INT32:
L
luxuhui 已提交
28
    case DT_BFLOAT16:
L
lichao18 已提交
29
    case DT_FLOAT16:
L
liuqi 已提交
30 31 32 33 34 35
      return true;
    default:
      return false;
  }
}

L
liuqi 已提交
36 37
std::string DataTypeToString(const DataType dt) {
  static std::map<DataType, std::string> dtype_string_map = {
李寅 已提交
38 39 40
      {DT_FLOAT, "DT_FLOAT"},
      {DT_HALF, "DT_HALF"},
      {DT_UINT8, "DT_UINT8"},
L
luxuhui 已提交
41
      {DT_INT32, "DT_INT32"},
L
lichao18 已提交
42 43
      {DT_BFLOAT16, "DT_BFLOAT16"},
      {DT_FLOAT16, "DT_FLOAT16"}};
44
  MACE_CHECK(dt != DT_INVALID, "Not support Invalid data type");
L
liuqi 已提交
45 46
  return dtype_string_map[dt];
}
47 48

size_t GetEnumTypeSize(const DataType dt) {
L
liuqi 已提交
49 50
  switch (dt) {
    case DT_FLOAT:
51
      return sizeof(float);
L
liuqi 已提交
52
    case DT_HALF:
53
      return sizeof(half);
L
lichao18 已提交
54 55
#if defined(MACE_ENABLE_NEON) && defined(__ANDROID__) || \
    defined(MACE_ENABLE_FP16)
56 57
    case DT_FLOAT16:
      return sizeof(float16_t);
L
luxuhui 已提交
58 59 60 61
#endif
#ifdef MACE_ENABLE_BFLOAT16
    case DT_BFLOAT16:
      return sizeof(BFloat16);
62
#endif
L
liuqi 已提交
63
    case DT_UINT8:
64
      return sizeof(uint8_t);
L
liuqi 已提交
65
    case DT_INT32:
66
      return sizeof(int32_t);
L
liuqi 已提交
67
    default:
68
      LOG(FATAL) << "Unsupported data type: " << dt;
69
      return 0;
L
liuqi 已提交
70 71 72
  }
}

L
Liangliang He 已提交
73
}  // namespace mace