Skip to content

Commit

Permalink
Revert Implement DML copy for Lora Adapters (#22814)
Browse files Browse the repository at this point in the history
Revert #22396
  • Loading branch information
zhangxiang1993 authored Nov 12, 2024
1 parent 7fa6946 commit 69a36eb
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ namespace Dml
}
else
{
if (!m_closed)
if (!m_context->IsClosed())
{
// Free the underlying allocation once queued work has completed.
#ifdef _GAMING_XBOX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ namespace Dml

void SetDefaultRoundingMode(AllocatorRoundingMode roundingMode);

void Close()
{
m_closed = true;
}

public: // onnxruntime::IAllocator
void* Alloc(size_t size, AllocatorRoundingMode roundingMode);
void* Alloc(size_t size) final;
Expand Down Expand Up @@ -88,7 +83,6 @@ namespace Dml
std::vector<Bucket> m_pool;
size_t m_currentAllocationId = 0;
uint64_t m_currentResourceId = 0;
bool m_closed = false;

// Unless specifically requested, allocation sizes are not rounded to enable pooling
// until SetDefaultRoundingMode is called. This should be done at completion of session
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace Dml
// for example, an allocation from BucketizedBufferAllocator attempts to queue a reference
// to its underlying D3D resource when freed. Furthermore, these references are unnecessary
// since Close() already blocks for scheduled GPU work before clearing m_queuedReferences.
if (!m_clearingQueue)
if (!m_closing)
{
QueuedReference queuedReference = {GetLastFenceValue(), object};

Expand All @@ -70,15 +70,15 @@ namespace Dml
}
}

void CommandQueue::WaitForSignalAndClearQueue()
void CommandQueue::Close()
{
// Wait for flushed work:
assert(!m_clearingQueue);
m_clearingQueue = true;
assert(!m_closing);
m_closing = true;
GpuEvent event = GetCurrentCompletionEvent();
event.WaitForSignal(m_cpuSyncSpinningEnabled);
m_queuedReferences.clear();
m_clearingQueue = false;
m_closing = false;
}

void CommandQueue::ReleaseCompletedReferences()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace Dml
}
#endif

void WaitForSignalAndClearQueue();
void Close();
void ReleaseCompletedReferences();

private:
Expand All @@ -61,7 +61,7 @@ namespace Dml

ComPtr<ID3D12Fence> m_fence;
uint64_t m_lastFenceValue = 0;
bool m_clearingQueue = false;
bool m_closing = false;
bool m_cpuSyncSpinningEnabled = false;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ namespace Dml
ID3D12Device* d3d12Device,
IDMLDevice* dmlDevice,
ID3D12CommandQueue* queue,
bool cpuSyncSpinningEnabled)
bool cpuSyncSpinningEnabled,
bool keepOpen
)
: m_queue(std::make_shared<CommandQueue>(queue, cpuSyncSpinningEnabled))
, m_dmlRecorder(d3d12Device, dmlDevice, m_queue)
, m_cpuSyncSpinningEnabled(cpuSyncSpinningEnabled)
, m_keepOpen(keepOpen)
{
ORT_THROW_IF_FAILED(dmlDevice->GetParentDevice(IID_GRAPHICS_PPV_ARGS(m_d3dDevice.GetAddressOf())));
}
Expand All @@ -33,6 +36,8 @@ namespace Dml
D3D12_RESOURCE_STATES srcState,
uint64_t byteCount)
{
assert(!m_closed);

SetCommandRecorder(&m_dmlRecorder);

std::vector<D3D12_RESOURCE_BARRIER> barriers;
Expand Down Expand Up @@ -79,6 +84,8 @@ namespace Dml
_Out_ uint64_t* completionValue
)
{
assert(!m_closed);

SetCommandRecorder(&m_dmlRecorder);
m_dmlRecorder.ExecuteCommandList(commandList, fence, completionValue);
}
Expand All @@ -88,6 +95,7 @@ namespace Dml
const DML_BINDING_DESC& persistentResourceBinding,
const DML_BINDING_DESC& inputArrayBinding)
{
assert(!m_closed);
SetCommandRecorder(&m_dmlRecorder);

m_dmlRecorder.InitializeOperator(op, persistentResourceBinding, inputArrayBinding);
Expand All @@ -99,27 +107,31 @@ namespace Dml
gsl::span<const DML_BINDING_DESC> inputBindings,
gsl::span<const DML_BINDING_DESC> outputBindings)
{
assert(!m_closed);
SetCommandRecorder(&m_dmlRecorder);

m_dmlRecorder.ExecuteOperator(op, persistentResourceBinding, inputBindings, outputBindings);
}

void ExecutionContext::AddUAVBarrier()
{
assert(!m_closed);
SetCommandRecorder(&m_dmlRecorder);

m_dmlRecorder.AddUAVBarrier();
}

void ExecutionContext::ResourceBarrier(gsl::span<const D3D12_RESOURCE_BARRIER> barriers)
{
assert(!m_closed);
SetCommandRecorder(&m_dmlRecorder);

m_dmlRecorder.ResourceBarrier(barriers);
}

void ExecutionContext::GetCommandListForRecordingAndInvalidateState(ID3D12GraphicsCommandList** commandList)
{
assert(!m_closed);
SetCommandRecorder(&m_dmlRecorder);

// Ensure the descriptor heap is reset to D3D as something external may change it before recording
Expand All @@ -130,6 +142,8 @@ namespace Dml

void ExecutionContext::SetCommandRecorder(ICommandRecorder* newRecorder)
{
assert(!m_closed);

// If changing which recorder is the current one, we need to flush the old one first. This is to ensure correct
// ordering of operations on the command queue.
if (m_currentRecorder != newRecorder)
Expand All @@ -146,6 +160,8 @@ namespace Dml

void ExecutionContext::Flush()
{
assert(!m_closed);

if (!m_currentRecorder || !m_currentRecorder->HasUnsubmittedWork())
{
// Nothing to flush
Expand All @@ -164,21 +180,34 @@ namespace Dml

void ExecutionContext::QueueReference(IUnknown* object)
{
assert(!m_closed);
// If something has been recorded into a command list but not submitted yet, it means that the *next* fence
// value is the one to signal completion.
bool waitForUnsubmittedWork = (m_currentRecorder != nullptr);
m_queue->QueueReference(object, waitForUnsubmittedWork);
}

void ExecutionContext::WaitForSignalAndClearQueue()
void ExecutionContext::Close()
{
assert(!m_closed);

// Discard unflushed work and clear queued references. This prevents the circular reference:
// Kernel --> ProviderImpl --> Context --> QueuedRefs --> Kernel
m_queue->WaitForSignalAndClearQueue();
m_queue->Close();

// Keep the execution context open when requested, e.g. when used through the python API where there's a single context
// and single command queue
if (!m_keepOpen)
{
m_currentRecorder = nullptr;
m_closed = true;
}
}

GpuEvent ExecutionContext::GetCurrentCompletionEvent()
{
assert(!m_closed);

GpuEvent event = m_queue->GetCurrentCompletionEvent();

// If something has been recorded into a command list but not submitted yet, it means that the *next* fence
Expand All @@ -194,11 +223,13 @@ namespace Dml

void ExecutionContext::ReleaseCompletedReferences()
{
assert(!m_closed);
m_queue->ReleaseCompletedReferences();
}

D3D12_COMMAND_LIST_TYPE ExecutionContext::GetCommandListTypeForQueue() const
{
assert(!m_closed);
return m_queue->GetType();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ namespace Dml
ID3D12Device* d3d12Device,
IDMLDevice* dmlDevice,
ID3D12CommandQueue* queue,
bool cpuSyncSpinningEnabled);
bool cpuSyncSpinningEnabled,
bool keepOpen);

void SetAllocator(std::weak_ptr<BucketizedBufferAllocator> allocator);

// Waits for flushed work, discards unflushed work, and discards associated references to
// prevent circular references.
void WaitForSignalAndClearQueue();
// prevent circular references. Must be the last call on the object before destruction.
void Close();

// Queues a CopyBufferRegion (see ID3D12GraphicsCommandList::CopyBufferRegion) for execution. Transition
// barriers are automatically inserted to transition the source and destination resources to COPY_SOURCE and
Expand Down Expand Up @@ -86,6 +87,7 @@ namespace Dml

D3D12_COMMAND_LIST_TYPE GetCommandListTypeForQueue() const;
bool CpuSyncSpinningEnabled() const { return m_cpuSyncSpinningEnabled; }
bool IsClosed() const { return m_closed; }

private:
Microsoft::WRL::ComPtr<ID3D12Device> m_d3dDevice;
Expand All @@ -101,6 +103,10 @@ namespace Dml

bool m_closed = false;
bool m_cpuSyncSpinningEnabled = false;

// The python API has a global state used for I/O binding where the execution context is shared between session,
// so we don't want to close the context when one of the sessions is destroyed
bool m_keepOpen = false;
};

} // namespace Dml
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,7 @@ namespace Dml
// Release the cached command list references before closing the context
m_capturedGraphs.clear();

// Close the allocator before clearing the command queue to stop it from
// appending resources to it in an attempt to keep them alive.
if (m_allocator)
{
m_allocator->Close();
}

// Destroy the allocators. We are closing the execution provider, so from now on the
// only thing it will be used for is doing copies via the DataTransfer, which doesn't
// require allocating any memory.
// TODO: Move the copy functions over to ExecutionContext so that we are able to cleanly
// destroy ExecutionProviderImpl, and instead have the DataTransfer keep the context alive.
m_allocator = nullptr;
m_cpuInputAllocator = nullptr;

// Wait for all pending commands to be done executing and empty the command queue. This will
// Force all kernels and resources in flight to get destroyed and, from this point forward,
// ExecutionProviderImpl will only be used to execute transfer between resources that are
// already existing via the DataTransfer;
m_context->WaitForSignalAndClearQueue();
m_context->Close();
}

void ExecutionProviderImpl::WaitForOutstandingWork()
Expand Down
4 changes: 2 additions & 2 deletions onnxruntime/core/providers/dml/dml_provider_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ std::unique_ptr<IExecutionProvider> DMLProviderFactory::CreateProvider() {

// First, check if an I/O binding API that was used before this session or another session has already created a queue
if (FAILED(d3d12_device->GetPrivateData(dml_execution_context_guid, &execution_context_ptr_size, execution_context.GetAddressOf()))) {
execution_context = wil::MakeOrThrow<Dml::ExecutionContext>(d3d12_device.Get(), dml_device_.Get(), cmd_queue_.Get(), true);
execution_context = wil::MakeOrThrow<Dml::ExecutionContext>(d3d12_device.Get(), dml_device_.Get(), cmd_queue_.Get(), true, true);
ORT_THROW_IF_FAILED(d3d12_device->SetPrivateDataInterface(dml_execution_context_guid, execution_context.Get()));
}
} else {
execution_context = wil::MakeOrThrow<Dml::ExecutionContext>(d3d12_device.Get(), dml_device_.Get(), cmd_queue_.Get(), cpu_sync_spinning_enabled_);
execution_context = wil::MakeOrThrow<Dml::ExecutionContext>(d3d12_device.Get(), dml_device_.Get(), cmd_queue_.Get(), cpu_sync_spinning_enabled_, false);
}

auto provider = Dml::CreateExecutionProvider(dml_device_.Get(), execution_context.Get(), metacommands_enabled_, graph_capture_enabled_, cpu_sync_spinning_enabled_, disable_memory_arena_);
Expand Down
Loading

0 comments on commit 69a36eb

Please sign in to comment.