Skip to content

feat(nn): implement stochastic masking in Dropout - #867

Merged
michalharakal merged 1 commit into
developfrom
feature/861-dropout
Jul 24, 2026
Merged

feat(nn): implement stochastic masking in Dropout#867
michalharakal merged 1 commit into
developfrom
feature/861-dropout

Conversation

@michalharakal

Copy link
Copy Markdown
Contributor

Closes #861.

What

sk.ainet.lang.nn.layers.Dropout was an identity placeholder in both phases (its own KDoc said so). It now implements inverted dropout:

  • Under a training-phase context (ctx.inTraining, training flag, p > 0): each element is zeroed with probability p, survivors are scaled by 1 / (1 - p) — expected activation preserved, no rescaling needed at inference.
  • Eval phase (or training = false, or p == 0): exact identity, as before.
  • The mask is a constant tensor combined with ops.multiply, so gradients flow to the surviving inputs only — standard dropout backward with no dedicated autograd rule.
  • New constructor parameter random: Random = Random.Default (defaulted, source-compatible) for reproducible masks.
  • Supported dtypes: FP32, FP16, BF16, FP64; anything else throws UnsupportedOperationException (mirrors CrossEntropyLoss's float-only contract).

Tests

  • lang-core (DropoutTest, DropoutPhaseTest): identity paths now assert values, masked path asserts shape. Note: the lang-core default context runs on the shape-only VoidTensorOps backend (every op returns zeros), which is why the value-level assertions can't live there — and, incidentally, why the placeholder behavior was never caught.
  • backend-cpu (DropoutMaskingTest, new): every output element is 0 or 1/(1-p) with both present; mean over 10k elements stays ≈ 1 (p = 0.3); seeded masks are bit-reproducible; eval phase is an exact identity.
  • API dumps updated for the new constructor signature (jvm via apiDump, android mirrored by hand — no Android SDK on this machine).

Motivation

Any model using Dropout for regularization currently trains without it, silently. Found while porting a GPT training pipeline (attention-weight dropout) to SKaiNET 0.36.0 — details in #861.

@github-actions

Copy link
Copy Markdown

📖 Documentation Preview

The documentation has been built successfully for this PR.

Generated Files:

  • Operator documentation: docs/modules/operators/_generated_/
  • JSON schema output: operators.json

Artifacts:

  • Download the documentation-preview-867 artifact to view the complete documentation locally.

This comment will be updated automatically when the PR is updated.

Dropout was an identity placeholder in both phases. It now applies
inverted dropout under a training-phase context: each element is zeroed
with probability p and survivors are scaled by 1/(1-p), so the expected
activation is preserved and inference needs no rescaling. The mask is a
constant tensor combined with an element-wise multiply, so gradients
flow to surviving inputs without a dedicated autograd rule.

An injectable Random enables reproducible masks. Identity paths stay in
lang-core tests; value-level masking behavior is tested on the real CPU
backend (the lang-core default context uses the shape-only Void ops).

Closes #861
@github-actions

Copy link
Copy Markdown

📖 Documentation Preview

The documentation has been built successfully for this PR.

Generated Files:

  • Operator documentation: docs/modules/operators/_generated_/
  • JSON schema output: operators.json

Artifacts:

  • Download the documentation-preview-867 artifact to view the complete documentation locally.

This comment will be updated automatically when the PR is updated.

@michalharakal
michalharakal requested a review from aharakal July 24, 2026 09:39
@michalharakal
michalharakal merged commit b00d72f into develop Jul 24, 2026
18 checks passed
@michalharakal
michalharakal deleted the feature/861-dropout branch July 24, 2026 09:41
MacOS pushed a commit to MacOS/SKaiNET that referenced this pull request Jul 27, 2026
Bump version 0.36.0 -> 0.37.0 (gradle.properties, docs/antora.yml,
README quickstart). Promote CHANGELOG [Unreleased] to [0.37.0]: Lstm
layer (SKaiNET-developers#824), real Dropout masking (SKaiNET-developers#867), LR schedules and mutable
optimizer lr (SKaiNET-developers#866), optional-bias and open Linear (SKaiNET-developers#870, SKaiNET-developers#875),
androidNative IO targets (SKaiNET-developers#836, SKaiNET-developers#842, SKaiNET-developers#845), the SDPA default-scale fix
(SKaiNET-developers#880), three autograd fixes (SKaiNET-developers#877), the argMax DAG output spec (SKaiNET-developers#878),
tokenizer BPE inference and N-D gather (SKaiNET-developers#879), plus the CI/docs
supply-chain hardening and toolchain bumps. Refresh README "What's New"
and add a Contributors (0.37.0) section.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dropout layer is an identity placeholder in TRAIN phase — no stochastic masking

2 participants