serializer.cc 2.3 KB
Newer Older
1 2 3 4 5 6 7 8
//
// Copyright (c) 2017 XiaoMi All rights reserved.
//

#include "mace/core/serializer.h"

namespace mace {

L
Liangliang He 已提交
9
unique_ptr<ConstTensor> Serializer::Serialize(const Tensor &tensor,
L
Liangliang He 已提交
10
                                              const string &name) {
11 12 13 14
  MACE_NOT_IMPLEMENTED;
  return nullptr;
}

L
Liangliang He 已提交
15
unique_ptr<Tensor> Serializer::Deserialize(const ConstTensor &proto,
16
                                           DeviceType type) {
L
Liangliang He 已提交
17 18
  unique_ptr<Tensor> tensor(
      new Tensor(GetDeviceAllocator(type), proto.data_type()));
19 20
  vector<index_t> dims;
  for (const index_t d : proto.dims()) {
21 22 23 24 25
    dims.push_back(d);
  }
  tensor->Resize(dims);

  switch (proto.data_type()) {
L
liuqi 已提交
26 27 28 29
    case DT_HALF:
      tensor->Copy<half>(reinterpret_cast<const half*>(proto.data()),
                         proto.data_size());
      break;
30
    case DT_FLOAT:
L
Liangliang He 已提交
31 32
      tensor->Copy<float>(reinterpret_cast<const float *>(proto.data()),
                          proto.data_size());
33 34
      break;
    case DT_DOUBLE:
L
Liangliang He 已提交
35 36
      tensor->Copy<double>(reinterpret_cast<const double *>(proto.data()),
                           proto.data_size());
37 38
      break;
    case DT_INT32:
L
Liangliang He 已提交
39 40
      tensor->Copy<int32_t>(reinterpret_cast<const int32_t *>(proto.data()),
                            proto.data_size());
L
liuqi 已提交
41 42
      break;
    case DT_INT64:
L
Liangliang He 已提交
43 44
      tensor->Copy<int64_t>(reinterpret_cast<const int64_t *>(proto.data()),
                            proto.data_size());
45 46
      break;
    case DT_UINT8:
李寅 已提交
47 48
      tensor->Copy<uint8_t>(reinterpret_cast<const uint8_t *>(proto.data()),
                            proto.data_size());
49 50
      break;
    case DT_INT16:
L
Liangliang He 已提交
51 52
      tensor->CopyWithCast<int32_t, uint16_t>(
          reinterpret_cast<const int32_t *>(proto.data()), proto.data_size());
53 54
      break;
    case DT_INT8:
L
Liangliang He 已提交
55 56
      tensor->CopyWithCast<int32_t, int8_t>(
          reinterpret_cast<const int32_t *>(proto.data()), proto.data_size());
57 58
      break;
    case DT_UINT16:
L
Liangliang He 已提交
59 60
      tensor->CopyWithCast<int32_t, int16_t>(
          reinterpret_cast<const int32_t *>(proto.data()), proto.data_size());
61 62
      break;
    case DT_BOOL:
L
Liangliang He 已提交
63 64
      tensor->CopyWithCast<int32_t, bool>(
          reinterpret_cast<const int32_t *>(proto.data()), proto.data_size());
65 66 67 68 69 70 71 72 73
      break;
    default:
      MACE_NOT_IMPLEMENTED;
      break;
  }

  return tensor;
}

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