Repository navigation
avoid unused bias draws in linear no_bias - #58
Merged
CanReader merged 1 commit intoOct 15, 2026
Merged
Conversation
CanReader
approved these changes
Oct 15, 2026
Owner
There was a problem hiding this comment.
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.
Owner
|
Ok I'm merging this PR now, for any improvments you can open a new PR, I'll review it |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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=1passes (CPU tests and doctests)cargo clippy --locked --offline --all-targets -- -D warningsis cleancargo fmt --checkpassesCUDA, Rust 1.87, and release-profile checks were not run locally.
Checklist
///docs