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

N
nhzlx 已提交
27 28 29 30 31 32 33
// Some trt base classes lack of the destructor.
// We use a assisted class to fix this.
struct DeleteHelper {
 protected:
  virtual ~DeleteHelper() {}
};

N
nhzlx 已提交
34
template <typename T>
N
nhzlx 已提交
35
inline void SerializeValue(void** buffer, T const& value);
N
nhzlx 已提交
36 37

template <typename T>
N
nhzlx 已提交
38 39
inline void DeserializeValue(void const** buffer, size_t* buffer_size,
                             T* value);
N
nhzlx 已提交
40

41
namespace details {
N
nhzlx 已提交
42 43 44 45 46 47 48 49

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 已提交
50
  static size_t SerializedSize(T const& value) { return sizeof(T); }
51

N
nhzlx 已提交
52 53
  static void Serialize(void** buffer, T const& value) {
    std::memcpy(*buffer, &value, sizeof(T));
N
nhzlx 已提交
54 55
    reinterpret_cast<char*&>(*buffer) += sizeof(T);
  }
56

N
nhzlx 已提交
57
  static void Deserialize(void const** buffer, size_t* buffer_size, T* value) {
N
nhzlx 已提交
58
    assert(*buffer_size >= sizeof(T));
N
nhzlx 已提交
59
    std::memcpy(value, *buffer, sizeof(T));
N
nhzlx 已提交
60 61 62 63 64 65 66
    reinterpret_cast<char const*&>(*buffer) += sizeof(T);
    *buffer_size -= sizeof(T);
  }
};

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

N
nhzlx 已提交
69
  static void Serialize(void** buffer, const char* value) {
70
    std::strcpy(static_cast<char*>(*buffer), value);  // NOLINT
N
nhzlx 已提交
71 72
    reinterpret_cast<char*&>(*buffer) += strlen(value) + 1;
  }
73

N
nhzlx 已提交
74
  static void Deserialize(void const** buffer, size_t* buffer_size,
N
nhzlx 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88
                          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 已提交
89
  static size_t SerializedSize(std::vector<T> const& value) {
N
nhzlx 已提交
90 91
    return sizeof(value.size()) + value.size() * sizeof(T);
  }
92

N
nhzlx 已提交
93 94
  static void Serialize(void** buffer, std::vector<T> const& value) {
    SerializeValue(buffer, value.size());
N
nhzlx 已提交
95
    size_t nbyte = value.size() * sizeof(T);
N
nhzlx 已提交
96
    std::memcpy(*buffer, value.data(), nbyte);
N
nhzlx 已提交
97 98
    reinterpret_cast<char*&>(*buffer) += nbyte;
  }
99

N
nhzlx 已提交
100
  static void Deserialize(void const** buffer, size_t* buffer_size,
N
nhzlx 已提交
101 102
                          std::vector<T>* value) {
    size_t size;
N
nhzlx 已提交
103
    DeserializeValue(buffer, buffer_size, &size);
N
nhzlx 已提交
104 105
    value->resize(size);
    size_t nbyte = value->size() * sizeof(T);
106
    PADDLE_ENFORCE_GE(*buffer_size, nbyte);
N
nhzlx 已提交
107
    std::memcpy(value->data(), *buffer, nbyte);
N
nhzlx 已提交
108 109 110 111 112
    reinterpret_cast<char const*&>(*buffer) += nbyte;
    *buffer_size -= nbyte;
  }
};

113
}  // namespace details
N
nhzlx 已提交
114 115

template <typename T>
N
nhzlx 已提交
116
inline size_t SerializedSize(T const& value) {
117
  return details::Serializer<T>::SerializedSize(value);
N
nhzlx 已提交
118 119 120
}

template <typename T>
N
nhzlx 已提交
121
inline void SerializeValue(void** buffer, T const& value) {
122
  return details::Serializer<T>::Serialize(buffer, value);
N
nhzlx 已提交
123 124 125
}

template <typename T>
N
nhzlx 已提交
126 127
inline void DeserializeValue(void const** buffer, size_t* buffer_size,
                             T* value) {
128
  return details::Serializer<T>::Deserialize(buffer, buffer_size, value);
N
nhzlx 已提交
129
}
130 131 132 133 134

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