DiffusionCrossAttention<T>

Models & Types · AiDotNet.Diffusion.Attention

Cross-attention layer for diffusion models with Flash Attention optimization.

Inheritance: LayerBase<T> → DiffusionCrossAttention<T>

Implements: IShapeContract

For Beginners

Cross-attention is how the model “looks at” text while generating images.

  • Query (Q): Comes from the image features
  • Key (K) and Value (V): Come from text embeddings
  • Output: Image features enriched with text information

This enables the model to generate images that match the text description.

How It Works

Cross-attention allows the model to attend to conditioning information (like text embeddings) when generating images. This is how text-to-image models like Stable Diffusion work.

Constructors

Constructor Summary
DiffusionCrossAttention(int queryDim, int contextDim, int numHeads, int spatialSize) Initializes a new diffusion cross-attention layer.

Properties

Property Summary
bool SupportsTraining { get; }
int ContextDim { get; } Gets the context dimension.
int QueryDim { get; } Gets the query dimension.

Methods

Method Summary
IReadOnlyList<ILayer<T>> GetSubLayers()
IReadOnlyList<OutputAxisContract> OutputAxesFor(int inputRank) Derived from this type’s [TensorLayout] declarations.
Tensor<T> ForwardWithContext(Tensor<T> input, Tensor<T> context) Performs the forward pass with context (conditioning).
void ResetState() Resets the layer’s internal state.
void SetTrainingMode(bool isTraining) Propagates eval/training mode to the nested cross-attention sublayer so inference uses the GPU-resident fast path rather than the allocating tape/training branch.
void UpdateParameters(T learningRate) Updates parameters.