Conversation
Summary of ChangesHello @samanklesaria, 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 introduces a new convenience method, Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces a convenient split method to the RngStream class, acting as an alias for fork(split=k). My review suggests enhancing this by allowing the split factor k to also be a tuple of integers, to fully align with the fork method's capabilities, and by adding a docstring to improve code clarity and maintainability.
flax/nnx/rnglib.py
Outdated
| def split(self, k: int): | ||
| return self.fork(split=k) |
There was a problem hiding this comment.
This is a great convenience method! To make it even more useful and align it better with the full capabilities of the fork method, consider allowing k to be a tuple of integers as well. This would make split a more complete alias for the splitting functionality of fork.
Additionally, adding a docstring would improve clarity for future users.
def split(self, k: int | tuple[int, ...]):
"""Forks the RngStream into `k` new streams.
This is a convenience method for `self.fork(split=k)`.
Args:
k: The number of new streams to fork. Can be an integer or a tuple of
integers to specify the shape of the split keys.
Returns:
A new `Rngs` object with `k` forked streams.
"""
return self.fork(split=k)|
We should add rngs = Rngs(params=0, dropout=1)
...
rngs = rngs.split(5, only='dropout')
@vmap(in_axes=Rngs.prefix(params=None, dropout=0), ...)
def f(rngs):
...Implementation could look something like: class Rngs:
@classmethod
def prefix(cls, default=None, **kwargs):
if default is not None:
kwargs['default'] = default
rngs = cls() # init empty
for name, value in kwargs.items():
setattr(rngs, name, nnx.data(value)) # set the inputs directly as attributes
return rngs |
@cgarciae It seems like the situation where we want to vmap different parts of a pytree with different batch axes extends beyond Rngs. The most generic way of doing this, as you mentioned before, is split and merge: rngs = nnx.Rngs(params=0, dropout=1)
rngs = rngs.split(5, only='dropout')
graphdef, dropout, params = nnx.split(rngs, 'dropout', ...)
@nnx.vmap(in_axis=(None, 0), ...)
def f(params, dropout):
rngs = nnx.merge(graphdef, params, dropout)
...
f(params, dropout)But I agree that the 'prefix' method you propose above is easier to use. What if we add it to Pytree instead? That way, it could be used for sharing some subset of parameters when vmapping modules as well. |
63ac193 to
647485b
Compare
2c90755 to
8bf1e6d
Compare
8bf1e6d to
7cb1261
Compare
This is a small convenience method for
RngStreams. Specifically,self.split(k) = self.fork(split=k).