Extension libraries#
FlyDSL extensions are libraries implemented with the core expression API. They
have canonical imports under flydsl.extension and lazy aliases under
flydsl.expr:
from flydsl.extension import coop, random
# Equivalent inside traced code:
import flydsl.expr as fx
fx.coop.warp_reduce(...)
fx.random.rand4x(...)
The dispatcher selects a target-specific implementation when one exists while preserving the exported operation’s contract.
Cooperative algorithms#
flydsl.extension.coop provides warp- and block-scope collectives. Every
participating lane/thread must execute a collective with uniform control
options. width=None uses the target’s native warp width (64 on gfx9 CDNA
targets; 32 on gfx10/gfx11/gfx12 targets, including CDNA5 gfx1250); specify
width when an algorithm requires the same logical group size across
targets.
Warp functions#
API |
Behavior |
|---|---|
|
Reduce one value or a per-lane item range. The aggregate is returned to every participating lane. |
|
Reduce ascending-lane segments identified by a nonzero head flag; results are valid at segment heads. |
|
Tail-flag form of segmented reduction; results are valid at segment tails. |
|
Broadcast a lane’s scalar or aggregate value within each logical warp. |
|
Ordered prefix scans with optional |
|
Return |
|
Return |
|
Reduce batched/thread-packed inputs with an explicit output layout. |
|
Convenience result-layout variants for batched reduction. |
|
Sort key or key/value inputs across a logical warp. |
op can be a built-in fx.ReductionOp or an associative callable. Some
algorithms can reorder operands only when the operation is declared
commutative. Floating-point reassociation can therefore change the last bits
even when the mathematical result is equivalent.
Warp primitive classes#
Class forms make data movement and reusable specializations explicit:
Class |
Purpose |
|---|---|
|
Reusable reduction and scan specializations corresponding to the function APIs. |
|
Move blocked/striped/transpose-oriented per-lane item arrays between memory and registers. |
|
Rearrange per-lane items between blocked and striped organizations. |
|
Reusable sorting-network specializations. |
|
Algorithm selectors for the corresponding primitive classes. |
Block primitives#
Block algorithms are specialized with subscription syntax. Their
SharedStorage type must be allocated once and shared by all block threads:
block_scan = fx.coop.BlockScan[
fx.Int32,
fx.known_block_size(),
fx.coop.BlockScanAlgorithm.WARP_SCANS,
]
storage = fx.SharedAllocator().allocate(block_scan.SharedStorage).peek()
prefix = block_scan.exclusive(
value,
fx.ReductionOp.ADD,
init=0,
storage=storage,
)
API |
Contract |
|---|---|
|
Reduce scalar or per-thread item ranges in flattened blocked order.
Every thread receives the aggregate. |
|
Provides |
|
Selects warp-reduction, raking, or commutative policies. |
|
Selects warp-scan, raking, or memoized-raking policies. |
Synchronize with fx.barrier() before reusing a collective’s shared storage.
The block size must agree with the launched block and the specialization.
Runnable examples:
examples/extension/coop/01-warp_collectives.pydemonstrates warp reduction and exclusive scan without shared memory.examples/extension/coop/02-block_scan.pydemonstrates a vector-valued block scan used for stream compaction.
Random-number generation#
flydsl.extension.random implements stateless Philox-based generation. A
seed selects a stream; offset selects a deterministic position in that
stream. Callers are responsible for assigning non-overlapping offsets when
threads produce more than one value.
API |
Result |
|---|---|
|
One deterministic unsigned integer word. |
|
Four consecutive integer words. |
|
One uniform floating-point value. |
|
Four uniform floating-point values. |
|
One normally distributed value. |
|
Four normally distributed values. |
See Random number generation for offset rules, vector generation, and a complete kernel example.
Stability and dispatch#
An extension entry point, exported child namespace, and exported symbol form a
stable path according to API Stability. fx.coop/fx.random
aliases have the same stability as their canonical flydsl.extension paths.
Target-specific implementations must preserve the same exported contract.