Repository navigation
Add a caching memory pool for dpctl.memory USM allocations - #2446
Draft
ndgrigorian wants to merge 34 commits into
Draft
ndgrigorian wants to merge 34 commits into
ndgrigorian wants to merge 34 commits into
Conversation
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.
|
View rendered docs @ https://intelpython.github.io/dpctl/pulls/2446/index.html |
Collaborator
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.memorygainsempty_cache,get_cache_limit/set_cache_limit,get_max_split_size/set_max_split_size,get_pool_statsandreset_peak_pool_stats, and settingDPCTL_USM_POOL=0disables cachingkeep_args_aliveno 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 throughusm_dataor__sycl_usm_array_interface__are treated the same way, andkeep_args_alivetakes a function finding the allocation an object carries (signalled byDPCTL_KEEP_ARGS_ALIVE_HAS_GET_OWNER) for native extensions.Device blocks behave different based on in-order or out-of-order queues:
keep_args_alivesubmits nothing for such blocks,The pool also has a C-API and
dpctl::memory::pool_allocate/dpctl::utils::keep_usm_aliveindpctl4pybind11.hppfor allocating from it in native extensionsCaching is added for in-order queues per device via
dpctl.get_device_cached_in_order_queue, andrecord_queuemarks a block as used on another queue.