[PR #1869] [MERGED] fix(layers): Dropout uses global unseeded np.random instead of module rng #29147

Closed
opened 2026-06-20 13:03:47 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/harvard-edge/cs249r_book/pull/1869
Author: @Shashank-Tripathi-07
Created: 6/16/2026
Status: Merged
Merged: 6/16/2026
Merged by: @profvjreddi

Base: mainHead: fix/dropout-seeded-rng


📝 Commits (1)

  • 180e933 fix Dropout to use the module-level seeded rng instead of global np.random

📊 Changes

1 file changed (+1 additions, -1 deletions)

View changed files

📝 tinytorch/src/03_layers/03_layers.py (+1 -1)

📄 Description

What breaks

03_layers.py seeds a module-level RNG for reproducibility:

rng = np.random.default_rng(7)

Dropout._create_mask() ignores this and calls np.random.random(shape), which draws from the global unseeded state. Dropout masks are therefore different on every run even when the user expects deterministic behavior from the module seed. Any unit test that checks a specific Dropout output value will be flaky.

Fix

# before
binary_mask = (np.random.random(shape) < keep_prob).astype(np.float32)

# after
binary_mask = (rng.random(shape) < keep_prob).astype(np.float32)

One line change -- Dropout now participates in the same reproducible stream as every other random operation in the module.

Test plan

  • pytest tinytorch/tests/03_layers/ passes
  • Run twice with same seed -- Dropout produces identical masks both times

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/harvard-edge/cs249r_book/pull/1869 **Author:** [@Shashank-Tripathi-07](https://github.com/Shashank-Tripathi-07) **Created:** 6/16/2026 **Status:** ✅ Merged **Merged:** 6/16/2026 **Merged by:** [@profvjreddi](https://github.com/profvjreddi) **Base:** `main` ← **Head:** `fix/dropout-seeded-rng` --- ### 📝 Commits (1) - [`180e933`](https://github.com/harvard-edge/cs249r_book/commit/180e9331971c039893c8bcd4b46347177866a2e3) fix Dropout to use the module-level seeded rng instead of global np.random ### 📊 Changes **1 file changed** (+1 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `tinytorch/src/03_layers/03_layers.py` (+1 -1) </details> ### 📄 Description ## What breaks `03_layers.py` seeds a module-level RNG for reproducibility: ```python rng = np.random.default_rng(7) ``` `Dropout._create_mask()` ignores this and calls `np.random.random(shape)`, which draws from the global unseeded state. Dropout masks are therefore different on every run even when the user expects deterministic behavior from the module seed. Any unit test that checks a specific Dropout output value will be flaky. ## Fix ```python # before binary_mask = (np.random.random(shape) < keep_prob).astype(np.float32) # after binary_mask = (rng.random(shape) < keep_prob).astype(np.float32) ``` One line change -- Dropout now participates in the same reproducible stream as every other random operation in the module. ## Test plan - [ ] `pytest tinytorch/tests/03_layers/` passes - [ ] Run twice with same seed -- Dropout produces identical masks both times --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-06-20 13:03:47 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/cs249r_book#29147