跳转至

Hyper-Connection Operators

本页暂无中文版。以下为英文原文。

Every op on this page is used the same way: construct it once, then call it. The constructor takes what the kernel is compiled with; the call takes the tensors. Both are documented under each op — __init__ and forward, where forward is what runs when you call op(...).

Manifold-Constrained Hyper-Connections

tileops.sequence_modeling.HyperConnectionPreFwdOp

The pre step of a manifold hyper-connection, as DeepSeek-V4-Flash Block.hc_pre runs it.

Per token, x is the n streams of residual flattened to n * C values in float32, and

\[ m = (x\, fn^T) \cdot \mathrm{rsqrt}(\mathrm{mean}(x^2) + \epsilon_{rms}) \]

splits into n pre, n post and n * n comb logits, each times its hc_scale entry plus hc_base. pre = sigmoid + hc_pre_eps and post_mix = sigmoid * hc_post_mult_value. comb_mix is a row softmax plus hc_sinkhorn_eps, then a column normalization and sinkhorn_repeat - 1 row-then-column normalizations, each dividing by the sum plus hc_sinkhorn_eps. layer_input = sum_i pre_i * residual_i, rounded once to bfloat16.

The projection is as accurate as a float32 matrix product: fn takes part as three bfloat16 terms whose sum is its float32 value. A residual whose stream axis has stride 0, one stream broadcast to all n as the first block's is, is read as that one stream.

__init__

__init__(
    rms_eps,
    hc_pre_eps,
    hc_sinkhorn_eps,
    hc_post_mult_value,
    sinkhorn_repeat,
    *,
    target=None
)

Build the op. Shapes are taken from each call.

Parameters:

  • rms_eps (float) –

    Added to the mean square before the root.

  • hc_pre_eps (float) –

    Added to each pre weight.

  • hc_sinkhorn_eps (float) –

    Added to the softmax and to each normalization's divisor.

  • hc_post_mult_value (float) –

    Multiplies each post weight.

  • sinkhorn_repeat (int) –

    Sinkhorn iterations, the first one's row step being the softmax.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    residual,
    fn,
    hc_scale,
    hc_base,
)

Mix the streams of each token down to its block input.

Parameters:

  • residual (Tensor) –

    (*S, n, C) bfloat16 streams.

  • fn (Tensor) –

    (n * n + 2 * n, n * C) float32 projection.

  • hc_scale (Tensor) –

    (3,) float32 scales of the pre, post and comb logits.

  • hc_base (Tensor) –

    (n * n + 2 * n,) float32 biases of the logits.

Returns:

  • Tensor –

    post_mix (*S, n, 1) float32, comb_mix (*S, n, n) float32 and

  • Tensor –

    layer_input (*S, C) bfloat16.

tileops.sequence_modeling.HyperConnectionPostFwdOp

Mix a block's output back into the n residual streams of a manifold hyper-connection.

Per token, output stream j is \(post\_layer\_mix_j \cdot x + \sum_i comb\_res\_mix_{i, j} \cdot residual_i\), summed in float32 and rounded once to bfloat16. This is DeepSeek-V4-Flash inference/model.py Block.hc_post after each attention and MoE block, with post_layer_mix and comb_res_mix the post and comb the pre step produced; comb_res_mix is read by column, comb_res_mix[i, j] weighting input stream i in output stream j.

The mixes are float32 or bfloat16; GLM-5.3-Flash rounds them to bfloat16 before mixing, and passing them so gives its mix computed in float32. Elements are summed in another order than a reference summing the streams first, so an output can differ from it by one bfloat16 rounding.

__init__

__init__(
    *,
    target=None
)

Build the op; shapes and dtypes arrive with each call. A subclass with manifest parameters assigns them first and then calls this.

It selects and builds no kernel and reads no device property: an op constructs wherever it is imported, and a device it cannot run on is refused at the first call.

Parameters:

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

Raises:

  • ValueError –

    A manifest parameter is outside its signature, or an implementation breaks its kernel interface's contract.

  • TypeError –

    An implementation breaks its kernel interface's contract.

forward

forward(
    x,
    residual,
    post_layer_mix,
    comb_res_mix,
)

Mix x into the residual streams.

Parameters:

  • x (Tensor) –

    The block output, \([*S \times C]\) bfloat16.

  • residual (Tensor) –

    The n streams the block read, \([*S \times n \times C]\) bfloat16.

  • post_layer_mix (Tensor) –

    Each output stream's weight of x, \([*S \times n \times 1]\), float32 or bfloat16.

  • comb_res_mix (Tensor) –

    Input stream i's weight in output stream j at [i, j], \([*S \times n \times n]\), in post_layer_mix's dtype.

Returns:

  • Tensor –

    The n new streams, \([*S \times n \times C]\) bfloat16.

Example
1
2
3
4
5
6
7
8
import torch
from tileops.sequence_modeling import HyperConnectionPostFwdOp

x = torch.randn(4096, 4096, dtype=torch.bfloat16, device="cuda")
residual = torch.randn(4096, 4, 4096, dtype=torch.bfloat16, device="cuda")
post = torch.rand(4096, 4, 1, device="cuda") * 2
comb = torch.softmax(torch.randn(4096, 4, 4, device="cuda"), dim=-1)
output = HyperConnectionPostFwdOp()(x, residual, post, comb)

tileops.sequence_modeling.HyperConnectionHeadFwdOp

Collapse the n residual streams of a manifold hyper-connection into one, before the LM head.

Per token, with x the n streams flattened to n * C float32 values, \(pre = \sigma((x \cdot hc\_fn^T) / \sqrt{mean(x^2) + rms\_norm\_eps} \cdot hc\_scale + hc\_base) + hc\_eps\) and the output is \(\sum_i pre_i \cdot hidden\_states_i\), summed in float32 and rounded once to bfloat16. This is DeepSeek-V4-Flash inference/model.py ParallelHead.hc_head; GLM-5.3-Flash differs in its rms_norm_eps. The dots and square sum are reduced in another order than the reference's matrix product, so an output can differ from it by one bfloat16 rounding.

__init__

__init__(
    rms_norm_eps,
    hc_eps,
    *,
    target=None
)

Build the op. Shapes are taken from each call.

Parameters:

  • rms_norm_eps (float) –

    Added to the mean square of a token's streams before the root.

  • hc_eps (float) –

    Added to each stream's weight after the sigmoid.

  • target (Target, default: None ) –

    Which set of kernels serves this op — a target name, BUILTIN for the in-tree kernels, or None to decide from the input device.

forward

forward(
    hidden_states,
    hc_fn,
    hc_scale,
    hc_base,
)

Collapse each token's streams.

Parameters:

  • hidden_states (Tensor) –

    The n streams, \([*S \times n \times C]\) bfloat16.

  • hc_fn (Tensor) –

    The projection giving each stream's weight logit, \([n \times n C]\) float32.

  • hc_scale (Tensor) –

    The logits' scale, \([1]\) float32.

  • hc_base (Tensor) –

    Each stream's logit bias, \([n]\) float32.

Returns:

  • Tensor –

    The collapsed stream, \([*S \times C]\) bfloat16.

Example
1
2
3
4
5
6
7
8
9
import torch
from tileops.sequence_modeling import HyperConnectionHeadFwdOp

hidden_states = torch.randn(64, 4, 4096, dtype=torch.bfloat16, device="cuda")
hc_fn = torch.randn(4, 4 * 4096, device="cuda") / 128
hc_scale = torch.ones(1, device="cuda")
hc_base = torch.zeros(4, device="cuda")
op = HyperConnectionHeadFwdOp(rms_norm_eps=1e-6, hc_eps=1e-6)
output = op(hidden_states, hc_fn, hc_scale, hc_base)