-
Notifications
You must be signed in to change notification settings - Fork 573
fix: register default global_scratch allocator on Blackwell GPUs #825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
a668170
27229be
84f040a
c426461
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -479,13 +479,18 @@ def map_triton_backend_to_torch_device() -> str: | |||||
| # This is a workaround for old nvidia card. | ||||||
| os.environ['TRITON_F32_DEFAULT'] = 'ieee' | ||||||
|
|
||||||
| def _default_alloc_fn(size: int, alignment: int, stream: int | None): | ||||||
| return torch.empty(size, device=torch.device(device_name, device_torch_lib.current_device()), dtype=torch.int8) | ||||||
|
|
||||||
| if IS_TMA_SUPPORTED: | ||||||
| logger.info('TMA is supported, using TMA by default.') | ||||||
|
|
||||||
| def alloc_fn(size: int, alignment: int, stream: int | None): | ||||||
| return torch.empty(size, device=torch.device(device_name, device_torch_lib.current_device()), dtype=torch.int8) | ||||||
|
|
||||||
| triton.set_allocator(alloc_fn) | ||||||
| triton.set_allocator(_default_alloc_fn) | ||||||
| elif IS_NVIDIA and torch.cuda.get_device_capability(0)[0] >= 10: | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using It is better to use
Suggested change
|
||||||
| # Blackwell (SM 10.0+): Triton compiler may emit global_scratch for | ||||||
| # autotuned kernels even without TMA. Register a default allocator to | ||||||
| # prevent NullAllocator crashes. See triton-lang/triton#10002. | ||||||
| logger.info('Blackwell detected: registering default global_scratch allocator.') | ||||||
| triton.set_allocator(_default_alloc_fn) | ||||||
|
|
||||||
|
|
||||||
| def get_all_max_shared_mem(): | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
triton set_allocator callback signature alignment stream requirements💡 Result:
The triton.set_allocator function in the Triton Language (triton-lang/triton Python package) allows users to specify a custom memory allocator callback for GPU kernel memory allocations. This is particularly useful for custom memory management, such as using torch.empty for temporary buffers or integrating with custom pools. Callback signature: def alloc_fn(size: int, alignment: int, stream: Optional[int] = None) -> torch.Tensor - size: Number of bytes to allocate. - alignment: Required alignment in bytes for the allocation (e.g., 128 or 256 bytes common for shared memory or cache lines; Triton's internal shared memory allocator uses 128-byte scratchAlignment). - stream: Optional CUDA stream handle (int); if provided, allocation should be stream-ordered (e.g., using cudaMallocAsync if implementing in C++/CUDA). Allocations must respect stream ordering to avoid synchronization issues. Usage example: def alloc_fn(size: int, alignment: int, stream: Optional[int]): return torch.empty(size, device="cuda", dtype=torch.int8) # or pinned memory, custom pool slice, etc. triton.set_allocator(alloc_fn) Requirements: - Alignment: Must return a pointer aligned to the requested 'alignment' bytes. Triton enforces alignments like 128 bytes for scratch buffers (lib/Analysis/Allocation.cpp). Misalignment can cause warp memory errors (e.g., Issue
#5136). - Stream: Optional but should use stream-ordered allocation (e.g., torch.empty implicitly uses current stream; for explicit control, use CUDA stream-ordered APIs like cudaMallocAsync from CUDA 11.2+ docs). Supports CUDA stream-ordered memory allocator for async alloc/free without global sync. - Return: A torch.Tensor (device="cuda") whose data_ptr is the allocated memory. Tensor should be contiguous and match size/alignment. - Thread-safe and efficient for kernel compilation/runtime. This feature is recent (not in all released versions; build from source or use latest PyTorch/Triton for availability, per Issues#6191,#155584). Used in advanced kernels like grouped-GEMM tutorials and third-party libs (e.g., gemlite). No official docs page found; inferred from source code patterns, tutorials, and issues.Citations:
The
alignmentparameter must not be ignored in the allocator callback.While ignoring
streamis acceptable (sincetorch.emptyimplicitly uses the current stream), thealignmentparameter is part of Triton's allocator contract and must be respected. Triton enforces alignment requirements (e.g., 128 bytes for scratch buffers), and misaligned allocations can cause warp memory errors. The current implementation usingtorch.empty(size, ...)does not guarantee the required alignment. This needs to be fixed by either using an alignment-aware allocation method or manually aligning the returned pointer.🤖 Prompt for AI Agents