Skip to content

Conversation

@ryancinsight
Copy link
Owner

@ryancinsight ryancinsight commented Jan 27, 2026

This PR refactors the TitansMAG forward pass implementation to ensure correctness and maintainability.

Key changes:

  1. State Consistency: Added reset_memory() at the start of forward. In the Titans architecture, the neural memory is typically treated as "fast weights" learned over the context window. The gradient computation (compute_gradients) assumes the memory starts at init_memory. Without an explicit reset in forward, the memory state would persist across calls (e.g., across training batches), leading to a disconnect between the forward pass state and the backward pass assumptions. This fixes potential bugs in training loops.
  2. Code Deduplication: Removed the local mlp_forward helper function in mag.rs and exposed the existing implementation in NeuralMemory (neural.rs) to the crate. This eliminates code duplication.
  3. Verification: Added a regression test test_titans_mag_deterministic_forward to confirm that the module is stateless across calls, as expected for a standard layer in this context. All existing tests passed.

PR created automatically by Jules for task 6565573961390046446 started by @ryancinsight

High-level PR Summary

This PR refactors the TitansMAG forward pass to fix memory state handling and eliminate code duplication. The key fix adds reset_memory() at the start of forward to ensure the memory state is reset between calls, preventing state persistence across training batches that would violate backward pass assumptions. The refactor also removes a duplicated mlp_forward helper function and exposes the existing implementation from NeuralMemory as pub(crate) instead. A new test verifies that forward passes are now deterministic across calls.

⏱️ Estimated Review Time: 5-15 minutes

💡 Review Order Suggestion
Order File Path
1 src/memory/titans/neural.rs
2 src/memory/titans/mag.rs

Need help? Join our Discord

- Added `self.memory.reset_memory()` to `TitansMAG::forward` to ensure consistent state initialization, matching the assumption in `compute_gradients` that execution starts from `init_memory`. This prevents state leakage across independent forward calls (e.g., between batches).
- Refactored `src/memory/titans/mag.rs` to use `NeuralMemory::mlp_forward` instead of a duplicated local helper function, improving code maintainability.
- Exposed `NeuralMemory::mlp_forward` as `pub(crate)` in `src/memory/titans/neural.rs` to support the refactoring.
- Added `test_titans_mag_deterministic_forward` to verify that repeated forward passes on the same input produce identical outputs, confirming the state reset logic works.

Co-authored-by: ryancinsight <55164720+ryancinsight@users.noreply.github.com>
@google-labs-jules
Copy link

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai
Copy link

coderabbitai bot commented Jan 27, 2026

Warning

Rate limit exceeded

@ryancinsight has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 11 minutes and 8 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link

Summary of Changes

Hello @ryancinsight, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the TitansMAG module to enhance its correctness and maintainability. It addresses a critical issue where neural memory state could persist across forward passes, leading to inconsistencies between forward and backward computations, by implementing an explicit memory reset. Additionally, it improves code hygiene by centralizing the mlp_forward function and includes a new test to validate the module's deterministic behavior.

Highlights

  • Memory State Handling: The TitansMAG::forward method now explicitly calls self.memory.reset_memory() at its beginning. This ensures that the neural memory state does not persist across different forward passes (e.g., training batches), aligning the forward pass behavior with the backward pass assumptions and preventing potential training bugs.
  • MLP Forward Function Refactoring: The mlp_forward helper function, previously defined locally within src/memory/titans/mag.rs, has been removed. Instead, the existing mlp_forward method in NeuralMemory (from src/memory/titans/neural.rs) has been made pub(crate) and is now used directly, eliminating redundant code.
  • Added Deterministic Forward Pass Test: A new regression test, test_titans_mag_deterministic_forward, has been added to src/memory/titans/mag.rs. This test verifies that the TitansMAG module's forward pass is deterministic and stateless across calls, confirming the intended behavior after the memory reset.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@recurseml recurseml bot left a comment

Choose a reason for hiding this comment

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

Review by RecurseML

🔍 Review performed on a004bfd..7f24bef

✨ No bugs found, your code is sparkling clean

✅ Files analyzed, no issues (2)

src/memory/titans/mag.rs
src/memory/titans/neural.rs

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a critical fix to the TitansMAG forward pass by ensuring the memory state is reset between calls. This prevents state leakage across batches and aligns the forward pass with the assumptions of the backward pass, resolving a potential bug in training loops. The changes also include a valuable refactoring that removes duplicated code by centralizing the mlp_forward logic into the NeuralMemory implementation. A new regression test has been added to verify that the forward pass is now deterministic. The changes are well-implemented, clearly explained, and improve both the correctness and maintainability of the code.

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.

2 participants