Record Class AttentionContext
java.lang.Object
java.lang.Record
smile.llm.attention.AttentionContext
- Record Components:
scale- attention scale;<= 0means kernel default (1/sqrt(D)).dropout- dropout probability (torch_native only; usually 0 at inference).isCausal- whether to apply causal masking whenmaskis null.numQoHeads- query / output head count (GQA).numKvHeads- key / value head count.headDim- per-head dimension.layerId- KV pool layer index (FlashInfer).startPos- write position in the request (FlashInfer); uniform whenstartPositionsis null.seqLen- query sequence length this step.cacheLen- total cached length after this step (startPos + seqLen) whencacheLensis null.kvPool- shared pool, ornullfor contiguous torch_native-only calls.kvMetadata- CSR page table for FlashInfer, ornull.workspace- FlashInfer workspace, ornull.startPositions- optional per-row write positions (decode);null= uniform.cacheLens- optional per-row cache lengths (decode);null= uniform.raggedIndptr- cumulative segment lengths for ragged contiguous prefill;nullotherwise.
public record AttentionContext(double scale, double dropout, boolean isCausal, int numQoHeads, int numKvHeads, int headDim, int layerId, int startPos, int seqLen, int cacheLen, KvCachePool kvPool, FlashInferKvMetadata kvMetadata, FlashInferWorkspace workspace, int[] startPositions, int[] cacheLens, int[] raggedIndptr)
extends Record
Per-call metadata for
AttentionKernel.forward(Tensor, Tensor, Tensor, Tensor, AttentionContext).-
Constructor Summary
ConstructorsConstructorDescriptionAttentionContext(double scale, double dropout, boolean isCausal, int numQoHeads, int numKvHeads, int headDim, int layerId, int startPos, int seqLen, int cacheLen, KvCachePool kvPool, FlashInferKvMetadata kvMetadata, FlashInferWorkspace workspace, int[] startPositions, int[] cacheLens, int[] raggedIndptr) Creates an instance of aAttentionContextrecord class. -
Method Summary
Modifier and TypeMethodDescriptionintcacheLen()Returns the value of thecacheLenrecord component.int[]Returns the value of thecacheLensrecord component.static AttentionContextcontiguous(double scale, double dropout, boolean isCausal) Contiguous SDPA context (gather path).doubledropout()Returns the value of thedropoutrecord component.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.intheadDim()Returns the value of theheadDimrecord component.booleanisCausal()Returns the value of theisCausalrecord component.booleanisPaged()Returns whether this call carries paged-KV metadata.booleanisRagged()Returns whether per-row cache lengths are set.booleanReturns whether this is FlashInfer ragged contiguous Q/K/V (non-paged).Returns the value of thekvMetadatarecord component.kvPool()Returns the value of thekvPoolrecord component.intlayerId()Returns the value of thelayerIdrecord component.intReturns the value of thenumKvHeadsrecord component.intReturns the value of thenumQoHeadsrecord component.static AttentionContextpaged(double scale, boolean isCausal, int numQoHeads, int numKvHeads, int headDim, int layerId, int startPos, int seqLen, int cacheLen, KvCachePool kvPool, FlashInferKvMetadata kvMetadata, FlashInferWorkspace workspace) FlashInfer / paged context (uniform length).static AttentionContextpagedRagged(double scale, boolean isCausal, int numQoHeads, int numKvHeads, int headDim, int layerId, int seqLen, int[] startPositions, int[] cacheLens, KvCachePool kvPool, FlashInferKvMetadata kvMetadata, FlashInferWorkspace workspace) FlashInfer / paged context with per-row decode lengths / positions.static AttentionContextragged(double scale, boolean isCausal, int numQoHeads, int numKvHeads, int headDim, int[] indptr) Ragged contiguous prefill (vision tower / varlen self-attention).int[]Returns the value of theraggedIndptrrecord component.doublescale()Returns the value of thescalerecord component.intseqLen()Returns the value of theseqLenrecord component.intstartPos()Returns the value of thestartPosrecord component.int[]Returns the value of thestartPositionsrecord component.final StringtoString()Returns a string representation of this record class.Returns the value of theworkspacerecord component.
-
Constructor Details
-
AttentionContext
public AttentionContext(double scale, double dropout, boolean isCausal, int numQoHeads, int numKvHeads, int headDim, int layerId, int startPos, int seqLen, int cacheLen, KvCachePool kvPool, FlashInferKvMetadata kvMetadata, FlashInferWorkspace workspace, int[] startPositions, int[] cacheLens, int[] raggedIndptr) Creates an instance of aAttentionContextrecord class.- Parameters:
scale- the value for thescalerecord componentdropout- the value for thedropoutrecord componentisCausal- the value for theisCausalrecord componentnumQoHeads- the value for thenumQoHeadsrecord componentnumKvHeads- the value for thenumKvHeadsrecord componentheadDim- the value for theheadDimrecord componentlayerId- the value for thelayerIdrecord componentstartPos- the value for thestartPosrecord componentseqLen- the value for theseqLenrecord componentcacheLen- the value for thecacheLenrecord componentkvPool- the value for thekvPoolrecord componentkvMetadata- the value for thekvMetadatarecord componentworkspace- the value for theworkspacerecord componentstartPositions- the value for thestartPositionsrecord componentcacheLens- the value for thecacheLensrecord componentraggedIndptr- the value for theraggedIndptrrecord component
-
-
Method Details
-
contiguous
Contiguous SDPA context (gather path).- Parameters:
scale- attention scale;<= 0means kernel default.dropout- dropout probability (usually 0 at inference).isCausal- whether to apply causal masking when mask is null.- Returns:
- contiguous attention context.
-
ragged
public static AttentionContext ragged(double scale, boolean isCausal, int numQoHeads, int numKvHeads, int headDim, int[] indptr) Ragged contiguous prefill (vision tower / varlen self-attention).- Parameters:
scale- attention scale;<= 0means kernel default.isCausal- whether to apply causal masking when mask is null.numQoHeads- query / output head count (GQA).numKvHeads- key / value head count.headDim- per-head dimension.indptr- cumulative segment lengths[B+1](CPU array; copied to device at call time).- Returns:
- ragged contiguous attention context.
-
paged
public static AttentionContext paged(double scale, boolean isCausal, int numQoHeads, int numKvHeads, int headDim, int layerId, int startPos, int seqLen, int cacheLen, KvCachePool kvPool, FlashInferKvMetadata kvMetadata, FlashInferWorkspace workspace) FlashInfer / paged context (uniform length).- Parameters:
scale- attention scale;<= 0means kernel default.isCausal- whether to apply causal masking when mask is null.numQoHeads- query / output head count (GQA).numKvHeads- key / value head count.headDim- per-head dimension.layerId- KV pool layer index.startPos- write position in the request.seqLen- query sequence length this step.cacheLen- total cached length after this step.kvPool- shared KV cache pool.kvMetadata- CSR page table for FlashInfer.workspace- FlashInfer workspace.- Returns:
- paged attention context.
-
pagedRagged
public static AttentionContext pagedRagged(double scale, boolean isCausal, int numQoHeads, int numKvHeads, int headDim, int layerId, int seqLen, int[] startPositions, int[] cacheLens, KvCachePool kvPool, FlashInferKvMetadata kvMetadata, FlashInferWorkspace workspace) FlashInfer / paged context with per-row decode lengths / positions.- Parameters:
scale- attention scale;<= 0means kernel default.isCausal- whether to apply causal masking when mask is null.numQoHeads- query / output head count (GQA).numKvHeads- key / value head count.headDim- per-head dimension.layerId- KV pool layer index.seqLen- query sequence length this step.startPositions- optional per-row write positions;null= uniform.cacheLens- optional per-row cache lengths;null= uniform.kvPool- shared KV cache pool.kvMetadata- CSR page table for FlashInfer.workspace- FlashInfer workspace.- Returns:
- paged ragged attention context.
-
isPaged
public boolean isPaged()Returns whether this call carries paged-KV metadata.- Returns:
truewhen this call carries paged-KV metadata.
-
isRagged
public boolean isRagged()Returns whether per-row cache lengths are set.- Returns:
truewhen per-row cache lengths are set.
-
isRaggedContiguous
public boolean isRaggedContiguous()Returns whether this is FlashInfer ragged contiguous Q/K/V (non-paged).- Returns:
truefor FlashInfer ragged contiguous Q/K/V (non-paged).
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
scale
-
dropout
-
isCausal
-
numQoHeads
public int numQoHeads()Returns the value of thenumQoHeadsrecord component.- Returns:
- the value of the
numQoHeadsrecord component
-
numKvHeads
public int numKvHeads()Returns the value of thenumKvHeadsrecord component.- Returns:
- the value of the
numKvHeadsrecord component
-
headDim
-
layerId
-
startPos
-
seqLen
-
cacheLen
-
kvPool
-
kvMetadata
Returns the value of thekvMetadatarecord component.- Returns:
- the value of the
kvMetadatarecord component
-
workspace
-
startPositions
public int[] startPositions()Returns the value of thestartPositionsrecord component.- Returns:
- the value of the
startPositionsrecord component
-
cacheLens
-
raggedIndptr
public int[] raggedIndptr()Returns the value of theraggedIndptrrecord component.- Returns:
- the value of the
raggedIndptrrecord component
-