Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't create the default allocator every single time. Rename API accordingly. Expose Session/Run log severity levels. #1615

Merged
merged 21 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b03dc6f
Mention OrtCreateSessionFromArray in C API doc
pranavsharma Jul 22, 2019
cc5b316
Merge branch 'master' of https://github.com/Microsoft/onnxruntime
pranavsharma Jul 23, 2019
4ef6284
Merge branch 'master' of https://github.com/Microsoft/onnxruntime
pranavsharma Jul 25, 2019
85bd0dc
Merge branch 'master' of https://github.com/Microsoft/onnxruntime
pranavsharma Jul 25, 2019
42aa76c
Merge branch 'master' of https://github.com/Microsoft/onnxruntime
pranavsharma Jul 31, 2019
27ac7f1
Merge branch 'master' of https://github.com/Microsoft/onnxruntime
pranavsharma Aug 7, 2019
8fdbd2e
Merge branch 'master' of https://github.com/Microsoft/onnxruntime
pranavsharma Aug 8, 2019
704934b
Merge branch 'master' of https://github.com/Microsoft/onnxruntime
pranavsharma Aug 13, 2019
10f46b4
Merge branch 'master' of https://github.com/Microsoft/onnxruntime
pranavsharma Aug 14, 2019
dcd6982
Don't create the default allocator every single time. Rename API acco…
pranavsharma Aug 14, 2019
723ded7
Don't create the default allocator every single time. Rename API acco…
pranavsharma Aug 14, 2019
a2c7ae3
Merge remote-tracking branch 'origin/master' into default_allocator
pranavsharma Aug 15, 2019
d07e8ff
Merge branch 'default_allocator' of https://github.com/Microsoft/onnx…
pranavsharma Aug 15, 2019
fe09f6d
Merge remote-tracking branch 'origin/master' into default_allocator
pranavsharma Aug 15, 2019
1f0a904
updates...
pranavsharma Aug 15, 2019
003fbd4
updates...
pranavsharma Aug 15, 2019
06c0af6
Merge branch 'default_allocator' of https://github.com/Microsoft/onnx…
pranavsharma Aug 16, 2019
12e2034
PR comments
pranavsharma Aug 17, 2019
0473368
Merge remote-tracking branch 'origin/master' into default_allocator
pranavsharma Aug 17, 2019
b999b82
fix typo in license header
pranavsharma Aug 17, 2019
58863c4
fix build
pranavsharma Aug 20, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,8 @@ internal static DisposableNamedOnnxValue CreateTensorFromOnnxValue(string name,
internal static DisposableNamedOnnxValue CreateFromOnnxValue(string name, IntPtr nativeOnnxValue)
{
IntPtr allocator = IntPtr.Zero;
NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateDefaultAllocator(out allocator));
NativeApiStatus.VerifySuccess(NativeMethods.OrtGetDefaultAllocator(out allocator));
var ret = CreateFromOnnxValue(name, nativeOnnxValue, allocator);
NativeMethods.OrtReleaseAllocator(allocator);
return (DisposableNamedOnnxValue)ret;
}

Expand Down
19 changes: 4 additions & 15 deletions csharp/src/Microsoft.ML.OnnxRuntime/NativeMemoryAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,18 @@ protected override bool ReleaseHandle()

internal class NativeMemoryAllocator : SafeHandle
{
protected static readonly Lazy<NativeMemoryAllocator> _defaultInstance = new Lazy<NativeMemoryAllocator>(CreateDefaultCpuAllocator);
protected static readonly Lazy<NativeMemoryAllocator> _defaultInstance = new Lazy<NativeMemoryAllocator>(GetDefaultCpuAllocator);

private static NativeMemoryAllocator CreateDefaultCpuAllocator()
private static NativeMemoryAllocator GetDefaultCpuAllocator()
{
IntPtr allocator = IntPtr.Zero;
try
{
IntPtr status = NativeMethods.OrtCreateDefaultAllocator(out allocator);
IntPtr status = NativeMethods.OrtGetDefaultAllocator(out allocator);
NativeApiStatus.VerifySuccess(status);
}
catch (Exception e)
{
if (allocator != IntPtr.Zero)
{
Delete(allocator);
}
throw e;
}

Expand Down Expand Up @@ -124,7 +120,7 @@ public override bool IsInvalid
}
}

internal IntPtr Handle
internal IntPtr Handle
{
get
{
Expand All @@ -138,15 +134,8 @@ protected NativeMemoryAllocator(IntPtr allocator)
this.handle = allocator;
}


protected static void Delete(IntPtr allocator)
{
NativeMethods.OrtReleaseAllocator(allocator);
}

protected override bool ReleaseHandle()
{
Delete(this.handle);
return true;
}
}
Expand Down
5 changes: 1 addition & 4 deletions csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,7 @@ public enum MemoryType
public static extern void OrtReleaseAllocatorInfo(IntPtr /*(OrtAllocatorInfo*)*/ allocatorInfo);

[DllImport(nativeLib, CharSet = charSet)]
public static extern IntPtr /*(OrtStatus*)*/OrtCreateDefaultAllocator(out IntPtr /*(OrtAllocator**)*/ allocator);

[DllImport(nativeLib, CharSet = charSet)]
public static extern void OrtReleaseAllocator(IntPtr /*(OrtAllocator*)*/ allocator);
public static extern IntPtr /*(OrtStatus*)*/OrtGetDefaultAllocator(out IntPtr /*(OrtAllocator**)*/ allocator);

/// <summary>
/// Release any object allocated by an allocator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main(int argc, char* argv[]) {

//*************************************************************************
// print model input layer (node names, types, shape etc.)
Ort::Allocator allocator = Ort::Allocator::CreateDefault();
Ort::Allocator allocator = Ort::Allocator::GetDefault();

// print number of model input nodes
size_t num_input_nodes = session.GetInputCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ int main(int argc, char* argv[]) {
size_t num_input_nodes;
OrtStatus* status;
OrtAllocator* allocator;
CHECK_STATUS(OrtCreateDefaultAllocator(&allocator));
CHECK_STATUS(OrtGetDefaultAllocator(&allocator));

// print number of model input nodes
status = OrtSessionGetInputCount(session, &num_input_nodes);
Expand Down Expand Up @@ -101,7 +101,6 @@ int main(int argc, char* argv[]) {

OrtReleaseTypeInfo(typeinfo);
}
OrtReleaseAllocator(allocator);

// Results should be...
// Number of inputs = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ private void VerifyNativeMethodsExist()
"OrtEnableMemPattern","OrtDisableMemPattern","OrtEnableCpuMemArena","OrtDisableCpuMemArena",
"OrtSetSessionLogId","OrtSetSessionLogVerbosityLevel","OrtSetSessionThreadPoolSize","OrtSetSessionGraphOptimizationLevel",
"OrtSetOptimizedModelFilePath", "OrtSessionOptionsAppendExecutionProvider_CPU","OrtCreateAllocatorInfo","OrtCreateCpuAllocatorInfo",
"OrtCreateDefaultAllocator","OrtAllocatorFree","OrtAllocatorGetInfo",
"OrtGetDefaultAllocator","OrtAllocatorFree","OrtAllocatorGetInfo",
"OrtCreateTensorWithDataAsOrtValue","OrtGetTensorMutableData", "OrtReleaseAllocatorInfo",
"OrtCastTypeInfoToTensorInfo","OrtGetTensorTypeAndShape","OrtGetTensorElementType","OrtGetDimensionsCount",
"OrtGetDimensions","OrtGetTensorShapeElementCount","OrtReleaseValue"};
Expand Down
2 changes: 1 addition & 1 deletion include/onnxruntime/core/session/onnxruntime_c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ ORT_API_STATUS(OrtAllocatorAlloc, _Inout_ OrtAllocator* ptr, size_t size, _Outpt
ORT_API_STATUS(OrtAllocatorFree, _Inout_ OrtAllocator* ptr, void* p);
ORT_API_STATUS(OrtAllocatorGetInfo, _In_ const OrtAllocator* ptr, _Out_ const OrtAllocatorInfo** out);

ORT_API_STATUS(OrtCreateDefaultAllocator, _Outptr_ OrtAllocator** out);
ORT_API_STATUS(OrtGetDefaultAllocator, _Outptr_ OrtAllocator** out);

ORT_API(const char*, OrtGetVersionString);
/**
Expand Down
3 changes: 1 addition & 2 deletions include/onnxruntime/core/session/onnxruntime_cxx_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ struct Exception : std::exception {
#define ORT_DEFINE_RELEASE(NAME) \
inline void OrtRelease(Ort##NAME* ptr) { OrtRelease##NAME(ptr); }

ORT_DEFINE_RELEASE(Allocator);
ORT_DEFINE_RELEASE(AllocatorInfo);
ORT_DEFINE_RELEASE(CustomOpDomain);
ORT_DEFINE_RELEASE(Env);
Expand Down Expand Up @@ -228,7 +227,7 @@ struct Value : Base<OrtValue> {
};

struct Allocator : Base<OrtAllocator> {
static Allocator CreateDefault();
static Allocator GetDefault();

explicit Allocator(nullptr_t) {}
explicit Allocator(OrtAllocator* p) : Base<OrtAllocator>{p} {}
Expand Down
4 changes: 2 additions & 2 deletions include/onnxruntime/core/session/onnxruntime_cxx_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ struct TypeToTensorType<uint32_t> { static constexpr ONNXTensorElementDataType t
template <>
struct TypeToTensorType<uint64_t> { static constexpr ONNXTensorElementDataType type = ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64; };

inline Allocator Allocator::CreateDefault() {
inline Allocator Allocator::GetDefault() {
OrtAllocator* p;
ORT_THROW_ON_ERROR(OrtCreateDefaultAllocator(&p));
ORT_THROW_ON_ERROR(OrtGetDefaultAllocator(&p));
return Allocator(p);
}

Expand Down
3 changes: 1 addition & 2 deletions onnxruntime/core/providers/cpu/symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ OrtCompareAllocatorInfo
OrtCreateAllocatorInfo
OrtCreateCpuAllocatorInfo
OrtCreateCustomOpDomain
OrtCreateDefaultAllocator
OrtGetDefaultAllocator
OrtCreateEnv
OrtCreateEnvWithCustomLogger
OrtCreateRunOptions
Expand Down Expand Up @@ -51,7 +51,6 @@ OrtGetValueType
OrtGetVersionString
OrtIsTensor
OrtGetOnnxTypeFromTypeInfo
OrtReleaseAllocator
OrtReleaseAllocatorInfo
OrtReleaseCustomOpDomain
OrtReleaseEnv
Expand Down
9 changes: 3 additions & 6 deletions onnxruntime/core/session/default_cpu_allocator_c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,10 @@ struct OrtDefaultAllocator : OrtAllocatorImpl {
return OrtCreateStatus(ORT_RUNTIME_EXCEPTION, ex.what()); \
}

ORT_API_STATUS_IMPL(OrtCreateDefaultAllocator, _Out_ OrtAllocator** out) {
ORT_API_STATUS_IMPL(OrtGetDefaultAllocator, _Out_ OrtAllocator** out) {
API_IMPL_BEGIN
*out = new OrtDefaultAllocator();
static OrtDefaultAllocator ort_default_allocator;
*out = &ort_default_allocator;
return nullptr;
API_IMPL_END
}

ORT_API(void, OrtReleaseAllocator, _In_ OrtAllocator* allocator) {
delete static_cast<OrtAllocatorImpl*>(allocator);
}
2 changes: 1 addition & 1 deletion onnxruntime/server/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void ServerEnvironment::InitializeModel(const std::string& model_path) {

auto output_count = session.GetOutputCount();

auto allocator = Ort::Allocator::CreateDefault();
auto allocator = Ort::Allocator::GetDefault();
for (size_t i = 0; i < output_count; i++) {
auto name = session.GetOutputName(i, allocator);
model_output_names_.push_back(name);
Expand Down
2 changes: 1 addition & 1 deletion onnxruntime/test/perftest/ort_test_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ OnnxRuntimeTestSession::OnnxRuntimeTestSession(Ort::Env& env, std::random_device

size_t output_count = session_.GetOutputCount();
output_names_.resize(output_count);
Ort::Allocator a = Ort::Allocator::CreateDefault();
Ort::Allocator a = Ort::Allocator::GetDefault();
for (size_t i = 0; i != output_count; ++i) {
char* output_name = session_.GetOutputName(i, a);
assert(output_name != nullptr);
Expand Down
2 changes: 1 addition & 1 deletion onnxruntime/test/server/unit_tests/converter_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ TEST(MLValueToTensorProtoTests, UInt8ProtoRoundTrip) {
tp.set_data_type(onnx::TensorProto_DataType_UINT8);
Ort::Value ml_value{nullptr};
char buf[1000];
auto allocator = Ort::Allocator::CreateDefault();
auto allocator = Ort::Allocator::GetDefault();
auto info = allocator.GetInfo();
MemBuffer buffer((void*)&buf, tp.ByteSizeLong(), *info);
onnxruntime::server::TensorProtoToMLValue(tp, buffer, ml_value);
Expand Down
2 changes: 1 addition & 1 deletion onnxruntime/test/shared_lib/test_allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ TEST_F(CApiTest, allocation_info) {
}

TEST_F(CApiTest, DefaultAllocator) {
Ort::Allocator default_allocator = Ort::Allocator::CreateDefault();
Ort::Allocator default_allocator = Ort::Allocator::GetDefault();
char* p = (char*)default_allocator.Alloc(100);
ASSERT_NE(p, nullptr);
memset(p, 0, 100);
Expand Down
5 changes: 2 additions & 3 deletions samples/c_cxx/imagenet/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class Validator : public OutputCollector<TCharString> {
CreateSession();
VerifyInputOutputCount(session_);
OrtAllocator* ort_alloc;
ORT_THROW_ON_ERROR(OrtCreateDefaultAllocator(&ort_alloc));
ORT_THROW_ON_ERROR(OrtGetDefaultAllocator(&ort_alloc));
{
char* t;
ORT_THROW_ON_ERROR(OrtSessionGetInputName(session_, 0, ort_alloc, &t));
Expand All @@ -139,7 +139,6 @@ class Validator : public OutputCollector<TCharString> {
OrtAllocatorFree(ort_alloc, t);
}

OrtReleaseAllocator(ort_alloc);
OrtTypeInfo* info;
ORT_THROW_ON_ERROR(OrtSessionGetInputTypeInfo(session_, 0, &info));
const OrtTensorTypeAndShapeInfo* tensor_info;
Expand Down Expand Up @@ -253,7 +252,7 @@ int main(int argc, ORTCHAR_T* argv[]) {
try {
ret = real_main(argc, argv);
} catch (const std::exception& ex) {
fprintf(stderr, "%s\n", ex.what());
fprintf(stderr, "%s\n", ex.what());
}
#ifdef _WIN32
CoUninitialize();
Expand Down