Neural API Examples
June 28, 2026 · View on GitHub
Start here — smallest demos
- Only two layers (XOR) — trains a neural network containing only two layers to learn the XOR boolean operation, about the smallest non-linearly-separable demo in the tree. Pure CPU, instant.
- Sine regression — smallest possible "does the library still train?" demo: a two-layer MLP fits
y = sin(pi*x)onx in [-1, 1]with hand-rolled mini-batch SGD; prints periodic MSE and an 11-point predicted-vs-truth table. - Binary adder — tiny "learns to add two binary numbers" demo: a small MLP learns 4-bit + 4-bit addition over all 256 input combinations (5-bit sigmoid output for carry), trains in a few seconds, reaches 100% exact-bit accuracy, and prints sample additions in binary.
- Self-test — runs the library's built-in correctness self-tests:
TestTNNetVolume()(the volume/tensor API),TestKMeans(), andTestConvolutionAPI(). A quick "is my build of the library sane?" smoke check. - Delphi template — minimal Delphi FMX GUI project (
NeuralInDelphi) showing the library compiling and running under Delphi/FireMonkey rather than only FPC/Lazarus — a starting skeleton for embedding neural-api in a Delphi desktop app. - Model-summary demo (
PrintSummarysmoke test) — constructs three structurally-distinct nets (a small MLP, a CIFAR-style conv stack, and a pre-norm residual block viaTNNet.AddPreNormResidual) and prints each viaTNNet.PrintSummary/SummaryString(the Keras-styleIdx / Layer / Output Shape / Params / Neuronstable with aTotals:footer). It doubles as a format smoke test: it parses each summary string and asserts the header is present, the body has exactlyCountLayers()rows, the per-row Params/Neurons sum toCountWeights()/CountNeurons(), and the footer totals match —Halt(1)on any mismatch. No training. Pure CPU, deterministic, < 1 s. - Euclidean-norm-reciprocal head (layer composition) — a teaching demo that the in-tree elementwise transcendental layers compose into a Euclidean-norm head:
Input(1,1,8) -> TNNetSquare -> TNNetFullConnectLinear(1)[all-ones, frozen sum] -> TNNetSqrt -> TNNetReciprocalcomputes1/||x||_2exactly (an all-ones frozen FC is used for thesum_i x_i^2reduction so there is noN^2pool-scaling to undo). Self-checks (Halt(1)on failure) over 200 random vectors: forward matches the analytic1/sqrt(sum x_i^2)to0float32 error, a unit vector returns1.0, thex*(1/||x||)L2-normalize extension has norm1within1.2e-7, and gradients flow through the stack without NaN. The README contrasts the composition with the dedicatedTNNetL2Normalize(which ships the exact Jacobian and an eps guard). Pure CPU, deterministic, < 1 s. - Involution demo — shows that
TNNetReverseChannels,TNNetReverseXY,TNNetFlipX, andTNNetFlipYare involutions: applying each twice round-trips to the input exactly (per-element). - Seeded reproducibility — trains a tiny MLP twice with the same
RandSeedandMaxThreadNum := 1, snapshots every weight and bias, and prints PASS/FAIL on bit-for-bit equality (non-zero exit code on FAIL — CI-friendly). - Quick-Start Sequence — "hello world" for sequence learning — the shortest possible "it learns a sequence" demo: a tiny 4-layer char-level next-token model (
Input(2*10) -> FullConnectReLU(16) -> FullConnectLinear(10) -> SoftMax, a few hundred weights) learns a fixed counting sequence0,1,2,...,9,0,1,...from its last two symbols, trained with a plain hand-written SGD loop (NN.Compute+NN.Backpropagate, noTNeuralFit, no threads, no data files). Seeded with01it regenerates three full cycles by feeding its own predictions back in (100% next-symbol accuracy). Pure CPU, ~1 s — a beginner-friendly counterpart to the full transformer in SimpleNLP.
Activations & gated feed-forwards
- PReLU vs LeakyReLU vs ReLU — three-config activation comparison on the toy hypotenuse regression task:
TNNetReLU(no negative leak),TNNetLeakyReLU(fixed slope0.01), andTNNetPReLU(single learnable scalar slope shared across all elements, He et al. 2015, initalpha = 0.25). Pure CPU. - Mainstream activation bake-off on the hypotenuse toy — companion to the hyperbolic bake-off comparing 15 ReLU-family activations (
TNNetReLU,TNNetLeakyReLU,TNNetVeryLeakyReLU,TNNetReLU6,TNNetPReLU,TNNetELU,TNNetSELU,TNNetCELU,TNNetSwish,TNNetSiLU,TNNetGELU,TNNetHardSwish,TNNetMish,TNNetSoftPlus,TNNetAconC) on the same tiny MLP / RNG seed and prints a CSV of final MSE and epochs-to-converge. - Hyperbolic activation bake-off on the hypotenuse toy — compares 8 hyperbolic-family activations (
TNNetHyperbolicTangent,TNNetLeCunTanh,TNNetSinhAct,TNNetArcSinh,TNNetLisht,TNNetBentIdentity,TNNetTanhExp,TNNetLogCoshActivation) on the same tiny MLP / RNG seed and prints a CSV of final MSE and epochs-to-converge. - APL activation bake-off — does the extra piecewise capacity of the Adaptive Piecewise Linear activation
TNNetAPL(Agostinelli et al. 2015) buy a lower final loss than plainTNNetReLUandTNNetPReLU? Trains all three on the same hypotenuse toy at matched seed/data and sweeps the number of per-channel learnable hingesSto chart capacity vs final MSE. Pure CPU. - SwiGLU feed-forward — tiny
Dense(2*D) → TNNetSwiGLU → Dense(1)block trained on a synthetic regression task; demonstrates the gated-FFN half of a transformer encoder using existing layers only. - GLU feed-forward — the same gated-FFN block built with
NN.AddGLUFeedForward(D_in, D_hidden, D_out), using the plain sigmoid-gatedTNNetGLU(GLU(a, b) = a * sigmoid(b)); sibling of the SwiGLU and GEGLU feed-forward builders. - GEGLU feed-forward — smallest possible end-to-end demo of the transformer feed-forward block: a
Dense -> TNNetGEGLU -> Densesandwich trained on a synthetic regression target (no attention, no embedding, no positional encoding), the canonical "GEGLU FFN" (Shazeer 2020) shrunk to CPU-friendly size so the gated activation is visible in isolation. Sibling of the GLU/SwiGLU feed-forward demos. - Gated-FFN bake-off — head-to-head comparison of all five in-tree gated feed-forward layers (
TNNetGLU/TNNetReGLU/TNNetGEGLU/TNNetSwiGLU/TNNetTanhGLU) on ONE synthetic per-position sequence-regression task. The same FFN block (PointwiseConvLinear(2*d_ff) -> GATE -> PointwiseConvLinear(1)) is built five times, identical except the gate — the gates are parameter-free so all arms have identical parameter counts — and trained at matched seed/LR/epochs/data. Prints a comparison table (initial vs final MSE, wall-clock seconds, epochs-to-converge) plus two built-in sanity checks (no NaN/Inf, every arm beats its pre-training baseline). The README notes the ranking is seed-dependent — the value is the apples-to-apples harness, not a universal winner. Pure CPU, ~4 s. - KAN-vs-MLP toy fit — puts the headline KAN claim (Liu et al. 2024) on a wiggly 1D regression
y = sin(3x) + 0.3*sin(11x): the same tiny MLP with its ReLUs swapped for the per-channel learnableTNNetSplineActivation, at a parameter count matched against a wider ReLU arm (TNNet.CountWeights: 21 vs 20). The spline arm reaches ~67% lower MSE and the example dumps the learned per-channel control points + sampled activation, showing one channel bent away from the identity that an untrained spline starts at (a sparse KAN fit). Pure CPU, a few seconds. - KAN dense layer vs ReLU-MLP toy fit — the WEIGHT-space sibling of SplineActivationKAN: uses the true Kolmogorov-Arnold dense layer
TNNetKANLayer(Liu et al. 2024), where every input→output edge carries its own learned Chebyshev univariate functionphi_{ij}(x)=sum_k c_{ijk} T_k(tanh(x))andy_j=sum_i phi_{ij}(x_i)(no weight matrix). On the same wiggly 1D fity = sin(3x) + 0.3*sin(11x), a KANLayer(8,K=4) arm is param-matched against a wider ReLU arm (TNNet.CountWeights: an exact 48 vs 48) and reaches ~27% lower MSE. Pure CPU, under a second. - KAN convolution per-edge nonlinear bake-off — the CONVOLUTIONAL sibling of KANLayer: the new
TNNetKANConvreplaces a convolution's linear dot product over each receptive-field patch with a sum of learned univariate Chebyshev edge functions (one per kernel-position × input-channel),y_filter(x,y)=sum_p phi_p(input_p),phi_p(t)=sum_k c_{p,k} T_k(tanh(t)), with NO output bias (thec_0term plays that role) — reusing the exact Chebyshev forward/backward ofTNNetKANLayerover a sliding window with the usual padding/stride plumbing inherited fromTNNetConvolutionLinear. On a synthetic 1-channel task whose target pixel is a per-3×3-window NONLINEAR map (y=sum_p a_p·f_p(neighbour_p), eachf_pa distinct sin/tanh/square/cube/abs), aTNNetKANConv(1,3,1,1,K=4)arm (45 weights) reaches val-MSE ≈ 0.0006 while a comparable linear-conv baseline4→1(40 weights) plateaus around 0.05 — the per-edge learnable nonlinearity directly fits what a linear conv can only linearise. Gradient-checked (input + every coefficient) and serialization-round-tripped. Pure CPU, well under a minute. - Spline knot-count sweep — a capacity study follow-up to SplineActivationKAN: the per-channel learnable
TNNetSplineActivation(KAN-style piecewise-linear activation, Liu et al. 2024) can fit a wiggly 1D target, so this asks the obvious next question — how many knots do you need, and what does extra capacity (more knots / wider range) actually buy? Pure CPU. - SIREN (periodic-activation 1D fit) — the headline Sitzmann et al. 2020 SIREN result from existing layers only: the SAME small coordinate-MLP (
Input(1) -> [FullConnectLinear(24) -> act]x3 -> FullConnectLinear(1)) fits a high-frequency targety = sin(3x) + 0.3*sin(11x)once withTNNetSinactivations and once with aTNNetHyperbolicTangentbaseline at matched width/depth/seed/epochs. The SIREN-specific init is reproduced by hand viaTNNetLayer.InitUniform(first layer folds theomega_0=12frequency into the weights, later sine-feeding layers usesqrt(6/fan_in)/omega_0). The sine arm reaches ~100%lower MSE (0.000016vs0.053), demonstrating that periodic activations escape the spectral bias that limits the tanh/ReLU MLP. Self-gating (Halt(1)on failure), pure CPU, deterministic, ~12 s. - Gumbel-softmax demo — demonstrates
TNNetGumbelSoftmax, a differentiable categorical sampling head computingy = softmax((logits + g)/tau)withg ~ Gumbel(0,1). Part (a) sweeps the temperaturetau ∈ {2.0, 1.0, 0.5, 0.1}on the deterministic (no-noise) inference path and prints the resulting softmax distribution plus its Shannon entropy (astaushrinks the distribution sharpens toward one-hot, entropy falls); part (b) shows hard straight-through mode emitting a one-hot output. Pure CPU. - Gumbel-Softmax temperature annealing autoencoder — a temperature-annealing micro-experiment for the
TNNetGumbelSoftmaxbottleneck (distinct from GumbelSoftmaxDemo, which only sweepstauon a fixed logit vector at inference). Here a tiny discrete-latent autoencoder (encoder MLP →K=6logits →TNNetGumbelSoftmax→ decoder MLP) is TRAINED end-to-end on a genuinelyK-category-structured synthetic set (6 well-separated cluster prototypes + noise) whiletauis ANNEALED2.0→1.0→0.5→0.25→0.1across phases. The net is built ONCE andtauis lowered IN-PLACE each phase via the publicTNNetGumbelSoftmax.SetTemperature(tau)setter (no more rebuild +TNNet.CopyWeightsper phase). Headline: a (tau, recon-MSE, mean bottleneck ENTROPY) table with a graded PASS/FAIL verdict — the categorical sharpens astaudrops (entropy collapses0.064→0nats) while reconstruction holds at the~0.01noise floor. Honest caveat (in output + README): on this well-separated dataset confident routing means even the highest-tauentropy starts modest, far belowln(K)=1.79; the verdict checks the trend, not an unrealistically uniform start. Pure CPU, ~2 s.
Losses & output heads
- LogCosh dual experiment — pairs
TNNetLogCoshActivation(hidden) withTNNetLogCoshLoss(output head) vs. a plain MSE head on the hypotenuse toy task; prints a CSV comparing final validation MSE and epochs-to-converge. - Loss-family bake-off (output heads) — head-to-head of robust regression loss heads (
TNNetHuberLoss,TNNetSmoothL1Loss,TNNetCharbonnierLoss,TNNetLogCoshLoss) vs. a plain MSE baseline on a noisy hypotenuse task with injected outliers, at matched trunk/seed/LR/epochs; prints clean-test MSE/MAE per arm and shows the robust heads beating MSE (which over-weights the squared outlier residuals). - Energy heads (L1 vs L2) — side-by-side comparison of two final-feature "energy" heads on a tiny regression task:
TNNetAbs(elementwise|x|, an L1-energy head) vsTNNetSquare(elementwisex^2, an L2-energy head). Both nets share an identical body and training data, so the final test-MSE gap isolates the effect of the energy layer. Pure CPU. - Quantile (pinball) regression + joint multi-quantile head — fits prediction intervals on a heteroscedastic 1-D dataset (
y = sin(x) + 0.3x + N(0, sigma(x)), noise growing with x) with the pinball-loss headTNNetQuantileLoss. Arm 1 trains three independent MLPs (one perq in {0.1, 0.5, 0.9}); arm 2 trains ONE model whose 3-wide output predicts all three quantiles jointly in a single forward pass via the newTNNetMultiQuantileLoss(the same scalar target replicated across channels). Both reach ~80% held-out[q=0.1, q=0.9]band coverage, and the example exercises the non-differentiable inference-time monotonicity guardTNNetMultiQuantileLoss.SortAscending(sorts each N-channel group so q=0.1 never crosses q=0.9), reporting crossing counts with vs without the guard. Pure CPU, ~40 s. - Tversky α/β Sweep - Asymmetry knob study on the
TNNetTverskyLosssegmentation head (Salehi et al. 2017): on a deliberately class-imbalanced mask, sweeps(α,β) ∈ {(0.5,0.5),(0.3,0.7),(0.7,0.3)}and prints a precision/recall/FN table showing howβ>α(penalising false negatives harder) trades precision for recall — recall rises and FN falls monotonically. Pure CPU, ~1s. - Mixture density network — a self-contained pure-CPU reproduction of Bishop's Mixture Density Network (Bishop 1994) on the classic inverse-problem toy, contrasting a plain MSE regressor (which provably collapses to the conditional mean) against an MDN head that recovers the full MULTIMODAL conditional
p(y|x). No new layer — uses existing layers plus hand-rolled gradient surgery for the mixture loss. - Mixture density network (multi-valued inverse map) — the headline "predict a distribution, not a point" win for the new
TNNetMixtureDensityprobabilistic regression head (Bishop 1994, Mixture Density Networks). Data comes from the non-monotonic forward mapx = y + 0.3·sin(2π·y) + noise, so the inversey|xis multi-valued — for manyxthere are several validybranches. Two models share ax → 32 → 32trunk: an MDN headTNNetFullConnectLinear(1,1,K·(1+2·D)) → TNNetMixtureDensity(K=5, D=1)that maps the trunk output to aK-component diagonal-Gaussian mixture (π=softmax mixing weights,μ=raw means,σ=softplus scales), owns the negative-log-likelihood loss (itsBackpropagateemits the exact responsibility-weighteddNLL/dparamvia a numerically-stable log-sum-exp), and exposes aSampleMixtureinference helper; versus a plain MSETNNetFullConnectLinear(1)baseline. The MDN target is read from the firstDchannels of the seeded target volume, and the batch-update deltas are scaled to the mean gradient so no component collapses; the head's mean biases are initialised spread across the target range to break symmetry (the classic MDN mode-collapse pitfall). On the folded region the MDN spreads its active components across the true branches — branch-coverage error ≈0.11 vs ≈0.20 for the single MSE point, which can only sit near one branch and otherwise lands in the gap-filling conditional mean — and drawn samples scatter onto the separate branches. Analytic backward finite-difference verified (TestMixtureDensityGradient). Pure CPU, ~25 s. - Deep evidential regression (closed-form epistemic uncertainty) — the headline "uncertainty from one deterministic pass" win for the new
TNNetEvidentialRegressionhead (Amini et al., NeurIPS 2020, Deep Evidential Regression). Instead of a point estimate, the head emits the 4 parameters of a Normal-Inverse-Gamma prior per scalar target —gamma(mean, linear),nu(>0),alpha(>1),beta(>0) via softplus links over4·Draw channels — and owns the NIG negative-log-likelihood + evidence-regularizer loss (closed-form Student-t marginal using a local LanczoslnGamma/digamma; itsBackpropagateemits the exactdL/d{gamma,nu,alpha,beta}chained through the softplus). From one forward pass it reads off, in closed form,prediction=gamma,aleatoric var=beta/(alpha-1)andepistemic var=beta/(nu·(alpha-1))— distinct fromTNNetMixtureDensity(aleatoric-only mixture) andTNNetKalmanFilterCell(recursive sequential covariance), this is the only head giving a closed-form epistemic estimate with no sampling and no ensemble. The demo learnsf(x)=sin(1.5x)+0.2xfrom a central band[-3,3]with the outer tails held out; a deliberately saturating tanh trunk makes far-OOD inputs fall back to the head's low-evidence (high-epistemic) bias prior rather than linearly extrapolating the NIG params, and thenubias starts low so evidence only grows where data is seen. Headline: mean epistemic variance is ~2× larger in the held-out tails than in-distribution (deepest in the far tail, ~5× the band centre). Analytic backward finite-difference verified (TestEvidentialRegressionGradient). Pure CPU, ~1 min, ~4 MB. - Evidential (Dirichlet) classification (uncertainty out of distribution) — the classification sibling of the above for the new
TNNetEvidentialClassificationhead (Sensoy et al., NeurIPS 2018, Evidential Deep Learning to Quantify Classification Uncertainty). Treats the previous layer'sKraw outputs as evidence for a Dirichlet over the class simplex (alpha_k = 1 + softplus(raw_k)) and reads off, from one deterministic pass,p_k = alpha_k/Sand an uncertainty massu = K/Sin [0,1] (u→1= the network abstains). It owns the EDL loss — the Bayes-risk expected-MSE (Eq. 5) plus alambda·KL-to-uniform regularizer on the misleading-evidence Dirichlet (local LanczoslnGamma/digamma/trigamma; itsBackpropagateemits the exactdL/d(raw)chained through softplus) — and exposesAlpha/Prediction/Uncertainty. The demo trains on two side-by-side upper-half-plane blobs (a geometry that makes the whole lower half-plane provably out-of-distribution, unlike antipodal blobs) with a saturating tanh trunk; headline: mean uncertainty ~5–6× larger out of distribution (ureaches exactly 1.0 below the blobs) than in-distribution (u≈0.15). Analytic backward finite-difference verified (TestEvidentialClassificationGradient). Pure CPU, <1 min.
Dense prediction & detection
- Dice-loss segmentation — a tiny synthetic binary-mask segmentation task (predict the filled disc/box mask from a noisy 16x16 grid) trained two ways at identical fully-convolutional architecture: a
Sigmoid -> TNNetDiceLosshead vs an MSE-head baseline. After training it reports mean Dice and IoU (threshold 0.5) on a held-out set and renders one ASCII (input, ground-truth, Dice-prediction, MSE-prediction) sample — the Dice head reaches markedly higher IoU on the class-imbalanced foreground. Pure CPU, finishes in a few seconds. - U-Net segmentation — trains a real symmetric U-Net built with the reusable
TNNet.AddUNetbuilder (DiceSegmentationis a FLAT fully-convolutional net with no down/upsampling) on a self-contained synthetic-shapes task and reports Dice/IoU on a held-out split.AddUNet(Depth, BaseFeatures, OutputChannels, out EncoderTaps, UseNorm)constructsDepthencoder stages (each2x [Conv3x3 -> (Norm) -> ReLU]then a2x2stride-2TNNetMaxPool, doubling features and recording a skip tap per stage), a bottleneck,Depthdecoder stages (nearestx2upsample viaTNNetDeMaxPool->TNNetDeepConcatwith the matching encoder tap ->2xconv, halving features), and a1x1conv head — and returns the encoder-tap layer indices inEncoderTapsso callers can inspect/re-wire the skips. Output spatial size equals input size (the defining U-Net property), so the 1-channel logit map feeds aSigmoid+TNNetDiceLosshead (the same Dice loss as DiceSegmentation). InputSizeX/SizeYmust each be divisible by2^Depth. Reports per-eval Dice/IoU (threshold 0.5), renders one ASCII (input/ground-truth/prediction) sample and writes a dependency-free PPM strip. Pure CPU; the default SMOKE run (depth 3, ~1 min) reaches Dice ≈ 0.97 / IoU ≈ 0.95, with a--fullmode for longer training. - SegFormer semantic segmentation — the demo for
BuildSegformerFromSafeTensors(neuralpretrained.pas), a semantic-segmentation importer (model_typesegformer: nvidia/segformer-b0-finetuned-ade-512-512 and the MiT-b0…b5 family). SegFormer (Xie et al. 2021) is a hierarchical Mix-Transformer (MiT) encoder — overlap-patch embeddings (strided convs, NOT a CLS token), spatial-reduction efficient attention (the rectangularSeqLen × (SeqLen/sr²)score matrix that makes attention affordable at image resolution), a Mix-FFN with a depthwise positional conv (so the model needs NO positional embedding) — feeding a lightweight all-MLP decode head: a per-stage Linear, bilinear upsample of every stage back to input/4 via the newTNNetBilinearUpsample, a reversed channel concat, a fuse conv with folded BatchNorm, and a classifier that emits a per-pixel logit map at input/4 resolution. The demo loads the committed pico fixture (tests/fixtures/tiny_segformer.safetensors, a tiny MiT-b0-shaped net), runs it on a synthetic image, takes the per-pixel argmax and renders the label map as a colored ASCII palette; a real run loads the nvidia checkpoint the same way and colorizes a photo. Parity is asserted to max |diff| < 1e-4 vs the realtransformersfloat64 forward over the whole logit map (TestSegformerSemanticSegmentationParity, fixture generatortools/make_pico_segformer_fixture.py). Pure CPU, <1 s on the fixture. - DPT / Depth-Anything monocular depth estimation — the demo for
BuildDPTFromSafeTensors(neuralpretrained.pas), a monocular-depth importer producing a dense per-pixel regression (a continuous depth value per pixel, not a class label; model_typedepth_anything/dpt: depth-anything/Depth-Anything-V2-Small-hf and the DPT-hybrid family). Depth Anything (Yang et al. 2024) and DPT (Ranftl et al. 2021) pair a plain ViT / DINOv2 transformer backbone (REUSED — the encoder is built from the existingBuildDINOv2FromSafeTensorspath, tapped at four intermediate stages with a SHARED final LayerNorm) with a convolutional reassemble + fusion neck and a small depth head. The new code is the decoder neck: a reassemble stage that per-stage -projects each hooked feature and resizes it by thereassemble_factors(4,2,1,½) — the up-samplingConvTranspose2d(kernel = stride = factor) is realized as a pointwise expansion feeding the existingTNNetPixelShuffle, the½step is a strided down-conv — building a 4-level pyramid; bias-free neck convs unify the channel count; then RefineNet-style additive fusion blocks (pre-activation residual conv units + the newTNNetBilinearResize, the absolute-target bilinear resize supporting PyTorch'salign_corners=True/False) merge the pyramid coarse-to-fine; and a 3-conv head (with a bilinear upsample to full resolution) emits a single-channel depth map. The demo loads the committed pico fixture (tests/fixtures/tiny_dpt.safetensors, a tiny DINOv2-backbone net), runs it on a synthetic image and renders the depth map as an ASCII grayscale ramp (near = bright, far = dark); a real run loads the Depth-Anything-V2 checkpoint the same way and processes a photo. Parity is asserted to max |diff| < 1e-4 vs the realtransformersfloat64 forward over the whole depth map (TestDPTDepthEstimationParity, fixture generatortools/make_pico_dpt_fixture.py). Pure CPU, <1 s on the fixture. - Depth Anything V2 → normalized depth-map image — the demo for the NAMED
BuildDepthAnythingV2FromSafeTensorsentry point (neuralpretrained.pas; model_typedepth_anything: depth-anything/Depth-Anything-V2-Small/Base/Large-hf). Depth Anything V2 (Yang et al. 2024) IS the DPT dense-prediction stack on a DINOv2 ViT backbone (S/B/L); this importer is the wiring of the landedBuildDINOv2*backbone into the landed DPT reassemble + fusion neck + 3-conv depth head, asserting thedepth_anythingfamily. The wiring honors the backbone'sout_indices(the four SELECTED encoder stages that feed the neck; 1-based stageK= encoder blockK−1, default last-4[N−3..N]) so non-default hooks load correctly. Distinct from the DepthEstimation ASCII-ramp DPT demo: it loads via the named V2 entry point, runs the pico fixture whose backbone hooks non-last-4 stages (out_indices=[2,3,5,6]), and WRITES image files — a min/max-normalized 8-bit grayscale PGM (P5) plus a color PPM (P6) depth visualization (near = warm, far = cool) — alongside an ASCII preview. Default is a SMOKE run on the committed pico fixture (tests/fixtures/tiny_depth_anything_v2.safetensors); pass a.safetensorspath to run a real checkpoint (config beside it). Parity is asserted to max |diff| < 1e-4 vs the realtransformersfloat64DepthAnythingForDepthEstimationforward over the whole depth map (TestDepthAnythingV2Parity, fixture generatortools/make_pico_depthanythingv2_fixture.py). Pure CPU, <1 s on the fixture. - ViTPose human-pose keypoint estimation — the demo for
BuildViTPoseFromSafeTensors(neuralpretrained.pas), a keypoint / human-pose importer with a heatmap output modality (a stack of per-joint 2-D heatmaps, not a class label, box or dense class map; model_typevitpose, backbonevitpose_backbone: usyd-community/vitpose-base-simple and the ViTPose family). ViTPose (Xu et al. 2022) is a top-down single-person estimator: a plain ViT transformer backbone (REUSED — the encoder is the same separate-q/k/v pre-LN block path as theBuildViTFromSafeTensorsimage classifier, over the cropped person image) feeds a small "simple" deconvolution head. The backbone has two ViTPose quirks faithfully reproduced: the patch conv carries a hardcodedpadding=2(forpatch_size ≥ 5the floored output grid still equalsimage // patch), and there is NO class token — each patch token instead getsposition_embeddings[1:]PLUS the broadcast cls-position row[:1], folded into a single learned position table at load. The new code is the patch-grid reshape + the deconvolution head (ReLU → bilinear upsample byscale_factor(the existingTNNetBilinearUpsample,align_corners=False) → conv tonum_labelschannels) emitting one(H·scale, W·scale)heatmap per joint, and the CPU spatial-argmax read-outDecodeViTPoseKeypoints(per-channel max →(x,y)peak + score; a plain helper, NOT a leaf layer). The demo loads the committed pico fixture (tests/fixtures/tiny_vitpose.safetensors, a tiny ViT-backbone net), runs it on a synthetic image, decodes the per-joint peaks and renders them as an ASCII plot (each joint a digit at its argmax) over the heatmap grid; a real run loads the vitpose-base-simple checkpoint the same way and processes a detector person crop. Parity is asserted to max |diff| < 1e-4 vs the realtransformersfloat64 forward over the whole heatmap stack (TestViTPosePoseEstimationParity), the argmax decode is verified against the oracle peaks (TestViTPoseKeypointDecode), fixture generatortools/make_pico_vitpose_fixture.py. Pure CPU, <1 s on the fixture. - DETR object detection (draws boxes) — the demo for
BuildDetrFromSafeTensors(neuralpretrained.pas), an object-detection importer (model_typedetr: facebook/detr-resnet-50). DETR (Carion et al. 2020) pairs a ResNet-50 backbone (conv + folded FrozenBatchNorm) with the DETR transformer encoder-decoder: an input-projection + flatten, a 2-D sinusoidal spatial position embedding (normalize=True, added to the queries+keys but NOT the values of every attention), a fixed set of learned object queries, post-LN encoder/decoder blocks with cross-attention, and a sigmoid-cxcywh box head + class head emitting, for each ofnum_queriesobject slots, a(num_labels + 1 + 4)vector (the class logits with a trailing "no-object" slot, then the normalized cxcywh box). Inference only — there is no Hungarian matcher (training-only). The demo runs a full DETR forward on ONE image, decodes the predictions withDecodeDetrDetections(softmax the class logits, drop the no-object slot, threshold on confidence, convert each normalized cxcywh box to pixel xyxy) and DRAWS the surviving boxes as colored rectangle outlines into the image, writing the annotated result toobject_detection.ppmand printing the(class, score, box)list. It loads the committed pico fixture (tests/fixtures/tiny_detr.safetensors, a tiny randomDetrForObjectDetection) by default so it runs fully OFFLINE on its pinned synthetic image; pass a real facebook/detr-resnet-50 checkpoint + config.json (+ optional confidence threshold) for real detections through the same path. Because the fixture has random weights the "detections" are not meaningful objects — the demo self-reports instead (asserts no NaN/Inf in the output and that every decoded box lands in the sigmoid[0,1]range / in-range pixel coords), exercising the full decode+draw pipeline end to end. Parity of the importer is asserted to max |diff| < 1e-4 vs the realtransformersfloat64 forward over both the class-logits and box tensors (TestDetrObjectDetectionParity), the decode read-out is verified against a manual softmax+argmax (TestDetrDetectionDecode), fixture generatortools/make_pico_detr_fixture.py. Pure CPU, <1 s on the fixture. - YOLOv8 single-shot object detection (draws boxes) — the demo for
BuildYoloFromSafeTensors(neuralpretrained.pas), an anchor-free fully-convolutional one-stage detector (model_typeyolov8: ultralytics yolov8n), structurally distinct from DETR (no transformer, no learned object queries). The importer reuses the conv + folded-BatchNorm loader path (every ultralyticsConv=conv2d(no bias) →BatchNorm2d→SiLU), and the new wiring is: the C2f cross-stage block (a conv → channel split into two halves →nchained Bottlenecks keeping every intermediate →DeepConcat→ ), the SPPF block ( → 3 chainedk5 s1maxpools all kept → concat → ), the PANet feature-pyramid neck (top-down nearest- upsample + concat + C2f, then bottom-up stride-2conv + concat + C2f), and the decoupled DFL detect head over 3 strides (a box branch emitting4*reg_maxdistribution logits + a class branch emittingnum_classeslogits per grid cell). The net outputs the raw head flattened to a(Σ Hᵢ·Wᵢ, 1, 4*reg_max + num_classes)per-cell tensor (the same per-cell layout as DETR's per-query tensor).DecodeYoloDetectionsis the CPU post-process: sigmoid the class logits, DFL-decode each of the 4 box sides (softmax thereg_maxbins → expected distance = the ltrb offset from the cell centre, in grid units), convert to anxyxypixel box, then greedy IoU NMS. The demo runs a full YOLOv8 forward on ONE image, decodes + NMS, DRAWS the surviving boxes as colored rectangle outlines, writesyolo_detect.ppm, and prints the(class, score, xyxy)list. It loads the committed pico fixture (tests/fixtures/tiny_yolo.safetensors, a tiny random yolov8) by default so it runs fully OFFLINE; pass a real ultralytics yolov8 checkpoint + config.json (+ optional score/IoU thresholds) for real detections through the same path. Because the fixture has random weights the "detections" are not meaningful objects — the demo self-reports (asserts no NaN/Inf and finite box coords), exercising the full DFL-decode + NMS + draw pipeline end to end. Importer parity is asserted to max |diff| < 1e-4 vs anumpyfloat64 oracle over the raw head (TestYoloObjectDetectionParity), the decode is verified against a manual DFL + sigmoid (TestYoloDetectionDecode), fixture generatortools/yolo_tiny_fixture.py. Pure CPU, <1 s on the fixture. - OWL-ViT open-vocabulary object detection — the demo for
BuildOwlViTFromSafeTensors(neuralpretrained.pas), an open-vocabulary (zero-shot, text-conditioned) object-detection importer (model_typeowlvit/owlv2: google/owlvit-base-patch32 and siblings). Unlike DETR (open but closed-vocabulary — a fixed label set), OWL-ViT (Minderer et al. 2022) matches every image patch against arbitrary free-text query embeddings by cosine similarity, so the "classes" are whatever text you encode, decided at inference time. It REUSES the CLIP towers verbatim (the checkpoint tensor names are CLIP's under anowlvit.prefix): the CLIP ViT image tower (via the reusableBuildClipVisionTower, here returning the post-layernorm(num_patches+1,1,hidden)hidden states) and the CLIP text tower (causal, argmax-of-ids EOS pooling,text_projection). The new code is the OWL-ViT detection head built on top of the post-LN vision states: the CLS merge (image_embeds[p] = post_ln[1+p] * post_ln[0], the class token broadcast over the patch rows viaTNNetChannelMulByLayer, then a finallayer_norm); the class headdense0(hidden → text_dim, L2-normalized and matched by cosine against the L2-normalized query embeddings) with a learnable per-patchlogit_shiftand ELU-gatedlogit_scale; and the box head (3-layer exact-erf-GELU MLP → raw cxcywh). The cosine match +(logit + shift)*scaleand the boxsigmoid(box_raw + grid_box_bias)are finished byDecodeOwlViTDetections(the grid box-bias depends on the patch index and is added there); query embeddings are pooled+normalized byOwlViTQueryEmbedding. The demo loads the committed pico fixture (tests/fixtures/tiny_owlvit.safetensors), runs one tiny image, embeds a few free-text query token sequences, and prints the best-matching patch (sigmoid score + cxcywh box) per query — offline, reproducing the parity test's numbers. Parity is asserted to max |diff| < 1e-4 vs the realtransformersfloat64 forward over the per-patch/per-query class logits AND the boxes (TestOwlViTOpenVocabDetectionParity), the decode is verified for ordering/range (TestOwlViTDetectionDecode), fixture generatortools/make_pico_owlvit_fixture.py. Pure CPU, <1 s on the fixture. - Mask R-CNN instance segmentation (per-object mask) — the demo for
BuildMaskRCNNFromSafeTensors(neuralpretrained.pas), the FIRST instance-segmentation importer (model_typemaskrcnn: torchvision maskrcnn_resnet50_fpn). Unlike SegFormer (one dense class map over the whole image) Mask R-CNN (He et al. 2017) emits a separate binary mask per OBJECT. SCOPE v1 (bounded, per the tasklist): INFERENCE with externally supplied proposal boxes — the RPN / anchor generator is SKIPPED, the backbone FPN-input feature maps are fed directly (the ResNet-50 backbone's C4/C5 taps in a real run, via the already-landedBuildResNetFromSafeTensors), and one fixed proposal box is pooled. The importer builds the FPN top-down pyramid (lateral convs + nearest- upsample viaTNNetDeMaxPool+ smoothing convs), the RoIAlign crop of the proposal from the chosen pyramid level (the already-landedTNNetRoIAlign, torchvisionaligned=Truehalf-pixel offset,sampling_ratiofrom config) at both the box pool size (7) and mask pool size (14), the box head (fc6 → ReLU → fc7 → ReLU → parallelcls_score+bbox_pred; the fc6 input columns are PERMUTED from PyTorch channel-major to CAI depth-major), and the small mask head ( conv+ReLU →ConvTranspose2d(2,stride2)+ReLU → conv to per-classH×Hmask logits).RunMaskRCNNrewrites the proposal box on the RoIAlign layers and returns the class logits, box deltas and per-class mask logits for one proposal. The demo loads the committed pico fixture (tests/fixtures/tiny_maskrcnn.safetensors, a tiny random Mask R-CNN) by default so it runs fully OFFLINE, runs the single fixture proposal, picks the best-scoring class, sigmoids that class's mask, OVERLAYS it (red) on a tiny synthetic image and writesinstance_segmentation.ppm. Because the fixture has random weights the "mask" is not a meaningful object — the demo self-reports (asserts no NaN/Inf and that the thresholded mask covers a sane pixel fraction), exercising the full FPN + RoIAlign + box/mask-head pipeline end to end. Importer parity is asserted to max |diff| < 1e-4 vs a self-containednumpyfloat64 oracle over the mask-head logits AND the class logits + box deltas (TestMaskRCNNParity; torchvision is not in the venv so the oracle is hand-written, the same stance as the ResNet fixture), fixture generatortools/make_pico_maskrcnn_fixture.py. Pure CPU, <1 s on the fixture. - Mask2Former universal segmentation (mask-classification set prediction) — the demo for
BuildMask2FormerFromSafeTensors+RunMask2FormerSemantic+DecodeMask2FormerSemantic(neuralpretrained.pas), the FIRST universal-segmentation importer (model_typemask2former: facebook/mask2former-swin-tiny-*-semantic). Distinct from both SegFormer (per-PIXEL argmax) and Mask R-CNN (RoIAlign on region proposals): Mask2Former (Cheng et al. 2022, arXiv:2112.01527) does mask-classification set prediction — a fixed set of learned object queries, each predicting ONE binary mask + a class, unifying semantic/instance/panoptic in a SINGLE head, with NO proposals and NO per-pixel classifier. The conceptual core is MASKED ATTENTION: each decoder layer's cross-attention is restricted to the FOREGROUND of the previous layer's predicted mask (sigmoid(mask) >= 0.5keys allowed, background keys get-1e9, with the HF "attend-to-nothing → unmask-all" fallback), realised over an explicitTNNetDotProductsscore matrix + additive-biasTNNetSum(there is no off-the-shelf masked-cross-attention leaf) in the Mask2Former-specific cross → self → FFN post-norm order; the packednn.MultiheadAttentionin_projis row-sliced into q/k/v. Because masked attention has a DYNAMIC feedback loop (layer L's mask comes from layer L−1's prediction), the decoder is built as one sub-net per layer and driven layer-by-layer byRunMask2FormerSemantic, which recomputes the mask bias (bilinear-downsampled to each round-robin level) between layers;DecodeMask2FormerSemanticthen folds the per-query class+mask logits into a per-pixel label map (softmax classes, drop the no-object slot, sigmoid masks, class-weighted argmax) exactly like HFpost_process_semantic_segmentation. SCOPE v1: SEMANTIC inference, decoder + heads only — the pixel-decoder outputs (mask_features + the 3 multi-scale memory levels) are fed as PRECOMPUTED inputs (mirroring how Mask R-CNN v1 took FPN feature maps directly); wiring the full Swin backbone + FPN pixel decoder into one forward is a documentedtasklist.mdfollow-up. The demo loads the committed pico fixture (tests/fixtures/tiny_mask2former.safetensors) + its precomputed feature maps fully OFFLINE, runs the masked-attention decoder, prints the label map as a colored ASCII palette and writessegmentation.ppm. Random pico weights → wiring/throughput smoke (one class wins everywhere, matching the HF reference map), not a trained segmenter. Importer parity is asserted to max |diff| < 1e-4 (actual ~1.2e-7) vs the REALtransformersfloat64 Mask2Former forward over BOTH the per-query mask logits AND class logits (TestMask2FormerParity, fixture generatortools/make_pico_mask2former_fixture.py). Pure CPU, <1 s on the fixture. - Image colorization (L → ab) — a self-supervised generative-vision task: predict the chroma of an image from its luminance alone, so any grayscale photo can be auto-colorized. Each CIFAR-10 RGB image is converted to CIELAB with the existing
TNNetVolume.RgbToLab/LabToRgbhelpers (standard sRGB → linear → XYZ(D65) → Lab and the exact inverse, now regression-covered byTestVolumeLabRoundTrip: RGB → Lab → RGB max‖diff‖ < 1 of 255). The L channel (1 channel,/100normalized) is the network INPUT and the a*,b* channels (2 channels,/110normalized) are the regression TARGET — no labels needed, the supervision is the image's own color. A small CPU-friendly conv encoder-decoder built with the reusableTNNet.AddUNet(Depth=2, BaseFeatures=16, OutputChannels=2, …)(the same builder as UNetSegmentation) is trained with plain per-pixel L2 (MSE) regression on the a*,b* channels, capped by aTNNetHardTanhso the output stays in the normalized chroma range. After training, a handful of validation images are colorized — the predicted a*,b* are recombined with the TRUE L, mapped back to RGB and written to disk assampleN_gray.png/sampleN_color.pngpairs (gray-in vs color-out) under./colorized/. The classic quantized-bin classification head (Zhang et al. 2016) would give more vivid output; this uses the simpler regression head. Reuses existing conv layers + the existing Lab helper — the new code is the L-in/ab-out data pipeline and the recombination. Pure CPU; the default SMOKE run (2000 train images, 10 epochs) finishes in a few minutes. Needs the CIFAR-10 binary batches in the working dir (data_batch_1.bin … data_batch_5.bin,test_batch.bin; see the link printed if missing).
Training dynamics & optimization
- Optimizer bake-off — trains the same tiny MLP on the same fixed hypotenuse toy four times, changing only the optimizer (plain SGD, SGD+momentum, Adam, and RMSProp via Adam with
Beta1=0); holds seed/data/architecture/LR fixed across arms and prints a loss-vs-epoch table, an epochs-to-converge summary, and an ASCII chart. - Learning-rate range test — Leslie Smith's LR-range-test in pure Pascal: trains a tiny MLP for ~100 mini-batches sweeping LR exponentially from 1e-6 to 1e+1, prints a 1-column ASCII chart of
log10(LR)vs smoothed loss with the steepest-descent row marked, and writes a CSV side-output for downstream tooling. - Scheduler Compare - Trains the same tiny MLP four times under different learning-rate schedules (constant / Step / Cosine / WarmupCosine) from the
neuralschedulerunit, driving the LR manually per epoch. Prints an ASCII chart of each LR curve plus a final-loss table — you can see constant stay flat, Step drop in stairs, and Cosine/WarmupCosine anneal. Pure CPU, ~1s. - Batch-size sweep — trains the SAME tiny MLP on the SAME synthetic hypotenuse task
y = sqrt(X^2 + Y^2)across batch sizes{1, 8, 32, 128}(net, data, and RNG seed all held fixed so batch size is the only variable) and prints how the knob trades wall-clock-per-epoch against epochs-to-converge. A beginner-oriented companion to the activation/optimizer bake-offs. Pure CPU, well under a minute. - Mixup Augmentation - Mixup data augmentation (Zhang et al. 2018):
CreateMixedVolumePairList(inneuralvolume) forms synthetic training pairs by convex-combining two real pairsx_mix = λ·x_i + (1-λ)·x_j,y_mix = λ·y_i + (1-λ)·y_jwithλ ~ Beta(α,α)(built-in Beta/Gamma sampler;Beta(1,1)=Uniformfast path). Trains a tiny classifier with vs without mixup. Pure CPU, ~1s. - CutMix Augmentation - CutMix data augmentation (Yun et al. 2019):
CreateCutMixVolumePairList(inneuralvolume) pastes a random rectangle of a permuted partner image into the input (box = (r·W)×(r·H),r = sqrt(1-λ), clamped) and mixes the targets by the TRUE pasted-area fractionλ_adj = 1 - box_area/(W·H)withλ ~ Beta(α,α)(shares Mixup's Beta/Gamma sampler).ComputeCutMixBoxexposes therand_bboxgeometry for deterministic use. Trains a tiny conv classifier with vs without cutmix on a synthetic image toy. Pure CPU, ~1s. - AutoAugment (RandAugment / TrivialAugment) — automatic single-image augmentation policy (in
neuraldatasets): a fixed op bank (autocontrast, equalize, rotate, shear-x/y, translate-x/y, posterize, solarize, color/contrast/brightness/sharpness) over aTNNetVolume, plus RandAugment (NeuralRandAugment, N ops at fixed magnitude M; Cubuk et al. 2020), the parameter-free TrivialAugment (NeuralTrivialAugment, one op, magnitude drawn uniformly; Müller & Hutter 2021) and RandomErasing / Cutout (NeuralRandomErasing; Zhong et al. 2020). Magnitudes follow the torchvision transforms-v2_AUGMENTATION_SPACEranges on a0..30integer scale (M=0≈ identity;rotate(0)/shear(0)/translate(0)are bit-identity).TNeuralAugmentationPolicybundles a policy + RandomErasing into the new opt-inTNeuralImageFit.ImageAugmentationFnhook, applied AFTER the built-in flip+crop so existing CIFAR examples can opt in unchanged. Demo trains a tiny conv classifier without vs with the policy on a synthetic toy. Pure CPU, ~3 s. Expected to give a small consistent top-1 lift on real CIFAR-10. - Shake-Shake regularization bake-off — the headline regularisation win for the
TNNet.AddShakeShakeBlockbuilder /TNNetShakeShakeMergelayer (Gastaldi 2017, Shake-Shake regularization): two parallel residual branches are merged with a STOCHASTIC convex weight (alphaforward, an independentbetabackward, both resampled per pass; eval is the deterministic0.5/0.5mean). The demo contrasts a shake-shake stack against an otherwise-identical deterministic two-branch (0.5/0.5) residual on a deliberately noisy, over-parameterised toy (40-D input, only 2 informative dims, 64 training samples with 25% label noise) so the over-sized net memorises the noise and the generalisation gap surfaces. Both arms hit 100% train accuracy, but Shake-Shake generalises better on every metric — val accuracy 74.5% vs 70.2%, val loss 0.619 vs 0.659, and a narrower train/val accuracy gap (25.5 vs 29.8). Pure CPU, ~7 s. - Pre-norm vs post-norm residual bake-off — wires the same 12-block deepish residual MLP three ways via the
TNNet.AddPreNormResidual/AddRMSNormResidual/AddPostNormResidualbuilders and trains each on the hypotenuse toy at matched seed/LR/epochs; surfaces the textbook stability gap (the pre-norm arms drop to near-zero error while post-norm oscillates and ends ~10x higher) and handles any diverging arm cleanly. - ReZero vs gated-residual depth ablation — trains the same deepish residual MLP on the hypotenuse toy two ways at matched seed/arch/LR/epochs, differing only in the residual gate: a scalar
TNNetReZero(one learnable weight, init 0) vs the per-channelTNNet.AddGatedResidualbuilder (one gate per channel, init 0). Dumps the learned gates per block as an ASCII chart so the per-channel gate's UNEVEN opening (many channels stay exactly 0, a few grow both signs) is visible against the uniformly-opening ReZero scalar. Runs in ~15 s on CPU. - Highway depth-trainability sweep — the headline trainability-at-depth win for the new
TNNetHighway, the input-dependent learned-gate Highway layer (Srivastava, Greff & Schmidhuber 2015):y = T(x)⊙H(x) + (1-T(x))⊙xwithT(x)=sigmoid(W_T·x+b_T)computed FROM THE INPUT each forward pass and an explicit(1-T)·xidentity carry. Negative gate-bias init (-1.5) starts a fresh deep stack near the identity. On an "identity + small residual" target, plain tanh-MLP stacks and Highway stacks are trained head-to-head at depths{2,5,10,20,40}: the plain stack's test MSE degrades monotonically (≈16× from depth 2→10, ≈44× by depth 20) while the Highway stack stays essentially flat through depth 10 and ≈8× better at depth 20, because the carry keeps gradients flowing to the early layers. Also reports the mean learned gateTper layer. Distinct fromTNNetReZero(scalar gate),AddGatedResidual(per-channel constant gate) andTNNetGLU/SwiGLU(no identity carry). Pure CPU, ≈40 s. - DropPath / Stochastic-Depth ablation — sweeps the DropPath probability
p in {0.0, 0.1, 0.2}on the same small 6-block ResNet-style classifier (each blocky = x + TNNetDropPath(p)(ReLU(PointwiseConvLinear(x))), the stochastic-depth layer on the residual BRANCH) at matched seed/data/epochs, and prints a per-ptable of final train/test loss and accuracy. Honest read: on this tiny easy 3-way toy DropPath does NOT raise test accuracy (all arms tie), but the held-out cross-entropy falls monotonically withp(3.03→2.80→2.37) — a real over-confidence-reducing regularisation signal. The gate asserts only true invariants (every arm trains, nothing diverges,p=0.0reproduces the no-drop baseline); eval forcesEnableDropouts(false)so inference is the deterministic identity. Pure CPU, ~12 s. - Noise-layer train/inference delta sweep — sweeps the drop probability
p in {0.0, 0.1, 0.2, 0.4}for all four stochastic noise layers (TNNetDropout,TNNetDropPath,TNNetSpatialDropout1D,TNNetSpatialDropout2D) on the same tiny 4-block ResNet-style classifier (each layer on the residual BRANCH,y = x + Noise_p(ReLU(PointwiseConvLinear(x)))), and for every arm reports the train loss with the noise ON (EnableDropouts(true), stochastic mask), the train loss with the noise OFF (EnableDropouts(false), inference identity), the held-out val loss and the train/val gap. The point is the train-vs-inference distinction: a loss is only comparable across train and val if the noise is the deterministic identity at inference. Built-in checks (Halt(1)on failure): every inference pass is bit-for-bit deterministic (each probe run twice), and atp=0.0train(ON)==train(OFF)exactly (the layer is the identity in both regimes). The Spatial1D/2D arms are numerically identical on aSizeY=1tensor (same channel masks, same RNG) — documented honestly. Pure CPU, single-threaded, ~63 s. - Weight Standardization + GroupNorm vs BatchNorm — the headline use case for the new
TNNetWeightStandardizationConv(the convolution sibling of the denseTNNetWeightStandardization, Qiao et al. 2019, Micro-Batch Training with Batch-Channel Normalization and Weight Standardization): each conv FILTER's weights are standardized to zero-mean/unit-std BEFORE the convolution (exact per-output-channel Jacobian in backward), which smooths the loss landscape INDEPENDENTLY of the batch — the regime where BatchNorm's noisy per-batch statistics hurt. A two-arm bake-off trains one matched backbone at a deliberately SMALL batch, swapping only the normalization:bn(plain conv filters +AddMovingNormBatchNorm + ReLU) vsws_gn(weight-standardized conv filters +TNNetGroupNorm+ ReLU, the Qiao recipe). Graded checks (Halt(1)on failure): both arms train and reach a healthy classifier, and WS+GroupNorm is COMPETITIVE — its test accuracy is within tolerance of (or beats) BatchNorm. The head-to-head is reported honestly, not assumed (on this easy synthetic toy the two MATCH within tolerance). Pure CPU, ~65 s. - Sharpness-Aware Minimization (SAM) — a hand-rolled two-pass SAM (Foret et al. 2021) on a noisy-label 2D-blob toy: ascent-perturb the weights by
rho * g/||g||using the global gradient norm, take a SECOND forward+backward at the perturbed point, then restore the original weights (whole-netSaveDataToStringsnapshot) and apply the perturbed gradient with plain SGD. Contrasts SAM vs plain SGD and prints the flatness viaTNNet.LossLandscapeProbefor both. Two built-in invariants hold:rho=0reproduces plain SGD bit-for-bit, and the LossLandscapeProbe sharpness falls asrhogrows. Pure CPU, ~8 s. - Muon optimizer (Newton-Schulz orthogonalized momentum) — a hand-rolled gradient-surgery demo of the Muon optimizer (Jordan et al. 2024) in the
SharpnessAwareMinimizationidiom (NOT a core optimizer rewrite). Per step underNN.SetBatchUpdate(True)(soNeurons[].Deltais populated), eachTNNetFullConnectLinearweight matrix gets a momentum bufferM <- mu*M + G, thenMis replaced by its nearest semi-orthogonal matrix via 5 fixed quintic Newton-Schulz iterations (coeffs3.4445/-4.7750/2.0315, all viaTNNetVolumematmuls), andW <- W - lr*sqrt(max(rows,cols))*O. Bakes Muon off against SGD-momentum and Adam on a tiny regressor (all converge comparably, ~1 s). Headline check (Halt(1)on failure): the published 5-step quintic is a deliberately approximate orthogonalizer, so the singular values ofOland in the Muon band ~[0.7,1.3](||O^T O - I||_Fbounded, top sigma cross-checked withEstimateSpectralNorm) — semi-orthogonal, not strictlysigma=1. README distinguishes it from the forward-weight reparametrizersTNNetWeightNormLinear/TNNetWeightStandardization(Muon normalizes the update, not the weights). Pure CPU. - SoftCapping logit stability — micro-experiment showing that a
TNNetSoftCapping(c)layer before the final SoftMax tames logit blow-ups under a deliberately-aggressive learning rate. The same tiny 4-class blob classifier is trained twice atLR=5.0, differing only in whetherTNNetSoftCapping(8.0)is inserted before SoftMax: the uncapped arm's logits explode (~1e4, accuracy collapses to chance) on every epoch while the capped arm pins every logit to[-8, 8], logs zero exp-overflow events, and reaches 100% accuracy. Pure CPU, ~2 s. - Domain-adversarial NN — smallest possible DANN (Ganin et al. 2015) demo around the new
TNNetGradientReversallayer: two 2D-blob domains with 90-degree-rotated class-conditional means, shared trunk feeding a label head + a domain head behind a Gradient Reversal Layer. With GRL enabled the label head reaches ~99% on both domains while the domain head collapses to chance (~0.500); toggle a constant to confirm the contrast. Runs in ~2s on CPU. - Forward-Forward — reproduces Geoffrey Hinton's 2022 Forward-Forward algorithm on a pure-CPU toy using only existing in-tree layers. It does NOT learn by end-to-end backpropagation: instead of forming one global loss and backpropagating it through the whole stack, FF trains each layer locally with separate positive/negative forward passes and a per-layer goodness objective.
- Predictive Coding — a backprop-free Predictive Coding Network (Rao & Ballard 1999), a SECOND biologically-plausible learning paradigm distinct from Forward-Forward. Each layer carries explicit value nodes and local prediction errors
e_l = x_l - W_l*act(x_{l+1}); training alternates a Phase-1 inference relaxation that settles the value nodes to minimise the summed squared prediction-error energy with a Phase-2 purely local Hebbian weight stepdW = e (x) act— no global loss, no backward pass chained across layers. The energy/value math is done directly withTNNetVolume(no new core layer). Prints the per-sweep energy descent and a side-by-side accuracy vs a same-shape backprop MLP (PCN ~0.873 vs backprop ~0.970 on 3 Gaussian blobs, ~3s on CPU).
Vision & convolutional models
- Image classifier with SELU — a 32x32x3 image classifier built around the SELU activation function (self-normalizing networks): conv + max-pool stack with SELU activations and SELU-friendly initialization, a worked example of wiring
TNNetSELUinto a CNN. - Simple image classifier (parallel) — a CIFAR-10 image classifier (
THistoricalNets+TNeuralImageFit) configured for parallel/multi-threaded training; a parallel-training companion to the simple CIFAR-10 classifier examples. - DenseNet Fashion-MNIST — CAI-optimized DenseNet image classifier trained on the Fashion-MNIST dataset (T-shirt/Trouser/Pullover/… 10 classes), a clothing-image companion to the CIFAR-10 DenseNet example.
- CAI-optimized DenseNet (CIFAR-10) — command-line tool that trains a CAI-optimized DenseNet on CIFAR-10 (inspired by liuzhuang13/DenseNet) with configurable bottleneck, inner-conv count, neuron count, and optional separable convolutions. A full image-classification training program rather than a toy.
- Pooling-head bake-off — same tiny conv classifier on a synthetic blob-quadrant task, swapping only the pooling head across
TNNetAvgPool/TNNetMaxPool/TNNetLpPool(sweepp in {1,2,4,8}) /TNNetSoftPool(sweepbeta in {0.5,1,2,8}) /TNNetStochasticPool(sample-at-train, expectation-at-inference). The task is built so the class-mean is invariant and only energy CONCENTRATION discriminates, soAvgPoolsits at chance whileMaxPoolsolves it andLpPool'sp/SoftPool'sbetatrace the average→max interpolation. Reports per-arm train/val accuracy and the train/val gap to probe whether StochasticPool's regularization narrows it. - Adaptive-pool resolution invariance — the headline property of adaptive pooling: ONE fully-convolutional stack (
Conv8(3x3,pad1)+ReLU x2 -> MaxPool(2) -> [adaptive head]) accepts inputs of DIFFERENT spatial sizes (16x16and24x24) and still emits a FIXED-size head, the classic adaptive global pool -> FC classifier pattern. It exercises bothTNNetAdaptiveAvgPoolandTNNetAdaptiveMaxPool(globalCreate(1)andCreate(2)heads), printsinput -> post-conv -> adaptive -> outputshapes per resolution to make "variable in, fixed out" explicit, asserts two built-in degeneracies (Create(1)== global pooling,Create(N)== identity whenNequals the post-conv size), and trains a global-head classifier at16x16then runs inference at the unseen24x24(via a weight-shared sibling net) to show the trained fixed-size head transfers across resolutions. Pure CPU, ~1.5 s. - CoordConv spiral — minimal demo of the new
TNNetCoordConvlayer (Liu et al. 2018). Two identical 1x1-conv + global-avg-pool nets are trained to regress the(x, y)coordinate of the single bright pixel in an 8x8 image. The plain net is translation-invariant by construction and gets stuck near the constant-prediction baseline (MSE ~0.84); a singleTNNetCoordConvat the front collapses MSE by ~400x to ~0.002. Runs in ~25s on CPU. - CBAM Attention - Convolutional Block Attention Module (
TNNet.AddCBAM, Woo et al. 2018): channel attention (dual avg+max pooling) followed by spatial attention, dropped into a small conv classifier. Pure CPU. - Sub-Pixel Super Resolution - A tiny
TNNetPixelShuffle(depth-to-space) head learns a 2x upscaling on synthetic tiles (test PSNR 4 dB → 39 dB), pure CPU - Spatial Transformer Network — learned geometric canonicalization — the headline demo for the new
TNNetAffineGridSample(Jaderberg et al. 2015, Spatial Transformer Networks), an op that warps a feature map by a CONTINUOUS, input-conditioned geometric transform (every existing spatial op — conv, pool, upsample, deconv — resamples on a FIXED integer grid). The parameter-free two-input layer reads an image source and a caller-wired 6-value affinetheta(a 2×3 matrix from a small localization head), back-warping each output pixel(x',y') = theta·[x_n,y_n,1]in normalized [-1,1] coords and bilinearly interpolating the 4 nearest source pixels; both backward paths (d/d-source scatter and the headline d/d-theta sampler partials) are finite-difference checked. The example prepends a conv localization head →FullConnectLinear(6)BIAS-INITIALISED to the identity affine[1,0,0,0,1,0](weights zeroed) → the sampler in front of a small shared classifier, trained end-to-end on RANDOMLY ROTATED/TRANSLATED synthetic glyphs (no data download): the plain classifier sits at 0.879 while the CNN+STN front-end learns to de-rotate/re-center the input and recovers to 1.000 (+12.1 pp). Concat-style serialization (theta source index in the structure string), no persisted weights. Pure CPU, ~40 s. - Glimpse Downsampler — learned scale+translate "hard" attention glimpse — the scale+translate-restricted follow-up to Spatial Transformer. Reuses the same
TNNetAffineGridSamplesampler but constrains the warp to a learned CROP/ZOOM via the new parameter-freeTNNetScatterToAffine, which scatters a localization head's 4 outputs(s_x, s_y, t_x, t_y)into the 2×3 affine[s_x,0,t_x; 0,s_y,t_y]with the rotation/shear slots held at a HARD zero (no parameter ever lands in them, so they can never drift). A small bright glyph is dropped at a random offset on a larger CLUTTERED canvas; the localization head learns WHERE and how much to zoom, reading a small 14×14 canonical patch from the 28×28 input (the sampler's warped view is avg-pooled to the patch size — output strictly smaller than input). Contrasted against a FIXED center-crop of the same patch size feeding the same classifier: the learned glimpse tracks the roaming glyph (1.000) while the blind center-crop misses off-centre glyphs (0.906, +9.4 pp). Pure CPU, ~1 min. - Deformable convolution (content-adaptive sampling grid) — the headline demo for the new
TNNetDeformableConv(Dai et al. 2017, Deformable Convolutional Networks), a conv whoseKxKsampling positions are PREDICTED rather than fixed. A zero-initialized "offset head" (an ordinary conv) emits2*K*Kper-location offset maps; each tap is then gathered from the input by BILINEAR interpolation at(base_position + offset)before the usual weighted sum, so the receptive field bends to follow content instead of sitting on a rigid axis-aligned window (distinct from dilated conv's fixed dilation and group conv's rotated copies). Backward propagates into the conv weights, the input, AND the offsets — the offset gradient flows through the four bilinear corner coefficients (dSample/dpx,dSample/dpy), the interesting/hard part, and is finite-difference checked. Zero-init means the layer starts identical to a plain conv. The demo asks both arms to reproduce a field translated by(+3,+3)whose answer lies OUTSIDE a 3×3 window: the rigidTNNetConvolutionLinear(9 weights) structurally cannot reach and floors at val-MSE ~0.040, whileTNNetDeformableConvlearns the+3sampling offset and reaches ~1e-6 (~40000× lower). Honest caveat (README): the offset head diverges to NaN at high LR (unbounded offsets × pixel-scaled bilinear gradient), so the example uses a small LR. Pure CPU, ~40 s. - Video action recognition with 3-D convolution — the headline demo for the new
TNNetConvolution3D(spatiotemporal / volumetric) convolution, a conv that mixes information across time as well as space. Each sample is a short grayscale CLIP (Tframes of acGrid×cGridimage) in which a bright blob slides in one of FOUR directions, and the task is to classify the MOTION direction — something a single frame is ambiguous about, so the network must integrate across time. The(T, H, W, C)clip is packed forTNNetVolume's three axes by laying theTframes contiguously along theDepthaxis (Depth = T·C,SizeX=W,SizeY=H); the layer slides a(FeatureSizeT × FeatureSizeXY × FeatureSizeXY)kernel over the spatial grid (padded/strided like the 2-D conv) AND over the time blocks within the depth axis, with each frame'sCchannels contiguous so every kernel tap is a contiguous-depth AVX dot product. The net stacks twoConvolution3D(F=8,T=3,K=3)blocks (each shrinkingOutputTby 2) →FullConnectLinear(4)→SoftMax. A BASELINE on the identical stack but withFeatureSizeT=1(a per-frame 2-D conv shared across frames, no cross-frame coupling) is trained alongside to make the temporal-mixing axis explicit. Layer covered byTestConvolution3DInputGradientCheck/TestConvolution3DWeightGradientCheck/TestConvolution3DSerializationRoundTrip. Pure CPU, ~1 min. - Video classification with an imported VideoMAE transformer — the end-to-end demo for
BuildVideoMAEFromSafeTensors, a video-classification importer (a clip ofTframes → an action label) and the pay-off of theTNNetConvolution3Dlayer. It loads the committed pico checkpointtests/fixtures/tiny_videomae.safetensors(the very fixture the float64 parity testTestVideoMAEClassificationParitypins to HF) and runs the full VideoMAE forward on CPU: a non-overlapping tubelet 3-D conv (kernel = stride = (tubelet_size, patch, patch)) splits the(T,H,W,C)clip intoT′×H′×W′space-time tokens, a fixed sin-cos 3-D position table (rebuilt exactly from HF'sget_sinusoid_encoding_table, not learned) is added, the stock pre-LN transformer encoder applies joint space-time attention (every token attends to every token), and the finetuned head mean-pools the tokens →fc_norm→ a linear classifier → action logits. The(T,H,W,C)clip is packed forTNNetVolume's three axes with framet'sCchannels contiguous at depth[t·C, t·C+C)(theTNNetConvolution3Dconvention); the temporal tubelet stride is realized by selecting the non-overlapping output frames withTNNetSplitChannels+TNNetDeepConcat. The program synthesizes two sliding-blob clips, classifies each, and prints the per-class logits + argmax (the pico weights are random, so the label is not semantic — the point is the importer + forward path runs). Pure CPU, < 1 s. - ImageNet top-1 / top-5 accuracy eval — the end-to-end demo for
EvaluateImageNet/ImageNetReport(neural/neuralimagemetrics.pas), the import-VERIFICATION backstop for the landed classifier importers (ResNet / ViT / Swin / DINOv2 / MobileNetV3 / VGG / Inception-v3 / EfficientNet) — the CV analogue of MMLUEval / PerplexityEval on the LLM side. Each importer's parity test only compares raw logits on one or two tensors, which catches a transposed weight but not a wrong preprocessing pipeline (resize / center-crop / normalize) or a label permutation; running a folder of labelled ImageNet-val images through the real transform (neuraldatasets.PreprocessImageForVisionModel: shorter-side resize → center-crop →(x/255 − csImageNetMean)/csImageNetStd) and the real net, then checking top-1 / top-5 against the published numbers, is exactly that backstop. The harness takes already-preprocessedTNNetImageNetSamplevolumes + gold labels, runsNN.Compute, forms top-1 and top-K viaTopKIndices(first-max tie-break), tallies top-1 (argmax == gold) and top-K (gold anywhere in the top-K) accuracy, and retains up toMaxConfusiontop-1 misses for a human-readable confusion sample (each flagged top-K-hit vs top-K-miss).ImageNetReportformats the top-1 / top-5 lines + the confusion sample. To stay self-contained (no network fetch, no multi-GB download, no real ImageNet) the default SMOKE builds a small CNN, trains it for 30 epochs on a tiny DETERMINISTIC synthetic 6-class coloured-pattern set (fixedRandSeed) rendered large and pushed through the SAMEPreprocessImageForVisionModeltransform the real path uses, then evaluates with the harness — exercising the real transform + real harness end to end in seconds under the 3 GB ulimit (the cleanly-separable synthetic classes report 1.0000 / 1.0000; the point is the harness + transform mechanics, not a real number). The scoring path is checkpoint-agnostic: swap the smoke CNN for aBuildResNetFromSafeTensors(or any classifier importer), feedLoadImageForVisionModel-produced volumes into the sameTNNetImageNetSamplerecords, and the harness is unchanged. The--full <dir>flag prints the documented real-ImageNet-val recipe (folder layout:labels.txtof<filename> <class_index>lines + the JPEGs; import a backbone,LoadImageForVisionModelwith itsImageSize+ ImageNet mean/std,EvaluateImageNet(NN, Samples, 1000, 5)); wiring a real checkpoint + the full 50 k-image ImageNet-val run is a documented follow-up. Pure CPU. - Capsule reconstruction & pose perturbation — the reconstruction-decoder stretch demo for
TNNetCapsuleRouting(Sabour et al. 2017, Dynamic Routing Between Capsules): a CapsNet is trained with the paper's MARGIN loss on capsule lengths plus a masked-reconstruction MSE — a small decoder (Input(16) → ReLU(64) → Sigmoid(144)) is fed the TRUE-class capsule's pose vector and its input gradient is chained back into the encoder. Headline: after training, the program sweeps each pose dimension over[-0.25,+0.25], picks the most responsive one, and shows that varying that single dimension smoothly and MONOTONICALLY varies an interpretable visual factor (stroke intensity/thickness: total ink32.6 → 24.9) — a genuine, visible disentanglement, not faked. Uses a tiny synthetic 12×12 two-class "bars" set (controllable thickness/position pose factors, chosen over a real MNIST parse to stay in budget). CapsNet and a param-matched MLP both hit 100% on the easy 2-class task (the capsule advantage here is the interpretable pose vector, not raw accuracy). Pure CPU, deterministic, ~18 s.
Generative image models
- DDPM diffusion digit generation — a generative-by-diffusion example (Ho et al. 2020): a small time-conditioned U-Net learns to denoise 28×28 MNIST digits and then generates fresh ones from pure Gaussian noise. Forward process is the standard LINEAR beta schedule (
beta_1=1e-4 .. beta_T=0.02,T=200); the training objective is epsilon-prediction MSE (x_t = sqrt(alpha_bar_t)·x_0 + sqrt(1-alpha_bar_t)·eps, predicteps); generation is the ancestral (reverse) DDPM sampling loop fromx_T~N(0,I)down tox_0, written out as a PNG grid. It USESTNNetSinusoidalTimeEmbedding(scalar timestep → sinusoidal vector) and injects the timestep into every U-Net block as a per-channel scale/shift viaTNNet.AddFiLMConditioned/TNNetFiLM; skip connections reuseTNNetDeepConcatand the decoder upsamples withTNNetUpsample(all layers pre-existing — the new content is the schedule + sampling loop + tiny U-Net builder). The eps-MSE starts at the ~1.0 trivial baseline (variance ofN(0,1)noise) and falls below it within ~100 steps, confirming real denoising; a from-scratch diffusion net spikes early so the loop clips the update magnitude. It also supports CLASS-CONDITIONAL generation (pick a digit) via classifier-free guidance (eps = eps_uncond + s·(eps_cond − eps_uncond), a label embedding added to the time embedding, trained with ~10% label dropout for a null/unconditional token) plus a deterministic DDIM fast sampler (10–50 steps vs the full ancestral loop). Default SMOKE mode (a few hundred steps + a 10×10 class-conditional grid where each row is a requested digit 0–9, sampled via DDIM+CFG) runs in ~2 min on CPU;--fullfor sharper digits. Needs the standard MNIST idx-ubyte files in the working directory. - Flow Matching / Rectified Flow digit generation — the modern ODE/transport alternative to DDPM (Lipman et al. 2023, Liu et al. 2023): the same tiny time-conditioned U-Net as
DiffusionMNIST, but trained to regress a velocity field along STRAIGHT interpolation paths. With noisex0~N(0,I), datax1, and continuoust~U(0,1), it builds the linear interpolantx_t = (1−t)·x0 + t·x1and minimisesMSE(v_theta(x_t,t), x1−x0)— no beta schedule, noalpha_bar, no noise/score parameterisation. Generation is a deterministic forward Euler ODEx_{t+dt} = x_t + dt·v_theta(x_t,t)from noise tot=1in just ~25 steps (far fewer than DDPM's ancestral loop). Continuoustis rescaled by 1000 beforeTNNetSinusoidalTimeEmbeddingso the embedding sees the integer-like range it was designed for. Reuses the DiffusionMNIST MNIST idx files (../DiffusionMNIST/..., no copy). Default SMOKE mode runs in ~2 min on CPU (velocity MSE ~2.8→~0.35) and writes an 8×8 Euler-sampled grid;--fullfor sharper digits. - Class-conditional diffusion + classifier-free guidance — a generative image example that lets you pick the digit (VisualGAN, DiffusionMNIST in its plain form, and FlowMatching all draw UNCONDITIONAL samples). It trains the same tiny time-conditioned U-Net as
DiffusionMNIST, but conditions on the class labely∈0..9by mapping it through a learnedTNNetEmbeddingand adding that vector to the sinusoidalTNNetSinusoidalTimeEmbeddingbefore a shared cond MLP, so a singleTNNetFiLMcond vector carries both how much noise (t) and which digit (y). The CFG mechanism comes from LABEL DROPOUT: index 10 is a dedicated NULL/unconditional token and, with ~10% probability per training example, the real label is swapped for it, so the one network learns both the conditional scoreeps(x_t,t,y)and the unconditionaleps(x_t,t,null)(Ho & Salimans 2022). The example is built around ONE question — what does the guidance weightwdo? — and answers it QUANTITATIVELY: a tiny side MNIST classifier is trained purely to score class fidelity, and the denoiser is sampled over a sweepw∈{0,1,2,4,8}reporting per-wclass-fidelity (classifier agreement with the requested digit; the textbook CFG effect is for it to RISE withw) and a diversity proxy (mean per-pixel std across same-class samples, expected to FALL withwas the model collapses onto the class mode) — the classic CFG fidelity/diversity trade-off. All noising (AddNoise), the reverse DDIM trajectory (Sample) and the CFG mix (ApplyCFG) reuse the model-agnosticneuraldiffusion.pas(TNNetDiffusionScheduler); nothing is hand-rolled. Writes a PNG grid whose ROWS are increasingwand COLUMNS are independent samples of one chosen digit, and asserts no NaN/Inf. The clean trade-off needs a well-trained denoiser (--full); the default SMOKE run (short denoiser + classifier + a small sweep, ~3 min on CPU) is intentionally undertrained — like the DiffusionMNIST smoke its samples are noisy, so the run mainly validates the pipeline and self-reports the observed metric direction. Reuses the DiffusionMNIST MNIST idx files (../DiffusionMNIST/..., no copy). - Consistency-model few-step distillation — a FAST-SAMPLING generative example (Song et al. 2023): the landed diffusion examples (DiffusionMNIST, ConditionalDiffusion, FlowMatching) all generate in 16–200 reverse steps, whereas this one DISTILS a multi-step DDPM teacher into a CONSISTENCY MODEL that samples a digit in 1, 2 or 4 steps. A consistency function
f(x_t,t)maps ANY point on a probability-flow trajectory directly to that trajectory's originx_0, so a single network call is already a (rough) sample. The boundary conditionf(x,t→0)=xis enforced EXACTLY by the Karras skip/out parameterisationf = c_skip(t)·x + c_out(t)·F_theta(x,t)withc_skip = σ_data²/(σ(t)²+σ_data²),c_out = σ(t)·σ_data/√(σ(t)²+σ_data²), andσ(t)=√((1−ᾱ_t)/ᾱ_t)read from the scheduler'sAlphaBar— pure example-side arithmetic, no new layer. Distillation: forward-noisex_0to a sub-grid timestept_{n+1}, take ONE deterministic teacher DDIM ODE step down tot_n, then minimise‖f_theta(x_{t_{n+1}},t_{n+1}) − f_target(x_{t_n},t_n)‖²; the target netf_targetis the repo'sTNNetEMAWrappershadow (stop-grad EMA of the student),Update()per step. Thec_out(t_{n+1})factor is folded into the backprop target so the raw network head trains on the right residual. Both teacher (eps-prediction) and studentF_thetareuse the DiffusionMNIST tiny time-conditioned U-Net wholesale (TNNetSinusoidalTimeEmbedding→ shared cond MLP →TNNetFiLMper block,TNNetDeepConcatskips,TNNetUpsampledecoder). The few-step sampler starts fromσ(T)·N(0,I)on the scaled view and, forK>1, re-noises thex_0estimate to the next lower timestep and re-evaluatesf. Reports per-sampler mean nearest-train-digit MSE (a fidelity proxy — lower = closer to the data manifold) + a NaN check, and writes a PNG grid whose rows are teacher-multistep / 1-step / 2-step / 4-step. Reuses the DiffusionMNIST MNIST idx files (symlinked); falls back to a SYNTHETIC bar dataset if they are absent so it still runs in CI. Default SMOKE mode (short teacher pretrain + short distillation, ~3 min on CPU) is intentionally undertrained — like the DiffusionMNIST smoke its samples are rough, so the headline is that the 1/2/4-step consistency MSE tracks the multi-step teacher at a fraction of the steps;--fullfor sharper output. Reuses the DiffusionMNIST U-Net +TNNetDiffusionScheduler+TNNetEMAWrapper(no new layer). - SDEdit image-to-image / real-image editing — a LATENT-DIFFUSION EDITING example (Meng et al. 2021), structurally distinct from every other generator in the tree, which all start from PURE noise: SDEdit starts the reverse process from a REAL image that has been encoded to a latent and then only PARTIALLY noised, so the source image's coarse layout survives while a NEW conditioning prompt steers the content — the classic "edit this picture" workflow. The whole pipeline reuses ALREADY-LANDED importers with no new model and no new layer: ① the VAE encoder (
BuildVaeEncoderFromSafeTensors) maps the RGB image to a clean latentz0; ② the reusableTNNetDiffusionScheduler.AddNoisenoisesz0up to an intermediate timestept_start = round(strength·T)(thestrengthknob in0..1:strength 0keeps the source,strength 1is full noise = ordinary text→image-from-noise); ③ the landed PixArt denoiser (BuildPixArtFromSafeTensors+PixArtDenoise) runs a TRUNCATED reverse DDIM trajectory fromt_startdown to 0 conditioned on the new prompt's T5 states, driven by the same schedulerSteploop the other diffusion examples use; ④ the VAE decoder (BuildVaeDecoderFromSafeTensors, latent/0.18215) returns the edited RGB. The ONLY new code is the encode→partial-noise→denoise→decode driver + thestrength/stepsknobs; nothing is hand-rolled. Because real SD/PixArt checkpoints are far too large for CI, the default offline SMOKE run wires up the committed tiny RANDOM importer-parity fixtures (tests/fixtures/tiny_vae_encoder|decoder|pixart), generates a synthetic color-ramp source, and exercises the full driver at several strengths in seconds under a tight RAM/time budget — the nets are untrained so the "edit" is not a meaningful picture; the point is that the pipeline runs end-to-end and writesedit_before.ppm(source VAE round-trip) /edit_after.ppm(edit), asserting no NaN/Inf decoded pixels. Point--vae-encoder/--vae-decoder/--pixart(with their--*-config) at real checkpoints to run it for real. Bridges the two independent fixture grids (8×8×4 VAE latent vs 6×6×4 PixArt sample) by center-cropping the latent for the denoise loop (a no-op with matched real checkpoints). The--inpaintflag turns the same driver into a diffusion INPAINTING run (the RePaint / SD-inpaint resample trick, Lugmayr et al. 2022): a binary mask over the latent grid marks the region to regenerate (1) vs keep (0), and BEFORE each reverse step the UNMASKED latent is overwritten with the clean encoded latent RE-NOISED (AddNoise) to that step's timestep, so the kept region always carries the correct noise level for the current step while only the masked region is driven by the denoiser — only the masked region is regenerated and the rest stays pixel-faithful. It adds a mask volume + a per-step composite to the existing loop (no new model); the smoke run asserts the unmasked output latent matches the source exactly and the masked region differs, and writesedit_mask.ppmalongside before/after. The diffusion-based sibling of the GAN context-encoder Inpainting example. Pure CPU; reuses the VAE/PixArt importers +TNNetDiffusionScheduler(no new layer). - Blended-diffusion Stable-Diffusion inpainting — latent-space SD INPAINTING on the standard 4-channel SD UNet (Avrahami et al. 2022, Blended Diffusion): regenerate ONLY a masked hole while the rest stays pixel-faithful, with no 9-channel inpaint-specialized
conv_inand no retrained inpaint weights — the most-used no-retrain SD inpaint path. The sibling of ImageToImage (it shares the encode→denoise→decode skeleton) but drives the landed SD UNet (BuildSDUNetFromSafeTensors+SDUNetDenoise) instead of PixArt and uses matched VAE/UNet latent grids (8×8×4) so there is no crop. The pipeline: ① the VAE encoder (BuildVaeEncoderFromSafeTensors) maps the RGB source to a clean latentz0; ② a binary RGB mask (1= hole to reconstruct,0= keep) is downsampled to a per-voxel latent mask in{0,1}; ③ the new reusable driverSDUNetDenoiseInpaint(neuralpretrained.pas) runs a full reverse DDIM trajectory and at EVERY step blends the known region back in —latents = mask·denoised + (1−mask)·AddNoise(z0,t)(the visible region always carries the right noise level fortvia the reusableTNNetDiffusionScheduler.AddNoiseforwardq_sample, so only the hole follows the denoiser; a final composite att=0pins the kept region toz0exactly); ④ the VAE decoder returns the inpainted RGB. The ONLY new code vs SDEdit is the per-step latent blend + the latent mask — both insideSDUNetDenoiseInpaint; the 4-channel UNet, VAE encoder/decoder and scheduler are reused untouched. Because real SD checkpoints are far too large for CI, the default offline SMOKE run wires up the committed tiny RANDOM importer-parity fixtures (tests/fixtures/tiny_vae_encoder|decoder+tiny_sd_unet), generates a synthetic color-ramp source + a rectangular right-half hole, runs the blended loop in seconds under a tight RAM/time budget (the nets are untrained so the inpaint is not a meaningful picture), and writesmasked_input.ppm(source round-trip with the hole greyed) /inpainted.ppm, asserting no NaN/Inf decoded pixels.TestDiffusionInpaintSmoke(TestNeuralPretrained.pas) asserts the KEPT latent region equalsz0exactly (max|diff| < 1e-4) while the MASKED hole was changed by the denoiser. Point--vae-encoder/--vae-decoder/--unet(with their--*-config) at real config-compatible checkpoints to run it for real. DEFERRED follow-ups: the 9-channel inpaint-specialized UNet (conv_inwidened tolatent|mask|masked-latent, the diffusersstable-diffusion-inpaintingweights) and the non-deterministic reparameterized VAE sampling head. Pure CPU; reuses the VAE/SD-UNet importers +TNNetDiffusionScheduler(one new reusable driver, no new layer). - Pix2Pix conditional image-to-image translation — a PAIRED image-to-image translation (Isola et al. 2017), distinct from the UNCONDITIONAL VisualGAN (noise → CIFAR): the generator is conditioned on an input image and emits a deterministic translation of it. On a synthetic generated-in-code grayscale → color task (random filled circles/rectangles on a dark background; the colorization rule is geometry-dependent — circles→red, rectangles→green, background→dark blue — so the net must both reconstruct the silhouette from grayscale AND infer each shape's color from its round-vs-straight edges, a genuine conditional translation, not a per-pixel lookup). The generator is a U-Net built by ONE
TNNet.AddUNetcall (the same builder as UNetSegmentation) with aTanhoutput in[-1,1]; the loss is an L1 reconstruction term + an adversarial loss; the discriminator is a PatchGAN — a small fully-convolutional net scoring a grid ofNxNoverlapping patches as real/fake (composed from existing conv layers, NOT a new leaf class) rather than one global logit. Trains the standard alternating D/G GAN loop (reusing the VisualGAN wiring). Reports per-epoch held-out L1 and a color-accuracy proxy and renders an ASCII (input | target | generated) triplet plus a PPM. Pure CPU; the default SMOKE run (16×16, depth 2, 30 epochs, ~3.5 min) drives held-out L1 from ≈0.69 → ≈0.13 and color-accuracy to ≈0.88, with a--fullflag for sharper output. Foundational conditional-generation recipe; reusesAddUNet+ existing convs (no new layer). - CycleGAN unpaired image-to-image translation — an UNPAIRED image-to-image translation (Zhu et al. 2017), the unpaired sibling of Pix2Pix: the two image domains are sampled from separate independent random draws, so no per-sample target exists and an L1-to-target loss is impossible. On a synthetic generated-in-code red shapes ↔ green shapes task (the same kind of filled circles/rectangles drawn in red for domain A and green for domain B, from independent seeds so a sample of A and a sample of B share no geometry — the learnable mapping is a content-preserving recoloring). Two generators
G:A→B/F:B→Aare each ONETNNet.AddUNetcall (the same builder as UNetSegmentation / Pix2Pix) with aTanhoutput in[-1,1]; two PatchGAN discriminators (image alone, no condition stacked in — the adversary judges domain membership, not input/output consistency) trained with the least-squares GAN objective. The crux is the cycle-consistency lossλ_cyc·(|F(G(a))−a| + |G(F(b))−b|)(round-trip) plus an identity colour anchorλ_id·(|G(b)−b| + |F(a)−a|): the cycle term genuinely backprops through the composedF∘G/G∘F—F.Backpropagateon the cycle gradient updatesFAND, viaEnableErrorCollectiononF's input, leavesd(cycle)/d(g)inF.Layers[0].OutputError, which is then fed intoG's output error (the mirror for the backward cycle); both directions accumulate per step underSetBatchUpdate(true)before a single weight update. No simplification of the objective vs canonical CycleGAN (L1 cycle/identity viasign(·)sub-gradient, LSGAN — itself a standard CycleGAN choice); only the scale is toy. Reports per-eval cycle-reconstruction error (cycA/cycB), a translation-colour score, and the two LSGAN losses, and renders an ASCII (a|G(a)|F(G(a))/b|F(b)|G(F(b))) panel plus a PPM. Pure CPU; the default SMOKE run (16×16, depth 2, 64 train / 32 test, 10 epochs, ~3.5 min) drives held-out cycle error from ≈0.68 → ≈0.05 and both translation scores above ≈0.93, with a--fullflag for sharper output. ReusesAddUNet+ existing convs (no new layer). - VQ-GAN train-from-scratch (adversarial discrete autoencoder) — the GENERATIVE-CV upgrade of the reconstruction-only VQVAE: trains the SAME discrete autoencoder (conv encoder →
TNNetVectorQuantizercodebook → conv decoder) but with the VQ-GAN objective of Esser et al. 2021 (Taming Transformers) — reconstruction + a perceptual / feature-matching term + the codebook commitment term + a PatchGAN adversarial term switched on only after a warmup. The adversarial + perceptual pressure is what turns blurry L2 VQ-VAE reconstructions into SHARP ones with crisp edges (the whole point of VQ-GAN over a plain-L2 VQ-VAE). Self-contained on a synthetic shapes task (filled circles + rectangles, grayscale in[-1,1], the same family as Pix2Pix); it trains BOTH a plain-L2 VQ-VAE and a VQ-GAN on the IDENTICAL data/architecture and directly compares a high-frequency edge-sharpness metric to demonstrate the payoff. ReusesTNNetVectorQuantizer(straight-through + commitment/codebook gradients), the PatchGAN + LSGAN loop + gradient-surgery trick from Pix2Pix/CycleGAN, and the LPIPS perceptual primitives fromneuralpretrained.pas(feature-matching fallback over the discriminator's own hidden maps — no VGG download). Reports codebook usage + perplexity and renders an ASCII (original | L2-VQVAE | VQ-GAN) panel plus a PPM. No new layer class (the new code is the loss SCHEDULE + commitment balance). Pure CPU. - Image inpainting (context encoder, free-form hole completion) — a FREE-FORM MASK COMPLETION example (Pathak et al. 2016, Context Encoders): fill a hole in an image from its surroundings. Distinct from both image-translation siblings — the UNCONDITIONAL VisualGAN (noise → image) and the PAIRED Pix2Pix (grayscale → color) — here the input and target are the SAME image: a random rectangular region is zeroed out and the network must hallucinate the missing pixels from the visible context. Self-contained, download-free synthetic colored-shapes scenes (a red circle + optional green rectangle over a blue gradient, RGB in
[-1,1]; the CIFAR loader would drop in unchanged but a synthetic scene keeps the smoke run tiny and offline). The two pieces of new code are (1) a random rectangular-hole mask generator — the network input is the masked RGB stacked with the binary mask on the depth axis (4channels[maskedRGB | mask], the standard context-encoder convention so the net knows exactly which pixels are missing), and (2) a masked-region-WEIGHTED reconstruction loss:L1 + (1−SSIM)(reusing the landedneuralimagemetrics.ComputeSSIMLossAndGradienthelper exactly as FrameInterpolation does, per RGB channel) with pixels inside the hole weighted vs outside — the hole is the only part the net cannot trivially copy, so up-weighting it focuses learning where it matters (Pathak et al. use 10×; gentler here because the masked input zeroed the hole too). The custom per-pixel gradient is injected through the standardTNNet.Backpropagatepath via the pseudo-target identityDesired = Output − GradOut. The network itself is a stock conv encoder-decoder + skip connections built by ONETNNet.AddUNetcall (same builder as Pix2Pix/UNetSegmentation) with aTanhoutput — the skips carry visible context across the bottleneck so capacity is spent on the hole. An OPTIONAL adversarial term (--adv) adds a small PatchGAN discriminator (LSGAN) scoring the COMPLETED image (visible context composited with the generated hole), reusing the exact hand-rolled GAN gradient loop from Pix2Pix; it is off by default so the smoke run stays fast. Reports held-out L1/SSIM for the WHOLE image AND for the HOLE interior separately, renders an ASCIImasked | reconstructed | originalpanel and writes the same triplet toinpainting_sample.ppm. Pure CPU (no LCL/image deps); the default SMOKE run (200 train / 40 test, 10 epochs) finishes in ~60 s and drives hole L1 ≈0.73 → ≈0.33 (SSIM ≈−0.02 → ≈0.44), with--advand--fullflags. The diffusion-based inpainting sibling (re-noise only the masked latent region, the RePaint/SDEdit trick — see ImageToImage) is a SEPARATE tracked task. ReusesAddUNet+ the SSIM helper (no new layer). - Neural Style Transfer — an optimise-the-pixels demo (Gatys, Ecker & Bethge, CVPR 2016): a frozen VGG-16 feature extractor (imported with
BuildVGGFromSafeTensors) scores a CONTENT match (MSE between a deep tap's activations) and a STYLE match (MSE between Gram matrices of five tapsrelu1_2..relu5_x), and the canvas image is optimised by gradient DESCENT on the INPUT pixels — the perceptual-loss flip-side of GradientAscent. The new code is a plainComputeGramhelper (G = F·Fᵀ / (C·H·W)over a feature map) plus the style-gradientdL/dFit implies; no new layer class. Gradients are injected at the tap layers and a single manualBackpropagate()from the truncated net's last layer carries them to the input (the net is FROZEN viaSetBatchUpdate(true)+ never callingUpdateWeights, so only the pixels move). Self-contained and CI-runnable: with no--vgg/--content/--styleit synthesises tiny 64×64 images and uses the committed tiny VGG fixture (pipeline demo — style loss 9.82→0.68 in 40 steps, ~0.1 s on CPU), and writes a stylized PNG; point--vgg/--configat real torchvision VGG-16 safetensors and pass real images for genuine artistic output. README documents the importer change that lets backprop reach the input. Pure CPU. - AdaIN Style Transfer — fast arbitrary style transfer (Huang & Belongie, ICCV 2017): the feed-forward counterpart to the optimise-the-pixels StyleTransfer demo. The new code is the parameter-free two-source
TNNetAdaINlayer —TNNetAdaIN.Create(ContentFeatures, StyleFeatures)instance-normalizes the content feature map per channel and re-scales/re-shifts it by the per-channel mean and std of the style feature map (out = style_std·(content − content_mean)/content_std + style_mean); its full input gradient flows to both sources and is numerically gradient-checked in the test suite. The example wires a shared shallow conv encoder → AdaIN → conv decoder, trains the decoder for a handful of iterations on a toy AdaIN-reconstruction objective, then stylizes a synthetic content image with a synthetic style image in a single forward pass (no per-image optimisation loop). Self-contained, no external weights or images; runs in well under a second on CPU underulimit -v 3000000. Pure CPU. - TinyNeRF differentiable volume renderer — a differentiable VOLUME RENDERER (Mildenhall et al. 2020, NeRF): an output modality distinct from every landed image generator (diffusion/GAN/VQ) and from the 2-D implicit-function fits (SIREN, FourierFeaturesSpectralBias) — those are 2-D pixel regressions, this is 3-D ray integration. A tiny coordinate MLP
F(x,y,z) -> (r,g,b,sigma)(Input(3) -> TNNetFourierFeatures(M,sigma)positional encoding — the SAME primitive the Fourier example uses —-> FullConnectReLU(W) -> FullConnectReLU(W) -> FullConnectLinear(4)) is rendered into an image by casting rays from a pinhole camera pose, sampling points along each ray (fixed near/far linspace), evaluatingFat every sample, and alpha-compositing:C = sum_i T_i (1 - exp(-sigma_i·delta_i)) c_iwith transmittanceT_i = exp(-sum_{j<i} sigma_j·delta_j). The new code is this compositing step and its hand-derived backward (the analytic gradient of the composite + transmittance + sigmoid-rgb/softplus-sigma activations w.r.t. each sample's raw MLP outputs, computed in anO(N)far-to-near suffix-sum sweep), fed to the last linear layer'sOutputErrorand driven through the standardBackpropagate()path (last-layerIncDepartingBranchesCnt()once before the loop,ResetBackpropCallCurrCnt()per sample). It is plain-array math in the example driver — no new layer class. The scene is a synthetic analytic coloured sphere ray-marched with a KNOWN emission/density field to generate the posed ground-truth views deterministically inside the program (NO dataset download, NO committed binary images); the MLP learns to reproduce a handful of training poses, then renders a HELD-OUT pose it never saw and writestinynerf_gt.ppm/tinynerf_pred.ppm(P6), reporting held-out PSNR before vs after training to prove the renderer learns (a flat output would mean the compositing gradient is wrong). Default SMOKE run (24×24 render, 5 poses, 8 samples/ray, ~1000 ray batches, ~1.5 min on CPU underulimit -v 3000000) lifts held-out PSNR well above the untrained baseline; scaleImgRes/poses/samples/iters up for a sharper view. Establishes the ray-marching + alpha-compositing primitive future 3-D / view-synthesis work would build on. Pure CPU.
Attention & transformers
- Attention copy task — smallest possible end-to-end attention training demo: a single
TNNetScaledDotProductAttentionhead learns to copy a 16-token input (vocab 8) to its output. Embedding + sinusoidal positional encoding + Q/K/V projection + SDPA + per-position softmax readout; trains on the fly in ~6 s CPU and reports 100% per-token accuracy on fresh probes. - Causal-mask sanity (does the unmasked model cheat?) — trains the SAME tiny
TNNetScaledDotProductAttentionnext-token model twice on a synthetic 2nd-order recurrence (tok[t]=(tok[t-1]+2*tok[t-2]+1) mod 6, target = the next token), differing only by SDPA'sCausalMaskflag (the strictly-upper-triangle-1e9fill, the SDPA-internal equivalent ofTNNetMaskedFill). The unmasked arm can peek at the future token it is asked to predict, so it drives train cross-entropy to ~0 (0.0002vs the masked0.92) and scores 100% teacher-forced — but under true autoregressive evaluation (future positions blanked) the cheat evaporates (26%) and the masked model wins (70%). Three built-in gates (Halt(1)on failure): unmasked train loss < masked (cheating confirmed), masked causal-eval accuracy > unmasked (it actually generalizes), and the unmasked head puts real attention mass on future keys (0.84) while the masked one puts ~0. Pure CPU, deterministic, fast. - Positional-encoding demo — forward-only ASCII-heatmap inspection of the two additive position-encoding layers that ship with neural-api:
TNNetSinusoidalPositionalEmbedding(Vaswani et al. sin/cos table) andTNNetAddPositionalEmbedding(despite the name, also a fixed sin/cos table). Builds a tiny(SeqLen=16, 1, Depth=16)model around each, feeds an all-zero volume so the output IS the encoding table, and renders it. Pure CPU. - Position-encoding bake-off — trains the same tiny causal attention model four times on a predict-the-previous-token task, switching only the position scheme: (a) none, (b) sinusoidal
TNNetAddPositionalEmbedding, (c) RoPETNNetRotaryEmbedding, (d) ALiBiTNNetALiBi. Prints a final-loss table plus one sample prediction per scheme. Sinusoidal and RoPE drive the loss to ~0; the no-position arm is worst and ALiBi (a single-head recency bias, no positional content in the values) sits just above it — showing ALiBi targets locality, not fixed-offset addressing. Pure CPU, all four arms in ~15 s. - CumSum position encoding — Part 1 is a forward-only demo showing that
TNNetCumSumapplied to a constant[1, 1, ..., 1]depth channel produces a strict linear position ramp[1, 2, 3, ...], ready to be concatenated alongside real features viaTNNetConcat. Part 2 is a train-time bake-off on a permutation-invariant "find the marker's position" task: the SAME order-agnostic model (PointwiseConvReLU -> MaxChannel -> FullConnect) is trained twice, differing only in whether the CumSum position ramp is concatenated to the input — the no-position arm sits at chance (~13%) while the CumSum arm solves it (~89%), proving the feature carries the positional signal. Pure CPU, ~19 s. - RoPE base-frequency sweep — empirically interrogates the cargo-culted
10000in RoPE's per-pair frequencytheta_i = base^(-2i/d). Trains the same tiny single-head causal attention model once perbase ∈ {1e2, 1e3, 1e4, 1e5}(the only knob isTNNetRotaryEmbedding.Create(base)) on a position-sensitive copy-the-token-3-steps-back task and prints a train/val-loss + accuracy table. Every arm solves the task (100% val accuracy), but the final loss is monotone inbase— the smaller base resolves this short-range offset more sharply and10000is beaten by100and1000— showing the base is a tunable inductive-bias knob, not a sacred constant. Pure CPU, all four arms in ~49 s, no downloads. - ALiBi slope-base sweep — empirically interrogates the cargo-culted
8in ALiBi's per-head slope2^(-Base*(h+1)/H). Trains the same tiny single-head causal attention model once perBase ∈ {4, 6, 8, 12}(the only knob isTNNetALiBi.Create(Base)) on a recency-sensitive copy-the-most-recent-vowel task and prints a train/val-loss + accuracy table. On this task the trend is monotone — flatter slopes (smallerBase) win and8is beaten by4and6— showing the slope base is a tunable inductive-bias knob, not a sacred constant. Pure CPU, all four arms in ~17 s, no downloads. - Causal-mask + logit SoftCapping interaction study — sweeps the logit soft-cap
c ∈ {5, 10, 20, 30, ∞}(the∞arm omits theTNNetSoftCappinglayer entirely) on the SAME tiny single-head causal next-token model (shared seed/arch/data across arms), asking what soft-cappingc*tanh(x/c)actually does to a softmax's input. It reports TWO logit norms because measuring one is misleading: the EFFECTIVE post-cap norm is always≤ c(that is the bound SoftCapping buys), but becausetanhsaturates the net responds by INFLATING its RAW pre-cap logits (raw-norm balloons to ~51 atc=5vs ~5 uncapped) — the cap conditions the softmax input without taming the underlying projection. Headline lesson: "logit norm" depends on where you measure. Pure CPU, all 5 arms in ~10 s. - Cosine-attention learnable scale — turns the
scalescalar ofTNNetCosineSimilarityAttentioninto a single learnable parameter (the ReZero single-weight pattern; init 1.0) and trains it on a tiny synthetic next-token task to check whether training drives it toward the cargo-culted1/τtemperatures (scale ≈ 10..20) that cosine-attention papers hard-code. On a task that rewards a sharp softmax (each query must copy exactly one key) the learned scale climbs1.0 → ~6.7(still rising, loss → 0); on a smooth task where sharpening does not help it instead collapses toward 0 (uniform attention) — so the learnable scale adapts to the data rather than to a fixed constant. Drives the layer directly (Compute/Backpropagate), pure CPU, no downloads. - Linear attention scaling probe — wall-clock scaling probe for the new softmax-free
TNNetLinearAttentionlayer (Katharopoulos et al. 2020, Transformers are RNNs). It replacessoftmax(QKᵀ)Vwith a positive feature mapφ(x)=elu(x)+1and exploits associativity to accumulate ad_k×d_vkey-value matrix once, so cost is O(SeqLen·d_k·d_v) — linear, with no SeqLen×SeqLen score matrix ever formed. Times one forward pass atSeqLen ∈ {16,32,64,128,256}and prints a table showing ~2× (linear), not ~4× (quadratic), growth per sequence-length doubling. - Linformer low-rank attention — the headline demo for the new
TNNetLinformerAttentionlayer, Linformer self-attention (Wang et al. 2020, Linformer: Self-Attention with Linear Complexity, arXiv:2006.04768). Single-head self-attention that first projects the Key and Value sequences DOWN along the sequence axis fromSeqLento a small fixed rankk ≪ SeqLenwith two LEARNABLE projection matricesE, F(eachk×SeqLen):K' = E·K,V' = F·V, thenAttn = softmax(Q·K'ᵀ / √d_k)(aSeqLen×kscore matrix, notSeqLen×SeqLen) andOut = Attn·V', making attention O(SeqLen·k) instead of O(SeqLen²). SameQ|K|Vinput contract asTNNetScaledDotProductAttention/TNNetLinearAttention(SizeY=1, input depth3·d_k, output depthd_v=d_k);E,Fare stored as two trainable neurons with exact finite-difference-checked input AND weight gradients. BecauseE,Fcarry a fixedSeqLendimension the layer requires a FIXED SeqLen (asserted inSetPrevLayer), the standard Linformer constraint. Distinct from the kernel/feature-map linear-attention family (TNNetLinearAttentionφ(x)=elu(x)+1,TNNetGatedLinearAttention,TNNetDeltaNet,TNNetWKV): Linformer keeps the softmax and instead low-rank-projects the sequence. The demo contrasts the Linformer arm against a full quadraticTNNetScaledDotProductAttentionarm at the SAME SeqLen on a majority-value classification task (predict the most frequent label — a global aggregate, the regime where attention is approximately low-rank): both arms share a Q|K|V projection → attention → readout → per-position softmax, differing ONLY in the attention layer. Headline: at SeqLen 17 the Linformer arm learns the task well above chance (20%) using a score matrix of just 23.5% the size of full SDPA ( vs ) — the classic Linformer trade of a little accuracy for LINEAR attention cost. Pure CPU, ~5 s. - Performer / FAVOR+ random-feature attention — the headline demo for the new
TNNetPerformerAttentionlayer, Performer self-attention (Choromanski et al. 2020, Rethinking Attention with Performers, arXiv:2009.14794). UnlikeTNNetLinearAttention(deterministicφ(x)=elu(x)+1, a different kernel) Performer uses positive random features (FAVOR+) to give an unbiased, low-variance estimate of the actual softmax kernelexp(q·k)at linear cost: for anm×d_kFROZEN (non-trainable) random projectionW,φ(x)=exp(W·x−‖x‖²/2)/√mso thatE[φ(q)·φ(k)]=exp(q·k). Attention then reassociates like the kernel family —S=Σ_s φ(K_s)⊗V_s(m×d_v),Z=Σ_s φ(K_s),Out_t=(φ(Q_t)·S)/(φ(Q_t)·Z)— at O(SeqLen·m·d_v) with noSeqLen×SeqLenscore matrix.W's rows are i.i.d.N(0,1)and, whenm≥d_k, orthogonalized block-by-block (Gram–Schmidt + chi-norm rescale) for the lower-variance "+" in FAVOR+. SameQ|K|Vcontract as the sibling attention layers (SizeY=1, input depth3·d_k, outputd_v=d_k).Wis frozen (no weight gradient) butdL/dQ,dL/dKARE backpropagated throughφ(chaining both theW·xand−‖x‖²/2terms — input gradient finite-difference checked);d_k,mand the RNG seed round-trip viaFStruct[]so the frozenWreloads bit-identically (verified by a save/load round-trip test). Part 1 reproduces the headline FAVOR+ claim: averaged over many random seeds, the RMS error between Performer and fullTNNetScaledDotProductAttentionSHRINKS asmgrows (≈0.118 atm=4→ ≈0.060 atm=128, withQ/Kpre-scaled byd_k^{−1/4}so both arms use the same kernel). Part 2 trains the layer as a drop-in attention block on a majority-value task, reaching 55.7% (chance 20%). Distinct fromTNNetLinformerAttention(keeps softmax, low-rank-projects the sequence axis) and the deterministic kernel family. Pure CPU, <1 min. Covered byTestPerformerAttentionInputGradientCheck/TestPerformerAttentionSerializationRoundTrip. - Latent Attention - Multi-head Latent Attention (MLA, DeepSeek-V2, Liu et al. 2024) via
TNNet.AddMultiHeadLatentAttention: unlike GQA (which shares K/V across query-head groups) MLA low-rank-factors the K/V projection through a tiny shared latentc_KV := x·W_DKV(widthd_c << d_model), then reconstructs per-head K/V by up-projections — so the cacheable per-token state shrinks from2·d_modelto justd_c, a savingd_c/(2·d_model)independent of head count. A three-arm next-token copy bake-off (NoPE MLA, decoupled-RoPE MLA via the builder'sRopeDimparameter — per-head rotated rope-Q plus ONE rope-K projection shared across all heads, with NO absolute positional embedding so position enters only through the rope slice — and a param-matchedAddMultiHeadSelfAttention) shows MLA solving the task at a comparable/lower weight budget. Then the headline KV-cache incremental-decode win is demonstrated and measured: (a) token-at-a-time decode through the SDPABeginIncrementalDecodecache machinery (RoPE arm usesTNNetRotaryEmbedding.PositionOffset := tper step) and (b) a TRUE latent-only cache whose per-token state is justd_cfloats (re-running the up-projections over the cached latents per step) — both match the full re-encode to < 1e-5, and the printed table compares cache bytes/token (32 B latent vs 192 B MHA K+V at d_model=24). Pure CPU, ~35s. - Grouped-Query Attention - GQA/MQA via
TNNet.AddMultiHeadGroupedQueryAttention(QueryHeads, KVHeads, CausalMask)(Ainslie et al. 2023, arXiv:2305.13245):QueryHeadsquery heads share onlyKVHeadskey/value projection heads -- the K and V token-wise projections shrink fromQueryHeads*d_ktoKVHeads*d_kchannels, a factorQueryHeads/KVHeadsfewer K/V projection parameters.KVHeads=QueryHeadsdegenerates to plain MHA (numerically identical toAddMultiHeadSelfAttentiongiven the same projection weights);KVHeads=1is Multi-Query Attention (Shazeer 2019, arXiv:1911.02150). The demo trains three arms differing ONLY inKVHeads(4=MHA, 2=GQA, 1=MQA) on the SAME content-based key->value recall stream and prints the per-arm parameter counts plus held-out recall MSE / exact-recall accuracy, reproducing the GQA paper's headline on a toy: recall quality stays competitive as the K/V heads are shared away. GQA nets stream throughTNNetStreamingDecoderunchanged (one cached SDPA per query head). Pure CPU, well under a minute. Covered byTestMultiHeadGroupedQueryAttention*andTestStreamingDecoderGQAMatchesFullForwardin the test suite. - Sink-attention stability — attention-SINK stability micro-experiment for
TNNetSinkAttention, the "attention sink" idea from StreamingLLM (Xiao et al. 2023). Softmax attention must distribute a full unit of probability mass over the keys; a learnable sink token absorbs the leftover mass so the real keys are not forced to soak up noise. Pure CPU. - Sliding-window vs full-causal attention bake-off - The same tiny causal next-token model trained three times, swapping only the mask layer:
TNNetSlidingWindowMaskedFill(2),(4), and full-causalTNNetMaskedFill. On a content-gated copy task whose answer lives in a width-2 window, both sliding arms match FULL's ~0 loss while inspecting far fewer keys per query (mean 1.92/3.50 vs 6.50) — charting the long-context cost/quality trade. Graded (PASS/FAIL), pure CPU - Differential-attention noise cancellation — the headline noise-cancellation micro-experiment for
TNNetDifferentialAttention(Differential Transformer, Ye et al. 2024). Softmax attention must spend a full unit of probability mass on the keys even when none is relevant; differential attention subtracts two softmax maps to cancel that common-mode noise. Shows the attention noise on an all-keys-irrelevant query position falling versus plain attention. Pure CPU. - Induction Heads - A tiny 2-layer causal attention transformer spontaneously forms an induction head that does in-context copying (Olsson et al. 2022). On a sequence repeated against itself the model learns content-based prefix-matching: second-half (repeated) accuracy ~100% vs first-half ~chance, and the layer-2 attention matrix shows the induction stripe directly as an ASCII heatmap. Pure CPU, ~17s.
- Tiny Transformer FFN - The feed-forward half of a transformer block (no attention): a stack of
AddRMSNormResidual([ AddSwiGLUFeedForward(...) ])pre-norm residual blocks on a per-token denoising toy. Shows the SwiGLU + RMSNorm + residual FFN composes into a stable deeper stack and reconstructs a clean nonlinear target from a noised input (~6x over the do-nothing baseline). Pure CPU, ~15s. - Transformer Decoder Block - Wires an encoder memory into a full encoder-decoder decoder block (
TNNet.AddTransformerDecoderBlock): causal self-attention + cross-attention + SwiGLU FFN residuals. A tiny end-to-end forward/backward demo on the decoder grid. Pure CPU. - TinyGPT — char-level GPT, end-to-end — the capstone decoder-only (GPT-style) transformer demo, built only from existing library blocks: one-hot char
Input(24,1,128) -> PointwiseConvLinear(64)token projection-> AddPositionalEmbedding -> 2x AddTransformerEncoderBlock(Heads=4, d_ff=64, CausalMask=true)(causal multi-head self-attention + SwiGLU FFN, pre-norm residuals)-> FullConnectLinear(128) -> SoftMax(43 layers, ~188k weights). A GPT block is just causal self-attention + FFN, so the causal-masked encoder block builder is exactly a decoder block (the repo'sAddTransformerDecoderBlockis an encoder-decoder block needing cross-attention, which a GPT does not). Trains next-char prediction on a tiny embedded "quick brown fox / lazy dog" corpus (no downloads), streams the dropping loss (accuracy ~0.01 -> ~0.29, loss ~5.2 -> ~3.5 over 8192 examples / 8 epochs, ~1.3 min on a free CPU; the loss falls monotonically and keeps dropping past ~1.0 with more epochs), then autoregressively generates corpus-style continuations from seed prompts withTNNetSamplerTopP(the 8-epoch samples already reproduce corpus n-grams likeog/ox/fog/for). Honest headline: it memorizes the tiny corpus (the expected, acceptable outcome for a sub-5-minute pure-CPU char-LM). The pure-transformer sibling of SimpleNLP (conv/transformer char-LM from a file). After training it also prints a teacher-forced corpus perplexity viaPerplexityFromCharsfromneural/neuralnlpmetrics.pas(perplexity / mean NLL / bits-per-char over the 8 unique sentences, EOS targets excluded; 128 = uniform/untrained, ~1 = perfect memorization). - Sequence packing for LM pretraining — the headline demo for the new
TNNetSequencePacker(neural/neuraldatasets.pas): instead of padding every short document to the context length, documents are concatenated (each followed by one separator token1) and cut into fullContextLenwindows — the GPT-2/GPT-3 pretraining recipe and a large throughput win on pad-heavy corpora. Three layouts:pmSplitAcrossWindows(stream packing, documents may split across window boundaries, only the final partial window padded),pmNoSplitGreedy(greedy bin fill, documents never cross a boundary) andpmOneDocPerWindow(the classic padded baseline). The packer also produces the per-position loss mask:GetTrainingPairleaves pad-target rows all-zero andApplyLossMaskcopies the actual output into the desired output at masked positions, so with the framework'se = Output - Desiredconvention exactly zero gradient flows from padding (verified to 0 intests/TestNeuralPacking.pas). The demo trains the SAME tiny causal RoPE transformer (per-position softmax head,TNNetDyTnorms so no cross-token statistics leak) three times for the SAME 400 optimizer steps on a templated word corpus with held-out sentences, re-shuffling + re-packing every epoch (fixed document order lets the model memorize the stream): padded 33.3% window utilization -> held-out PPL 5.41; no-split packed 73.3% -> 5.22; stream-packed 100% utilization -> PPL 4.35 (neuralnlpmetrics.Perplexity, same wall-clock per step). Attention may cross document boundaries inside a packed window (no per-sample dynamic SDPA masks; standard GPT-2/3 behaviour, noted follow-up in tasklist.md). Pure CPU, ~1 s. - Sequence reverse (attention learns a permutation) — the classic "copy/reverse" probe: a tiny single-block self-attention model learns to output the REVERSE of its input token sequence (
[a..h] → [h..a]), showing that attention can learn a pure POSITIONAL permutation. Stack:TNNetEmbedding → TNNetSinusoidalPositionalEmbedding(the position signal reversal needs) → one non-causal block (PointwiseConvLinear(3*d_k)per-token Q|K|V →TNNetScaledDotProductAttention(CausalMask=False)→PointwiseConvLinear(d_model)→ residualTNNetSum) → per-position readoutPointwiseConvLinear(Vocab) → TNNetPointwiseSoftMax(1), trained with a hand-rolled per-position cross-entropy loop. Reaches 100% per-token AND 100% exact-sequence accuracy on 1000 held-out sequences (chance = 8.33%); CE collapses2.58 → <0.001. Per-token projections usePointwiseConvLinear(notFullConnect, which would flatten/mix the sequence). Pure CPU, ~2 s. - Token Merging (ToMe) — drop half the tokens, keep the accuracy — the headline demo for the new weightless
TNNetTokenMerginglayer (Token Merging, Bolya et al. 2023, ICLR, arXiv:2210.09461) + theAddToMeTransformerBlockbuilder. ToMe shortens a(SeqLen,1,Depth)token sequence to a static(SeqLen-R,1,Depth)by bipartite soft matching: split tokens into alternating A/B sets, score each A-token's cosine similarity to its best B-token, merge the top-R A→B pairs by size-weighted averaging (the paper's proportional bookkeeping), and pass the rest through — with zero trainable parameters (likeTNNetSinkhorn). It is distinct from every other sequence reducer in tree:AddAttentionPooling/AddPerceiverEncoderlearn fixed query slots,AddMixtureOfDepthsroutes/skips tokens, and the pooling layers collapse the whole axis — ToMe instead fuses redundant tokens. Backward is the plain weighted-average adjoint; the top-R selection is frozen per forward pass (like MaxPool's argmax). A small SDPA classifier with one ToMe layer inserted before the deep block drops 50% of its tokens (64 → 32) while accuracy holds (66.3% → 74.5%) and wall-clock falls (~62 s → ~36 s), at no extra parameter cost. Pure CPU. - Span-corruption pretraining (T5/BART objective, from scratch) — the natural end-to-end demo for
neuraldatasets.TNNetSpanCorruptionCollator(the T5/SpanBERT contiguous-span masking collator): a tiny encoder-decoder pretrained FROM SCRATCH on the span-corruption objective. The collator masks contiguous WORD-level spans, collapses each masked span in the ENCODER input to one unique sentinel id (<extra_id_0>,<extra_id_1>, … at the TOP of the vocabulary, descending), and the DECODER is trained to emit the dropped spans as the sentinel/span stream<0> span0 <1> span1 … <final>. Both branches live in ONETNNet(twoTNNetInputlayers, fed together with the array form ofTNNet.Compute) so the loss back-propagates end-to-end through the cross-attention into the encoder — the from-scratch pretraining graph, not the importer's hand-filled-encoder-states convention. Architecture:TNNetTokenAndPositionalEmbedding→ 2×AddTransformerEncoderBlock(bidirectional) →LayerNorm= encoder states; decoderTNNetTokenAndPositionalEmbedding→ 2×AddTransformerDecoderBlock(causal self-attn + cross-attn over the encoder states) →LayerNorm→ per-tokenPointwiseConvLinear(Vocab)→TNNetPointwiseSoftMax(1)LM head. Teacher forcing with a right-shifted decoder; the padded decoder tail is loss-masked by copying the model's own output into thoseFDesiredrows (zero seed, theApplyLossMaskidiom). The corpus is a small strongly deterministic lexicon (the fox chases the rabbit at dawn,the owl hunts the mouse at night, …) where each subject fixes its verb/object, so a masked word-span is recoverable from context — exactly the regularity span corruption is meant to capture (char-level masking of an ambiguous corpus, by contrast, collapses to a constant output — the wrong demo). The inlined greedy seq2seq decode (theDecodeSeq2SeqGreedyconvention: argmax of the per-token logits, autoregressive, padded-causal) reconstructs the held-out masked spans: per-token CE falls ~2.0 → ~0.04, and the reconstructions are clearly INPUT-DEPENDENT (fox→chases, owl→hunts, whale→swims/ocean, …) hitting exact-match on ~10/12 demo lines (~95% per-token target accuracy) — the few misses are honest near-ties (an ambiguous time prepositionatvsin). ~223 K weights; the whole build + 12000 train steps + decode demo runs in ~2 min of pure CPU insideulimit -v 3000000. - Conformer (convolution-augmented transformer) — the headline demo for the new
TNNet.AddConformerBlock(Heads, d_ff, ConvKernelSize)builder, the convolution-augmented transformer block of Conformer (Gulati et al. 2020, Conformer: Convolution-augmented Transformer for Speech Recognition, arXiv:2005.08100). A "macaron" block that sandwiches a multi-head self-attention module (GLOBAL mixing) and a convolution module (LOCAL mixing) between two HALF-step feed-forward modules, every sub-module a pre-norm residual, with a final LayerNorm:x += 0.5·FFN(x); x += MHSA(x); x += Conv(x); x += 0.5·FFN(x); x := LayerNorm(x). It is a builder composed entirely from existing serializable primitives (TNNetLayerNorm,TNNetPointwiseConvLinearper-token projections,AddMultiHeadSelfAttention,TNNetGLUconv gating,TNNetCausalConv1D1-D conv over the time axis,TNNetSwish/SiLU,TNNetSumresiduals,TNNetMulByConstant(0.5)macaron scaling), so it needs no new leaf class and round-trips throughSaveToString/LoadFromString; shape-preserving over(SeqLen,1,d_model)so blocks stack. (The paper's per-channel depthwise conv has no 1-D-over-sequence primitive in tree, soTNNetCausalConv1D— a channel-mixing 1-D sequence conv — is the documented stand-in.) The toy is a per-token tag = (adjacent bigram (3,4) at t) XOR (far token S[0]==1): the bigram is a LOCAL pattern only the conv can localise, S[0] is a LONG-RANGE bit only attention can route, and the XOR forces the model to use both pathways (no pooling — a per-token softmax keeps every position's gradient alive). Headline (asserted, gate > 90%): one block reaches 97% overall with 93% at local-bigram positions (conv pathway) and 84% on global-bit-set sequences (attention pathway). Covered byTestAddConformerBlockShape/TestAddConformerBlockSerializationRoundTrip/TestAddConformerBlockGradientFlowin the test suite. Pure CPU, ~8 s.
Generation & decoding strategies
- Beam-Search Decoding — sequence-level deterministic decoding (
neuraldecodeunit:DecodeGreedy/DecodeBeamSearch/DecodeBeamSearchAll), the missing counterpart to the per-token stochasticTNNetSamplerGreedy/TopK/TopPfamily. Beam search keeps theBhighest cumulative-log-probability partial sequences (log-space, summed — never multiply raw probs), so it can RECOVER from a locally-greedy first-token mistake that single argmax locks in forever. The demo trains a tiny char-level next-token model on a SYNTHETIC corpus deliberately built so greedy dead-ends (the locally-likeliest first token opens a high-entropy branch; a slightly-less-likely token opens a near-deterministic high-probability tail), then prints a Greedy-vs-Beam(B=2,4,8) total-log-prob table, the Wu et al. 2016 length-penaltyαcontrast (α=0short-biased vsα>0), and a diversity contrast against the TopK/TopP samplers. Pure CPU. - Speculative Decoding - Speculative sampling (Leviathan et al. 2023 / Chen et al. 2023) on a tiny pure-CPU draft/target pair: a small fast DRAFT proposes a block of K tokens, the big TARGET scores them in one batched pass, and each token is ACCEPTED with prob
min(1, p_target/p_draft)or, on first rejection, resampled from the renormalised residualmax(0, p_target - p_draft). The committed distribution is provably the target's, exactly — pinned bit-for-bit in the degenerate draft==target case (mandatoryHalt(1)gate) and confirmed empirically with a real imperfect draft (histogram TV ~0.02). A draft-quality sweep shows the accept rate (hence big-model calls saved, ~70→80%) rising with draft/target agreement. Forward-only (recomputes the prefix each pass; KV-cache is the follow-up). Pure CPU, ~70s. - Multi-Token Prediction (MTP) — the headline demo for the new
TNNet.AddMultiTokenPredictionbuilder, Multi-Token Prediction (Gloeckle et al. 2024, Better & Faster LLMs via Multi-token Prediction; scaled up by DeepSeek-V3). Instead of predicting only the next token, the builder taps the shared trunk hidden state(SeqLen,1,d_model)and attachesNumFutureparallel per-token heads, headhforecasting the token att+1+h— each head isPointwiseConvLinear(Vocab) → TNNetPointwiseSoftMax(token-wise 1×1 convs, NOT FullConnect, so the sequence axis survives), and the heads areTNNetDeepConcat'd into one(SeqLen,1,NumFuture*Vocab)output where slabhis thet+1+hdistribution. Supervise it with a matching target whose slabhat positiontis the one-hot of tokent+1+h; the framework's default(output−target)seed gives the standard per-head cross-entropy, so plainBackpropagatetrains all heads at once — densifying the training signal (every position now carriesNumFuturelosses, not one). It is a builder composed from existing primitives (no new leaf class), distinct from the two-net draft+verify SpeculativeDecoding and from the decode-time repetition penaltyTNNetTokenHistoryPenalty— MTP is ONE net with parallel future heads. The toy rule is a deterministic arithmetic progressiontoken[t]=(start+t·step) mod V(step∈{1,2,3,4}); two arms share an identical causal trunk and the same data/eval streams per seed, differing only in the MTP arm's extrat+2/t+3heads. Headline (asserted): at an EARLY checkpoint averaged over 8 seeds the MTP arm reaches higher next-token (t+1) accuracy (mean ≈75% vs ≈70%, ahead in 7/8 seeds) — the auxiliary future losses accelerate the primary head (the win is about convergence SPEED; train to convergence and the easy rule saturates for both). At inference the extra heads are reusable for self-speculative decoding (draftNumFuturetokens in one pass, verify next pass — no separate draft net). Pure CPU, ~12 s. - Self-Speculative Decoding — speculative decoding from one model: the extra future heads of a
TNNet.AddMultiTokenPredictionmodel act as their own draft, dropping SpeculativeDecoding's second draft net entirely (the MTP-as-draft trick deployed by DeepSeek-V3). Each pass forwards the committed prefix plus the pending drafts ONCE: head-0's greedy argmax at rowrverifies the draft at positionr+1(accept the longest prefix of matches), the first mismatch is corrected from the same row (a rejection still commits one token), a full accept yields a bonus token, and heads1..NumFuture-1at the last committed row draft the next block — so each forward commits1..NumFuturetokens. Greedy speculative decoding is exact: every committed token is head-0's argmax over fully-committed causal context, so the output is asserted IDENTICAL to plain one-token-per-forward greedy decoding on every run (Halt(1)gate). On a heavily-overfit char-level toy (vocab 25,NumFuture=4): per-distance accept rates 68.5% / 79.5% / 89.7% (t+2/t+3/t+4; later rates are conditional on earlier accepts), 2.63 tokens committed per pass, 60.9% of forward passes saved (225 vs 576), 2.04× wall-clock (forward-ratio 2.56×). Deterministic smoke test intests/TestNeuralNumerical.pas(TestSelfSpeculativeDecodeGreedyExactness). Forward-only; composing with the SDPA KV-cache (IncrementalDecode) is a separate open task. Pure CPU, ~75 s. - Early-exit / self-speculative decoding (LayerSkip / CALM) — self-speculative decoding from one model with no second checkpoint and no separate prediction head: the model becomes its own draft by reading logits at an intermediate layer through its own LM head (the frozen-body LogitLens splice — snapshot the exit layer's activation, copy it into the head-input slot, recompute ONLY the head sub-stack →
p_exit). The library routineDecodeEarlyExitSelfSpeculative(neural/neuraldecode.pas) draftsargmax(p_exit)when the early exit is confident (max p_exit ≥ Confidence, the LayerSkip/CALM static gate) and verifies it against the full-depth argmax (exact-greedy verify, exactly as SpeculativeDecoding does for a separate draft net). The emitted token is always the full-depth argmax, so the accepted sequence is bit-identical to plain greedy — the early exit only changes how much tail-layer work a cached decoder could skip, never the output. Distinct from SelfSpeculativeDecoding (drafts from MTP prediction HEADS) and EarlyExitNetwork (a BranchyNet CLASSIFICATION demo with trained auxiliary heads): here the draft is the model's own intermediate-layer readout. The example trains a small constant-width char-level LM on a deterministic corpus, asserts the self-speculative continuation equals plain greedy bit-for-bit (Halt(1)gate on mismatch), and reports the accept/reject counters, acceptance rate, and tokens/sec for both paths. Counters from one run:steps=16 drafts=16 accepted=2 rejected=14→ 12.5% acceptance, full-depth ≈1500 tok/s vs self-speculative ≈1330 tok/s (v1 has no cached tail-skip yet, so it does the full forward PLUS a head-only splice and is intentionally a touch slower — the acceptance rate is the speed signal: with the cached tail-skip each accepted token would skip the tail layers, turning acceptance directly into speedup at bit-identical output). Regression testTestEarlyExitMatchesGreedyBitIdentical(+…HighConfidenceMatchesGreedy,…AcceptCountsAreConsistent) intests/TestNeuralDecode.pas. Open follow-up: per-token-adaptive exit + the cached tail-skip. Pure CPU, ~1 min. - Constrained (structured) decoding — the
TNNetTokenConstraint"allowed next tokens" hook (neural/neuraldecode.pas) that the streamed loop applies to the post-softmax row before the sampler. The same tiny untrained char-level net is decoded three ways: free (character soup), JSON mode (TNNetJSONConstraint— a char-level JSON pushdown automaton makes even an untrained model emit ONLY valid JSON, EOS gated until a complete value stands), and forced sequence (TNNetForcedSequenceConstraintmultiple-choice overyes/no/maybe). Every JSON sample is re-checked through a fresh automaton. Pure CPU, seconds. - Structured output (schema-constrained / function-calling decoding) — extends ConstrainedDecoding from a hardcoded free-form JSON grammar to a user-supplied JSON Schema.
CompileJSONSchemaToGBNF(neural/neuraldecode.pas) compiles a JSON Schema into a GBNF grammar the existingTNNetGrammarconsumes, andCreateJSONSchemaConstraintwraps it in aTNNetGrammarConstraint. On aget_weather(location, days, unit)tool-call arguments schema (two required fields + an optionalenum,additionalProperties:false) the same untrained char-level net can ONLY emit JSON that validates against the schema — right keys in declared order, right value types, nothing extra — every sample re-checked through a freshTNNetGrammarmachine and parsed as JSON. Covers the practical subset (object/array/string+enum+pattern/number/integer/boolean/null, anyOf/oneOf,$ref/$defsrecursion). Pure CPU, seconds. - Decode-Efficiency Features Bakeoff —
SimpleNLP/DecodeFeaturesBakeoff.lpr: a 6-phase, 270-seconds-per-phase benchmark of the decode-efficiency features on a REAL tokenized NLP workload (TinyStories, 3k-token vocab — dataset download required, see the SimpleNLP README). Each phase time-boxes training (~190 s, enough for a clear convergence clue) then benchmarks decoding with a hard token-exactness assert (Halt(1)on divergence) between the full re-encode and streamed arms: (1) SDPA KV-cache incremental decode, 20–38× ms/token; (2) MTP-heads self-speculative decode, 0.53 target-fwd/token, 1.90× wall-clock; (3) DiagonalSSM O(1)-per-step decode, 22–65× with FLAT step cost vs prefix length; (4) MLA decoupled-RoPE cached decode, ~25× plus the latent-cache economics table (160 B/token analytic = 15.6% of MHA K+V); (5) speculative decoding composed with the KV cache (TruncateCacherollback) — verification really halves target forwards (0.54), yet plain KV-cached decoding still wins wall-clock on CPU (compute-bound verify), the day's headline negative result; (6) the hybrid the other phases recommend — 2×DiagonalSSM + 1×MLA(latent 32, rope 8) with DyT + SwiGLU and token-only embedding, whose streamed loop drives BOTH mixer families at once (SSM O(1) state + SDPA caches + ropePositionOffset) — final loss 1.39 vs 2.34/2.53/2.87 for the pure SSM/MLA/transformer stacks at the same wall-clock budget, decoding at ~0.35 ms/token, ~31×, flat in prefix length. Run one phase (--phase N) or all six (run_decode_bakeoff.sh). Pure CPU, ≤270 s per phase. - Abstractive summarization with imported BART — the demo for
BuildBartFromSafeTensors(neuralpretrained.pas), the dominant pretrained encoder-decoder for summarization (model_typebart: facebook/bart-large-cnn, sshleifer/distilbart-cnn-12-6). BART is a bidirectional BERT-style encoder + GPT-2-style causal decoder with cross-attention, returned as the same TWO-net pair as the T5/Marian importers (run withRunT5; decode autoregressively with the landedDecodeSeq2SeqBeamSearch). The importer reuses the Marian POST-norm block skeleton with BART's deltas: LEARNED absolute positions with the +2 padding offset (token positionpreadsembed_positionsrowp+2), alayernorm_embeddingLayerNorm after the token+position embeddings, exact-erf GELU FFN (the BERTPhi+ReGLUcomposition),scale_embeddingoff,decoder_start = eos(BART's shift), and the shared embedding tied to the lm_head plus afinal_logits_biasrow. End to end: the checkpoint's GPT-2 byte-level BPEtokenizer.json(already read byTNeuralHFTokenizer— no SentencePiece) encodes the article asbos … eos, token-id beam search writes the summary, and ROUGE-1/2/L F1 (neuralnlpmetrics) score it against a reference. Runs on a built-in demo article with no input files, or point-af/-rfat your own. Pico parity fixture: encoder hidden 1.5e-6 / decoder logits 3.6e-6 vs the float64 HF oracle (TestBartParity, generatortools/bart_tiny_fixture.py). Needs a real BART download for a meaningful summary; keep-enc/-dec/-beamsmall on CPU. Pegasus (model_typepegasus: google/pegasus-xsum, pegasus-cnn_dailymail) is the close PRE-norm cousin and rides the SAME two-net +RunT5+DecodeSeq2SeqBeamSearchpath viaBuildPegasusFromSafeTensors— its deltas vs BART are pre-norm blocks (LayerNorm before each sub-layer), STATIC half-split sinusoidal positions (the Marian table builder, no +2 offset), nolayernorm_embedding, a FINAL encoder and decoderlayer_normclosing each pre-norm stack, andscale_embeddingon. The model import is parity-verified (TestPegasusParity, generatortools/pegasus_tiny_fixture.py); end-to-end text summarization is gated on the SentencePiece/Unigram tokenizer task (Pegasus tokenizes with.spm, not the GPT-2 BPE the Summarize demo uses), but once states are in hand the decode is identical:SummaryIds := DecodeSeq2SeqBeamSearch(Enc, Dec, SourceIds, {StartTokenId=}Config.DecoderStartTokenId, {EOSTokenId=}Config.EosTokenId, {MaxNewTokens=}64, {BeamWidth=}4, {LengthPenalty=}0.8);. - Seq2seq beam-decode + BLEU/ROUGE plumbing (OFFLINE) — a fully self-contained demo of the end-to-end seq2seq decode-and-evaluate pipeline that needs no network access and no multi-GB checkpoint: it imports the committed pico Marian fixture (
tests/fixtures/tiny_marian.*, the same fixture the importer parity test uses) viaBuildMarianFromSafeTensors, runs token-id beam search (DecodeSeq2SeqBeamSearch,neuraldecode.pas) over a fixed source-id sequence, and scores the decoded ids against a reference with corpus BLEU + ROUGE-1/2/L (neuralnlpmetrics, using their token-id overloads so no detokenizer is needed). Because the pico fixture is randomly initialized, the "translation" is gibberish and the decoded-vs-reference scores are meaningless as quality — this is deliberately a PLUMBING demo (import → beam decode → BLEU/ROUGE numbers print), not a quality demo. It asserts the two things that ARE meaningful on random weights: the beam decode is deterministic (an identical re-run reproduces the ids exactly) and the metrics are well-formed (a reference scored against itself gives BLEU = ROUGE-1 F1 = 1.0); the program halts non-zero if either fails. To translate real text, point the sameDecodeSeq2SeqBeamSearchcall at a realHelsinki-NLP/opus-mt-*checkpoint and feed ids from itstokenizer.json(see Summarize for the BART text path). Pure CPU, runs in seconds insideulimit -v 3000000.
Multimodal & vision-language
- CLIP zero-shot classification — the demo for
BuildClipFromSafeTensors(neuralpretrained.pas), a vision-language importer and ViT (model_typeclip: openai/clip-vit-base-patch32 and siblings). CLIP is a contrastive dual encoder returned as TWO independent nets (the T5/Marian two-net convention, but as peers — no cross-attention): a causal pre-LN TEXT tower (token embedding + learned positions, biased q/k/v/out, quick_gelux*sigmoid(1.702x)=TNNetSwishLearnable(1.702),final_layer_norm, bias-freetext_projectionper token, pooled at the eot position viaClipTextEosPosition— both modeling_clip branches: the legacyeos_token_id=2ARGMAX-of-ids rule of every published OpenAI CLIP and the fixed first-eos rule) and a bidirectional ViT VISION tower (bias-free patch conv with kernel = stride = patch_size, the learned class token folded into row 0 of the position table over a zero-padded class slot — exact, no new layer —pre_layrnorm,post_layernorm+ bias-freevisual_projectionper token, image embedding = row 0; factored as the reusableBuildClipVisionTowerfor future ViT/DINO/SigLIP imports). The demo embeds one deterministic test image and N class-prompt token sequences, scores HF-styleexp(logit_scale) * cosine(ClipExtractEmbedding+ClipSimilarity) and softmaxes — offline on the committed pico fixture (its two reference logits reproduce HF'slogits_per_imageexactly; parity ~7e-7 vs the float64 oracle,TestClipParity), or on a real checkpoint passed as argument. Pure CPU, <1 s on the fixture. - CLIPScore (reference-free text↔image alignment metric) — the standard reference-free generative-quality metric (Hessel et al. 2021, arXiv:2104.08718) for text-to-image / image-captioning, reusing the landed CLIP dual encoder
BuildClipFromSafeTensors(neuralpretrained.pas). It runs the image through the vision tower and the prompt through the text tower, L2-normalizes both pooled embeddings and returnsw · max(0, cos(image, text))with the paper'sw = 2.5— a semantic image↔text score that complements the image-only FID / IS / KID (neuralimagemetrics.pas); themax(0, ·)clips the rare negative cosines to 0. New helpers (a metric/helper, NOT a new layer):ClipScore(end-to-end — runs both towers, pools vision row 0 + text at the eot row viaClipTextEosPosition, L2-normalizes),ClipScoreFromEmbeddings(from two pre-extracted unit-L2 embeddings) andRefClipScoreFromEmbeddings— the captioning RefCLIPScore variant (harmonic mean of CLIPScore with the candidate↔reference-caption cosine). The demo scores one image against three prompts and shows the mismatched prompt scores lower (its negative cosine clips CLIPScore to exactly0) while the matching prompt scores higher, then prints a RefCLIPScore — offline on the committedtiny_clippico fixture. Parity (TestClipScore): the torch float64 oracle already ships in the fixture (logits_per_image = exp(logit_scale)·cosine, so cosine= logit/exp(logit_scale)andCLIPScore = max(0, 2.5·cosine));ClipScorereproduces it to< 1e-4andRefClipScoreFromEmbeddingsmatches the harmonic-mean formula to< 1e-5. Pure CPU, <1 s on the fixture. - SigLIP zero-shot classification (the sigmoid-loss dual encoder) — the demo for
BuildSigLIPFromSafeTensors(neuralpretrained.pas), the de-facto vision tower of modern open VLMs (model_typesiglip/siglip2: google/siglip-base-patch16-224 and siblings). SigLIP is a sigmoid-pairwise-loss image-text dual encoder returned as TWO independent nets and reuses CLIP's pre-LN encoder block, but is architecturally DISTINCT from CLIP and is NOT force-fit onto the CLIP path: (a) the score has a learnablelogit_scaleANDlogit_biasand a per-pair SIGMOID (SigLIPLogit=exp(logit_scale)*cosine + logit_bias), so each class is an independent yes/no — not CLIP's softmax-over-classes; (b) the TEXT tower is BIDIRECTIONAL (no causal mask) and pools the LAST token through a biasedhead(text_model.head) — not CLIP's eos-argmax + bias-free projection; the VISION tower pools via a Multihead Attention Pooling head (MAP: one learnable probe query cross-attends over the patch tokens viaTNNetSoftPrompt+TNNetCrossAttention, thenout = attn + mlp(LayerNorm(attn)), row 0) — not a CLS token; (c) the MLP activation isgelu_pytorch_tanh(the tanh-approx GELU); (d) the patch conv is biased, there is NO class token, and the position table covers exactlynum_patchesrows. The reusableBuildSigLIPVisionToweroffers apVisionFeaturesskip-pooling / select-hidden-layer mode for future LLaVA/VLM consumption. The demo embeds one deterministic test image and N class-prompt token sequences and prints both the native per-pair sigmoid match probabilities and a softmax ranking — offline on the committed pico fixture (its reference logits reproduce HF'slogits_per_imageexactly; parity < 1e-4 vs the float64 oracle,TestSigLIPParity, generatortools/siglip_tiny_fixture.py), or on a real checkpoint passed as argument. NaFlex / variable-resolution siglip2 is an explicit follow-up. Pure CPU, <1 s on the fixture. - BLIP image captioning — the demo for
BuildBlipForCaptioningFromSafeTensors(neuralpretrained.pas), a GENERATIVE vision-language importer of the encoder-decoder kind (model_typeblip: Salesforce/blip-image-captioning-base). Unlike the CLIP/SigLIP dual encoders (contrastive, no generation) and unlike a future decoder-only-with-projector LLaVA, BLIP captioning is architecturally a two-net encoder-decoder: a ViT image encoder (the ViT-importer tower — biased patch conv, class token folded into position row 0, pre-LN encoder blocks,post_layernorm; emits allnum_patches+1last_hidden_staterows, no pooling) feeds a BERT-style causal text DECODER through cross-attention (TNNetCrossAttention, the same rectangular Q-from-text / K|V-from-image wiring as the T5/Marian/Pegasus decoders, plus theT5EncoderStatesInputsecond-TNNetInputconvention). Each decoder block is the Marian POST-norm skeleton with BERT's deltas: causal self-attention (attention.self.query/key/value+output.dense/output.LayerNorm), then cross-attention to the image (crossattention.*), then an exact-erf GELU FFN (intermediate.dense+output.dense, thePhi+ReGLUcomposition), then the BERT LM head (cls.predictions: transformLN(GELU(dense(x)))then the vocab decoder). The vision attention loads from BLIP's fusedself_attn.qkvslab ([Q\|K\|V]over all heads) +self_attn.projection. The image is encoded ONCE andDecodeBlipCaptionGreedyrolls out the caption autoregressively frombos_token_id, stopping atsep/eos. The demo greedily captions the committed pico fixture's deterministic test image — offline, printing the generated token ids (decode with the BLIP WordPiece tokenizer for text). Pico parity: per-position next-token logits < 1e-4 vs the float64 HF oracle and the greedy caption ids match HFgenerate()exactly (TestBlipCaptioningParity/TestBlipCaptionGreedy, generatortools/make_pico_blip_fixture.py). A real caption needs a real BLIP download + the WordPiece tokenizer (decode-to-text is the follow-up). Pure CPU, <1 s on the fixture. - LLaVA image captioning — the demo for
BuildLlavaFromSafeTensors(neuralpretrained.pas), a GENERATIVE vision-language importer of the decoder-only-with-projector kind (model_typellava: llava-hf/llava-interleave-qwen-0.5b-hf and siblings) — the classic LLaVA recipe. Unlike the CLIP/SigLIP dual encoders (contrastive, cannot generate) and unlike the BLIP/TrOCR encoder-decoder-with-cross-attention captioners, LLaVA is a decoder-only LM whose token-embedding sequence is spliced with projected visual tokens — no cross-attention, just ordinary causal self-attention over[text-embeds | visual tokens | text-embeds]. Returned as THREE nets: (a) a ViT vision tower in vision-feature mode (BuildSigLIPVisionTower/BuildClipVisionTowerwithpVisionFeatures—vision_feature_layer = -1runs every encoder block but skipspost_layernorm, since HF captureshidden_states[-1]BEFORE the post-norm;SelectHiddenLayer = num_layers + feature_layer + 1selects an earlier layer like CLIP's-2), (b) a 2-layer MLP projector (multi_modal_projector.linear_1 → gelu → linear_2, biased, mapping vision_hidden → text_hidden —BuildLlavaProjector), and (c) the stock Llama/Qwen2 decoder (BuildLlamaFromSafeTensors, itsTNNetEmbeddingfed externally). The new plumbing:LlavaProjectImageruns the tower + projector once;LlavaAssembleEmbeddingslooks up each text token's embedding row and splices the projected visual tokens at theimage_token_indexplaceholder slots;LlavaRunLogitsinjects the assembled(SeqLen,1,d)embedding sequence into the decoder's embedding-layer output and runs the decoder from the next layer onward (skipping the token lookup so the splice survives) — the embedding-injection convention, the decoder-only sibling ofRunT5's external-states feed. The multimodalcfLlavachat template (neuralchat.pas: the llava_v1 vicuna preamble +"USER: <image>\n… ASSISTANT:"turns) renders the prompt. The demo greedily captions the committed pico fixture's deterministic test image — offline (the pico LLaVA is randomly initialized, so the caption ids are gibberish: the demo exercises the image→text PLUMBING). Pico parity: the projected visual tokens AND the mixed image+text next-token logits both < 1e-4 vs the float64 HF oracle (TestLlavaVisualTokenParity/TestLlavaNextTokenLogitsParity, generatortools/llava_tiny_fixture.py);cfLlavaround-trips inTestLlavaMultimodalTemplate. A real caption needs a real LLaVA download + its tokenizer + the SigLIP/CLIP image preprocessing (ReadClipImageProcessorConfig+ClipPreprocessImage); the KV-cache fast decode path (the demo re-runs the full prompt per token) is the follow-up. Opens the door to Qwen-VL / PaliGemma. Pure CPU, <1 s on the fixture; cap memory withulimit -von a real checkpoint. - PaliGemma captioning — the demo for
BuildPaliGemmaFromSafeTensors(neuralpretrained.pas), a PREFIX-LM vision-language importer (model_typepaligemma: google/paligemma-3b-mix-224 and siblings). Structurally PaliGemma is LLaVA-with-a-twist and reuses almost everything: the SigLIP vision tower (BuildSigLIPVisionTowerin feature mode, but — unlike LLaVA'svision_feature_layer = -1— it uses the SigLIPlast_hidden_stateWITHpost_layernorm, soSelectHiddenLayer = 0), the LLaVA splice (LlavaAssembleEmbeddings: text ids look up the √d-scaled Gemma embedding rows, image slots receive the projected visual tokens), and the stock Gemma decoder (BuildLlamaFromTensorReaderWithConfigwith thegemmamodel_type — √hidden embedding scale, +1 RMSNorm gain, decoupledhead_dim). The NEW pieces are (a) a SINGLE biased linear multimodal projector (multi_modal_projector.linear, no gelu, no second layer —BuildPaliGemmaProjector), and (b) the PREFIX-LM attention mask: the image tokens AND the prompt tokens (the "prefix",token_type_id0) attend to all prefix positions with FULL BIDIRECTIONAL attention, while ONLY the generated suffix (token_type_id1) is causal. This is wired with the new transientTNNetScaledDotProductAttention.PrefixLenknob (TNNet.SetAttentionPrefixLen): for queryi/keyj,jis attendable iff causal-allowed (j ≤ i) OR bothi,jare in the prefix block[0..PrefixLen-1].PaliGemmaRunLogitssetsPrefixLenfor the duration of the forward then restores pure causal;PrefixLenstays FIXED at the image+prompt length while the suffix grows. The demo greedily captions the committed pico fixture's deterministic test image — offline (the pico PaliGemma is randomly initialized, so the caption ids are gibberish: the demo exercises the PREFIX-LM image→text PLUMBING). Pico parity: the mixed image+text next-token logits < 1e-4 vs the float64 HF oracle — and the test proves the bidirectional prefix mask is load-bearing by re-running with a causal-everywhere mask (PrefixLen = 0, the LLaVA mask) and asserting the logits differ (TestPaliGemmaLogitParity, generatortools/make_pico_paligemma_fixture.py). Follow-ups: multi-image prompts, a real-checkpoint slicer + the Gemma tokenizer's<image>expansion, 448/896 resolutions, the M-RoPE Qwen2-VL path. Pure CPU, <1 s on the fixture; cap memory withulimit -von a real checkpoint. - TrOCR optical-character recognition — the demo for
BuildTrOCRFromSafeTensors(neuralpretrained.pas), an OCR / image-to-text vertical (model_typevision-encoder-decoder: microsoft/trocr-small-printed, trocr-small-handwritten and siblings) — a cropped text-line image → a transcribed string. Structurally a two-net encoder-decoder seq2seq with a VISION encoder, riding the sameT5EncoderStatesInputtwo-net +RunT5/DecodeSeq2Seq*convention as T5/Marian/Pegasus/BART/BLIP. The DeiT/ViT image encoder is the ViT-importer tower with two OCR-specific traits: a BIASED patch conv (unlike CLIP's bias-free conv), and two prepended special tokens — a class token AND a distillation token — folded into position rows 0 and 1 over zero-padded slots (position_embeddingshasnum_patches+2rows); pre-LN blocks (layernorm_before/layernorm_after, separateq/k/v/o_projloaded into the fused Q|K|V slab,mlp.fc1/fc2, exact-erf GELU) then a finallayernorm; it emits allnum_patches+2last_hidden_staterows (cls + distillation + patches), no pooling. The decoder cross-attends to every one of those rows (TNNetCrossAttention). The TrOCR decoder is a BART decoder reusing the landed BART/Marian post-norm block builder (BuildBartStackBlocks+LoadMarianStack): LEARNED absolute positions with the +2 padding offset, alayernorm_embedding, post-norm blocks, exact-erf GELU FFN, all q/k/v/out/fc Linears biased,scale_embedding(√d_model on the token embeddings), and theoutput_projectiontied to the token embeddings with nofinal_logits_bias. The image is encoded ONCE andDecodeTrOCRGreedyrolls out the transcription autoregressively fromdecoder_start_token_id, stopping ateos. The demo greedily transcribes the committed pico fixture's deterministic test image — offline, printing the generated token ids (decode with the TrOCR GPT-2 byte-level BPE tokenizer for text). Pico parity: per-position next-token logits < 1e-4 vs the float64 HF oracle (TestTrOCRParity, generatortools/make_pico_trocr_fixture.py). A real transcription needs a real TrOCR download + the GPT-2 BPE tokenizer (decode-to-text is the follow-up). Pure CPU, <1 s on the fixture. - Florence-2 unified vision — the demo for
BuildFlorence2FromSafeTensors(neuralpretrained.pas), the repo's first "spatial-output-as-text" VLM (model_typeflorence2: microsoft/Florence-2-base/-large) — a structurally DISTINCT vision-language model that does captioning, detection, segmentation AND OCR through ONE task-prompted seq2seq head, unlike the single-task PaliGemma (prefix-LM caption) or LLaVA (causal chat). The input is an image + a short TASK TOKEN (<CAPTION>,<OD>, ...) and a BART-style decoder emits a text/coordinate token stream parsed per task. The genuinely new idea: boxes and polygons are emitted as quantized LOCATION tokens<loc_0..loc_999>in the vocabulary — spatial outputs as text — viaFlorence2QuantizeCoord/Florence2DequantizeCoord(a normalized coordinate ↔<loc_>token id round-trip). Architecture, riding the landed seq2seq two-net +T5EncoderStatesInputconvention: the ENCODER is a TEXT BART encoder fed a VISUAL-TOKEN PREFIX — the multimodal projector turns the DaViT vision-tower feature map into visual tokens (a learned 2D absolute position embedding = row table + column table concatenated per grid cell, added to the feature map; flatten H·W; a fixed cosine 1D temporal embed; the visual tokens =[spatial-mean ; per-cell tokens]; a bias-freeimage_projectionLinear + a biasedimage_proj_normLayerNorm), those visual tokens are prepended to the embedded+√d-scaled task-prompt text, and the whole[visual; text]sequence runs through the BART encoder (BuildBartStackBlocksREUSE). The BART decoder cross-attends to it (RunFlorence2Logits, the encoder-states feed). The whole projector + visual-prefix encoder + decoder are pinned to the REAL HFFlorence2ForConditionalGenerationfloat64 oracle: pico parity< 1e-4on the decoder logits (TestFlorence2Parity) and the location-token round-trip (TestFlorence2LocationTokens), generatortools/make_pico_florence2_fixture.py(its self-checks assert the projector means/positions, the visual prefix, the cross-attn and the +2 positions all move the reference). The demo runs ONE greedy decoder step (the caption argmax) and prints a sample detection box encoded as<loc_>tokens and decoded back — offline on the committed pico fixture (random weights → wiring smoke, not a trained caption). Scope v1:<CAPTION>+<OD>inference; the DaViT vision tower is the DEFERRED gap — the importer takes the tower'slast_hidden_statefeature map as a precomputed input (mirroring the tracked Qwen2-VL "merged visual tokens as input v1"). A real run needs a real Florence-2 download + its tokenizer + the DaViT tower (depthwise convs + window attention + grouped channel attention + 4-stage conv-embed). Pure CPU, <1 s on the fixture; cap memory withulimit -von a real checkpoint. - Qwen2-Audio audio understanding — the demo for
BuildQwen2AudioFromSafeTensors(neuralpretrained.pas), the AUDIO analogue of LLaVA/PaliGemma (model_typeqwen2_audio: Qwen/Qwen2-Audio-7B-Instruct and siblings) — a clip-in / text-out demo (transcribe / caption / answer a question about an audio clip). Like LLaVA it is a decoder-only LM whose token-embedding sequence is spliced with projected modality tokens — here the modality is audio, not vision. Almost everything is pure REUSE: the audio tower is the Whisper-style log-mel conv+transformer ENCODER (the landedBuildWhisperStackBlocks/LoadWhisperStack/LoadWhisperConv1Dmachinery under theaudio_tower.key spelling — Conv1d+GELU×2 frontend whose stride-2 conv2 halves the mel frames, fixed-table positions, pre-norm blocks, bias-freek_proj, exact-erf GELU), and the text side is the stock Qwen2 decoder (BuildLlamaFromTensorReaderWithConfig, itsTNNetEmbeddingfed externally). The NEW pieces are (a) the Qwen2-Audio encoder TAIL — after the encoder blocks, anAvgPool1d(2, stride 2)over the frame axis (halves the frames a second time) then a final LayerNorm (Qwen2AudioProjectAudiodoes the frame-pair average explicitly, sinceTNNetAvgPool's 2-Dpoolsize²divisor is wrong on a(frames,1,d)grid), so the mel input length2*max_source_positions→max_source_positionsframes after conv2 →max_source_positions//2audio tokens after the pool; (b) a SINGLE biased linear multimodal projector (multi_modal_projector.linear, audio d_model → text hidden —Qwen2AudioBuildProjector); and (c) the embed SPLICE that replaces the<|AUDIO|>placeholder rows with the projected audio frames — the SAME splice the vision-LLM importers use,LlavaAssembleEmbeddingsreused verbatim (audio frames in place of visual tokens).Qwen2AudioRunLogitsruns the tower+pool+norm+projector once, splices, then injects the assembled embedding sequence into the decoder and runs it causally (the embedding-injection convention). The demo greedily answers about the committed pico fixture's deterministic test mel — offline (the pico Qwen2-Audio is randomly initialized, so the answer ids are gibberish: the demo exercises the audio→text PLUMBING). Pico parity: BOTH the projected audio tokens AND the mixed audio+text next-token logits < 1e-4 vs the float64 HFQwen2AudioForConditionalGenerationoracle (TestQwen2AudioParity, generatortools/qwen2audio_tiny_fixture.py). Scope v1: a SINGLE full-length clip + text, single-turn — padded/batched audio (feature_attention_mask, the legacy per-audio expand), the real log-mel frontend from a waveform, and the KV-cache fast decode path are follow-ups. Pure CPU, <1 s on the fixture; cap memory withulimit -von a real checkpoint. - BLIP-2 Q-Former bridge — the demo for
BuildBlip2FromSafeTensors/BuildBlip2QFormerFromSafeTensors(neuralpretrained.pas), a querying-transformer vision-language bridge (model_typeblip-2: Salesforce/blip2-flan-t5-xl and siblings). Unlike the LLaVA/PaliGemma/SigLIP projectors — simple linear/MLP heads from a frozen vision tower into an LLM — BLIP-2's Q-Former is a DIFFERENT bridging module: a small BERT-style transformer fed a fixed set of LEARNED query tokens (query_tokens, e.g. 32) that, in each block, SELF-attend among themselves AND CROSS-attend (crossattention) into the FROZEN ViT patch features, distilling the image into 32 query embeddings, which alanguage_projectionlinear then maps into the LLM token space (spliced ahead of the prompt for the FLAN-T5 decode). The new code is the interleaved self/cross-attention Q-Former block: BERT POST-LN sub-blocks (q := LN(q + attn.output.dense(MHA(q)))) plus, on layers wherelayer_idx mod cross_attention_frequency = 0, a RECTANGULAR cross-attention — the query stream is Q, the (NumPatches, encoder_hidden) ViT features (fed as the net's SECONDTNNetInput, theT5EncoderStatesInputtwo-source convention) supply K|V, so the scores areNumQuery × NumPatchesandencoder_hiddenmay differ fromhidden(per-head leaf =TNNetCrossAttention, exactly the T5/Marian cross-attn wiring) — then the query-specific FFN (intermediate_query/output_query), all in exact-erf gelu (the sameReGLU(Phi|x)composition the BERT importer uses). A model-levellayernormnormalizes the query embeddings first.BuildBlip2FromSafeTensorsreturns the Q-Former net + the learnedquery_tokens(input0) + thelanguage_projectionnet;BuildBlip2QFormerFromSafeTensorsbuilds the standalone Q-Former alone. The FROZEN ViT tower (BuildClipVisionTower, the EVA/CLIP-style ViT) and the FLAN-T5 decode tail (BuildT5FromSafeTensorsviaT5EncoderStatesInput) are REUSE — documented, not built by this v1. The real checkpoints are large / not obtainable offline, so — like the CLIPSeg/LLaVA pico fixtures — it falls back to the committed config-faithful random pico BLIP-2 (tests/fixtures/tiny_blip2_full.*, built bytools/blip2_qformer_tiny_fixture.pyfrom the real HFBlip2QFormerModelfloat64 oracle), parity-checked< 1e-4(TestBlip2QFormerParity— the Q-Former query embeddings, measured ~2.5e-7;TestBlip2FullBridgeParity— the projected query embeddings throughlanguage_projection). The fixture self-checks assert the cross-attention, the query tokens AND the exact-vs-tanh gelu all genuinely move the reference, so the gate is not vacuous. The demo builds the Q-Former + projection, feeds the learned query_tokens and a deterministic synthetic ViT patch grid, runs the bridge and prints the projected query embeddings that splice into the LLM — offline (random pico weights → wiring/throughput smoke, not a trained caption). Scope v1: image-conditioned query distillation with the FLAN-T5 tail reused; deferred are the text-groundeduse_qformer_text_input=trueITC/ITM path,blip2-opt(no OPT importer in-tree), InstructBLIP, and the full ViT→Q-Former→FLAN-T5 caption on a real download. Establishes the Q-Former primitive shared by InstructBLIP and many later VLMs. Pure CPU, <1 s on the fixture; cap memory withulimit -von a real checkpoint.
Audio, speech & music
- Speech Commands keyword spotting — the audio analogue of the simple image classifier and a from-scratch (no pretrained-model import) audio TRAINING example: it proves the log-mel frontend in
neural/neuralaudio.pas(ComputeWhisperLogMel— the same feature extractor that drives the Whisper / Wav2Vec2 importers) is usable for ordinary supervised training, not only checkpoint replay. Pipeline: 16 kHz mono waveform →ComputeWhisperLogMel(the REAL frontend;(NumFrames,1,NumMelBins)= time alongSizeX, mel bins alongDepth) → a small conv stack over the time axis (ConvolutionReLU(24,5)/MaxPool(4)→ConvolutionReLU(32,3)/MaxPool(4)→ConvolutionReLU(48,3)/MaxPool(2)) →FullConnectReLU(64)→Dropout→FullConnectLinear+SoftMax, trained withTNeuralFit(ClassCompareargmax accuracy). The default smoke needs no network: a deterministic synthetic ten-keyword set (fixedRandSeed = 1234) chosen to be genuinely CONFUSABLE rather than trivially separable — three closely-spaced pure tones (430/470/510 Hz, ~9% apart), two two-tone chords that overlap one of those tones, a 7-9 Hz AM (tremolo) tone, fast up/down chirps, and two colored-noise textures differing only in spectral tilt — each with seeded pitch jitter and a comparatively LOW SNR (noiseAmp ≈ 0.18). It is generated in-process and passed through the real frontend, so the whole frontend+training path runs end to end on CPU in under a minute; because the task is hard, validation climbs GRADUALLY (≈0.30 at epoch 1 → 0.65 at 6 → 0.91 at 10 → 0.99 at 16) and only hits the 100% early-stop at epoch 17 rather than saturating at epoch 2, finishing at ≈99% held-out test accuracy (observed 98.93%) far above the 1/10 = 10% chance line. An optional--full <dir>path loads real Google Speech Commands v2 WAVs (one subfolder per label) viaLoadWavResampledToVolume(the new windowed-sinc resampler inneural/neuralaudio.pasaccepts ANY sample rate — a 16 kHz file passes through bit-identically); a tiny downloader (scripts/download_speech_commands.sh, NOT run by the smoke) fetches a few keyword folders. Pure CPU, reproducible. - Whisper speech-to-text (imported openai/whisper-tiny) — an audio demo: transcribes a 16 kHz WAV to text with a real openai/whisper-tiny checkpoint, pure CAI Pascal end to end. WAV reader + the exact HF
WhisperFeatureExtractorlog-mel frontend (400-pt periodic-hann STFT / hop 160 / 80 slaney mel bins /log10/ global max-8 clamp, in the newneural/neuralaudio.pas; parity ~1e-5 vs the float64 oracle) feedsBuildWhisperFromSafeTensors(neuralpretrained.pas, an encoder-decoder import on the same two-netRunT5convention): a Conv1d+GELU×2 frontend whose stride-2 conv halves 3000 mel frames to the 1500-state encoder grid, fixed sinusoidal encoder positions, learned decoder positions, pre-norm blocks with final stack norms, bias-freek_proj, exact erf GELU, rectangularTNNetCrossAttention, tied head. Greedy decode from the<|startoftranscript|><|en|><|transcribe|><|notimestamps|>prologue, byte-level BPE detokenization. Verified on whisper.cpp'sjfk.wav: "And so my fellow Americans ask not what your country can do for you ask what you can do for your country." Pico parity fixture: encoder hidden 2.7e-6 / decoder logits 4.8e-6 vs HF (TestWhisperParity). Needs the ~151 MB download; ~5 min and ~2.5 GB on CPU. Pass--word-timestampsto also emit per-word[start - end]spans with an alignment confidence: the curated "alignment heads" cross-attention is averaged into a (token×frame) matrix, median-filtered (kernel 7, openai-whisper default) to kill single-frame spikes, then DTW'd into a monotonic path (WhisperWordTimestamps, single-30 s-window;TestWhisperWordTimestampsasserts the score/DTW parity, that kernel 1 ≡ no smoothing, kernel-7 monotonic boundaries, and in-range confidence). - Wav2Vec2 / HuBERT speech-to-text (imported facebook/wav2vec2-base-960h) — an audio demo and a self-supervised-encoder ASR: transcribes a 16 kHz WAV with a real facebook/wav2vec2-base-960h (or
facebook/hubert-large-ls960-ft) CTC checkpoint, pure CAI Pascal end to end. Architecturally distinct from Whisper (a mel-spectrogram encoder-decoder that autoregresses tokens): Wav2Vec2/HuBERT is a RAW-WAVEFORM conv feature extractor → transformer encoder → linear CTC head, no decoder, decoded in ONE forward pass.LoadWav16ToVolume(neural/neuralaudio.pas) reads the WAV to mono floats in[-1,1), optionally zero-mean/unit-var normalized (the base-960hdo_normalize), thenBuildWav2Vec2FromSafeTensors(neuralpretrained.pas, model_typewav2vec2/hubert) builds: a multi-layer strided 1-D conv feature extractor (TNNetConvolutionLinearon the(T,1,C)grid, bias-free; first conv →TNNetGroupNorm(channels)per-channel-over-time GroupNorm → GELU, the rest conv → GELU), a feature projection (TNNetTokenLayerNorm+ Linear), a conv-based relative positional embedding (a groupedconv1d, kernelnum_conv_pos_embeddings, weight-norm-parametrized — the effective weight reconstructed fromoriginal0/original1withdim=2; an even kernel makes aTNNetCropSamePad drop the extra frame) added to the projected features thenencoder.layer_norm, a post-LN bidirectional transformer encoder (the SAME BERT block math:LN(x+Attn(x))/final_LN(x+FFN(x)), exact erf GELU), and a linear CTC head.DecodeCTCGreedy(neuraldecode.pas) collapses the per-frame argmax (blank = last vocab id); a tinyvocab.jsonchar map renders the ids (the|delimiter → space; no SentencePiece). HuBERT shares the EXACT topology and CTC head — the SAME importer with thehubertflag. Runs a self-contained pico smoke test (synthetic tone on the committed fixture) with no arguments, or transcribes a real WAV with a downloaded checkpoint. Pico parity fixture: encoder hidden 2.3e-6 / CTC logits 1.2e-7 (wav2vec2) and 2.6e-6 / 2.0e-7 (hubert) vs the float64 HFWav2Vec2ForCTC/HubertForCTCoracle (TestWav2Vec2CTCParity/TestHubertCTCParity, generatortools/wav2vec2_tiny_fixture.py). The-large/robust LayerNorm-everywhere + pre-norm variant is a documented follow-up. Needs the ~360 MB download for a real transcription; CPU-only. - Speaker diarization — "who speaks when" (pyannote/segmentation-3.0 shape) — a speaker-diarization model and a distinct voice vertical: frame-level multi-speaker activity ("who was speaking when"), deliberately not transcription/CTC like the landed Whisper/Wav2Vec2 paths.
BuildPyannoteSegmentationFromSafeTensors[Ex](neuralpretrained.pas, model_typepyannote) imports thepyannote/segmentation-3.0SHAPE: a SincNet learnable band-pass front-end — the new leaf layerTNNetSincConv1D, whose every filter is materialized from just two scalars(low_freq, band)as a Hamming-windowed difference of ideal low-pass sincs — over the raw(T,1,1)waveform, thenabs → MaxPool → TokenLayerNorm, a standard conv block (Conv1d + ReLU → MaxPool → TokenLayerNorm), a bidirectional minimal-LSTM temporal trunk (TNNetMinLSTMforward + time-reversed viaTNNetFlipX, concatenated along Depth — REUSE of the landed LSTM cell), a linear layer, and a per-frame POWERSET multilabel head (the 3.0 model emits the 7 powerset classes covering every subset of ≤ 3 concurrent speakers;PyannotePowersetDecodeturns the per-frame argmax back into a per-speaker binary activity matrix). The smoke synthesizes a short two-tone "two speaker" 4 kHz waveform, runs the net on the committed pico fixture, prints a per-frame speaker-activity timeline and one RTTM line per speaker turn, and saves the clip viaSaveVolumeToWav16(neural/neuralaudio.pas). Pairs naturally with WhisperTranscribe for "who said what". Pico parity (tools/make_pico_pyannote_fixture.py→tests/fixtures/tiny_pyannote*): the per-frame powerset logits match a hand-written numpy float64 forward oracle (SincNet kernel materialization, conv/pool/LayerNorm, BiLSTM, powerset head) to < 1e-4 (TestPyannoteParity, observed ~2e-7). Scope v1: inference-only CPU, raw-waveform in, the pyannote SHAPE on a re-randomized pico fixture (thepyannote.audiopython package is not installed here; a real-checkpoint key-mapping + a vanilla-LSTM trunk are documented follow-ups). The SincNet front-end is also reusable for raw-waveform speaker-verification / keyword-spotting fronts later. Pure CPU, a fraction of a second. - Speaker verification — utterance → embedding → cosine score (ECAPA-TDNN shape) — the companion to SpeakerDiarization and a distinct voice vertical: a discriminative utterance → fixed-length speaker embedding encoder so two clips can be COMPARED (the core of speaker verification, "are these the same person?", and the cross-window clustering a full diarization pipeline needs).
BuildEcapaTdnnFromSafeTensors[Ex]+TEcapaTdnnConfig(neuralpretrained.pas, thespeechbrain/spkrec-ecapa-voxcelebSHAPE) imports the ECAPA-TDNN architecture over a(T,1,NumMel)log-mel frame sequence: aconv_predilated TDNN conv + ReLU, then 3× SE-Res2Block (TNNet.AddSERes2Block) — each a Res2Net hierarchical-residual dilated TDNN cascade built on the new leafTNNetTDNNConv1D(a non-causal centred "SAME" dilated channel-mixing conv whose receptive field grows within the block) with squeeze-excitation channel gating (REUSE of the landedAddSEBlock) — then multi-layer feature aggregation (concat the 3 block outputs + conv), then the new leafTNNetAttentiveStatsPooling(a per-frame attention head whose softmax weights give a context-weighted mean AND standard-deviation over time, concatenated — distinct from the parameter-freeAvgChannel/MaxChanneland fromAttentionPoolingwhich computes neither a weighted std nor the mean‖std concat), and finally a linear → the 192-d-style embedding (AAM-softmax is training-only; inference reads the embedding directly). Speaker verification is exposed asEcapaCosineScore(cosine between two embeddings). The smoke synthesizes three tiny log-mel clips — two phrasings of speaker A and one of speaker B — embeds all three on the committed pico fixture, and prints the same-speaker vs different-speaker cosine scores, showing the same-speaker pair scores higher (observed 0.97 vs 0.75). Pico parity (tools/make_pico_ecapa_fixture.py→tests/fixtures/tiny_ecapa*): the embedding matches a hand-written numpy float64 forward oracle (TDNN convs, the full SE-Res2Block cascade incl. theTNNetAvgChannel/N²scaling quirk, attentive statistics pooling, embedding linear) to < 1e-4, and the verification cosine scores match too (TestEcapaParity). Scope v1: inference-only CPU, the ECAPA SHAPE on a re-randomized pico fixture (speechbrain is not installed here; a real-checkpoint key-mapping + a VoxCeleb EER smoke are network/RAM-gated follow-ups). Pure CPU, a fraction of a second. - Moonshine streaming-ASR encoder (imported UsefulSensors/moonshine-tiny) — a speech-to-text architecture deliberately distinct from Whisper. Whisper pads every clip to a fixed 30 s log-mel spectrogram (so a 1 s utterance costs the same as a 30 s one); Moonshine has NO mel frontend — it convolves RoPE-positioned features directly off the raw 16 kHz waveform with a small strided-conv stem, so the encoder compute scales with the actual audio length.
BuildMoonshineFromSafeTensors[Ex](neuralpretrained.pas, model_typemoonshine) imports the encoder (the v1 parity surface): a raw-waveform conv stem (TNNetConvolutionLinearconv11→hidden k=127 s=64 bias-free →tanh;TNNetGroupNorm(1)over the whole(T,C)block;conv2hidden→2·hidden k=7 s=3 → erf-GELU;conv32·hidden→hidden k=3 s=2 → erf-GELU), then a PRE-norm BIDIRECTIONAL transformer encoder with partial RoPE (partial_rotary_factor, rotates the firstint(head_dim·factor)channels of each q/k head, the tail passes through — the Phi-3 partial-rotary slice wiring reused verbatim), bias-free q/k/v/o, bias-free (gain-only)TNNetTokenLayerNormentry/exit norms, and a standard erf-GELU fc1/fc2 MLP (the encoder MLP, not SwiGLU — the decoder is the SwiGLU tower). The decoder is now imported too:BuildMoonshineEncoderDecoderFromSafeTensors[WithConfig]returns the encoder net and an autoregressive RoPE + SwiGLU transformer decoder that cross-attends the encoder states (the per-block layout isx += self_attn_causal_RoPE(input_layernorm(x))→x += cross_attn(post_attention_layernorm(x), enc_states)with no RoPE on cross-attn →x += swiglu_mlp(final_layernorm(x)), closed by a final gain-only LayerNorm + a tied bias-free LM head; the SwiGLUfc1packs[up|gate]andTNNetSwiGLUcomputesup·SiLU(gate)). The decoder is the standard two-netTNNetInput-pair shape —Layers[0]= decoder token ids, the secondTNNetInputholds the encoder hidden states, filled before eachComputeviaT5EncoderStatesInput/Seq2SeqEncoderStatesInput(the landed convention shared with T5/Marian/Pegasus/Whisper) — soDecodeSeq2SeqGreedy/DecodeSeq2SeqBeamSearchdrive token-id seq2seq decoding (the audio encoder takes a raw waveform, so the example instead callsDecodeMoonshineGreedyCached— a KV-cache incremental decode that caches the self-attn K/V across steps and re-reads the constant cross-attn encoder states each step, running a length-Ltranscript in O(L) instead of the O(L²) re-encode-the-whole-prefix loop; the streamed argmax is bit-identical to that loop,TestMoonshineKVCacheDecodeParity). Runs a self-contained deterministic smoke (committed random fixturetests/fixtures/tiny_moonshine.*, a synthetic tone at two lengths) with no arguments — pure CPU, a fraction of a second — printing the per-length frame count + latency so the length-proportional-compute contrast vs Whisper's fixed cost is visible; with a realmoonshine-tiny/moonshine-basecheckpoint dir (it ships atokenizer.json, loaded viaTNeuralHFTokenizer) it imports encoder+decoder and greedily transcribes to real text. Pico parity (tools/make_pico_moonshine_fixture.py): the encoder hidden states match the HFMoonshineModelfloat64 oracle to < 1e-4 (TestMoonshineEncoderParity), and the decoder next-token logit row matches the HFMoonshineForConditionalGenerationfloat64 oracle to < 1e-4 (TestMoonshineDecoderLogitParity). - EnCodec neural audio codec round-trip (imported facebook/encodec_24khz) — an audio-generative demo: a neural codec that compresses a waveform into a stack of discrete codes and reconstructs it (waveform → codes → waveform), the inverse of the analysis-only Whisper / Wav2Vec2 / HuBERT path. The new building block is Residual Vector Quantization (RVQ): a cascade of
num_codebookscodebooks where each successive one quantizes the residual left by the previous (the single-codebookTNNetVectorQuantizerused by VQ-VAE / MaskGIT is exactly the one-stage special case), so the latent is a grid of codes (one row per codebook, one column per latent frame).BuildEnCodecFromSafeTensors(neuralpretrained.pas, model_typeencodec, the causal weight-normfacebook/encodec_24khzfamily) builds a self-containedTEnCodecModelholder (EncodeAudioToCodes/DecodeCodesToAudio/Reconstruct): a streaming conv ENCODER (causal weight-normConv1dwith reflect left-pad + ELU + resnet blocks + strided downsample convs + a residual 2-layer LSTM bottleneck) → RVQ encode (per frame, argmin-L2 over codebook 0, subtract, argmin over codebook 1, …) → RVQ decode (sum of chosen codebook vectors) → a mirror conv DECODER (ConvTranspose1dupsamplers + LSTM + resnet). Conv weights areweight_norm-parametrized in the checkpoint (original0= g,original1= v); the importer reconstructsw[o] = g[o]·v[o]/‖v[o]‖(dim 0). Runs a self-contained pico smoke test (committed random fixture + a synthesized tone) with no arguments — pure CPU, a couple of seconds — or round-trips a real downloadedencodec_24khz. Pico parity (TestEnCodecRoundTripParity, generatortools/encodec_tiny_fixture.py): RVQ codes match the HFEncodecModeloracle exactly (integer argmin) and the reconstructed waveform to< 1e-4. The 48 kHz stereonormalize=truevariant and the chunked-streaming long-audio path are documented follow-ups; this is the audio decoder the MusicGen / Bark text-to-audio follow-ups build on. - Mimi streaming neural audio codec round-trip (imported kyutai/mimi) — the 12.5 Hz neural audio tokenizer behind Moshi / Kyutai-TTS / Sesame CSM. Mimi extends the EnCodec SEANet + RVQ design with two new pieces: a small causal RoPE transformer bottleneck (pre-norm attention + GELU MLP with per-channel LayerScale residuals) inserted after the conv encoder and before the conv decoder — with a strided downsample
Conv1d/ grouped upsampleConvTranspose1dto step between the EnCodec frame rate and 12.5 Hz — and a split residual vector quantizer: a semantic RVQ (the firstnum_semantic_quantizerscodebooks, distilled at train time, here a nearest-centroid lookup) concatenated with an acoustic RVQ cascade, each owning its 1×1input_proj/output_projconvs and storing each codebook asembed_sum+cluster_usage(effective centroidembed_sum / clamp(cluster_usage, eps)).BuildMimiFromSafeTensors(neuralpretrained.pas, model_typemimi) builds a self-contained channel-majorTNNetMimiholder (Encode/Decode/Reconstruct): causalConv1d(zero/constantleft-pad, plain.conv.weight— no weight_norm) + ELU + resnet blocks → RoPE transformer → downsample → split-RVQ encode → … → upsample → RoPE transformer → mirror conv decoder. The SEANet conv blocks and RVQ codebook math are reused from the EnCodec primitives; the transformer + split-VQ glue is new. The holder carries its signal in double precision (weights stay F32) so the deep conv + high-gain transformer + conv pipeline stays inside the parity gate; the transformer's RoPE uses the Llamarotate_halfconvention and an exacterf-GELU MLP. Runs a self-contained pico smoke test (committed random fixturetests/fixtures/tiny_mimi.*+ a synthesized tone) with no arguments — pure CPU, a couple of seconds — and writes the resynthesized clip to a 16-bit WAV viaSaveVolumeToWav16; or round-trips a real downloadedkyutai/mimi. Pico parity (TestMimiParity, generatortools/mimi_tiny_fixture.py): the split-VQ code stack matches the HFMimiModeloracle exactly and the reconstructed waveform to< 1e-4. Real-checkpoint parity and the streaming chunk-at-a-time padding-cache / KV-cache decode path are documented follow-ups. - DAC (Descript Audio Codec) neural audio codec round-trip (imported descript/dac_*) — the RVQGAN-lineage neural audio codec (
model_typedac, the HFDacModel), the third codec landed here after EnCodec and Mimi and the codec the Parler-TTS importer builds on. DAC keeps the conv-encoder → RVQ → conv-decoder shape but differs from EnCodec/Mimi in three ways the importer reproduces exactly: (1) Snake activations everywhere —x + (1/(α+1e-9))·sin(α·x)²with a learnable per-channel α of shape(1, C, 1)(the in-treeTNNetSnakeis a parameter-free scalar, so the holder applies the per-channel snake math directly); (2) symmetric, non-causal conv padding (padding=padon both sides), unlike EnCodec's causal reflect-left-pad / Mimi's causal constant-left-pad; (3) a factorized, L2-normalized RVQ: each quantizer projects the latent down to a smallcodebook_dimwith a 1×1in_projconv, L2-normalizes both the projected latent and the codebook rows, picks the nearest entry (argmax cosine = argmin L2 of the unit vectors), looks up the raw codebook embedding, projects it back up with a 1×1out_projconv, adds it to the running quantized sum and subtracts it from the residual in the full hidden space.BuildDACFromSafeTensors(neuralpretrained.pas) builds a self-contained channel-majorTNNetDACholder (Encode/Decode/Reconstruct) carrying its signal in double precision (weights stay F32). Each residual unit issnake1 → conv1(k=7, dilated) → snake2 → conv2(k=1)with the input center-cropped before the skip add; conv weights load from a fused.weightor weight_norm (parametrizations.weight.original0/1/ legacyweight_g/weight_v, foldedw = g·v/‖v‖); the decoder ends withTanh. Runs a self-contained pico smoke test (committed random fixturetests/fixtures/tiny_dac.*+ a synthesized tone) with no arguments — pure CPU, a couple of seconds — and writes the resynthesized clip to a 16-bit WAV viaSaveVolumeToWav16; or round-trips a real downloadeddescript/dac_44khz/dac_16khz. Pico parity (TestDACRoundTripParity, generatortools/make_pico_dac_fixture.py): the factorized RVQ code stack matches the HFDacModelfloat64 oracle exactly (max code diff0) and the reconstructed waveform to max |diff| ≈ 9.1e-10 < 1e-4. Real 44 kHz / 16 kHz checkpoint parity is a documented follow-up. - MusicGen text-to-music delay-pattern generation (imported facebook/musicgen-small) — a text-to-audio generative importer: the MusicGen LM decoder predicts a stack of EnCodec codes autoregressively, the inverse of the analysis-only audio path and the audio analogue of the VQ-Model → image-LM pipeline. It composes three landed pieces — a T5 text encoder (
BuildT5FromSafeTensors) for conditioning, a single-stage transformer decoder that emits the code stack, and the EnCodec decoder (BuildEnCodecFromSafeTensors) to synthesize the waveform. The new building block is the delay pattern: each of theK = num_codebookscodebooks is offset by one decode step (codebookk's frameflives at sequence positionf+k, padded before it appears), so a single set ofKLM heads can predict all codebooks causally;MusicGenDelayInterleave/MusicGenDelayDeinterleave(neuralpretrained.pas) match HFbuild_delay_pattern_maskexactly.BuildMusicGenFromSafeTensors(model_typemusicgen) builds a self-containedTMusicGenModelholder whose decoder is the PRE-norm cross-attention block skeleton (the Pegasus path, not post-norm BART) withKsummed code-embedding tables, HFcat([cos, sin])half-split sinusoidal positions, bias-free q/k/v/out + fc, a final decoder LayerNorm, andKuntied LM heads, plus a biasedenc_to_dec_projmapping the T5 hidden size to the decoder hidden size before cross-attention.Generategreedily decodes a[K][frames]code stack through the delay pattern. Runs a self-contained pico smoke test (committed random fixture, a fixed pseudo-encoder-state tensor standing in for the T5 encoder) with no arguments — pure CPU, a fraction of a second. Pico parity (tools/musicgen_tiny_fixture.py):TestMusicGenDecoderParitymatches the HF float64 next-token logits (K × T × vocab) to 0.0 < 1e-4 andTestMusicGenDelayPatternmatches the HF delay-pattern interleave exactly + round-trips. Stereo (audio_channels=2), the full text-prompt → waveform pipeline, KV-cache incremental decode, and top-k/temperature sampling are documented follow-ups. - End-to-end TEXT-CONDITIONED MusicGen generation (prompt → T5 → music → WAV) — the successor to
MusicGenSmoke: it wires the REAL T5 text encoder that the smoke test stubbed with a fixed pseudo-encoder-state tensor, so a free-text prompt (its token ids) genuinely steers the generated music. Full pipeline, no new layer types — pure example wiring composing three landed importers: prompt token ids →BuildT5FromSafeTensorsT5 encoder → its final hidden states (EncSeq × text_d_model) → MusicGenenc_to_dec_proj→ cross-attention conditioning →TMusicGenModel.Generategreedy delay-pattern decode → a[K][frames]EnCodec code stack →BuildEnCodecFromSafeTensorsEnCodec decoder (DecodeCodesToAudio) → mono waveform →SaveVolumeToWav16(neuralaudio.pas) → a short.wavclip. The T5 hidden states feed the exact slotMusicGenSmokefilled by hand; the only difference is they now come from a genuine encoder run, so changing the prompt changes the codes. Runs a self-contained pico demo on committed random fixtures (tests/fixtures/tiny_musicgen_t5enc.*,tiny_musicgen_encodec.*, generated by the extendedtools/musicgen_tiny_fixture.pyat the matchedtext_d_model/codebook_size) with no arguments — pure CPU, a fraction of a second, writesmusicgen_text_demo.wav; weights are untrained random so the clip is noise, not music. Regression testTestMusicGenTextWiringasserts the pipeline runs, is deterministic (same prompt → identical codes + waveform), and that the text conditioning is live (a different prompt steers the codes). Classifier-free guidance is now landed:TMusicGenModel.GenerateCFG(EncStates, UncondStates, NumFrames, GuidanceScale, Codes)runs the decoder TWICE per step — conditional + unconditional — and blends the per-codebook logits asguided = uncond + scale·(cond − uncond)before the argmax (MusicGen's defaultscale = 3.0; the null branch is a ZEROED text condition matching HF). WhenGuidanceScale ≤ 1.0orUncondStates = nilit is bit-identical to plainGenerate. Pass--guidance Nto the example to enable it. Regression testTestMusicGenCFGpins the scale-1.0/nil-uncond equivalence and that a scale-3.0 pass shifts at least one emitted code. KV-cache incremental decode and top-k/temperature sampling are now landed viaTMusicGenModel.GenerateEx(EncStates, UncondStates, NumFrames, GuidanceScale, UseCache, Sampler, Temperature, Codes): withUseCachethe self-attention heads run on a lazily-built width-1 twin decoder (BeginIncrementalDecode, weights copied from the full decoder) and the loop feeds one delayed frame per step instead of re-running the whole prefix — bit-identical to the full re-encode greedy loop (cross-attention re-reads the fixed encoder states; the per-frame sinusoidal position is baked into the fed embedding). A non-nilSampler(anyneuralvolumeTNNetSampler*, e.g.TNNetSamplerWeightedTopK) draws each codebook token fromsoftmax(logits / Temperature);Sampler = nilis the exact argmax. The example defaults to the KV-cache greedy path and accepts--topk N,--temperature N, and--no-cache. Regression testTestMusicGenGenerateExasserts the cached greedy decode equals the full re-encode loop, that a weighted top-k=1 sampler equals greedy on both paths, and that the temperature/top-k path is reproducible at a fixedRandSeed. In--downloadmode the example now seeds its sampling recipe from the checkpoint'sgeneration_config.jsonwhen one ships beside the weights, via the newReadGenerationDefaultsFromDir/ReadGenerationDefaultsFromJSONFilereader (neuralpretrained.pas, parsingtop_k/top_p/temperature/do_sample/guidance_scale/max_length; a missing or unparsable file is graceful —Found=False, no exception).facebook/musicgen-smallpinsdo_sample=true, top_k=250, temperature=1.0, guidance_scale=3.0, which the file now supplies (so a non-standard MusicGen variant with different decode defaults is honored automatically); the hardcodedtop_k=250fallback still applies when the file is absent, and explicit--flagsalways override the file. The reader generalizes beyond MusicGen to any imported generative LM that ships ageneration_config.json(TestGenerationDefaultsFromJSONFile). Stereo (the 2K-codebook layout) and a real tokenizer + large downloaded checkpoint remain documented follow-ups. - MELODY-conditioned MusicGen generation (melody → chroma → music → WAV) — the melody-conditioned sibling of
MusicGenText(facebook/musicgen-melody,model_type "musicgen_melody"): generation is steered by a reference melody (a 12-bin chromagram) instead of (or alongside) text. It reuses the landed text-MusicGen path for the EnCodec codec, the delay-pattern interleaving, theKembedding tables /KLM heads, and the sinusoidal positions; two pieces are genuinely new. (1) The chroma front-endComputeMusicgenMelodyChroma(neuralaudio.pas) matches the HFMusicgenMelodyFeatureExtractorbit-for-bit: a power spectrogram (n_fft=16384,hop=4096, periodic Hann window,center=Truereflect pad, normalized by the window L2 energysqrt(Σwindow²)— exactlytorchaudio.transforms.Spectrogram(normalized=True),power=2) projected through a librosa-stylechroma_filter_bank(BuildChromaFilterBank,tuning=0/power=2/weighting=(5,2)/start_at_c_chroma) onto 12 pitch classes, per-frame inf-norm normalized, then argmax one-hot. (2) A decoder-only architecture: unlike text-MusicGen's cross-attention decoder, the melody decoder is a causal self-attention LM — the conditioning is PREPENDED to the decoder sequence asconcat([audio_enc_to_dec_proj(chroma), enc_to_dec_proj(text)])(chroma first), the chroma part repeat-tiled/truncated tochroma_length, with sinusoidal positions over the whole sequence and logits read at the decoder-frame positions.BuildMusicGenMelodyFromSafeTensors[Ex](neuralpretrained.pas) builds a self-containedTMusicGenMelodyModelholder (BuildConditioningPrefix/ComputeLogits/Generate); the self-attention-only blocks ride the Pegasus block skeleton via a newpSelfAttnOnlyflag (noencoder_attnsub-block). Runs a self-contained pico smoke demo (committed random fixturestests/fixtures/tiny_musicgen_melody.*+ the matchedtiny_musicgen_encodec.*) with no arguments — pure CPU, a fraction of a second: it synthesizes a deterministic A4+E5 reference melody, extracts its chroma (every frame resolves to pitch class 9 = A), prepends the chroma + a fixed text condition, greedily delay-pattern decodes a[K][frames]code stack, decodes it to audio with the EnCodec decoder, and writesmusicgen_melody_demo.wav(untrained weights → noise, exercising the wiring).--no-textruns chroma-only conditioning;--frames Nsets the decoder frame count. Pico parity (tools/make_pico_musicgen_melody_fixture.py,TestMusicGenMelodyParity): the chroma extractor matches the HF float64MusicgenMelodyForConditionalGenerationoracle exactly (one-hot, max |diff| = 0) and ONE decoder forward step (chroma + text prepended) matches the HF logits to < 1e-4. A--downloadreal-checkpoint mode forfacebook/musicgen-melody(and a real-melody-conditioned smoke clip) is a documented follow-up. - VITS / MMS-TTS text-to-speech (imported facebook/mms-tts-eng, kakao-enterprise/vits-ljs) — a text-to-speech model: VITS synthesizes a raw waveform end-to-end from token ids.
BuildVitsFromSafeTensors[Ex](model_typevits,neuralpretrained.pas) builds a self-containedTNNetVitsholder doing the inference pipeline directly on channel-major arrays: a relative-position transformer text encoder (HFVitsAttentionwithemb_rel_k/emb_rel_vwindowed bias) → per-token prior mean/log-variance; a deterministic duration predictor (use_stochastic_duration_prediction=false, the MMS-TTS default) → frames-per-token; a monotonic length regulator that expands the prior along time;prior_latents = mean + z·exp(logvar)·noise_scale; a normalizing FLOW run in REVERSE — the conditional prior IS RealNVP/Glow additive coupling (VITS'slog_stddev≡0, so each layer is a pure shiftsecond_half -= mean(first_half)with the mean from aconv_pre → WaveNet → conv_poststack, channel-flip between layers); and the HiFi-GAN decoder — the SAME generator asBuildHiFiGANFromSafeTensors(TNNetHiFiGAN), reused under thedecoder.key prefix (bias-freeconv_post) → tanh → waveform written withSaveVolumeToWav16(neuralaudio.pas). WaveNet/coupling convs foldweight_normg/v at import. VITS sampling injects prior noise, sozis an explicit input toSynthesizefor a deterministic result. Runs a self-contained pico smoke test (committed random fixturetests/fixtures/tiny_vits.*, fixed ids + fixed noise) with no arguments — pure CPU, a fraction of a second, writes/tmp/tts_smoke.wav; the weights are untrained random so the output is noise, not speech. Pico parity (tools/make_pico_vits_fixture.py,TestVitsSynthesisParity): the text-encoder prior stats, the deterministic durations, the flow run in reverse, AND the end-to-end waveform all match the HFVitsModelfloat64 oracle to < 1e-4 (the oracle'szfed explicitly). A STRING can now be synthesized directly:TNNetVitsTokenizeris the char-level HFVitsTokenizer— it loads a char→idvocab.jsonplus theadd_blank/normalizeflags fromtokenizer_config.json(LoadFromFiles/LoadFromDir) andEncode(text)reproduces HF's exact id sequence (per-char lowercasing, out-of-vocab drop, and the blank/pad id0interleaved between/around every char whenadd_blank=true), cross-checked against the realtransformers.VitsTokenizerinTestVitsTokenizerParity. RunningTextToSpeech "hello world"tokenizes the string (pico vocab) and synthesizes from the derived ids; with no argument the fixed-id smoke test still runs. The uroman romanization and espeak phonemizer front-ends are out of scope (the tokenizer rejectsis_uroman=true/phonemize=trueloudly) — feed already-romanized lowercase text. The stochastic duration predictor and multi-speaker / global conditioning remain documented follow-ups; a real downloaded checkpoint synthesizes a sentence within the ~5 min /ulimit -v 3000000budget. - Kokoro / StyleTTS2 text-to-speech (phonemes → waveform smoke) — a text-to-speech model in the StyleTTS2 family (hexgrad/Kokoro-82M, Apache-2.0), genuinely DISTINCT from the VITS path.
BuildKokoroFromSafeTensors[Ex]/BuildKokoroFromSafeTensorsWithConfig(model_typekokoro,neuralpretrained.pas) builds a self-contained channel-majorTNNetKokoroholder running the deterministic phonemes+style→waveform forward graph directly, wiring the three StyleTTS2 pieces that distinguish it from VITS: (1) style-vector conditioning — astyle_dim-d voice/style vector (an explicit input in v1) is split into a prosody halfs_pred = style[0..H-1]and an acoustic/decoder halfs_dec = style[H..], each AdaIN/affine-injected asAdaIN1d(x,s) = gamma(s)·InstanceNorm_ch(x) + beta(s)(per-channel instance norm over time;[gamma;beta] = fc(s)) into the duration / F0 / energy predictors and the decoder — the new conditioning math vs VITS's WaveNetcondconvs; (2) an iSTFTNet decoder — the generator predicts a magnitudeexp(conv)+ phasesin(conv)spectrogram and runs an inverse STFT to the waveform, reusing the landedISTFTOverlapAdd(mag, phase, …)overlap-add primitive inneuralaudio.pasrather than HiFi-GAN's transposed-conv upsampling; (3) a prosody/duration stack — a style-conditioned duration predictor →round(exp(log_dur)·speed)frames per token → a monotonic length regulator expanding the per-token text encoding along time, then style-conditioned F0 and energy (N) predictors feed the decoder. Every conv reusesTHiFiGANConv/RunHiFiGANConv. The grapheme→phoneme (misaki/espeak) front-end is out of scope — pre-phonemized integer ids are the input andlanguage/g2p/phonemizerconfig is rejected loudly. Runs a self-contained pico smoke test (committed random fixturetests/fixtures/tiny_kokoro.*) with no arguments — pure CPU, a fraction of a second — writing the synthesized clip to a 16-bit WAV viaSaveVolumeToWav16; the weights are untrained random so the audio is noise, not speech. Pico parity (tools/make_pico_kokoro_fixture.py, a self-contained numpy float64 oracle — thekokoro/HF packages are not installed, so the generator defines a faithful StyleTTS2-shaped graph in pure numpy and the holder reimplements it;TestKokoroSynthesisParity): every stage (text-encoder hidden, style-conditioned log-durations + integer durations, length-regulator expansion, F0/energy curves, iSTFTNet magnitude+phase, end-to-end waveform) matches to < 1e-4, with the reference style vector fed explicitly. The real hexgrad/Kokoro-82M checkpoint key mapping (ProsodyPredictor LSTMs, AdaINResBlock1 stacks, harmonic source module), multi-speaker / per-voicevoices/*.ptreference vectors, and the g2p front-end are documented follow-ups. - Bark text-to-speech GPT cascade (model_type
bark, suno/bark) — a text-to-speech generative importer: the autoregressive GPT-style TTS family, genuinely distinct from the VITS / StyleTTS2 (Kokoro) flow-and-decoder paths. Bark chains THREE stacked GPT-2-style decoders then the landed EnCodec decoder (reused verbatim from the MusicGen path) to a waveform: a SEMANTIC model (BarkCausalModel: text+semantic tokens → semantic tokens), a COARSE acoustic model (BarkCausalModel: semantic tokens → coarse EnCodec codebooks), and a FINE acoustic model (BarkFineModel) that predicts the remaining EnCodec codebooks NON-causally over the codebook axis.BuildBarkFromSafeTensors[Ex]+TBarkConfig/ReadBarkConfigFromJSONFile(neuralpretrained.pas, model_typebark) builds a self-containedTBarkModelholder of threeTBarkSubModels. No new leaf layer — each sub-model is a pre-norm GPT-2 block stack (x + out_proj(MHA(ln1(x))),x + out_proj(gelu(in_proj(ln2(x))))) reusingTNNetLearnedPositionalEmbedding+AddMultiHeadSelfAttention+TNNetTokenLayerNormwith exact-erfnn.GELU(theMulByConstant·Erf·…·ReGLUcomposition). The genuinely new wiring: (1) Bark usesnn.Linear([out,in], a new transpose-freeLoadLinearWeights) for the fusedatt_projq|k|v /out_proj/ MLP, with biases gated byconfig.biasand bias-freelm_head(s), unlike GPT-2's HFConv1D([in,out]); (2) the fine model's merged input embedding — one embedding table per codebook (n_codes_totaltables,n_codes_total - n_codes_givenlm_heads), where for target codebookidxthe input is the sum of codebook embeddings0..idxand the trunk runs with bidirectional time attention (TBarkSubModel.ComputeFineLogitsdoes the codebook-sum + per-head selection in Pascal, matching HFBarkFineModel.forward). Runs a self-contained pico smoke (committed random fixturestests/fixtures/tiny_bark_*.safetensors+ the matchedtiny_musicgen_encodec.*codec) with no arguments — pure CPU, a fraction of a second: runs the SEMANTIC → COARSE → FINE cascade end-to-end, EnCodec-decodes the 6-codebook stack, and writes a shortbark_tts_demo.wavviaSaveVolumeToWav16(untrained random weights → noise, not speech). Pico parity (tools/make_pico_bark_fixture.py,TestBarkParity): all three sub-models' forward logits match the HF float64 oracle (BarkSemanticModel/BarkCoarseModel/BarkFineModelin.double()) to max |diff| ≈ 0 < 1e-4 — the semantic + coarse next-token logits over fixed id sequences and the fine model's codebook-conditioned non-causal logits per(sequence, codebook_idx)case; the generator's self-checks prove each quirk (positional embedding, fine codebook conditioning, fine bidirectional-over-time attention) moves the oracle. Realsuno/barkkey-mapping (one nested checkpoint withsemantic/coarse_acoustics/fine_acousticsprefixes), a real tokenizer + voice/history prompt, and full autoregressive sampling are documented follow-ups; this v1 imports the three sub-models from separate files and pins the deterministic forward cascade. - F5-TTS flow-matching voice clone (model_type
f5tts, SWivid/F5-TTS) — a NON-autoregressive, NON-GAN text-to-speech voice cloner, genuinely DISTINCT from the VITS (GAN), Kokoro / StyleTTS2 (adversarial) and Bark / Parler (codec-LM) paths: F5-TTS (Chen et al. 2024) regresses a mel-spectrogram by integrating a conditional-flow-matching ODE through a DiT velocity field, conditioned in-context on a masked reference mel + an embedded character sequence (raw chars / pinyin, no phonemizer), so the model "infills" the target speech in the reference speaker's voice.BuildF5TTSFromSafeTensors[Ex]+TF5Config/ReadF5ConfigFromJSONFile(neuralpretrained.pas, model_typef5tts) builds the DiT velocity field as a four-inputTNNet(x_t, reference cond mel, character ids, scalar timet) with NO new leaf layer — pure composition: (1) a text branch of a char embedding + ConvNeXt-V2 1-D blocks (TNNetDepthwiseConv1Dk=7 → affine-freeTNNetTokenLayerNorm→ pointwise expand → exact-erfTNNetGELUErf→TNNetGRNglobal-response-norm → pointwise project, residual); (2) the genuinely new in-context conditioning —TNNetDeepConcat([x_t, cond, text_emb])→Linear(dim)+ a depthwise conv-positional residual (the F5 InputEmbedding); (3) a time branch —TNNetSinusoidalTimeEmbedding(t·1000)→ SiLU-MLP → conditioning vectorc; (4) a DiT trunk of adaLN-zero blocks (the landedDiTModCond/TNNetFiLMmodulation, chunks) with RoPE SDPA self-attention (AddMultiHeadSelfAttention(..., UseRoPE), the q/k slab loaded with therotate_half→interleaved permute viaLoadLlamaLinearWeights'RotaryHeadDim), then an adaLN norm-out +proj_out→ the mel-width velocity field. The mel is produced by the flow-matching Euler ODE driver (the same machinery as FlowMatching /neuraldiffusion.pas): start atx_0 ~ N(0,I), integratex_{t+dt} = x_t + dt·v_theta(x_t, cond, text, t)fromt=0tot=1. NON-autoregressive — there is no KV-cache (every ODE step is a full parallel forward over the whole sequence, so a cache would not help). Runs a self-contained pico smoke (committed random fixturetests/fixtures/tiny_f5.*) with no arguments — pure CPU, a fraction of a second: builds the velocity field, runs 8 Euler steps and prints the sampled mel statistics (untrained random weights → the mel is noise, not speech). SCOPE v1: imports the DiT velocity field (the genuinely new importable piece) and outputs a MEL — pair it with an already-landed vocoder (Vocos / HiFi-GAN) to reach a waveform. Pico parity (tools/f5_tiny_fixture.py, a self-contained numpy float64 oracle — F5-TTS is not in transformers, so the generator re-implements the officialmodel/backbones/dit.pyforward in pure numpy and the importer mirrors it;TestF5TTSParity): the velocity field matches the float64 oracle to < 1e-4 (the ConvNeXt-V2 text embed, in-context concat input embedding, RoPE adaLN-zero DiT trunk and adaLN norm-out). RealSWivid/F5-TTScheckpoint parity (offline / RAM-gated), the E2-TTS flat-UNet variant, non-defaultrope_theta, and a classifier-free-guidance cond/uncond sweep are documented follow-ups. - Parler-TTS description-conditioned codec-LM decode (model_type
parler_tts, parler-tts/parler-tts-mini-v1) — a description-conditioned text-to-speech model (Lyth & King 2024) in the codec-LM family alongside MusicGen / Bark, genuinely distinct in that it is steered by a free-text STYLE DESCRIPTION ("a female speaker with a slightly low-pitched voice, very clear audio") in addition to the transcript to speak. Parler composes three landed pieces — a (By)T5 text ENCODER (BuildT5FromSafeTensors) encoding the description for cross-attention conditioning, a codec-LM DECODER that autoregressively predicts the DELAY-PATTERNED multi-codebook DAC code stack (architecturally the MusicGen decoder: PRE-norm cross-attention blocks reusing the Pegasus skeleton, BIAS-FREE q/k/v/out + fc1/fc2, K embedding tables summed at the input, the half-split sinusoidal position table, a final decoder LayerNorm, K untied LM heads, the standard MusicGen delay pattern reusingMusicGenDelayInterleave/Deinterleave), and the landed DAC decoder (BuildDACFromSafeTensors) for the waveform — and adds ONE genuinely new wiring step: the DUAL PROMPT. The transcript prompt token ids (what to SAY) are embedded by a separate learned table (embed_prompts) and PREPENDED on the sequence axis before the codec frames, so the decoder self-attends over[transcript_prefix | codec_frames]while ALSO cross-attending the description; the K LM heads are read only at the codec-frame positions (the prefix is pure conditioning context, like MusicGen-Melody's chroma prefix but a learned text-token embedding).BuildParlerTTSFromSafeTensors[Ex]+TParlerConfig/ReadParlerConfigFromJSONFile(neuralpretrained.pas, model_typeparler_tts) builds aTParlerTTSModelholder (ProjectEncoderStates/ComputeLogits/Generate) — NO new leaf layer, the codec decoder reuses the sharedBuildMusicGenDecoderNet. The autoregressive DAC-code decode uses the SDPA KV-CACHE incremental-decode machinery (the same width-1-twin path as MusicGen; cross-attention re-reads the fixed description states each step, self-attention runsBeginIncrementalDecode/EndIncrementalDecode), and is bit-identical to the full re-encode loop under greedy decoding (gated by the test). Runs a self-contained pico smoke (committed fixturetests/fixtures/tiny_parler.*) with no arguments — pure CPU, a fraction of a second: feeds a FIXED description-encoder hidden-state tensor + a FIXED transcript prompt, autoregressively generates the DAC code stack with KV-cache, and prints the per-codebook codes (untrained random weights → the codes are not real speech). Pico parity (tools/parler_tiny_fixture.py, a self-contained numpy float64 oracle —parler_ttsis not installed, so the generator re-implements the published Parler decoder step in pure numpy and the importer mirrors it, the SAME random state dict saved to safetensors;TestParlerTTSParity): the description-conditioned, prefix-prepended codec decoder's next-codebook logits match the float64 oracle to < 1e-4, and the KV-cache decode equals the full re-encode decode. SCOPE v1: imports the description-conditioned codec DECODER (the new importable piece) and drives it standalone with a fixed encoder-state tensor — pairing it with the real (By)T5 encoder + DAC decoder to a waveform (all three already importable in-tree), the real parler-tts-mini-v1 checkpoint key mapping, and classifier-free guidance are documented follow-ups. - Demucs music source separation (imported facebook/demucs / htdemucs time branch) — an audio source-separation model with an audio output modality: one MIXED stereo track in, FOUR stems out (drums / bass / other / vocals).
BuildDemucsFromSafeTensors[Ex](neuralpretrained.pas, model_typedemucs/htdemucs) builds a self-containedTNNetDemucsholder doing the time-domain (waveform) Demucs (Defossez et al. 2019, arXiv:1911.13254) U-Net directly on channel-major arrays — a symmetric 1-D conv stack, reusingTHiFiGANConv/RunHiFiGANConvfor every conv (NO new leaf layer): an encoder ofdepthblocks (, channels doubling, each block saving a U-Net skip) → a bi-LSTM bottleneck (lstm_layersstacked bidirectional cells run inline in the holder — there is no bidirectional-LSTM leaf layer — thenLinear(2C → C)) → a decoder ofdepthblocks (skip-add (Demucs center_trim) → Conv1d → GLU → ConvTranspose1d → ReLUexcept the last) emittingsources·audio_channelschannels reshaped to the 4 stems, center-trimmed to the input length. Conv/nn.LSTMweights load as plain folded tensors (encoder.i.{0,2},lstm.weight_ih_l*/*_reverse,lstm_linear,decoder.i.{0,2}). Runs a self-contained pico smoke test (committed random fixturetests/fixtures/tiny_demucs.*, a synthesized stereo two-tone mix) with no arguments — pure CPU, a fraction of a second — writing each separated stem to a 16-bit WAV viaSaveVolumeToWav16(neuralaudio.pas, stem channels averaged to mono for the mono writer); the weights are untrained random so the stems are noise, not instruments. Pico parity (tools/make_pico_demucs_fixture.py, a self-contained numpy float64 oracle — transformers has no Demucs — built from the published architecture math;TestDemucsSeparationParity): the four separated stems match to < 1e-4. The hybrid time+spectral HTDemucs spectral branch, the v3 cross-domain transformer bottleneck, input/output normalization and a real downloaded checkpoint are documented follow-ups; this v1 lands the time-domain U-Net import. - CLAP zero-shot audio tagging — the demo for
BuildClapFromSafeTensors(neuralpretrained.pas), the audio-domain analogue of CLIP (model_typeclap: laion/clap-htsat-unfused and siblings) for zero-shot audio classification and audio↔text retrieval. CLAP shares CLIP's contrastive head — two towers → projection → L2-normalize →exp(logit_scale_a) * cosine(reusingClipExtractEmbedding+ClipSimilarity, plusClapSimilarityMatrixfor the audio×text grid) — but is genuinely NOT a near-duplicate. The image tower is replaced by an HTS-AT audio tower: a Swin hierarchical windowed-attention transformer over a log-mel spectrogram, which is pure REUSE of the landed Swin machinery (per(head, window)TNNetWindowAttentionwith relative-position bias + cyclic-shift mask,TNNetGatherTokenswindow partition/reverse,SwinBuildWindowLayout/SwinSetWindowBias, the patch-merge reorder) under theclap_audio_modelkey spelling (attention.self.{query,key,value}+attention.output.dense,intermediate.dense/output.dense,attention.self.relative_position_bias_table) with a Conv2d patch-embed + LayerNorm front, a final LayerNorm + token mean-pool, and a 2-layerClapProjectionLayer(linear1 → ReLU → linear2). The text tower is RoBERTa (clap_text_model): token + learned positions offset pastpad_token_id+ a token-type row, post-LN bidirectional blocks (built inline exactly likeBuildBertFromSafeTensors, exact-erf GELU FFN), a BERT-style pooler (dense → tanhon token 0), then the same 2-layer projection. The HF encoder'sBatchNorm2dover the mel axis + thereshape_mel2imgfreq↔time transpose are applied up front byClapBatchNormMelImage(the caller supplies the batch-normed, transposed(time, mel, 1)image, exactly as CLIP supplies normalized pixels). The demo embeds one synthetic log-mel clip and N text prompts, prints the cosine-similarity matrix and the top-1 zero-shot label — offline on the committed pico fixture (parity< 1e-4on BOTH embeddings vs the float64 HFClapModeloracle,TestClapParity, generatortools/clap_tiny_fixture.py), or on a realclap-htsat-unfusedcheckpoint passed as argument. Scope v1:freq_ratio = 1(spec_size = num_mel_bins) andenable_fusion = falseonly — the real laionfreq_ratio = 4mel2img/group-CNN glue and the "fused" windowing are loudly rejected and tracked as follow-ups. Pure CPU, <1 s on the fixture (random pico weights → wiring/parity smoke, not a trained model). - MERT music-representation embedding — the demo for
BuildMERTFromSafeTensors[Ex]+TMERTConfig(neuralpretrained.pas, model_typemert_model/music2vec: m-a-p/MERT-v1-95M), the first MUSIC understanding encoder — the audio analogue of a frozen vision backbone, distinct from the Wav2Vec2 / HuBERT speech encoders only in its pretraining target (music) and its read-out. With the released MERT-v1-95M config (feature_extractor_cqt=false,attention_relax=-1.0,deepnorm=false,do_stable_layer_norm=false) the forward is architecturally identical to HuBERT, so the importer is pure REUSE of the landed Wav2Vec2 / HuBERT path: a raw-waveform strided 1-D conv feature extractor (first conv →TNNetGroupNorm(channels)→ exact-erf GELU, the rest conv → GELU, bias-free), a feature_projection (TNNetTokenLayerNorm+ biased Linear), aweight_norm-parametrized grouped conv positional embedding added to the projected features + encoder LayerNorm, then POST-LN bidirectional transformer blocks (x := LN(x + Attn(x)); x := final_LN(x + FFN(x)), the exactLoadWav2Vec2FeatureConv/LoadWav2Vec2PosConv/LoadLlamaLinearWeights/LoadLayerNormWeightsloaders) — no new leaf layer. The deltas vs the speech path are: tensors at the TOP level (nohubert./wav2vec2.prefix — the MERTModel is the backbone), NO CTC head, and the MERT-specific WEIGHTED-LAYER-SUM music embedding — the deep weighted sum over allnum_hidden_layers+1transformer hidden states (the encoder input after pos-conv+LayerNorm, then each block's output, the HFoutput_hidden_statesorder) with a learned per-layer softmax weight vector (HFuse_weighted_layer_sum/ the*ForSequenceClassificationlayer_weightshead, kept inTMERTConfig.LayerWeightssince the base MERTModel ships none — default uniform). The builder records theN+1hidden-state layers in an out array;MERTWeightedLayerSumpools them after aCompute()into the fixed(1,1,hidden)music embedding (per-frame weighted sum then a mean over the time frames, the HF pooled_output). The demo embeds two synthetic raw clips, prints the embeddings and theirCosineSimilarity(the same cosine the CLAP / ClipSimilarity examples use) — offline on the committed pico fixture (parity< 1e-4on the last_hidden_state, EACH raw transformer hidden state, AND the weighted-layer-sum embedding vs the float64 HFHubertModel-with-weighted-layer-sum oracle,TestMERTParity, generatortools/make_pico_mert_fixture.py), or on a real m-a-p/MERT-v1-95M checkpoint passed as argument. Scope v1: the GroupNorm base path only — the MERT-v1-330M CQT-fused front-end (feature_extractor_cqt=true), the relaxed-attention (attention_relax > 0) and DeepNorm variants are loudly rejected and tracked as follow-ups (along with downloading the real m-a-p/MERT-v1-95Mpytorch_model.bin). Pure CPU, <1 s on the fixture (random pico weights → wiring/parity smoke, not a trained model).
Sequence models, recurrence & SSMs
- String manipulation (next-char) — the smallest "predict the next character in a string" demo: a tiny in-memory 3-string dataset (
'happy good morning.', etc.) trains a char-level next-character model so the full embedding → sequence → readout pipeline is visible end to end. Pure CPU. - Sentiment analysis (SST2) — learns sentiment classification on the SST2 dataset (Stanford Sentiment Treebank, via HuggingFace) with a char/token context model, an end-to-end NLP training program with autosave.
- Byte-processing relation table — a tiny, readable peek inside the symbolic byte engine (
TEasyLearnAndPredictClass) that powersTNNetByteProcessing. Unlike the gradient layers, this engine INDUCES discrete cause→effect rules ("relations") mapping an input byte pattern to an output byte. The demo drives it exactly as the layer does internally (Predict()thennewStateFound()), then prints the learned rules withprintRelationTable. Pure CPU. - Bit-processing shows its work — a hybrid net built on
TNNetBitProcessing(1:1 affine-quantize-each-scalar-to-a-byte sibling ofTNNetByteProcessing) learnsy = a - bona,b in [0,10], then is asked to EXTRAPOLATE to the unseen boxa,b in [10,20]. Because the layer reduces the inputs to a discrete affine CODE that a tinyTNNetFullConnectLinear(1)readout combines, the pipeline is scale-free and barely degrades out of range (RMSE 0.099 → 0.114), while a same-size dense tanh baseline overfits its training box (0.232 → 0.579, ~5x worse extrapolation). The payoff: the symbolic engine PRINTS the human-readable rule it induced (fE[B] := (A[0] - A[1]), the engine's nativecsSub,f=1) via a directly-drivenTEasyLearnAndPredictClassmirror — a "neural net that shows its work" the dense baseline cannot match. Deterministic, pure CPU, <1 s. - TokenShift Baseline - Head-to-head bake-off of the attention-free
TNNetTokenShift(RWKV-style t-1 token mixing) against real self-attention (TNNet.AddMultiHeadSelfAttention) on the same tiny char-level next-token copy task. TokenShift fully solves the lag-1 region for ~3x fewer weights but is stuck at chance on the long-range region that attention routes to — the structural cost/capability trade made concrete. Pure CPU. - Echo State Network (reservoir computing) — the Jaeger 2001 Echo State Network recipe, a training paradigm different from everything else in tree: the recurrent core is fixed and random and only a single linear readout is trained, so there is no backprop-through-time. A sparse random reservoir (
N=100, leak0.3) is rescaled to a target spectral radius via the existingTNNet.EstimateSpectralNormpower-iteration helper; the leaky-integrator stateh_t = (1-a)h_{t-1} + a*tanh(W_in*x_t + W*h_{t-1})is run forward oversin(0.2t)+0.3*sin(0.31t), the states are collected, and only aTNNetFullConnectLinear(1)readout is fit on(h_t -> next value)pairs. It then free-runs autonomously, feeding its own prediction back, and renders predicted-vs-true as an ASCII plot. Built-in gates (Halt(1)on failure): teacher-forced one-step NRMSE0.0161beats a persistence baseline0.2136, and arho>1ablation diverges — proving the spectral-radius<1 echo-state property is what makes it work. Contrasts withDiagonalSSM(which trains its recurrence; the ESN freezes it). Pure CPU, ~4 s. - Spatial Gating Unit (attention-free token mixing) — the gMLP Spatial Gating Unit (
TNNetSpatialGatingUnit/TNNet.AddgMLPBlock, Liu et al. 2021, Pay Attention to MLPs): an attention-FREE sequence mixer with no queries/keys/values and no per-pair dot product — it gates one channel half against a single learned, content-independentSeqLen×SeqLenspatial projection of the other half (Wfixed after training). A two-arm bake-off trains a gMLP block vs a same-parameter-budget single-head attention baseline on a long-range first-token-broadcast toy; both reach 100% per-token accuracy, demonstrating attention-free token mixing actually learns the dependency. The gMLP-paper LayerNorms that bound the multiplicative gate live in the block builder, not the pure-primitive layer. Pure CPU, ~13 s. - MLP-Mixer block (all-MLP sequence mixer) — the attention-free MLP-Mixer block (
TNNet.AddMLPMixerBlock, Tolstikhin et al. 2021, MLP-Mixer: An all-MLP Architecture for Vision): replaces self-attention over a(Tokens,1,Channels)sequence with TWO pre-LayerNorm residual MLPs — a TOKEN-mixing MLP that mixes across token positions (shared over channels, implemented by transposing the token/channel axes viaTNNetTransposeXDand running a pointwise MLP over the new Depth=Tokens axis) and a CHANNEL-mixing per-token pointwise FFN. A small "which-half" token-classification toy plants a spike at one random position and asks whether it landed in the FIRST or SECOND half of the sequence — solvable ONLY by mixing information across tokens. A 2-block all-MLP Mixer stack (2320 weights) reaches 100% train/test accuracy with a cleanly converging cross-entropy loss. Pure CPU, ~6 s. - Tiny Vision Transformer (ViT patch embedding) — a from-scratch ViT image classifier built on the reusable
TNNet.AddPatchEmbedding(PatchSize, EmbedDim, AddClassToken, AddPositionalEmbedding)builder (Dosovitskiy et al. 2021, An Image is Worth 16x16 Words). The builder turns a 2D image into a(SeqLen[+1],1,EmbedDim)token sequence in one call: akernel=stride=PatchSizeconv patchify (TNNetConvolutionLinear) → flatten the patch grid (TNNetReshape) → an optional learnable[CLS]token prepended at position 0 (TNNetSoftPrompt) → an optional learnable positional embedding (TNNetLearnedPositionalEmbedding) — replacing the conv-stride-then-reshape boilerplate patch-tokenizing examples used to hand-roll inline. The demo is a synthetic "which-quadrant" task (an image with one bright spike; classify which quadrant it lands in — needs comparing token positions across the patch grid):AddPatchEmbedding(2,16,classtoken)→ → classify from the[CLS]token, converging to 100% train/test accuracy. Pure CPU, <1 min. - Hyena operator (attention-free implicit long convolution) — the order-2 Hyena operator (
TNNetImplicitLongConv/TNNet.AddHyenaOperator, Poli et al. 2023, Hyena Hierarchy): a sub-quadratic sequence mixer whose per-channel causal filter spans the WHOLE sequence yet is generated IMPLICITLY by a tiny shared MLP over positional features (times a learnable exponential-decay window), so its weight count does NOT grow withSeqLen. A two-arm bake-off trains a Hyena operator vs a single-head self-attention baseline on a long-range recall toy (copy an early random payload to the last position). Hyena's global receptive field wins clearly while using FEWER weights (e.g. recall MSE ~0.0014 with 152 weights vs ~0.090 with 688 for attention). Pure CPU, ~11 s. - Liquid CfC cell (closed-form continuous-time recurrence) — the CfC "liquid" recurrent cell (
TNNetClosedFormContinuous, Hasani et al. 2022, Closed-form continuous-time neural networks, Nature MI): a sequence mixer that updates a hidden state with the ANALYTIC closed-form solution of a liquid time-constant ODE (no ODE solver), gating between a fasttanhinput pathway and the previous state with an INPUT-DEPENDENT, per-channel continuous-time constant — distinct from the numerically-integratedAddNeuralODEBlockand the fixed-decayTNNetRetention/TNNetDiagonalSSM. A two-arm remember-then-recall toy (a cue at position 0 must be reproduced at the last position) trains the CfC cell vs a single SDPA head at MATCHED parameter count (~220 weights each); both reach 100% last-position recall, the CfC by carrying the cue in its liquid state and attention by looking back. Pure CPU, ~3 s. - Liquid CfC vs fixed-decay diagonal SSM (last-write-wins) — the sibling of the LiquidCfC toy, isolating the one thing the CfC liquid cell (
TNNetClosedFormContinuous) has and a fixed-decay diagonal SSM (TNNetDiagonalSSM) does NOT: an INPUT-DEPENDENT time constant. A longer-horizon (SeqLen=16) multi-cue last-write-wins task writes 2–4 cues at random positions (each flagged by a WRITE marker channel) and asks for the MOST-RECENTLY written cue at the last position — which requires input-dependent forgetting: each write must OVERWRITE the previous value. Both models share anembed -> mixer -> LayerNorm -> readoutskeleton; only the mixer differs, and the diagonal SSM is given a wider width (so MORE total weights) since it is cheap per channel (4·dvs the CfC's2·d²+2·d). The CfC's gate reacts to the WRITE pulse and resets its state; the fixed-decay SSM can only blend, so earlier cues bleed in. Result (seed-dependent): CfC 100% (475 weights) vs SSM ~94% (639 weights) — the liquid cell wins despite FEWER parameters. Pure CPU, ~10 s. - sLSTM vs CfC vs diagonal SSM (xLSTM exp-gated recurrence) — the headline toy for the new
TNNetSLSTMCell(TNNet.AddSLSTM), the scalar xLSTM cell (Beck et al. 2024, xLSTM: Extended Long Short-Term Memory) and a classic-LSTM-style multiplicative-gate recurrence (every other mixer —TNNetClosedFormContinuous,TNNetDiagonalSSM,TNNetRetention— is linear-state or fixed/learned decay, with no input/forget/output gates). The sLSTM's distinguishing machinery: EXPONENTIAL input/forget gatesi_t=exp(...),f_t=exp(...)(sharper storage revision than sigmoid) made trainable by a running-max STABILIZER statem_t=max(log f_t+m_{t-1}, log i_t)that renormalizes the unbounded exp gates so they never overflow, plus a normalizern_twith hiddenh_t=o_t*(c_t/n_t). ASeqLen=24copy/state-reset task with an explicit "clear memory" pulse channel contrasts sLSTM against CfC and the diagonal SSM at matched-ish budgets; all three reach 100% last-position recall (sLSTM 950, CfC 330, SSM 150 weights) — honest framing: the toy is learnable by every gated/decay mixer, the point is sLSTM matches them via a fundamentally different exp-gate-plus-stabilizer mechanism. Pure CPU, ~16 s. - Minimal parallelizable RNNs: minGRU & minLSTM — the headline demo for the two new
TNNetMinGRUandTNNetMinLSTMcells, the minimal recurrent units of Feng, Tung, Hassani, Hamarneh & Ravanbakhsh 2024 (Were RNNs all we needed?, arXiv:2410.01201). Both strip a classic GRU/LSTM down to the one recurrence that matters but make every gate a function ofx_tONLY (noh_{t-1}feed):minGRUisz_t=sigmoid(W_z x_t),ht~=W_h x_t,h_t=(1−z_t)⊙h_{t-1}+z_t⊙ht~;minLSTMadds an input gate and NORMALIZES the two gates to sum to one per channel —f'_t=f_t/(f_t+i_t),i'_t=i_t/(f_t+i_t),h_t=f'_t⊙h_{t-1}+i'_t⊙ht~. Because the gates no longer depend on the previous hidden state the recurrence becomes a linear scanh_t=a_t⊙h_{t-1}+b_tthat a parallel prefix-scan can solve — distinct from the xLSTM family (TNNetSLSTMCell/TNNetMLSTMCell), whose gates DO readh_{t-1}(and which add exp gates + a running-max stabilizer). v1 keeps the simple non-log-space parameterization and a sequential scan forward (parallel-scan is a noted follow-up); the backward is exact BPTT — forminLSTMthef/(f+i)normalization couples the two gate gradients and is differentiated exactly (input + all weight tensors finite-difference checked, max-abs err ≤ 0.003). The demo is a selective-copy recall task: one of several value tokens is flagged, and a final query token asks the net to reproduce the marked value — solving it requires latching the marked token into the hidden state and holding it across distractors. Contrasted against a param-comparable memoryless per-token MLP (no cross-time state): both minimal recurrent arms reach near-100% exact recall (minGRU 100% / MSE 0.006, minLSTM 99.3% / MSE 0.009) while the MLP is stuck at the 16.7% chance floor. NOTE: BPTT here is momentum-sensitive, so the recurrent arms train with plain SGD (momentum 0). Pure CPU, ~2 s. - Next-frame video prediction (ConvLSTM) — the headline demo for the new
TNNetConvLSTMCell, a Convolutional LSTM (Shi, Chen, Wang, Yeung, Wong & Woo 2015, Convolutional LSTM Network). It is the spatial, image-state analogue of the dense recurrent cells (TNNetMinLSTM/TNNetSLSTMCell/TNNetMLSTMCell): instead of a vector hidden state it carries(H,W,HiddenC)cell/hidden feature maps and replaces every gate matrix-multiply with a K×K same-padding convolution over the channel-concatenationz=[x_t ; h_{t-1}]—i/f/o=σ(W∗z+b),g=tanh(W∗z+b),c_t=f_t⊙c_{t-1}+i_t⊙g_t,h_t=o_t⊙tanh(c_t)— so the recurrence can track where a moving object is and where it is heading without flattening away the spatial layout (which a dense LSTM would). The task is Moving-MNIST-style next-frame prediction on a self-contained, download-free synthetic dataset: a small bright blob translates across a 12×12 grid at constant integer velocity, bouncing off the walls; the model watches the firstN=4frames and predicts the 5th. The input packs theNframes stacked on the X axis as(N·H, W, 1)(the layout the cell expects), the ConvLSTM emits theNper-step hidden maps(N·H, W, HiddenC), aTNNetCropkeeps only the last timestep's map (the post-sequence summary), and a 3×3TNNetConvolutionLinear+Tanhhead projects it to the predicted next frame in[-1,1]. Training is plain supervised regression (Compute(frames); Backpropagate(nextFrame), MSE); backward is exact BPTT carrying both thedL/dc_tanddL/dh_tspatial maps right-to-left (theh_{t-1}gate feed couples adjacent timesteps — the classic ConvLSTM BPTT bug if truncated; input + all eight weight tensors finite-difference checked, max-abs err ≤ 0.0015). Reports per-epoch held-out MSE/MAE and renders an ASCII (input frames | predicted | ground-truth) panel plus a PPM (inputs gray, prediction green, truth red). Pure CPU; the default SMOKE run (240 train / 40 test clips, hidden 8, 6 epochs, ~28 s) drives held-out MSE from ≈1.20 → ≈0.095 (a ≈92% drop), with a--fullflag (hidden 16, 800/80 clips, 16 epochs) for a sharper prediction. - Video frame interpolation (direct synthesis vs. flow warping) — the headline demo for the new
TNNetFlowWarpdense per-pixel backward-warp primitive, and a video task structurally distinct from VideoPrediction: rather than EXTRAPOLATING the next frame, it interpolates the unseen MIDDLE frame that sits between two endpoints (the RIFE/FILM task). It reuses the same self-contained, download-free Moving-MNIST-style blob world (a bright blob translating across a 16×16 grid, bouncing off the walls); from each 3-frame clip the model sees the two endpointstandt+2(stacked as the two CHANNELS of one image) and is supervised on the hidden middle framet+1. The reconstruction loss is pixel L1 + (1−SSIM) using the landedneuralimagemetrics.ComputeSSIMLossAndGradienthelper (SSIM's 11×11 window is why the grid is 16); the custom per-pixel gradient is injected through the standardTNNet.Backpropagatepath via the pseudo-target identityDesired = Output − GradOut(since the library's last-layer rule isOutputError = Output − Desired). Two model variants are trained and compared: (a) a direct conv encoder-decoder that hallucinates the middle frame's pixels from scratch, and (b) a flow path where the SAME encoder predicts, per pixel, a dense optical-flow field for each endpoint (F0 = mid→t,F1 = mid→t+2),TNNetFlowWarpbackward-warps each endpoint frame along its flow, and the two warps are averaged (symmetric 0.5/0.5 blend). The flow model never has to invent texture — only learn WHERE each pixel moved, a far better-posed problem — so on this rigid-motion data it reaches a lower held-out error than direct synthesis (typical smoke run: flow L1 ≈0.040 / SSIM ≈0.81 vs direct L1 ≈0.056 / SSIM ≈0.49, ~25% lower L1), the textbook illustration of why warping beats direct synthesis for motion. Reports per-epoch train loss and held-out L1/SSIM for both models, renders ASCIIbefore | predicted | truth | afterpanels, and dumps abefore | middle(green=pred, red=truth) | afterPPM triplet for each. Pure CPU (no LCL/image deps); the default SMOKE run (200 train / 40 test clips, 8 epochs) finishes in ~7 s, with a--fullflag for a sharper result. - Optical flow with RAFT (correlation volume + ConvGRU refinement) — an optical-flow, two-image-in / dense-
(dx,dy)-out demo exercising the new RAFT importer (Teed & Deng 2020, RAFT: Recurrent All-Pairs Field Transforms for Optical Flow) end to end. It loads the committed picoraft_smallfixture (tests/fixtures/tiny_raft) withBuildRaftFromSafeTensorsand runs the full forward: a shared feature encoder over both frames → the all-pairsTNNetCorrelationVolume(dot-products between EVERY pair of feature locations — the new primitive) → an iterativeTNNetConvGRUCellupdate operator that, via a localTNNetCorrelationLookuparound the current flow, refines the(dx,dy)field over a fixed small number of steps. The data is self-contained and download-free: a bright textured square on a dark field, with frame-2 a known integer translation of frame-1 (so the true flow is a constant shift — a sanity target). Because the fixture weights are random (this is a forward / plumbing demonstration, not a trained model; point the importer at a real torchvisionraft_smallexport for real flow), the example focuses on the pipeline and the two visualisations it writes:opticalflow_field.ppmcolor-codes the predicted flow the standard Middlebury way (hue = direction, brightness = magnitude), andopticalflow_warp.ppmshowsframe-1 | frame-1 warped toward frame-2 by the predicted flow (TNNetFlowWarp) | frame-2side by side at the/4flow grid — closing the loop with the landed dense-warp primitive (FrameInterpolation'sTNNetFlowWarp). Pure CPU, inference-only, ~1 s. - Retention dual form (parallel == recurrent), with learnable gamma — the headline toy for
TNNetRetention(TNNet.AddRetention, RetNet, Sun et al. 2023, Retentive Network): a softmax-FREE sequence mixer whose only score weighting is a FIXED exponential-decay causal maskD[n,m]=gamma^(n-m)(n>=m, else 0). The demo trains the O(n^2) PARALLEL form on a tiny fixed-offset char-copy task, then runs the SAME trained weights through a hand-rolled O(1)-state-per-step RECURRENT loop (S_n = gamma*S_{n-1} + K_n^T V_n,out_n = Q_n S_n) and gates (Halt(1)) that the two forward passes agree token-for-token to fp tolerance — RetNet's headline dual-form property. A second LEARNABLE-GAMMA arm exercises the new follow-up:TNNetRetention.Create(..., LearnGamma:=true)stores an UNCONSTRAINED raw scalar (1 neuron weight) and usesgamma=sigmoid(raw)so the effective decay is always in (0,1) under plain SGD; backward accumulatesdL/dgammathroughD[n,m](d/dgamma gamma^k = k*gamma^(k-1)) and chains the sigmoid. The arm deliberately inits gamma WRONG (0.50, below the rewarded ~0.90 schedule) and gates that gradient moves it back UP (observed 0.50 -> ~0.78). Pure CPU, ~4 s. - Neural ODE - Continuous-depth residual trunk (
TNNet.AddNeuralODEBlock, Chen et al. 2018): one shared functionfintegrated overStepsexplicit-Euler updatesy := y + h·f(y)(h = 1/Steps), so depth becomes a time axis and the parameter count is independent ofSteps. Trains a tiny classifier whose only trunk is the ODE block and shows accuracy stays high and roughly flat asSteps in {1,2,4}at constant weight count — the "depth for free" property. Pure CPU, ~7s. - Deep Equilibrium - Implicit fixed-point trunk (
TNNet.AddDeepEquilibriumBlock, Bai/Kolter/Koltun 2019): one weight-tied transformfiteratedz := f(z + x)to its fixed pointz* = f(z*; x), so the effective depth adapts to the input at a parameter count independent of the iteration count. Trains a tiny classifier whose only trunk is the DEQ block, reports per-epoch accuracy plus the mean forward iteration-count-to-convergence (the adaptive-depth signal), and runs a param-matchedAddNeuralODEBlockside-by-side (explicit unroll vs implicit fixed point). The backward is the jacobian-free phantom gradient (Geng et al. 2021), honestly disclosed. Pure CPU, ~35s. - Hamiltonian pendulum (symplectic, energy-conserving dynamics) — the headline toy for the new
TNNetHamiltonianCell, a structure-preserving (symplectic) learned-dynamics cell (Hamiltonian Neural Networks, Greydanus, Dzamba & Yosinski 2019, Hamiltonian Neural Networks). Unlike every other continuous-dynamics layer in tree — which regresses the time-derivative field directly and conserves nothing (AddNeuralODEBlock= unconstrained field,TNNetClosedFormContinuous= liquid gate,TNNetDiagonalSSM/TNNetSelectiveSSM= linear state space,TNNetKalmanFilterCell= uncertainty) — this layer parameterizes a scalar learned HamiltonianH_theta(q,p)with a small inner MLP and takes a symplectic step from its gradient (dq=+dH/dp,dp=-dH/dq), so energy is conserved by construction. The forward already needsdH/dz(one backward sweep through the inner MLP), so the training backward differentiates through that gradient — a Hessian-vector product ofHdone as a second tape pass (no Hessian materialized). The demo trains on noisy(q,p)samples of an ideal pendulum and contrasts a long autoregressive rollout against an unconstrained NeuralODE-style residual MLP field of identical width. Headline: both fit the one-step transition equally well (down to the noise floor), yet over 800 rollout steps the HNN conserves energy ~4× better (max|dE|≈ 0.16 vs ≈ 0.63) — the free field drifts off its level set while the symplectic cell stays on it. Pure CPU, ~30 s. - DeltaNet delta-rule associative recall — the headline demo for the new
TNNetDeltaNet, the delta-rule linear-attention recurrence (Yang et al. 2024, Parallelizing Linear Transformers with the Delta Rule over Sequence Length, arXiv:2406.06484). It carries a(d×d)matrix memorySand updates it per timestep with the classic delta (Widrow–Hoff) rule: it first READS the current value predictionS_{t-1}ᵀ k_t, measures the error against the targetv_t, and writes back ONLY the correction scaled by a per-token write strengthβ_t = sigmoid(…) ∈ (0,1)—S_t = S_{t-1} + β_t·k_t ⊗ (v_t − S_{t-1}ᵀ k_t), read-outy_t = S_tᵀ q_t. This removes-then-adds associations (a true editable associative memory), unlikeTNNetRetention(fixed/learned exponential decay),TNNetMLSTMCell(unbounded outer-product accumulation) orTNNetSLSTMCell(scalar exp-gated) — none of which do error-correcting writes. Keys are L2-normalized for stability and the exactdL/dSis carried right-to-left through the rank-1 write (intok,v,βANDS_{t-1}) and the read-out (input+weight gradients finite-difference checked). The demo is an overwrite key→value recall task: a key is written, then RE-written with a new value, then queried for its MOST RECENT value — retrieving it requires erasing the stale association, exactly the delta rule's job. Headline: at a matched parameter budget the delta-rule arm reaches 100% exact recall (MSE 0.006) vs the fixed-decayTNNetRetentionbaseline's 93% (MSE 0.023), which blends the stale and fresh values. Pure CPU, ~2 s. - Legendre Memory Unit delayed-signal reconstruction — the headline demo for the new
TNNetLegendreMemoryUnit, the HiPPO-LegS Legendre Memory Unit (Voelker, Kajić & Eliasmith 2019, Legendre Memory Units: Continuous-Time Representation in Recurrent Neural Networks, NeurIPS 2019). Each input channel carries an order-N memory vectorm_tholding the coefficients of an orthogonal shifted-Legendre-polynomial projection of a sliding window of that channel's signal, driven by the dense, structured, NON-diagonal HiPPO-LegS transition matrixA_ij = (2i+1)·(−1 if i<j else (−1)^{i−j+1}),B_i = (2i+1)·(−1)^i, discretized once at build time (Euler,Ā = I + (1/θ)A,B̄ = (1/θ)B) and run as the linear recurrencem_t = Ā·m_{t−1} + B̄·u_t. A trainable per-channel read-out collapses the N coefficients to one value per step. The fixedĀ/B̄need NO gradient; only the read-out trains, and backward is a clean right-to-left adjoint scan (dL/dm_{t−1} = Āᵀ dL/dm_t, input picks upB̄ᵀ dL/dm_t) with the read-out weight gradient — both finite-difference checked (max-abs err ≈2e-4), plus a brute-force discretization smoke check. Memory cost is N numbers regardless of window length, distinct from the diagonal/complex-diagonal/matrix-memory mixers in tree (TNNetDiagonalSSM,TNNetLRU,TNNetDeltaNet,TNNetGatedLinearAttention): the state mixes ALL N coefficients through a fixed polynomial-projection operator. The demo is a pure-delay reconstruction task — a smooth random 1-D signal must be reproduced delayed byDsteps — exactly what an orthogonal sliding-window memory makes trivial and a leaky scalar accumulator cannot. Headline (asserted): at a smaller parameter budget the LMU arm reaches MSE 0.0008 vs a state-matchedTNNetDiagonalSSMarm's 0.0034 (40 vs 192 params). Pure CPU, ~10 s. Noted follow-up: a learnable window length θ. Covered byTestLMU*in the test suite.θis a fixed build-time constant in v1. - RWKV WKV time-mixing recurrence — the headline demo for the new
TNNetWKVlayer +TNNet.AddRWKVTimeMixbuilder, the weighted key-value (WKV) time-mixing operator that defines RWKV-4 (Peng et al. 2023, RWKV: Reinventing RNNs for the Transformer Era, arXiv:2305.13048) — a softmax-free, attention-free sequence mixer. Over ak|v-split(SeqLen,1,2C)sequence it computes the numerically-stabilized exponential-decay KV averagewkv_t = (a_{t-1} + e^{u+k_t} v_t)/(b_{t-1} + e^{u+k_t})with running accumulatorsa_t = e^{-w}a_{t-1} + e^{k_t}v_t,b_t = e^{-w}b_{t-1} + e^{k_t}, a learnable per-channel positive decayw = softplus(w_raw)and a per-channel "bonus"uthat up-weights the current token (the running max is carried in log-space soe^{k}never overflows). This is distinct from every other linear-attention layer in tree:TNNetSelectiveSSM(input-dependent SSM),TNNetDeltaNet(error-correcting delta rule),TNNetDiagonalSSM(LTI),TNNetRetention(fixed-decay outer-product) — WKV is the exponential-decay KV average with a current-token bonus. Exact BPTT runs two coupled right-to-left adjoint scans over the cached accumulators (input/w/ugradients finite-difference checked). TheAddRWKVTimeMixbuilder composes the leaf withTNNetTokenShift+{r,k,v}pointwise projections + a sigmoid receptance gate + output projection. The demo contrasts the WKV time-mix arm against aTNNetDeltaNetarm on an associative-recall task: both reach 100% exact recall (WKV MSE ≈0.006, DeltaNet ≈0.003) well above the 16.7% chance floor. Pure CPU, ~30 s. - RWKVDecode flat-memory recurrent decoding — the constant-memory autoregressive decode headline of RWKV (the point of
BuildRWKVFromSafeTensors), exercised on the coreTNNetWKVrecurrence via its new incremental state-carry API:BeginIncrementalDecode/Compute(one token) /ResetState/ResetCache/EndIncrementalDecodeplusCaptureState/RestoreStatesession fork — names that mirrorTNNetDiagonalSSMand the SDPA KV-cache so a decoder drives every recurrent layer type the same way. Where the ordinaryCompute()re-runs a left-to-right scan over the WHOLE sequence each call,ComputeIncremental()advances one token in O(1): it resumes from the persisted RWKV-v4 running numerator/denominator state(A,B,Q)(a fixed3·C-float triple, independent of position) instead of restarting at(0,0,-∞), applying the EXACT same log-space-stable single-step update. The demo shows two facts. (A) Bit-exact equivalence: feeding a 24-token sequence token-by-token through the incremental path reproduces the full-sequence/prefillCompute()output with max abs error 0.0 (< 1e-5). (B) Flat work/memory: after warming to position 64, four timed chunks of 128 decode steps each show us/step that does NOT trend up with position (constant3·C-float state carried regardless of context length) — the constant-memory contrast against a transformer KV cache whose per-step attention work grows ~linearly with context. Self-contained and offline (tiny random-initTNNetWKV, no checkpoint). Token-shift block-level decode integration intoTNNetStreamingDecoderis a noted follow-up (the layer-level incremental API lands here). Pure CPU, ~1 s. - MambaDecode flat-memory recurrent decoding — the constant-memory autoregressive decode headline of Mamba (the point of
BuildMambaFromSafeTensors), exercised on the core Mamba/S6 selective scanTNNetSelectiveSSMvia its new incremental state-carry API — the direct sibling ofRWKVDecode. The same uniform vocabulary:BeginIncrementalDecode/Compute(one token) /ResetState/ResetCache/EndIncrementalDecodeplusCaptureState/RestoreStatesession fork, mirroringTNNetWKV,TNNetDiagonalSSMand the SDPA KV-cache so a decoder drives every recurrent layer type the same way. Where the ordinaryCompute()re-runs the left-to-right selective scan over the WHOLE sequence each call,ComputeIncremental()advances one token in O(1): it resumes from the persisted[d_inner × d_state]hidden stateh(a fixedDepth·DState-float matrix, independent of position) instead of restarting ath=0, applying the EXACT same single-step recurrenceh_t = exp(-Δ·exp(A))(*)h_{t-1} + Δ·B_t·x_t,y_t = C_t·h_t + D·x_t(with the input-dependentΔ/B/C— the "selective" part). All three layer modes (legacyDState=1, multi-state real-MambaDState>1, Jamba inner-norm) share the path. The demo shows two facts. (A) Bit-exact equivalence: feeding a 24-token sequence token-by-token through the incremental path reproduces the full-sequence/prefillCompute()output with max abs error 0.0 (< 1e-5). (B) Flat work/memory: after warming to position 64, four timed chunks of 128 decode steps each show us/step that does NOT trend up with position (constantDepth·DState-float state carried regardless of context length) — the constant-memory contrast against a transformer KV cache whose per-step attention work grows ~linearly with context. Self-contained and offline (tiny random-initTNNetSelectiveSSM, no checkpoint). Full Mamba-block conv-state decode (the causalconv1dring buffer) +TNNetStreamingDecoderwiring is a noted follow-up (the SSM-leaf incremental API lands here). Pure CPU, ~1 s. - RWKVGenerate end-to-end flat-memory RWKV block decoding — the follow-up
RWKVDecode/MambaDecodedeferred: driving a whole RWKV block token-by-token, not just the bareTNNetWKVleaf. The missing piece was the other stateful layer in an RWKV block —TNNetTokenShift, the per-channel time-shift that mixesx_twithx_{t-1}— which now gets the same incremental state-carry API asTNNetWKV/TNNetSelectiveSSM:BeginIncrementalDecode/Compute(one token) /ResetState/ResetCache/EndIncrementalDecodeplusCaptureState/RestoreState. Its single-step output is the EXACT algebraic equivalent of one step of the full-sequence shifty[t,c]=mix[c]·x[t,c]+(1-mix[c])·x[t-1,c](x[-1,c]=0), resumingx_{t-1}from a persisted Depth-long previous-token buffer (the entire carried state, independent of position) instead of re-scanning. A new net-wide driver —TNNet.BeginIncrementalDecode/ResetIncrementalDecode/EndIncrementalDecode— loops the layers and switches every zero-arg recurrent leaf (TNNetTokenShift,TNNetWKV,TNNetSelectiveSSM,TNNetDiagonalSSM) onto its O(1)-per-step path together, so a completeAddRWKVBlock(time-mixTokenShift→r/k/v projections→WKV→gate→out-proj plus a channel-mix sub-block with its own twoTokenShifts) decodes one token at a time with all stateful layers advancing in lockstep (the stateless per-token pointwise/sum/concat/norm layers need no state). The demo builds a small COMPLETE RWKV LM net (embedding→twoAddRWKVBlock→norm→vocab logits) and shows (A) bit-exact equivalence: the driver reports 8 recurrent leaves switched on (2 blocks × (time-mixTokenShift+WKV+ 2 channel-mixTokenShift)), and decoding 20 tokens one-at-a-time reproduces the full-sequence next-token logits with max abs error 0.0 (< 1e-5) and the identical greedy argmax at every step (0/20mismatches); (B) flat work/memory: after warming to position 64, four timed chunks show us/step that does NOT trend up with position (fixed-size state regardless of context). Attention KV-cache layers (which take aMaxContextbudget) stay on theTNNetStreamingDecoderpath; this driver targets the zero-arg recurrent leaves. Self-contained and offline (tiny random-init RWKV net, no checkpoint). Pure CPU, ~1 s. - CrossWKV two-source external-memory recall — the headline demo for the new
TNNetCrossWKVlayer, a two-source variant of the RWKV-4 WKV time-mixing recurrence (Peng et al. 2023, arXiv:2305.13048). WhereTNNetWKVsplits its OWN input into thek|vpair driving its state — so the memory it accumulates and the stream that reads it are ONE sequence —TNNetCrossWKVreads thekey|valuestream from a SEPARATE source than the receptance/query stream, exactly asTNNetCrossAttentiongeneralises self-attention's packedQ|K|Vto two sources. Per channel/timestep it runs the EXACT log-space-stable RWKV-v4 kernel (wkv_t = (a_{t-1}+e^{u+k_t}v_t)/(b_{t-1}+e^{u+k_t}),a_t=e^{-w}a_{t-1}+e^{k_t}v_t, per-channelw=softplus(w_raw)+ bonusu, running-max stabiliser) withk,vdrawn from the key|value source and asigmoid(r_t)receptance gate read from the query source:y_t = sigmoid(r_t)·wkv_t. The key|value source index is serialized likeTNNetConcat/TNNetCrossAttention(round-trips throughSaveToString/LoadFromString); exact coupled-BPTT foldsdL/dk,dL/dvinto the key|value source,dL/drinto the receptance source, anddL/dw,dL/duper channel (input grads into BOTH sources + weight grads finite-difference checked). The layer offers two seqlen contracts via thepAsymmetricconstructor flag (FStruct[1], serialized): (a) the default symmetric/v1 contract — equal length on both sources, read-out attuses the state accumulated over the kv source up tot; and (b) the asymmetric/full-context cross (Create(KV, pAsymmetric=true)) — a rectangularQSeqLen × KVSeqLenshape exactly likeTNNetCrossAttention, where the kv memory is summarised once by a single decay scan over ALL key|value positions and EVERY query position reads that SAME full-context summarywkv = A/Bgated by its own receptance (y_i = sigmoid(r_i)·(A/B)), so the query stream may be a DIFFERENT length than the memory — true permuted associative recall, not a position-aligned copy. In the asymmetric path the per-query bonusuis unused (no current-token term) so only the decaywcarries weight gradient; the rectangular shape and flag round-trip throughSaveToString/LoadFromString, and input grads into both sources + thew_rawgrad are finite-difference checked withQSeqLen ≠ KVSeqLen. The demo is a cross-copy task — a memory sequence carries a re-randomised value stream, a SEPARATE query sequence carries read pulses, and the read-out must reproduce the memory value at each position (a value living ONLY in the memory tensor). Headline: the two-source arm reaches 100% exact recall (MSE 0.005) while a memory-blind single-sourceTNNetWKVthat only sees the query stream stays at the 16.7% chance floor — the capability the two-source layer adds. Pure CPU, ~20 s. - Gated Linear Attention per-channel forget gate — the headline demo for the new
TNNetGatedLinearAttentionlayer, Gated Linear Attention (GLA) (Yang et al. 2023, Gated Linear Attention Transformers with Hardware-Efficient Training, arXiv:2312.06635). It carries a(d×d)matrix memorySand updates it per timestep with a data-dependent, per-channel (vector) diagonal forget gateα_t = sigmoid(W_a x_t):S_t[d,e] = α_t[d]·S_{t-1}[d,e] + k_t[d]·v_t[e], read-outy_t = Sᵀ_t q_t. Each key channelddecays its own slice of memory by its own input-dependent factorα_t[d] ∈ (0,1)before the new outer-product write. This is the only layer in tree with a data-dependent VECTOR forget gate on a 2-D state, distinct fromTNNetWKV(FIXED-learned per-channel exp decay, not input-dependent),TNNetRetention(a single SCALAR γ),TNNetMLSTMCell(scalar exp gates + running-max) andTNNetDeltaNet(scalar WRITE gate, no multiplicative forget). Keys are L2-normalized; the exactdL/dSis carried right-to-left through the gated write (intoα,k,vand the per-rowα_t-scaled carry) and the read-out (input + all four weight sets + the gate bias finite-difference checked). The demo is the same overwrite key→value recall task as DeltaNet/RWKV (a key is written, RE-written with a new value, then queried for its MOST RECENT value): at a comparable parameter budget the GLA arm reaches 100% exact recall (MSE 0.009) — matching the delta-ruleTNNetDeltaNet(100%, MSE 0.008) and clearly ahead of the single-scalar fixed-decayTNNetRetentionbaseline (96.3%, MSE 0.019), which blends the stale and fresh values. A fourth arm wires the mixer via the newTNNet.AddGatedLinearAttentionbuilder (token-shift + per-token projection into the leaf + sigmoid receptance gate + out-projection) to show the drop-in time-mixing block; chunked/parallel forward remains a noted follow-up. Pure CPU, ~3 s. - Gated Linear Attention block tower — the headline demo for the new
TNNet.AddGatedLinearAttentionBlockbuilder, a full transformer-style block that wraps the gated-linear-attention time mixer (TNNet.AddGatedLinearAttention, around theTNNetGatedLinearAttentionleaf — Yang et al. 2023, arXiv:2312.06635) in a pre-norm residual + token-wise SwiGLU FFN structure, mirroringAddTransformerEncoderBlockbut swapping the multi-head self-attention arm for gated linear attention:x := x + GLA(LayerNorm(x))thenx := x + FFN(LayerNorm(x))withFFN = PointwiseConvLinear(d_model)∘SwiGLU∘PointwiseConvLinear(2·d_ff). It is shape-preserving over a(SeqLen,1,d_model)sequence so blocks stack into a deep tower;PreNorm=Falsemoves the norm after each residual sum andNormClassswaps the norm class (defaultTNNetLayerNorm). Pure builder (no new leaf class, no new save/load format). The demo runs two arms on the SAME overwrite key→value recall task (a key is written, RE-written with a new value, then queried for its MOST RECENT value): a single bareAddGatedLinearAttentionmixer vs a 3-block tower ofAddGatedLinearAttentionBlock, both between projections. Headline (asserted): the residual+FFN tower trains stably and reaches 100% exact recall (MSE 0.007) vs the bare mixer's 81.0% (MSE 0.033) — the LayerNorm/residual/FFN scaffolding is what makes deep stacking of the mixer pay off. Pure CPU, ~8 s. The example is covered byTestAddGatedLinearAttentionBlockShape/TestAddGatedLinearAttentionBlockSmokeTrainin the test suite. - LRU block tower (Linear Recurrent Unit) — the headline demo for the new
TNNet.AddLRUbuilder, the full transformer-style block wrapping the stable complex-diagonalTNNetLRUrecurrence (Linear Recurrent Unit, Orvieto et al. 2023, Resurrecting Recurrent Neural Networks for Long Sequences, arXiv:2303.06349).AddLRUMixerbuilds the shape-preserving D→D arm — inputPointwiseConvLinear(D)projection →TNNetLRUcomplex-diagonal scan → a GLU non-linearity (PointwiseConvLinear(2D)→TNNetSwiGLU, i.e.(Wx)⊙SiLU(Vx)) → outputPointwiseConvLinear(D)— andAddLRU(d_ff, PreNorm, NormClass)wraps it in a pre-norm residual + token-wise SwiGLU FFN exactly likeAddGatedLinearAttentionBlock, so blocks stack into a deep tower over a(SeqLen,1,d_model)sequence (PreNorm=Falsemoves the norm after each residual sum;NormClassdefaults toTNNetLayerNorm). Pure builder — no new leaf class, no new save/load format. The demo trains a 2-blockAddLRUtower on a 24-step delayed-recall task (a symbol presented early must be reproduced after a long delay): held-out recall climbs from chance (16.7%) to 96.2% (recall CE 4.29 → 0.26). Pure CPU, ~18 s. Covered byTestAddLRUBlockShape/TestAddLRUBlockSerializationRoundTrip/TestAddLRUBlockSmokeTrainin the test suite. - Titans test-time neural long-term memory — the headline demo for the new
TNNetTitansMemorylayer, the Memory-as-Context (MAC) leaf-layer variant of Titans (Behrouz et al. 2024, Titans: Learning to Memorize at Test Time, arXiv:2501.00663). The hidden state is a small inner MLPM(z)=W2·GeLU(W1·z)whose weights are gradient-descended AT INFERENCE on the per-token associative loss½‖M(k_t)−v_t‖², with the two mechanisms that separate Titans from plain Test-Time Training: (a) a momentum / "surprise" stateS_t = η⊙S_{t−1} − θ⊙∇_tso a surprising store token keeps writing for several steps, and (b) a data-dependent forget gateM_t = (1−α_t)⊙M_{t−1} + S_t(α_t = sigmoid(α_raw + W_α x_t)) that leaves a stored association untouched while bland distractor tokens stream past. Read-outy_t = M_t(q_t). The outer training is exact second-order BPTT (a GeLU Hessian-vector product) carryingdL/dW1,dL/dW2,dL/dSright-to-left through the coupled momentum/forget adjoint scans — input + all weight sets numerically gradient-checked (max-abs err ≈5e-4). The demo is a long-context associative-recall task: storecNumPairskey→value pairs up front, then a long span ofcDistractor=24 random distractor tokens, then query one stored key. Two param-matched arms train on the SAME stream: the Titans arm vs a plain FIXED-decay linear-attention baseline (TNNetRetention, gamma NOT learnable). Headline (asserted): across the long distractor span the test-time neural memory wins on BOTH metrics — recall MSE 0.16 / exact-recall 33.3% vs the fixed-decay baseline's MSE 0.20 / 19.8%, which unavoidably bleeds every stored value toward zero at its constant decay rate. Pure CPU, ~18 s. Noted follow-ups: a gated-DeltaNet-style chunked parallel scan and anAddTitansMemoryMAC-residual builder. - Linear Recurrent Unit long-range integration — the headline demo for the new
TNNetLRUlayer, the Linear Recurrent Unit (Orvieto, Smith, Gu, Fernando, Gulcehre, Pascanu & De 2023, Resurrecting Recurrent Neural Networks for Long Sequences, arXiv:2303.06349) — a stable, diagonal, COMPLEX-valued linear recurrence over the time axis. It is distinct from the existing real-diagonalTNNetDiagonalSSM(h_t = a·h_{t-1} + b·xwith a REALa = sigmoid(a_raw) ∈ (0,1)): the LRU's two defining features are (1) a complex eigenvalueλ = exp(−exp(ν) + i·exp(θ))whose magnitude|λ| = exp(−exp(ν)) < 1is stable BY CONSTRUCTION (the exp-of-exp parameterisation keeps it strictly inside the unit disk for any realν, so a channel can sit arbitrarily close to|λ|=1— a leak-free accumulator — without ever going unstable), and (2) a normalisation factorγ = sqrt(1−|λ|²)on the input drive so the hidden-state variance stays constant as|λ|→1. Per channel the COMPLEX state ish_t = λ·h_{t-1} + γ·B·x_t(carried as real & imaginary parts) read out asy_t = Re(C·h_t) + D·x_t; the complexλlets a channel encode a damped oscillation (a rotation byexp(i·exp(θ))per step) that a real-only diagonal SSM cannot. Six per-channel real params (ν, θ, B, Cre, Cim, D); forward is a left-to-right scan and backward is exact BPTT carryingdL/d(Re h), dL/d(Im h)right-to-left, chaining through|λ|=exp(−exp(ν)),angle=exp(θ)andγ=sqrt(1−|λ|²)(input + all six weight groups finite-difference checked, max-abs err ≈6e-4 input / ≈1.5e-3 weights). The demo is a causal prefix-sum / long-range integration task: a signal channel (plus irrelevant noise channels) must be integrated into a running cumulative sum at every position, evaluated at the FINAL position which integrates the whole 24-step window. Warm-started toward the long-range regime (|λ|≈0.999,γ·B≈1so each channel is a true running sum, exactly the paper's "init|λ|near 1" advice), the LRU arm drives the integration MSE to 0.00003 while a param-comparable memoryless per-token MLP — which can only see the current token — is stuck at MSE 0.66, ~20000× worse. Per-channel scalar scan (scalar is fine, no AVX). Pure CPU, ~3 s. - PonderNet adaptive computation time — the headline demo for the new
TNNetPonderHaltinghalting head +TNNet.AddPonderNetBlockbuilder +TNNetPonderCostLossregularizer (PonderNet, Banino, Balaguer & Blundell 2021, PonderNet: Learning to Ponder, arXiv:2107.05407): a learned PROBABILISTIC halting paradigm, distinct from the implicit fixed-point Deep Equilibrium block and from any fixed-depth stack. A weight-tied step functionfis applied up toMaxStepstimes (h_n = h_{n-1} + f(h_{n-1}), parameter count independent ofMaxStepsviaTNNetDeepEquilibriumSharedConvweight sharing); a shared tiny halting head emitsλ_n = sigmoid(...) ∈ (0,1)per step, giving the geometric halting distributionp_n = λ_n·∏_{k<n}(1−λ_k)(last step forced toλ=1so thep_nsum to 1). The block output is the smoothp_n-weighted sum of the per-step states — no hard argmax, so the whole block is differentiable end-to-end (input gradient finite-difference checked at 4e-5). TheTNNetPonderCostLosshead addsKL(p ‖ truncated-geometric(prior_λ)), a regularizer that pulls the expected step count toward the prior's mean and so makes the model pay to ponder — it only spends extra steps where the task forces it. The toy is parity of a variable-length bit string (difficultyL= number of active leading bits — a textbook "harder needs more sequential XOR steps" problem); both the task cross-entropy and the KL ponder cost train in one backward pass by DeepConcat-ing the two heads into a single output. Headline (asserted): the network's expected ponder stepsE[n]=Σ(n+1)·p_nRISE monotonically with difficulty — ≈2.85 atL=1up to ≈3.51 atL=6— adaptive computation time emerging from the learned halting distribution. Inference always unrollsMaxSteps(static shapes; a true cumulative-p_nearly-exit would need dynamic shapes the unrolled-graph API lacks, documented). Pure CPU, ~13 s. - Time-series forecasting (causal-conv stack) — a one-screen forecasting demo on a SYNTHETIC seasonal+trend+noise series (generated in-code, no downloads). A causal 1-D conv stack —
TNNetCausalConv1D(incl. a dilation-2 layer for a longer receptive field) +TNNetPointwiseConvLinear+ an FC head, ~5k weights — predicts the next value from a sliding window, then is rolled out auto-regressively for 24 steps. Shows the train/val MSE curve dropping (1.01 → 0.013over 40 epochs) and a final horizon error of MAE 0.22 / RMSE 0.27 vs a naive-persistence baseline of MAE 1.00 (~5× better). Pure CPU, ~1-2 min. - Growing Neural Cellular Automata — a tiny pure-CPU reproduction of Growing Neural Cellular Automata (Mordvintsev, Randazzo, Niklasson & Levin 2020, distill.pub/2020/growing-ca) built with no new layer classes — the headline use of
TNNetConvolutionSharedWeights. A grid of cells carriesCh=12channels (4 visible RGBA + 8 hidden scratch); one CA rule step is a shared-weight residual conv stack applied to every cell in place — learned perceive conv →TNNetPointwiseConvReLU→TNNetPointwiseConvLinearupdate added residually, thenclampvia a bounded leakyTNNetReLUL. The rule is unrolledT=32times sharing ONE set of weights (so the ~4.3k trainable params are independent ofT); because the steps are ordinary chained layers,TNNet.Backpropagatedoes exact BPTT through the whole recurrence for free, with theSetBatchUpdate(True)idiom and gradient-norm clipping. Trained with an L2 loss to a target glyph (a chunky letter "A") at the final step, the net grows the glyph from a single live seed pixel — ASCII renders show diffuse noise at step 4, the "A" emerging by step 16, and a clean glyph at step 32 (L20.9 → ~0.002). Stability needs three standard NCA guards (zero-init update head, state clamp, grad clip) or the 32-deep residual recurrence NaNs in one update. The paper's stochastic update mask, sample-replacement pool and damage-regeneration are dropped to stay deterministic and in-budget (documented honestly in the README). Full BPTT through all 32 steps fit the budget — pure CPU, ~61 s on two cores. - Normalizing flow (exact-likelihood density) — the headline demo for the new
TNNetAffineCouplinglayer, an exact-likelihood normalizing-flow primitive: an invertible RealNVP/Glow-style affine coupling (Dinh et al. 2016, Density estimation using Real NVP, arXiv:1605.08803; Kingma & Dhariwal 2018, Glow, arXiv:1807.03039). It splits the Depth axis into two halves, passes one half unchanged, and applies an affine mapy_b = x_b·exp(s) + tto the other, where the per-channel log-scalesand shifttcome from a tiny conditioner reading the unchanged half;sis tanh-clamped (Glow's stability trick). The map is analytically invertible (x_b = (y_b − t)·exp(−s), exposed via thepInverseconstructor flag for sampling) and its Jacobian log-determinant is justsum(s), surfaced as the publicLogDetJacobianproperty — so a stack of couplings trains by exact maximum likelihood under a unit-Gaussian base (loss = ½‖z‖² − Σ couplings log-det). This is distinct from the memory-savingAddReversibleBlock(a RevNet recompute trick with NO tractable Jacobian) and from theTNNetMixtureDensityhead (a density head that is not invertible). ThepTransformSecondflag alternates which half is transformed across stacked layers so every channel is updated; the-Σs(negative log-det) gradient is folded into each layer's backward pass (LogDetLossWeight, default 1) so the whole flow trains end-to-end by injecting only the data-loss gradientdL/dz = z. Both the forward transform AND the conditioner weights are finite-difference gradient-checked, and the negative-log-det term is checked separately; the forward∘inverse round-trip reconstructs the input to ~1e-7 (exact bijection). The demo fits a 2-D two-moons density with 6 alternating couplings, prints the mean log-likelihood climbing (≈−2.6 → −1.5), then drawsz ~ N(0,I)and pushes it through the inverse flowz → xto generate new points on the data manifold. The demo also interleaves the coupling layers withTNNetInvertible1x1Conv(Glow's learnable invertible 1×1 convolution, a trainable generalization of the fixed channel permutation, with the cheap LU-parametrizedΣ log|s|log-det) — the real Glow step — and shows the learnable mixing reaching a higher mean log-likelihood (≈−1.16) than the fixed-permute baseline (≈−1.56). Pure CPU, ~35 s (baseline arm) / ~150 s (Glow arm).
Embeddings & metric learning
- Char tokenizer + embedding lookup — smallest possible
TNNetEmbeddingdemo: builds a unique-char vocabulary from a hard-coded in-memory pangram corpus, trains a single-tokenchar -> next-charclassifier through a 16-d embedding lookup, then prints the top-5 nearest characters by cosine similarity on the embedding rows for a few probe chars ('q','e','t', space). Pure CPU, finishes in under one second, no external data. - Word2Vec skip-gram — from-scratch word2vec skip-gram with negative sampling (SGNS, Mikolov et al. 2013) on a tiny built-in (~400-token, 70-word) corpus, no download. The canonical two-matrix design is realised as two
Input + TNNetEmbedding(vocab, d)networks (an input/center matrixWand an output/context matrixW'); positive(center, context)pairs are formed within each sentence andKnegatives are drawn from a unigram^0.75 table per positive. The sigmoid/BCE SGNS lossL = -log σ(v_c·u_w) - Σ log σ(-v_c·u_neg)and its analytic gradient are computed in Pascal and seeded into eachTNNetEmbeddingvia the "target = output - grad" trick (frequent function words are kept in the vocabulary but excluded as center/context/negative, the role of frequent-word subsampling). It then prints cosine nearest-neighbour lists (king↔queen,dog↔cat,cat→kitten,boy→girl, ...) and the textbook analogy arithmetic —king - man + woman → queen(0.74),prince - man + woman → princess(0.85),puppy - dog + cat → kitten(0.87) all land; a thinly-attested seniority analogy honestly does not. The unsupervised distributional-semantics counterpart to SimpleNLP (char-LM) and CharTokenizer (tokenisation). Pure CPU, deterministic (fixedRandSeed), finishes in well under ten seconds. - Cosine-Embedding Siamese - A shared-weight (siamese) embedding MLP trained with the
TNNetCosineEmbeddingLosshead on a synthetic "same vs different class" pair task. Both members of a pair pass through identical pointwise-conv weights, are L2-normalized, and reshaped into the head'sa|b|ydepth layout (the per-pair labelyis depth-concatenated, not an external target). Prints learned cosine-similarity histograms: same-class pairs collapse ontocos≈+1while different-class pairs are driven below the margin. Pure CPU, ~1s. - Triplet embedding (metric learning) — learns a 3-D unit-sphere embedding of a synthetic 4-class 2D-Gaussian-blob dataset with the
TNNetTripletLosshead andTNNetL2Normalize, using one weight-shared siamese network: a triplet(anchor, positive, negative)is fed as three X positions of a(3,1,2)input, a pointwise-conv MLP (featuresize=1, so the same weights apply at every position) embeds each point,TNNetL2Normalizeprojects each onto the unit sphere, and aTNNetReshape(1,1,3*embed_dim)lays the three embeddings out as theanchor|positive|negativedepth layout the loss head consumes (no external target — supervision is implicit in that layout). After training it prints a per-class mean pairwise cosine-similarity matrix (within-class ~0.99, cross-class low) and dumps the learned embeddings toembeddings.csvfor plotting. Pure CPU, deterministic (fixedRandSeed), finishes in under a second. - InfoNCE contrastive embedding — learns a unit-sphere embedding of a synthetic multi-class task with the
TNNetInfoNCELosshead andTNNetL2Normalize, using one weight-shared encoder: a sample packs a query, its positive (another augmented view of the same class), andK-1negatives (views of other classes) as theK+1X positions of a(K+1,1,in_dim)input; a pointwise-conv MLP embeds each view,TNNetL2Normalizeprojects them onto the unit sphere, and aTNNetReshape(1,1,(K+1)*embed_dim)lays them out as theq | k_0(+) | k_1..k_{K-1}(-)depth layout the loss head consumes (no external target — the head seeds its own gradient via a temperature-scaled softmaxL = -s_0 + logsumexp_j(s_j)). UnlikeTNNetTripletLoss(a margin/hinge loss with a single negative), InfoNCE contrasts the positive againstKnegatives at once. Reports the positive-vs-negative cosine gap and the Wang & Isola (2020) alignment/uniformity before vs after training (gap widens, alignment drops, loss falls ~50x). Pure CPU, deterministic (fixedRandSeed), finishes in seconds. - Center loss + softmax (joint) — reproduces the headline result of Wen et al. 2016, A Discriminative Feature Learning Approach for Deep Face Recognition: softmax cross-entropy alone makes the classes separable but leaves their features spread out, while adding the center-loss penalty jointly (lambda-weighted) pulls each sample toward its learned class center, giving visibly tighter intra-class clusters at the same accuracy. Two arms train on the same architecture / seed / data — ARM A softmax-only (
TNNetCenterLossoff), ARM B softmax +TNNetCenterLoss(K, lambda=0.30)joint — and the demo reports, per arm, final accuracy, an intra-class tightness metric (mean within-class feature radius), the inter-class mean separation, the intra/inter ratio (smaller = cleaner), and an ASCII scatter of the 2-D embedding so the tightening is visible with no external plotting. Both arms reach 1.0 accuracy, but ARM B's intra-class radius drops ~17x (1.97 → 0.11) and its intra/inter ratio ~5x (0.141 → 0.029) — the Wen et al. headline. Joint wiring: a shared 2-DPointwiseConvLinearembedding feeds two consumers — aPointwiseConvLinear(K)logits head AND aDeepConcat([emb, label]) -> TNNetCenterLosspenalty head — rejoined at a finalDeepConcat([logits, center])so a singleBackpropagatewalks back through both and the embedding accumulates gradients from both heads (the departing-branch counter). The cross-entropy gradient is seeded on the logits region (target = output - (softmax - onehot)) while the center region's seeded residual is ignored (the center head self-generates its ownlambda*(x - center_c)pull). Pairs withFeatureSeparability(which measures cluster geometry) andArcFaceEmbedding(the angular-margin alternative). Pure CPU, single-threaded, ~2 s. - Matryoshka embedding (nested-prefix representation learning) — trains ONE encoder whose single
d=64embedding has nested PREFIXES{8,16,32,64}that are EACH independently usable (Kusupati et al., NeurIPS 2022). The shared MLP feeds K classifier heads, each reading only the firstpchannels viaTNNetSplitChannels(0, p) -> TNNetFullConnectLinear -> TNNetSoftMax; all heads areTNNetDeepConcat'd and the target is K stacked one-hots, so the softmax+CE backward sums the per-prefix losses (coordinate 0 gets gradient from all 4 heads, coordinate 63 only from the widest — the Matryoshka nesting). Unlike the fixed-width embedding examples (Triplet/InfoNCE/ArcFace/Cosine), one run yields a whole accuracy-vs-width curve and the vector can be truncated at retrieval time for free. Prints an ASCII accuracy-vs-width curve and contrasts the 8/16-dim prefixes against separately-trained DEDICATED fixed-8/fixed-16 baselines (observed: prefixes within ~1% of the dedicated models). README documents the prefix-loss-weighting pitfall (small prefix must not dominate the gradient). Synthetic 8-class Gaussian-blob task (MNIST would blow the budget given 3 models are trained), pure CPU, fixedRandSeed, ~6.5s. - Semantic search with imported MiniLM sentence embeddings — an end-to-end USE of an imported encoder checkpoint (GPT2Import/LlamaImport cover decoders): embeds a small corpus with a real sentence-transformers/all-MiniLM-L6-v2 snapshot (
BuildBertFromSafeTensors+ the WordPiecetokenizer.jsonsupport inneuralhftokenizer.pas) and ranks it by cosine similarity for a query — the paraphrase ("kitty on the window ledge" vs "cat on the windowsill") wins at 0.72 over distractors below 0.34. The sentence vector is the sentence-transformers recipe via theneuralpretrained.pasSENTENCE EMBEDDINGS helpers:BertTokenizeSentence([CLS] ids [SEP]) -> encoder hidden states ->BertPoolSentenceEmbedding(attention-mask-aware MEAN pooling over the real tokens only — deliberately NOTTNNetAvgChannel, which would average pad rows too — then L2 normalize, so cosine = dot product). Since the imported encoder has no attention padding mask, the demo never pads: it caches one inference-only net per distinct token length, which keeps the Pascal embeddings at cosine = 0.9999999 vs HuggingFace per sentence (compare_st_embeddings.py, bar > 0.999). Needs the ~90 MB checkpoint download; embeds 9 sentences in a few seconds on CPU. - E5/BGE pooling-mode + instruction-prefix retrieval — the self-contained sibling of SemanticSearch: where that demo needs a ~90 MB MiniLM download and only covers the mean-pool / no-prefix case, this one exercises the two pieces that distinguish the published E5 / BGE / GTE retrievers — the pooling-mode selector (
PoolSentenceEmbedding+TNNetEmbedPooling:epCLSfor BGE,epMeanfor E5/GTE,epLastTokenfor e5-mistral) and the instruction-prefix table (EmbedInstructionPrefix/ApplyEmbedInstruction+TNNetEmbedInstruction: E5's"query: "/"passage: ", BGE's"Represent this sentence …"query instruction, the gte-Qwen2"Instruct: …\nQuery: "template) — entirely on the committed pico fixturetests/fixtures/tiny_e5.*, so it runs in under a second on CPU with no network. The mean-pooled + L2-normalized query/passage vectors match the HFtransformersfloat64 oracle within 1e-4 (tools/e5_embed_tiny_fixture.py,TestE5EmbeddingParity); nosentence-transformersinstall needed (for E5/BGE itsSentenceTransformeris exactlyAutoModel forward → mean/CLS pool → L2 normalizein float64, reproduced by the fixture maker). The shared-body passage ranks above the unrelated one. Always builtpTrainable=false; pure CPU. - DeBERTa-v3 cross-encoder reranking (RAG second stage) — the canonical retrieval-augmented-generation reranker demo on an imported DeBERTa-v3
*ForSequenceClassificationcheckpoint (the ms-marco family:cross-encoder/ms-marco-...,naver/trecdl...), usingBuildDebertaV2FromSafeTensorsEx(..., pSeqClsHead=true)(neuralpretrained.pas) and a disentangled-attention encoder (TNNetDisentangledAttention: content-to-content plus content-to-position plus position-to-content, the position terms projecting a separate relative-position embedding table gathered by clamped log-bucketed relative distance — distinct from the scalaravT5RelPosBias). A cross-encoder concatenates the query and a candidate passage into one sequence ([CLS] query [SEP] passage [SEP]) and reads the row-0 classifier logit as the relevance score, jointly attending every query token to every passage token (far more accurate than independent bi-encoder cosine, the RAG second stage after a fast retriever returns the top-N). Text mode encodes with the DeBERTa-v3 Unigramtokenizer.json(the landedTNeuralHFTokenizerUnigram reader) when one sits beside the checkpoint; a raw-token-id mode runs offline on the committed pico seq-cls fixture (tests/fixtures/tiny_debertav2_seqcls.*, whose float64 oracle logits the importer reproduces to <1e-4,TestDebertaV2SeqClassificationParity). Ranks the candidates descending by score. Always builtpTrainable=false; pure CPU. - ColBERT late-interaction retrieval (MaxSim, RAG third paradigm) — completes the bi-encoder / cross-encoder / late-interaction retrieval trio (after SemanticSearch and DebertaReranker) on an imported colbert-ir/colbertv2.0-class checkpoint. ColBERT sits between the other two and is different: it keeps the per-token contextual embeddings of query and document (NO pooling), projects each token to a small dim (128) and L2-normalizes via the ColBERT
linearhead (a bias-free[hidden → 128]dense), then scores a(query, doc)pair by the MaxSim late-interaction sumscore = Σ_{q∈query} max_{d∈doc} ⟨E_q, E_d⟩— every query token matched to its single best document token, then summed (cross-encoder-grade accuracy at bi-encoder cost: docs are pre-encoded once). Newneuralpretrained.pashelpers:BuildColBERTFromSafeTensors[Ex](the stockBuildBertFromSafeTensorsencoder + thelinearprojection head),ColBERTBuildInput(the[Q]/[D]marker convention with query[MASK]augmentation),ColBERTEmbedTokens(the per-token L2-normalized projected matrix, skipping the pooling the bi-encoder helper forces),ColBERTMaxSimScore, andColBERTRetrievalReport(Recall@k + nDCG@10 driven by MaxSim instead of cosine). The ColBERT forward (encoder + bias-free projection + per-row L2 norm + MaxSim) is pinned against an HF float64 oracle on a synthesized pico checkpoint (tools/colbert_tiny_fixture.py,TestColBERTParity) to <1e-4. Always builtpTrainable=false; pure CPU. - Two-stage retrieve-then-rerank RAG pipeline — the full retrieve-then-rerank pipeline that production RAG runs, wiring the bi-encoder first stage (SemanticSearch's
BertEncodeSentence+ cosine) to a cross-encoder second stage that re-scores the top-K jointly with the query. The newneuralpretrained.pasCROSS-ENCODER RERANKER helpers complete the missing rung:BertTokenizePairlays out[CLS] query [SEP] passage [SEP]and the parallel segment ids (0 over the query span, 1 over the passage span — HF'stoken_type_ids, exercising the BERT importer'stoken_type_embeddingstable per-position, with HFlongest_firsttruncation);CrossEncoderScoreruns one joint forward through anum_labels=1*ForSequenceClassificationnet (feeding the segment ids into channel 1 of the(SeqLen,1,2)input) and returns the[CLS]relevance logit (sigmoid);RerankPassagesscores a query against a candidate list (one forward each, optionally int8 via the backbone'spQuantizeInt8) and returns them most-relevant first;RerankReportquantifies the precision lift with MRR / nDCG@k before vs after reranking. Distinct from DebertaReranker (that demo scores a hand-assembled DeBERTa sequence; this one is the standard BERT-familynum_labels=1reranker driven end-to-end from a bi-encoder shortlist via the new library helpers). The pair/segment-id=1 path is pinned against the HF float64AutoModelForSequenceClassificationlogit to <1e-4 on a synthesized pico reranker (tools/bert_reranker_tiny_fixture.py,TestRerankerPairLogitParity; the fixture boosts thetoken_typeembeddings so the segment-1 path measurably moves the logit, proving the test is not vacuous);-demoruns theRerankReportlift offline (MRR 0.3333 → 1.0000) on the committed pico fixture with no download. Always builtpTrainable=false; pure CPU. - End-to-end retrieval-augmented generation (RAG) — the canonical RAG application, tying the already-landed retrieval and generation halves together with no new core code. The pipeline: chunk a small built-in knowledge base; embed each chunk and the question with the SemanticSearch sentence-embedding path (
BertTokenizeSentence→BuildBertFromSafeTensorsencoder →BertPoolSentenceEmbeddingmean-pool + L2 normalize, one inference-only net cached per token length); retrieve the top-k chunks by cosine similarity (= dot product of the unit vectors); splice them into the canonical templateContext:\n{chunks}\n\nQuestion: {q}\nAnswer:; and generate a grounded answer through the ChatTerminal chat-template + streaming-decode infra (BuildFromPretrainedinference-only →EncodeChat/ApplyChatTemplate→ full-recompute greedy decode streamed token-by-token). The demo turns on the headline RAG property: the default question ("the launch date of Project Halcyon") asks for an invented fact no pretrained model can know — the bare model can only guess, but once the chunk that states the fact is retrieved into the context the grounded decoder reads it off. Both models are optional CLI paths:--embed-model DIRswaps in a real sentence encoder (e.g. all-MiniLM-L6-v2) but is otherwise replaced by a deterministic built-in hashing-bag-of-words embedder, and--gen-model DIRadds the decoder — so with no arguments and no download the example still runs the chunk→embed→retrieve→splice half end-to-end (the fallback embedder already ranks the Halcyon chunk #1 for the Halcyon question) and prints the spliced prompt it would send.--selftestruns 16 offline unit checks (arg parsing, fallback cosine top-k, prompt-template shape, ChatML render of the spliced prompt) with no model files. Pure CPU; the retrieval/splice half runs withinulimit -v 3000000in well under a minute. - Extractive question answering (SQuAD span head) — the extractive-QA path through
neuralpretrained.pas, the sibling of the sequence-classification importers (DebertaReranker/seq-cls) on the same BERT backbone but with a different head. A*ForQuestionAnsweringcheckpoint is the stock BERT-family encoder plus a single[hidden → 2]qa_outputsdense that emits, per token, a start logit and an end logit; the answer isargmax_{s≤e≤s+L} start[s]+end[e]over the context tokens. New helpers:BuildBertForQuestionAnsweringFromSafeTensors[Ex](reusesTNNet.AddQuestionAnsweringHead— two per-tokenTNNetPointwiseConvLinear(1)projectionsDeepConcat'd to(SeqLen,1,2)— and loadsqa_outputsrow 0 onto the start projection, row 1 onto the end projection);AnswerSpan(question, context, …)(runs[CLS] question [SEP] context [SEP], masks the question/special/pad rows, picks the best valid span over context tokens, recovers the substring viaEncodeWithOffsets, and returns the SQuAD2start[CLS]+end[CLS]null-answer baseline);QAReport(SQuAD Exact-Match + macro token-F1 with the official answer normalization, mirroringSTSReport/RetrievalReport); plus the exposedNormalizeSquadAnswer/SquadTokenF1primitives. The released QA checkpoints (distilbert-base-cased-distilled-squad,deepset/roberta-base-squad2) are ~250 MB, so the importer's span-logit parity is pinned against an HF float64 oracle on a synthesized picoDistilBertForQuestionAnswering(tools/distilbert_qa_tiny_fixture.py,TestDistilBertQALogitParity) to <1e-4 (the fixture boosts the head so a start/end row swap fails loudly); the demo itself hand-wires a span head over the committed WordPiece tokenizer so the wholeAnswerSpan/QAReportpipeline runs on CPU in under a second with no download. Swap inBuildBertForQuestionAnsweringFromSafeTensorsfor a real model —AnswerSpan/QAReportare unchanged. - Hyperbolic tree embedding (Poincaré ball vs Euclidean) — the headline "trees embed into hyperbolic space with little distortion" win for the
TNNetHyperbolicLinearPoincaré-ball layer and its companion readout headTNNetHyperbolicDistance(Nickel & Kiela 2017, Poincaré Embeddings). A complete binary tree of depth 4 (31 nodes) has ground-truth node distance = tree path length; two parameter-matched embedders (one weight matrix, no bias) map each one-hot node id to a 2-D point — Model ATNNetHyperbolicLinear(2, c)into the Poincaré ball with the curvature-cdistancedist_c(a,b) = (2/√c)·atanh(√c·‖(-a) ⊕_c b‖)(the same Möbius-distance formulaTNNetHyperbolicDistancecomputes against its prototype bank), Model B a plainTNNetFullConnectLinear(2)with Euclidean‖a−b‖. Both regress the embedded distance to the (scaled) tree path length over sampled node pairs by a hand-rolled coupled-pair MSE loop (two forward passes per pair, seed the embedder'sOutputErrorwith the analyticdMSE/dembedding— finite-difference verified — and backprop each;SetBatchUpdate(True), gradient-clipped boundary-safe step). At the identical 2-D budget the hyperbolic embedding fits the exponentially-branching tree markedly better — held-out MSE ≈0.052 / Pearson corr ≈0.94 vs Euclidean ≈0.155 / ≈0.83 (~3× lower MSE) — because hyperbolic ball volume grows exponentially with radius (matching the tree's branching) while Euclidean space crowds the leaves. Pure CPU, ~4 s.
Efficiency, structured layers & compression
- Affine Fine-Tuning (BitFit-style) - Freeze a pretrained classifier and fine-tune only inserted per-channel affine blocks (
TNNet.AddAffineBlock) on a shifted task. Adapts at a small fraction of the trainable parameters (here 68 of 388, ~18%) while recovering most of the accuracy. - Quantization-Aware Training (QAT) — the headline demo for the
TNNetFakeQuantizelayer (per-tensor symmetric, observer-driven running-max-abs fake quantization; forward =dequant(quant(x)), straight-through gradient inside the clamp band,Freezeto stop the observer at inference,pQMaxselects the bit-width). On a low-SNR synthetic 4-class image task it prints the three accuracy points that motivate QAT: FLOAT (full-precision baseline), PTQ (the same weights int8-quantized viaTNNet.QuantizeWeightsInt8with calibrated-but-frozen low-bitTNNetFakeQuantizeactivations, no retraining — observers are populated by a 2-epoch pass with every layer'sLearningRate := 0), and QAT (the same quantized topology trained from scratch with the fake-quant rounding in the forward pass so the weights converge to a quantization-robust optimum through the straight-through estimator). Aggressive low-bit activations (qmax = 5) are used on purpose — at 8 bits the calibrated fake-quant is nearly lossless on this small net, leaving no PTQ gap to recover. Deterministic run (MaxThreadNum := 1, fixedRandSeed): float 94.25% → PTQ 88.00% (−6.25 pts from quantization) → QAT 91.75% (+3.75 pts recovered, near float). Includes a note on why warm-starting QAT from the float weights fails (pins it to the float net's quantization-sensitive minimum). Self-contained synthetic data, pure CPU, a few seconds underulimit -v 3000000. Coded by Claude (AI). - LoRA Fine-Tuning - Parameter-efficient fine-tuning with low-rank adapters (
TNNet.AddLoRAAdapter, Hu et al. 2021): freeze a pretrained classifier and train only a zero-initialised rank-rbypass added to a frozen layer. Sweepsr in {1,2,4,8}and charts trainable-param count vs recovered accuracy — the textbook "most of the accuracy at a few % of the params" curve. Pure CPU. - DPO Fine-Tuning (preference alignment) — aligns a TinyGPT-style char-level causal LM with Direct Preference Optimization (
TNeuralDPOTrainerinneural/neuraldpo.pas, Rafailov et al. 2023): preference fine-tuning on (prompt, chosen, rejected) pairs withloss = -ln sigmoid(beta*((logpi_c-logref_c)-(logpi_r-logref_r)))against a frozen cloned reference — no reward model, no RL. A briefly-pretrained style-agnostic model (loss starts at exactlyln 2, margin 0) is pushed to prefer patterned over noise completions: margin0 -> ~27, preference accuracy50% -> 100%, loss0.693 -> 0.0003. EachStepbackpropagates the exactsigmoid(-beta*margin)-scaled(softmax - onehot)gradient (positive on chosen tokens, negative on rejected; derivation in the unit header). Pure CPU, ~3 s. - Magnitude pruning report — runs
TNNet.MagnitudePruningReport(NN, Samples [, Labels]), a forward-only no-retrain compressibility diagnostic answering "if I zero the smallest-magnitude weights, how much can I throw away before the model breaks?" — measured by actually pruning and re-running, not predicted from a proxy. It snapshots the whole net once (SaveDataToString), then for each global sparsitysin{0,10,...,90,95,99}%computes the magnitude threshold that zeros the smallests%of|w|across all trainable layers (a single global percentile — the standard global-magnitude criterion), zeros every|w| <= thresholdin place, runs one forward pass over the probe batch to read the resulting loss (and, with labels, top-1 accuracy), and restores the original weights bit-for-bit (LoadDataFromString) before the next level. It reports an accuracy-(or loss-)vs-sparsity#-bar curve, the prunability knee (max sparsity whose top-1 drop stays withinTolerance, default 1%), the per-layer pruned fraction at the knee (which layers absorb the pruning — typically the wide head), the realised-vs-requested sparsity at each level (a built-in check that the percentile threshold hit its target to within one weight), and ahighly-compressible/moderate/fragileverdict; an optionalPerLayerflag switches to a per-layer percentile (the uniform-per-layer baseline) so global-vs-uniform pruning is visible side by side. The example runs the SAME synthetic 3-class problem three times — an over-wide net (deep knee, highly-compressible), a tight-fit net (shallower knee), and the over-wide net again with the per-layer criterion — so the over-parameterised-is-compressible story shows up in one run. Built-in checks:s=0%reproduces the unpruned loss/accuracy exactly. Distinct fromFisherImportanceReport(ranks by a Fisher proxy, never removes weights) andLayerSensitivityReport(random weight jitter, never a magnitude-thresholded zeroing). Pure forward-only — weights are restored bit-for-bit, never stepped. - Magnitude pruning + fine-tune recovery — the persistent-mask counterpart to MagnitudePruning: instead of restoring the weights, it KEEPS them pruned and fine-tunes.
TNNet.PruneWeightsByMagnitude(Sparsity [, PerLayer])zeros the smallests%of|w|across the net and installs a persistent keep/prune mask (one volume per neuron) that is re-enforced after every weight update — the baseAfterWeightUpdatehook callsZeroPrunedWeights, covering both the batchUpdateWeightspath and the inline onlineTNNetFullConnect.BackpropagateCPUpath, and it also zeros the pruned weights' delta / inertia / Adam-moment entries so a pruned weight can never grow back. The example trains a small 4-class classifier, measures the dense baseline, prunes to a target sparsity (50 / 80 / 90%), measures accuracy right after pruning (a drop — capacity removed), fine-tunes for a few epochs with the mask enforced, and measures accuracy after fine-tuning — printing thedense -> pruned -> fine-tunedtriple per sparsity so the recovery is visible (e.g. at 80% sparsity a ~11-pt drop recovers ~+15 pts past the dense baseline). The mask survives throughout (reported pruned-weight count and realised sparsity are unchanged after fine-tuning). Companion helpers:ApplyPruneMasks/HasPruneMasks/ClearPruneMasks/CountPrunedWeights/GetPruneSparsity. Pure CPU, fixedRandSeed, well under a minute. - Lottery Ticket — Iterative Magnitude Pruning (IMP) — a follow-up to LotteryTicket implementing the paper's ACTUAL method, ITERATIVE magnitude pruning (Frankle & Carbin 2019): loop train → prune the bottom 45% of SURVIVING weights → rewind survivors to
theta_0→ retrain for 5 rounds, so(1-0.45)^5 ≈ 5%survivors = ~95% sparsity, the level where the sibling one-shot run collapsed. Prints a per-round sparsity/accuracy table plus a budget-matched head-to-head against a one-shot prune-to-95% baseline, and GRADES it with an explicit verdict. HONEST negative result: with a generous matched epoch budget the one-shot ticket does NOT collapse (the sibling collapses only because every arm got just 100 epochs), so IMP shows no advantage on this easy toy — reported plainly rather than assumed, mirroring the sibling's own 95%-collapse honesty. Pure CPU, ~140 s. - Knowledge distillation (KL divergence) — trains a teacher MLP on a synthetic multi-class 2D-Gaussian-blob task, then distills a smaller student against the teacher's temperature-softened soft targets using the
TNNetKLDivergencehead (SoftMax -> TNNetKLDivergence, target = teacher distribution), and contrasts it with an identical-capacity student trained on hard one-hot labels via the standard cross-entropy path. Prints the loss curves and final test accuracy for teacher / distilled-student / hard-label-student so the soft-target effect is visible. Pure CPU, fixedRandSeed, finishes in well under a second. - Mixture of Experts - Soft/dense Mixture-of-Experts feed-forward block (
TNNet.AddMixtureOfExperts, Shazeer et al. 2017): a softmax gating network blends N parallel shape-preserving expert MLPs. Trains on a synthetic multi-task toy where different experts can specialise. Pure CPU, no dataset download. (Hard top-k routing is the TopKMoE example below.) - Top-k Mixture of Experts (sparse routing + load balancing) — the sparse-dispatch follow-up to the soft
AddMixtureOfExperts:TNNet.AddTopKMixtureOfExperts(InputLayer, NumExperts, ExpertHiddenDim, TopCnt, out AuxLossHead, AuxCoeff)routes each token through only itsTopCnthighest-gated experts via the newTNNetTopKGate(keeps the top-TopCntgate weights, zeroes the rest, renormalizes survivors to sum 1, exact fused mask+renorm Jacobian backward) and attaches a load-balancing auxiliary lossTNNetLoadBalanceLoss(Switch Transformer, Fedus et al. 2021:L_aux = coeff·E·Σ_i f_i·P_i, withf_ithe stop-gradient fraction of tokens routed to expertiandP_iits mean gate prob, so the gradient flows throughP_ionly). The demo trains two identical networks — both deliberately seeded collapsed onto one expert — differing only in the aux-loss weight: WITHOUT it the router stays fully collapsed (held-out per-expert load E0=100%, imbalancemax/mean = 4.00); WITH it the load becomes near-uniform (≈28/23/26/23%,max/mean = 1.11). Pure CPU, ~19 s. (Gumbel-noise gating and true compute-sparse dispatch are logged follow-ups.) - Expert-Choice Mixture of Experts (transposed routing, load-balanced by construction) — the transpose of the token-choice MoE family above: instead of each token picking its top-
TopCntexperts, each EXPERT picks its top-CapacityTOKENS via the newTNNetExpertChoiceGate(Expert Choice routing, Zhou et al. 2022, Mixture-of-Experts with Expert Choice Routing, arXiv:2202.09368). The gate transposesTNNetTopKGate's logic — it keeps the top-Capacitytoken positions ALONG the SizeX (token) axis per expert channel and zeroes the rest — so every expert processes EXACTLYCapacitytokens and load balance is guaranteed structurally, with NO Switch-styleTNNetLoadBalanceLosshead required (and a token may be handled by 0, 1, or several experts). The builderTNNet.AddExpertChoiceMixtureOfExperts(InputLayer, NumExperts, ExpertHiddenDim, Capacity)reuses the per-expert MLP +SplitChannels/DeepConcat.Replicate/CellMulByCellcombine ofAddTopKMixtureOfExperts, swapping only the gate; the survivor gate value is the raw per-token softmax (no renorm) so the backward is a hard 0/1 straight-through mask. The demo drives both gates on the SAME skewed gate matrix and prints the per-expert token counts: token-choice top-1 →[7 3 2](spread 5, lopsided) vs expert-choice Capacity →[4 4 4](spread 0, uniform by construction). Pure CPU, sub-second. - Mixture of Depths - Conditional-compute block (
TNNet.AddMixtureOfDepths, Raposo et al. 2024): a per-token router sends only the top-Capacitysequence positions through a wrapped block and lets the rest bypass it via the residual path, so FLOPs drop by(SeqLen-Capacity)/SeqLenat static tensor shapes. SweepsCapacity ∈ {SeqLen, SeqLen/2, SeqLen/4}on a tiny next-token task and charts the loss/accuracy vs processed-token-fraction trade. Pure CPU. - Early-exit / adaptive-inference network — the BranchyNet (Teerapittayanon, McDanel & Kung 2016) "anytime inference" pattern: ONE trunk of stacked
FC+ReLUblocks with an auxiliary softmax classifier head branching off after each block (wired viaAddLayerAfterand all headsConcat'd into a single packedK*NumClassesoutput), trained JOINTLY by deep supervision — a manual loss loop seeds each head's(p - onehot)softmax-cross-entropy gradient through theConcatand a singleBackpropagate(underSetBatchUpdate(true)) accumulates into the shared trunk. The synthetic task is difficulty-graded (easy well-separated blobs mixed with hard near-the-margin points). At INFERENCE a confidence-gated dynamic-compute policy walks heads shallow→deep and EXITS at the first head whose softmax max-prob exceeds a thresholdtau, recording the per-sample exit depth; sweepingtauprints the accuracy-vs-average-exit-depth trade-off (split by easy/hard) as an ASCII chart, showing easy samples leave at depth ~1 while hard ones run the full depth. Two built-in invariants hold:tau=1.0forces every sample to the final head (== plain full-depth accuracy, exactly) and average exit depth is monotone non-decreasing intau. Distinct from thePredictionDepthexample above: there the heads do not exist — a post-hoc k-NN probe measures where a FIXED single-head net "makes up its mind"; here the early heads are TRAINED and actually GATE compute. Pure CPU, runs in ~9 s. - HyperNetwork — context-generated weights — the headline demo for the new
TNNetHyperLinear(Ha, Dai & Le 2016, HyperNetworks), a layer that owns NO trainable weights of its own: its weight matrix is GENERATED on the fly by an upstream network and read from a SECOND input tensor rather than fromNeurons[].Weights. Two-source wiring (likeTNNetCrossAttention/TNNetAffineGridSample): the main feature vector onPrevLayer, a flat generated-weights vector (Din*Dout (+Dout)row-major matrix + optional bias) onWeightsSource; forward isy = W_gen·x (+b)and backward propagates BOTH into the main features (W_gen^T·dy) AND back into the generated-weights tensor (dy⊗x,dy), so the upstream generator trains end-to-end. The demo conditions a small generator (learned task embedding →FullConnect→FullConnectLinear(Din*Dout+Dout)) on a one-hot task id to emit the weights of ONE shared hyper layer, which then solves a FAMILY of 4 distinct 2D→2D rotate+scale tasks: per-task held-out MSE collapses to ~1e-12 (the hyper layer has zero owned weights) while a fixed sharedTNNetFullConnectLinearbaseline is stuck averaging the tasks at ~8e-2. Concat-style serialization (weights-source index in the structure string), save/load round-trip checked. Pure CPU, ~1 s. A spatial cousin,TNNetHyperConv(builderTNNet.AddHyperConv(InChannels, OutChannels, FeatureSize, ContextLayer)), extends the same idea to a VALID stride-1 convolution: the generator emits the whole flat conv kernelW[o,ky,kx,i](+ optional per-output-channel bias) and the weightless HyperConv applies it to a(SizeX,SizeY,InChannels)image, with both gradient paths (into the image and back into the generated kernel) written. Memory/param trade-off: the generator must output the ENTIRE kernel in one shot, so its parameter count scales asOutChannels*K*K*InChannels— keepKand the channel counts small (chunked/tiled kernel generation is a deferred follow-up). - BitLinear bake-off — ternary-vs-full-precision head swap, the
TNNetBitLinear(BitNet) follow-up. The same tiny classifier is trained three times on the same synthetic multi-class task at fixed seed/data/init order, changing only the head type: full-precisionTNNetFullConnectLinear(32 bits/weight), the BitNet-styleTNNetBitLinearthat quantizes its weights to{-1, 0, +1}, and the BitNet b1.58 fully-quantized path (TNNetBitLinearwith its activation-quant flag ON: ternary weights plus per-token absmax-int8 activations, STE backward). Reports accuracy, effective weight memory, and a PASS/FAIL gate. Pure CPU. - Circulant linear parameter-efficiency bake-off — the headline accuracy-per-weight win for the new
TNNetCirculantLinear, a STRUCTURED-MATRIX dense layer whosen×nweight matrix is CIRCULANT (every row a cyclic shift of one learned length-nvectorc), so the map isy = circular_convolution(c, x) (+ bias)and the layer storesO(n)weights instead ofO(n²). On a target that is genuinely a circular convolution it fits the teacher kernel almost exactly with2nweights where a param-matched denseTNNetFullConnectLinearneedsn²+n— atn=16that is 32 vs 272 weights (8.5× fewer) and a far higher accuracy-per-weight. Distinct from LoRA (low-rank),AddGroupedFullConnect(block-diagonal) andTNNetBitLinear(quantized): this one imposes shift-invariant Toeplitz/circulant structure. Pure CPU, <1 s. (Opt-in FFTO(n log n)fast path is a logged follow-up; the direct sum is the default.) - Monarch structured-linear parameter/accuracy bake-off — the headline "structured = fewer params, comparable accuracy" win for the new
TNNetMonarchLinear, a SUB-QUADRATIC structured dense layer that factorises ann×nmap asy = Pᵀ(L(P(R·x)))whereRandLare BLOCK-DIAGONAL (bblocks of sizem,n=b·m) andPis a fixed reshape-transpose permutation, so the densen×nmatrix is never formed — forward and backward are all block-localm×mmatmuls and the layer stores only2·n·√nweights instead ofn². The square map INFERSnfrom the previous layer (constructor is justCreate(pSuppressBias), noNarg). Three square64→64mixers are trained on the same random linear-then-tanhregression teacher behind an IDENTICAL tiny linear read-out head: Monarch (b=m=8, 1088 mixing weights) lands at a comparable training MSE to the denseTNNetFullConnectLinear(64)(4096 weights, 3.8× more) whileTNNetCirculantLinear(64)(128 taps) is an even leaner structured point on the params-vs-accuracy curve. Distinct fromTNNetCirculantLinear(shift-invariant circulant),TNNetHouseholderLinear(exactly orthogonal) andAddGroupedFullConnect(single block-diagonal): Monarch is a TWO-factor butterfly with an interleaving permutation between them. NOTE: the asked-for DFT-init sub-check is intentionally SKIPPED — the layer exposes no DFT-init path (InitDefaultis random), documented in the program and README rather than invented. Pure CPU, ≈3 s. - Kronecker structured-linear parameter/accuracy bake-off — the headline "structured = far fewer params, comparable accuracy" win for the new
TNNetKroneckerLinear, a SUB-QUADRATIC structured dense layer whosen×nweight is a single KRONECKER PRODUCTW = A ⊗ Bof two small learned factorsA (p×p)andB (q×q)withn = p·q. The densen×nmatrix is never formed:xis reshaped to aq×pmatrixX(X[i,j] = x[i·p+j]) and the matvec is two small GEMMsY = B·X·Aᵀ(O(n^1.5)), withy = vec(B·X·Aᵀ) = (A⊗B)·xunder the row-major vec convention; backward is the exact transpose chaindX = Bᵀ·dY·A,dA = dYᵀ·(B·X),dB = dY·(X·Aᵀ)ᵀ(all gradient-checked). Stores onlyp²+q² ≈ 2nfactor weights instead ofn². The square map INFERSnfrom the previous layer (constructorCreate(pSuppressBias, pP),pP=0auto-picksp = round(√n)). Three square256→256mixers are trained on a tiny MNIST-shaped 10-class task (16×16 prototype+noise images) behind an IDENTICALReLU→linear(10)→softmaxhead: Kronecker (p=q=16, 768 mixing weights) reaches the SAME 100% test accuracy as the denseTNNetFullConnectLinear(256)(65536 weights, 85× more) andTNNetMonarchLinear(8448 weights) is an intermediateO(n^1.5)structured point. Distinct fromTNNetCirculantLinear(single cyclic kernel),TNNetHouseholderLinear(exactly orthogonal),TNNetMonarchLinear(two block-diagonal factors + permutation) and LoRA (low-rank): Kronecker is a single tensor-product factorisation. Pure CPU, a few seconds. - Tensor-Train structured-linear parameter/accuracy bake-off — the "structured = fewer params, comparable accuracy" win for the new
TNNetTensorTrain, the next rung of the structured/sub-quadratic weight family afterTNNetMonarchLinearandTNNetKroneckerLinear. It factors then×nmap as a Tensor-Train (Matrix-Product-State / MPO) — a CHAIN ofdsmall 4-D coresG_k ∈ R^{r_{k-1}×m_k×n_k×r_k}with boundary TT-ranksr_0=r_d=1and a tunable interior rankr(defaultd=2) — and contracts them left→right via the exact MPO-vector sweep, so the densen×nmatrix is never materialized (params~ d·m·n·r²instead ofn²). The square map INFERSnfrom the previous layer (noNarg, like Monarch); backward is the standard "freeze the other cores, contract" reverse sweep (numerically gradient-checked, both cores). The bake-off trains a TensorTrain mixer (576 weights) against a denseTNNetFullConnectLinear(4096 weights, 7.1× more) at comparable MSE, with aTNNetKroneckerLineararm as an even-leaner structured point. Distinct from Kronecker (single 2-factorA⊗B) and Monarch (two block-diagonal factors + permutation): ad-core chain with a tunable internal rank gives a smooth compression↔capacity dial. Pure CPU, a few seconds. - Householder exactly-orthogonal dense stack — the gradient-stability win for the new
TNNetHouseholderLinear, an EXACTLY-orthogonal dense layer whosen×nweight is a product ofKHouseholder reflectionsQ = H_1·…·H_K(H_i = I − 2·v_iv_iᵀ/v_iᵀv_i), soQis orthogonal for ANY reflection vectorsv_i— no constrained optimization or re-projection, yet exactly norm-preserving and invertible. The demo builds deep plain linear stacks (no activation/norm/residual) at depths1…32and prints the input-side gradient norm for a unit gradient planted at the top: the Householder stack holds it at exactly 1.0 at every depth (an orthogonal Jacobian is an isometry) while a param-matched unconstrainedTNNetFullConnectLinearstack explodes geometrically (≈1047 at depth 32). A second sweep variesK ∈ {1, n/2, n}to showKtrades cost (O(K·n)/layer) and expressivity for representational reach but NOT gradient stability (everyKis exactly orthogonal). Distinct fromTNNetSpectralNorm(bounds onlyσ_1), the structured-matrix layers (constrain the matrix form) and Muon (orthogonalizes the update). BuilderTNNet.AddHouseholderLinear(N, NumReflections, UseBias). Pure CPU, well under a minute. - Complex linear parameter-efficiency bake-off — the structured-weight-sharing win for the new
TNNetComplexLinear, the 2-dimensional base rung of the same Cayley–Dickson ladder asTNNetQuaternionLinear(4D) andTNNetOctonionLinear(8D). It reinterprets the input/output Depth (a multiple of 2) as packed complex numbers (groupgholdsRe = chan[2g],Im = chan[2g+1]) and learns an(OutC × InC)grid of complex weightsw = a + b·i, each driving a full 2×2 complex-multiply block[[a,-b],[b,a]](Re' = a·Re − b·Im,Im' = a·Im + b·Re) — so one complex's 2 reals steer a whole 2×2 output block and the layer stores ~1/2 the weights of an equal-width denseTNNetFullConnectLinearwhile still mixing the real and imaginary parts. Backward propagates into BOTH the input (transpose block) and the two real weight components per block (numerically gradient-checked). A param-matched bake-off on a true complex-product task (left-multiply by a fixed 50° phase rotation + gain plus cross-complex coupling): the complex layer essentially solves it (val-MSE ≈ 0) at 8 weights (+4 bias) while a param-matched dense bottleneck and block-diagonal grouped head plateau around 0.4 — vs the 16 weights of a full 4×4 dense layer. It then verifies the algebraic guarantee the layer rests on,|w·X| = |w|·|X|andarg(w·X) = arg(w) + arg(X), to ~1e-7. Pure CPU, well under a minute. - Quaternion linear parameter-efficiency bake-off — the headline structured-weight-sharing win for the new
TNNetQuaternionLinear, a hypercomplex layer (Parcollet et al. 2019; Gaudet & Maida 2018). It reinterprets the input Depth (a multiple of 4) as packed quaternions and learns an(OutQ × InQ)grid of quaternion weightsq = r + xi + yj + zk, each driving a full 4×4 Hamilton-product block[[r,-x,-y,-z],[x,r,-z,y],[y,z,r,-x],[z,-y,x,r]]— so one quaternion's 4 reals steer a whole 4×4 output block and the layer stores ~1/4 the weights of an equal-width denseTNNetFullConnectLinearwhile still mixing all four components (cross-channel coupling a block-diagonalAddGroupedFullConnectcannot express). Backward propagates into BOTH the input (conjugate/transpose block) and the four real weight components per block (numerically gradient-checked). A param-matched bake-off on a true quaternion-rotation task: the quaternion layer essentially solves it (val-MSE ≈ 0) at 80 weights while a dense bottleneck (128 weights) and a block-diagonal grouped head (64 weights) plateau around 0.2. Pure CPU, ~3 s. - Quaternion convolution parameter-efficiency bake-off — the spatial sibling of the dense quaternion layer above: the structured-weight-sharing win for the new
TNNetQuaternionConv. Both the input Depth and the filter count are multiples of 4; withInQ = inDepth/4andOutQ = features/4the layer learns, per kernel tap, an(OutQ × InQ)grid of quaternion weightsq = r + xi + yj + zkapplied by the same trusted 4×4 Hamilton-product block asTNNetQuaternionLinear, accumulated over the kernel window — so it stores ~1/4 the weights of a real conv of equal input/output width while coupling the four channel components across space. Backward propagates into BOTH the input (transpose/conjugate block) and the four real weight components of every tap (numerically gradient-checked, the classic one-component sign error is exactly what the test catches). A param-matched bake-off on a true "colour-rotation + 3×3 blur" image task (, 4 channels = one quaternion/pixel; target = a fixed unit-quaternion rotation of a fixed 3×3 spatial average, which is EXACTLY a quaternion convolution): the quaternion conv essentially solves it (val-MSE ≈ 0) at 40 weights while a param-matched real-conv4→1→4bottleneck (72 weights) plateaus around 0.03. Padding/stride follow the usual convolution semantics. Pure CPU, well under a minute. - Octonion linear parameter-efficiency bake-off — the 8-dimensional sibling of the quaternion layer: the structured-weight-sharing win for the new
TNNetOctonionLinear. It reinterprets the input/output Depth (both multiples of 8) as packed octonions and learns an(OutO × InO)grid of octonion weightsw = o0 + o1·e1 + ... + o7·e7, each driving a full 8×8 Cayley–Dickson blockM(W)[i][j] = SGN[i][j]·W[i xor j]— so one octonion's 8 reals steer a whole 8×8 output block and the layer stores ~1/8 the weights of an equal-width denseTNNetFullConnectLinearwhile still mixing all eight components. The hard-coded multiplication table is derived from the standard octonion = pair-of-quaternions doubling(a,b)(c,d) = (a·c − d*·b, d·a + b·c*)and verified independently by norm multiplicativity|W·X| = |W|·|X|in the tests; backward propagates into BOTH the input (transpose block) and the eight real weight components per block (numerically gradient-checked). A param-matched bake-off on a true octonion-product task: the octonion layer essentially solves it (val-MSE ≈ 0) at 32 weights (+16 bias) while a dense bottleneck (64 weights) and a block-diagonal grouped head (128 weights) plateau around 0.1–0.3 — vs the 256 weights of a full 16×16 dense layer. Pure CPU, well under a minute. - Octonion convolution parameter-efficiency bake-off — the spatial sibling of the dense octonion layer above and the 8-dimensional analogue of
TNNetQuaternionConv: the structured-weight-sharing win for the newTNNetOctonionConv. Both the input Depth and the filter count are multiples of 8; withInO = inDepth/8andOutO = features/8the layer learns, per kernel tap, an(OutO × InO)grid of octonion weightsw = o0 + o1·e1 + ... + o7·e7applied by the same trusted 8×8 Cayley–Dickson blockM(W)[i][j] = SGN[i][j]·W[i xor j]asTNNetOctonionLinear(same hard-coded, norm-multiplicativity-verified multiplication table), accumulated over the kernel window — so it stores ~1/8 the weights of a real conv of equal input/output width while coupling all eight channel components across space. Backward propagates into BOTH the input (transpose block) and the eight real weight components of every tap (numerically gradient-checked, the classic one-component sign error is exactly what the test catches). A param-matched bake-off on a true "octonion left-mul + 3×3 blur" image task (, 8 channels = one octonion/pixel; target = a fixed unit-octonion left-multiplication of a fixed 3×3 spatial average, which is EXACTLY an octonion convolution): the octonion conv essentially solves it (val-MSE ≈ 0) at 80 weights while a param-matched real-conv8→1→8bottleneck (144 weights) plateaus around 0.036 — vs the 576 weights of a full 8×8-channel 3×3 real conv. Padding/stride follow the usual convolution semantics. Pure CPU, well under a minute. - CondConv dynamic-convolution bake-off — the headline "match a wider conv at single-conv inference cost" win for the new
TNNetCondConv, a CONDITIONALLY-PARAMETERIZED ("dynamic") convolution (Yang et al. 2019, NeurIPS, arXiv:1904.04971). The layer owns a bank of K expert kernelsW_1..W_K(each a normalFeatures × FeatureSize × FeatureSize × InChannelskernel) plus a tiny per-sample routing head (global-average-pool → FullConnect → sigmoid) emitting K mixing coefficientsalpha_kPER INPUT SAMPLE; the effective kernel is the per-sample blendW_eff = sum_k alpha_k · W_kapplied as ONE ordinary convolution — so inference cost stays that of a single conv regardless of K while capacity grows with the bank. Backward routesdL/dW_k = alpha_k · dL/dW_eff, sendsdL/dalpha_k = <dL/dW_eff, W_k>back through the sigmoid + FC + pool into the input, and propagates the standard conv input gradient throughW_eff(all three — input, expert-bank weights, routing head — are numerically gradient-checked;K/Features/FeatureSize/Padding/Strideround-trip viaFStruct). DISTINCT from siblings:TNNetHyperConvGENERATES the whole kernel from a second tensor in one shot;AddMixtureOfExpertsmixes K expert OUTPUTS post-hoc (K forward passes); CondConv mixes K kernels BEFORE the conv (one forward pass). The demo is an input-dependent filtering task on single-channel fields where the SIGN of each sample's global mean selects which of two ground-truth 3×3 filters (edge vs blur) made the target — so the right kernel DEPENDS on the input. AK=2CondConv (22 weights, single-conv inference) drives val-MSE to ≈0.02 while a single plain conv (9 weights) is stuck at ≈1.18 (it can't switch behaviour) and a much WIDER plain conv (8 maps + 1×1, 80 weights, ~2× the inference wall-clock) only reaches ≈1.96. Pure CPU, well under a minute. - p4 group-equivariant CNN vs param-matched plain CNN — the headline "rotation robustness BY CONSTRUCTION, not from augmentation" win for the new
TNNetGroupConvP4+TNNetGroupPoolP4(Cohen & Welling 2016, Group Equivariant Convolutional Networks, arXiv:1602.07576) — a layer that is rotation-equivariant by construction rather than only measured after the fact byTNNet.EquivarianceReport.TNNetGroupConvP4is the lifting rung for the C4 group of 90° rotations: ONE learnedK×Kkernel bank is shared across the four rotations, the layer convolves the input with the four rot-{0,90,180,270} copies of the same filter and stacks the responses along a new 4-fold orientation sub-axis (Depth = 4·FeaturesCount, channel= co·4 + r), so a 90° input rotation only cyclically permutes those orientation channels (and rotates the spatial map) — it never scrambles them. The three extra rotated filters are index views of the one trained bank materialised at forward time (FRotMap), and the backward folds the four orientation gradients back onto the single shared kernel (rotation-tied weight-gradient sum — the place a silent reduction bug would hide, so it is gradient-checked hard alongside the input gradient and an EXACT-equivariance forward test that reads 0.0 error to machine precision).TNNetGroupPoolP4then max- (or mean-) reduces over the four orientation channels of each feature to collapse the C4 field back to a rotation-invariant(SizeX, SizeY, FeaturesCount)map; followed by a global spatial average pool the whole stack is exactly C4-invariant. DISTINCT fromTNNetFlipX/FlipY/TransposeXD(fixed parameter-free involutions — data-augmentation primitives, not weight-shared equivariant maps) and fromCondConv/Quaternion/Octonion(which share weights across a different algebra, none across a spatial symmetry group).Features/FeatureSize/Padding/Stride/SuppressBias(and the pool's reduce mode) round-trip viaFStruct. The demo trains BOTH a tiny p4-CNN and a parameter-matched plain CNN (≈320 weights each, same pooling/classifier tail) on UPRIGHT-only chiral 8×8 glyphs (the classic Cohen–Welling setting, no rotation augmentation) and evaluates on a 90/180/270°-rotated test set: the p4 net more than DOUBLES the plain net's rotated accuracy at equal weights (≈0.92 vs ≈0.41) and its measured C4-invariance error is ≈1e-6 vs ≈0.64 — and it printsTNNet.EquivarianceReporton both to close the loop with the existing diagnostic. v1 is the lifting conv only; the full p4m group (reflections), steerable/SO(2) harmonics, and a field→field p4 group conv are noted as follow-ups. Pure CPU, ≈45 s. - Tropical morphology hypothesis-class demo — the headline "different algebra, different hypothesis class" win for the new
TNNetTropicalLinear, a max-plus / min-plus morphological dense layer computing in the TROPICAL semiring instead of the usual multiply-accumulate ring:y_i = max_j (x_j + W[i,j])(a morphological DILATION) with a paired ERODE modey_i = min_j (x_j + W[i,j])selected by a constructor flag (round-trips viaFStruct[6]). The weights are learnable additive thresholds and the combine op is max/min, so the layer learns piecewise-linear convex (dilation) / concave (erosion) functions and tropical polynomials — different fromTNNetFullConnect*/ the structured-linear family (allsum_j W·x) and from parameterless max/min pooling. Backward is the same hard arg-max/arg-min subgradient asTNNetMaxPool(routedy_ito the single winningj*). The demo fits a convex piecewise-linear envelope (the upper envelope of three lines) with an affine-feature bank →TNNetTropicalLineardilation stack, versus a SAME-width single linear layer: the tropical stack drives the MSE to ≈0 while the linear baseline plateaus at the best-fitting single straight line (≈1.39 MSE — it provably cannot bend), and the min-plus ERODE sibling does the same for a concave target. Forward isO(Din·Dout); input AND weight gradients are numerically gradient-checked away from the tie kink. Pure CPU, a few seconds. A second program in the same directory (TropicalMorphologyConv.lpr) exercises the SPATIAL conv siblingTNNetTropicalConv(subclass ofTNNetConvolutionLinear): it slides a learnable additive structuring element over a 12×12 binary glyph and takes a per-cell max (DILATION → thicker strokes) or min (ERODE → thinner strokes), constructorCreate(NumFeatures, FeatureSize, Padding, Stride, Erode). Trained against the glyph's classical 3×3 morphological dilation/erosion versus a same-size linearTNNetConvolutionLinear, the tropical conv wins on the trained per-pixel MSE on BOTH targets — dilation is an exact MSE-0 / 100%-pixel win, while the linear sum-of-products can't match the per-pixel max; erosion is also won on MSE but stops just short of 0 because the hard arg-min subgradient (one-hot, likeTNNetMaxPool) leaves a few boundary taps un-pressured. Runs in ≈1 s. - Soft decision tree vs matched MLP (two-moons) — the headline "hierarchical soft routing wins AND stays interpretable" demo for the new
TNNetSoftDecisionTreelayer (Kontschieder et al. 2015, Deep Neural Decision Forests; Frosst & Hinton 2017, Distilling a Neural Network Into a Soft Decision Tree) — a structurally new paradigm in this repo (not factorization/attention/recurrence/kernel). A single balanced depth-3 tree (7 inner gatesp_i = sigmoid(β·(wᵢ·x + bᵢ)), 8 leaf logit vectors) routes each sample to leaves with probability = product of left/right gate decisions along the root-to-leaf path; the output is the path-probability-weighted mixture of leaf logits. The exact analytic backward collapses the product-of-gates path responsibilities (thepᵢ/(1−pᵢ)divisions cancel againstdpᵢ/dzᵢ = β·pᵢ·(1−pᵢ)) todL/dzᵢ = β·(Aᵢ·(1−pᵢ) − Bᵢ·pᵢ)withAᵢ/Bᵢthe left/right subtree responsibility sums — finite-difference verified for input, gate weights/biases AND leaf vectors (TestSoftDecisionTree*). On a non-linearly-separable two-moons toy, the tree and a plainReLU(7) → linear(2)MLP are sized to the same 37 parameters behind an identical softmax; the tree reaches ≈99.9% held-out accuracy vs ≈95% for the matched MLP and exposes a human-readable decision path (per-node left/rightp(left)and the dominant leaf logits) the MLP cannot. Pure CPU, ~15 s.
Sets, graphs, memory & spectral / operator methods
-
Deep Sets (permutation-invariant set learning) — reproduces the Zaheer et al. 2017 Deep Sets recipe from existing layers only: the
Nelements of a set are laid along the X axis as an(N,1,1)bag, a shared per-element encoder (TNNetPointwiseConvReLU->TNNetConvolutionLinear,featuresize=1so every element sees identical weights) maps each one, a symmetric pool (TNNetMaxChannel,(N,1,F)->(1,1,F)) collapses the set, and aTNNetFullConnectReLU -> TNNetFullConnectLinear(1)rho head regresses the set's MAX. The headline is the architectural invariant: the trained output is bit-for-bit unchanged (max|dy| = 0) under 200 random permutations of the elements yet shifts sharply when an element's value is edited, and the same weights generalize to an unseen set size (train N=5, test N=8). README explains why a flatten->dense net cannot be permutation-invariant and contrasts with self-attention. Pure CPU, deterministic, ~3 s. (Note: on an(N,1,F)bagTNNetAvgChanneldivides byN^2, notN;TNNetMaxChannelis exact.) -
Set Transformer (ISAB + PMA) — exercises the two new permutation-invariant Set-Transformer primitives (Lee et al. 2019):
TNNetInducedSetAttention(ISAB — replacesO(N^2)self-attention with anO(N*M)bottleneck throughMlearnable inducing points via two stacked cross-attentionsH=MAB(I,X),Y=MAB(X,H)) andTNNetAttentionPooling(PMA — a learnable, content-addressed pooler that collapses a set(N,1,d)to a fixed(k,1,d)by lettingklearnable seed vectors cross-attend over the inputs;k=1is a learned-query weighted-sum pool, categorically unlike the parameter-freeTNNetAvgChannel/TNNetMaxChannel). Three parts: (1) a tinyISAB->PMA(k=1)net gives bit-for-bit invariant pooled output (max|dy| ~ 1e-8) on a shuffled vs original bag before training (every softmax-over-inputs is symmetric in the rows); (2) on max-of-set regressionISAB+PMA(k=1)beats a mean-pool baseline (MSE0.014vs0.046) because the attention pool can concentrate its softmax mass on the largest element; (3) prints theN×M(ISAB) vsN×N(full self-attention) score-matrix sizes. v1 MABs are single-head with identity Q/K/V projections (only the inducing/seed bank is learnable — keeps the two-stage softmax-Jacobian backward exact and gradient-checkable). Pure CPU, deterministic, runs in seconds. -
Set Attention Block (SAB) — the
TNNet.AddSAB(InducingPoints, Heads, DFF)builder, the Set Transformer's Set Attention Block (Lee et al. 2019): it wraps the multi-head MAB (built exactly likeAddInducedSetAttention—Headsheads, each a per-token input projection feeding a single-headTNNetInducedSetAttentionwith its own inducing bank,DeepConcat, per-token out-projection) in two post-norm residual sublayersH=LayerNorm(X+MAB(X,X)),out=LayerNorm(H+FFN(H)), with a token-wise FFN (TNNetPointwiseConvReLU(DFF)->TNNetPointwiseConvLinear(d_model)). Every op is a 1×1 (pointwise) conv over Depth except the symmetric softmax, so the block is shape-preserving(N,1,d)->(N,1,d)and permutation-equivariant (shuffle the rows, the output rows follow). Two parts: (1) the SAB stack's output rows track the input permutation tomax|dy| < 1e-6before training; (2) on a per-element above-the-set-mean classification task — which REQUIRES cross-element interaction (the mean depends on every element) — a 2-block SAB stack reaches ~97% while a per-element MLP baseline (no cross-element path) is stuck near its ~86% ceiling because it cannot compute the set mean. Pure CPU, deterministic, runs in seconds. -
Perceiver latent-bottleneck encoder — the
TNNet.AddPerceiverEncoder(NumLatents, d_latent, Heads, Depth)builder, the Perceiver latent bottleneck (Jaegle et al. 2021, Perceiver: General Perception with Iterative Attention; Perceiver IO 2021). A small, fixed-size learnable latent arrayZofNumLatentsrows (NumLatents << InputSeqLen) (1) cross-attends ONCE to the (possibly huge) input to absorb it into a(NumLatents,1,d_latent)summary — the only step touching the input, cost LINEAR in length — then (2) is refined by a stack ofDepthself-attention + FFN blocks acting only over theNumLatentsrows (O(NumLatents^2)per block, independent of length). Composes already-landed pieces: a token-wise input projection,AddAttentionPooling(NumLatents, Heads)as the latent cross-attention read (its seed bank IS the latent arrayZ), andAddTransformerEncoderBlock×Depthfor the tower. Output length =NumLatentsregardless of input — the missing third mode vs the Set-Transformer builders (InducedSetAttentionprojects BACK toninput rows;AttentionPoolingis a single pool with no refinement). Headline: the demo builds the SAME net onSEQLENand2*SEQLENand prints identical weight counts (doubling the input adds ZERO weights — the cost lives in the latent tower), then classifies a deliberately long 256-token input climbing from chance (~25%) to 100%. Pure CPU, ≈55 s. -
Product-Key Memory (sparse key->value retrieval) — the
TNNetProductKeyMemorylayer /TNNet.AddProductKeyMemory(NumKeys, ValueDim, TopK, Heads)builder (Lample et al. 2019, Large Memory Layers with Product Keys): a large, sparsely-accessed key→value memory that factorizes|K|keys into the Cartesian product of two small half-key banksK1,K2(eachsqrt(|K|)keys of half the query dim), so a query is scored against the productK1 x K2inO(sqrt(|K|))work — top-TopKper half, re-score theTopK x TopKcombinations, pick the global top-TopK, softmax, and gate a sparse weighted sum over only the touched value rows. The demo learnsNumPairs=24random(query, value)associations and trains the product-key memory against a same-capacity flat (dense softmax over allNumKeys) baseline on identical data: the product-key memory matches retrieval accuracy (MSE 0.000000vs0.000019) while touching onlyTopK=4value rows per query instead of all64. Prints a per-slot read-count histogram (49/64distinct slots used here) to expose the classic key-usage collapse failure mode, with a README note on the paper's batch-norm-on-query fix that spreads reads. v1 is single-head (Heads=1; multi-head is a documented follow-up). Pure CPU, deterministic, ~4 s. -
Hopfield Retrieval - A modern Hopfield network as attention (Ramsauer et al. 2020, Hopfield Networks is All You Need): store K patterns and recover a corrupted query in a SINGLE softmax-attention step
softmax(β·Xq)X. Sweeps the inverse-temperature β to show the blurry-average → clean-snap transition, and the corruption level to find the capacity edge. Forward-only, pure CPU, ~1s. -
Modern Hopfield associative memory (one-shot recall) — the continuous modern-Hopfield layer (
TNNetModernHopfield/TNNet.AddModernHopfieldRetrieval, Ramsauer et al. 2020, Hopfield Networks is All You Need): an ENERGY-BASED associative memory over a learnable bank of stored patternsXthat ITERATES the softmax retrievalxi := X^T · softmax(beta·X·xi)to a fixed point — distinct from the single-pass attention layers becauseK>1update steps SHARPEN toward the single nearest stored memory. The demo stores four binary 8×8 pixel patterns directly in the bank, presents each as a bottom-half-masked + noise-flipped query, and contrastsK=1(one-pass attention, a blurry blend of patterns) againstK=3(iterated retrieval) — iterating cleanly completes the pattern (e.g. total Hamming 8→0 and L2 14.4→7.5 over the four queries) where one pass does not. Pure CPU, <1 s. -
Neural Turing Machine (writable external memory, COPY task) — the headline demo for
TNNetNTMMemory, a writable differentiable external-memory layer (Neural Turing Machine, Graves et al. 2014, arXiv:1410.5401). Unlike the read-only associative memories (TNNetModernHopfielditerated recall,TNNetProductKeyMemorysparse lookup), an NTM carries a persistent memory matrixM(NumSlots × SlotWidth) the layer both reads and writes as it sweeps the time axis of an(T,1,InputDim)input → emits the per-step read vectors(T,1,SlotWidth). Per step the input is projected to a content key (cosine-addressed against every slot, softplus-beta-sharpened softmax over slots → weightsw, readr=w^T·M) plus a sigmoid eraseeand addathat updateM[i] := M[i]·(1−w[i]·e)+w[i]·a; the four projection matrices are the only trainables and backprop is full BPTT through the recurrentMupdate (bothdL/dManddL/dwchain backward across steps). The classic COPY task presents a random binary sequence + delimiter then asks the net to reproduce it from memory: the NTM matches a param-matchedTNNetSLSTMCellarm's recall (87.5% bit-accuracy) at ~2.6× fewer weights (138 vs 366). v1 is content-addressing only, single read+write head (location-based addressing + DNC temporal links are documented follow-ups). Pure CPU, ~0.8 s. -
Holographic Reduced Representation cleanup memory — the headline demo for the new
TNNetHolographicBinding, the Holographic Reduced Representation (HRR) vector-symbolic binding layer (Plate 1995, Holographic Reduced Representations), the associative-binding sibling of the exotic-algebra family (TNNetComplexLinear/TNNetQuaternionLinear/TNNetOctonionLinear/TNNetTropicalLinear/TNNetHyperbolicLinear). It reads two equal-length Depth vectorsaandbpacked as adjacent halves of the input (Depth must be even=2n; firstnchannels =a, lastn=b, the same adjacent-halves idiomTNNetComplexLinearuses for Re/Im) and outputs then-vector circular convolutionc = a ⊛ b,c[k] = Σ_j a[j]·b[(k-j) mod n]— the HRR bind operator that composes a role→filler pair into a new vector dissimilar to both operands, so many pairs can be superposed (just added) into ONE fixed-width trace. AnUnbindflag switches the forward to the circular correlationc = a ⊛ involution(b)(the approximate inverse used to query a bound trace). Distinct from the FFT mixers (TNNetFourierMix/TNNetFourierMixFFTlearn a spectral mix of one tensor; HRR is a bilinear bind of two). Weightless; directO(n²)cyclic forward, exact bilinear adjoint backward (both bind and unbind input gradients numerically gradient-checked, max-abs err ≈2e-4/8e-4),Unbindround-trips viaFStruct[6]. The demo (n=256) binds a growing numberPof randomkey→valueatom pairs into ONE superposed tracet = Σ_i key_i ⊛ value_i, unbinds each key, and snaps the noisy result to the nearest codebook value (cosine cleanup): single-pair sanity confirms the algebra (trace dissimilar to its own fillercos ≈ 0, correct-key unbind recovers the valuecos ≈ 0.72→ right nearest neighbour), and the HRR capacity curve degrades GRACEFULLY — 100% recall atP=1..9down to 88.5% atP=24(the textbook superposition tradeoff, not a catastrophic cliff). Noted follow-ups: an FFTO(n log n)path, a learnable per-channel "protect" permutation, and aTNNet.AddHRRMemorybuilder pairing binding with aTNNetVectorQuantizercodebook. Pure CPU, no training, ≈2 s. -
Graph node classification (spectral GCN) — the headline demo for the new
TNNetGraphConvolution(Kipf & Welling 2017, Semi-Supervised Classification with Graph Convolutional Networks), a layer that does MESSAGE PASSING over an arbitrary node graph rather than a grid (image) or 1-D sequence. The input is a(NumNodes,1,FeatureDim)volume;SetAdjacency(A)takes the raw 0/1 adjacency and builds the symmetric-normalizedAhat = D^-1/2 (A+I) D^-1/2internally, and forward isH' = Ahat·(H·W)(+bias)— a per-node pointwise linear map over the feature axis (nodes never mixed byW, reusing thePointwiseConvLinearweight layout) followed by a constant-Ahatneighbour aggregation (its backward just left-multiplies the error by the symmetricAhat). Two stacked GCN layers + SoftMax do semi-supervised TRANSDUCTIVE node classification on a synthetic two-community stochastic-block-model graph from only a handful of labelled nodes: the GCN reaches 100% held-out accuracy while a param-matched features-only MLP baseline (identity adjacency) sits at 50% chance — the message passing is what carries the signal. The adjacency is caller-supplied and NOT serialized (re-SetAdjacencyafter load). Pure CPU, ~2 s. -
Graph attention vs spectral GCN — the headline demo for the new
TNNetGraphAttention(Veličković et al. 2018, Graph Attention Networks), the ATTENTIONAL counterpart to GraphNodeClassification's fixed-weightTNNetGraphConvolution. Same(NumNodes,1,FeatureDim)layout and caller-suppliedSetAdjacency0/1 mask, but instead of a constant symmetric-normalizedAhat, each edge gets a LEARNED coefficient:e[i,j] = LeakyReLU(a_src·Z[i] + a_dst·Z[j])(slope 0.2) over the transformed featuresZ = H·W, masked to the graph's edges and softmax-normalized per node's neighbourhood, thenY[i] = Σ alpha[i,j]·Z[j]. The two arms share data + seed on an SBM graph with injected NOISY/HETEROPHILOUS cross-community edges; GAT down-weights the bad edges the GCN is forced to average in, reaching 90% held-out vs the GCN's 85% (+5 pp). Honest caveat (in output + README): attention only wins in this noisy-edge + class-indicative-feature regime — on a clean graph with weak features the GCN's symmetric averaging already wins. Single head; the attention vector serializes per-neuron, the adjacency is caller-supplied and re-SetAdjacencyafter load. Also demos MULTI-HEAD GAT via theTNNet.AddMultiHeadGraphAttentionbuilder (K independent heads CONCAT in the hidden layer, AVERAGED at the output — paper eq. 5/6; 4 heads beats 1 head by ~7.5 pp) and the paper's ATTENTION-DROPOUT regulariser on the normalized per-edge coefficients (training-time only, deterministic at inference; ~+5 pp on the noisy graph). Pure CPU, a couple of seconds. -
Fourier features and spectral bias — the headline Tancik et al. 2020 spectral-bias demo for
TNNetFourierFeatures: the SAME small ReLU coordinate-MLP fits a high-frequency targety = sin(20x) + 0.5*sin(53x)once on the raw scalarxand once behind a fixed random Fourier-feature front-end (the raw MLP cannot fit the high frequencies, the Fourier one nails them — ~700x lower MSE), then sweepssigma in {0.5, 2, 8, 32}to show the single-knob bandwidth U-curve. -
Random Fourier Features RBF-kernel demo — the headline "a single FROZEN random-feature layer + linear head = an RBF-kernel machine" win for the new
TNNetRandomFourierFeatures(Rahimi & Recht 2007). It maps aDin-vectorxto a2·D-vectorphi_k(x) = sqrt(1/D)·[cos(w_k·x), sin(w_k·x)]with the projection rowsw_k(D×Din) drawn once i.i.d. fromN(0, 1/sigma²)and frozen by default, so that<phi(x),phi(y)> → exp(-‖x-y‖²/(2·sigma²))(the RBF / Gaussian kernel) asDgrows and a plain linear head overphi(x)approximates a kernel SVM — without forming theN×NGram matrix. This is mathematically DISTINCT from the learnable FFT layers (TNNetFourierMixFFT,TNNetSpectralConv1D/2D, theTNNetCirculantLinearFFT path): RFF is a FIXED random Gaussian projection of a shift-invariant kernel, not a transform along a signal axis. An optional constructor flag makesWtrainable ("deep kernel learning";sigmastays fixed).D/seed/trainable round-trip viaFStruct[0,5,6],sigmaviaFFloatSt[0], andWreloads identically. The demo classifies concentric rings (not linearly separable): the frozenRFF(D=256) → FullConnectLinear → SoftMaxmodel hits ≈1.0 test accuracy (matching a ReLU MLP) while the same linear classifier on the raw(x,y)is stuck at ≈0.47 (chance — one straight boundary). Pure CPU, ≈5 s. -
FNet Fourier token mixer bake-off — the FNet expressiveness-vs-cost trade for the new
TNNetFourierMix, the parameter-free token mixer of Lee-Thorp et al. 2021 (FNet). Over a(SeqLen, 1, d)sequence it replaces self-attention with an UNPARAMETERISED 2D real DFT across the sequence and hidden axes,y = Re(DFT_seq(DFT_hidden(x))), so the mixer holds zero trainable weights. BecauseRe(DFT)is a fixed self-adjoint real linear operator, the exact input gradient is the same DFT applied todL/dy(verified against finite differences). On a tiny global token-mixing task the FNet arm (Fourier mix + per-token MLP) is benchmarked head-to-head against an attention arm (AddMultiHeadSelfAttention+ the same MLP): the Fourier mixer drops the entire learned Q|K|V|out projection (≈51% fewer parameters) and trades only a modest accuracy loss — the paper's point that on short sequences the mix is nearly free. Distinct fromTNNetTokenShift(RWKV t-1 shift),TNNetCirculantLinear(learned circular conv) and attention (learned mix): this one is a fixed, weightless spectral mix. Opt-in radix-2 FFT fast path (UseFFT, default off, checked vs the direct path to<1e-5). Pure CPU, ≈13 s. -
Fourier Neural Operator resolution-invariance demo — the headline resolution-invariance win for the new
TNNetSpectralConv1D, the core layer of the Fourier Neural Operator (Li et al. 2021, arXiv:2010.08895) and a layer with learnable complex spectral weights (distinct from the parameter-freeTNNetFourierMixand the fixed-randomTNNetFourierFeatures). Over a(SeqLen, 1, InDepth)sequence it takes a real radix-2 FFT alongSeqLen(reusing the provenFourierMixFFThelper), truncates to the lowestModesfrequencies (a spectral low-pass), applies a learnable per-(in,out)-channel complex weightR[m]per kept mode (anInDepth×OutDepthcomplex matmul packed via the same 2×2 complex-multiply idiom asTNNetQuaternionLinear/TNNetOctonionLinear), then inverse-FFTs back. Because the learned weights live in mode space, not grid space, the SAME weights describe the SAME continuous operator at ANY resolution. The demo learns the 1-D antiderivative operator (u' = f, a Poisson/diffusion-step solution operator) on a coarse32-point grid, then evaluates with no retraining on a finer64-point grid it never saw (weights copied withCopyWeights): the FNO keeps essentially the same held-out relative-L2 error across resolutions (≈7.9% → 7.9%) while a param-matched localTNNetCausalConv1Dbaseline — whose taps encode a fixed grid spacing — degrades sharply (≈63% → 86%). Backward is the exact real adjoint of the FFT → complex-matmul → IFFT pipeline; both the input AND the complex (real+imag) spectral-weight gradients are numerically gradient-checked. Pure CPU, ≈90 s. -
2-D Fourier Neural Operator resolution-invariance demo — the 2-D headline for the new
TNNetSpectralConv2D, the 2-D spectral convolution of the Fourier Neural Operator (Li et al. 2021, arXiv:2010.08895) and the natural 2-D sibling ofTNNetSpectralConv1D. Over a(SizeX, SizeY, InDepth)image it takes a 2-D FFT (real radix-2 FFT along X for every row, then along Y for every column — both reuse the provenFourierMixFFThelper), truncates to the lowestModesX × ModesY2-D modes (a 2-D spectral low-pass), applies a learnable per-(in,out)-channel complex weightR[mx,my]per kept 2-D mode (anInDepth×OutDepthcomplex matmul packed via the same 2×2 complex-multiply idiom asTNNetQuaternionLinear/TNNetOctonionLinear), then inverse-2D-FFTs back and takes the real part. Because the learned weights live in 2-D mode space, not grid space, the SAME weights describe the SAME continuous operator at ANY resolution. The demo learns a smooth 2-D low-pass diffusion operator (each 2-D mode scaled by1/(1+c·(kx²+ky²))) on a coarse grid, then evaluates with no retraining on a finer grid it never saw (weights copied withCopyWeights): the 2-D FNO keeps essentially zero held-out relative-L2 error across resolutions (≈0.01% → 0.01%) while a param-matched localTNNetConvolutionReLU3×3 baseline — whose taps encode a fixed grid spacing — degrades sharply (≈29% → 58%). Backward is the exact real adjoint of the 2-D FFT → complex-matmul → 2-D IFFT pipeline; both the input AND the complex (real+imag) spectral-weight gradients are numerically gradient-checked.OutDepth/ModesX/ModesYround-trip viaFStruct[0..2]. Pure CPU, small grids, ≈2 min. -
Darcy-flow PDE surrogate (FNO coefficient→solution map) — the headline FNO use case applied end-to-end with the
TNNet.AddFourierNeuralOperator2Dbuilder: learn a parametric-PDE coefficient→solution operatorG : a(x,y) → u(x,y)in ONE forward pass, replacing an iterative numerical solve. The data is generated in pure Pascal at startup — a smooth strictly-positive permeabilitya = exp(band-limited random field)and the deterministic SOLUTIONuof the periodic Poisson problem-Δu = (a-1)(the linear, FNO-tractable member of the Darcy-flow family), obtained by Jacobi sweeps of the 5-point finite-difference Laplacian with the grid spacingh²folded into the source so the SAME continuous operator is reproduced at any resolution. The surrogate () trains with MSE on a grid: held-out relative-L2 error drops≈0.64 → 0.025over 150 epochs (HEADLINE 1, the FNO learns the operator). HEADLINE 2 (resolution invariance): the SAME trained weights are evaluated with NO retraining on a finer grid drawn from the same continuous operator (weights copied withCopyWeights) — error stays bounded (0.0251at →0.0284at the unseen ) because the spectral weights live in resolution-independent mode space. README documents honestly that the fully NONLINEAR-div(a grad u)=fDarcy operator (non-diagonal in mode space) does not converge under plain SGD within the CPU budget, so the linear Poisson member is used. Power-of-two grids only (the separable radix-2 FFT requires it). Pure CPU, ≈3 min. -
Wavelet shrinkage denoising — the headline "multi-resolution basis beats a single-scale low-pass filter" win for the new
TNNetDWT1D, the lifting-scheme single-level 1-D discrete wavelet transform. Over a(SeqLen,1,Depth)sequence one level maps(L,1,D) → (L div 2,1,2·D)(firstDchannels = approximation / low-pass band, nextD= detail / high-pass band); forward and inverse share ONE lifting step list so it is exactly invertible for any taps (InverseChannelreconstructs a channel from packed[approx|detail]bands), with Haar / CDF-5/3 / Daubechies-4 filter selectors and an optional learnable-taps mode. The demo runs the classic Donoho-Johnstone wavelet shrinkage denoiser on the canonical piecewise-constant "Blocks" signal under Gaussian noise: a Mallat pyramid (recurse the DWT on the approximation band only) → per-level soft-threshold of the detail coefficients at the universal thresholdλ = σ·sqrt(2 ln M)with a robust MADσestimate → inverse pyramid. Because Blocks is sparse in the wavelet domain (jump energy collapses onto a few large detail coefficients) but dense in the local-average domain, wavelet shrinkage reaches ≈21.2 dB reconstruction SNR, beating a param-free moving-average low-pass baseline (≈16.6 dB, +4.6 dB) and the noisy input (≈18.3 dB, +2.9 dB) — the single fixed scale must blur the very edges that define the signal. Also exercises the newTNNet.AddWaveletPacketTransform(Levels, Filter, Learnable)builder, which stacksLevelssingle-level DWTs into the full balanced wavelet-PACKET tree (every channel recursively decomposed →(SeqLen div 2^Levels, 1, (2^Levels)·Depth)). Pure CPU, no training, ≪1 s. -
Learning to sort through a doubly-stochastic relaxation — the headline "learn a DISCRETE operation through a CONTINUOUS relaxation" win for the new
TNNetSinkhorn, a differentiable optimal-transport / doubly-stochastic normalization layer (Mena et al. 2018, arXiv:1802.08665). WhereTNNetSoftMax/ sparsemax normalize ONE axis,TNNetSinkhornnormalizes a square(N,1,N)score matrix to be doubly stochastic (every row AND every column sums to 1) by iterating Sinkhorn–Knopp in log-space —KIteralternating row/col subtract-logsumexp steps onscore/tau, thenexp. Doubly-stochastic matrices are the convex hull of permutation matrices, and as the temperaturetau → 0the output sharpens to a hard permutation, so a permutation becomes a smooth function of a score matrix. The demo trainsInput(N) → FullConnectReLU → FullConnectLinear → Reshape(N,1,N) → TNNetSinkhornto sort 5 scalars: the soft permutationPis applied to the input (yhat = P·x) and trained with plain MSE against the ascending sort, the loss gradientdL/dP[i,j] = (yhat[i]−sorted[i])·x[j]set by hand on the Sinkhorn output and back-propagated through the entire unrolled iteration (each step caches its input so backward replays the exact softmax-style adjoint). Annealingtaufrom1.0 → 0.07sharpensPand exact-sort accuracy climbs monotonically ≈2.5% → ≈62.5% (chance is 1/120 ≈ 0.8% for all 5 elements). No trainable params in the layer;KIter/tauround-trip viaFStruct[0]/FFloatSt[0]. Pure CPU, smallNand batches, ≈50 s. -
Solving a linear-assignment problem with a soft permutation — the optimal-transport / bipartite-MATCHING companion to the sort demo, on the canonical linear-assignment task (distinct from sorting). Given an
N × Ncost matrixC, the net emits a soft permutation matrixPthroughTNNetSinkhornand is trained on the continuous OT cost of that soft assignment,L = sum_{i,j} P[i,j]·C[i,j]with the trivial hand-set gradientdL/dP[i,j] = C[i,j]back-propagated through the entire unrolled Sinkhorn iteration. The net is never told the optimal permutation — only its own soft cost — yet learns to assign workers to tasks at minimum cost. PipelineInput(N,1,N) → PointwiseConvReLU → PointwiseConvLinear(N) → TNNetSinkhorn(token-wise score head over the row axis;FullConnectwould flatten/mix rows). Annealingtaufrom1.0 → 0.15sharpensPand on held-out cost matrices the exact-match rate climbs ≈0.5% → ≈95% (chance 1/24 ≈ 4% forN=4) while the mean optimality gap shrinks ≈200× (0.32 → 0.0015). Evaluation brute-forces the true optimum to score the gap. Pure CPU, tinyN/batches, ≈18 s. -
A spiking net learns through a surrogate gradient — the headline "an EVENT-DRIVEN spiking net learns via a SURROGATE GRADIENT" win for the new
TNNetLIFNeuron, a spiking leaky-integrate-and-fire neuron layer (Neftci, Mostafa & Zenke 2019; Zenke & Ganguli 2018, SuperSpike). A new computational paradigm: a stateful neuron integrates an input current into a membrane potential over a time axis and emits a binary {0,1} spike when it crosses threshold, then resets —V[t] = beta·V[t-1]·(1−S[t-1]) + I[t],S[t] = 1 if V[t] ≥ V_th(beta = exp(-1/tau)leak). The forward is exactly this hard, faithfully-binary dynamics; since the Heaviside derivative is zero almost everywhere, the backward substitutes the fast-sigmoid SuperSpike surrogatesigma'(V) = 1/(1+alpha·|V−V_th|)^2and back-propagates through time across theTunrolled steps. No trainable params (a pointwise neuron model over an upstream linear/conv layer, like an activation);beta/V_th/alpharound-trip viaFFloatSt[0..2]. The demo rate-encodes a few synthetic classes as Bernoulli spike trains and trainsInput(T,1,DIN) → PointwiseConvLinear → TNNetLIFNeuron → AvgChannel(rate readout) → FullConnect → SoftMax. Headline payoff: accuracy reported alongside the spike rate — the net reaches ≈99% while its hidden neurons fire on only ≈14% of (time, neuron) sites (≈86% silent, event-driven sparsity). Honest caveat: hard-forward / smooth-backward means the surrogate is a biased gradient estimator, so it wants a gentler LR / more steps than a ReLU MLP (training dips before it climbs). Pure CPU, tiny dims, ≈2 s.
Science-of-deep-learning phenomena
- Toy models of superposition — pure-CPU reproduction of Anthropic's Toy Models of Superposition (Elhage et al. 2022) using existing layers only. An importance-weighted-MSE autoencoder
Input(20) → TNNetFullConnectLinear(5) → TNNetFullConnectReLU(20)packs more sparse features than it has bottleneck dimensions, and the geometry it picks tracks feature sparsity. Sweeping sparsityS ∈ {0.0, 0.7, 0.9, 0.99}shows the monosemantic→polysemantic phase transition (superposition ratio 1.0 dense → 3.8 sparse), with per-feature represented-norm bars and a glyph-shaded interference-matrix heatmap. Runs in ~2 min. - Double descent — pure-CPU reproduction of the model-wise "double descent" risk curve (Belkin et al. 2019; Nakkiran et al. 2020). The same tiny MLP is trained across a width sweep
H ∈ {1..128}on a 60-sample regression task with 15% label noise; test error falls, then rises to a sharp peak at the interpolation threshold (params ≈ train size), then falls again in the over-parameterised regime. A label-noise on/off ablation shows the peak is noise-driven (the clean-label curve is ~monotone). Runs in ~1 min. - Epoch-wise double descent — reproduces the THIRD axis of double descent from Nakkiran et al. 2020 (the epoch-wise / temporal figure): a FIXED mildly over-parameterised MLP is trained on a small label-noisy classification set and held-out test error is charted against training EPOCH (the only swept axis is time). Pure-CPU, in-tree layers only. Complements the model-wise DoubleDescent example.
- Random label memorization — pure-CPU reproduction of Zhang et al. (ICLR 2017), Understanding deep learning requires rethinking generalization. The same fixed over-parameterised MLP () is trained twice on the same 5-class Gaussian-blob inputs: once with true labels and once with the labels randomly shuffled. Both runs reach ~100% train accuracy (it memorises pure noise), yet only the true-label run generalises (test ≈ 100% vs ≈ chance 1/K for random labels) — train error alone says nothing about generalisation. Complements DoubleDescent (fixed capacity, true-vs-random labels). A Part 2 label-corruption-fraction sweep (
p ∈ {0.0, 0.25, 0.5, 1.0}) charts the smooth interpolation between real structure and pure memorization: epochs-to-fit-train rises withpand the train/test gap widens (0% → 75%). Runs in ~12 s. - Edge of Stability - Reproduces "progressive sharpening" under full-batch gradient descent: the top Hessian eigenvalue rises to ~
2/ηand hovers there (Cohen et al. 2021), measured online withTNNet.HessianCurvatureReport - EWC continual learning — pure-CPU reproduction of catastrophic forgetting and the Elastic Weight Consolidation cure (Kirkpatrick et al., PNAS 2017). A tiny 4-class 2-D MLP is trained to convergence on Task A, then continued on Task B (the same clusters with the input coordinates rotated, a "perturbed-input" task in the spirit of permuted-MNIST). After Task A it snapshots the optimal weights
w_Aand the diagonal empirical FisherF_iof every parameter, computed exactly the wayTNNet.FisherImportanceReportdoes (accumulate squared per-parameter gradients over Task A on a frozen net; the curvature an EWC penalty consumes). Two arms then learn Task B: PLAIN sequential fine-tuning (no penalty → Task-A accuracy collapses, the forgetting) versus EWC, which after each weight step applies a decoupled, clamped pullw_i ← w_i − clamp(LR·λ·F_i,0,1)·(w_i − w_A_i)— the gradient of the quadratic penaltyΣ (λ/2)·F_i·(w_i − w_A_i)²— that pins the high-Fisher (important-for-A) parameters atw_Awhile leaving the low-Fisher ones free to learn B (the clamp keeps the step stable despite the large λ the tiny empirical Fisher forces). The headline is a 2×2 table — on this run PLAIN drops Task A to 0.285 while learning B to 1.000, whereas EWC retains Task A at 0.535 (a +0.25 retention gain) for only a 0.016 Task-B cost. Built-in PASS/FAIL gates assert that PLAIN actually forgets A and that EWC retains A meaningfully better, so the demo cannot silently prove nothing. Manual training loop (SetBatchUpdate(true)); pure CPU, single seed, ~3 s, no download.
Diagnostics & introspection reports
Architecture & cost
- Architecture diff — demonstrates
TNNet.DiffArchitecture(OtherNet)by building two near-identical classifier variants (one with an extraTNNetChannelStdNormalization, one with a swapped activation) and printing a unified-diff-style report so refactors of builder helpers are easy to verify. - FLOPs report — builds a tiny MLP and a tiny CIFAR-style conv stack and prints
TNNet.CountFLOPsPerLayer(NN): per-layer estimated forward-pass FLOPs, the layer's share of the network total, and a tally of layer classes the estimator doesn't model (so unrecognised layers are visibly accounted for). Pure structure inspection — no training, no probe batch. - Memory-footprint report — demonstrates
TNNet.MemoryFootprintReport, a static per-layer estimator of activation / parameter / gradient memory for aTNNet. Builds a tiny MLP (64 -> 256 -> 256 -> 10 + softmax) and prints its per-layer memory table. Pure structure inspection — no probe batch, no forward pass. - Receptive-field report — a purely analytical walk (no data, no forward pass) that propagates the receptive-field recurrence through a VGG-style 3x3 stack and a stride-2 downsampling stack and prints
TNNet.ReceptiveFieldReport(NN): per-layer cumulative receptive-field size, effective stride (jump), the fraction of the input plane one deepest output unit can see, the shallowest layer whose RF first covers the whole input ("the rest is global mixing" cut point), and a 1x1/pointwise-tail flag list. Tracks X and Y independently. Answers "does my stem see enough context before the global-pool head?" with no trained weights. - Effective receptive-field report — runs
TNNet.EffectiveReceptiveFieldReport(NN, Probes), the empirical (gradient-measured) counterpart of the analyticalReceptiveFieldReport: it answers "what input region does a deep output unit actually WEIGHT?" rather than "what COULD it see?". It picks the centre output unit of the final spatial layer, enables input-gradient flow viaTNNet.EnableInputGradient, and over a probe batch back-propagates a one-hot output error to accumulate|d out_centre/d input|(summed over depth) into a per-(x,y)input-plane ASCII heatmap, then reports the effective RF (smallest centred square holding 90% of the gradient mass — radius, per-axis half-widths, centroid, mass-weighted spatial std) and the effective/theoretical ratio side-by-side (it callsReceptiveFieldReportinternally for the analytical number). The example sweeps kernel-size / stack-depth configs (1x3x3 .. 4x5x5) whose theoretical RF roughly doubles, prints a(config, theoretical_RF, effective_RF_diam, eff/theo)table, and writes the(radius, mass_fraction)CSV side-output per config — single large kernels weight their whole window (ratio ≈ 1.0) while stacked small kernels concentrate the gradient (ratio drops to ≈ 0.76), so the effective RF growing sub-linearly in the theoretical RF (Luo et al. 2016) is visible in one run. Distinct fromSaliencyReport(per-sample attribution for a class logit, not a batch-averaged spatial-extent measurement). Forward+backward only on a frozen net; weights are never modified.
Gradient, weight & training health
- Gradient-norm report — runs a single forward+backward pass on a 12-layer ReLU MLP with and without a midpoint
TNNetLayerNormand printsTNNet.GradientNormReport(NN, Input, Target): per-layer||dL/dx_in||and||dL/dW||, consecutive-layer ratio, vanishing/exploding flags, and alog1010-bin ASCII histogram. - Numerical-gradient eps sweep — a didactic finite-difference diagnostic: takes one well-tested net (
TNNetFullConnectLinear->TNNetHyperbolicTangent, fixed hand-set weights/input, MSE loss) and runs the exact central-difference gradient check fromtests/TestNeuralNumerical.pasforeps in {1e-1 .. 1e-7}, printing per-eps max abs/rel error vs the analyticBackpropagategradient. Reproduces the classic U-shaped error curve — largeepsdominated byO(eps^2)truncation, tinyepsdominated by FP32 round-off/cancellation — with the minimum neareps ~ 1e-2..1e-3. Explains why the test suite probes ateps = 1e-4with a~0.01tolerance (FP32, not FP64, sets the sweet spot). Forward+backward only, no RNG, sub-second. - Weight-histogram report — builds a small MLP and prints
TNNet.WeightHistogramReport(NN)before and after a short training run: per-trainable-layer min/max/mean/std,||W||_2,||W||_inf, near-zero count, and a per-layer 32-bin ASCII bar histogram over[-MaxAbs, +MaxAbs]. Lets you eyeball how the weight distribution moves away from its init. - Weight-spectrum report — for every trainable layer prints
TNNet.WeightSpectrumReport(NN)before and after a short training run: the top singular valuesigma_1(W)(a few power-iteration steps via the reusableTNNet.EstimateSpectralNormhelper),||W||_F, the stable-rank-flavoured ratiosigma_1/||W||_F(near 1 hints at rank-1 collapse), a Marchenko-Pastur baseline ratio, a network histogram, and a flag list (spectral-norm > threshold / stable-rank ≈ 1). Pure forward-only on the weight tensors — no probe batch. - Weight spectral-tail report — runs
TNNet.WeightSpectralTailReport(NN), a label-free, weights-only heavy-tailed self-regularization (HT-SR) diagnostic that predicts per-layer training quality from the weights alone — no probe batch, no labels, no test set (Martin, Mahoney & Peng 2021, Nature Communications; the WeightWatcheralphametric). For every trainable layer it forms the smaller Gram matrix of the reshaped weight tensor (W^T WorW W^T), computes its full eigenvalue spectrum with a self-contained Double-precision cyclic Jacobi eigensolver, and fits a power lawrho(lambda) ~ lambda^(-alpha)to the upper tail via the Clauset/Hill MLE (swept overlambda_mincuts, min-KS selection). It reports per layer the power-law exponent alpha (well-trained layers land in[2,4];>6flags under-trained / still-random-like,<2flags over-correlated / memorising), the capacity-weighted weighted-alphaalpha*log10(lambda_max), the KS goodness-of-fit,lambda_max, the Marchenko-Pastur bulk edge, and alog10(lambda)histogram, plus the network-level average weighted-alpha (a single label-free model-quality scalar), an alpha-across-depth chart, and per-layer flags. Built-in checks: PSD non-negativity, Frobenius trace invariance, andlambda_max == EstimateSpectralNorm(W)^2. Pure weight inspection — no forward pass, weights never touched. - Weight-drift report — trains a 6-layer ReLU MLP on a tiny hypotenuse-like task for a few epochs with ONE hidden layer's
LearningRatepinned at 0, snapshots the network before and after training, and printsTNNet.WeightDriftReport(SnapA, SnapB): the frozen layer shows ~0 L2 drift and a ~1.0 frozen fraction while the surrounding layers show non-trivial drift. Pure CPU, runs in seconds. - Activation-stats report — runs a probe batch through a small net and prints
TNNet.ActivationStatsReport(NN, Probes)before and after a short training run: per-layer mean/std/min/max/|median|/|skew|/kurtosis, saturated/negative/near-zero fractions, a 16-bin ASCII histogram per layer, plus a network-level per-layer-std histogram and a flag list (near-collapsed and saturating layers) so vanishing/exploding activation patterns jump out at a glance. Pure forward-only. - Dead-neuron report — trains the same ReLU MLP twice (sane LR vs deliberately aggressive LR) and prints
TNNet.DeadNeuronReport(NN, Probes)for each: per-TNNetReLUBase-family layer dead-unit count and percentage across a probe batch, mean per-sample zero-fraction, a 10-bin ASCII histogram of dead% across layers, and the worst layer. Surfaces the dying-ReLU pathology empirically. - Dead-ReLU diagnostic — trains the same small classifier four times, identical except the hidden activation (
TNNetReLU/TNNetLeakyReLU/TNNetGELU/TNNetSwish), and prints the per-epoch fraction of dead hidden units plus a summary table comparing the four. Unlike the static Dead-neuron report, this is a per-epoch trajectory and a cross-activation comparison: under an aggressive LR plain ReLU strands ~19% of units at zero gradient while the leaky/smooth activations lose none. - Dead-ReLU learning-rate sweep — the LR-sweep follow-up to the Dead-ReLU diagnostic: sweeps the learning rate over a grid (0.02 -> 0.5) and, for each LR, trains the same classifier with
TNNetReLU/TNNetLeakyReLU/TNNetGELU/TNNetSwish, printing an LR x activation table of peak dead-unit fraction. ReLU's dead fraction climbs monotonically (10% -> 17%) as the LR grows while the leaky/smooth activations stay at 0% across the whole sweep — the cleanest "dying ReLU is a learning-rate pathology" curve. - Neuron-correlation report — runs
TNNet.NeuronCorrelationReport(NN, Probes)on a probe batch before and after a short training run on a single-ReLU-feature synthetic target (y = max(0, w.x)): for every trainable layer it computes the pairwise Pearson correlationrho_ijbetween neurons across the batch (along the neuron axis) and prints a 10-bin|rho_ij|histogram, the top-K most-correlated pairs (a merge/prune candidate list), an effective neuron count = participation ratioN^2 / sum_ij rho_ij^2(in[1, N]), and per-layer flags (near-duplicate pair, collapsed layer, constant neurons). At fresh init the high-|rho|tail is empty and no near-duplicate flags fire; the trained net grows a clear|rho|>0.8tail, raises near-duplicate flags, and its effective neuron count drops as units align onto the single useful direction — intra-layer redundancy made visible. Pure forward-only. - Layer-sensitivity report — runs
TNNet.LayerSensitivityReport(NN, Probes [, Targets])across three model families (a tiny MLP with a loss target, a CIFAR-style conv stack, and an attention stack) on synthetic probe batches. For every trainable layer it multiplicatively jitters the weights (W *= 1 + eta,eta ~ N(0, sigma^2)), re-runs the probe batch to measure the forward output-delta L2 (and the MSE loss-delta when targets are supplied), then restores the weights exactly between trials via a whole-netSaveDataToString/LoadDataFromStringsnapshot. It prints per-layer mean/max output-delta, a param-count-normalised sensitivity, a 10-bin histogram, high-/low-impact flags (top/bottom 10%), and a one-line fragility verdict (max/median layer sensitivity). Pure forward-only, no backward pass. - Gradient-conflict report — runs
TNNet.GradientConflictReport(NN, Samples)on two labelled probe batches measured on the same frozen trained classifier, answering "do the samples in this batch pull the weights in compatible directions, or do they fight each other?". For each sample it runs one forward + one backward (ClearDeltasbefore each, neverUpdateWeights) and snapshots that sample's full flattened per-parameter weight-gradient vectorg_i(reusing the per-parameter gradient tensorsBackpropagatealready populates — no input-gradient enablement, likeFisherImportanceReport), then reports the pairwise gradient cosinecos(g_i,g_j) = <g_i,g_j>/(||g_i|| ||g_j||): a 10-bin ASCII histogram over[-1,1], the conflict fraction (share of pairs withcos < 0— gradients that actively undo each other) plus a strong-conflict fraction (cos < -0.5, the genuinely-opposed tail, since cross-class pairs of a softmax head sit just below zero by construction), the mean/median cosine, the most-conflicting sample pair (a "these two examples disagree most" pointer), and a per-class-pair mean-cosine matrix (numeric + glyph heatmap) so a class pair whose gradients systematically oppose stands out. An optionalLayerIdxrestricts the cosine to one trainable layer's gradient slab (the conflict is often concentrated in the classifier head). The example contrasts a clean linearly-separable 3-cluster batch (cosines clustered positive, conflict fraction ~0) against a deliberately label-noised / overlapping batch (a fat negative-cosine tail and a high conflict fraction emerge). The self-cosinecos(g_i,g_i)=1diagonal and matrix symmetry are the built-in correctness checks. Weights are never stepped (a measurement, not training). - Gradient noise-scale report — runs
TNNet.GradientNoiseScaleReport(NN, Samples), the gradient signal-to-noise diagnostic that analytically predicts the batch-size sweep (McCandlish et al. 2018, An Empirical Model of Large-Batch Training). On a frozen net (ClearDeltasbefore each sample, neverUpdateWeights) it snapshots each sample's full per-parameter weight-gradient vectorg_i, forms the mean gradient and per-parameter variance, and reports the per-parameter gradient SNR|g_bar|/stdhistogram + per-layer mean, the simple noise scaleB_simple = tr(Sigma)/||g_bar||^2(the critical batch size beyond which bigger batches stop buying faster convergence), the effective-batch noise curvenoise(B)=B_simple/B, and per-layer signal-/noise-dominated flags (with an optionalLayerIdxrestricting every statistic to one layer's gradient slab). The example contrasts a clean linearly-separable batch (high SNR, tinyB_simple) against a label-noised / overlapping batch (low SNR, largeB_simple) and prints an empirical batch-size sweep so the prediction can be eyeballed against reality. Built-in checks: feeding the same sample N times drivesB_simpleto ~0, and a single-sample batch warns rather than dividing by zero. Weights are never stepped. - Hessian-curvature report — runs
TNNet.HessianCurvatureReport(NN, Samples), a loss-surface sharpness diagnostic built on Hessian-vector products estimated by central-differencing the gradient (Hv ~= (grad L(theta+eps*v) - grad L(theta-eps*v))/(2*eps)) — no second-order autograd, reusing the whole-batch forward+backward machinery and theSaveDataToString/LoadDataFromStringsnapshot/restore pattern. On a frozen net it reports the Hessian tracetr(H)via the Hutchinson estimator over Rademacher probes (mean curvature), the top eigenvaluelambda_max(H)via power iteration on the HVP operator (the canonical flat-vs-sharp-minimum metric, Keskar et al. 2017 / Foret et al. SAM 2021), the curvature-concentration ratiolambda_max/(tr(H)/N), a per-layer trace breakdown, a per-probev^T H vhistogram, and a flat/moderate/sharp verdict. The example trains the same tiny MLP into a sharp minimum (small batch + high LR) and a flat one (large batch + low LR + weight decay) so thelambda_maxgap the generalization literature ties to sharpness is visible. Built-in checks: probe-count-independence oftr(H)on a linear net, andlambda_max <= tr(H)in the PSD regime. Weights are never stepped (a measurement, not training). - Local Learning Coefficient (LLC) report — runs
TNNet.LocalLearningCoefficientReport(NN, Samples [, ChainLen, Eps, Gamma]), an empirical estimate of the Local Learning Coefficient (LLC) — the Real Log Canonical Threshold (RLCT) from Singular Learning Theory (Watanabe; Lau, Murfet, Wei et al. 2023, Quantifying Degeneracy in Singular Models via the LLC). Unlike a second-order Hessian top-eigenvalue (HessianCurvatureReport), the LLC measures the volume-scaling / EFFECTIVE dimensionality of the minimum the optimizer settled into — it counts the flat, degenerate directions a sharpness metric cannot see, so a redundant / over-parameterised solution readsLLC_hat << dim(w)(far fewer effective degrees of freedom than raw weights). From the trained weightsw*it runs a short tempered, anchored SGLD chain pinned to the basin by a Gaussian anchor(gamma/2)*||w - w*||^2, then forms the WBIC free-energy estimateLLC_hat = n*beta*(mean_chain[L(w)] - L(w*))withbeta = 1/ln(n)and the per-step anchored-Langevin updatew <- w - (eps/2)*(n*beta*g + gamma*(w - w*)) + N(0, eps). It reuses the existing forward+backward gradient machinery (SetBatchUpdate(true),Delta = -LR*graddivided back out; the only new infrastructure is the anchored update + chain average) and is non-destructive —w*is snapshotted and restored bit-for-bit on return. The report printsLLC_hat, the raw parameter countdim(w)and the ratioLLC_hat/dim(w), with the caveat that the absolute value is calibration-dependent (it shifts witheps/gamma/chain-length) but the ordering across nets under fixed hyperparameters is the robust signal. The example contrasts a minimal trained net, an over-parameterised net with two hidden units forced redundant (duplicate-then-halve), and a random-init net: both trained nets read smallLLC_hat << dim(w)while the untrained net (not a critical point) reads a large/negativeLLC_hat— the honest "this is not a minimum" signal (the README documents the known pitfall and why the fine minimal-vs-over-parameterised ordering needs a longer chain than the CPU-toy budget allows). Distinct fromHessianCurvatureReport(sharpness / top-eigenvalue, blind to degeneracy) andIntrinsicDimensionReport(activation-manifold geometry, not loss-basin volume). Weights are never stepped (a measurement, not training). - Loss-landscape probe — given a trained network and a small validation batch, samples the loss along a random filter-normalised (Li et al. 2018) 1D direction in weight space at K offsets in
[-R, +R], then prints an ASCII curve, a "sharpness" scalar (second central difference of the loss at the centre), and the "loss-doubling radius" (smallest|alpha|whereL(alpha) > 2*L(0)). Restores the original weights at the end; usesTNNet.LossLandscapeProbe(NN, Samples, K, R). - Mode-connectivity report — runs
TNNet.ModeConnectivityReport(NN, SnapshotB, Samples), the linear mode-connectivity / loss-barrier diagnostic between two trained nets of the same architecture (Garipov et al. 2018; Frankle et al. 2020). With the live net as endpoint A and aSaveDataToStringsnapshot of endpoint B, it sweepsalpha in [0,1]atK+1points, sets the live weights to the interpolationtheta(alpha) = (1-alpha)*A + alpha*Bvia whole-net snapshot arithmetic (TNNetVolume.MulMulAdd, no per-scalar hot loop), runs one whole-batch forward overSamplesat each alpha, and reports the loss curveL(alpha)as a#-bar ASCII chart, the barrier heightmax_alpha L(alpha) - max(L(0), L(1))(>0= a bump between basins;~0= linearly connected), the argmax-alpha where the barrier peaks, and aconnected/weak barrier/separatedverdict. The example trains the same tiny MLP twice — once from the same init (low barrier, same basin) and once from different inits (higher barrier) — so the contrast is visible in one run. Built-in checks:L(0)/L(1)recomputed on the path match a direct forward to<1e-5(snapshot-arithmetic faithfulness), andB := Acollapses the curve to a flat zero-barrier line. Distinct fromWeightDriftReport(weight-space L2 drift, no loss along the path) andLossLandscapeProbe(one net, random direction). Endpoint-A weights are restored bit-for-bit; pure forward-only. - Permutation-alignment report (Git Re-Basin) — runs
TNNet.PermutationAlignReport(NN, SnapshotB, Samples [, ScoreMode, K]), the "Git Re-Basin" weight-space NEURON-PERMUTATION alignment diagnostic (Ainsworth, Hayase & Srinivasa 2022; Entezari et al. 2021) — the dual ofModeConnectivityReport. That report measures the linear-interpolation loss barrier between two independently-trained nets but does nothing about it; this one shows most of that barrier is an illusion of neuron-labelling: a hidden layer's units are interchangeable up to a permutation (permute the units and the next layer's matching input-weight columns and the represented function is unchanged). It walks the trainable layers front-to-back, for each hidden layer greedily solves the permutationP_Lof net B's units that best aligns them to net A's — by weight-row cosine (ScoreMode=0, default) or per-unit activation correlation overSamples(ScoreMode=1) — appliesP_Lto B's output neurons and compensates the next layer's input columns, then re-runsModeConnectivityReport's interpolation sweeptheta(alpha) = (1-alpha)*A + alpha*P(B)(the sameTNNetVolume.MulMulAddsnapshot arithmetic) and reports the loss barrier before vs after alignment as two scaled#-bar rows, the per-layer permutation churn (fraction of units moved), and abarrier collapsed/partially reduced/unchangedverdict. Three built-in PASS/FAIL checks: permutation invariance (permute+compensate leavesB.Computebit-for-bit unchanged — the foundational identity), align-to-self (SnapshotB := Agives identity permutations and a flat zero barrier), and monotonicity (post-alignment barrier<=pre-alignment barrier). The example trains the same tiny MLP twice from different inits (so a real barrier exists) and prints a weight-matching run, an activation-matching run, and the align-to-self check — the barrier visibly shrinks (~65–75% reduction) once the permutation symmetry is quotiented out. Distinct fromRepresentationSimilarityReport(compares activations for similarity, never produces a weight permutation or re-interpolates),NeuronCorrelationReport(intra-layer redundancy of one net) andWeightDriftReport(raw L2 drift, no symmetry quotient). Endpoint-A weights restored bit-for-bit; forward-only. - Intrinsic-dimension report — runs
TNNet.IntrinsicDimensionReport(NN, Probes), a forward-only representation-geometry diagnostic answering "how many effective dimensions does each layer's activation cloud actually occupy?" — the dimensionality of the data manifold the probe batch traces out at each depth. For every trainable layer it runs one forward pass over an unlabelled probe batch, flattens each sample to a row of anN x D_lmatrix, and reports two complementary intrinsic-dimension estimates side by side: (1) the linear / PCA ID via the participation ratio of the activation covariance eigenspectrumPR = (sum lambda)^2 / sum lambda^2(eigenvalues from the smallerN x NGram /D x Dcovariance via the same Double-precision cyclic Jacobi eigensolverWeightSpectralTailReportships), and (2) the TwoNN nonlinear estimator (Facco et al. 2017) read off the least-squares slope of-log(1 - F(mu))againstlog(mu), withmu = r2/r1the per-sample 2nd-to-1st nearest-neighbour distance ratio. It reports per layer both IDs, the linear-vs-nonlinear gap, aD_l-normalised compression ratioTwoNN_ID/D_l, an ID-across-depth ASCII bar chart, andexpanded/compressed/ near-full-rankflags. The example contrasts a fresh-init net (flat, near-input ID) against a trained net (the expand-then-contract "hunchback" of Ansuini et al. 2019) and includes a known-k-dim-subspace ground-truth recovery so the estimate is visibly correct in one run. Built-in checks: ak-dim linear subspace recoversPCA_ID ~ kandTwoNN_ID ~ k, identical samples drive both IDs to ~0, and PCA eigenvalues are non-negative. Distinct fromNeuronCorrelationReport(linear redundancy among feature axes) andFeatureSeparabilityReport(label-aware class geometry). Pure forward-only —NN.Computeonly, weights never touched. - Neural-tangent-kernel report — runs
TNNet.NeuralTangentKernelReport(NN, Samples [, TargetClass]), a forward-only diagnostic measuring the empirical Neural Tangent Kernel (Jacot, Gabriel & Hongler 2018) of a classifier over a small probe batch (Nkept to ~8–16; the kernel isO(N^2)entries each anO(P)-param dot product). On a frozen net (SetBatchUpdate(true),ClearDeltasbefore each sample, neverUpdateWeights) it runs one forward + one backward per probe, seeded one-hot atTargetClass(default-1= each sample's own predicted argmax), to snapshot the per-parameter weight-gradient vectorg_iof that scalar logit — reusing the sameNeurons[*].Delta/FBiasDeltagradient read-out (divided back out by the layer learning rate) thatFisherImportanceReport/GradientConflictReport/GradientNoiseScaleReportshare; theDelta = -LR*gradsign cancels in the Gram dot products. It forms the empirical NTK GramK_ij = <g_i, g_j>and reports the kernel as a glyph-shaded ASCII heatmap, its full eigenspectrum via the same self-contained Double-precision cyclic Jacobi eigensolverWeightSpectralTailReport/IntrinsicDimensionReportship (no new numerical code), the condition numberlambda_max/lambda_min(guarded whenlambda_min<=0), the kernel-target alignment<K, yy^T>_F/(||K||_F ||yy^T||_F)(Cristianini et al. 2001;y= centered target-class indicator) — the headline number, the effective rank / participation ratio(sum lambda)^2/sum lambda^2, and alog10(lambda)histogram. The example contrasts a fresh-init vs a trained tiny MLP on a synthetic 3-class blob task so the NTK reshaping (alignment / conditioning / effective rank) is visible in one run. Built-in checks: kernel symmetry residual~0and a strictly-positive diagonalK_ii = ||g_i||^2. A possible follow-up (not done here) is a fresh-init-vs-trained NTK-drift contrast. Distinct fromGradientConflictReport(pairwise gradient cosines, not the raw Gram + its spectrum + label alignment) andFisherImportanceReport(per-parameter curvature, not the sample-sample kernel). Forward + backward read only — weights never stepped. - Fisher-importance report — runs
TNNet.FisherImportanceReport(NN, Samples)on a labelled probe batch for a freshly-initialised classifier and the same architecture after a short training run, estimating the diagonal (empirical) Fisher informationF[theta] = E_x[(d log p(y|x)/d theta)^2]of every trainable parameter by accumulating per-sample squared parameter gradients (one forward + one backward per sample, gradient taken w.r.t. the true or predicted label) on a frozen net whose weights are never stepped. Reports per-layer total Fisher mass and its network share (the "which layers can I least afford to prune" ranking), per-layer mean/max per-parameter Fisher and a near-zero count, a 10-bin ASCIIlog10(Fisher)histogram, the effective-parameter-count = participation ratio(sum F)^2/sum F^2(a one-number concentration proxy), and per-layer high-importance / prunable / dead-layer flags. The README sketches how the same Fisher tensor feeds a downstream Elastic-Weight-Consolidation (EWC) two-task penalty. Forward+backward only; reuses the per-parameter gradient tensorsBackpropagatealready populates (no input-gradient enablement, unlike the saliency report).
Representation & interpretability
- Linear-probe report — runs
TNNet.LinearProbeReport(NN, Samples [, ValSamples])on a labelled probe batch for a freshly-initialised classifier and the same architecture after a short training run, answering "where does the model become a classifier?". For every intermediate layer it fits a closed-form ridge linear probeW = (X^T X + Lambda*I)^-1 X^T Yon that layer's flat activations (a self-contained Double-precision Gauss-Jordan solve — no SGD loop, no backward pass) and reports per-layer top-1 probe accuracy, a held-out probe accuracy (the train/val gap flags overfit probes), the one-hot regression MSE, the per-layer accuracy delta, a 10-bin ASCII bar chart of probe accuracy across depth, andCollapse /Saturation-point / near-Random flags. Over-wide layers are deterministically random-projected down toMaxFeatDim(default 256) to bound theO(D^3)solve. On the synthetic XOR + concentric-rings task (not linearly separable from the raw input) the untrained net's probe accuracy degrades with depth while the trained net's is preserved and climbs — the per-layer linear-separability reshaping made visible. Pure forward-only on the backbone, which is never modified. - Logit-lens report — runs
TNNet.LogitLensReport(NN, pInput [, HeadStartIdx])on an unlabelled probe batch for a freshly-initialised classifier and the same architecture after a short training run, the logit-lens diagnostic (nostalgebraist 2020; cf. "Tuned Lens", Belrose et al. 2023) answering "if we read out the prediction at THIS layer using the network's OWN trained output head, what would it already say?" — the model's running, self-decoded belief at each depth, fitting zero parameters. It identifies the trailing readout head (defaultHeadStartIdx= the last trainable layer plus its activation/softmax tail) and, for every earlier layer whose flat activation is shape-compatible with the head's expected input, splices that activation into the head's input slot and recomputes only the head layers to get a per-layer lens distributionp_L. Reports the per-layer argmax-agreement-with-final bar chart, the crystallization depth (shallowest layer after which the lens argmax matches final and never flips again — per-batch mean + 10-bin histogram), per-layer mean top-1 confidence and lens entropy (the readout sharpens with depth), and the per-layerKL(p_L || p_final)curve (monotone decrease = the residual stream incrementally refining toward the final answer); width-incompatible layers are listed explicitly as SKIPPED (the honest constraint of the classic lens). Built-in checks: applying the lens at the head input (no substitution) reproducesp_finalexactly (agreement 1.0, KL 0), and a single-layer head degenerates to the trivial "everything resolves at the last layer" profile. The constant-width body keeps every hidden layer lens-compatible so the depth profile is dense, and the input layer shows up as SKIPPED. Distinct fromLinearProbeReport(which fits a fresh ridge probe per layer — the lens fits nothing and reuses the model's own trained head),ActivationPatchingReport(causal cross-input swaps) andFeatureSeparabilityReport(cluster geometry, no readout). Pure forward-only —NN.Computeplus per-head-layer recompute, weights never stepped; the live state is restored on exit. - Tuned-lens report — runs
TNNet.TunedLensReport(NN, pInput [, HeadStartIdx, TrainIters, LearningRate])next toTNNet.LogitLensReporton the same unlabelled probe batch, the learned sibling of the logit lens (Belrose et al. 2023, "Eliciting Latent Predictions with the Tuned Lens"). Where the logit lens splices a raw hidden activation straight into the model's own frozen head, the tuned lens first passes that activation through a small per-layer learned affine translator (oneTNNetFullConnectLinearof the head-input width, plus bias) trained to map the layer's residual state into the final-layer basis before the frozen head decodes it — correcting the representation drift that biases the raw logit lens at early depths. The trunk and head are frozen; for each lens-compatible layer a private throw-away mini-netInput -> Translator(identity-seeded) -> clone of the frozen headis fit by minimising KL to the model's own final distribution (distillation-to-self, no labels — with a softmax head, backpropagatingp_finalas the soft target is exactly that KL gradient). Prints the tuned lens'KL-to-final, entropy and agreement side by side with the raw logit-lens columns, plus a pairedKL-to-finalcurve (logit.vs tuned#) and the aggregate meanKL-to-finalfor the logit lens, the untrained (identity) tuned lens and the trained tuned lens. The headline Belrose result is visible: the tuned curve sits lower at the early/middle layers (commits earlier, tracks the final answer more faithfully). Built-in PASS/FAIL checks: an untrained translator does no better than the raw lens (ties its KL-to-final — no free lunch before fitting), fitting lowers the mean KL-to-final, and at the head input the translator collapses to the identity sotuned == logit == final(max|dp| ~ 0). The model's own weights are never stepped (translators live in a private net) and the live state is restored on exit. Distinct fromLogitLensReport(zero fitted params) andLinearProbeReport(a fresh label-supervised ridge probe per layer — the tuned lens is label-free and reuses the model's own frozen head). Pure CPU, ~1 s. - Representation-similarity report — runs
TNNet.RepresentationSimilarityReport(NN, Probes [, OtherNet])on a deliberately over-deep ReLU MLP (16 -> 16 x6 -> 1) on a single-ReLU-feature target, answering "how does the representation reshape with depth, and which layers do redundant work?". It computes the linear Centered Kernel Alignment (CKA, Kornblith et al. 2019) similarity between every pair of layer activations over a shared probe batch: for each layer it flattens the per-sample activation to a row of anN x D_lmatrixX_l, column-centers it, and via theN x NGram trick computesCKA(i,j) = <K_i,K_j>_F / (||K_i||_F ||K_j||_F)— a number in[0,1], invariant to orthogonal rotation and isotropic scaling. Reports the fullLxLCKA matrix as a glyph-shaded ASCII heatmap, the adjacent-layerCKA(l,l+1)vector (high = near pass-through redundant layer, sharp dip = reorganization), the single most-redundant layer pair (a merge/prune candidate), the block structure (contiguous runs of mutually-similar layers — "representational stages"), and a one-line verdict. The self-CKA diagonal is1.0by construction (the built-in correctness check) and the matrix is symmetric. The example contrasts a fresh-init net (adjacent CKA already high — untrained layers barely transform their input) against the same net after training (a clearer block structure emerges yet the over-deep middle layers stay near-duplicates, so "depth is wasted" lights up), then runs cross-CKA between two independent nets of the same shape ("do these two nets learn the same intermediate features?"). Pure forward-only; weights are never touched, no backward pass is run. - Feature-separability report — runs
TNNet.FeatureSeparabilityReport(NN, Samples, NumClasses)on a labelled probe batch, a label-aware class-geometry / Neural-Collapse diagnostic (Papyan, Han & Donoho 2020) answering "how tightly does each layer cluster the samples of a class, and how far apart are the classes?" — the geometry of the feature space, not its decodability. For every trainable layer it runs one forward pass over the batch and computes the Fisher-style scatter decomposition: within-class scattertr(Sw) = mean_c mean_{i in c} ||x_i - mu_c||^2(NC1 cluster tightness), between-class scattertr(Sb) = mean_c ||mu_c - mu||^2, the Fisher ratiotr(Sb)/tr(Sw), the mean silhouette coefficient (fit-free cohesion-vs-separation in[-1,1]), and the class-mean pairwise-cosine matrix (numeric + glyph heatmap) with the simplex-ETF check (NC2): the mean off-diagonal cosine printed next to its collapse target-1/(NumClasses-1). It prints a per-layer Fisher-ratio-across-depth ASCII bar chart andCollapse / well-Separated / near-Random flags; over-wide layers are deterministically random-projected down toMaxFeatDim(default 256). The built-in correctness check is the scatter-decomposition identitytr(Stot) == tr(Sw) + tr(Sb)(exact on a class-balanced batch). The example contrasts well-separated vs deliberately-overlapped 2-D Gaussian blobs in one run, so the Fisher-ratio and silhouette contrast (and the off-diagonal cosines approaching the ETF target) is visible. Pure forward-only —NN.Computeonly, weights never touched, no backward pass. - Neural-collapse report — runs
TNNet.NeuralCollapseReport(NN, Samples, NumClasses [, FeatureLayerIdx, MaxFeatDim])on a class-balanced labelled probe batch, measuring the four canonical Neural-Collapse metrics (Papyan, Han & Donoho 2020, Prevalence of neural collapse during the terminal phase of deep learning training) on the penultimate-layer features (the activations feeding the final linear head;FeatureLayerIdx = -1auto-selects). WhereFeatureSeparabilityReportstops at thetr(Sw)collapse + Fisher magnitude (a partial NC1), this computes the full headline geometry. It reuses that report's class-mean / within-class scatterSw/ between-class scatterSbmachinery for NC1 = within-class variability collapsetr(Sw·Sb⁺)/C → 0(trace-ratio surrogate). NC2 = convergence to a simplex equiangular tight frame: the centered class means become equinorm (coefficient of variation of‖mu_c − mu‖ → 0) and equiangular (every pairwise cosine →−1/(C−1); it prints the mean cosine plus the mean and max deviation from that target, with a centered-mean cosine glyph heatmap). NC3 = self-duality, the cosine alignment between the centered class-mean matrix and the classifier weight rows — honestly skipped with a printed flag when the head is not a width-matchedTNNetFullConnectLinear/TNNetPointwiseConvLinear. NC4 = the classifier collapses to a nearest-class-mean rule: the fraction of probe points whose argmax logit equals their nearest centered class mean (→ 1). The example trains a small 4-class classifier well past zero train-error (the terminal phase) on synthetic Gaussian blobs and calls the report every N epochs on a fixed probe, printing an ASCII trajectory of the mean pairwise cosine marching onto the−1/(C−1)line — the simplex assembling itself. Pure forward-only —NN.Computeonly, weights read but never stepped; pure CPU, no download. - Information-plane trajectory — reproduces the Information Bottleneck information-plane story (Tishby & Zaslavsky 2015; Shwartz-Ziv & Tishby 2017, Opening the Black Box of Deep Neural Networks via Information) for a tiny fully-connected classifier on a synthetic binary task: it tracks the mutual-information pair
(I(X;T), I(T;Y))of each hidden layer across training epochs and prints every layer's path through the 2-D information plane as an ASCII scatter — no new layer, no new gradient machinery, only a forward pass (Net.Compute) over the full dataset at each logged epoch reading each layer'sOutput. MI uses the original binning estimator (each neuron's activation discretized intoBequal-width bins, the per-sample bin-tuple is a discrete code;I(X;T)=H(T)since the deterministic net with unique inputs hasH(T|X)=0, andI(T;Y)=H(T)-H(T|Y)from per-class code histograms, reported in bits). The narrative target is the two phases — a fast fitting phase (I(T;Y)rises to its 1-bit ceiling) followed by slow compression (I(X;T)drops whileI(T;Y)stays high). HONEST headline in the house "what did NOT reproduce" style: the binning estimator requires a saturating activation to show compression, so two arms ship — Arm A (tanh trunk,TNNetFullConnect, bounded[-1,1]) where the bend appears (deepest hidden layer loses ~2 bits ofI(X;T)over training while keepingI(T;Y)=1), and Arm B (ReLU trunk,TNNetFullConnectReLU, unbounded → per-epoch-rescaled bins are ill-defined) where the clean bend vanishes — demonstrating the Saxe et al. 2018 controversy rather than overclaiming. Pitfalls documented: MI is upper-bounded bylog2(#samples)andB^width, and absolute values are estimator-dependent — the robust signal is the shape of the trajectory and the tanh-vs-ReLU difference, not the nats. Pure CPU, deterministic, well under a minute. - Prediction-depth report — runs
TNNet.PredictionDepthReport(NN, Support, SupportLabels, Queries [, QueryLabels])on a labelled support batch and a query batch for a freshly-initialised classifier and the same architecture after a short training run, the per-example difficulty diagnostic of prediction depth (Baldock, Maennel & Neyshabur 2021, Deep Learning Through the Lens of Example Difficulty) answering "at how deep a layer does the network actually make up its mind about THIS example?" — a per-sample resolution depth, not a per-layer aggregate. It snapshots each trainable layer's flattened activation over the support and query batches, takes a k-NN vote (defaultK=5, cosine distance) over the support at every layer, and defines a query's prediction depth as the index of the shallowest layer after which the k-NN vote agrees with the network's final argmax and never disagrees again. Reports a 10-bin ASCII histogram of prediction depth + mean/median, the per-layer newly-resolved count (where examples get decided), theKdeepest (= hardest) query indices as a ready-made hard-example / relabel queue, and — withQueryLabels— a correctness cross-tab (mean depth of correctly vs incorrectly classified queries; the literature's headline result is depth correlates with error). Over-wide layers are deterministically random-projected down toMaxFeatDim(default 256) to bound the k-NN cost (the same projectionLinearProbeReportuses). On the synthetic 4-class 2D-blob task with an ambiguous between-blob query subset, fresh-init depths pile up at the last layer (right-skewed) while after training the mass shifts shallow for the well-separated clusters and the hard subset keeps a deep tail, and the incorrect-minus-correct mean-depth gap is positive. Built-in checks: feeding the support set as its own queries gives every sample a finite depth with final-layer agreement ~1.0 (a point is its own nearest neighbour at distance 0), and a one-class support set drives every depth to 0. Distinct fromLinearProbeReport(parametric per-layer accuracy via a ridge solve — this needs only distances),FeatureSeparabilityReport(per-layer aggregate cluster geometry),TopLogitMarginReport(last-layer confidence) andMCDropoutUncertaintyReport(stochastic uncertainty). Pure forward-only —NN.Computeonly, weights never touched, no backward pass. - Equivariance report — runs
TNNet.EquivarianceReport(NN, Probes)on two synthetic 8x8x3 image classifiers: a plain conv net (flip-sensitive) and a global-average net (Input -> TNNetAvgChannel -> FC) that is FlipX/FlipY-invariant by construction. For a fixed menu of input-side symmetry transforms (TNNetFlipX,TNNetFlipY,TNNetReverseChannels, a 1-channelTNNetRoll) it reports the per-transform invariance errormean_x ||f(T(x)) - f(x)||_2 / ||f(x)||_2, the top-1 argmax agreement rate, a 10-bin ASCII histogram of per-sample error, and a verdict (invariant/approximately invariant/sensitive). The plain net readssensitiveon the flips while the global-average net reads ~0 (invariant) — the built-in correctness check. Pure forward-only; eachT(x)is produced by a tinyInput -> Transformwrapper net and the inspected weights are never touched. - Saliency report — runs
TNNet.SaliencyReport(NN, Probe)on a trained synthetic 8x8x2 two-class image classifier and prints, for the predicted classc, three input-attribution heatmaps side by side: vanilla input-gradient saliency|d logit_c/dx|(one forward + one backward pass with a one-hot output error), SmoothGrad (the vanilla map averaged over N noisy copies of the input), and Integrated Gradients (the path integral from a zero baseline tox). Per channel it reports total attribution mass, the top-K most-attributing pixels, and the IG completeness gap|sum(IG) - (logit_c(x) - logit_c(0))|as a built-in correctness check (small relative gap == faithful integration). The discriminative blob lights up in all three maps. Forward+backward only; the trained weights are left untouched. - LRP report — runs
TNNet.LRPReport(NN, Probe)(Layer-wise Relevance Propagation; Bach et al. 2015) on a trained synthetic 6x6x1 two-class dense classifier (FullConnectReLU -> FullConnectReLU -> FullConnectLinear -> SoftMax). Unlike the gradient-basedSaliencyReport/GradCAMReport, LRP is a conservation method: it seeds the explained class's relevance withR_c = logit_cand back-distributes it toward the input via the epsilon-ruleR_i = sum_j (a_i w_ij)/(sum_k a_k w_kj + eps*sign(z_j)) R_j, redistributing (not differentiating) relevance. It prints the headline per-layer conservation residual|sum(R_in) - sum(R_out)|(which isO(eps)and -> 0 aseps -> 0), the top-K most-relevant input positions(channel,x,y)with signed relevance, and a per-channel ASCII relevance heatmap (the top-relevant cells cluster on the class-specific blob). A dense stack is used on purpose so the epsilon-rule has an exact closed form; theSoftMaxhead and any attention/normalisation layer are skipped honestly (listed asSKIPPED/passthru, never faked). Forward-only; the trained weights are restored bit-for-bit before returning. - Activation-patching report — runs
TNNet.ActivationPatchingReport(NN, CleanInput, CorruptInput [, TargetIdx]), a forward-only causal mechanistic-interpretability diagnostic (activation patching / causal tracing; Meng et al. ROME 2022, Wang et al. IOI 2022) answering "which layer's activations CARRY the information that decides this prediction?" — by intervention, not correlation. Given a(CleanInput, CorruptInput)pair the trained net maps to different argmax classes, it caches every layer's cleanOutput, runs a corrupt forward, then for each layerLrestores only that layer's cached clean activation into the corrupt run (CopyNoChecks) and recomputes layersL+1..last, reading off the recoveryr_L = (logit_c(patch_L) - logit_c(corrupt))/(logit_c(clean) - logit_c(corrupt))(c= clean argmax class, the defaultTargetIdx). It prints a per-layerr_LASCII bar chart across depth (the causal-trace curve), a per-layer un-normalised delta-logit column (so a near-zero normalisation denominator is visible), the argmax-flip layer, the peak-recovery layer, and an early/late/distributed localisation verdict. The example builds a branched net (a main ReLU branch + a raw-input skip fused by aConcat) on a synthetic XOR-of-signs task so the trace is graded by construction: patching a single main-branch layer recovers little (the corrupt input still flows on the skip) and recovery jumps to~1at the fusion layer — the ground-truth localisation. Built-in checks:r_0 == 1exactly (patching the input reconstructs the full clean run),r_last == 1exactly (the last layer'sOutputIS the logits), andCorruptInput == CleanInputcollapses the denominator so the report WARNS rather than dividing by zero. Distinct fromSaliencyReport(input-space gradient attribution) andLayerSensitivityReport(random weight jitter — never swaps real activations between two inputs). Forward-only — the only mutation is the transient activation overwrite, reverted by a final clean recompute; weights are never touched. - Activation steering (concept vectors) — the interventional flip-side of the read-only probes (ActAdd / representation engineering, Turner et al. 2023): rather than asking "what is decodable from this layer?", it INJECTS a concept direction into a hidden activation mid-forward and shows it causally controls the output. It trains a small softmax classifier on a synthetic two-cluster task, computes a steering vector
v = mean(act_k | class 1) - mean(act_k | class 0)(a diff-of-class-means direction at hidden layerk, no extra training), then for a sweep ofalpha in {-3..+3}runs forward up tok, doesOutput_k.MulAdd(alpha, v)and recomputes layersk+1..last(reusing theActivationPatchingReportrecompute machinery), charting target-class probability vsalphaas an ASCII curve. Built-in checks:alpha = 0reproduces the unsteered forward pass bit-for-bit, the target-class probability moves monotonically withalpha, and steering withvshifts the output far more per unit norm than an equal-norm RANDOM direction (the concept direction is special). Distinct fromActivationPatchingReport(swaps WHOLE cached activations between two inputs),SaliencyReport(input-space gradient),GradientAscent(ascends on the input image) andLinearProbeReport(only READS what a layer encodes — none ADD a direction to a hidden activation and measure the controllable output shift). Pure CPU, forward-only, weights never stepped after the classifier is trained. The sibling activation-steering depth sweep repeats the alpha sweep at EVERY hidden layerkand reports which depth gives the cleanest monotoneP(target)-vs-alphacontrol and the smallest alpha-to-flip — the "where does a concept vector bite hardest?" question (on the toy task k=1/3/4 are perfectly monotone while k=2's diff-of-means direction is genuinely non-monotone). - Concept bottleneck model (test-time intervention) — an interpretable-by-design Concept Bottleneck Model (Koh et al. 2020) built from existing layers only: a trunk feeds a
TNNetFullConnectSigmoid(K)layer ofKhuman-meaningful concepts which is the sole input to aTNNetFullConnectLinearlabel head, trained jointly with deep supervision (label cross-entropy + a concept-prediction loss on the bottleneck, the same packed-target /SetBatchUpdate(True)idiom asEarlyExitNetwork). The headline payoff is test-time concept intervention: overwrite the predicted concept vector at the bottleneck and recompute only the downstream label head (theCopyNoChecks-then-recompute machinery ofActivationSteering/ActivationPatching) — injecting the true concepts lifts label accuracy to the head's ceiling, and flipping a single concept bit deterministically flips the predicted class in the direction that concept controls. Built-in invariants: a no-op overwrite with the model's own predicted concepts reproduces the logits bit-for-bit, and setting the concept-loss weightlambda = 0makes the bottleneck drift (mean concept accuracy collapses toward chance — the "leaky" CBM failure mode). Distinct fromLinearProbeReport(post-hoc frozen probe, READS only),ActivationSteering(edits anonymous activations, no concept supervision), andDomainAdversarial(gradient reversal REMOVES info). Pure CPU, deterministic, ~10 s. - Sparse autoencoder — a self-contained pure-CPU reproduction of the headline result of Anthropic's Towards Monosemanticity (Bricken et al. 2023): a sparse autoencoder with an OVERCOMPLETE dictionary, trained on dense POLYSEMANTIC activations, recovers the original sparse ground-truth features as MONOSEMANTIC dictionary atoms — and there is a sweet-spot sparsity weight (too little gives dense polysemantic atoms, too much kills atoms). Complements the Superposition example.
- VQ codebook-usage probe — the demonstration companion to the new
TNNetVectorQuantizercodebook-usage probe (ResetCodebookUsage/ActiveCodeCount/CodebookUsageCount), which exposes the headline VQ-VAE failure mode: CODEBOOK COLLAPSE, where only a handful of codes ever win the nearest-neighbour argmin and the rest of the codebook is dead weight. A tiny VQ-VAE-style bottleneck (encoder → quantizer → decoder, MSE reconstruction) trains on synthetic well-separated Gaussian blobs with MORE codes than clusters (K=12vs 5 clusters) so unused codes are visible; each epoch it probes the codebook over a fixed batch and prints active-code count + a per-code usage histogram, ending with a graded PASS/FAIL verdict (PASS = stays healthy, using ≥ #clusters distinct codes;Halt(1)on collapse). The probe is pure runtime bookkeeping — it does NOT touch the quantization math, any gradient, or serialization. Pure CPU, <1 s. - VQ codebook collapse — stress test + dead-code re-init — the failure-mode counterpart to VQCodebookUsage: it deliberately DRIVES codebook collapse on
TNNetVectorQuantizerand then shows a published mitigation lifting it back out. A 64-code codebook on 16 Gaussian blobs runs two arms sharing data + seed; the COLLAPSE arm contracts the encoder weights each epoch (×0.70) to shrink the latent cloud, and the active-code count (read viaResetCodebookUsage→ probe pass →ActiveCodeCount) falls 23 → 5 of 64. The MITIGATED arm adds DEAD-CODE RE-INITIALIZATION every 3 epochs — codes with zeroCodebookUsageCountare re-seeded to live encoder latents through the publicNeurons[code].Weightsaccessor — recovering to ~16 active (the true mode count), ending with a gradedVERDICT: PASS. Example-only (core untouched). Gotcha documented in its README: build the encoder asCreate(1,1,cEmb)so latents live on the Depth axis (Create(cEmb)gives Depth=1 scalar codes). Pure CPU, ~1.5 s. - VQ-VAE discrete-image generation — the GENERATIVE payoff of
TNNetVectorQuantizer, the two diagnostic probes (VQCodebookUsage / VQCodebookCollapse) only inspect codebook health on synthetic blobs; here the same vector-quantization bottleneck is wired into a real convolutional encoder/decoder that reconstructs 28×28 MNIST digits and then an autoregressive prior synthesises brand-new ones (van den Oord et al. 2017, Neural Discrete Representation Learning). Stage 1 (representation): encoder →TNNetVectorQuantizer→ decoder trained on reconstruction MSE (gradient flows through the non-differentiable argmin via the layer's built-in straight-through estimator + commitment/codebook terms), writing a reconstruction grid PNG and reporting codebook usage. Stage 2 (prior + generation): FREEZE the encoder, harvest each digit's 7×7 grid of discrete code indices via the new publicLVQ.ChosenCodeIndex(X, Y)accessor (the argmin token cached on the lastCompute()— this is what turns the continuous latent into a 49-token discrete sequence), fit a tiny causal transformer LM (reusingAddTransformerEncoderBlockwithCausalMask=true, exactly like TinyGPT) over those sequences, then AUTOREGRESSIVELY sample new 49-token grids, look each token up in the codebook, and DECODE through the frozen decoder to write a generated-samples PNG. SMOKE mode finishes in ~2 min on one CPU (reconstruction MSE falls, codebook stays healthy, no NaN);--fullfor sharper digits. Mirrors the classic two-stage VQ-VAE recipe (discrete autoencoder first, autoregressive prior over its codes second). Needs the standard MNIST idx-ubyte files in the working directory. Pure CPU. - FSQ-VAE — codebook-free Finite Scalar Quantization on MNIST — the collapse-free counterpart to the learned-codebook VQVAE / VQCodebookCollapse, built on the new layer
TNNetFiniteScalarQuant(Mentzer et al. 2023, Finite Scalar Quantization: VQ-VAE Made Simple; a port of the lucidrainsvector-quantize-pytorchFSQ). FSQ has NO learned codebook, NO EMA and NO commitment loss: each latent channel is squashed by a boundedtanhand ROUNDED to one ofL_iinteger levels (f(z)=tanh(z+shift)·half_l−offset,zhat=round(f(z)), gradient through the round via the layer's built-in straight-through estimator), so the implicit codebook (here5⁶ = 15625codes) cannot collapse by construction. A tiny MLP autoencoder784→…→6 FSQ channels→…→784is trained on a small MNIST subset with hand-rolled mini-batch SGD; a per-channelTNNetChannelStdNormalization+ fixed gain keeps the latents in the informative tanh region (the documented FSQ training pitfall — latents that collapse to ~0 or saturate both stick on one level). After training it probes the bottleneck over a fresh batch and reports per-channel level utilization (climbs to ~100% — every level of every channel exercised, the collapse-free guarantee in action) and the number of DISTINCT full codes seen via the publicLFSQ.CodeIndex(X,Y)accessor (the discrete token a downstream transformer prior would consume), ending with a gradedVERDICT: PASS. SMOKE finishes in ~33 s on one CPU;--fullfor a longer run. Needs the standard MNIST idx-ubyte files in the working directory (e.g. run from../VQVAE). Pure CPU. - LFQ-VAE — Lookup-Free (binary) Quantization demo — the BINARY sibling of FSQVAE, built on the new layer
TNNetLookupFreeQuant(Yu et al. 2023, MagViT-v2, Language Model Beats Diffusion: Tokenizer is Key to Visual Generation; a port of the lucidrainsvector-quantize-pytorchLFQ). LFQ takes FSQ's per-channel quantization to the limitL_i = 2: each latent channel is justsign(z)in{-1,+1}, so the implicit codebook is the product set{-1,+1}^Dof size2^Dwith NO codebook lookup at all (no learned vectors, no argmin). The discrete token at a position is the bit-packed sign pattern read via the publicLFQ.CodeIndex(X,Y); gradients flow through the non-differentiablesignvia the straight-through estimator clipped to the|z| <= 1band (the lucidrains LFQ math, reproduced exactly). The headline addition is LFQ's entropy auxiliary loss in its tractable factorized binary form — per channelsoftmax(-t·[(z+1)², (z-1)²]), thenEntropyAuxLoss = PerSampleEntropy − DiversityWeight·CodebookEntropy— surfaced as PUBLIC accessors (PerSampleEntropy/CodebookEntropy/EntropyAuxLoss) on the layer; the demo READS them to show they track code usage (the layer injects NO entropy gradient, so a real tokenizer ADDS this scalar to its reconstruction loss to keep the codebook diverse — exactly likeTNNetLoadBalanceLossis added to a MoE loss). A tiny self-contained MLP autoencoder (16→…→6 LFQ channels→…→16) trains on synthetic prototype data with hand-rolled mini-batch SGD and reports, before/after training, the distinct binary-code count + the entropy terms, ending with a gradedVERDICT: PASS(the binary bottleneck cleanly separates the data prototypes into distinct codes). NO external data; finishes in ~2 s on one CPU. Pure CPU. - VAE continuous-latent generation — the continuous / Gaussian counterpart to the discrete VQVAE: a small Variational Autoencoder on MNIST (Kingma & Welling 2014, Auto-Encoding Variational Bayes) built around the new layer
TNNetGaussianReparameterize. The encoder emits a packed(mu | log_var)tensor on the depth axis; the new layer splits it and draws the latent with the reparameterization trickz = mu + exp(0.5·log_var)·eps,eps~N(0,1)— sampled once per forward and frozen for the matching backward (the fixed-noise discipline ofTNNetGumbelSoftmax/TNNetDropout), so the draw stays differentiable w.r.t.muandlog_var. A conv decoder reconstructs the digit and training minimises reconstruction-MSE +beta·KL(q(z|x)‖N(0,1)). KL composition: the reparameterize layer contributes ONLY the reconstruction gradient (dz/dmu=1,dz/dlog_var=0.5·sigma·eps); the KL term is a SEPARATE penalty (dKL/dmu=mu,dKL/dlog_var=0.5·(exp(log_var)−1), also packaged as theTNNetVAEKLDivergenceloss head), and the example wires the two explicitly so they SUM at the(mu|log_var)fork — a reconstruction backward through the reparameterize layer followed by a KL-only backward through the encoder. Generation: samplez~N(0,1)from the prior the KL term trained toward, inject it at the latent layer and run the decoder tail, writing an 8×8 grid of fresh digits. SMOKE mode finishes in ~1–2 min on one CPU (reconstruction MSE ~0.21→~0.02, KL stays finite, no NaN);--fullfor sharper digits. Needs the standard MNIST idx-ubyte files in the working directory. Pure CPU. - MaskGIT non-autoregressive image generation — the masked-token PARALLEL generator over the same discrete VQ codebook as VQVAE (Chang et al. 2022, MaskGIT: Masked Generative Image Transformer). Structurally distinct from every landed generator — autoregressive (TinyGPT, the VQVAE prior), GAN (VisualGAN, StyleGAN2Generate) and diffusion (DiffusionMNIST, ConsistencyDistill). Stage 1 reuses the VQVAE discrete autoencoder verbatim (conv encoder →
TNNetVectorQuantizer→ conv decoder over MNIST; reconstruction-MSE training, dead-code revival, codebook-usage reporting, reconstruction PNG) so each 28×28 digit becomes a 7×7 grid of 49 discrete code indices viaLVQ.ChosenCodeIndex(X,Y). Stage 2 is the new part: a BIDIRECTIONAL transformer (AddTransformerEncoderBlockwithCausalMask=FALSE— the exact opposite of the VQVAE/TinyGPT causal prior, plus a per-positionTNNetPointwiseSoftMaxhead) is trained with a masked-token objective — a cosine-family fraction of the 49 tokens is replaced by a dedicated[MASK]symbol (id =codebook_size, an extra vocab slot) and the model predicts the ORIGINAL ids at the masked positions only (cross-entropy on masked positions; the report shows masked-token prediction accuracy rising epoch over epoch). Generation is PARALLEL ITERATIVE DECODING: start the whole grid all-[MASK], then for ~10 steps predict every position at once, sample tokens with a per-position confidence (the sampled token's probability), keep the most-confident fraction per a cosine mask schedulegamma(t/T)=cos(pi/2·t/T)and re-mask the rest, until the grid is full — ~10 forward passes for the whole image vs the prior's 49 autoregressive steps — then look each final token up in the codebook and DECODE through the frozen VQ decoder. The only new code is the[MASK]masking + the confidence-based cosine unmasking SCHEDULER (the generation loop); the transformer, VQ encode/decode and PNG I/O are all reused — NO newTNNetlayer. Self-reports masked-token accuracy, generated-grid codebook usage, and a NaN/Inf check. SMOKE mode finishes in ~3.5 min on one CPU; it is intentionally undertrained (like the DiffusionMNIST smoke its digits are rough — the headline is that the parallel-decode pipeline fills a coherent grid in ~10 steps),--fullfor sharper output. Reuses the MNIST idx files (working dir, then../VQVAE/../DiffusionMNIST, no copy); falls back to a SYNTHETIC bar dataset if absent so it still runs offline in CI. Pure CPU. - VQGAN / discrete image-tokenizer import — the demo for
BuildVqModelFromSafeTensors(neuralpretrained.pas), the IMPORTER for a pretrained diffusers VQModel (VQGAN), the discrete tokenizer used by autoregressive / masked image generators (MaskGIT, Parti, LlamaGen) to turn an image into a grid of codebook token IDs and back. Where VQVAE trains a VQ-VAE from scratch, this imports one: it reuses the landed VAE encoder/decoder ResNet+attention blocks (the AutoencoderKLBuildVaeEncoder/BuildVaeDecodersiblings) but wires the discrete quantizer between them — encoder →quant_conv→ nearest-neighbour codebook lookup (image → token IDs via argmin squared-L2 toquantize.embedding.weight) and the inverse (token IDs → gathered embeddings →post_quant_conv→ decoder → image), with NO 0.18215 latent scaling (that is AutoencoderKL-only). The new code is plain-Pascal argmin/gather on the holder classTNNetVqModel.EncodeImageToTokens/DecodeTokensToImage(no newTNNet*layer), so an imported autoregressive image LM (stock Llama/GPT path) could model these token grids end to end. The demo round-trips a tiny image through the codebook (image → 8×8 token grid → image) and reports the reconstruction error; with no arguments it loads the committed pico fixture (tests/fixtures/tiny_vqmodel.*), the same one the parity tests use — encode token IDs match a numpy float64 oracle exactly (integers,TestVqModelEncodeParity) and decoded pixels match< 1e-4(TestVqModelDecodeParity/TestVqModelRoundTrip). Point it at a real diffusers VQModel checkpoint +config.jsonfor the full thing. Pure CPU, <1 s on the fixture. - StyleGAN2 generator synthesis import — the demo for
BuildStyleGAN2Generator(neuralpretrained.pas), a style-based generative import (Karras et al. 2020, Analyzing and Improving the Image Quality of StyleGAN). The new primitive is the leaf layerTNNetModulatedConv2D: a WEIGHT-SPACE modulated/demodulated convolution whose owned base kernel is scaled per-forward by a per-input-channel style vector read from a SECOND input —w'_{ijk}=s_i·w_{ijk}(modulate) thenw''_{ijk}=w'_{ijk}/sqrt(Σ w'^2+ε)(demodulate) — with the backward routing gradient to the kernel (through the demod normalization), the feature input AND the style branch (numerical-gradient-checked input+weight+style inTestNeuralNumerical). The importer builds the full INFERENCE-ONLY synthesis path: an 8-layer-style mapping MLP (z→w,TNNetFullConnectLinear+TNNetLeakyReLU), then a tower of resolution blocks each = nearest-x2 upsample (TNNetDeMaxPool) → [modulated conv + per-pixel noise injection (a learnedTNNetReZerostrength) + LeakyReLU] → a toRGB modulated 1×1 conv (no demod) summed into an upsampled RGB skip tower; each modulated conv's style comes from a small affine "A" (TNNetFullConnectLinear) off w. NO discriminator / path-length reg / training (v1). The official weights are not obtainable offline, so — exactly like the RRDBNet/VAE-decoder pico fixtures — it falls back to the committed config-faithful random pico generator (tests/fixtures/tiny_stylegan2.*, built bytools/make_pico_stylegan2_fixture.py), parity-checked< 1e-4against a hand-written float64 numpy oracle of the exact modulated-conv+demod+noise+toRGB math (TestStyleGAN2GeneratorParity). The demo synthesizes one image from a fixed latent and writes a P6 color PPM + an ASCII intensity preview; point it at a real.safetensors(+config.json) for your own checkpoint. Pure CPU, <1 s on the fixture. - Real-ESRGAN / ESRGAN RRDBNet super-resolution upscale — the END-TO-END demo for
BuildRRDBNetFromSafeTensors(neuralpretrained.pas), the repo's pure-convolutional, NO-diffusion super-resolution importer (Wang et al. 2021, Real-ESRGAN;xinntao/Real-ESRGAN). RRDBNet is a Residual-in-Residual Dense Block network:conv_first→num_blockRRDBs (each = 3 chained ResidualDenseBlocks of 5 dense channel-concat 3×3 convs withTNNetDeepConcatskips +LeakyReLU(0.2), scaled residualx + 0.2·block(x)) →conv_bodyglobal residual → an upsample tail of stages (TNNetDeMaxPool(2)for theF.interpolate(mode='nearest')2× spacing) →conv_hr+conv_last. Both upscaling factors are wired:scale=4= two upsample stages (conv_up1,conv_up2,RealESRGAN_x4plus),scale=2= one stage (conv_up1only,RealESRGAN_x2plus). Real-ESRGAN ships its weights as a.pthwhose state_dict is nested under a top-levelparams_emakey — loaded transparently through the sameBuildRRDBNetFromSafeTensorscall (CreatePretrainedTensorReaderdispatches.pth/.pt/.bintoTNNetTorchBinReader, which unwraps theparams_ema/params/state_dict/modelwrapper dict automatically). The example builds the committed pico RRDBNet (tests/fixtures/tiny_rrdbnet{,_x2}.*, built bytools/rrdbnet_tiny_fixture.py), synthesises a tiny 6×6 RGB gradient, writes it as a PNG (neuraldatasets.SaveImageFromVolumeIntoFile), reads it back (LoadImageFromFileIntoVolume), normalises 0..255→[−1,1], runs the forward upscale and writes the output PNG for both scales (6×6→24×24 at x4, 6×6→12×12 at x2). Parity is asserted< 1e-4against a hand-written float64 numpy oracle for the safetensors x4 path, theparams_ema.pthx4 path and the x2 path (TestRRDBNetParity{,Pth,Scale2}). Point it at a realRealESRGAN_x4plus.pth/.safetensors(+config.json) to upscale your own images. Pure CPU, <1 s on the fixture (random pico weights → import + forward + PNG-I/O smoke, not a photoreal upscaler). - NAFNet image restoration import — the demo for
BuildNAFNetFromSafeTensors(neuralpretrained.pas), a non-diffusion image-to-image RESTORATION import that is NOT super-resolution (the RRDBNet/ESRGAN path is x4 upscaling only); NAFNet restores (denoise / deblur / dejpeg) at the same resolution (Chen et al. 2022, Simple Baselines for Image Restoration). The new primitive is the parameter-free leaf layerTNNetSimpleGate: split the channel axis in half and multiply the halves (out[c]=in[c]·in[C+c]) — a GLU with no activation, backward = product rule (numerical-gradient-checked inTestNeuralNumerical). Simplified Channel Attention (SCA) reuses landed pieces — global avg pool (TNNetAvgChannel) → 1×1 conv → channelwise multiply (TNNetChannelMulByLayer), no activation. The importer assembles a symmetric U-Net of NAFBlocks: each NAFBlock = per-pixel LayerNorm2d (TNNetTokenLayerNorm) → 1×1 conv → 3×3 depthwise conv → SimpleGate → SCA → 1×1 conv, residual-added with a learnable per-channelbeta, then a second LayerNorm → 1×1 conv → SimpleGate → 1×1 conv, residual-added withgamma; the U-Net downsamples with a stride-2 conv (2× channels) and upsamples with a 1×1 conv + PixelShuffle (TNNetDepthToSpace), adding the encoder skip, and ends with a 3×3 conv + global input skip. The official checkpoints are large / not obtainable offline, so — exactly like the RRDBNet/VAE-decoder pico fixtures — it falls back to the committed config-faithful random pico NAFNet (tests/fixtures/tiny_nafnet.*, built bytools/nafnet_tiny_fixture.py), parity-checked< 1e-4against a hand-written float64 numpy oracle of the exact NAFBlock math (TestNAFNetParity). The demo adds synthetic Gaussian noise to a deterministic test image, runs the restoration forward pass, writes before/after P6 PPMs + an ASCII preview and reports the round-trip RMSE; point it at a real.safetensors(+config.json) for your own trained checkpoint. Pure CPU, <1 s on the fixture (random pico weights → wiring/throughput smoke, not a trained denoiser). - SwinIR transformer super-resolution import — the demo for
BuildSwinIRFromSafeTensors(neuralpretrained.pas), a transformer image-restoration import (classical super-resolution), architecturally distinct from the CNN-only RRDBNet/ESRGAN SR path and the SimpleGate-CNN NAFNet denoiser (Liang et al. 2021, SwinIR: Image Restoration Using Swin Transformer). The window / shifted-window attention is pure REUSE of the landed Swin building blocks — per (head, window)TNNetWindowAttention(relative-position bias + cyclic-shift attention mask) withTNNetGatherTokenswindow partition / reverse. The new pieces are only: a shallow conv stem (3×3 → embed_dim), the Residual Swin Transformer Blocks (RSTB =depthSwin layers — each a token-sequence W-MSA/SW-MSA + GELU MLP over the H×W grid — then a 3×3 conv, with a residual over the whole block), a final token LayerNorm + conv_after_body with a deep-feature residual onto the stem, and a pixel-shuffle upsample tail (3×3 conv →TNNetDepthToSpace(upscale)→conv_last, with aTNNetLeakyReLU(0.2)). Keys follow the official SwinIR repo state_dict (single packedattn.qkvsliced into q/k/v at load,layers.L.residual_group.blocks.M.*). The official checkpoints are large / not obtainable offline, so — exactly like the RRDBNet/NAFNet pico fixtures — it falls back to the committed config-faithful random pico SwinIR (tests/fixtures/tiny_swinir.*, built bytools/swinir_tiny_fixture.py), parity-checked< 1e-4against a hand-written float64 numpy oracle of the exact RSTB + window-attention + pixel-shuffle math (TestSwinIRParity; the 2-layer RSTB exercises both W-MSA and SW-MSA). The demo runs the 2× SR forward pass on a deterministic synthetic image, writes input/upscaled P6 PPMs + an ASCII preview and reports the upscaled shape; point it at a real.safetensors(+config.json) for your own trained checkpoint. Pure CPU, <1 s on the fixture (random pico weights → wiring/throughput smoke, not a trained SR model). - RIFE video frame interpolation import — the demo for
BuildRIFEFromSafeTensors(neuralpretrained.pas), a VIDEO-generative import: it synthesises an unseen intermediate frame between two input frames (t=0.5). This fills the gap left by the RAFT optical-flow import (which estimates flow but does NOT synthesise frames) and by the from-scratch toy FrameInterpolation (a trained-hereTNNetFlowWarpmodel, not an importer). RIFE (Huang et al. 2022, Real-Time Intermediate Flow Estimation for Video Frame Interpolation;hzwer/Practical-RIFE) estimates a bidirectional intermediate flow with a coarse-to-fine stack of IFBlocks, then backward-warps both frames and blends them with a learned soft fusion mask. The new primitive is the differentiable backward-warp leaf/two-source layerTNNetBackwardWarp:out(x,y,c) = image(x+dx(x,y), y+dy(x,y), c), bilinear, flow in PIXEL units, border-clamp padding — the integer-pixel equivalent of RIFE'sF.grid_sample(mode='bilinear', padding_mode='border', align_corners=True)(a sibling ofTNNetFlowWarp; full forward + backward, bothdL/d(image)anddL/d(flow)numerically gradient-checked inTestNeuralNumerical). Everything else reuses landed layers: the IFBlock is (4 flow channels + 1 mask), each block adds a flow RESIDUAL (the last block's mask wins); the accumulated 4-ch flow splits into(dx,dy)for each frame (TNNetSplitChannels), each frame is warped, and the soft blendmerged = warped0·m + warped1·(1−m)withm = sigmoid(mask)broadcast over RGB (TNNetSigmoid+TNNetDeepConcat.Replicate+TNNetCellMulByCell+TNNetSum). The input is the depth-concat[frame0 | frame1](2·in_channelchannels); the output is thein_channelinterpolated middle frame. The official Practical-RIFE checkpoints are not obtainable offline, so — exactly like the NAFNet/SwinIR pico fixtures — it falls back to the committed config-faithful random pico RIFE (tests/fixtures/tiny_rife.*, built bytools/rife_tiny_fixture.py), parity-checked< 1e-4against a hand-written float64 numpy oracle of the exact IFBlock + backward-warp math (TestRIFEParity). The demo makes two synthetic frames (a bright blob translating across the grid), stacks them, runs the interpolation forward pass, writesrife_frame0/middle/frame1.ppm+ an ASCIIframe0 | middle | frame1preview; point it at a real.safetensors(+config.json) for your own trained checkpoint. Scope v1: one intermediate frame att=0.5, inference-only, a small IFNet ofnum_blocksfull-resolution IFBlocks; real multi-scale checkpoint parity, arbitrary-t, and the recursive 2×→4× schedule are tracked follow-ups. Pure CPU, <1 s on the fixture (random pico weights → wiring/throughput smoke, not a trained interpolator). - CLIPSeg text-prompted zero-shot segmentation import — the demo for
BuildCLIPSegFromSafeTensors(neuralpretrained.pas), a "free-text prompt → dense single-channel mask" import: given an image and an arbitrary text prompt it emits one H×W logit map for "whatever the prompt names", with NO fixed label set (unlike the box/mask importers DETR/SAM/Mask2Former, which are class- or geometry-driven). CLIPSeg (Lüddecke & Ecker 2022, Image Segmentation Using Text and Image Prompts;CIDAS/clipseg-rd64-refined) is heavy REUSE: the frozen CLIP ViT image tower (BuildClipVisionTower-style pre-LN blocks) and the frozen CLIP text tower (theBuildClipFromSafeTensorstext half) are the same landed CLIP encoder blocks. The new piece is the lightweight FiLM-conditioned transformer decoder: the vision tower exposes the hidden states after each tapped encoder block (config.extract_layers, default[3,6,9]); each(tokens, vision_hidden)tap is projected toreduce_dim(reduces[i]), and at theconditional_layeris FiLM-modulated by the CLIP text embedding of the prompt —film_mul(cond)·x + film_add(cond)viaTNNetFiLM(γ|β broadcast over the token axis) — then refined by a post-norm CLIP-style block (relu MLP, LayerNorm after each residual). The CLS token is dropped, the patch tokens reshape to a(grid, grid, reduce_dim)map, and a single non-overlappingConvTranspose2d(reduce_dim, 1, patch, stride=patch)upsamples to the image-resolution logit mask — realized asTNNetPointwiseConvLinear(patch²)+TNNetDepthToSpace(patch)(the channelpatch_w·patch + patch_hcarries the spatial offset). The importer returns three nets (vision, text, decoder); the conditional embedding is read off the text tower's eot row (ClipTextEosPosition, not L2-normalized) and the tapped vision hidden states are fed to the decoder's per-tap inputs (theT5EncoderStatesInput"fill an Input before Compute" idiom generalized to several inputs), driven end-to-end byRunCLIPSeg. The real checkpoint is large / not obtainable offline, so — like the NAFNet/SwinIR pico fixtures — it falls back to the committed config-faithful random pico CLIPSeg (tests/fixtures/tiny_clipseg.*, built bytools/clipseg_tiny_fixture.pyfrom the real HFCLIPSegForImageSegmentationfloat64 oracle), parity-checked< 1e-4(TestCLIPSegParity: both the conditional embedding and the full mask logits). The demo runs the image+prompt pipeline on a deterministic synthetic image (prompt given as token ids — the pico fixture has no tokenizer), thresholds the logits at0and writes a binary-mask PPM + an ASCII preview; point it at a real.safetensors(+config.json) and real tokenizer ids for your own checkpoint. Scope v1: a single text prompt → one mask, inference-only (use_complex_transposed_convolution=false); image-prompt conditioning and the 3-stage complex upsample are deferred. Pure CPU, <1 s on the fixture (random pico weights → wiring/throughput smoke, not a trained segmenter). - Segment Anything (SAM) promptable click→mask import — the demo for
BuildSAMFromSafeTensors/BuildSAMVisionTowerand the newRunSAMMaskDecoder(neuralpretrained.pas), a promptable-segmentation importer (model_typesam: facebook/sam-vit-base and siblings). SAM (Kirillov et al. 2023, Segment Anything) is built around a heavy ViT-det image encoder that produces a dense(Grid, Grid, OutCh)image embedding once per image, after which a lightweight prompt-conditioned mask decoder turns a CLICK into a binary mask cheaply — and both stages now import with float64 parity. The IMAGE ENCODER: a biased patch conv (kernel = stride = patch) to a(Grid, Grid, hidden)grid; a learned 2-D absolutepos_embedadded directly (no CLS token, no flatten — viaTNNetCellBias);num_hidden_layerspre-LN transformer blocks whose self-attention is the leafTNNetSAMVisionAttention— windowed attention (SAM zero-padding window partition) for most blocks, global attention for theglobal_attn_indexesblocks, plus the MViTv2 DECOMPOSED relative-position biasQ·rel_pos_h + Q·rel_pos_w(query-dependent, distinct from Swin's query-independent bias table); then the neck (conv11×1 no-bias → LayerNorm2d →conv23×3 pad1 no-bias → LayerNorm2d) tooutput_channels; the MLP uses the exact-erf GELU (TNNetGELUErf). The MASK DECODER (v1: single point → single mask) composes the prompt encoder (point positional encodingcat(sin,cos)of2π·((2c−1)·P)+ learned point/not-a-point embeddings + the dense no-mask embedding), the two-way transformer (token↔image cross-attention over the learned IoU + mask-output tokens, withattention_downsample_rateon the cross-attentions and the layer-0skip_first_layer_peno-residual quirk), and the output upscaling (2×ConvTranspose2d×2 + channels-first LayerNorm) followed by the per-mask hypernetwork-MLP dot with the upscaled embedding → low-res mask logits. The real checkpoint is large / not obtainable offline, so it falls back to the committed config-faithful random pico SAM (tests/fixtures/tiny_sam.*, built bytools/make_pico_sam_fixture.pyfrom the real HFSamModelfloat64 oracle): the encoder embedding parity-checks< 1e-4(TestSAMEncoderParity) and the single-click mask logits parity-check< 1e-4vs HF's own forward (TestSAMMaskDecoderParity, oracle intiny_sam_mask.json). The demo encodes a deterministic synthetic image once, then runsRunSAMMaskDecoderon one positive click (default the image centre; override viaargv[2] argv[3]pixel coords) and writes a real click→mask binary PPM by thresholding the mask logits at 0 (sigmoid 0.5). Point it at a real.safetensors(+config.json) for your own checkpoint. Multi-point/box prompts, multi-mask + IoU output, a trainable TNNet-graph decoder, and a real-checkpoint processor are documented follow-ups intasklist.md. Pure CPU, <1 s on the fixture (random pico weights → wiring/throughput smoke, not a trained segmenter). - End-to-end latent text-to-image (PixArt → VAE) — an end-to-end latent text-to-image pipeline: it CHAINS the already-landed generative importers into one offline CPU sampling loop, the SD3/Sora/PixArt-alpha recipe end to end. No new leaf layer — pure plumbing over landed pieces. The flow is: caller-supplied T5 text states → PixArt-alpha transformer denoiser (
BuildPixArtFromSafeTensors+PixArtConditioning/PixArtDenoise,neuralpretrained.pas) driven by a multi-step DDIM / DPM-Solver++(2M) reverse loop (TNNetDiffusionScheduler,neuraldiffusion.pas) with classifier-free guidance — the cond branch uses the prompt T5 states, the uncond branch uses the null/empty-caption = ZERO T5 states (the PixArt CFG convention; mixed viaTNNetDiffusionScheduler.ApplyCFG) — → the sampled(sample_size, sample_size, in_channels)latent → VAE decode (BuildVaeDecoderFromSafeTensors; the/0.18215latent scaling lives inside the decoder's firstTNNetMulByConstantlayer) → RGB image → a P6 PPM. Steps 1 & 2 only: Step 3 (a real T5 encoder over a tokenized prompt + a real PixArt/VAE checkpoint) is a tracked follow-up intasklist.md; this demo supplies deterministic synthetic T5 states and uses the committed config-faithful pico fixtures (random weights), so it is a wiring / throughput smoke — it proves the chain runs offline and produces a finite image, not photorealism. The pico pair is sized so the latent flows straight through with no reshaping: T5 states(5,1,12)→ PixArt latent(6,6,4)→ VAE image(12,12,3). No network access / self-contained: falls back to the committed pico PixArt (tests/fixtures/tiny_pixart.*,tools/make_pico_pixart_fixture.py, parity-checked< 1e-4inTestPixArtParity) and a matched pico VAE decoder (tests/fixtures/tiny_vae_decoder_ltt.*,tools/vae_decoder_ltt_fixture.py— thevae_decoder_tiny_fixture.pyoracle re-sized tolatent_size 6/latent_channels 4). Regression-tested byTestLatentTextToImageSmoke(TestNeuralPretrained.pas): it runs the full CFG DDIM loop + VAE decode and asserts no NaN/Inf in both the sampled latent and the decoded image. Flags:--steps N(default 4),--cfg W(default 4.0),--dpm(DPM-Solver++(2M) instead of DDIM),--unipc(the UniPC order-2 bh2 predictor-corrector few-step sampler,TNNetSamplerMethod.smUniPCinneuraldiffusion.pas— a unified predictor-corrector that REUSES the previous step's model output as a free corrector before the predictor, noticeably better quality than the DPM++(2M) predictor alone at very low step counts 5-10; Zhao et al. 2023, arXiv:2302.04867),--lcm(the Latent Consistency Model few-step sampler,TNNetLCMScheduler.LCMSample,neuraldiffusion.pas),--smoke(assert finiteness, printSMOKE OK/SMOKE FAIL, set the exit code — the build step exercises this). The--lcmpath swaps the iterative CFG loop for a consistency-model few-step loop: it evaluates a learned consistency functionf(x_t,t) = c_skip(t)·x_t + c_out(t)·x0_hatthat maps any noised latent straight towardx0, so it needs only ~4 steps and a SINGLE conditional model pass per step — guidance is baked in (no cond/uncond double pass). The matching training recipe is LCM-distillation: a consistency loss that distills a many-step teacher (DDIM/DPM++) into this few-step student, trainingfso that adjacent points on the same probability-flow ODE trajectory map to the samex0(self-consistency to the teacher). The committed pico fixtures are not LCM-distilled, so--lcmhere is a wiring smoke (it proves the few-step loop runs and yields a finite image, not 4-step teacher parity); the LCM path is exercised by the sameTestLatentTextToImageSmoke. Point it at your ownpixart.safetensors vae.safetensors(with siblingconfig.jsonfiles) for a real checkpoint. Pure CPU, <1 s on the fixtures. - End-to-end base-UNet + ControlNet single-step denoise — the offline CPU inner loop of diffusers'
StableDiffusionControlNetPipeline, wired end to end over the already-landed importers. No new leaf layer — pure plumbing over the landed ControlNet importer + the newSDUNetDenoiseWithControldriver. The flow is: noisy latent + text states + a (canny-edge-style) control image → ControlNet (BuildControlNetFromSafeTensors+ControlNetResiduals,neuralpretrained.pas) produces thedown_block_res_samples+mid_block_res_sample→ a base SD UNet built WITH control injection (BuildSDUNet(..., pWithControl=true): each down-path skip and the mid output get an extra zero-defaultTNNetInput+TNNetSum, so a plainSDUNetDenoiseis bit-identical to the base UNet) →SDUNetDenoiseWithControlADDS those residuals into the decoder skip connections exactly as diffusers does —down_block_res_samples = [d + c for d, c in zip(down, controlnet_down)]andmid_block_res_sample += controlnet_mid→ the predicted noise a sampler would step on. The committed pico fixtures have random weights, so this is a wiring / throughput smoke — it proves the base-UNet + ControlNet chain runs offline and produces a finite noise prediction, not photorealism. No network access / self-contained: falls back to the committed config-compatible pico SD UNet (tests/fixtures/tiny_sd_unet.*) and a matched pico ControlNet (tests/fixtures/tiny_controlnet.*) — both shareblock_out_channels [16,32], latent grid 8, text seq 5, cross dim 12, so the ControlNet residuals flow straight into the base UNet skips with no reshaping (latent(8,8,4)+ text(5,1,12)+ control(16,16,3)→ 4 down residuals + 1 mid residual → predicted noise(8,8,4)). The combined forward (base UNet noise WITH the ControlNet residuals injected) is parity-checked< 1e-4against a numpy float64 oracle (tools/controlnet_combined_fixture.py) byTestControlNetCombinedParity(TestNeuralPretrained.pas). Pass<unet.st> <unet.cfg> <controlnet.st> <controlnet.cfg>for a real (config-compatible) diffusers base UNet + ControlNet checkpoint pair. Pure CPU, well under a second on the fixtures. - End-to-end base-UNet + T2I-Adapter single-step denoise — the offline CPU inner loop of diffusers'
StableDiffusionAdapterPipeline, wired end to end over the already-landed importers. T2I-Adapter is the lighter sibling of ControlNet (its natural successor): a small conv encoder over a spatial hint (canny/sketch/depth) producing a pyramid of per-resolution feature maps that are ADDED into the SD UNet down-block hidden state — no transformer, no per-block zero-conv, just a lightweight ResNet-ish ladder. No new leaf layer — pure plumbing over existing layers (TNNetSpaceToDepthPixelUnshuffle,TNNetConvolutionLinear,TNNetReLU,TNNetAvgPool,TNNetSum) + the newSDUNetDenoiseWithAdapterdriver. The flow is: a spatial hint (sketch/canny-style image) → T2I-Adapter (BuildT2IAdapterFromSafeTensors+T2IAdapterFeatures,neuralpretrained.pas): PixelUnshuffle(downscale_factor) reshapes the hint onto the latent grid (TNNetSpaceToDepth, with theconv_ininput channels permuted at load from torch PixelUnshuffle order to space-to-depth order),conv_in3×3 →channels[0], thenlen(channels)AdapterBlocks (block 0 keeps the grid, blocks 1..AvgPool2d(2)first; an optional 1×1in_convchannel change, thennum_res_blocksadapter ResnetBlocks of block1 3×3 → ReLU → block2 1×1 → + identity) → one feature map per UNet down block → a base SD UNet built WITH adapter injection (BuildSDUNet(..., pWithAdapter=true): each down block gets an extra zero-defaultTNNetInput+TNNetSumon the main hidden-state path before its downsampler, so a plainSDUNetDenoiseis bit-identical to the base UNet) →SDUNetDenoiseWithAdapterADDS those features intosampleat the end of each down block exactly as diffusers does (down_intrablock_additional_residuals) → the predicted noise a sampler would step on. The committed pico fixtures have random weights, so this is a wiring / throughput smoke — it proves the base-UNet + T2I-Adapter chain runs offline and produces a finite noise prediction, not photorealism. No network access / self-contained: falls back to the committed config-compatible pico SD UNet (tests/fixtures/tiny_sd_unet.*) and a matched pico T2I-Adapter (tests/fixtures/tiny_t2i_adapter.*) — the adapter channels[16,32]and grids (8×8, 4×4) match the pico SD UNet's two down-block stages, so the features add straight into the UNet hidden state with no reshaping (hint(16,16,3)→ 2 features(16,8,8)+(32,4,4); latent(8,8,4)+ text(5,1,12)→ predicted noise(8,8,4)). The adapter feature pyramid is parity-checked< 1e-4against a numpy float64 oracle (tools/t2i_adapter_tiny_fixture.py) byTestT2IAdapterParity(TestNeuralPretrained.pas). Pass<unet.st> <unet.cfg> <adapter.st> <adapter.cfg>for a real (config-compatible) diffusers base UNet + T2I-Adapter checkpoint pair. Pure CPU, well under a second on the fixtures. - VAR next-scale image generation (coarse-to-fine → VQ decode) — the offline CPU VAR (Visual AutoRegressive, next-scale prediction; Tian et al. 2024, arXiv:2404.02905) image-generation loop, end to end over already-landed importers. No new leaf layer — pure plumbing over the landed VAR backbone + VQModel tokenizer. The flow is: class label
y→ class-conditional VAR transformer (BuildVARFromSafeTensors,neuralpretrained.pas) → the coarse-to-fine autoregressive SAMPLING loop (VARGenerate): for each pyramid levels = 0..K-1it runs the forward over the partially-filled multi-scale token sequence (coarser scales already sampled, finer scales still zero), reads the next-scale logits at scales's positions, argmax/temperature-samples theVocabSize-way tokens, and writes them back so the finer scales attend to them through the scale-block-causal mask (the newBlockCausalSegmentsSDPA flag) → the final scale's token grid is a VQ token map → residual/discrete VQ decode to pixels (DecodeVARTokensToImage→TNNetVqModel.DecodeTokensToImage, the landedBuildVqModelFromSafeTensorsfamily) → RGB image → a P6 PPM. Faithfulness note: canonical FoundationVision/var carries the cross-scale residual-VQ feature accumulation (next-scale interpolation/up-sampling of the runningf_hat) inside the VQ tokenizer that produces the input embeddings; this importer's input contract is plain codebook indices embedded byword_embed, so the coarse→fine information flow here is carried purely by the transformer attention over the already-sampled coarser tokens — the full residual-VQ input embedding (and the text-conditioned Infinity variant + real-checkpoint parity) are tracked follow-ups intasklist.md. The committed pico fixtures have random weights, so this is a wiring / throughput smoke — it proves the multi-scale loop + VQ decode run offline and produce a finite image, not a real picture. No network access / self-contained: falls back to the committed pico VAR (tests/fixtures/tiny_var.*,tools/make_pico_var_fixture.py, parity-checked< 1e-4inTestVARParity) and a matched pico VQModel (tests/fixtures/tiny_var_vqmodel.*,tools/make_pico_var_vqmodel_fixture.py— latent grid 3 = VAR final patch_num, codebook 12 = VAR vocab, so the VAR final 3×3 token map is exactly the VQ token grid; image 6×6×3). Regression-tested byTestVARGenerateSmoke(TestNeuralPretrained.pas): it runs the full loop + VQ decode and asserts the deterministic (fixed-seed greedy) fullSeqLentoken sequence shape, legal token ids, and a finite image. Flags:--class N(default 0),--temp T(default 0 = greedy argmax;>0= temperature softmax sampling),--seed N(default 424242),--smoke(assert finiteness, printSMOKE OK/SMOKE FAIL, set the exit code). Point it at your ownvar.safetensors vq.safetensors(with siblingconfig.jsonfiles) for a real checkpoint. Pure CPU, well under a second on the fixtures. - Native text-to-VIDEO (CogVideoX flat-DiT → 3D-causal VAE) — the offline CPU CogVideoX (THUDM/CogVideoX-2b; Yang et al. 2024, CogVideoX, arXiv:2408.06072) native text-to-video loop, end to end over the landed importer. Architecturally distinct from AnimateDiff (which bolts a temporal module onto a frozen SD UNet): CogVideoX is a flat MMDiT-style transformer over a flattened (frame × height × width) latent token sequence with T5 text conditioning + expert adaLN-Zero modulation, with no UNet dependency. No new leaf layer — pure plumbing over landed pieces, and the two genuinely-new CogVideoX primitives are reused landed leaves: 3D RoPE over the (t,h,w)-factored video positions is
TNNetMRotaryEmbedding(the Qwen2-VL M-RoPE leaf, applied to the video portion of Q/K only); the 3D causal-conv VAE isTNNetCausalConv1Dover the VideoMAE space↔time view (a depth-axis causal temporal convolution — left-pad the time axis, no future-frame leakage). The flow is: caller-supplied T5 text states → CogVideoX flat-DiT denoiser (BuildCogVideoXFromSafeTensors+CogVideoXConditioning,neuralpretrained.pas) driven by a multi-step DDIM / DPM-Solver++(2M) reverse loop (TNNetDiffusionScheduler,neuraldiffusion.pas) → the sampled(NumFrames, GridH, GridW, in_channels)video latent → 3D-causal-conv VAE decode tail (BuildCogVideoXVaeDecoderFromSafeTensorsEx+DecodeCogVideoXVae: a per-spatial-cell causal temporal conv + SiLU + pointwise conv) → per-frame RGB → a sequence of P6 PPM files (frame_00.ppm,frame_01.ppm, …). The committed pico fixture has random weights, so this is a wiring / throughput smoke — it proves the chain runs offline and produces finite video frames, not real video. No network access / self-contained: falls back to the committed pico CogVideoX (tests/fixtures/tiny_cogvideox.*,tools/make_pico_cogvideox_fixture.py, parity-checked< 1e-4on one denoiser step AND one VAE decode inTestCogVideoXParity). Flags:--steps N(default 4),--dpm(DPM-Solver++(2M) instead of DDIM),--smoke(assert finiteness, printSMOKE OK/SMOKE FAIL, set the exit code — the build step exercises this). Point it at your owncogvideox.safetensors(with a siblingconfig.json) for a real checkpoint. A real T5 encoder over a tokenized prompt + a real CogVideoX/VAE checkpoint is a tracked follow-up intasklist.md; this demo supplies deterministic synthetic T5 states. Pure CPU, well under a second on the fixture. - Attention entropy report — trains a tiny
TNNetScaledDotProductAttentionnet on a copy task and printsTNNet.AttentionEntropyReport(NN, Probes)for every SDPA layer: per-row softmax entropy asmean ± std, a 10-bin ASCII histogram, plus per-layer "dead head" count (rows attending nearly uniformly — no routing) and "spike head" count (rows attending to one key). Uses the new publicAttentionWeightsaccessor on SDPA. - TracIn training-data attribution — a self-contained TracIn demo (Pruthi et al. 2020, "Estimating Training Data Influence by Tracing Gradient Descent") that attributes a prediction back to the training examples that shaped it (not to input features like
SaliencyReport, nor to layers likeActivationPatchingReport): the influence of a training point on a test point is the dot product of their per-sample loss gradients< grad_loss(z_train), grad_loss(z_test) >— positive = proponent (pushed toward the prediction), negative = opponent (pushed against it). Uses the single-checkpoint "TracInLast" form (final trained weights only, no checkpoint summation). On a clearly-separable 2-D 2-class blob task it plants exactly ONE mislabelled training point (a class-0 point near the boundary, label flipped to class 1), trains a tiny MLP, then ranks every training point by TracIn influence on a corrupted test point and asserts the planted mislabel lands among the top-K most-negative opponents — the paper's headline "TracIn surfaces mislabelled data" result — printing top-K proponents/opponents and a gradedPASS/FAILline (the mislabel comes out as the single most-negative opponent, the only training point with negative influence). Per-sample weight gradients are read out via theSetBatchUpdate(true)/ClearDeltas/Compute/Backpropagateidiom (Neuron.Deltadivided back out by the layer LR; neverUpdateWeights) thatFisherImportanceReport/GradientConflictReportshare; the trained net is frozen. Cost isO(N_train)backward passes per test point (N_trainkept to a few hundred). Multi-checkpoint TracIn-CP summation and a reusableTNNet.TracInReportmethod are noted but deferred — the single-checkpoint ranking is already clean on the toy. Pure CPU, no dataset download, well under a second.
Evaluation, calibration & uncertainty
-
Confusion matrix report — trains a small MLP on a synthetic 3-cluster 2D Gaussian dataset and prints
TNNet.ConfusionMatrixReport(NN, Samples, NumClasses): full CxC confusion matrix, row-normalized recall, per-class precision/recall/F1, macro/micro F1, top-1 and balanced accuracy, most-confused class pairs, and per-class hard-example indices. -
Decision-boundary report — runs
TNNet.DecisionBoundaryReport(NN, Probes)on a 2-input classifier head, sweeping aGx x Gygrid (default 41x41) over an auto-fitted bounding box of the input plane and running one forward pass per cell to render the learned function over its whole domain: an ASCII class map (argmax glyph per cell), a confidence-shaded overlay (top-1 softmax prob, or a normalised top1-top2 logit margin for linear heads), an estimated boundary length scalar (count of grid cells whose 4-neighbours disagree on argmax — a one-number proxy for how convoluted the boundary is), an optional true-class probe overlay so misclassified points stand out, and an optionalx,y,argmax,top1probCSV side-output. The example prints the class map before training (a near-constant single-class plane, boundary length 0) and after (clean separated regions), then contrasts a small net against a deliberately-overfit oversized net on a noisy two-moons set (the overfit net prints wigglier art and a larger boundary length). Guards a non-2-D input layer with a clear error message. Pure forward-only. -
Margin report — trains a small MLP on a synthetic 4-cluster 2D Gaussian dataset (one cluster deliberately wider) and prints
TNNet.TopLogitMarginReport(NN, Samples, NumClasses): the per-sample top-logit margintop1_logit - top2_logiton the final-layer output as an overall 10-bin ASCII histogram, per-class mean/median margin grouped by true class (so a systematically uncertain class stands out), and the lowest-margin sample indices per class as a ready-made "hard examples" pool. Pure forward-only, one validation pass. -
Perplexity evaluation — trains tiny char-level sequence models with
TNNetSoftMaxandTNNetLogSoftMaxheads and printsTNNet.PerplexityReport(NN, Tokens, ContextLen): per-token cross-entropy in nats and bits, perplexity, bits-per-character, top-1 / top-5 accuracy, a 10-bin ASCII histogram of per-token bits, and the K worst-predicted positions. Auto-detects log-space vs probability-space output by sniffing the final layer. -
Knowledge distillation — end-to-end demo of
TNeuralKDTrainer(neural/neuralkd.pas), classic Hinton knowledge distillationL = alpha*CE(label, softmax(z_s)) + (1-alpha)*T^2*KL(softmax(z_t/T) || softmax(z_s/T)). A larger char-level next-token teacher (hidden=96) is trained with ordinary SGD on a structured synthetic 12-symbol stream, then two identical small students (hidden=12, same RNG init) are trained at matched steps / examples-seen: one WITH KD (alpha=0.3, T=3.0, soft teacher targets blended with the hard label) and one HARD-LABEL ONLY (the same trainer withalpha=1.0, where the soft term vanishes andStep()is an ordinary cross-entropy SGD step — the equivalence pinned byTestAlphaOneMatchesPlainCE). Same data order, same LR, sameStep()count, so the runs differ only in the soft term. Held-outTNNet.PerplexityReportshows the KD student more than halves the hard-only student's perplexity (representative: teacher 14.06, KD student 18.71, hard-only 40.91) — the teacher's temperature-softened "dark knowledge" (relative probabilities of the wrong classes) regularises the tiny student better than a one-hot label alone. The teacher is run forward-only inside the trainer so it stays frozen (TestTeacherWeightsUnchanged). The README shows how to swap in a real imported teacher (GPT-2 / TinyStories via the safetensors /.binimporters) for users with more RAM — the only requirement is a shared vocabulary width and aTNNetFullConnectLinear(Vocab) -> SoftMaxtail on both nets. Pure CPU, ~25 s, fits a 3 GB cap. -
HellaSwag-style multiple-choice eval — the end-to-end demo for
EvaluateMultipleChoice(neural/neuralnlpmetrics.pas), the lm-evaluation-harness HellaSwag / ARC / PIQA scoring pattern: for each item it scores every candidate completion of a shared context withScoreCompletionand lets the argmax win, reporting acc (gold wins by sum of completion log-probs, lm-evalacc) and acc_norm (gold wins by mean / length-normalized log-prob, lm-evalacc_norm). To stay within a tiny CPU/memory budget (no multi-GB download) it trains a small char-level next-token model on toy bigram phrases and scores four hand-writtenTNNetMultipleChoiceItemrecords — but the scoring path is checkpoint-agnostic: swap the toy model for aBuildLlamaFromSafeTensors(or any importer) and feedTNeuralHFTokenizertoken ids into the same item records and the harness is unchanged. Highlights the single-next-token-head encoding gotcha (ScoreSequenceusesCopyReversedNoChecksIntArr; the training loop must match it or accuracy collapses to chance). The toy model learns the bigrams perfectly so both metrics report1.0000(4/4). Pure CPU, well under a minute.EvaluateMultipleChoicenow scores each item's candidates throughScoreCompletionsBatch, sharing the common context prefix (single-head nets skip the shared-context forwards for scores identical to the per-candidate path), andScoreSequence/ScoreCompletionaccept an optionalLastWindowflag that scores over-context sequences on their trailing context-window instead of raising. -
MMLU few-shot accuracy eval — the end-to-end demo for
EvaluateMMLU/MMLUReport(neural/neuralnlpmetrics.pas), the canonical 4-choice (A/B/C/D) knowledge benchmark scored the HF lm-evaluation-harness way: for each question it builds the standard k-shot prompt (Question ..\nA. ..\nB. ..\nC. ..\nD. ..\nAnswer:, with the few-shot demos drawn from the same subject) and predicts by the log-probability of the single answer-letter token that follows — not by the full-continuation perplexity thatEvaluateMultipleChoice/ HellaSwagEval uses (the two scoring modes are kept clearly separate). Reports per-subject accuracy plus the macro-average (mean over subjects — the headline MMLU number) and the micro-average (pooled over questions), in both 0-shot and 5-shot modes. To stay within a tiny CPU/memory budget (no network fetch, no multi-GB download) it trains a small char-level next-token model on a tiny embedded smoke subset (two toy "subjects") and runs in seconds under the 3 GB ulimit — the goal is the harness mechanics, not a real accuracy number. The scoring path is checkpoint-agnostic: swap the toy model for aBuildLlamaFromSafeTensors(or any importer), feedTNeuralHFTokenizerids of the prompt and the four" A".." D"answer-letter tokens into the sameTNNetMMLUQuestionrecords, and the harness is unchanged. Wiring the fullcais/mmlu(hendrycks_test) splits via the venv-xdatasetspackage is a documented follow-up. -
Calibration report — runs the forward-only
neuralcalibrationunit (CalibrationReport(NN, Inputs, Labels, BinCount)) on a held-out validation split of a synthetic 4-class 2D-Gaussian classifier and reports how well the model's confidence matches its accuracy: Expected Calibration Error (ECE), Maximum Calibration Error (MCE), the Brier score, and a reliability diagram (per-bin mean-confidence / accuracy / count) rendered both as an ASCII chart (accuracy#bars vs confidence-bars) and as a P2 (ASCII) PGM image (bin-accuracy bars against they=xreference line). It then fits a single temperature-scaling scalarTviaFitTemperature(a 1-D grid scan minimising validation NLL) and prints the report before and after scaling, so the ECE drop on an over-confident model is visible. Because a softmax head only exposes probabilities, temperature scaling operates on pseudo-logitsz := ln(p)read off the final layer (softmax(z/T)is exactly temperature scaling on the original logits up to an additive constant); the trained backbone is never re-trained or mutated. -
Label-smoothing calibration check — tests the textbook claim (Mueller, Kornblith & Hinton, NeurIPS 2019) that label smoothing improves calibration at a small accuracy cost. Trains the same tiny softmax MLP once per smoothing strength
eps ∈ {0, 0.05, 0.10, 0.20}— the only knob beingTNNetLabelSmoothingLoss.Create(eps), an identity-passthrough loss head that smooths the target tot' = (1-eps)*onehot + eps/NumClasses(soeps=0is exactly plain cross-entropy, the baseline arm) — then feeds each trained model into the forward-onlyneuralcalibrationreport and prints aneps | val-accuracy | ECE | Briertable. The task is deliberately hard so calibration actually discriminates: 6 heavily-overlapping 2D Gaussian clusters on a ring (large sigma) plus 15% random training-label noise, with calibration measured on a clean-label validation split. On this run smoothing helps with a clear sweet spot —eps=0.10is best on both ECE (0.307 vs the 0.423 baseline) and Brier (0.944 vs 1.086) for a ~2% accuracy cost, whileeps=0.20over-smooths and regresses — so it is a calibration knob with an optimum, not a free win. Pure CPU, all four arms in ~85 s, no downloads. -
Test-time augmentation — trains a tiny synthetic 8x8x3 colored-pattern classifier (red vertical stripe / green horizontal stripe / blue checkerboard) and prints
TNNet.TTAReport(NN, Probes, Labels): baseline top-1 accuracy on the untransformed inputs, per-transform top-1 accuracy with each transform applied alone (identity,TNNetFlipX,TNNetFlipY,TNNetReverseChannels,TNNetRoll(+1)), the full-ensemble TTA accuracy (argmax of the per-class average across all transforms) and its signed delta vs baseline, a per-class accuracy delta (so classes that lose under TTA are visible), the per-sample agreement rate, and aTTA helps/TTA neutral/TTA hurtsverdict. Printed twice — averaging raw logits (default) and averaging post-softmax probabilities (soft voting) — so the linear-vs-geometric-mean question is visible side by side. Forward-only; eachT(x)is produced by a tinyInput -> Transformwrapper net (identity feeds the raw input straight through) and the trained weights are never touched. -
MC-dropout uncertainty report — runs
TNNet.MCDropoutUncertaintyReport(NN, Probes [, Labels])on a synthetic 3-cluster 2D dropout classifier, the Monte-Carlo-Dropout epistemic uncertainty estimator (Gal & Ghahramani 2016). Unlike the rest of the family it deliberately keeps dropout active at inference (NN.EnableDropouts(true)), runsNumPasses(default 30) stochastic forward passes per probe and aggregates the per-pass softmax vectors to separate total uncertainty (predictive entropyH[mean_p]), aleatoric (expected entropymean_t H[p_t]) and epistemic (their difference, the mutual-information / BALD scoreH[mean_p] - mean_t H[p_t]), plus per-sample top-class-prob variance and the pass-to-pass argmax flip rate. Across the batch it prints a 10-bin BALD histogram, the K most-uncertain sample indices (an active-learning query queue) and — with labels — a correctness cross-tab (mean entropy of correct vs incorrect predictions). The example evaluates three probe groups: the cluster cores (in-distribution — BALD ~ 0, 0% flips), an OOD band placed in the empty space between clusters (BALD ~4x higher, flip rates up to ~48% mid-band as the argmax wobbles), and a labelled validation split where the model is far more uncertain on its mistakes (0.033vs0.530mean entropy) — "the model knows what it doesn't know". Spec invariants: withNumPasses=1and dropout disabled BALD collapses to ~0, and a net with noTNNetAddNoiseBaselayer emits a clear "no stochastic layers — MC sampling is a no-op" warning. The dropout-enabled flag is saved and restored; weights are never touched (forward-only). -
Deep Ensembles predictive uncertainty — the gold-standard Deep Ensembles baseline (Lakshminarayanan, Pritzel & Blundell, NeurIPS 2017) on the same synthetic 3-cluster 2D task as MCDropoutUncertainty, using only existing layers (no new layer). Trains M=5 INDEPENDENT softmax MLPs (identical architecture, different
RandSeedper member), then averages the per-member post-softmax probability vectors and reports, per probe group: (a) the average single-member accuracy + ECE/Brier vs the ensemble's, both vianeuralcalibration.ComputeCalibration/CalibrationReport(the ensemble-mean probabilities are fed through anInput->Identitypassthrough net so the forward-only calibrator is reused, not re-implemented); (b) the predictive-entropy decompositiontotal H[mean_p] = aleatoric (mean_m H[p_m]) + epistemic (mutual information I = H[mean_p] - mean_m H[p_m] >= 0); and (c) an ASCII bar of per-group epistemic uncertainty. On the OOD band between clusters the epistemic/MI term spikes (~0.027nats, ~270x the cluster cores'0.0001) while staying ~0 on the confident cores, and the ensemble's Brier beats the average member's — the ensemble knows what it doesn't know. Built-in checks: M=1 reproduces member 0 bit-for-bit, ensemble accuracy >= mean single-member accuracy, and epistemic MI >= 0 everywhere and strictly higher on the OOD band than on the cores. Contrast: MCDropoutUncertainty samples dropout inside one net (one posterior mode) whereas an ensemble uses M independent nets (several modes); KnowledgeDistillation compresses such an ensemble teacher away, here we keep it and quantify its uncertainty; TestTimeAugmentation averages input transforms of a single model, not independent models. -
Conformal prediction — split (inductive) conformal prediction: wraps a frozen softmax MLP (trained on a synthetic 5-class overlapping-2D-Gaussian-blob task) in a guaranteed-coverage SET predictor (Vovk; Angelopoulos & Bates 2021), using only existing layers (no new layer, no library change). The data is split TRAIN / CALIBRATION / TEST; on the calibration split it scores the LAC / threshold nonconformity
s = 1 - softmax[true_class], takes the conformal quantileqhat= theceil((n+1)(1-alpha))/nempirical quantile (handling theqhat = +infall-labels edge case), and at test time emits the set{ k : 1 - softmax[k] <= qhat }. Sweepingalpha in {0.01, 0.05, 0.10, 0.20}it prints a table ofalpha | target-coverage | empirical-coverage | mean-set-size | singleton% | empty%. The point is the finite-sample, distribution-free marginal guaranteeP(true label in set) >= 1 - alphathat the scalar/point uncertainty examples never provide: CalibrationReport matches a single confidence to accuracy, MarginReport reports the top-1-vs-top-2 logit margin, MCDropoutUncertainty / DeepEnsembleUncertainty report stochastic/ensemble entropy — none promises anything about the true label, whereas conformal returns a variable-size LABEL SET (singleton on easy inputs, larger on hard ones) that provably covers it. Built-in checks (PASS/FAIL,Halt(1)on failure): empirical test coverage>= 1 - alpha - slackacross the whole sweep, and mean set size shrinks monotonically as alpha grows. Pure CPU, single-threaded, runs in under 10 seconds. -
Adversarial-robustness report — runs
TNNet.AdversarialRobustnessReport(NN, Samples, Labels, EpsList)on a small synthetic 3-class 2-D classifier, crafting FGSM (fast gradient sign method, Goodfellow et al. 2015) input perturbationsx_adv = x + eps*sign(d loss/d x)at an increasing menu of epsilons (one forward + one backward per sample to get the input gradient, reusingTNNet.EnableInputGradient) and re-running a clean forward on each perturbed input. Reports a top-1 accuracy-vs-eps degradation curve (eps=0 is the clean baseline), a 10-bin histogram of the per-sample critical epsilon (smallest eps that flips the clean argmax), the mean clean-confidence of the earliest flippers vs the longest survivors, per-class accuracy at the median eps, and arobust/moderately fragile/fragileverdict. A second model trained with input-noise augmentation shows a flatter degradation curve (the expected robustness gain). The network is frozen —ClearDeltas, neverUpdateWeights— so the trained weights are never modified (an evaluation of robustness, not adversarial training). -
Mahalanobis OOD Detector - Reproduces Lee et al. 2018 out-of-distribution detection: freeze a small classifier, fit per-class Gaussians with a tied covariance over the penultimate features, and score new points by negative squared Mahalanobis distance. Reports a single AUROC (rank-statistic / Mann-Whitney U form) separating in- from out-of-distribution. Pure CPU, ~2s.
-
Anomaly-detection autoencoder (reconstruction-error AUROC) — the textbook reconstruction-error anomaly detector: an undercomplete autoencoder (
8→16→2→16→8, ~320 weights) is trained ONLY on a synthetic NORMAL distribution (samples on a low-dimensional 2-D manifold embedded in 8-D), then scores held-out points by per-sample reconstruction MSE — anomalies off the learned manifold reconstruct poorly. Reports the mean reconstruction error of normal vs anomaly (0.95 vs 6.38) and a single rank-AUROC = 0.97 (Mann-Whitney-U form, the helper copied locally from MahalanobisOOD rather than promoted to the library) with a built-in PASS gate. Distinct from MahalanobisOOD (which scores classifier features against fitted Gaussians; this scores reconstruction error from an unsupervised generative model). Pure CPU, a few seconds. -
Masked Autoencoder (MAE) self-supervised pretraining — the self-supervised-vision example (He et al. 2022, Masked Autoencoders Are Scalable Vision Learners, arXiv:2111.06377): unlike the repo's existing full-image autoencoders (VisualAutoencoder/SparseAutoencoder/AnomalyAutoencoder/GumbelAnnealingAutoencoder), MAE drops most of the image and reconstructs only the missing patches from the few that remain, which is what forces the encoder to learn structure with no labels. Each image is cut into a 3×3-patch grid (16 tokens); 50% of the patches are randomly masked each step (replaced by a shared zero mask token), a ViT-style transformer encoder (
AddTransformerEncoderBlock×3) with learned positional embeddings processes the token sequence, a lighter transformer decoder (×1) + a per-token linear head reconstructs all patches, and the reconstruction MSE is applied ONLY to the masked patches — realised exactly without a custom layer by building the regression target so visible entries equal the prediction (zero error) while masked entries hold the true pixels (FOutputError = pred − targetis the per-element MSE gradient for a linear head). The payoff: the encoder is then FROZEN and a single linear+softmax linear probe is trained on its mean-pooled features, versus the same probe on a randomly-initialised encoder of the SAME architecture — a typical run scores 0.768 vs 0.656 (an ~11-point gap, well above the 0.250 chance level), averaged over 5 probe seeds so the number is stable and reproducible. The downstream task classifies the stripe PERIOD (spatial frequency) with orientation randomised as a nuisance — a global property no single patch reveals — and the encoder width is kept deliberately narrow (a bottleneck) so a random projection has little capacity and the learned representation wins (with a wide encoder, random nonlinear features are a famously strong linear-probe baseline and the gap closes; narrowing the bottleneck is what exposes the value of pretraining). The new code versus a plain ViT is the random patch mask, the masked-only loss target, and the encoder-end feature tap the probe reads. Honest deviation from canonical MAE (a static graph can't express a per-sample variable sequence length): the encoder runs over the full token grid with masked positions held by the zero mask token, so the asymmetric encode-visible-only compute speedup is dropped — everything else (random masking, learned positions, encoder+lighter-decoder asymmetry, masked-only loss) is faithful. Pure CPU, ~10 s. -
Deep Q-Learning (DQN) — a reinforcement-learning example: a minimal but complete Deep Q-Network (Mnih et al. 2015, Human-level control through deep reinforcement learning) that learns an optimal navigation policy on a tiny, deterministic 5x5 grid-world (fixed goal +1, two terminal pits -1, a small per-step living penalty pressuring the shortest path) — the agent, the environment and the replay machinery all live in the single
.lprand the Q-network is composed from existing dense layers (no new layer class). Textbook DQN: a25 -> ReLU(64) -> ReLU(64) -> Linear(4)Q-net (one-hot state in, one Q-value per action out), an experience-replay ring buffer of(s,a,r,s',done)transitions sampled in random minibatches, a target network re-synced periodically viaCopyWeights(notLoadFromFile) to stabilise the TD target, epsilon-greedy exploration with exponential decay, and the standard single-action TD updatey = r + gamma·max_a' Q_target(s',a')regressed intoQ(s,a)for the taken action ONLY (the target vector is seeded with the currentQ(s,·)so the gradient is exactly zero on untaken actions). Minibatch gradients are accumulated with theSetBatchUpdate(True)idiom (the manualUpdateWeightspath bypasses grad clipping, so one-hot inputs + a small LR keep it stable) and applied once per batch. Headline: a moving-average learning curve climbs from random (return ~-1.06, ~17 steps, hitting pits / timing out) toward optimal (return ~0.78, the optimal 8 steps), then the greedy (epsilon=0) policy is rolled out and its ASCII trajectory reaches the goal along the shortest pit-avoiding path — 100% greedy success rate over all 21 non-terminal start cells. Pure CPU, ~32 s on two cores. -
HuggingFace Hub fetch -> import in one command — demo for the opt-in
neural/neuralhfhub.pasHub download helper, the only HTTP anywhere near the import path (the core importers inneuralpretrained.passtay strictly offline; only programs thatuses neuralhfhublinkfphttpclient/OpenSSL).HubFetchModel(repo)resolveshttps://huggingface.co/{repo}/resolve/{rev}/{file}(redirects to the CDN handled,revdefaults tomain), downloadsconfig.json+tokenizer.json(404-tolerated) + the safetensors weights — transparently falling back tomodel.safetensors.index.jsonand every shard in itsweight_mapwhen the checkpoint is sharded — into a skip-if-present local cache (~/.cache/neural-api/hub/{repo}/{rev}/..., overridable viaHubSetCacheDir/NEURAL_API_HUB_CACHE;.part-then-rename so interrupted downloads never poison it;HF_TOKEN/token param sent as a Bearer header for gated repos) and returns the snapshot directory thatBuildFromPretrainedaccepts, soBuildFromPretrained(HubFetchModel('sentence-transformers/all-MiniLM-L6-v2'))is the whole program. Verified end to end: the MiniLM download is byte-identical to what python'shuggingface_hubfetched, builds to the same 242-layer/22.5M-weight encoder, and the default no-args run pulls a ~100KB five-shardhf-internal-testingcheckpoint through the index-json path (123 layers). Second run is instant and fully offline. -
Chat terminal — interactive REPL chat over any instruct checkpoint the generic importer dispatch understands (
BuildFromPretrainedinneural/neuralpretrained.pas: gpt2, llama, mistral, qwen2/3, gemma/2/3, phi/phi3, gpt_neo(x), gptj, cohere/cohere2, rwkv, mamba, bloom, deepseek_v2, ...): point it at a model directory (config.json + safetensors/torch-bin + tokenizer.json) and chat. The Cohere route (Command-R / Aya / Command-R7B) makes this the entry point for open multilingual chat — see the ChatTerminal README's cross-lingual session example. Multi-turn history rendered through theneuralchat.paschat templates — the format is fingerprinted fromtokenizer_config.json'schat_template(DetectChatFormatFromConfigFile) with a--formatoverride — and each reply streams token-by-token to stdout (decoded-delta printing with a BPE/UTF-8 prefix guard,Flushper token so piping streams too). Inference knobs map onto the existing decode toolbox:--temperature/ penalties run in the probability domain through aTNNetLogitsProcessorChain(TNNetTemperatureProcessor,TNNetPenaltyProcessoroverTNNetTokenHistoryPenalty),--top-k/--top-p/--min-ppick the matchingTNNetSampler*, and generation stops on the tokenizer EOS or the format's end-of-turn marker matched as a token-id stop sequence. Always builtpTrainable=false;--int8adds weight-only int8 (pQuantizeInt8). REPL niceties:/exit,/reset,/system <msg>;--selftestruns 31 offline unit checks (arg parsing, ChatML render parity, REPL command parsing) with no model files needed. Full forward per token (the GPT2Import convention), so it works across every imported family including the non-KV-streamable ones. -
StarCoder2 code completion — a minimal CPU code-completion demo for the StarCoder2 importer (
BuildStarCoder2FromSafeTensors/BuildFromPretrainedmodel_typestarcoder2, the bigcode/starcoder2-3b/7b/15b code-LLM family — a code-specialised decoder). Point it at a model directory (config.json + safetensors/torch-bin) and its stock BPEtokenizer.json, give it a code prompt (default a Pythondef fibonacci(n):header), and it greedily extends the prompt one argmax token at a time and prints the decoded completion. StarCoder2 is architecturally distinct from every Llama-path importer: it pairs RoPE + GQA + (optional) sliding-window attention with three GPT-2-flavoured pieces the RMSNorm/SwiGLU importers never touch — biasednn.LayerNormnorms (NOT RMSNorm), bias=True on every linear including q/k/v AND o_proj (the exact path OLMo-2 deliberately rejects), and a plain two-matrixgelu_pytorch_tanhFFN (c_fc -> GELU -> c_proj, no SwiGLU gate). All of that lives inneuralpretrained.pas; this demo is just the generation harness (always builtpTrainable=false; keepSeqLensmall on the real 3B checkpoint — the full 16k context is slow on CPU). The committed pico parity fixtures (tests/fixtures/tiny_starcoder2{,_window}.*, generated bytools/starcoder2_tiny_fixture.py) pin both a full-attention and a sliding-window config to the HF float64 oracle within 1e-4 (TestStarCoder2LogitParity/TestStarCoder2WindowLogitParity).