Skip to content

Add a caching memory pool for dpctl.memory USM allocations - #2446

Draft
ndgrigorian wants to merge 34 commits into
masterfrom
feature/dpctl-sycl-usm-pooling
Draft

ndgrigorian wants to merge 34 commits into
masterfrom
feature/dpctl-sycl-usm-pooling

Conversation

@ndgrigorian

@ndgrigorian ndgrigorian commented Dec 6, 2026 •

Copy link
Copy Markdown
Collaborator

This PR proposes a caching memory pool for USM allocations, so that most allocations and frees do not reach the SYCL runtime. Allocations are made from larger USM allocations, split and merged as they are released, and cached per context, device and USM type. dpctl.memory gains empty_cache, get_cache_limit/set_cache_limit, get_max_split_size/set_max_split_size, get_pool_stats and reset_peak_pool_stats, and setting DPCTL_USM_POOL=0 disables caching

keep_args_alive no longer holds objects whose memory comes from the pool. It records its kernel as a use of the block instead, and the pool does not reuse the block before that work completes. Objects carrying pooled memory through usm_data or __sycl_usm_array_interface__ are treated the same way, and keep_args_alive takes a function finding the allocation an object carries (signalled by DPCTL_KEEP_ARGS_ALIVE_HAS_GET_OWNER) for native extensions.

Device blocks behave different based on in-order or out-of-order queues:

  • In-order queue allocated blocks are owned by that queue and reused by it as soon as they are released, since the queue orders the new work after the old. keep_args_alive submits nothing for such blocks,
  • Out of order queues hold blocks while work may still use them, until it completes. An allocation waits for the oldest such blocks once they exceed a limit relative to the memory in use and to the device memory left, and the oldest cached allocations of a device are freed once its pools hold more than 90% of its memory.

The pool also has a C-API and dpctl::memory::pool_allocate/dpctl::utils::keep_usm_alive in dpctl4pybind11.hpp for allocating from it in native extensions

Caching is added for in-order queues per device via dpctl.get_device_cached_in_order_queue, and record_queue marks a block as used on another queue.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?
  • If this PR is a work in progress, are you opening the PR as a draft?

Unaligned USM allocations come from a pool per context, device and kind, with size classes and best-fit reuse. Adopted and aligned pointers are freed directly. Adds empty_cache, get/set_cache_limit and get_pool_stats; DPCTL_USM_POOL=0 disables caching.
After draining and emptying the cache, a failed dpctl.memory allocation waits
for the events that deferred releases are held for, drains again and retries.
The registry holds pools by shared_ptr and each block's deleter keeps its pool,
so empty_cache can drop pools nothing refers to, releasing their contexts.
Also stop releasing a block twice when making its owner throws.
keep_args_alive and keep_usm_alive record their kernel on the pool block
instead of holding the owner, and a released block is reused only once its
recorded uses complete. Allocations made while not caching bypass the pool.
Large cached blocks from the limit on are not split, nor used for requests
below it or more than 1 MiB smaller than the block.
Each pool counts its used, pending, cached and reserved bytes with peaks, and
its USM allocations, frees and failures. get_pool_stats takes a queue and USM
kind for one pool, and reset_peak_pool_stats resets the peaks.
Waits for the failing pool's released blocks, then for held objects, a growing
number at a time, retrying after each wait. Memory_PoolAllocate skips the waits
when the caller holds the GIL.
The wait takes a scan lock so that it sees the items a concurrent scan has
taken off the list, and the allocation drains after every wait, as an item
may come due before it is waited for.
Deferred releases record the device of their queue, and a failed device or
shared allocation waits only for those on its device, or with none.
Blocks of nearby sizes can then reuse each other's allocation: a buffer growing
by 37 KiB a step from 8 to 19 MiB now makes 7 USM allocations rather than 300.
Power-of-two alignments up to the pool's block granularity come from the
pool, falling back to an aligned allocation if the block is not aligned.
USMAllocationError gives the requested size and the reserved, used, pending
and cached bytes of the pool for the device and USM kind.
…vironment

DPCTL_USM_POOL_CACHE_LIMIT and DPCTL_USM_POOL_MAX_SPLIT_SIZE take numbers of
bytes; invalid values are ignored with a warning.
Describes the pool's sizes and splitting, keep_args_alive recording uses,
what a failing allocation waits for, shared allocations keeping their advice,
and the pool's environment variables.
Caches one in-order queue per context and device, next to the default
cached queue. Queues pickled with only the in_order property are
restored as that queue.
Device allocations for an in-order queue are owned by it, which reuses
them without waiting, as its order puts new work after the old. Other
queues, empty_cache and the cache limit fence them with a barrier.
An in-order queue's order already covers the work on the device blocks
it owns, so keep_args_alive and keep_usm_alive skip them, and return a
complete event when nothing else needs gating. Adds Memory_PoolIsOwnedBy.
The counterpart of Tensor.record_stream: a block with recorded queues
gets a barrier on each when released, and is not reused before they
complete. Adds Memory_PoolRecordQueue and dpctl::memory::record_queue.
An object whose usm_data is pooled memory, or whose
__sycl_usm_array_interface__ points into a pooled block in use, is
recorded rather than held. The C++ keep_args_alive takes a function
finding the allocation an object carries.
make_free fences an owned block that becomes a whole allocation over the
cache limit, appending it to the released blocks being iterated.
DPCTLQueue_SubmitBarrier holds the implementation-specific barrier, so
the pool's fences need no DPC++ extension of their own.
Querying an event the Level Zero adapter has not submitted yet makes it
submit its open batch, so release no longer queries a block's fresh uses,
recorded uses are pruned oldest first, and released blocks are checked
oldest first only on a miss, as Torch does.
Lets native extensions use the keep_args_alive overload taking get_owner
while still building against dpctl versions without it.
An allocation the pool cannot reuse in queue order waits for the oldest
released blocks, rather than allocating more, while the bytes released
while in use exceed a floor or a ratio of the bytes in use.
The pending limit is the smaller of the ratio of bytes in use and a
headroom fraction of the device memory neither in use nor cached by the
pool, so that run-ahead leaves room for allocations outside the pool.
Matches running without the limit on the iGPU from 1 to 16 MiB arrays,
where 256 MiB cost 3-8%.
Unless set, the floor is 128 MiB for CPU devices and 512 MiB for others,
as CPU devices ran faster with less run-ahead.
Before allocating more, a pool frees the device's oldest cached USM
allocations while its pools hold over DPCTL_USM_POOL_GC_THRESHOLD of
the device memory, 0.9 by default. Documents the pending limit too.
@github-actions

github-actions Bot commented Dec 6, 2026

Copy link
Copy Markdown

@coveralls

coveralls commented Dec 6, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 75.429%. remained the same — feature/dpctl-sycl-usm-pooling into master

AdaptiveCpp's plain allocations are less than 512-byte aligned, so
aligned allocations keep pooled blocks aligned there. Skips pool tests
that need distinct USM types where the device does not report them.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants