Core API
The flashdreams.core package collects the low-level attention primitives,
KV-cache implementations, and process-group utilities that integrations share.
Attention
The attention package provides the kernels used by the transformer and the block-structured KV cache that backs streaming inference.
flashdreams.core.attention.NativeAttention
NativeAttention(qkv_format: Literal['bhsd', 'bshd'] = 'bhsd', backend: Literal['math', 'efficient', 'cudnn', 'flash'] = 'cudnn')
Bases: Module
Native attention module with configurable QKV layout and SDPA backend.
Configure attention format and backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qkv_format
|
Literal['bhsd', 'bshd']
|
Layout of the QKV tensors; |
'bhsd'
|
backend
|
Literal['math', 'efficient', 'cudnn', 'flash']
|
SDPA backend selected via |
'cudnn'
|
Source code in flashdreams/flashdreams/core/attention/native.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
set_context_parallel_group
set_context_parallel_group(cp_group: ProcessGroup | None) -> None
Enable or disable context parallelism for ring attention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cp_group
|
ProcessGroup | None
|
Process group for context parallel; use None to disable. |
required |
Source code in flashdreams/flashdreams/core/attention/native.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
is_context_parallel_enabled
is_context_parallel_enabled() -> bool
Return True if context parallelism is active.
Source code in flashdreams/flashdreams/core/attention/native.py
62 63 64 | |
context_parallel_size
context_parallel_size() -> int
Return the context parallel world size, or 1 if disabled.
Source code in flashdreams/flashdreams/core/attention/native.py
66 67 68 | |
forward
forward(query: Tensor, key: Tensor, value: Tensor) -> Tensor
Run context-parallel SDPA (or single-rank SDPA when CP is disabled).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
Tensor
|
Query tensor in configured |
required |
key
|
Tensor
|
Key tensor in configured |
required |
value
|
Tensor
|
Value tensor in configured |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Attention output in the same format as inputs. |
Source code in flashdreams/flashdreams/core/attention/native.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
flashdreams.core.attention.ContextParallelAttention
ContextParallelAttention(qkv_format: Literal['bhsd', 'bshd'] = 'bhsd', backend: Literal['cudnn', 'flash'] = 'cudnn', method: Literal['ring', 'ulysses'] = 'ring', convert_to_fp32: bool = True)
Bases: NativeAttention
Context-parallel attention with selectable method and SDPA backend.
Configure context-parallel attention method and backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qkv_format
|
Literal['bhsd', 'bshd']
|
Layout of the QKV tensors; |
'bhsd'
|
backend
|
Literal['cudnn', 'flash']
|
SDPA backend; |
'cudnn'
|
method
|
Literal['ring', 'ulysses']
|
Context-parallelism strategy; |
'ring'
|
convert_to_fp32
|
bool
|
Promote LSE accumulators to fp32 during ring merges. |
True
|
Source code in flashdreams/flashdreams/core/attention/cp.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
flashdreams.core.attention.BlockKVCache
dataclass
BlockKVCache(k_shape: tuple[int, ...], v_shape: tuple[int, ...], seq_dim: int, chunk_size: int, window_size: int, sink_size: int = 0, device: device | str = device('cuda'), dtype: dtype = float16, _prev_chunk_idx: int = -1, _curr_chunk_idx: int | None = None, _n_cached: int = 0)
KV cache for causal attention with a fixed-size local window, CUDA-graph compatible.
Keys and values can have arbitrary shape [..., total_size, ...]; the sequence
(rolling) dimension is given by seq_dim (dimension index, can be negative).
Layout along that dimension: [sink tokens | local window tokens]. Sink tokens are
never evicted; the local window rolls left as new chunks are added if full. Chunks are
non-overlapping: each update adds one chunk of chunk_size tokens at the
next logical position in the full sequence.
Phases
- Filling: cache not yet full; tokens are written contiguously;
cached_k()/cached_v()return only the valid prefix. - Steady-state: if adding a chunk would exceed the fixed cache size, the
local window rolls left by the overflow amount and the new chunk
overwrites the rightmost positions;
cached_k()/cached_v()return the full buffer.
The argument chunk_idx (0, 1, 2, ...) is the index of the new chunk in the full
sequence (not an index into the cache). If chunk_idx is greater than
the previous one, the chunk is appended (or, in steady-state, written after
the roll). If chunk_idx equals the previous one, the same cache positions
are overwritten.
Per-step usage
- before_update(chunk_idx) — prepare (roll local window if steady-state).
- update(k, v) — write the new chunk's keys/values into the cache.
- cached_k() / cached_v() — get cached keys/values for attention.
- after_update(chunk_idx) — update internal bookkeeping.
k_shape
instance-attribute
k_shape: tuple[int, ...]
Shape of the keys. Must be the same as the values shape except for the last dimension.
v_shape
instance-attribute
v_shape: tuple[int, ...]
Shape of the values. Must be the same as the keys shape except for the last dimension.
seq_dim
instance-attribute
seq_dim: int
Sequence dimension that will be rolled. Can be negative.
chunk_size
instance-attribute
chunk_size: int
Number of tokens processed each time.
window_size
instance-attribute
window_size: int
Size of the local attention window (excluding sink tokens).
sink_size
class-attribute
instance-attribute
sink_size: int = 0
Number of sink tokens at the start of the cache that are never evicted. Defaults to 0.
device
class-attribute
instance-attribute
device: device | str = torch.device('cuda')
Device to store the cache on.
dtype
class-attribute
instance-attribute
dtype: dtype = torch.float16
Data type to store the cache in.
size
property
size: int
Number of valid cached tokens visible to attention.
write_end
property
write_end: int
Right edge of the current chunk in the physical cache layout.
from_tensor
classmethod
from_tensor(k: Tensor, v: Tensor, seq_dim: int) -> Self
Build a single-chunk cache pre-filled with the given key and value tensors.
Source code in flashdreams/flashdreams/core/attention/kvcache.py
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 | |
is_steady_state
is_steady_state() -> bool
Return True if the cache is full (steady-state phase).
Source code in flashdreams/flashdreams/core/attention/kvcache.py
745 746 747 748 749 750 751 752 753 754 755 756 757 | |
before_update
before_update(chunk_idx: int) -> None
Prepare the cache before writing new tokens.
If chunk_idx equals the previous chunk index, this is a no-op. Otherwise,
we expect the chunk_idx to be +1 from the previous chunk index. In this case,
we will roll the local window left if the cache is in steady-state, or no op
if the cache is in filling phase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_idx
|
int
|
Chunk index of the new chunk in the full sequence. |
required |
Source code in flashdreams/flashdreams/core/attention/kvcache.py
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 | |
update
update(k: Tensor, v: Tensor) -> None
Write the new chunk's keys and values into the cache.
Must be called after before_update() and before after_update().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
Tensor
|
Keys; shape must match cached keys except at seq_dim, where length must be chunk_size. |
required |
v
|
Tensor
|
Values; shape must match cached values except at seq_dim, where length must be chunk_size. |
required |
Source code in flashdreams/flashdreams/core/attention/kvcache.py
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 | |
after_update
after_update(chunk_idx: int) -> None
Finalize bookkeeping after writing new tokens.
Updates _prev_chunk_idx and, in filling phase, _n_cached.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_idx
|
int
|
The index of the new chunk in the full sequence. |
required |
Source code in flashdreams/flashdreams/core/attention/kvcache.py
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 | |
cached_k
cached_k() -> Tensor
Return cached keys for attention (valid prefix in filling phase, full buffer in steady-state).
Source code in flashdreams/flashdreams/core/attention/kvcache.py
844 845 846 847 848 | |
cached_v
cached_v() -> Tensor
Return cached values for attention (valid prefix in filling phase, full buffer in steady-state).
Source code in flashdreams/flashdreams/core/attention/kvcache.py
850 851 852 853 854 | |
reset
reset() -> None
Reset bookkeeping while preserving the allocated tensor storage.
Source code in flashdreams/flashdreams/core/attention/kvcache.py
856 857 858 859 860 | |
clone_kv
clone_kv() -> tuple[Tensor, Tensor]
Return clones of the full physical K/V buffers.
Source code in flashdreams/flashdreams/core/attention/kvcache.py
862 863 864 | |
overwrite_kv_
overwrite_kv_(k: Tensor, v: Tensor) -> None
Overwrite the full K/V buffers without changing their addresses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
Tensor
|
Replacement keys with the exact cache shape. |
required |
v
|
Tensor
|
Replacement values with the exact cache shape. |
required |
Source code in flashdreams/flashdreams/core/attention/kvcache.py
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 | |
Distributed
Helpers for multi-GPU / multi-node inference. init boots the NCCL process
group with sensible defaults and replaces the boilerplate at the top of
example launchers.
flashdreams.core.distributed.init
init() -> int | None
Initialize distributed training.
Source code in flashdreams/flashdreams/core/distributed/__init__.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
flashdreams.core.distributed.Device
Device(device_idx: int)
Lightweight wrapper around an NVML device handle for CPU-affinity queries.
Bind to the NVML handle for the GPU at device_idx.
Source code in flashdreams/flashdreams/core/distributed/__init__.py
158 159 160 161 | |
get_name
get_name() -> str
Return the marketing name reported by NVML for this device.
Source code in flashdreams/flashdreams/core/distributed/__init__.py
163 164 165 | |
get_cpu_affinity
get_cpu_affinity() -> list[int]
Return the indices of CPUs ideally affined to this GPU per NVML.
Source code in flashdreams/flashdreams/core/distributed/__init__.py
167 168 169 170 171 172 173 174 175 176 177 | |