Scaled Dot Product Attention — Machine Learning/Neural Networks
Machine_Learning/Neural_Networks/Scaled_Dot_Product_Attention · 1 input / 1 output port(s) at insert · exports to Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text
Description#
The block's own DESCRIPTION_HTML, rendered verbatim — the same text the config dialog's info panel and the library navigator show. Fix a wrong sentence in the block's .cpp (R-D9), never here.
Scaled Dot-Product Attention
Machine Learning / Neural Networks
One attention head over a window of T positions:
Q = X·Wq, K = X·Wk, V = X·Wv, A = softmax(Q·K' ÷ √dk) row-wise, Y = A·V
Each output row is a weighted average of the value vectors, with the weights decided by how well that position's query matches every position's key. It is the mechanism a transformer is built from, and the reason a model can relate two positions directly rather than passing information along a chain of recurrences.
The window arrives as a matrix rather than being buffered inside the block, which is what lets Positional Encoding feed straight into it – attention alone is order-blind, so that block exists to mark position, and the two are meant to be used together.
Ports
- X – the window, [T, dmodel]: one row per position, one column per feature. T and dmodel are read from the signal, not configured.
- Y – the attended window, [T, dv], where dv is the width of the value projection.
Parameters
- Query Weights – Wq, [dmodel, dk].
- Key Weights – Wk, the same shape as Wq: queries and keys are compared by dot product, so they must live in the same space. A mismatch is reported rather than broadcast.
- Value Weights – Wv, [dmodel, dv]. Its width sets the output width and is free of the other two.
- Masking – whether a position may attend to later ones.
- None – every position attends to the whole window. The encoder-style reading, and the right one when the window is a complete snapshot.
- Causal – position i attends only to j ≤ i, so no output row depends on a position later than itself. The decoder-style reading, and the one to use when the window is a sliding history and a later row must not leak into an earlier one.
- Sampling Time (s) – zero or less inherits the solver's rate; a positive value runs the block at that period.
Code export
All ten targets: Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog and PLC Structured Text. Every projection, score and weight is unrolled: T, dmodel, dk and dv are known at export time, so no backend contains a loop, an index type or a mask array. The scale 1 ÷ √dk is folded into the score's coefficients once, and under Causal the masked terms are simply not emitted – there is no −∞ and no comparison anywhere.
⚠ The emitted body grows as T²·dk terms. That is inherent to unrolling attention, and it is the cost of having no loops: a window of 4 is a few hundred lines per backend, a window of 32 is tens of thousands. Keep T to what the model actually needs.
The three HDL targets are simulation-only real
arithmetic – the softmax needs exp and a division, neither of
which belongs in a Q16.16 datapath – quantizing only at the port boundary,
exactly as Dense Layer does for its Tanh and Sigmoid activations.
Simulink bridge
None. Simulink's attention layers live inside its Deep Learning blocks,
which take a trained network object rather than three weight matrices, and
no config value can carry an object across the bridge. The bridge reports the
block rather than dropping it silently, and it has no parity testbench,
which is the documented consequence of Support::None rather than a
gap.
Notes
- Algebraic and stateless: the whole window comes from the current input, so nothing is carried between steps. Buffering the window is the upstream block's job.
- The row maximum is subtracted before exponentiating, inherited from Softmax for the same reason: without it a logit a user will really produce overflows in single precision and in the HDL paths. It cancels exactly in the ratio, so it changes the arithmetic's range and not its value.
- Single head. Multi-head attention is this block several times over with different weights and its outputs concatenated – which Matrix Concatenate already does – so the head count is a wiring decision rather than a parameter here.
Code facts#
| Fact | Value |
|---|---|
| registered type | Machine_Learning/Neural_Networks/Scaled_Dot_Product_Attention |
| family | Machine_Learning/Neural_Networks |
| solver environment class | ICoreBlock_0_Machine_Learning_1_Neural_Networks_2_Scaled_Dot_Product_Attention |
| source | src/ICoreSDK/ICoreBlockLibrary/Blocks/Machine_Learning/Neural_Networks/Scaled_Dot_Product_Attention/ICoreBlock_0_Machine_Learning_1_Neural_Networks_2_Scaled_Dot_Product_Attention.cpp |
| header | src/ICoreSDK/ICoreBlockLibrary/Blocks/Machine_Learning/Neural_Networks/Scaled_Dot_Product_Attention/ICoreBlock_0_Machine_Learning_1_Neural_Networks_2_Scaled_Dot_Product_Attention.h |
| default size on canvas | 150 × 90 px |
| ports at insert | 1 in, 1 out |
| code generators implemented | Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text |
Ports#
| # | Direction | Signal type | Description label |
|---|---|---|---|
| 1 | in | ICoreDouble | X |
| 2 | out | ICoreDouble | Y |
Ports the constructor creates. A block whose port list changes with its configuration adds or removes ports at load time; the count above is the one a freshly inserted block has.
Configuration variables#
| Config variable | Default | Simulink parameter |
|---|---|---|
Query Weights | [0.6 -0.2; 0.1 0.5; -0.4 0.3] | — |
Key Weights | [0.3 0.45; -0.5 0.2; 0.25 -0.35] | — |
Value Weights | [0.7 0.15; 0.2 -0.6; -0.1 0.4] | — |
Masking | None%~%Causal~~None | — |
Every block also carries Sampling Time (s) from ICoreBlockSolverEnvironment: zero or less inherits the solver's rate, a positive value runs the block at that period.
Simulink bridge#
| support | Support::None |
| Simulink path | — |
| port-count rule | PortsParam::None |
SampleTime parameter | yes |
Caveat (shown to the user): no Simulink equivalent as a signal block: its attention layers exist only inside the Deep Learning blocks, which take a trained network OBJECT rather than the three weight matrices this block carries as config, and no ParamRule can move an object across the bridge
Catalog contract: src/ICoreSDK/ICoreCoder/ICoreCommandSystem/SimulinkBridge/ICoreSimulinkBlockCatalog.h
Description vs code#
The checker has a blind spot here — it could not resolve something (a grouped port bullet, a computed config name), which is reported and never counted as a pass. A reader has to settle it:
B0every stimulus in the sample errored — cross-checks skipped
The verdict above is
tools/docs/check_block_descriptions.py(P7.1), which compares LISTS. It cannot read a sentence: "stateless" on a block with a state, an initial-value semantic the recursion does not implement, a "not synthesizable" caveat the HDL banner contradicts. That is the agent audit (P7.3) on BLOCK_DESCRIPTION_AUDIT.md, and this tool's green is not a substitute for one.
File banner (developer view)#
The top comment of the block's .cpp — the maths, the realization and the export strategy, addressed to whoever changes it. It must not contradict the description above (P7.5).
Scaled Dot-Product Attention — single head over a [T, d_model] window Q = X*Wq K = X*Wk V = X*Wv S = Q*K' / sqrt(d_k) A = softmax(S) row-wise (the row max is subtracted -- Softmax's rule) Y = A*V [T, d_v]
Stateless: the window arrives as a matrix rather than being buffered here, so Positional_Encoding -- which exists precisely so this block is not order-blind -- feeds straight in. Everything is unrolled at export time, and the causal mask is expressed by NOT EMITTING the masked terms rather than by a runtime comparison.
The three HDL targets are simulation-only
real: this needs exp() and a division.
Sample results#
No stimulus produced a sampled output in this rig — Invalid "Query Weights" at: ICore Blocks/Home/Scaled Dot Product Attention. That is a fact about the single-block rig, not a verdict on the block: an offline batch fit, a block whose output only appears at onSolverFinish, or one that needs a driven environment cannot be exercised alone.
Category unsampled · sample time 0.1 · 60 steps · commit ccf005c8 · produced by docsSample --out <folder> --steps 60
Sample data: docs/generated/samples/Machine_Learning__Neural_Networks__Scaled_Dot_Product_Attention.json