paddle_infer_contrib.h 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2021 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

17
#include "paddle_inference_api.h"  // NOLINT
18 19 20 21 22 23 24 25 26 27

namespace paddle_infer {
namespace contrib {

class TensorUtils {
 public:
  static void* CudaMallocPinnedMemory(size_t size);
  static void CudaFreePinnedMemory(void* mem);

  static void CopyTensor(Tensor* p_dst, const Tensor& src);
28 29
  static void CopyTensorAsync(Tensor* p_dst,
                              const Tensor& src,
30
                              void* exec_stream);
31 32 33
  static void CopyTensorAsync(Tensor* p_dst,
                              const Tensor& src,
                              CallbackFunc cb,
34 35 36
                              void* cb_params);

 private:
37 38 39 40
  static void CopyTensorImpl(Tensor* p_dst,
                             const Tensor& src,
                             void* exec_stream,
                             CallbackFunc cb,
41 42 43
                             void* cb_params);
};

44 45 46 47 48 49 50
/// \brief A status class, used to intercept exceptions and convert
/// them into a status number.
class Status {
 public:
  using Code = int;
  struct Impl;

石晓伟 已提交
51 52
  Status();
  explicit Status(std::exception_ptr e);
53

石晓伟 已提交
54
  Status(const Status&);
55 56 57 58 59 60 61 62 63
  Status& operator=(const Status&) noexcept;
  Status& operator=(Status&&) = default;
  Status(Status&&) = default;

  ///
  /// \brief Construct a status which indicate ok.
  ///
  /// \return A status which indicate ok.
  ///
石晓伟 已提交
64
  static Status OK();
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

  ///
  /// \brief Determine whether the status is ok.
  ///
  /// \return Whether the status is ok.
  ///
  bool ok() const noexcept;

  ///
  /// \brief Return the error code.
  /// The meaning corresponds to the following.
  ///
  /// CODE    IMPLICATION
  ///  -1      UNKNOWN
  ///  0        NORMAL
  ///  1        LEGACY
  ///  2    INVALID_ARGUMENT
  ///  3       NOT_FOUND
  ///  4     OUT_OF_RANGE
  ///  5    ALREADY_EXISTS
  ///  6   RESOURCE_EXHAUSTED
  ///  7  PRECONDITION_NOT_MET
  ///  8   PERMISSION_DENIED
  ///  9   EXECUTION_TIMEOUT
  ///  10    UNIMPLEMENTED
  ///  11     UNAVAILABLE
  ///  12        FATAL
  ///  13       EXTERNAL
  ///
  /// \return The error code.
  ///
  Code code() const noexcept;

  ///
  /// \brief Return the error message.
  ///
  /// \return The error message.
  ///
  const std::string& error_message() const noexcept;

  bool operator==(const Status& x) const noexcept;
  bool operator!=(const Status& x) const noexcept;

 private:
  std::shared_ptr<Impl> impl_;
};

///
/// \brief A wrapper used to provide exception safety.
///
/// \param func Wrapped function.
/// \param args Parameters of the wrapped function.
/// \return State result of calling function.
///
template <typename Func, typename... Args>
Status get_status(Func func, Args&&... args) noexcept(
    noexcept(Status(std::declval<Status>()))) {
  try {
    func(std::forward<Args>(args)...);
  } catch (...) {
    return Status(std::current_exception());
  }
  return Status::OK();
}

130 131
}  // namespace contrib
}  // namespace paddle_infer