Class DeepSeekOps

java.lang.Object
smile.llm.model.deepseek.DeepSeekOps

public final class DeepSeekOps extends Object
Tensor operations of the DeepSeek-V4.1 forward pass, written with generic ATen ops (Aten) so they run on any device. They follow the formulas of the reference kernels (act_quant, fp4_act_quant, hc_split_sinkhorn, sparse_attn, ...) and are checked against a pure-PyTorch port of them.

Quantized weights are dequantized to float32 and multiplied with fake-quantized activations: the products are exact, only the fp32 accumulation order differs from the fused kernels.

  • Method Summary

    Modifier and Type
    Method
    Description
    static Tensor
    Dequantizes an FP4 (e2m1) weight with per-32 e8m0 scales along K.
    static Tensor
    Dequantizes a block-FP8 weight with 32x32 e8m0 scales.
    static Tensor
    Converts e8m0 scale bytes to float32 (2^(byte - 127)).
    static Tensor
    Casts to float32.
    static Tensor
    fakeQuantFp4(Tensor x, int block, boolean e4m3Scale)
    Block-wise fp4 fake quantization (the reference fp4_act_quant with inplace=True).
    static Tensor
    fakeQuantFp8(Tensor x, int block)
    Block-wise fp8 e4m3 fake quantization with power-of-two scales (the reference act_quant with inplace=True): values go through fp8 and back, in the dtype of x.
    static Tensor[]
    hcSplitSinkhorn(Tensor mixes, Tensor scale, Tensor base, int hc, int iters, double eps)
    The Hyper-Connections coefficients from the projected stream: sigmoid pre/post weights and a doubly stochastic (Sinkhorn) combination matrix.
    static Tensor
    2^ceil(log2 v) for positive float32 values (the reference fast_round_scale).
    static Tensor
    precomputeFreqsCis(int dim, int seqLen, int originalSeqLen, double base, double factor, int betaFast, int betaSlow)
    Rotary frequencies as unit complex numbers, with YaRN extrapolation when originalSeqLen > 0.
    static Tensor
    rmsNorm(Tensor x, Tensor weight, double eps)
    RMS normalization weight * x / sqrt(mean(x^2) + eps), computed in float32 and cast back.
    static Tensor
    rope(Tensor x, Tensor freqsCis, boolean inverse)
    Applies rotary embedding to adjacent element pairs of the last dimension.
    static Tensor
    Rounds float32 values in [-6, 6] to the nearest e2m1 value, ties to the even code.
    static Tensor
    sparseAttention(Tensor q, Tensor kv, Tensor sink, Tensor idxs, double scale)
    Attention over gathered key/value rows with an attention-sink logit per head (the reference sparse_attn); a query with no valid index gets a zero output.
    static Tensor
    windowTopkIdxs(int window, int ring, int seqLen, int startPos)
    The sliding-window slots each query attends to, for prefill (one row per query) or one decode step.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • f32

      public static Tensor f32(Tensor x)
      Casts to float32.
      Parameters:
      x - a tensor.
      Returns:
      x as float32.
    • pow2Ceil

      public static Tensor pow2Ceil(Tensor v)
      2^ceil(log2 v) for positive float32 values (the reference fast_round_scale).
      Parameters:
      v - positive float32 values.
      Returns:
      powers of two.
    • fakeQuantFp8

      public static Tensor fakeQuantFp8(Tensor x, int block)
      Block-wise fp8 e4m3 fake quantization with power-of-two scales (the reference act_quant with inplace=True): values go through fp8 and back, in the dtype of x.
      Parameters:
      x - activations, the last dimension a multiple of block.
      block - elements per scale (32).
      Returns:
      the fake-quantized tensor.
    • roundFp4

      public static Tensor roundFp4(Tensor v)
      Rounds float32 values in [-6, 6] to the nearest e2m1 value, ties to the even code.
      Parameters:
      v - values in [-6, 6].
      Returns:
      the rounded values.
    • fakeQuantFp4

      public static Tensor fakeQuantFp4(Tensor x, int block, boolean e4m3Scale)
      Block-wise fp4 fake quantization (the reference fp4_act_quant with inplace=True).
      Parameters:
      x - activations, the last dimension a multiple of block.
      block - elements per scale: 32 with power-of-two scales (indexer), 16 with e4m3 scales (compressed KV).
      e4m3Scale - true for e4m3 scales, false for power-of-two scales.
      Returns:
      the fake-quantized tensor, in the dtype of x.
    • e8m0ToFloat

      public static Tensor e8m0ToFloat(Tensor e8m0)
      Converts e8m0 scale bytes to float32 (2^(byte - 127)).
      Parameters:
      e8m0 - the raw bytes as a uint8 tensor.
      Returns:
      float32 scales.
    • dequantFp8

      public static Tensor dequantFp8(Tensor w, Tensor scale)
      Dequantizes a block-FP8 weight with 32x32 e8m0 scales.
      Parameters:
      w - fp8 e4m3 weight [N, K].
      scale - e8m0 bytes [ceil(N/32), ceil(K/32)] (uint8).
      Returns:
      the float32 weight.
    • dequantFp4

      public static Tensor dequantFp4(Tensor w, Tensor scale)
      Dequantizes an FP4 (e2m1) weight with per-32 e8m0 scales along K.
      Parameters:
      w - packed weight [N, K/2] (int8 or uint8 bytes, the low nibble is the even element).
      scale - e8m0 bytes [N, K/32] (uint8).
      Returns:
      the float32 weight [N, K].
    • rmsNorm

      public static Tensor rmsNorm(Tensor x, Tensor weight, double eps)
      RMS normalization weight * x / sqrt(mean(x^2) + eps), computed in float32 and cast back.
      Parameters:
      x - the input.
      weight - the scale.
      eps - epsilon (1e-20 in the released model).
      Returns:
      the normalized tensor in the dtype of x.
    • precomputeFreqsCis

      public static Tensor precomputeFreqsCis(int dim, int seqLen, int originalSeqLen, double base, double factor, int betaFast, int betaSlow)
      Rotary frequencies as unit complex numbers, with YaRN extrapolation when originalSeqLen > 0.
      Parameters:
      dim - rotated dimension.
      seqLen - number of positions.
      originalSeqLen - YaRN original context (0 disables YaRN).
      base - RoPE base.
      factor - YaRN factor.
      betaFast - YaRN beta fast.
      betaSlow - YaRN beta slow.
      Returns:
      complex64 [seqLen, dim/2].
    • rope

      public static Tensor rope(Tensor x, Tensor freqsCis, boolean inverse)
      Applies rotary embedding to adjacent element pairs of the last dimension.
      Parameters:
      x - [b, s, d] or [b, s, h, d].
      freqsCis - complex [s, d/2].
      inverse - conjugate the rotation (removes a previous rotation).
      Returns:
      the rotated tensor in the dtype of x.
    • hcSplitSinkhorn

      public static Tensor[] hcSplitSinkhorn(Tensor mixes, Tensor scale, Tensor base, int hc, int iters, double eps)
      The Hyper-Connections coefficients from the projected stream: sigmoid pre/post weights and a doubly stochastic (Sinkhorn) combination matrix.
      Parameters:
      mixes - [..., (2 + hc) * hc] float32.
      scale - [3] float32.
      base - [(2 + hc) * hc] float32.
      hc - residual copies.
      iters - Sinkhorn iterations.
      eps - epsilon.
      Returns:
      {pre [..., hc], post [..., hc], comb [..., hc, hc]}.
    • sparseAttention

      public static Tensor sparseAttention(Tensor q, Tensor kv, Tensor sink, Tensor idxs, double scale)
      Attention over gathered key/value rows with an attention-sink logit per head (the reference sparse_attn); a query with no valid index gets a zero output.
      Parameters:
      q - queries [1, m, h, d].
      kv - shared keys/values [1, n, d].
      sink - sink logits [h] float32.
      idxs - int32 indices into kv [1, m, topk], -1 for none.
      scale - softmax scale.
      Returns:
      the output [1, m, h, d] in bf16.
    • windowTopkIdxs

      public static Tensor windowTopkIdxs(int window, int ring, int seqLen, int startPos)
      The sliding-window slots each query attends to, for prefill (one row per query) or one decode step.
      Parameters:
      window - window size.
      ring - slots of the window ring (decode).
      seqLen - number of queries (1 for decode).
      startPos - position of the first query.
      Returns:
      int32 [1, seqLen, width], -1 for slots holding nothing.