mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-14 18:12:05 +00:00
* Fix up constness in pybindings Fix up return argument treatments. Specifically, for all functions that return pointers or references to the members of other pybind registered classes, we want not to copy them, but internally bump up a reference to the hosting class so they do not disappear before the reference to the returned members is re-claimed. This policy is applied by default to def_property and def_readwrite but not to def_readonly and other def methods. See https://pybind11-jagerman.readthedocs.io/en/stable/advanced.html#return-value-policies https://pybind11.readthedocs.io/en/stable/advanced/functions.html#return-value-policies Move OrtValue binding to a separate file Move IOBinding into separate file.
97 lines
3.2 KiB
C++
97 lines
3.2 KiB
C++
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT License.
|
|
|
|
#pragma once
|
|
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
|
|
|
|
#include <pybind11/iostream.h>
|
|
#include <pybind11/pybind11.h>
|
|
#include <pybind11/stl.h>
|
|
|
|
#include "core/common/logging/logging.h"
|
|
#include "core/common/logging/sinks/clog_sink.h"
|
|
#include "core/common/logging/sinks/cerr_sink.h"
|
|
#include "core/framework/allocatormgr.h"
|
|
#include "core/session/environment.h"
|
|
#include "core/framework/ml_value.h"
|
|
#include "core/session/inference_session.h"
|
|
|
|
|
|
namespace onnxruntime {
|
|
namespace python {
|
|
|
|
namespace py = pybind11;
|
|
|
|
extern const char* PYTHON_ORTVALUE_OBJECT_NAME;
|
|
extern const char* PYTHON_ORTVALUE_NATIVE_OBJECT_ATTR;
|
|
|
|
bool IsNumericNumpyType(int npy_type);
|
|
|
|
bool IsNumericNumpyArray(py::object& py_object);
|
|
|
|
int OnnxRuntimeTensorToNumpyType(const DataTypeImpl* tensor_type);
|
|
|
|
MLDataType NumpyTypeToOnnxRuntimeType(int numpy_type);
|
|
|
|
using MemCpyFunc = void (*)(void*, const void*, size_t);
|
|
|
|
void CpuToCpuMemCpy(void*, const void*, size_t);
|
|
|
|
void AddTensorAsPyObj(const OrtValue& val, std::vector<pybind11::object>& pyobjs,
|
|
const DataTransferManager* data_transfer_manager,
|
|
const std::unordered_map<OrtDevice::DeviceType, MemCpyFunc>* mem_cpy_to_host_functions);
|
|
|
|
void AddNonTensorAsPyObj(const OrtValue& val, std::vector<pybind11::object>& pyobjs,
|
|
const DataTransferManager* data_transfer_manager,
|
|
const std::unordered_map<OrtDevice::DeviceType, MemCpyFunc>* mem_cpy_to_host_functions);
|
|
|
|
|
|
#ifdef USE_CUDA
|
|
|
|
void CpuToCudaMemCpy(void* dst, const void* src, size_t num_bytes);
|
|
|
|
void CudaToCpuMemCpy(void* dst, const void* src, size_t num_bytes);
|
|
|
|
const std::unordered_map<OrtDevice::DeviceType, MemCpyFunc>* GetCudaToHostMemCpyFunction();
|
|
|
|
bool IsCudaDeviceIdValid(const onnxruntime::logging::Logger& logger, int id);
|
|
|
|
AllocatorPtr GetCudaAllocator(OrtDevice::DeviceId id);
|
|
|
|
#endif
|
|
|
|
#ifdef USE_ROCM
|
|
|
|
bool IsRocmDeviceIdValid(const onnxruntime::logging::Logger& logger, int id);
|
|
|
|
AllocatorPtr GetRocmAllocator(OrtDevice::DeviceId id);
|
|
|
|
void CpuToRocmMemCpy(void* dst, const void* src, size_t num_bytes);
|
|
|
|
void RocmToCpuMemCpy(void* dst, const void* src, size_t num_bytes);
|
|
|
|
const std::unordered_map<OrtDevice::DeviceType, MemCpyFunc>* GetRocmToHostMemCpyFunction();
|
|
|
|
#endif
|
|
|
|
|
|
void CreateGenericMLValue(const onnxruntime::InputDefList* input_def_list, const AllocatorPtr& alloc,
|
|
const std::string& name_input, py::object& value, OrtValue* p_mlvalue,
|
|
bool accept_only_numpy_array = false, bool use_numpy_data_memory = true, MemCpyFunc mem_cpy_to_device = CpuToCpuMemCpy);
|
|
|
|
void GetPyObjFromTensor(const Tensor& rtensor, py::object& obj,
|
|
const DataTransferManager* data_transfer_manager = nullptr,
|
|
const std::unordered_map<OrtDevice::DeviceType, MemCpyFunc>* mem_cpy_to_host_functions = nullptr);
|
|
|
|
template <class T>
|
|
struct DecRefFn {
|
|
void operator()(T* pyobject) const {
|
|
Py_XDECREF(pyobject);
|
|
}
|
|
};
|
|
|
|
template <class T>
|
|
using UniqueDecRefPtr = std::unique_ptr<T, DecRefFn<T>>;
|
|
|
|
} // namespace python
|
|
} // namespace onnxruntime
|