Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
19 changes: 18 additions & 1 deletion src/models/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ State::State(const GeneratorParams& params, const Model& model)
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(1, INT_MAX);
graph_id_ = std::to_string(dis(gen));
graph_id_value_ = dis(gen);
graph_id_ = std::to_string(graph_id_value_);
Comment thread
qjia7 marked this conversation as resolved.
}
}

Expand Down Expand Up @@ -93,6 +94,7 @@ void State::Run(OrtSession& session, bool graph_capture_this_run) {
DurationTrace trace{"State::Run"};

if (params_->use_graph_capture) {
graph_capture_session_ = &session;
if (graph_capture_this_run) {
run_options_->AddConfigEntry("gpu_graph_id", graph_id_.c_str());
} else {
Expand Down Expand Up @@ -222,6 +224,21 @@ void State::SetActiveAdapter(Adapters* adapters, const std::string& adapter_name
}

State::~State() {
// Release captured graph resources in the EP.
// Note: the EP contract is to no-op when the id was never captured (e.g., when
// graph_capture_this_run was always false for this State). The call is wrapped
// in try/catch because destructors must not throw -- a throw during unwinding
// would call std::terminate.
#if ORT_API_VERSION >= 27
if (graph_capture_session_ && graph_id_value_ > 0) {
try {
graph_capture_session_->ReleaseCapturedGraph(graph_id_value_);
} catch (...) {
// Best-effort cleanup; swallow to keep the destructor non-throwing.
Comment thread
qjia7 marked this conversation as resolved.
}
}
#endif

Comment thread
qjia7 marked this conversation as resolved.
if (adapters_) {
for (const auto& adapter_name : adapter_names_) {
adapters_->ReleaseAdapter(adapter_name);
Expand Down
5 changes: 5 additions & 0 deletions src/models/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ struct State {

private:
std::string graph_id_{};
int graph_id_value_{0}; // integer form of graph_id_, used to avoid re-parsing in the destructor
// Session used for graph capture; not owned. Lifetime invariant: the OrtSession
// outlives this State because State is owned by Generator, and Generator is
// destroyed before the Model (and its session) that produced it.
OrtSession* graph_capture_session_{nullptr};
std::shared_ptr<Adapters> adapters_;
ExtraOutputs extra_outputs_;
};
Expand Down
4 changes: 4 additions & 0 deletions src/models/onnxruntime_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,10 @@ struct OrtSession {

void SetEpDynamicOptions(_In_opt_ const char* const* keys, const char* const* values, size_t kv_len);

#if ORT_API_VERSION >= 27
Comment thread
qjia7 marked this conversation as resolved.
void ReleaseCapturedGraph(int graph_annotation_id); ///< Wraps OrtApi::SessionReleaseCapturedGraph (ORT 1.27+)
#endif

static void operator delete(void* p) { Ort::api->ReleaseSession(reinterpret_cast<OrtSession*>(p)); }
Ort::Abstract make_abstract;
};
Expand Down
6 changes: 6 additions & 0 deletions src/models/onnxruntime_inline.h
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,12 @@ inline void OrtSession::SetEpDynamicOptions(const char* const* keys, const char*
Ort::ThrowOnError(Ort::api->SetEpDynamicOptions(this, keys, values, kv_len));
}

#if ORT_API_VERSION >= 27
inline void OrtSession::ReleaseCapturedGraph(int graph_annotation_id) {
Ort::ThrowOnError(Ort::api->SessionReleaseCapturedGraph(this, graph_annotation_id));
}
Comment thread
qjia7 marked this conversation as resolved.
#endif

inline std::string OrtModelMetadata::GetProducerName() const {
Ort::StringAllocator string_allocator;
Ort::ThrowOnError(Ort::api->ModelMetadataGetProducerName(this, &string_allocator, &string_allocator.out));
Expand Down
Loading