include_preview_kernels.h#
-
namespace onnx_light
Alias that makes onnx-light headers compatible with code that references
ONNX_LIGHT_NAMESPACE(the macro used in the standard onnx package).Set to
ONNX_LIGHT_NAMESPACEso both names resolve to the same namespace.Symbol-visibility attribute for the public onnx-light C++ API.
Defined as empty because onnx-light does not require explicit
__declspec(dllexport)or__attribute__((visibility("default")))annotations — visibility is controlled at the shared-library level. The macro is provided so that vendored ONNX headers that decorate their declarations withONNX_APIcompile without modification.Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal
onnxnamespace — rather than theONNX_NAMESPACEmacro — resolves to the onnx-light namespace. The standard onnx package lives innamespace onnx; onnx-light usesonnx_light(via ONNX_LIGHT_NAMESPACE), so this alias keeps onnx-light a true drop-in. It is only introduced when the onnx-light namespace differs fromonnx.-
namespace onnx_kernels
-
namespace kernel
-
class FlexAttention : public onnx_light::onnx_kernels::kernel::KernelBase#
- #include <include_preview_kernels.h>
Reference implementation of
ai.onnx.preview::FlexAttention(v1).Computes
Y = Softmax((Q @ K^T) * scale, axis=-1) @ Vover rank-4 (batched, multi-head) floating-point inputs (FLOAT or DOUBLE; FLOAT16 and BFLOAT16 are supported via internal promotion to FLOAT32). Supports Grouped Query Attention (GQA): whenq_num_heads != kv_num_headseach K/V head is shared by a contiguous group of query heads, i.e. query headhuses K/V headfloor(h / (q_num_heads / kv_num_heads));q_num_headsmust be a multiple ofkv_num_heads.The optional
score_modmodifier subgraph is supported via theScoreModFncallback overload: callers that have a way to evaluate thescore_modsubgraph (e.g. a graph-executor) can pass a callable that receives the pre-softmax score tensor of shape(batch_size, q_num_heads, q_seq_len, kv_seq_len)and rewrites it in place; the kernel then runsSoftmaxon the rewritten scores before the finalprobs @ Vmatmul. Overloads without a callback retain the baseline behavior (score_modtreated as identity).The optional
prob_modmodifier subgraph is supported via theProbModFncallback overload: callers that have a way to evaluate theprob_modsubgraph (e.g. a graph-executor) can pass a callable that receives the post-softmax probability tensor of shape(batch_size, q_num_heads, q_seq_len, kv_seq_len)and rewrites it in place; the kernel then uses the rewritten probabilities for the finalprobs @ Vmatmul. Overloads without a callback retain the baseline behavior (prob_modtreated as identity).FLOAT and DOUBLE tensors are supported (and FLOAT16/BFLOAT16 via internal promotion to FLOAT32).
Q,KandVmust share the same element type.Public Types
-
using ScoreModFn = std::function<void(Tensor&)>#
Callback used to apply the
score_modmodifier subgraph to the pre-softmax score tensor of shape(batch_size, q_num_heads, q_seq_len, kv_seq_len). The callback receives a mutable reference to a tensor sharingQ’s element type and must rewrite its contents in place while preservingdata_typeandshape; the kernel validates these invariants after the call.
-
using ProbModFn = std::function<void(Tensor&)>#
Callback used to apply the
prob_modmodifier subgraph to the post-softmax probability tensor of shape(batch_size, q_num_heads, q_seq_len, kv_seq_len). The callback receives a mutable reference to a tensor sharingQ’s element type and must rewrite its contents in place while preservingdata_typeandshape; the kernel validates these invariants after the call.
Public Functions
-
Tensor operator()(const Tensor &Q, const Tensor &K, const Tensor &V) const#
Computes the attention output for the given Q, K, V tensors using the default scaling factor
1 / sqrt(head_size).
-
Tensor operator()(const Tensor &Q, const Tensor &K, const Tensor &V, float scale) const#
Computes the attention output for the given Q, K, V tensors using an explicit
scalevalue (matching thescaleattribute of the operator).
-
Tensor operator()(const Tensor &Q, const Tensor &K, const Tensor &V, float scale, const ProbModFn &prob_mod) const#
Computes the attention output and applies the
prob_modcallback to the post-softmax probability tensor before computingY = probs @ V. Whenprob_modis an emptystd::functionthis is equivalent to the overload without aprob_modcallback.
-
Tensor operator()(const Tensor &Q, const Tensor &K, const Tensor &V, float scale, const ScoreModFn &score_mod, const ProbModFn &prob_mod) const#
Computes the attention output and applies the
score_modcallback to the pre-softmax score tensor before the softmax, and theprob_modcallback to the post-softmax probability tensor before computingY = probs @ V. When either callback is an emptystd::functionit is treated as identity.
-
void operator()(const Tensor &Q, const Tensor &K, const Tensor &V, float scale, Tensor &output) const#
In-place overload writing into a caller-allocated
outputtensor.outputmust already shareQ’s element type and have shape(batch_size, q_num_heads, q_seq_len, v_head_size)with itsdatabuffer sized to match.
-
void operator()(const Tensor &Q, const Tensor &K, const Tensor &V, float scale, const ProbModFn &prob_mod, Tensor &output) const#
In-place overload with
prob_modsupport. See the returning overload taking aProbModFnfor the semantics ofprob_mod;outputhas the same preconditions as the in-place overload without a callback.
-
void operator()(const Tensor &Q, const Tensor &K, const Tensor &V, float scale, const ScoreModFn &score_mod, const ProbModFn &prob_mod, Tensor &output) const#
In-place overload with
score_modandprob_modsupport. See the returning overloads for the semantics of the callbacks;outputhas the same preconditions as the in-place overload without a callback.
-
inline explicit KernelBase(const KernelContext &ctx)#
Public Static Functions
-
static inline constexpr bool CanRunInPlace() noexcept#
FlexAttention computes a fresh output buffer from independent reads of Q, K, V and never aliases an input buffer.
-
using ScoreModFn = std::function<void(Tensor&)>#
-
class FlexAttention : public onnx_light::onnx_kernels::kernel::KernelBase#
-
namespace kernel
-
namespace onnx_kernels