common.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include "NvInferPlugin.h"
  16. #include "NvInferRuntimeCommon.h"
  17. #include "ultra_infer/utils/utils.h"
  18. #include <cstring>
  19. #include <iostream>
  20. #include <memory>
  21. #include <sstream>
  22. #include <string>
  23. #include <vector>
  24. namespace ultra_infer {
  25. class BasePlugin : public nvinfer1::IPluginV2DynamicExt {
  26. protected:
  27. void setPluginNamespace(const char *libNamespace) noexcept override {
  28. mNamespace = libNamespace;
  29. }
  30. const char *getPluginNamespace() const noexcept override {
  31. return mNamespace.c_str();
  32. }
  33. std::string mNamespace;
  34. };
  35. class BaseCreator : public nvinfer1::IPluginCreator {
  36. public:
  37. void setPluginNamespace(const char *libNamespace) noexcept override {
  38. mNamespace = libNamespace;
  39. }
  40. const char *getPluginNamespace() const noexcept override {
  41. return mNamespace.c_str();
  42. }
  43. protected:
  44. std::string mNamespace;
  45. };
  46. typedef enum {
  47. STATUS_SUCCESS = 0,
  48. STATUS_FAILURE = 1,
  49. STATUS_BAD_PARAM = 2,
  50. STATUS_NOT_SUPPORTED = 3,
  51. STATUS_NOT_INITIALIZED = 4
  52. } pluginStatus_t;
  53. // Write values into buffer
  54. template <typename T> void write(char *&buffer, const T &val) {
  55. std::memcpy(buffer, &val, sizeof(T));
  56. buffer += sizeof(T);
  57. }
  58. // Read values from buffer
  59. template <typename T> T read(const char *&buffer) {
  60. T val{};
  61. std::memcpy(&val, buffer, sizeof(T));
  62. buffer += sizeof(T);
  63. return val;
  64. }
  65. } // namespace ultra_infer