Core 注意力

精确 SDPA、布局分发、后端策略、RoPE、packed sequence、上下文并行和 block KV cache。

本页内容

WorldFoundry 把投影层和模型语义留在模型代码中,由 Core 负责那些经常被重复实现、也容易出错的机械工作:多头布局转换、精确后端选择、mask 处理、RoPE 应用、packed sequence 范围、上下文并行通信和滚动 KV 状态。

选择最窄的入口

当 Q、K、V 已经是标准的拆头形状时,使用 scaled_dot_product_attention。它遵循 PyTorch SDPA 契约,同时增加显式后端上下文、兼容的 GQA 扩展以及精确的 matmul fallback。

当张量是 (batch, sequence, hidden)、只缺少 head 拆分时,使用 flattened_multihead_attention,让拆头和合头留在一个公共位置。当模型接入有 einops 风格的 QKV 布局,或者需要通过策略选择可选 provider 时,使用 attention_forward。传入 mask 或设置 compatibility_mode=True 会有意回到 PyTorch 路径。

NativeAttention 把 SDPA 封装成 module,也可以绑定上下文并行 group。ContextParallelAttentionUlyssesSchedulerCSOHelper 是更底层的分布式机制,只有在模型 runtime 同时掌握对应 split metadata 时才应该直接使用。

一个可以在 CPU 或 GPU 上运行的例子

import torch

from worldfoundry.core import scaled_dot_product_attention

torch.manual_seed(7)
q = torch.randn(2, 8, 32, 64)
k = torch.randn(2, 8, 48, 64)
v = torch.randn(2, 8, 48, 96)

output = scaled_dot_product_attention(
    q,
    k,
    v,
    dropout_p=0.0,
    backend="math",  # 本例显式选择确定的 provider
)
assert output.shape == (2, 8, 32, 96)

推理时要明确传 dropout_p=0.0;PyTorch SDPA 不会从 module.eval() 自动推导这个值。布尔 mask 表示哪些 score 可以保留,浮点 mask 则会直接加到 score 矩阵上。

Block KV cache 的调用顺序

BlockKVCache 不是可以随意更新的字典。每个 chunk 都必须遵循 before_update → update → cached_k/cached_v → after_update。重复当前 chunk_idx 会覆盖同一个逻辑 chunk;递增一会追加或滚动本地窗口;跳过编号会报错。

import torch
from worldfoundry.core.attention import BlockKVCache

cache = BlockKVCache(
    k_shape=(1, 1, 4, 2),
    v_shape=(1, 1, 4, 3),
    seq_dim=2,
    chunk_size=2,
    window_size=4,
    device="cpu",
    dtype=torch.float32,
)

for chunk_idx in range(3):
    k = torch.full((1, 1, 2, 2), float(chunk_idx))
    v = torch.full((1, 1, 2, 3), float(chunk_idx))
    cache.before_update(chunk_idx)
    cache.update(k, v)
    visible_k = cache.cached_k()  # 依次可见 2、4、滚动后的 4 个 token
    cache.after_update(chunk_idx)

sink_size 保留永不淘汰的前缀,window_size 描述滚动区域。两者之和必须等于 cache 的序列维长度,并且能够被 chunk_size 整除。

完整参考

以下为该类别的生成签名。可用本页符号索引跳转;源码链接指向各惰性导出背后的具体实现。

41 个公开符号

def apply_nd_rotary_embedding(query: torch.Tensor,key: torch.Tensor,freqs_cis: torch.Tensor | tuple[torch.Tensor, torch.Tensor],head_first: bool = False,start_offset: int = 0) -> tuple[torch.Tensor, torch.Tensor]
worldfoundry.core.apply_nd_rotary_embeddingfrom worldfoundry.core import apply_nd_rotary_embedding
源码

简介

apply_nd_rotary_embedding — Apply precomputed n-D rotary frequencies to query and key tensors. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:tuple[torch.Tensor, torch.Tensor]

参数

querytorch.Tensor
Query tensor in `[B, S, H, D] layout by default, or [B, H, S, D] when head_first=True`.
keytorch.Tensor
Key tensor with the same layout convention and head width.
freqs_cistorch.Tensor | tuple[torch.Tensor, torch.Tensor]
Complex rotary frequencies or an explicit `(cos, sin)` pair covering the requested sequence window.
head_firstbool
Select `[B, H, S, D]` sequence-axis interpretation.默认值: False
start_offsetint
First frequency position, used with an existing KV prefix.默认值: 0

说明

The final head width must be compatible with complex pairs. Build matching structured frequencies with `get_nd_rotary_pos_embed`.

异常

ValueError
Frequency tensors do not cover the requested shape.

返回值: tuple[torch.Tensor, torch.Tensor]Rotated `(query, key)` tensors with original dtype and shape.

def apply_rotary_embedding(value: Any,cos: Any,sin: Any,rotary_dim: int | None = None,interleaved: bool = False) -> Any
worldfoundry.core.apply_rotary_embeddingfrom worldfoundry.core import apply_rotary_embedding
源码

简介

apply_rotary_embedding — Apply RoPE to the leading `rotary_dim features of the last dimension. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:Any`。

参数

valueAny
cosAny
sinAny
rotary_dimint | None
默认值: None
interleavedbool
默认值: False

返回值: Any

def attention_backend_capability(name: str,device: torch.device | str | int | None = None) -> AttentionKernelCapability
worldfoundry.core.attention_backend_capabilityfrom worldfoundry.core import attention_backend_capability
源码

简介

attention_backend_capability — Retrieve runtime capability metadata for a single normalized backend. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:AttentionKernelCapability

参数

namestr
devicetorch.device | str | int | None
默认值: None

返回值: AttentionKernelCapability

def attention_backend_context(backend: Literal['math', 'efficient', 'cudnn', 'flash'] | Any | None = None,backends: Any = None) -> Any
worldfoundry.core.attention_backend_contextfrom worldfoundry.core import attention_backend_context
源码

简介

attention_backend_context — Return a context manager that selects PyTorch SDPA backends via core. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:Any

参数

backendLiteral['math', 'efficient', 'cudnn', 'flash'] | Any | None
默认值: None
backendsAny
默认值: None

返回值: Any

def attention_backend_from_env(environ: Mapping[str, str] | None = None) -> str
worldfoundry.core.attention_backend_from_envfrom worldfoundry.core import attention_backend_from_env
源码

简介

attention_backend_from_env — Read and resolve the canonical attention backend requested by the environment. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:str

源码 docstring

Read and resolve the canonical attention backend requested by the environment.

Inspects WORLDFOUNDRY_ATTENTION_IMPLEMENTATION or WORLDFOUNDRY_ATTENTION_BACKEND, falling back to "auto" if neither is specified.

参数

environMapping[str, str] | None
默认值: None

返回值: str

def attention_backend_info() -> AttentionBackendInfo
worldfoundry.core.attention_backend_infofrom worldfoundry.core import attention_backend_info
源码

简介

attention_backend_info — Return the available generic PyTorch attention backend. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:AttentionBackendInfo

返回值: AttentionBackendInfo

def attention_backend_report() -> tuple[AttentionKernelCapability, ]
worldfoundry.core.attention_backend_reportfrom worldfoundry.core import attention_backend_report
源码

简介

attention_backend_report — Retrieve capability status of all registered backends, ordered by dispatch priority. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:tuple[AttentionKernelCapability, ...]

返回值: tuple[AttentionKernelCapability, ...]

def attention_dispatch_report() -> dict[str, object]
worldfoundry.core.attention.attention_dispatch_reportfrom worldfoundry.core.attention import attention_dispatch_report
源码

简介

attention_dispatch_report — Return lightweight operator-selection and quarantine state. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:dict[str, object]

返回值: dict[str, object]

def attention_forward(q: torch.Tensor,k: torch.Tensor,v: torch.Tensor,q_pattern = 'b n s d',k_pattern = 'b n s d',v_pattern = 'b n s d',out_pattern = 'b n s d',dims = None,attn_mask = None,scale = None,compatibility_mode = False)
worldfoundry.core.attention_forwardfrom worldfoundry.core import attention_forward
源码

简介

面向布局的注意力入口:适配 einops 风格 QKV,并经分发策略选择可选融合算子。

源码 docstring

Dispatch Q/K/V attention across qualified exact backends.

Tensor layouts are described by einops-style patterns and normalized before execution. Automatic mode tries only providers that are available for the current device, dtype, and shape, remembers unsupported workload signatures, and always retains the in-tree PyTorch SDPA path as the exact fallback.

参数

qtorch.Tensor
Query tensor in `q_pattern` layout.
ktorch.Tensor
Key tensor in `k_pattern` layout.
vtorch.Tensor
Value tensor in `v_pattern` layout.
q_pattern
Layout pattern for `q`.默认值: 'b n s d'
k_pattern
Layout pattern for `k`.默认值: 'b n s d'
v_pattern
Layout pattern for `v`.默认值: 'b n s d'
out_pattern
Requested output layout.默认值: 'b n s d'
dims
Named dimensions needed to expand grouped pattern terms such as `(n d)`.默认值: None
attn_mask
Optional boolean or additive attention mask. A mask forces the PyTorch compatibility path because optional fused providers do not share one mask contract.默认值: None
scale
Optional softmax scale; `None` uses the backend default.默认值: None
compatibility_mode
Skip optional providers and execute PyTorch SDPA directly.默认值: False

说明

Backend choice can be inspected with `attention_dispatch_report. Use clear_attention_dispatch_cache` after changing provider availability inside a long-lived process.

异常

RuntimeError
The selected provider fails for a reason other than an unsupported kernel or optional-library availability problem.
class AttentionBackendInfo(backend: str, uses_torch_sdpa: bool)
worldfoundry.core.AttentionBackendInfofrom worldfoundry.core import AttentionBackendInfo
源码

简介

完成探测与归一化后的注意力后端元数据。

属性

backendstr
uses_torch_sdpabool
class AttentionKernelCapability(name: str,package: str,available: bool,usable: bool,reason: str = '')
worldfoundry.core.AttentionKernelCapabilityfrom worldfoundry.core import AttentionKernelCapability
源码

简介

AttentionKernelCapability — Runtime availability metadata for a single attention kernel family. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。

属性

namestr
Canonical identifier of the attention backend.
packagestr
The underlying Python package or C++/CUDA extension.
availablebool
True if the module/package is physically installed in the environment.
usablebool
True if the backend is physically runnable on the active hardware (e.g., CUDA capability).
reasonstr
Explains why the backend is unusable or unavailable, if applicable.默认值: ''
class BlockKVCache(k_shape: tuple[int, ...],v_shape: tuple[int, ...],seq_dim: int,chunk_size: int,window_size: int,sink_size: int = 0,device: torch.device | str = torch.device('cuda'),dtype: torch.dtype = torch.float16)
worldfoundry.core.attention.BlockKVCachefrom worldfoundry.core.attention import BlockKVCache
源码

简介

分块滚动 KV cache,必须遵循 before_update → update → after_update;不要跳过 chunk 下标。

源码 docstring

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.

Note: Currently only supports `total_size (sink_size + window_size) divisible by chunk_size`.

Phases: - Filling: cache not yet full; tokens are written contiguously; `cached_k() / cached_v() return only the valid prefix. - Steady-state: cache full; each new chunk triggers a left-roll of the local window and 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: 1. before_update(chunk_idx) — prepare (roll local window if steady-state). 2. update(k, v) — write the new chunk's keys/values into the cache. 3. cached_k() / cached_v() — get cached keys/values for attention. 4. after_update(chunk_idx) — update internal bookkeeping.

属性

k_shapetuple[int, ...]
v_shapetuple[int, ...]
seq_dimint
chunk_sizeint
window_sizeint
sink_sizeint
默认值: 0
devicetorch.device | str
默认值: torch.device('cuda')
dtypetorch.dtype
默认值: torch.float16

方法

propsize -> int源码

简介

size — Number of valid cached tokens visible to attention.

参数

self

返回值: int

propwrite_end -> int源码

简介

write_end — Right edge of the current chunk in the physical cache layout.

参数

self

返回值: int

cmethfrom_tensor(k: Tensor,v: Tensor,seq_dim: int) -> Self源码

简介

from_tensor — Build a single-chunk cache pre-filled with the given key and value tensors.

参数

kTensor
vTensor
seq_dimint

返回值: Self

methis_steady_state() -> bool源码

简介

is_steady_state — Return True if the cache is full (steady-state phase).

返回值: bool

methbefore_update(chunk_idx: int) -> None源码

简介

before_update — Prepare the cache before writing new tokens.

源码 docstring

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.

参数

chunk_idxint
Chunk index of the new chunk in the full sequence.

返回值: None

methupdate(k: Tensor, v: Tensor) -> None源码

简介

update — Write the new chunk's keys and values into the cache.

源码 docstring

Write the new chunk's keys and values into the cache.

Must be called after `before_update() and before after_update()`.

参数

kTensor
Keys; shape must match cached keys except at seq_dim, where length must be chunk_size.
vTensor
Values; shape must match cached values except at seq_dim, where length must be chunk_size.

返回值: None

methafter_update(chunk_idx: int) -> None源码

简介

after_update — Finalize bookkeeping after writing new tokens.

源码 docstring

Finalize bookkeeping after writing new tokens.

Updates `_prev_chunk_idx and, in filling phase, _n_cached`.

参数

chunk_idxint
The index of the new chunk in the full sequence.

返回值: None

methcached_k() -> Tensor源码

简介

cached_k — Return cached keys for attention (valid prefix in filling phase, full buffer in steady-state).

返回值: Tensor

methcached_v() -> Tensor源码

简介

cached_v — Return cached values for attention (valid prefix in filling phase, full buffer in steady-state).

返回值: Tensor

methreset() -> None源码

简介

reset — Reset the cache to its initial empty state.

返回值: None

def clear_attention_dispatch_cache() -> None
worldfoundry.core.attention.clear_attention_dispatch_cachefrom worldfoundry.core.attention import clear_attention_dispatch_cache
源码

简介

clear_attention_dispatch_cache — Clear workload decisions and runtime failure quarantine. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:None

返回值: None

class ContextParallelAttention(qkv_format: Literal['bhsd', 'bshd'] = 'bhsd',backend: Literal['cudnn', 'flash'] = 'cudnn',method: Literal['ring', 'ulysses'] = 'ring',convert_to_fp32: bool = True)
worldfoundry.core.attention.ContextParallelAttentionfrom worldfoundry.core.attention import ContextParallelAttention
源码

简介

ContextParallelAttention — Context-parallel attention with selectable method and SDPA backend. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。

参数

qkv_formatLiteral['bhsd', 'bshd']
Layout of the QKV tensors; `"bhsd" or "bshd"`.默认值: 'bhsd'
backendLiteral['cudnn', 'flash']
SDPA backend; `"cudnn" or "flash"`.默认值: 'cudnn'
methodLiteral['ring', 'ulysses']
Context-parallelism strategy; `"ring" or "ulysses"`.默认值: 'ring'
convert_to_fp32bool
Promote LSE accumulators to fp32 during ring merges.默认值: True
def cp_post_process(cp_size: int,cp_strategy: str,x: torch.Tensor,meta_args: ModelMetaArgs) -> torch.Tensor
worldfoundry.core.cp_post_processfrom worldfoundry.core import cp_post_process
源码

简介

cp_post_process — Gather context-parallel output back to the original sequence layout. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:torch.Tensor

参数

cp_sizeint
Context-parallel world size. `1 returns x` unchanged.
cp_strategystr
`"cp_ulysses" or "cp_shuffle_overlap"`.
xtorch.Tensor
This rank's local output tensor.
meta_argsModelMetaArgs
Split sizes and padding recorded by `cp_pre_process`.

异常

ValueError
`cp_strategy` is unknown.

返回值: torch.TensorGlobally gathered tensor with shuffle-overlap padding removed.

def cp_pre_process(cp_size: int,cp_strategy: str,x: torch.Tensor,condition_map: torch.Tensor,rope: torch.Tensor,xattn_mask_for_cuda_graph: Union[torch.Tensor, None],ardf_meta: dict,core_attn_params: PackedCoreAttnParams,cross_attn_params: PackedCrossAttnParams)
worldfoundry.core.cp_pre_processfrom worldfoundry.core import cp_pre_process
源码

简介

cp_pre_process — This function is used to handle context parallel behavior, split input tensors into multiple parts and scatter them to different GPUs. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。

源码 docstring

This function is used to handle context parallel behavior, split input tensors into multiple parts and scatter them to different GPUs.

Input: cp_strategy: str. cp_ulysses for hopper or newer, cp_shuffle_overlap for 4090 or older x: (S, N, D). torch.Tensor of inputs embedding (images or latent representations of images) condition_map: (N * S). torch.Tensor determine which condition to use for each token rope: (S, 96). torch.Tensor of rope xattn_mask_for_cuda_graph: (N * denoising_range_num, L, 1, 1). torch.Tensor of xattn mask for cuda graph, None means no cuda graph core_attn_params: PackedCoreAttnParams. Packed sequence parameters for core_atten cross_attn_params: PackedCrossAttnParams. Packed sequence parameters for cross_atten

Output: x: (S', N, D). torch.Tensor of inputs embedding (images or latent representations of images) condition_map: (N * S'). torch.Tensor determine which condition to use for each token rope: (S', 96). torch.Tensor of rope cp_split_sizes: List[int]. Split sizes for each rank core_attn_params: PackedCoreAttnParams cross_attn_params: PackedCrossAttnParams

参数

cp_sizeint
cp_strategystr
xtorch.Tensor
condition_maptorch.Tensor
ropetorch.Tensor
xattn_mask_for_cuda_graphUnion[torch.Tensor, None]
ardf_metadict
core_attn_paramsPackedCoreAttnParams
cross_attn_paramsPackedCrossAttnParams
def cso_communication(input: torch.Tensor,cp_world_size: int,cp_split_sizes: List[int],comm_type: str = None) -> Tuple[torch.Tensor, torch.distributed.Work]
worldfoundry.core.cso_communicationfrom worldfoundry.core import cso_communication
源码

简介

cso_communication — Launch one context-shuffle-overlap all-to-all operation. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:Tuple[torch.Tensor, torch.distributed.Work]

参数

inputtorch.Tensor
Local sequence/head tensor.
cp_world_sizeint
Context-parallel group size.
cp_split_sizesList[int]
Per-rank split sizes.
comm_typestr
`"kv"` additionally reshapes KV heads before exchange.默认值: None

返回值: Tuple[torch.Tensor, torch.distributed.Work]Output buffer and asynchronous work handle. For one rank, the input and a completed fake handle are returned.

class CSOHelper(cp_shuffle_num,cp_world_size,cp_split_sizes)
worldfoundry.core.CSOHelperfrom worldfoundry.core import CSOHelper
源码

简介

CSOHelper — Pipeline chunked query exchange with attention computation. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。

源码 docstring

Pipeline chunked query exchange with attention computation.

The helper splits queries into `cp_shuffle_num chunks, starts the first asynchronous exchange, and rotates later query/output chunks so communication can overlap with fattn` execution.

参数

cp_shuffle_num
cp_world_size
cp_split_sizes

方法

methsplit_query_for_overlap(query)源码

简介

该类型上的公开 method

参数

query
methoverlap(fattn,qs,k,v)源码

简介

该类型上的公开 method

参数

fattn
qs
k
v
def flattened_multihead_attention(query: Tensor,key: Tensor,value: Tensor,num_heads: int,attn_mask: Tensor | None = None,dropout_p: float = 0.0,is_causal: bool = False,scale: float | None = None,backend: Literal['math', 'efficient', 'cudnn', 'flash'] | Any | None = None,backends: Any = None) -> Tensor
worldfoundry.core.flattened_multihead_attentionfrom worldfoundry.core import flattened_multihead_attention
源码

简介

面向 (batch, sequence, hidden) 张量的便捷封装,只需头数即可在 SDPA 前后完成拆分/合并。

源码 docstring

Apply SDPA to flattened `[B, S, H*D]` Q/K/V tensors.

This is the shared layout adapter for diffusion transformers. Model code should keep projections/RoPE/mask construction locally and delegate the generic head reshape, mask canonicalization, backend context, and output merge here.

参数

queryTensor
keyTensor
valueTensor
num_headsint
attn_maskTensor | None
默认值: None
dropout_pfloat
默认值: 0.0
is_causalbool
默认值: False
scalefloat | None
默认值: None
backendLiteral['math', 'efficient', 'cudnn', 'flash'] | Any | None
默认值: None
backendsAny
默认值: None

返回值: Tensor

def get_1d_rotary_pos_embed(dim: int,pos: torch.Tensor | int,theta: float = 10000.0,use_real: bool = False,theta_rescale_factor: float = 1.0,interpolation_factor: float = 1.0) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]
worldfoundry.core.get_1d_rotary_pos_embedfrom worldfoundry.core import get_1d_rotary_pos_embed
源码

简介

get_1d_rotary_pos_embed — Build one-dimensional rotary position frequencies. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:torch.Tensor | tuple[torch.Tensor, torch.Tensor]

参数

dimint
postorch.Tensor | int
thetafloat
默认值: 10000.0
use_realbool
默认值: False
theta_rescale_factorfloat
默认值: 1.0
interpolation_factorfloat
默认值: 1.0

返回值: torch.Tensor | tuple[torch.Tensor, torch.Tensor]

def get_meshgrid_nd(start: int | tuple[int, ...] | list[int],*args: int | tuple[int, ...] | list[int],dim: int = 2,device: torch.device | str | None = None) -> torch.Tensor
worldfoundry.core.get_meshgrid_ndfrom worldfoundry.core import get_meshgrid_nd
源码

简介

get_meshgrid_nd — Build an n-D meshgrid with PyTorch `linspace(endpoint=False) semantics. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:torch.Tensor`。

参数

startint | tuple[int, ...] | list[int]
argsint | tuple[int, ...] | list[int]
dimint
默认值: 2
devicetorch.device | str | None
默认值: None

返回值: torch.Tensor

def get_nd_rotary_pos_embed(rope_dim_list: list[int],start: int | tuple[int, ...] | list[int],*args: int | tuple[int, ...] | list[int],theta: float = 10000.0,use_real: bool = False,theta_rescale_factor: float | list[float] = 1.0,interpolation_factor: float | list[float] = 1.0,device: torch.device | str | None = None) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]
worldfoundry.core.get_nd_rotary_pos_embedfrom worldfoundry.core import get_nd_rotary_pos_embed
源码

简介

get_nd_rotary_pos_embed — Build n-D RoPE frequencies for tokens with structured grid coordinates. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:torch.Tensor | tuple[torch.Tensor, torch.Tensor]

参数

rope_dim_listlist[int]
startint | tuple[int, ...] | list[int]
argsint | tuple[int, ...] | list[int]
thetafloat
默认值: 10000.0
use_realbool
默认值: False
theta_rescale_factorfloat | list[float]
默认值: 1.0
interpolation_factorfloat | list[float]
默认值: 1.0
devicetorch.device | str | None
默认值: None

返回值: torch.Tensor | tuple[torch.Tensor, torch.Tensor]

def gpu_supports_flash_attention(device: torch.device | str | int | None = None) -> bool
worldfoundry.core.gpu_supports_flash_attentionfrom worldfoundry.core import gpu_supports_flash_attention
源码

简介

gpu_supports_flash_attention — Determine if the active CUDA device is architecturally capable of running FlashAttention. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:bool

源码 docstring

Determine if the active CUDA device is architecturally capable of running FlashAttention.

This in-tree FA2 path targets NVIDIA Ampere (SM8x), Ada (SM89), and Hopper (SM90). Blackwell kernels are resolved separately because SM100/103 and SM120 are not binary-compatible feature targets.

参数

devicetorch.device | str | int | None
默认值: None

返回值: bool

class ModelMetaArgs(H: int,W: int,cp_pad_size: int,cp_split_sizes: List[int],slice_point: int,denoising_range_num: int,range_num: int,extract_prefix_video_feature: bool,fwd_extra_1st_chunk: bool,distill_nearly_clean_chunk: bool,clip_token_nums: int,enable_cuda_graph: bool,core_attn_params: PackedCoreAttnParams,cross_attn_params: PackedCrossAttnParams)
worldfoundry.core.ModelMetaArgsfrom worldfoundry.core import ModelMetaArgs
源码

简介

ModelMetaArgs — Context-parallel layout metadata carried between pre/post processing. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。

源码 docstring

Context-parallel layout metadata carried between pre/post processing.

The fields describe spatial size, padding and per-rank splits, denoising ranges, optional feature-prefix behavior, CUDA-graph mode, and the packed self/cross-attention range objects. Model integrations normally construct this once per request and pass it unchanged through transformer blocks.

属性

Hint
Wint
cp_pad_sizeint
cp_split_sizesList[int]
slice_pointint
denoising_range_numint
range_numint
extract_prefix_video_featurebool
fwd_extra_1st_chunkbool
distill_nearly_clean_chunkbool
clip_token_numsint
enable_cuda_graphbool
core_attn_paramsPackedCoreAttnParams
cross_attn_paramsPackedCrossAttnParams
class NativeAttention(qkv_format: Literal['bhsd', 'bshd'] = 'bhsd',backend: Literal['math', 'efficient', 'cudnn', 'flash'] = 'cudnn')
worldfoundry.core.attention.NativeAttentionfrom worldfoundry.core.attention import NativeAttention
源码

简介

Core SDPA 的模块形态,可挂接 context-parallel 进程组。

参数

qkv_formatLiteral['bhsd', 'bshd']
Layout of the QKV tensors; `"bhsd" is (B, H, S, D), "bshd" is (B, S, H, D)`.默认值: 'bhsd'
backendLiteral['math', 'efficient', 'cudnn', 'flash']
SDPA backend selected via `sdpa_kernel`.默认值: 'cudnn'

方法

methset_context_parallel_group(cp_group: ProcessGroup | None) -> None源码

简介

set_context_parallel_group — Enable or disable context parallelism for ring attention.

参数

cp_groupProcessGroup | None
Process group for context parallel; use None to disable.

返回值: None

methis_context_parallel_enabled() -> bool源码

简介

is_context_parallel_enabled — Return True if context parallelism is active.

返回值: bool

methcontext_parallel_size() -> int源码

简介

context_parallel_size — Return the context parallel world size, or 1 if disabled.

返回值: int

methforward(query: Tensor,key: Tensor,value: Tensor) -> Tensor源码

简介

forward — Run context-parallel SDPA (or single-rank SDPA when CP is disabled).

参数

queryTensor
Query tensor in configured `qkv_format`.
keyTensor
Key tensor in configured `qkv_format`.
valueTensor
Value tensor in configured `qkv_format`.

返回值: TensorAttention output in the same format as inputs.

def normalize_attention_backend(value: str | None) -> str
worldfoundry.core.normalize_attention_backendfrom worldfoundry.core import normalize_attention_backend
源码

简介

normalize_attention_backend — Normalize colloquial or varied attention backend names into standard keys. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:str

源码 docstring

Normalize colloquial or varied attention backend names into standard keys.

For example, maps 'flash-attn-2', 'flash2', or 'flash_attention_2' to 'flash_attention_2' while stripping and lowering input values to handle typos gracefully.

参数

valuestr | None

返回值: str

def packed_sequence_attention(q: torch.Tensor,k: torch.Tensor,v: torch.Tensor,num_heads: int,compatibility_mode = False,scale = None)
worldfoundry.core.attention.packed_sequence_attentionfrom worldfoundry.core.attention import packed_sequence_attention
源码

简介

packed_sequence_attention — Apply the shared dispatcher to flattened packed-sequence Q/K/V. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。

参数

qtorch.Tensor
Query tensor shaped `(batch, sequence, heads * head_dim)`.
ktorch.Tensor
Key tensor with the same flattened-head convention.
vtorch.Tensor
Value tensor with the same flattened-head convention.
num_headsint
Head count used to split the final dimension.
compatibility_mode
Force the exact PyTorch SDPA path.默认值: False
scale
Optional attention softmax scale.默认值: None

说明

This adapter is appropriate when the model already packs heads into the hidden dimension. Use `attention_forward` directly for custom layouts or explicit masks.

class PackedCoreAttnParams(q_range: torch.Tensor,k_range: torch.Tensor,np_q_range: np.ndarray,np_k_range: np.ndarray,max_seqlen_q: int,max_seqlen_k: int)
worldfoundry.core.PackedCoreAttnParamsfrom worldfoundry.core import PackedCoreAttnParams
源码

简介

PackedCoreAttnParams — Cumulative ranges and maximum lengths for packed self-attention. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。

属性

q_rangetorch.Tensor
Device-side query range boundaries.
k_rangetorch.Tensor
Device-side key range boundaries.
np_q_rangenp.ndarray
CPU/Numpy query boundaries used by planning code.
np_k_rangenp.ndarray
CPU/Numpy key boundaries used by planning code.
max_seqlen_qint
Maximum query segment length.
max_seqlen_kint
Maximum key segment length.
class PackedCrossAttnParams(q_ranges: torch.Tensor | None = None,kv_ranges: torch.Tensor | None = None,cu_seqlens_q: torch.Tensor | None = None,cu_seqlens_kv: torch.Tensor | None = None,max_seqlen_q: int | None = None,max_seqlen_kv: int | None = None)
worldfoundry.core.PackedCrossAttnParamsfrom worldfoundry.core import PackedCrossAttnParams
源码

简介

PackedCrossAttnParams — Optional cumulative ranges for packed cross-attention. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。

属性

q_rangestorch.Tensor | None
Query segment ranges.默认值: None
kv_rangestorch.Tensor | None
Key/value segment ranges.默认值: None
cu_seqlens_qtorch.Tensor | None
Cumulative query lengths for varlen kernels.默认值: None
cu_seqlens_kvtorch.Tensor | None
Cumulative key/value lengths for varlen kernels.默认值: None
max_seqlen_qint | None
Maximum query segment length.默认值: None
max_seqlen_kvint | None
Maximum key/value segment length.默认值: None
def piecewise_attention(q: torch.Tensor,k: torch.Tensor,v: torch.Tensor,scale: float | None = None,density: float = 0.1,block_size: int = 64,min_sequence_length: int | None = None,strict: bool = False) -> torch.Tensor
worldfoundry.core.piecewise_attentionfrom worldfoundry.core import piecewise_attention
源码

简介

piecewise_attention — Run PISA block-routed attention or an exact SDPA fallback. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:torch.Tensor

源码 docstring

Run PISA block-routed attention or an exact SDPA fallback.

Inputs use `[batch, heads, sequence, head_dim]. density` is the fraction of KV blocks computed exactly; the remaining blocks use centroid approximation. Because this changes model math, PISA is never selected by the generic dense-attention dispatcher without an explicit caller choice.

参数

qtorch.Tensor
ktorch.Tensor
vtorch.Tensor
scalefloat | None
默认值: None
densityfloat
默认值: 0.1
block_sizeint
默认值: 64
min_sequence_lengthint | None
默认值: None
strictbool
默认值: False

返回值: torch.Tensor

def piecewise_attention_available(device: torch.device | str | None = None) -> bool
worldfoundry.core.piecewise_attention_availablefrom worldfoundry.core import piecewise_attention_available
源码

简介

piecewise_attention_available — Return whether the in-tree TMA implementation is eligible on `device. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:bool`。

参数

devicetorch.device | str | None
默认值: None

返回值: bool

class PositionGetter()
worldfoundry.core.PositionGetterfrom worldfoundry.core import PositionGetter
源码

简介

PositionGetter — Cache 2D patch-grid positions by grid shape. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。

方法

meth__call__(batch_size: int,height: int,width: int,device: torch.device) -> Tensor源码

简介

该类型上的公开 method

参数

batch_sizeint
heightint
widthint
devicetorch.device

返回值: Tensor

def probe_attention_backends(device: torch.device | str | int | None = None) -> dict[str, AttentionKernelCapability]
worldfoundry.core.probe_attention_backendsfrom worldfoundry.core import probe_attention_backends
源码

简介

probe_attention_backends — Probe installed attention packages and hardware compatibility. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:dict[str, AttentionKernelCapability]

源码 docstring

Probe installed attention packages and hardware compatibility.

Results are cached by runtime and compute capability, rather than globally. This keeps mixed A100/H100 nodes correct when the active tensor/device changes after module import.

参数

devicetorch.device | str | int | None
默认值: None

返回值: dict[str, AttentionKernelCapability]

def resolve_attention_backend(preferred: str | None = None,device: torch.device | str | int | None = None) -> str
worldfoundry.core.resolve_attention_backendfrom worldfoundry.core import resolve_attention_backend
源码

简介

综合参数、环境变量与能力探测,解析应使用的注意力后端。

源码 docstring

Resolve the highest priority usable backend, falling back gracefully to PyTorch SDPA.

If "auto" is preferred, traverses priority list to select the first runnable package. If the requested package is not usable on the active hardware, automatically degrades to "torch" SDPA to ensure robust execution.

参数

preferredstr | None
默认值: None
devicetorch.device | str | int | None
默认值: None

返回值: str

def resolve_transformers_attention_implementation(preferred: str | None = None,device: torch.device | str | int | None = None) -> str
worldfoundry.core.resolve_transformers_attention_implementationfrom worldfoundry.core import resolve_transformers_attention_implementation
源码

简介

resolve_transformers_attention_implementation — Resolve a backend name accepted by Transformers model configs. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:str

源码 docstring

Resolve a backend name accepted by Transformers model configs.

Transformers currently exposes portable `eager/sdpa paths and the separately installed flash_attention_2 provider. WorldFoundry has a wider backend vocabulary (including FA3 and model-specific kernels), so a small adapter is needed before writing config._attn_implementation`. Unsupported or unavailable providers conservatively map to PyTorch SDPA.

参数

preferredstr | None
默认值: None
devicetorch.device | str | int | None
默认值: None

返回值: str

def rotary_frequencies(seq_len: int,dim: int,base: float = 10000.0,start_index: int = 0,dtype: Any = None) -> tuple[Any, Any]
worldfoundry.core.rotary_frequenciesfrom worldfoundry.core import rotary_frequencies
源码

简介

rotary_frequencies — Build RoPE cosine/sine tables with shape `(seq_len, dim) using NumPy. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:tuple[Any, Any]`。

参数

seq_lenint
dimint
basefloat
默认值: 10000.0
start_indexint
默认值: 0
dtypeAny
默认值: None

返回值: tuple[Any, Any]

class RotaryPositionEmbedding2D(frequency: float = 100.0, scaling_factor: float = 1.0)
worldfoundry.core.RotaryPositionEmbedding2Dfrom worldfoundry.core import RotaryPositionEmbedding2D
源码

简介

RotaryPositionEmbedding2D — Apply rotary embeddings to tokens with `(y, x)` patch coordinates. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。

参数

frequencyfloat
默认值: 100.0
scaling_factorfloat
默认值: 1.0

方法

methforward(tokens: Tensor, positions: Tensor) -> Tensor源码

简介

该类型上的公开 method

参数

tokensTensor
positionsTensor

返回值: Tensor

def rotate_half(value: Any) -> Any
worldfoundry.core.rotate_halffrom worldfoundry.core import rotate_half
源码

简介

rotate_half — Rotate the last dimension as `[-x2, x1] for RoPE application. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:Any`。

参数

valueAny

返回值: Any

def scaled_dot_product_attention(query: Any,key: Any,value: Any,*args: Any,attn_mask: Any = None,dropout_p: float = 0.0,is_causal: bool = False,scale: float | None = None,enable_gqa: bool = False,backend: Literal['math', 'efficient', 'cudnn', 'flash'] | Any | None = None,backends: Any = None) -> Any
worldfoundry.core.scaled_dot_product_attentionfrom worldfoundry.core import scaled_dot_product_attention
源码

简介

带显式后端上下文的精确 SDPA。当 Q/K/V 已是标准分头形状时优先使用;推理时保持 dropout_p=0.0。

源码 docstring

Compute exact scaled dot-product attention through one stable core API.

The function mirrors PyTorch SDPA, adds explicit backend selection, and preserves compatibility with PyTorch versions that do not yet accept `enable_gqa`. When native SDPA is unavailable it evaluates the same attention equation with matmul, softmax, and an optional dropout.

参数

queryAny
Query tensor shaped `(..., query_length, head_dim). The common layout is (batch, heads, query_length, head_dim)`.
keyAny
Key tensor shaped `(..., key_length, head_dim)`.
valueAny
Value tensor shaped `(..., key_length, value_dim)`.
argsAny
Backward-compatible positional values for `attn_mask, dropout_p, is_causal, and scale`, in that order.
attn_maskAny
Boolean keep-mask or additive attention bias broadcastable to the attention score shape.默认值: None
dropout_pfloat
Probability applied to attention weights. Pass `0.0` at inference time; SDPA applies a non-zero value even in eval mode.默认值: 0.0
is_causalbool
Apply a lower-triangular causal mask.默认值: False
scalefloat | None
Softmax scale. `None uses 1 / sqrt(head_dim)`.默认值: None
enable_gqabool
Expand key/value heads when query has a compatible larger head count.默认值: False
backendLiteral['math', 'efficient', 'cudnn', 'flash'] | Any | None
One requested PyTorch SDPA backend: `math, efficient, cudnn, or flash`.默认值: None
backendsAny
Ordered backend collection. Takes precedence over `backend` and is useful when an explicit fallback order is required.默认值: None

说明

Prefer this function for already split heads. For flattened `(batch, sequence, hidden) tensors, use flattened_multihead_attention` so reshape and mask normalization stay centralized.

异常

TypeError
More than four compatibility positional options are given.
ValueError
Grouped-query attention head counts are incompatible.

返回值: AnyAttention values with the query prefix shape and `value.shape[-1]` as the final dimension.

class UlyssesScheduler()
worldfoundry.core.UlyssesSchedulerfrom worldfoundry.core import UlyssesScheduler
源码

简介

UlyssesScheduler — Overlap Ulysses all-to-all communication with Q/K/V and cross attention. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。

源码 docstring

Overlap Ulysses all-to-all communication with Q/K/V and cross attention.

Static entry points select separate, fused-KV, or fused-QKV communication schedules. Each returns self-attention output restored to model layout plus the concurrently computed cross-attention output.

方法

smethget_attn_and_xattn_with_comm_overlap(get_q_func: Callable,get_k_func: Callable,get_v_func: Callable,kv_cache_func: Callable,core_attn_func: Callable,cross_attn_func: Callable,overlap_degree: int,batch_size: int,cp_size: int,cp_split_sizes: List[int] = None)源码

简介

get_attn_and_xattn_with_comm_overlap — Get Q, K, V with communication overlap.

参数

get_q_funcCallable
get_k_funcCallable
get_v_funcCallable
kv_cache_funcCallable
core_attn_funcCallable
cross_attn_funcCallable
overlap_degreeint
batch_sizeint
cp_sizeint
cp_split_sizesList[int]
默认值: None
smethget_attn_and_xattn_with_fused_kv_comm(get_q_func: Callable,get_kv_func: Callable,kv_cache_func: Callable,core_attn_func: Callable,cross_attn_func: Callable,overlap_degree: int,batch_size: int,cp_size: int,cp_split_sizes: List[int] = None)源码

简介

get_attn_and_xattn_with_fused_kv_comm — When seq_len is very small, CPU-bound issues are severe.

参数

get_q_funcCallable
get_kv_funcCallable
kv_cache_funcCallable
core_attn_funcCallable
cross_attn_funcCallable
overlap_degreeint
batch_sizeint
cp_sizeint
cp_split_sizesList[int]
默认值: None
methget_attn_and_xattn_with_fused_qkv_comm(get_qkv_func: Callable,kv_cache_func: Callable,core_attn_func: Callable,cross_attn_func: Callable,overlap_degree: int,batch_size: int,cp_size: int,cp_split_sizes: List[int] = None)源码

简介

get_attn_and_xattn_with_fused_qkv_comm — By fusing the communication of q, k, and v together, further optimize CPU-bound issues.

参数

get_qkv_funcCallable
kv_cache_funcCallable
core_attn_funcCallable
cross_attn_funcCallable
overlap_degreeint
batch_sizeint
cp_sizeint
cp_split_sizesList[int]
默认值: None
smethget_attn_and_xattn_base(query: torch.Tensor,key: torch.Tensor,value: torch.Tensor,core_attn_func: Callable,cross_attn_func: Callable,overlap_degree: int,batch_size: int,cp_size: int,cp_split_sizes: List[int] = None)源码

简介

该类型上的公开 staticmethod

参数

querytorch.Tensor
keytorch.Tensor
valuetorch.Tensor
core_attn_funcCallable
cross_attn_funcCallable
overlap_degreeint
batch_sizeint
cp_sizeint
cp_split_sizesList[int]
默认值: None
def varlen_scaled_dot_product_attention(query: torch.Tensor,key: torch.Tensor,value: torch.Tensor,cu_seqlens_q: torch.Tensor,cu_seqlens_k: torch.Tensor,max_seqlen_q: int | None = None,max_seqlen_k: int | None = None,dropout_p: float = 0.0,softmax_scale: float | None = None,causal: bool = False,version: int | None = None,window_size: tuple[int, int] = (-1, -1),**kwargs: Any) -> torch.Tensor
worldfoundry.core.varlen_scaled_dot_product_attentionfrom worldfoundry.core import varlen_scaled_dot_product_attention
源码

简介

varlen_scaled_dot_product_attention — Run in-tree packed attention, or external FlashAttention 2 explicitly. 属于 Core 注意力(SDPA、后端、RoPE、packed sequence、KV cache)。 标注返回类型:torch.Tensor

参数

querytorch.Tensor
keytorch.Tensor
valuetorch.Tensor
cu_seqlens_qtorch.Tensor
cu_seqlens_ktorch.Tensor
max_seqlen_qint | None
默认值: None
max_seqlen_kint | None
默认值: None
dropout_pfloat
默认值: 0.0
softmax_scalefloat | None
默认值: None
causalbool
默认值: False
versionint | None
默认值: None
window_sizetuple[int, int]
默认值: (-1, -1)
kwargsAny

返回值: torch.Tensor