Class DeepSeekOps
java.lang.Object
smile.llm.model.deepseek.DeepSeekOps
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 TypeMethodDescriptionstatic TensordequantFp4(Tensor w, Tensor scale) Dequantizes an FP4 (e2m1) weight with per-32 e8m0 scales along K.static TensordequantFp8(Tensor w, Tensor scale) Dequantizes a block-FP8 weight with 32x32 e8m0 scales.static Tensore8m0ToFloat(Tensor e8m0) Converts e8m0 scale bytes to float32 (2^(byte - 127)).static TensorCasts to float32.static TensorfakeQuantFp4(Tensor x, int block, boolean e4m3Scale) Block-wise fp4 fake quantization (the referencefp4_act_quantwithinplace=True).static TensorfakeQuantFp8(Tensor x, int block) Block-wise fp8 e4m3 fake quantization with power-of-two scales (the referenceact_quantwithinplace=True): values go through fp8 and back, in the dtype ofx.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 Tensor2^ceil(log2 v)for positive float32 values (the referencefast_round_scale).static TensorprecomputeFreqsCis(int dim, int seqLen, int originalSeqLen, double base, double factor, int betaFast, int betaSlow) Rotary frequencies as unit complex numbers, with YaRN extrapolation whenoriginalSeqLen > 0.static TensorRMS normalizationweight * x / sqrt(mean(x^2) + eps), computed in float32 and cast back.static TensorApplies rotary embedding to adjacent element pairs of the last dimension.static TensorRounds float32 values in [-6, 6] to the nearest e2m1 value, ties to the even code.static TensorsparseAttention(Tensor q, Tensor kv, Tensor sink, Tensor idxs, double scale) Attention over gathered key/value rows with an attention-sink logit per head (the referencesparse_attn); a query with no valid index gets a zero output.static TensorwindowTopkIdxs(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.
-
Method Details
-
f32
-
pow2Ceil
-
fakeQuantFp8
Block-wise fp8 e4m3 fake quantization with power-of-two scales (the referenceact_quantwithinplace=True): values go through fp8 and back, in the dtype ofx.- Parameters:
x- activations, the last dimension a multiple ofblock.block- elements per scale (32).- Returns:
- the fake-quantized tensor.
-
roundFp4
-
fakeQuantFp4
Block-wise fp4 fake quantization (the referencefp4_act_quantwithinplace=True).- Parameters:
x- activations, the last dimension a multiple ofblock.block- elements per scale: 32 with power-of-two scales (indexer), 16 with e4m3 scales (compressed KV).e4m3Scale-truefor e4m3 scales,falsefor power-of-two scales.- Returns:
- the fake-quantized tensor, in the dtype of
x.
-
e8m0ToFloat
-
dequantFp8
-
dequantFp4
-
rmsNorm
-
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 whenoriginalSeqLen > 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
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
Attention over gathered key/value rows with an attention-sink logit per head (the referencesparse_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 intokv[1, m, topk],-1for none.scale- softmax scale.- Returns:
- the output
[1, m, h, d]in bf16.
-
windowTopkIdxs
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],-1for slots holding nothing.
-