Skip to content

avoid unused bias draws in linear no_bias - #58

Merged
CanReader merged 1 commit into
CanReader:masterfrom
Boulea7:fix/linear-no-bias-rng-20261015
Oct 15, 2026
Merged

CanReader merged 1 commit into
CanReader:masterfrom
Boulea7:fix/linear-no-bias-rng-20261015

Conversation

@Boulea7

@Boulea7 Boulea7 commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

What this changes

Constructs the weight directly in Linear::no_bias, without generating a bias that is immediately discarded. This preserves the layer's weight initialization while removing the unused bias draws from the subsequent RNG stream.

Why

Fixes #51.

How it was verified

The seeded RNG-stream regression fails on unmodified master and passes with this change. The same tests also check ordinary biased initialization and bias-free forward/parameter behavior.

  • cargo test --locked --offline -- --test-threads=1 passes (CPU tests and doctests)
  • cargo clippy --locked --offline --all-targets -- -D warnings is clean
  • cargo fmt --check passes

CUDA, Rust 1.87, and release-profile checks were not run locally.

Checklist

  • Public items have /// docs

@Boulea7
Boulea7 requested a review from CanReader as a code owner October 15, 2026 14:33

@CanReader CanReader left a comment •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, for your PR I checked that the new test fails on master and passes here, and CI is green currently.

There's one optional thing, not blocking: new and no_bias now both spell out the weight init. Conv2d handles this with a private build and bias.then(...), and the same works here (doc comments as before):

pub fn new(in_features: usize, out_features: usize) -> Linear {
    Linear::build(in_features, out_features, true)
}

pub fn no_bias(in_features: usize, out_features: usize) -> Linear {
    Linear::build(in_features, out_features, false)
}

fn build(in_features: usize, out_features: usize, bias: bool) -> Linear {
    Linear {
        weight: Param::new(Tensor::kaiming_uniform(
            &[out_features, in_features],
            in_features,
        )),
        bias: bias.then(|| Param::new(uniform_bias(out_features, in_features))),
        in_features,
        out_features,
    }
}

The weight is still drawn before the bias, so new_preserves_weight_then_bias_initialization passes unchanged. Happy to merge as is if you'd rather leave it.

@CanReader

Copy link
Copy Markdown
Owner

Ok I'm merging this PR now, for any improvments you can open a new PR, I'll review it

@CanReader
CanReader merged commit 3e2c2ae into CanReader:master Oct 15, 2026
12 checks passed
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.

Linear::no_bias still draws a bias from the RNG and throws it away

2 participants