fdwr / OnnxRuntimeDirectMLEPSample

Little console app to run an ONNX model through ONNX Runtime via the DirectML execution provider.
Creative Commons Zero v1.0 Universal
18 stars 6 forks source link

Resource Cleanup #2

Open Nietaa opened 1 year ago

Nietaa commented 1 year ago

It looks like the dmlAllocatorResourceCleanup deleter in CreateTensorValueFromExistingD3DResource() never gets called (unless CreateTensor() throws an exception) since it's being released before the function returns, and serves no purpose. Thus FreeGPUAllocation() will never be called for the dml resource. There's no call for ReleaseValue() either anywhere in the code.

Same code in WinML.

fdwr commented 1 year ago

Assuming I'm understanding the ORT API correctly, when you call this overload of CreateTensor, the OrtValue does not "own" the contents:

    Ort::Value newValue(
        Ort::Value::CreateTensor(
            memoryInformation,
            dmlAllocatorResource,
            tensorByteSize,
            tensorDimensions.data(),
            tensorDimensions.size(),
            elementDataType
        )
    );

And so you wouldn't call ReleaseValue on it.

The Dml::AllocationInfo returned from CreateGPUAllocationFromD3DResource is an opaque COM object that doesn't expose any additional methods. CreateTensorValueUsingD3DResource(..., dmlAllocatorResource) then returns the dmlEpResourceWrapper into a ComPtr<IUnknown> executionProviderTensorWrapper;, which implicitly cleans up the lifetime when done. Note Dml::FreeGpuAllocation inside ORT just calls the same Release method on the Dml::AllocationInfo (indirectly via the ComPtr destructor).

So I think it's correct 🤔.

DaiShaoJie77 commented 11 months ago

Add printing to your dmlAllocatorResourceCleanup and find that it will never be executed. I think it is redundant code. The resources here are released through the external execution of ProviderTensorWrapper. @fdwr