Skip to content

Commit 3a83f8b

Browse files
authored
Update the functions in tensorprotoutils.h to use std::filesystem::path instead (microsoft#20920)
### Description 1. Update the functions in tensorprotoutils.h to use std::filesystem::path instead of onnxruntime::Path. Eventually we can remove the whole onnxruntime::Path class, but to this PR small I am not doing that. 2. Remove the _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING macro def when TensorRT EP is enabled.
1 parent 0cbe7ee commit 3a83f8b

File tree

72 files changed

+566
-603
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+566
-603
lines changed

cmake/onnxruntime_providers_tensorrt.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
set(OLD_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
1414
set(PROTOBUF_LIBRARY ${PROTOBUF_LIB})
1515
if (WIN32)
16-
add_definitions(-D_SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING=1)
1716
set(OLD_CMAKE_CUDA_FLAGS ${CMAKE_CUDA_FLAGS})
1817
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4099 /wd4551 /wd4505 /wd4515 /wd4706 /wd4456 /wd4324 /wd4701 /wd4804 /wd4702 /wd4458 /wd4703")
1918
if (CMAKE_BUILD_TYPE STREQUAL "Debug")

include/onnxruntime/core/graph/graph.h

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,7 @@
1010
#include <type_traits>
1111
#include <unordered_map>
1212
#include <unordered_set>
13-
14-
#ifdef _WIN32
15-
#pragma warning(push)
16-
// disable some warnings from protobuf to pass Windows build
17-
#pragma warning(disable : 4244)
18-
#endif
19-
20-
#ifdef _WIN32
21-
#pragma warning(pop)
22-
#endif
13+
#include <filesystem>
2314

2415
#include "core/common/flatbuffers.h"
2516

@@ -147,7 +138,7 @@ class Node {
147138
const std::string& Domain() const noexcept { return domain_; }
148139

149140
/** Gets the path of the owning model if any. */
150-
const Path& ModelPath() const noexcept;
141+
const std::filesystem::path& ModelPath() const noexcept;
151142

152143
/** Gets the Node's execution priority.
153144
@remarks Lower value means higher priority */
@@ -693,7 +684,7 @@ class Graph { // NOLINT(clang-analyzer-optin.performance.Padding): preserve exi
693684
const std::string& Description() const noexcept;
694685

695686
/** Gets the path of the owning model, if any. */
696-
const Path& ModelPath() const;
687+
const std::filesystem::path& ModelPath() const;
697688

698689
/** Returns true if this is a subgraph or false if it is a high-level graph. */
699690
bool IsSubgraph() const { return parent_graph_ != nullptr; }
@@ -1149,13 +1140,14 @@ class Graph { // NOLINT(clang-analyzer-optin.performance.Padding): preserve exi
11491140
ONNX_NAMESPACE::GraphProto ToGraphProto() const;
11501141

11511142
/** Gets the GraphProto representation of this Graph
1152-
@params external_file_name name of the binary file to use for initializers
1143+
@param external_file_path File path of the binary file to use for initializers.
1144+
@param model_file_path path of the model file.
11531145
@param initializer_size_threshold initializers larger or equal to this threshold (in bytes) are saved
11541146
in the external file. Initializer smaller than this threshold are included in the onnx file.
11551147
@returns GraphProto serialization of the graph.
11561148
*/
1157-
ONNX_NAMESPACE::GraphProto ToGraphProtoWithExternalInitializers(const std::string& external_file_name,
1158-
const PathString& file_path,
1149+
ONNX_NAMESPACE::GraphProto ToGraphProtoWithExternalInitializers(const std::filesystem::path& external_file_path,
1150+
const std::filesystem::path& model_file_path,
11591151
size_t initializer_size_threshold) const;
11601152

11611153
/** Gets the ISchemaRegistry instances being used with this Graph. */

include/onnxruntime/core/graph/graph_viewer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
// Licensed under the MIT License.
33

44
#pragma once
5+
#include <unordered_set>
6+
#include <filesystem>
57

68
#include "core/graph/graph.h"
79
#include "core/framework/session_options.h"
8-
#include <unordered_set>
910

1011
namespace onnxruntime {
1112
class Function;
@@ -43,7 +44,7 @@ class GraphViewer {
4344
const std::string& Description() const noexcept;
4445

4546
/** Gets the path of the owning model if any **/
46-
const Path& ModelPath() const noexcept { return graph_->ModelPath(); }
47+
const std::filesystem::path& ModelPath() const noexcept { return graph_->ModelPath(); }
4748

4849
/**
4950
Gets a tensor created from an initializer.

onnxruntime/core/framework/graph_partitioner.cc

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ static Status InlineFunctionsAOTImpl(const ExecutionProviders& execution_provide
637637

638638
static Status CreateEpContextModel(const ExecutionProviders& execution_providers,
639639
const Graph& graph,
640-
const std::string& ep_context_path,
640+
const std::filesystem::path& ep_context_path,
641641
const logging::Logger& logger) {
642642
InlinedVector<const Node*> all_ep_context_nodes;
643643
for (const auto& ep : execution_providers) {
@@ -658,22 +658,20 @@ static Status CreateEpContextModel(const ExecutionProviders& execution_providers
658658
return std::make_pair(false, static_cast<const Node*>(nullptr));
659659
};
660660

661-
onnxruntime::PathString context_cache_path;
662-
PathString model_pathstring = graph.ModelPath().ToPathString();
661+
std::filesystem::path context_cache_path;
662+
const std::filesystem::path& model_path = graph.ModelPath();
663663

664664
if (!ep_context_path.empty()) {
665-
context_cache_path = ToPathString(ep_context_path);
666-
} else if (!model_pathstring.empty()) {
667-
context_cache_path = model_pathstring + ToPathString("_ctx.onnx");
665+
context_cache_path = ep_context_path;
666+
} else if (!model_path.empty()) {
667+
context_cache_path = model_path.native() + ORT_TSTR("_ctx.onnx");
668+
} else {
669+
return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Both ep_context_path and model_path are empty");
668670
}
669671

670-
{
671-
#ifdef _WIN32
672-
std::wifstream fs(context_cache_path);
673-
#else
674-
std::ifstream fs(context_cache_path);
675-
#endif
676-
ORT_RETURN_IF(fs.good(), "Failed to generate EP context model since the file exist already.");
672+
if (std::filesystem::exists(context_cache_path)) {
673+
return ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "Failed to generate EP context model since the file '",
674+
context_cache_path, "' exist already.");
677675
}
678676

679677
Model ep_context_model(graph.Name(), false, ModelMetaData(), PathString(), IOnnxRuntimeOpSchemaRegistryList(),

onnxruntime/core/framework/model_metadef_id_generator.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ int ModelMetadefIdGenerator::GenerateId(const onnxruntime::GraphViewer& graph_vi
4040

4141
// prefer path the model was loaded from
4242
// this may not be available if the model was loaded from a stream or in-memory bytes
43-
const auto& model_path_str = main_graph.ModelPath().ToPathString();
43+
const auto model_path_str = main_graph.ModelPath().string();
4444
if (!model_path_str.empty()) {
4545
MurmurHash3::x86_128(model_path_str.data(), gsl::narrow_cast<int32_t>(model_path_str.size()), hash[0], &hash);
4646
} else {

onnxruntime/core/framework/node_unit.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ const std::string& NodeUnit::OpType() const noexcept { return target_node_.OpTyp
277277
const std::string& NodeUnit::Name() const noexcept { return target_node_.Name(); }
278278
int NodeUnit::SinceVersion() const noexcept { return target_node_.SinceVersion(); }
279279
NodeIndex NodeUnit::Index() const noexcept { return target_node_.Index(); }
280-
const Path& NodeUnit::ModelPath() const noexcept { return target_node_.ModelPath(); }
280+
const std::filesystem::path& NodeUnit::ModelPath() const noexcept { return target_node_.ModelPath(); }
281281
ProviderType NodeUnit::GetExecutionProviderType() const noexcept { return target_node_.GetExecutionProviderType(); }
282282

283283
void NodeUnit::InitForSingleNode() {

onnxruntime/core/framework/node_unit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <string>
1010
#include <optional>
1111
#include <vector>
12+
#include <filesystem>
1213

1314
#include "core/graph/basic_types.h"
1415
#include "core/graph/graph.h"
@@ -78,7 +79,7 @@ class NodeUnit {
7879
const std::string& Name() const noexcept;
7980
int SinceVersion() const noexcept;
8081
NodeIndex Index() const noexcept;
81-
const Path& ModelPath() const noexcept;
82+
const std::filesystem::path& ModelPath() const noexcept;
8283
ProviderType GetExecutionProviderType() const noexcept;
8384

8485
const Node& GetNode() const noexcept { return target_node_; }

onnxruntime/core/framework/session_options.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <vector>
88
#include <iostream>
99
#include <codecvt>
10+
#include <filesystem>
1011
#include "core/common/gsl.h"
1112
#include "core/common/inlined_containers.h"
1213
#include "core/framework/config_options.h"
@@ -89,7 +90,7 @@ struct SessionOptions {
8990
//
9091
// If session config value is not set, it will be assumed to be ONNX
9192
// unless the filepath ends in '.ort' (case insensitive).
92-
std::basic_string<ORTCHAR_T> optimized_model_filepath;
93+
std::filesystem::path optimized_model_filepath;
9394

9495
// enable the memory pattern optimization.
9596
// The idea is if the input shapes are the same, we could trace the internal memory allocation

0 commit comments

Comments
 (0)