Skip to content

Commit dc2d850

Browse files
authored
Cherry-pick pull-request for patch release 0.8.3 (#1592)
1 parent ae1e975 commit dc2d850

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

src/dml/interface.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,17 @@ void InitDmlInterface(LUID* p_device_luid) {
218218
g_dml_device = std::make_unique<Dml::InterfaceImpl>(p_device_luid);
219219
}
220220

221+
void CloseDmlInterface() {
222+
Dml::dml_device_.Reset();
223+
g_dml_device.reset();
224+
Dml::dml_pooled_upload_heap_.reset();
225+
Dml::dml_execution_context_.reset();
226+
Dml::dml_readback_heap_.reset();
227+
Dml::smart_directml_dll_.reset();
228+
Dml::dml_objects_ = {};
229+
Dml::ort_allocator_ = nullptr;
230+
}
231+
221232
void SetDmlProvider(OrtSessionOptions& session_options) {
222233
Ort::ThrowOnError(Dml::dml_api_->SessionOptionsAppendExecutionProvider_DML1(&session_options, Dml::dml_device_.Get(), Dml::dml_objects_.command_queue.Get()));
223234
}

src/dml/interface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace Generators {
1313
void InitDmlInterface(LUID* p_device_luid);
1414
void SetDmlProvider(OrtSessionOptions& options);
1515

16+
void CloseDmlInterface();
17+
1618
DeviceInterface* GetDmlInterface();
1719

1820
} // namespace Generators

src/models/model.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,6 @@ DeviceInterface* SetProviderSessionOptions(OrtSessionOptions& session_options,
453453

454454
if (!disable_graph_capture) {
455455
session_options.AddConfigEntry("ep.dml.enable_graph_capture", "1");
456-
session_options.AddConfigEntry("ep.dml.disable_memory_arena", "1");
457456
}
458457

459458
SetDmlProvider(session_options);
@@ -643,7 +642,21 @@ Model::Model(std::unique_ptr<Config> config) : config_{std::move(config)} {
643642
p_device_kvcache_ = p_device_;
644643
}
645644

646-
Model::~Model() = default;
645+
Model::~Model() {
646+
#if USE_DML
647+
if (p_device_->GetType() == DeviceType::DML) {
648+
auto& allocator = GetOrtGlobals()->device_allocators_[static_cast<int>(DeviceType::DML)];
649+
allocator.session_.reset();
650+
allocator.allocator_.reset();
651+
session_options_.reset();
652+
// DML objects are globally scoped and launch background threads that retain hardware resources.
653+
// These threads persist beyond the lifetime of a Model, preventing proper cleanup and potentially causing deadlocks.
654+
// To avoid blocking driver threads, we explicitly destroy DML objects when the Model is destroyed.
655+
// They will be recreated as needed when a new Model is initialized.
656+
CloseDmlInterface();
657+
}
658+
#endif
659+
}
647660

648661
void Model::CreateSessionOptionsFromConfig(const Config::SessionOptions& config_session_options,
649662
OrtSessionOptions& session_options,

src/tensor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Tensor::Tensor(std::unique_ptr<OrtValue> ort_tensor) : ort_tensor_{std::move(ort
1616
Tensor::~Tensor() {
1717
if (buffer_ != nullptr) {
1818
p_device_->GetAllocator().Free(buffer_);
19+
buffer_ = nullptr;
1920
}
2021
}
2122

tools/python/util/android.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def start_emulator(
126126
"-no-audio",
127127
"-no-boot-anim",
128128
"-gpu",
129-
"guest",
129+
"swiftshader_indirect",
130130
"-delay-adb",
131131
]
132132

0 commit comments

Comments
 (0)