Wrapper class to expose the configuration in a read-only manner, maintaining backward compatibility with attribute-style access and JAX object types.
| 148 | |
| 149 | |
| 150 | class HyperParameters: |
| 151 | """ |
| 152 | Wrapper class to expose the configuration in a read-only manner, |
| 153 | maintaining backward compatibility with attribute-style access and JAX object types. |
| 154 | """ |
| 155 | |
| 156 | def __init__(self, pydantic_config: types.MaxTextConfig): |
| 157 | object.__setattr__(self, "_pydantic_config", pydantic_config) |
| 158 | |
| 159 | final_dict = pydantic_config.model_dump() |
| 160 | final_dict["dtype"] = jnp.dtype(final_dict["dtype"]) |
| 161 | final_dict["grad_dtype"] = jnp.dtype(final_dict["grad_dtype"]) |
| 162 | final_dict["weight_dtype"] = jnp.dtype(final_dict["weight_dtype"]) |
| 163 | final_dict["mu_dtype"] = ( |
| 164 | final_dict["weight_dtype"] if not final_dict["mu_dtype"] else jnp.dtype(final_dict["mu_dtype"]) |
| 165 | ) |
| 166 | |
| 167 | final_dict["logical_axis_rules"] = _lists_to_tuples(final_dict["logical_axis_rules"]) |
| 168 | final_dict["data_sharding"] = _lists_to_tuples(final_dict["data_sharding"]) |
| 169 | |
| 170 | final_dict["decoder_block"] = DecoderBlockType(final_dict["decoder_block"]) |
| 171 | final_dict["shard_mode"] = ShardMode(final_dict["shard_mode"]) |
| 172 | |
| 173 | object.__setattr__(self, "_flat_config", final_dict) |
| 174 | |
| 175 | def __deepcopy__(self, memo): |
| 176 | new_pydantic_config = copy.deepcopy(self._pydantic_config, memo) |
| 177 | return HyperParameters(new_pydantic_config) |
| 178 | |
| 179 | def tree_flatten(self): |
| 180 | return (), self |
| 181 | |
| 182 | def __getattr__(self, attr: str) -> Any: |
| 183 | """Provides attribute-style access to the final configuration dictionary.""" |
| 184 | # Use object.__getattribute__ to avoid recursion when accessing _flat_config |
| 185 | # This is necessary for proper pickling/unpickling support |
| 186 | flat_config = object.__getattribute__(self, "_flat_config") |
| 187 | if attr in flat_config: |
| 188 | return flat_config[attr] |
| 189 | raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{attr}'") |
| 190 | |
| 191 | def __setattr__(self, attr: str, value: Any) -> None: |
| 192 | """Makes the configuration object read-only.""" |
| 193 | raise ValueError("Configuration is read-only and cannot be modified after initialization.") |
| 194 | |
| 195 | def get_keys(self) -> dict[str, Any]: |
| 196 | """Returns the configuration as a flat dictionary for backward compatibility.""" |
| 197 | return self._flat_config |
| 198 | |
| 199 | |
| 200 | def initialize(argv: list[str], **kwargs) -> HyperParameters: |
no outgoing calls
no test coverage detected