Conversation
WalkthroughModified the neural network evaluator to replace heap-allocated accumulator vectors with a fixed-size stack array and simplified indexing by removing the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~15 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/evaluation/nnue.rs (1)
131-166: 🧹 Nitpick | 🔵 TrivialEncapsulate the perspective-to-index conversion to prevent silent reordering bugs.
The code uses
perspective as usizedirectly to index two-element arrays (acc,last_seen). This works only becauseColor::White = 0andColor::Black = 1(enforced byprimitive_enum!), but an explicit conversion helper makes the invariant local and exhaustively checked. This pattern appears throughout the codebase; consider applying the same pattern here as a starting point.Proposed fix
impl NeuralNetwork { + fn perspective_index(perspective: Perspective) -> usize { + match perspective { + Color::White => 0, + Color::Black => 1, + } + } + fn set(&mut self, piece: Piece, color: Color, square: Square, perspective: Perspective) { let input_idx = Self::input_index( piece, Self::transformed_color(color, perspective), Self::transformed_square(square, perspective), ); + let perspective_idx = Self::perspective_index(perspective); for i in 0..HIDDEN_LAYER_SIZE { - self.acc[perspective as usize][i] += self.params.acc_weights[i * INPUT_SIZE + input_idx]; + self.acc[perspective_idx][i] += self.params.acc_weights[i * INPUT_SIZE + input_idx]; } } fn clear(&mut self, piece: Piece, color: Color, square: Square, perspective: Perspective) { let input_idx = Self::input_index( piece, Self::transformed_color(color, perspective), Self::transformed_square(square, perspective), ); + let perspective_idx = Self::perspective_index(perspective); for i in 0..HIDDEN_LAYER_SIZE { - self.acc[perspective as usize][i] -= self.params.acc_weights[i * INPUT_SIZE + input_idx]; + self.acc[perspective_idx][i] -= self.params.acc_weights[i * INPUT_SIZE + input_idx]; } } fn forward(&self, perspective: Perspective) -> f64 { let mut eval: f64 = 0.0; + let perspective_idx = Self::perspective_index(perspective); for i in 0..HIDDEN_LAYER_SIZE { - let hidden_out = Self::relu(self.acc[perspective as usize][i] + self.params.acc_biases[i]); + let hidden_out = Self::relu(self.acc[perspective_idx][i] + self.params.acc_biases[i]); eval += hidden_out * self.params.out_weights[i]; } eval + self.params.out_bias @@ fn forward_and_cache(&mut self, position: &Position) -> f64 { let perspective = position.side_to_move(); let res = self.forward(perspective); - self.last_seen[perspective as usize] = *position; + self.last_seen[Self::perspective_index(perspective)] = *position; res } fn evaluate_unscaled(&mut self, position: &Position) -> f64 { let perspective = position.side_to_move(); - let diff = position.diff(&self.last_seen[perspective as usize]); + let diff = position.diff(&self.last_seen[Self::perspective_index(perspective)]);
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: e34dfefd-8cd8-4e39-86d3-7807411a1f80
📒 Files selected for processing (1)
src/evaluation/nnue.rs
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #443 +/- ##
=====================================
- Coverage 92% 91% -0%
=====================================
Files 33 33
Lines 3760 3754 -6
Branches 3760 3754 -6
=====================================
- Hits 3422 3416 -6
Misses 319 319
Partials 19 19 🚀 New features to boost your workflow:
|
|
Against master [hash=64]: 🆗 Elo: 2.78 +/- 15.98, nElo: 3.75 +/- 21.53 |
|
Against v1.6.0 [hash=64]: ✅ Elo: 77.34 +/- 19.23, nElo: 89.60 +/- 21.53 |
Summary by CodeRabbit