Skip to content

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

Open
michalharakal wants to merge 1 commit into
developfrom
feature/861-dropout
Open

feat(nn): implement stochastic masking in Dropout#867
michalharakal wants to merge 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.

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

1 participant