Skip to content

Commit 4b639c8

Browse files
committed
stash
1 parent af1a960 commit 4b639c8

File tree

8 files changed

+66
-32
lines changed

8 files changed

+66
-32
lines changed

c_cxx/CMakeLists.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ option(LIBPNG_ROOTDIR "libpng root dir")
1919
option(ONNXRUNTIME_ROOTDIR "onnxruntime root dir")
2020
include(FetchContent)
2121

22-
set(CMAKE_CXX_STANDARD 17)
22+
set(CMAKE_CXX_STANDARD 20)
2323

2424
if(NOT ONNXRUNTIME_ROOTDIR)
2525
if(WIN32)
@@ -41,14 +41,14 @@ link_directories("${ONNXRUNTIME_ROOTDIR}/lib")
4141

4242
if(WIN32)
4343
add_library(wil INTERFACE)
44-
44+
set(WIL_BUILD_PACKAGING OFF CACHE BOOL "" FORCE)
45+
set(WIL_BUILD_TESTS OFF CACHE BOOL "" FORCE)
4546
FetchContent_Declare(
4647
microsoft_wil
47-
URL https://github.com/microsoft/wil/archive/refs/tags/v1.0.220914.1.zip
48+
URL https://github.com/microsoft/wil/archive/refs/tags/v1.0.250325.1.zip
4849
)
49-
FetchContent_Populate(microsoft_wil)
50-
target_include_directories(wil INTERFACE ${microsoft_wil_SOURCE_DIR}/include)
51-
set(WIL_LIB wil)
50+
FetchContent_MakeAvailable(microsoft_wil)
51+
set(WIL_LIB "WIL::WIL")
5252
endif()
5353

5454
# On Linux the samples use libjpeg and libpng for decoding images.
@@ -97,4 +97,4 @@ add_subdirectory(squeezenet)
9797
if(WIN32 OR PNG_FOUND)
9898
add_subdirectory(fns_candy_style_transfer)
9999
endif()
100-
add_subdirectory(model-explorer)
100+
add_subdirectory(model-explorer)

c_cxx/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Note: These build instructions are for the Windows examples only.
2121

2222
## Prerequisites
2323
1. Visual Studio 2019 or 2022
24-
2. cmake(version >=3.13)
24+
2. cmake(version >=4.0)
2525
3. (optional) [libpng 1.6](https://libpng.sourceforge.io/)
2626

2727
## Install ONNX Runtime

c_cxx/imagenet/controller.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
Controller::Controller() : cleanup_group_(CreateThreadpoolCleanupGroup()), event_(CreateOnnxRuntimeEvent()) {
77
InitializeThreadpoolEnvironment(&env_);
8+
#pragma warning(disable : 6387) // The doc didn't say if the default pool could be used as callback pool or not
89
SetThreadpoolCallbackPool(&env_, nullptr);
10+
#pragma warning(default : 6387)
911
SetThreadpoolCallbackCleanupGroup(&env_, cleanup_group_, nullptr);
1012
}
1113

@@ -37,7 +39,7 @@ void Controller::SetFailBit(_Inout_opt_ ONNXRUNTIME_CALLBACK_INSTANCE pci, _In_
3739
}
3840
}
3941

40-
bool Controller::SetEof(ONNXRUNTIME_CALLBACK_INSTANCE pci) {
42+
bool Controller::SetEof(_Inout_opt_ ONNXRUNTIME_CALLBACK_INSTANCE pci) {
4143
std::lock_guard<std::mutex> g(m_);
4244
if (state_ == State::RUNNING) {
4345
state_ = State::SHUTDOWN;

c_cxx/imagenet/image_loader_wic.cc

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,37 @@
88
#include <sstream>
99

1010
#include "image_loader.h"
11+
#include "string_utils.h"
12+
#include "assert.h"
13+
14+
std::string ToMBString(std::wstring_view s) {
15+
if (s.size() >= static_cast<size_t>(std::numeric_limits<int>::max())) throw std::runtime_error("length overflow");
16+
17+
const int src_len = static_cast<int>(s.size() + 1);
18+
const int len = WideCharToMultiByte(CP_ACP, 0, s.data(), src_len, nullptr, 0, nullptr, nullptr);
19+
assert(len > 0);
20+
std::string ret(static_cast<size_t>(len) - 1, '\0');
21+
#pragma warning(disable : 4189)
22+
const int r = WideCharToMultiByte(CP_ACP, 0, s.data(), src_len, (char*)ret.data(), len, nullptr, nullptr);
23+
assert(len == r);
24+
#pragma warning(default : 4189)
25+
return ret;
26+
}
27+
28+
std::string ToUTF8String(std::wstring_view s) {
29+
if (s.size() >= static_cast<size_t>(std::numeric_limits<int>::max())) throw std::runtime_error("length overflow");
30+
31+
const int src_len = static_cast<int>(s.size() + 1);
32+
const int len = WideCharToMultiByte(CP_UTF8, 0, s.data(), src_len, nullptr, 0, nullptr, nullptr);
33+
assert(len > 0);
34+
std::string ret(static_cast<size_t>(len) - 1, '\0');
35+
#pragma warning(disable : 4189)
36+
const int r = WideCharToMultiByte(CP_UTF8, 0, s.data(), src_len, (char*)ret.data(), len, nullptr, nullptr);
37+
assert(len == r);
38+
#pragma warning(default : 4189)
39+
return ret;
40+
}
41+
1142

1243
bool CreateImageLoader(void** out) {
1344
IWICImagingFactory* piFactory;
@@ -84,24 +115,24 @@ OrtStatus* LoadImageFromFileAndCrop(void* loader, const ORTCHAR_T* filename, dou
84115
rect.Width = bbox_w_size;
85116

86117
ATLENSURE_SUCCEEDED(ppIFormatConverter->CopyPixels(&rect, stride, static_cast<UINT>(data.size()), data.data()));
87-
float* float_file_data = new float[data.size()];
118+
std::unique_ptr<float[]> float_file_data(new float[data.size()]);
88119
size_t len = data.size();
89120
for (size_t i = 0; i != len; ++i) {
90-
float_file_data[i] = static_cast<float>(data[i]) / 255;
121+
float_file_data.get()[i] = static_cast<float>(data[i]) / 255;
91122
}
92123

93-
*out = float_file_data;
124+
*out = float_file_data.release();
94125
*out_width = bbox_w_size;
95126
*out_height = bbox_h_size;
96127
return nullptr;
97128
} catch (const std::exception& ex) {
98-
std::ostringstream oss;
129+
std::basic_ostringstream<ORTCHAR_T> oss;
99130
oss << "Load " << filename << " failed:" << ex.what();
100-
return Ort::GetApi().CreateStatus(ORT_FAIL, oss.str().c_str());
131+
return Ort::GetApi().CreateStatus(ORT_FAIL, ToUTF8String(oss.str()).c_str());
101132
} catch (const CAtlException& ex) {
102-
std::ostringstream oss;
133+
std::basic_ostringstream<ORTCHAR_T> oss;
103134
oss << "Load " << filename << " failed:";
104135
PrintErrorDescription(ex.m_hr, oss);
105-
return Ort::GetApi().CreateStatus(ORT_FAIL, oss.str().c_str());
136+
return Ort::GetApi().CreateStatus(ORT_FAIL, ToUTF8String(oss.str()).c_str());
106137
}
107138
}

c_cxx/imagenet/local_filesystem_win.cc

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
#include "local_filesystem.h"
55
#include <assert.h>
66
#include <mutex>
7-
8-
static std::mutex m;
7+
#include "string_utils.h"
98

109
void ReadFileAsString(const ORTCHAR_T* fname, void*& p, size_t& len) {
1110
if (!fname) {
@@ -15,17 +14,17 @@ void ReadFileAsString(const ORTCHAR_T* fname, void*& p, size_t& len) {
1514
HANDLE hFile = CreateFileW(fname, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1615
if (hFile == INVALID_HANDLE_VALUE) {
1716
int err = GetLastError();
18-
std::ostringstream oss;
17+
std::basic_ostringstream<ORTCHAR_T> oss;
1918
oss << "open file " << fname << " fail, errcode =" << err;
20-
throw std::runtime_error(oss.str().c_str());
19+
throw std::runtime_error(ToMBString(oss.str()));
2120
}
2221
std::unique_ptr<void, decltype(&CloseHandle)> handler_holder(hFile, CloseHandle);
2322
LARGE_INTEGER filesize;
2423
if (!GetFileSizeEx(hFile, &filesize)) {
2524
int err = GetLastError();
26-
std::ostringstream oss;
25+
std::basic_ostringstream<ORTCHAR_T> oss;
2726
oss << "GetFileSizeEx file " << fname << " fail, errcode =" << err;
28-
throw std::runtime_error(oss.str().c_str());
27+
throw std::runtime_error(ToMBString(oss.str()));
2928
}
3029
if (static_cast<ULONGLONG>(filesize.QuadPart) > std::numeric_limits<size_t>::max()) {
3130
throw std::runtime_error("ReadFileAsString: File is too large");
@@ -53,16 +52,16 @@ void ReadFileAsString(const ORTCHAR_T* fname, void*& p, size_t& len) {
5352
int err = GetLastError();
5453
p = nullptr;
5554
len = 0;
56-
std::ostringstream oss;
55+
std::basic_ostringstream<ORTCHAR_T> oss;
5756
oss << "ReadFile " << fname << " fail, errcode =" << err;
58-
throw std::runtime_error(oss.str().c_str());
57+
throw std::runtime_error(ToMBString(oss.str()));
5958
}
6059
if (bytes_read != bytes_to_read) {
6160
p = nullptr;
6261
len = 0;
63-
std::ostringstream oss;
62+
std::basic_ostringstream<ORTCHAR_T> oss;
6463
oss << "ReadFile " << fname << " fail: unexpected end";
65-
throw std::runtime_error(oss.str().c_str());
64+
throw std::runtime_error(ToMBString(oss.str()));
6665
}
6766
}
6867
p = buffer.release();

c_cxx/imagenet/main.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
#include <chrono>
45
#include <string>
56
#include <string.h>
67
#include <sstream>
@@ -27,7 +28,7 @@
2728
#ifdef _WIN32
2829
#include <atlbase.h>
2930
#endif
30-
31+
#include "string_utils.h"
3132
using namespace std::chrono;
3233

3334

@@ -44,10 +45,10 @@ class Validator : public OutputCollector<TCharString> {
4445
if (!line.empty()) labels.push_back(line);
4546
}
4647
if (labels.size() != expected_line_count) {
47-
std::ostringstream oss;
48+
std::basic_ostringstream<ORTCHAR_T> oss;
4849
oss << "line count mismatch, expect " << expected_line_count << " from " << file_path.c_str() << ", got "
4950
<< labels.size();
50-
throw std::runtime_error(oss.str());
51+
throw std::runtime_error(ToMBString(oss.str()));
5152
}
5253
return labels;
5354
}

c_cxx/model-explorer/model-explorer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ std::string print_shape(const std::vector<std::int64_t>& v) {
3939
return ss.str();
4040
}
4141

42+
// XXX: this function does not handle integer overflow
4243
int calculate_product(const std::vector<std::int64_t>& v) {
4344
int total = 1;
44-
for (auto& i : v) total *= i;
45+
for (auto& i : v) total *= static_cast<int>(i);
4546
return total;
4647
}
4748

@@ -105,7 +106,7 @@ int main(int argc, ORTCHAR_T* argv[]) {
105106

106107
// generate random numbers in the range [0, 255]
107108
std::vector<float> input_tensor_values(total_number_elements);
108-
std::generate(input_tensor_values.begin(), input_tensor_values.end(), [&] { return rand() % 255; });
109+
std::generate(input_tensor_values.begin(), input_tensor_values.end(), [&] { return static_cast<float>(rand() % 255); });
109110
std::vector<Ort::Value> input_tensors;
110111
input_tensors.emplace_back(vec_to_tensor<float>(input_tensor_values, input_shape));
111112

c_cxx/squeezenet/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ option(ONNXRUNTIME_ROOTDIR "onnxruntime root dir")
1010
include(CheckIncludeFileCXX)
1111
CHECK_INCLUDE_FILE_CXX(tensorrt_provider_factory.h HAVE_TENSORRT_PROVIDER_FACTORY_H)
1212

13-
set(CMAKE_CXX_STANDARD 14)
13+
set(CMAKE_CXX_STANDARD 17)
1414
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1515

1616
include_directories(

0 commit comments

Comments
 (0)