2020-03-20 03:59:41 +00:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
// Licensed under the MIT License.
|
|
|
|
|
|
2021-05-26 16:47:41 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-03-20 03:59:41 +00:00
|
|
|
#include "core/common/logging/logging.h"
|
|
|
|
|
#include "core/common/logging/sinks/cerr_sink.h"
|
2021-07-27 15:16:47 +00:00
|
|
|
#include "core/common/optional.h"
|
2020-03-20 03:59:41 +00:00
|
|
|
#include "core/framework/allocator.h"
|
|
|
|
|
#include "core/framework/session_options.h"
|
2020-03-23 23:23:34 +00:00
|
|
|
#include "core/session/environment.h"
|
2023-01-05 01:56:29 +00:00
|
|
|
#include "core/session/abi_session_options_impl.h"
|
2020-08-28 20:24:29 +00:00
|
|
|
#include "core/session/inference_session.h"
|
2021-05-21 17:28:09 +00:00
|
|
|
#ifdef ENABLE_TRAINING
|
|
|
|
|
#include "core/dlpack/dlpack_converter.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-11-18 06:58:02 +00:00
|
|
|
#include "onnxruntime_pybind.h" // must use this for the include of <pybind11/pybind11.h>
|
2021-07-22 22:24:36 +00:00
|
|
|
|
2021-05-26 16:47:41 +00:00
|
|
|
// execution provider factory creator headers
|
|
|
|
|
struct OrtStatus {
|
|
|
|
|
OrtErrorCode code;
|
|
|
|
|
char msg[1]; // a null-terminated string
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-22 21:53:40 +00:00
|
|
|
#define BACKEND_DEVICE BACKEND_PROC BACKEND_DNNL BACKEND_OPENVINO BACKEND_TVM BACKEND_OPENBLAS BACKEND_MIGRAPHX BACKEND_ACL BACKEND_ARMNN BACKEND_DML BACKEND_CANN
|
2021-05-26 16:47:41 +00:00
|
|
|
#include "core/session/onnxruntime_cxx_api.h"
|
|
|
|
|
#include "core/providers/providers.h"
|
2022-06-15 21:01:41 +00:00
|
|
|
#include "core/providers/provider_factory_creators.h"
|
2022-02-05 19:15:12 +00:00
|
|
|
#include "core/providers/tensorrt/tensorrt_provider_options.h"
|
2021-05-26 16:47:41 +00:00
|
|
|
|
|
|
|
|
#if defined(USE_CUDA) || defined(USE_ROCM)
|
|
|
|
|
#define BACKEND_PROC "GPU"
|
|
|
|
|
#else
|
|
|
|
|
#define BACKEND_PROC "CPU"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if USE_DNNL
|
|
|
|
|
#define BACKEND_DNNL "-DNNL"
|
|
|
|
|
#else
|
|
|
|
|
#define BACKEND_DNNL ""
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if USE_MIGRAPHX
|
|
|
|
|
#define BACKEND_MIGRAPHX "-MIGRAPHX"
|
|
|
|
|
#else
|
|
|
|
|
#define BACKEND_MIGRAPHX ""
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef USE_OPENVINO
|
|
|
|
|
#if OPENVINO_CONFIG_CPU_FP32
|
|
|
|
|
#define BACKEND_OPENVINO "-OPENVINO_CPU_FP32"
|
|
|
|
|
|
2022-09-22 19:31:40 +00:00
|
|
|
#elif OPENVINO_CONFIG_CPU_FP16
|
|
|
|
|
#define BACKEND_OPENVINO "-OPENVINO_CPU_FP16"
|
|
|
|
|
|
2021-05-26 16:47:41 +00:00
|
|
|
#elif OPENVINO_CONFIG_GPU_FP32
|
|
|
|
|
#define BACKEND_OPENVINO "-OPENVINO_GPU_FP32"
|
|
|
|
|
|
|
|
|
|
#elif OPENVINO_CONFIG_GPU_FP16
|
|
|
|
|
#define BACKEND_OPENVINO "-OPENVINO_GPU_FP16"
|
|
|
|
|
|
2023-04-26 03:59:42 +00:00
|
|
|
#elif OPENVINO_CONFIG_VPUX_FP16
|
|
|
|
|
#define BACKEND_OPENVINO "-OPENVINO_VPUX_FP16"
|
2021-05-26 16:47:41 +00:00
|
|
|
|
2023-04-26 03:59:42 +00:00
|
|
|
#elif OPENVINO_CONFIG_VPUX_U8
|
|
|
|
|
#define BACKEND_OPENVINO "-OPENVINO_VPUX_U8"
|
2021-05-26 16:47:41 +00:00
|
|
|
|
|
|
|
|
#elif OPENVINO_CONFIG_MULTI
|
|
|
|
|
#define BACKEND_OPENVINO "-OPENVINO_MULTI"
|
|
|
|
|
|
2021-11-15 21:41:12 +00:00
|
|
|
#elif OPENVINO_CONFIG_AUTO
|
|
|
|
|
#define BACKEND_OPENVINO "-OPENVINO_AUTO"
|
|
|
|
|
|
2021-05-26 16:47:41 +00:00
|
|
|
#elif OPENVINO_CONFIG_HETERO
|
|
|
|
|
#define BACKEND_OPENVINO "-OPENVINO_HETERO"
|
|
|
|
|
#endif
|
|
|
|
|
#else
|
|
|
|
|
#define BACKEND_OPENVINO ""
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-02-15 09:21:02 +00:00
|
|
|
#ifdef USE_TVM
|
|
|
|
|
#define BACKEND_TVM "-TVM"
|
2021-12-16 00:59:20 +00:00
|
|
|
#else
|
2022-02-15 09:21:02 +00:00
|
|
|
#define BACKEND_TVM ""
|
2021-12-16 00:59:20 +00:00
|
|
|
#endif
|
|
|
|
|
|
2021-05-26 16:47:41 +00:00
|
|
|
#if USE_VITISAI
|
|
|
|
|
#define BACKEND_VITISAI "-VITISAI"
|
|
|
|
|
#include "core/providers/vitisai/vitisai_execution_provider.h"
|
|
|
|
|
#else
|
|
|
|
|
#define BACKEND_VITISAI ""
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if USE_OPENBLAS
|
|
|
|
|
#define BACKEND_OPENBLAS "-OPENBLAS"
|
|
|
|
|
#else
|
|
|
|
|
#define BACKEND_OPENBLAS ""
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if USE_ACL
|
|
|
|
|
#define BACKEND_ACL "-ACL"
|
|
|
|
|
#else
|
|
|
|
|
#define BACKEND_ACL ""
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if USE_ARMNN
|
|
|
|
|
#define BACKEND_ARMNN "-ARMNN"
|
|
|
|
|
#else
|
|
|
|
|
#define BACKEND_ARMNN ""
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if USE_DML
|
|
|
|
|
#define BACKEND_DML "-DML"
|
|
|
|
|
#else
|
|
|
|
|
#define BACKEND_DML ""
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-09-22 21:53:40 +00:00
|
|
|
#if USE_CANN
|
|
|
|
|
#define BACKEND_CANN "-CANN"
|
|
|
|
|
#else
|
|
|
|
|
#define BACKEND_CANN ""
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-05-26 16:47:41 +00:00
|
|
|
#ifdef USE_CUDA
|
|
|
|
|
#include "core/providers/cuda/cuda_provider_factory.h"
|
|
|
|
|
#include "core/providers/cuda/cuda_execution_provider_info.h"
|
|
|
|
|
#endif
|
2021-10-14 22:15:51 +00:00
|
|
|
#ifdef USE_ROCM
|
|
|
|
|
#include "core/providers/rocm/rocm_provider_factory.h"
|
|
|
|
|
#include "core/providers/rocm/rocm_execution_provider_info.h"
|
|
|
|
|
#endif
|
2021-05-26 16:47:41 +00:00
|
|
|
#ifdef USE_TENSORRT
|
|
|
|
|
#include "core/providers/tensorrt/tensorrt_provider_factory.h"
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef USE_MIGRAPHX
|
|
|
|
|
#include "core/providers/migraphx/migraphx_provider_factory.h"
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef USE_OPENVINO
|
|
|
|
|
#include "core/providers/openvino/openvino_provider_factory.h"
|
|
|
|
|
// TODO remove deprecated global config
|
2020-03-20 03:59:41 +00:00
|
|
|
namespace onnxruntime {
|
2021-05-26 16:47:41 +00:00
|
|
|
ProviderInfo_OpenVINO* GetProviderInfo_OpenVINO();
|
2020-03-20 03:59:41 +00:00
|
|
|
namespace python {
|
2021-05-26 16:47:41 +00:00
|
|
|
extern std::string openvino_device_type;
|
|
|
|
|
}
|
training with custom autograd Functions (#7513)
* Register Torch Custom autograd.Function
* Add flag to supress pybind11 warning
* Avoid unnecessary include in cmake
* Add missing reference
* Add getter for registerred functions
* Format for making subsquent changes cleaner
* Fix interop feature build failure
* Forward pass, run PyOP on CPU EP
* clean up the code
* Fix build
* Define new ops
* refactor pyop - extract PyOpLibProxy class
* Hacks to run example
* implement the kernel compute func
* add back PyOP for comparision experiments
* debug info - thread id
* refine the kernels
* Polish code
(cherry picked from commit 4ed606f9a0833592b325a4b40cf917e219845f6f)
* Fix a the Tensor address mismatch in C++ side
* PythonOpGrad compute
* add distributed test case
* refine test cases
* get dist.get_rank() in Autograd forward pass
* Add CUDA kernels
* Store float, int, and tuple of them as PythonOp's attributes
* Populate local changes
* Fix bugs
* PythonOp/PythonOpGrad CUDA kernels
* Support non-tensor inputs
* Single GPU FP16 Run Pass
(cherry picked from commit e539989e91e18ee997900292d3493b97d3eafa8a)
* Fix segement
* add basic test cases
* Save progress
* fix gradient builder for a Add op who have same inputs
* add test cases for auto grad fallback feature
* fix ref cnt issue. add thread id for debugging
* POC: remove interface class
* Remove interface classes
* Clean a bit
* Coarse-grained clean up after rebase master
* reset pyop and language_interop_ops to latest master
* Fix missing part during merge
* re-structure torch related language interop files
* Fix build
* Fix tests and build
* Fix build and basic unit tests
* Fix most of uts
* remove unnecessary import
* clean up and fix build when enabling language_interop_ops
* Fix single-GPU UTs
* Move runner register into ORT package
* Update dist UTs to new style
* Also fix distributed UTs and leaf gradient problem
* Static generation for constant args
* Move arg_positions_ to static field
* Rename some functions
* Move arg ceration into a function
* Clean output logic in PythonOp
* Move PythonOp's ctor
* Revise PythonOpGrad
* Fix "ORT only supports contiguous tensor for now" for inputs
* Fix evaulation mode error, add test & clean up
* clean up codes
* Fix issues introduced by recent master change (enabled symbolic shape infer)
* automatically register forward/backward function pointers && clean up
* Fix multi-output case
* Add a test back
* fix build and clean up
* RAII for function params PyObject
* Use new exporter
* Clean full name in new exporter
* Fix UTs
* Format a file
* Add "inplace" back
Remove a legacy comment
* Refine TorchProxy
1. Make TorchProxy a formal singleton class.
2. Remove unused Scope class.
3. Simplify the call to Forward and Backward. The two functions now
automatically acquire and release GIL state, so user doesn't need
any GIL-related calls.
* Format
* Add lock to avoid racing condition when registering Python objs
* Fix Python call param ref issues && Add RefcountTracker for debug build && Clean up
* clean up print
* Resolve part of comments && clean up
* Fix a potential bug
* track pyobject consistently
* move kernels to cpu provider as base class
* Refactor - 1. Extract PythonOpBase/PythonOpGradBase 2. Implement CPU kernels 3. Test coverage for CPU kernels
* Refine register code
* Add a missing macro
* Release python call result objects with PythonObjectPtr && Add UnRegisterContext && Track PyObject for Debugging && Clena up
* Fix random segfault issue - relasing a wrong ctx pointer for inplace cases
* put ref count in debug macro
* Move GIL out
* Refine tests
* Fix memory leak issue && forward output lifecycle issue:
1. Unregister the OrtValue PythonObject. Currently, the OrtValue shared same buffer with PythonOp/PythonOpGrad's output. So after those kernels outputs are released, the "leaked" OrtValue caused the shared buffer cannot be released.
2. According PyTorch forward+backward execution. The forward outputs (e.g. torch tensors) maintains the context/saved variables/dirty inputs, etc, which are used for backward execution, so its life should be after the backward runs. This change added such a depencencies between PythonOpGrad on PythonOp.
* Move dlpack->ortvalue into C++ to avoid temp object registration
* Fix the over released Py_False/Py_True && refine tests
* Clean up unused functions
* Always assume the first forward output is context so we don't need to test unused cases.
* Fix a memory leak
* move-copy unique_ptr & avoid C-style casting
* Use inplace attribute to determine if input tensors are copied
* Move DlpackCapsuleDestructor's to a common place
* Thread-safe TorchProxy
* Use OrtValue instead of OrtValue*
* Only keep checks for Debug build
* Wrap some long line per comment
* onnx_export_type --> kwargs
* Use requires_grads to create PythonOpGrad's inputs
* add missing files during master merge
* Fix build issue after merge
* Address two comments.
1. Internalize DlpackCapsuleDestructor
2. Change "(" to "]" for describing closed interval.
* Address some comments.
1. "override" -> "overwrite" to avoid using reserved keyword.
2. Call DLPack's helper to create OrtValue for avoiding repeated code.
* Address comments.
1. Pass std::mutex to registeration helpers so their callers don't
have to lock the mutex expclicitly.
2. Rename "func_context_pool_mutex_" to "mutex_". This mutex is the global mutex for OrtTorchFunctionPool.
* Add bridging code to make cuda kernels work with merged master
* put debue macro check within RefCountTracker && use default logger for debug info && remove useless ortvalue_ptr interface && typos && revert unncessary blank line changes
* fix some comments
* Resolve more comments
* Capitalize a word
* use unique_ptr instead of ObjectPointer for PyObject management && add converntion
* Support symbolic shape
* Remove unused variable
* fix build
* Enable function registration for training only && rectify ToDlpack/FromDlpack merge with master.
* Don't add context for non-PythonOp opeartors (for example AtenOp)
* Fix build error
* Polish frontend part.
1. Avoid adding kwargs to ORTModule's ctor
2. Use onnx_export_type rather than kwargs for type safty
3. Fix some build bugs.
* Resolve simpler comments
* Resolve export related comments
* sync master && fix tests && fix non-training build error
* Fix build errors
* add target link lib
* windows build error
* Fix orttraining-linux-ci build
* disable autograd test && clean up
* fix linux orttraining ci build
* try fixing win build error
* Revise append calls in runner
* Enable custom function using a function
* Rename to avoid using reservied keyword
* Use list comprehension
* Set ORT random seed in tests
* Remove print code and fix ctx shape
* [] -> list()
* Move autograd.Function and nn.Module into corresponding functions
* Move test helpers
* Polish dist test a bit. Tried move helpers to helper file but it causes a deadlock.
* trying fix undefined reference
* Context is not managed by global pool
* Polish dist test
* Polish dist test
* Add enable_custom_autograd_function
* Remove enable_custom_autograd_function from ctors
* Add doc strings
* Shorter code
* Address comments
* Add one empty line
* revert a minor and not needed change
* Address comments
* Back to reference
* Fix windows builds
* Fix windows debug build fail to find "'python39_d.lib'"
* fix mac build error
* revert _to_contiguous change
* add debugging tag for orttraining-cpu-ci
* Fix the wrong PYTHON_LIBRARIES which is affected by PYTHON_LIBRARY given in build command
* add debugging info
* Fix the build in this case: PYTHON_LIBDIR: /opt/_internal/cpython-3.7.10/lib, PYTHON_EXECUTABLE: /opt/python/cp37-cp37m/bin/python3, PYTHON_MULTIARCH: x86_64-linux-gnu
PYTHON_LIBRARY_PATH python3.7m
* fix build error due to python lib not found
* Fixes
1. Release PyObject's
2. Not useing deepcopy because we assume autograd.Function's
non-tensor inputs are static (constants) so there should
be no side effect after calling any autograd.Function
multiple times.
* Revert dtoc for decreasing refcnt
* add debugging log
* add debugging tag
* Fix a small leak
* Remove ONNX_FALLTHROUGH flag
* debug tag
* debug tag
* fix builds
* remove debug tag
* fix build
* fix builds
* fix build
* install python3 in centos, in case there is no libpython3.xm.so
* build python so for redhat
* add training cpu specific docker, build python so inside
* revert build-cpython change
* try fixing numpy include issue
* install_deps after re-installing cpython
* fix build && remove debug tag
* install openssl before cpython
* let's say: builds pass!
* add build flag for torch iterop, only enable it when training+Python is enabled
* skip ComputeBroadcastBackwardAxesDynamic for the shared inputs
* fix build
* add debug info for padgrad test
* Fix builds
* Split dlpack_converter into C++ and Python interfaces respecitively. Then different build use them as needed.
* clean up the changes
* fix addsubgradient builder
* Fix builds
* clean up
* clean up
* Address some comments.
1. Use pointer wraper to avoid calling Py_DECREF
2. Remove unregister_* functions
3. Allow repeated registration by skipping those with existing keys
4. Unregister context in PythonOpGrad
* Fix over-released Py_Boolean
Co-authored-by: Wei-Sheng Chin <wschin@outlook.com>
2021-06-07 20:01:21 +00:00
|
|
|
} // namespace onnxruntime
|
2021-05-26 16:47:41 +00:00
|
|
|
#endif
|
2022-02-15 09:21:02 +00:00
|
|
|
#ifdef USE_TVM
|
2022-03-16 12:55:04 +00:00
|
|
|
#include "core/providers/tvm/tvm_ep_options.h"
|
2021-12-16 00:59:20 +00:00
|
|
|
#endif
|
2021-05-26 16:47:41 +00:00
|
|
|
#ifdef USE_ACL
|
|
|
|
|
#include "core/providers/acl/acl_provider_factory.h"
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef USE_ARMNN
|
|
|
|
|
#include "core/providers/armnn/armnn_provider_factory.h"
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef USE_DML
|
|
|
|
|
#include "core/providers/dml/dml_provider_factory.h"
|
|
|
|
|
#endif
|
2022-09-22 21:53:40 +00:00
|
|
|
#ifdef USE_CANN
|
|
|
|
|
#include "core/providers/cann/cann_provider_factory.h"
|
|
|
|
|
#include "core/providers/cann/cann_execution_provider_info.h"
|
|
|
|
|
#endif
|
2021-05-26 16:47:41 +00:00
|
|
|
|
|
|
|
|
#ifdef USE_CUDA
|
|
|
|
|
namespace onnxruntime {
|
2021-06-25 09:26:50 +00:00
|
|
|
ProviderInfo_CUDA* TryGetProviderInfo_CUDA();
|
|
|
|
|
ProviderInfo_CUDA& GetProviderInfo_CUDA();
|
2021-05-26 16:47:41 +00:00
|
|
|
namespace python {
|
|
|
|
|
// TODO remove deprecated global config
|
|
|
|
|
extern OrtCudnnConvAlgoSearch cudnn_conv_algo_search;
|
|
|
|
|
// TODO remove deprecated global config
|
|
|
|
|
extern bool do_copy_in_default_stream;
|
2022-11-15 06:43:54 +00:00
|
|
|
// TODO remove deprecated global config
|
|
|
|
|
extern onnxruntime::cuda::TunableOpInfo tunable_op;
|
2021-05-26 16:47:41 +00:00
|
|
|
extern onnxruntime::CUDAExecutionProviderExternalAllocatorInfo external_allocator_info;
|
2021-10-14 22:15:51 +00:00
|
|
|
extern onnxruntime::ArenaExtendStrategy arena_extend_strategy;
|
2021-05-26 16:47:41 +00:00
|
|
|
} // namespace python
|
|
|
|
|
} // namespace onnxruntime
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-09-22 21:53:40 +00:00
|
|
|
#ifdef USE_CANN
|
|
|
|
|
namespace onnxruntime {
|
|
|
|
|
ProviderInfo_CANN* TryGetProviderInfo_CANN();
|
|
|
|
|
ProviderInfo_CANN& GetProviderInfo_CANN();
|
|
|
|
|
} // namespace onnxruntime
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-05-26 16:47:41 +00:00
|
|
|
#ifdef USE_ROCM
|
|
|
|
|
namespace onnxruntime {
|
2021-10-14 22:15:51 +00:00
|
|
|
ProviderInfo_ROCM* TryGetProviderInfo_ROCM();
|
|
|
|
|
ProviderInfo_ROCM& GetProviderInfo_ROCM();
|
2021-05-26 16:47:41 +00:00
|
|
|
namespace python {
|
|
|
|
|
// TODO remove deprecated global config
|
2021-10-14 22:15:51 +00:00
|
|
|
extern bool miopen_conv_exhaustive_search;
|
|
|
|
|
// TODO remove deprecated global config
|
|
|
|
|
extern bool do_copy_in_default_stream;
|
2022-10-20 05:35:08 +00:00
|
|
|
// TODO remove deprecated global config
|
|
|
|
|
extern onnxruntime::rocm::TunableOpInfo tunable_op;
|
2021-10-14 22:15:51 +00:00
|
|
|
extern onnxruntime::ROCMExecutionProviderExternalAllocatorInfo external_allocator_info;
|
2021-05-26 16:47:41 +00:00
|
|
|
extern onnxruntime::ArenaExtendStrategy arena_extend_strategy;
|
2021-10-14 22:15:51 +00:00
|
|
|
} // namespace python
|
2021-05-26 16:47:41 +00:00
|
|
|
} // namespace onnxruntime
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "core/providers/dnnl/dnnl_provider_factory.h"
|
|
|
|
|
#include "core/providers/shared_library/provider_host_api.h"
|
|
|
|
|
|
|
|
|
|
namespace onnxruntime {
|
2021-12-02 20:56:51 +00:00
|
|
|
#if !defined(SHARED_PROVIDER) && !defined(DISABLE_SPARSE_TENSORS)
|
2021-07-22 22:24:36 +00:00
|
|
|
class SparseTensor;
|
|
|
|
|
#endif
|
2021-05-26 16:47:41 +00:00
|
|
|
namespace python {
|
|
|
|
|
|
2021-11-18 06:58:02 +00:00
|
|
|
using ExecutionProviderRegistrationFn = std::function<void(InferenceSession*,
|
2021-08-27 23:23:35 +00:00
|
|
|
const std::vector<std::string>&,
|
|
|
|
|
const ProviderOptionsMap&)>;
|
|
|
|
|
|
2021-05-26 16:47:41 +00:00
|
|
|
// TODO remove deprecated global config
|
|
|
|
|
extern OrtDevice::DeviceId cuda_device_id;
|
|
|
|
|
// TODO remove deprecated global config
|
|
|
|
|
extern size_t gpu_mem_limit;
|
|
|
|
|
|
2023-01-05 01:56:29 +00:00
|
|
|
using PySessionOptions = OrtSessionOptions;
|
2020-08-28 20:24:29 +00:00
|
|
|
|
|
|
|
|
// Thin wrapper over internal C++ InferenceSession to accommodate custom op library management for the Python user
|
|
|
|
|
struct PyInferenceSession {
|
2023-03-10 21:55:31 +00:00
|
|
|
PyInferenceSession(std::shared_ptr<Environment> env, const PySessionOptions& so)
|
2023-04-18 16:26:58 +00:00
|
|
|
: env_(std::move(env)) {
|
2023-03-10 21:55:31 +00:00
|
|
|
sess_ = std::make_unique<InferenceSession>(so.value, *env_);
|
2020-09-03 16:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if !defined(ORT_MINIMAL_BUILD)
|
2023-04-18 16:26:58 +00:00
|
|
|
PyInferenceSession(std::shared_ptr<Environment> env, const PySessionOptions& so, const std::string& arg, bool is_arg_file_name)
|
|
|
|
|
: env_(std::move(env)) {
|
2020-08-28 20:24:29 +00:00
|
|
|
if (is_arg_file_name) {
|
|
|
|
|
// Given arg is the file path. Invoke the corresponding ctor().
|
2023-03-10 21:55:31 +00:00
|
|
|
sess_ = std::make_unique<InferenceSession>(so.value, *env_, arg);
|
2020-08-28 20:24:29 +00:00
|
|
|
} else {
|
|
|
|
|
// Given arg is the model content as bytes. Invoke the corresponding ctor().
|
|
|
|
|
std::istringstream buffer(arg);
|
2023-03-10 21:55:31 +00:00
|
|
|
sess_ = std::make_unique<InferenceSession>(so.value, *env_, buffer);
|
2020-08-28 20:24:29 +00:00
|
|
|
}
|
|
|
|
|
}
|
2020-09-03 16:10:48 +00:00
|
|
|
#endif
|
2020-08-28 20:24:29 +00:00
|
|
|
|
|
|
|
|
InferenceSession* GetSessionHandle() const { return sess_.get(); }
|
|
|
|
|
|
2023-03-10 21:55:31 +00:00
|
|
|
virtual ~PyInferenceSession() = default;
|
2020-08-28 20:24:29 +00:00
|
|
|
|
|
|
|
|
protected:
|
2023-04-18 16:26:58 +00:00
|
|
|
PyInferenceSession(std::shared_ptr<Environment> env, std::unique_ptr<InferenceSession> sess)
|
|
|
|
|
: env_(std::move(env)), sess_(std::move(sess)) {
|
2020-09-02 00:10:36 +00:00
|
|
|
}
|
|
|
|
|
|
2020-09-03 16:10:48 +00:00
|
|
|
private:
|
2023-03-10 21:55:31 +00:00
|
|
|
std::shared_ptr<Environment> env_;
|
2020-08-28 20:24:29 +00:00
|
|
|
std::unique_ptr<InferenceSession> sess_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
inline const PySessionOptions& GetDefaultCPUSessionOptions() {
|
|
|
|
|
static PySessionOptions so;
|
2020-03-20 03:59:41 +00:00
|
|
|
return so;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline AllocatorPtr& GetAllocator() {
|
2021-12-08 01:56:58 +00:00
|
|
|
static AllocatorPtr alloc = std::make_shared<CPUAllocator>();
|
2020-03-20 03:59:41 +00:00
|
|
|
return alloc;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-02 20:56:51 +00:00
|
|
|
#if !defined(DISABLE_SPARSE_TENSORS)
|
2021-07-22 22:24:36 +00:00
|
|
|
// This class exposes SparseTensor to Python
|
|
|
|
|
// The class serves two major purposes
|
|
|
|
|
// - to be able to map numpy arrays memory and use it on input, this serves as a reference holder
|
2021-10-27 18:28:37 +00:00
|
|
|
// so incoming arrays do not disappear. To this end we create an instance of SparseTensor
|
|
|
|
|
// on top of the user provided numpy arrays and create a duplicate of py::objects for those
|
|
|
|
|
// numpy array for ref-counting purposes and store it here.
|
2021-11-18 06:58:02 +00:00
|
|
|
//
|
2021-10-27 18:28:37 +00:00
|
|
|
// - to be able to expose SparseTensor returned from run method. We get an OrtValue from run()
|
|
|
|
|
// and store a copy of it in ort_value_. The OrtValue shared_ptr ref-counting will make sure
|
|
|
|
|
// the memory stays around.
|
|
|
|
|
//
|
|
|
|
|
// An object of the class must never have both instance_ and ort_value_ have data at the same time.
|
2021-07-22 22:24:36 +00:00
|
|
|
class PySparseTensor {
|
|
|
|
|
public:
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Use this constructor when you created a SparseTensor instance which is backed
|
|
|
|
|
/// by python array storage and it important that they stay alive while this object is
|
|
|
|
|
/// alive
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="instance">a fully constructed and populated instance of SparseTensor</param>
|
|
|
|
|
/// <param name="storage">a collection reference guards</param>
|
|
|
|
|
PySparseTensor(std::unique_ptr<SparseTensor>&& instance,
|
|
|
|
|
std::vector<pybind11::object>&& storage)
|
2021-10-27 18:28:37 +00:00
|
|
|
: instance_(std::move(instance)), backing_storage_(std::move(storage)), ort_value_() {
|
2021-07-22 22:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Same as above but no backing storage as SparseTensor owns the memory
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="instance"></param>
|
|
|
|
|
explicit PySparseTensor(std::unique_ptr<SparseTensor>&& instance)
|
2021-10-27 18:28:37 +00:00
|
|
|
: instance_(std::move(instance)), backing_storage_(), ort_value_() {
|
2021-07-22 22:24:36 +00:00
|
|
|
}
|
|
|
|
|
|
2021-10-27 18:28:37 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Edge case when we can not copy memory on GPU and therefore
|
|
|
|
|
/// can not own it.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ort_value"></param>
|
2021-07-22 22:24:36 +00:00
|
|
|
explicit PySparseTensor(const OrtValue& ort_value)
|
2021-10-27 18:28:37 +00:00
|
|
|
: instance_(), backing_storage_(), ort_value_(ort_value) {}
|
2021-07-22 22:24:36 +00:00
|
|
|
|
|
|
|
|
PySparseTensor(const PySparseTensor&) = delete;
|
|
|
|
|
PySparseTensor& operator=(const PySparseTensor&) = delete;
|
|
|
|
|
|
|
|
|
|
PySparseTensor(PySparseTensor&& o) noexcept {
|
|
|
|
|
*this = std::move(o);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PySparseTensor& operator=(PySparseTensor&& o) noexcept {
|
2021-10-27 18:28:37 +00:00
|
|
|
instance_ = std::move(o.instance_);
|
2021-07-22 22:24:36 +00:00
|
|
|
backing_storage_ = std::move(o.backing_storage_);
|
2021-10-27 18:28:37 +00:00
|
|
|
ort_value_ = std::move(o.ort_value_);
|
2021-07-22 22:24:36 +00:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~PySparseTensor();
|
|
|
|
|
|
|
|
|
|
const SparseTensor& Instance() const {
|
2021-10-27 18:28:37 +00:00
|
|
|
if (instance_) {
|
|
|
|
|
return *instance_;
|
|
|
|
|
}
|
2021-07-22 22:24:36 +00:00
|
|
|
return ort_value_.Get<SparseTensor>();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 18:28:37 +00:00
|
|
|
std::unique_ptr<OrtValue> AsOrtValue() const;
|
2021-07-22 22:24:36 +00:00
|
|
|
|
|
|
|
|
private:
|
2021-12-20 04:54:29 +00:00
|
|
|
// instance_ represents data that comes as input. Thus we depend on numpy
|
2022-06-15 21:01:41 +00:00
|
|
|
// arrays that own the underlying memory to stay around. We store copies
|
|
|
|
|
// of py::objects for those arrays in backing_storage_ as an extra ref-count.
|
2021-10-27 18:28:37 +00:00
|
|
|
|
|
|
|
|
// If we have and are able to copy from the OrtValue returned by run() to CPU, then this owns the data
|
|
|
|
|
// and backing_storage_ is empty.
|
|
|
|
|
std::unique_ptr<SparseTensor> instance_;
|
2021-07-22 22:24:36 +00:00
|
|
|
std::vector<pybind11::object> backing_storage_;
|
2021-10-27 18:28:37 +00:00
|
|
|
|
|
|
|
|
// We create a copy of OrtValue when we obtain it from a run method.
|
2021-07-22 22:24:36 +00:00
|
|
|
OrtValue ort_value_;
|
|
|
|
|
};
|
2021-12-02 20:56:51 +00:00
|
|
|
#endif // !defined(DISABLE_SPARSE_TENSORS)
|
2021-07-22 22:24:36 +00:00
|
|
|
|
2021-12-20 04:54:29 +00:00
|
|
|
#if defined(_MSC_VER) && !defined(__clang__)
|
|
|
|
|
#pragma warning(push)
|
2022-06-15 21:01:41 +00:00
|
|
|
// You can attempt to make 'onnxruntime::python::SessionObjectInitializer::Get' constexpr
|
2021-12-20 04:54:29 +00:00
|
|
|
#pragma warning(disable : 26497)
|
|
|
|
|
#endif
|
2020-03-20 03:59:41 +00:00
|
|
|
class SessionObjectInitializer {
|
|
|
|
|
public:
|
2020-08-28 20:24:29 +00:00
|
|
|
typedef const PySessionOptions& Arg1;
|
2020-03-23 23:23:34 +00:00
|
|
|
// typedef logging::LoggingManager* Arg2;
|
|
|
|
|
static const std::string default_logger_id;
|
2020-03-20 03:59:41 +00:00
|
|
|
operator Arg1() {
|
|
|
|
|
return GetDefaultCPUSessionOptions();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-23 23:23:34 +00:00
|
|
|
// operator Arg2() {
|
|
|
|
|
// static LoggingManager default_logging_manager{std::unique_ptr<ISink>{new CErrSink{}},
|
|
|
|
|
// Severity::kWARNING, false, LoggingManager::InstanceType::Default,
|
|
|
|
|
// &default_logger_id};
|
|
|
|
|
// return &default_logging_manager;
|
|
|
|
|
// }
|
2020-03-20 03:59:41 +00:00
|
|
|
|
|
|
|
|
static SessionObjectInitializer Get() {
|
|
|
|
|
return SessionObjectInitializer();
|
|
|
|
|
}
|
|
|
|
|
};
|
2021-12-20 04:54:29 +00:00
|
|
|
#if defined(_MSC_VER) && !defined(__clang__)
|
|
|
|
|
#pragma warning(pop)
|
|
|
|
|
#endif
|
2023-03-10 21:55:31 +00:00
|
|
|
std::shared_ptr<Environment> GetEnv();
|
2020-03-23 23:23:34 +00:00
|
|
|
|
2020-09-03 16:10:48 +00:00
|
|
|
// Initialize an InferenceSession.
|
|
|
|
|
// Any provider_options should have entries in matching order to provider_types.
|
|
|
|
|
void InitializeSession(InferenceSession* sess,
|
2021-08-27 23:23:35 +00:00
|
|
|
ExecutionProviderRegistrationFn ep_registration_fn,
|
2020-09-03 16:10:48 +00:00
|
|
|
const std::vector<std::string>& provider_types = {},
|
2021-03-29 08:39:48 +00:00
|
|
|
const ProviderOptionsVector& provider_options = {},
|
|
|
|
|
const std::unordered_set<std::string>& disabled_optimizer_names = {});
|
2020-06-26 19:02:14 +00:00
|
|
|
|
2020-12-09 02:20:51 +00:00
|
|
|
// Checks if PyErrOccured, fetches status and throws.
|
|
|
|
|
void ThrowIfPyErrOccured();
|
|
|
|
|
|
2021-05-26 16:47:41 +00:00
|
|
|
void addOrtValueMethods(pybind11::module& m);
|
|
|
|
|
|
|
|
|
|
void addIoBindingMethods(pybind11::module& m);
|
|
|
|
|
|
2021-07-22 22:24:36 +00:00
|
|
|
void addSparseTensorMethods(pybind11::module& m);
|
|
|
|
|
|
|
|
|
|
void addGlobalSchemaFunctions(pybind11::module& m);
|
|
|
|
|
|
|
|
|
|
void addOpKernelSubmodule(pybind11::module& m);
|
|
|
|
|
|
|
|
|
|
void addOpSchemaSubmodule(pybind11::module& m);
|
|
|
|
|
|
2021-05-26 16:47:41 +00:00
|
|
|
const char* GetDeviceName(const OrtDevice& device);
|
|
|
|
|
|
|
|
|
|
bool IsCudaDeviceIdValid(const onnxruntime::logging::Logger& logger, int id);
|
|
|
|
|
|
|
|
|
|
AllocatorPtr GetCudaAllocator(OrtDevice::DeviceId id);
|
|
|
|
|
|
|
|
|
|
bool CheckIfTensor(const std::vector<const NodeArg*>& def_list,
|
|
|
|
|
const std::string& name,
|
|
|
|
|
/*out*/ ONNX_NAMESPACE::TypeProto& type_proto);
|
|
|
|
|
|
2021-07-09 03:47:11 +00:00
|
|
|
#ifdef ENABLE_TRAINING
|
|
|
|
|
|
|
|
|
|
// Allocate a new Capsule object, which takes the ownership of OrtValue.
|
|
|
|
|
// Caller is responsible for releasing.
|
|
|
|
|
// This function calls OrtValueToDlpack(...).
|
|
|
|
|
PyObject* ToDlpack(OrtValue ort_value);
|
|
|
|
|
|
|
|
|
|
// Consume a Capsule object and claims the ownership of its underlying tensor to
|
|
|
|
|
// create a OrtValue. This function calls DlpackToOrtValue(...) to do the conversion.
|
|
|
|
|
OrtValue FromDlpack(PyObject* dlpack_tensor, const bool is_bool_tensor);
|
|
|
|
|
|
2022-04-06 07:12:58 +00:00
|
|
|
// Destructor for Capsule object holding a DLPack structure.
|
|
|
|
|
void DlpackCapsuleDestructor(PyObject* data);
|
|
|
|
|
|
2021-07-09 03:47:11 +00:00
|
|
|
#endif
|
|
|
|
|
|
2020-08-28 20:24:29 +00:00
|
|
|
} // namespace python
|
2021-07-22 22:24:36 +00:00
|
|
|
|
2022-09-07 22:11:18 +00:00
|
|
|
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_Tensorrt(const OrtTensorRTProviderOptions* params);
|
|
|
|
|
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_Tensorrt(const OrtTensorRTProviderOptionsV2* params);
|
|
|
|
|
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_Tensorrt(int device_id);
|
|
|
|
|
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_MIGraphX(const OrtMIGraphXProviderOptions* params);
|
|
|
|
|
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_MIGraphX(int device_id);
|
|
|
|
|
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_Cuda(const OrtCUDAProviderOptions* params);
|
2023-01-31 22:37:13 +00:00
|
|
|
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_Dnnl(const OrtDnnlProviderOptions* params);
|
2022-09-07 22:11:18 +00:00
|
|
|
#ifdef USE_TVM
|
|
|
|
|
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_Tvm(const tvm::TvmEPOptions& info);
|
|
|
|
|
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_Tvm(const char* params);
|
|
|
|
|
#endif
|
|
|
|
|
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_VITISAI(const char* backend_type, int device_id,
|
|
|
|
|
const char* export_runtime_module,
|
|
|
|
|
const char* load_runtime_module);
|
|
|
|
|
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_ACL(int use_arena);
|
|
|
|
|
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_ArmNN(int use_arena);
|
|
|
|
|
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_DML(int device_id);
|
|
|
|
|
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_Nnapi(
|
|
|
|
|
uint32_t flags, const optional<std::string>& partitioning_stop_ops_list);
|
|
|
|
|
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_Rknpu();
|
|
|
|
|
std::shared_ptr<IExecutionProviderFactory> CreateExecutionProviderFactory_CoreML(uint32_t flags);
|
2021-11-18 06:58:02 +00:00
|
|
|
constexpr const char* kDefaultExecutionProviderEntry = "GetProvider";
|
2020-08-28 20:24:29 +00:00
|
|
|
} // namespace onnxruntime
|