serialize.h 4.1 KB
Newer Older
N
nhzlx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// 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.

#pragma once

#include <cstring>
#include <type_traits>
#include <vector>
20 21 22 23 24 25
#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace inference {
namespace tensorrt {
namespace plugin {
N
nhzlx 已提交
26 27

template <typename T>
N
nhzlx 已提交
28
inline void SerializeValue(void** buffer, T const& value);
N
nhzlx 已提交
29 30

template <typename T>
N
nhzlx 已提交
31 32
inline void DeserializeValue(void const** buffer, size_t* buffer_size,
                             T* value);
N
nhzlx 已提交
33

34
namespace details {
N
nhzlx 已提交
35 36 37 38 39 40 41 42

template <typename T, class Enable = void>
struct Serializer {};

template <typename T>
struct Serializer<T, typename std::enable_if<std::is_arithmetic<T>::value ||
                                             std::is_enum<T>::value ||
                                             std::is_pod<T>::value>::type> {
N
nhzlx 已提交
43
  static size_t SerializedSize(T const& value) { return sizeof(T); }
44

N
nhzlx 已提交
45 46
  static void Serialize(void** buffer, T const& value) {
    std::memcpy(*buffer, &value, sizeof(T));
N
nhzlx 已提交
47 48
    reinterpret_cast<char*&>(*buffer) += sizeof(T);
  }
49

N
nhzlx 已提交
50
  static void Deserialize(void const** buffer, size_t* buffer_size, T* value) {
N
nhzlx 已提交
51
    assert(*buffer_size >= sizeof(T));
N
nhzlx 已提交
52
    std::memcpy(value, *buffer, sizeof(T));
N
nhzlx 已提交
53 54 55 56 57 58 59
    reinterpret_cast<char const*&>(*buffer) += sizeof(T);
    *buffer_size -= sizeof(T);
  }
};

template <>
struct Serializer<const char*> {
N
nhzlx 已提交
60
  static size_t SerializedSize(const char* value) { return strlen(value) + 1; }
61

N
nhzlx 已提交
62
  static void Serialize(void** buffer, const char* value) {
63
    std::strcpy(static_cast<char*>(*buffer), value);  // NOLINT
N
nhzlx 已提交
64 65
    reinterpret_cast<char*&>(*buffer) += strlen(value) + 1;
  }
66

N
nhzlx 已提交
67
  static void Deserialize(void const** buffer, size_t* buffer_size,
N
nhzlx 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81
                          const char** value) {
    *value = static_cast<char const*>(*buffer);
    size_t data_size = strnlen(*value, *buffer_size) + 1;
    assert(*buffer_size >= data_size);
    reinterpret_cast<char const*&>(*buffer) += data_size;
    *buffer_size -= data_size;
  }
};

template <typename T>
struct Serializer<std::vector<T>,
                  typename std::enable_if<std::is_arithmetic<T>::value ||
                                          std::is_enum<T>::value ||
                                          std::is_pod<T>::value>::type> {
N
nhzlx 已提交
82
  static size_t SerializedSize(std::vector<T> const& value) {
N
nhzlx 已提交
83 84
    return sizeof(value.size()) + value.size() * sizeof(T);
  }
85

N
nhzlx 已提交
86 87
  static void Serialize(void** buffer, std::vector<T> const& value) {
    SerializeValue(buffer, value.size());
N
nhzlx 已提交
88
    size_t nbyte = value.size() * sizeof(T);
N
nhzlx 已提交
89
    std::memcpy(*buffer, value.data(), nbyte);
N
nhzlx 已提交
90 91
    reinterpret_cast<char*&>(*buffer) += nbyte;
  }
92

N
nhzlx 已提交
93
  static void Deserialize(void const** buffer, size_t* buffer_size,
N
nhzlx 已提交
94 95
                          std::vector<T>* value) {
    size_t size;
N
nhzlx 已提交
96
    DeserializeValue(buffer, buffer_size, &size);
N
nhzlx 已提交
97 98
    value->resize(size);
    size_t nbyte = value->size() * sizeof(T);
99
    PADDLE_ENFORCE_GE(*buffer_size, nbyte);
N
nhzlx 已提交
100
    std::memcpy(value->data(), *buffer, nbyte);
N
nhzlx 已提交
101 102 103 104 105
    reinterpret_cast<char const*&>(*buffer) += nbyte;
    *buffer_size -= nbyte;
  }
};

106
}  // namespace details
N
nhzlx 已提交
107 108

template <typename T>
N
nhzlx 已提交
109
inline size_t SerializedSize(T const& value) {
110
  return details::Serializer<T>::SerializedSize(value);
N
nhzlx 已提交
111 112 113
}

template <typename T>
N
nhzlx 已提交
114
inline void SerializeValue(void** buffer, T const& value) {
115
  return details::Serializer<T>::Serialize(buffer, value);
N
nhzlx 已提交
116 117 118
}

template <typename T>
N
nhzlx 已提交
119 120
inline void DeserializeValue(void const** buffer, size_t* buffer_size,
                             T* value) {
121
  return details::Serializer<T>::Deserialize(buffer, buffer_size, value);
N
nhzlx 已提交
122
}
123 124 125 126 127

}  // namespace plugin
}  // namespace tensorrt
}  // namespace inference
}  // namespace paddle