Skip to content

feat(nn): optional bias in Linear#870

Open
michalharakal wants to merge 1 commit into
developfrom
feature/linear-optional-bias
Open

feat(nn): optional bias in Linear#870
michalharakal wants to merge 1 commit into
developfrom
feature/linear-optional-bias

Conversation

@michalharakal

Copy link
Copy Markdown
Contributor

What

Linear's initBias becomes nullable with a null default, giving a bias-less projection y = x W^T — the equivalent of PyTorch's nn.Linear(..., bias=False):

  • No bias → only the weight parameter is registered, so trainableParameters() counts and checkpoints match architectures defined without bias (e.g. GPT-2's scratch config with qkv_bias=False totals exactly 163,009,536 parameters only when the Q/K/V projections and output head carry no bias).
  • onForward returns the matmul directly when no bias is present; existing bias behavior (including the 1-D input reshape path) is unchanged.
  • New biasOrNull() helper next to the throwing bias() accessor.

Compatibility

  • Kotlin callers: unaffected — they bind the full constructor (with default-marker), and passing a non-null Tensor still type-checks against Tensor?.
  • Java callers: the @JvmOverloads overload set changes — the 4-arg (inFeatures, outFeatures, initWeights, initBias) convenience overload is replaced by (inFeatures, outFeatures, initWeights). Flagged here explicitly; API dumps updated (jvm via apiDump, android mirrored by hand — no Android SDK on this machine). If preserving that overload matters pre-review, a secondary constructor can be added — happy to adjust.

Tests

  • LinearOptionalBiasTest (lang-core): bias-less layer registers exactly one parameter, biasOrNull()/bias() contracts, trainable-parameter counts (15 vs 12 on a 4→3 layer).
  • LinearNoBiasTest (backend-cpu): hand-computed pure projection, no-bias ≡ zero-bias on real ops, 1-D vector input path.
  • Full jvmTest for lang-core and backend-cpu green, apiCheck green.

Motivation

Needed by GPT-style architectures (qkv_bias=False, weight-tied heads); found while porting an educational GPT project to SKaiNET, which currently carries its own Linear implementation solely for this. Related prior contributions: #866 (LR schedules), #867 (Dropout).

@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-870 artifact to view the complete documentation locally.

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

Linear's initBias is now nullable with a null default, creating a
bias-less projection (y = x W^T) — the equivalent of PyTorch's
nn.Linear(bias=False), needed for GPT-style qkv_bias=False projections
and weight-tied output heads. A bias-less layer registers only its
weight parameter, so parameter counts and checkpoints match
architectures defined without bias. Adds a biasOrNull() helper next to
the throwing bias() accessor.

Note for Java callers: the @jvmoverloads overload set changes — the
4-arg (in, out, weights, bias) convenience overload is replaced by
(in, out, weights); Kotlin callers bind the full constructor and are
unaffected.
@michalharakal
michalharakal force-pushed the feature/linear-optional-bias branch from c9a1fee to 3830ebe Compare July 23, 2026 21:04
@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-870 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.

1 participant