You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working with a modular deep learning codebase using flax/NNX where I have a generic RNN model that can accept different types of RNN cells. I need to be able to configure both:
The specific RNN cell class to use (which should remain uninstantiated)
The arguments for that cell class
While maintaining the ability to easily swap between different cell types via hydra's configuration system.
Here's a simplified version demonstrating the core challenge:
# Base cell interfaceclassRNNCell(nnx.Module):
definitialize_carry(self, input_shape: tuple[int, ...], rngs: Optional[nnx.Rngs] =None) ->Any:
raiseNotImplementedError()
def__call__(self, carry: Any, inputs: jax.Array) ->tuple[Any, jax.Array]:
raiseNotImplementedError()
classSimpleRNNCell(RNNCell):
def__init__(self, in_features: int, hidden_features: int, rngs: nnx.Rngs):
...
classLSTMCell(RNNCell):
def__init__(self, in_features: int, hidden_features: int, rngs: nnx.Rngs):
...
# The RNN module that needs to be configuredclassRNN(nnx.Module):
def__init__(
self,
cell_type: Type[RNNCell], # Should be uninstantiated classcell_kwargs: dict[str, Any] |None=None,
*,
rngs: Optional[nnx.Rngs] =None,
):
self.cell=cell_type(rngs=rngs, **cell_kwargs)
...
My attempt at configuration:
fromdataclassesimportMISSINGfromhydra_zenimportbuilds, hydrated_dataclass, instantiate, store, zen@hydrated_dataclass(target=dict)classSimpleRNNCellArgs:
in_features: int=MISSINGhidden_features: int=MISSING@hydrated_dataclass(target=dict)classLSTMCellArgs:
in_features: int=MISSINGhidden_features: int=MISSING# Store cell configsstore(SimpleRNNCellArgs, group="cell_type_args", name="simple_rnn_args")
store(LSTMCellArgs, group="cell_type_args", name="lstm_args")
store(SimpleRNNCell, group="cell", name="simple_rnn")
store(LSTMCell, group="cell", name="lstm")
# Define RNN config that should compose with a cell config@hydrated_dataclass(target=dict)classRNNArgs:
"""Config for RNN that should compose with a cell"""cell_type: Union[SimpleRNNCell, LSTMCell] =MISSINGcell_kwargs: Union[SimpleRNNCellArgs, LSTMCellArgs] =MISSINGstore(
RNNArgs,
group="models",
name="rnn",
hydra_defaults=["_self_", dict(cell_type="simple_rnn"), dict(cell_kwargs="simple_rnn_args")],
)
deftask(model: RNNArgs):
"""Task showing challenge with instantiation"""config=instantiate(model)
print(f"Model Args: {config}")
# The challenge: How do we properly compose these so that:# 1. The cell_type remains uninstantiated until needed# 2. The cell kwargs are properly passed throughcell=config["cell_type"]
cell_kwargs=config["cell_kwargs"]
# Goal: Be able to do something like:# rnn = RNN(cell_type=cell, cell_kwargs=cell_kwargs)TaskConfig=builds(
task, populate_full_signature=True, hydra_defaults=["_self_", dict(models="rnn")]
)
store(TaskConfig, name="my_task")
if__name__=="__main__":
store.add_to_hydra_store()
zen(task).hydra_main(version_base="1.3", config_name="my_task", config_path=None)
The Challenge
The key challenges I'm running into are:
How to properly store and reference the uninstantiated cell classes (SimpleRNNCell, LSTMCell) in the config system
How to properly compose the cell class with its arguments in a way that:
Keeps the cell class uninstantiated until needed
Correctly passes through the cell's configuration parameters
Allows easy swapping between different cell types via hydra's config system
How to structure the configs to maintain type safety and configuration validation
I'm unsure how to properly:
Keep the cell_type as an uninstantiated class while properly passing its arguments
Structure the configs to make cell types cleanly swappable
Maintain proper typing throughout
What's the recommended pattern in hydra-zen for this type of hierarchical config where we need to compose uninstantiated classes with their arguments?
Thanks in advance. This has been boggling my mind so any help is appreciated.
The text was updated successfully, but these errors were encountered:
The Problem
I'm working with a modular deep learning codebase using flax/NNX where I have a generic RNN model that can accept different types of RNN cells. I need to be able to configure both:
While maintaining the ability to easily swap between different cell types via hydra's configuration system.
Here's a simplified version demonstrating the core challenge:
My attempt at configuration:
The Challenge
The key challenges I'm running into are:
I'm unsure how to properly:
What's the recommended pattern in hydra-zen for this type of hierarchical config where we need to compose uninstantiated classes with their arguments?
Thanks in advance. This has been boggling my mind so any help is appreciated.
The text was updated successfully, but these errors were encountered: