CrossAttentionLayer<T>
Layers · AiDotNet.NeuralNetworks.Layers
Implements cross-attention for conditioning diffusion models on text or other context.
Inheritance: LayerBase<T> → CrossAttentionLayer<T>
Implements: IShapeContract
For Beginners
Cross-attention is how the model “reads” the text prompt.
- Queries: “What should I generate at each position?”
- Keys/Values: “What does the text describe?”
- Output: Spatial features modified to match the text description
How It Works
Cross-attention differs from self-attention in that queries come from spatial features while keys and values come from conditioning (text embeddings). This is the core mechanism that allows text-to-image models like Stable Diffusion to follow prompts.
Constructors
| Constructor | Summary |
|---|---|
CrossAttentionLayer(int queryDim, int contextDim, int headCount, int sequenceLength) |
|
CrossAttentionLayer(int queryDim, int contextDim, int headCount, int sequenceLength, bool zeroOutputProjection) |
Creates cross attention with optional zero initialization of its output projection, as used by residual temporal-attention adapters. |
Properties
| Property | Summary |
|---|---|
AttentionKernelPath LastAttentionKernelPath { get; set; } |
Gets the attention kernel the most recent forward pass executed (#2312). |
IReadOnlyList<LayerPort> InputPorts { get; } |
|
bool SupportsTraining { get; } |
Methods
| Method | Summary |
|---|---|
Dictionary<string, string> GetParameterRoles() |
Returns parameter roles for per-role optimizer configuration (e.g., weight decay exemption for biases). |
IReadOnlyList<OutputAxisContract> OutputAxesFor(int inputRank) |
Derived from this type’s [TensorLayout] declarations. |
IReadOnlyList<Tensor<T>> GetTrainableParameters() |
Returns all trainable parameter tensors marked with [TrainableParameter]. |
void ClearGradients() |
|
void ResetState() |
|
void SetTrainableParameters(IReadOnlyList<Tensor<T>> parameters) |
Replaces trainable parameter tensors (e.g., with ParameterBuffer views). |
void UpdateParameters(T learningRate) |
|
void ZeroGrad() |
Clears all gradient fields discovered by convention ({paramName}Gradient). |