Configuration shared by all models. Specific models should inherit from this class, and implement the `create` method to create the corresponding model.
| 210 | |
| 211 | @dataclasses.dataclass(frozen=True) |
| 212 | class BaseModelConfig(abc.ABC): |
| 213 | """Configuration shared by all models. Specific models should inherit from this class, and implement the `create` |
| 214 | method to create the corresponding model. |
| 215 | """ |
| 216 | |
| 217 | # Action space dimension. |
| 218 | action_dim: int |
| 219 | # Action sequence length. |
| 220 | action_horizon: int |
| 221 | # Tokenized prompt maximum length. |
| 222 | max_token_len: int |
| 223 | |
| 224 | @property |
| 225 | @abc.abstractmethod |
| 226 | def model_type(self) -> ModelType: |
| 227 | """The model type.""" |
| 228 | |
| 229 | @abc.abstractmethod |
| 230 | def create(self, rng: at.KeyArrayLike) -> "BaseModel": |
| 231 | """Create a new model, initializing parameters.""" |
| 232 | |
| 233 | def load(self, params: at.Params, *, remove_extra_params: bool = True) -> "BaseModel": |
| 234 | """Create a model with the given parameters.""" |
| 235 | model = nnx.eval_shape(self.create, jax.random.key(0)) |
| 236 | graphdef, state = nnx.split(model) |
| 237 | if remove_extra_params: |
| 238 | params = ocp.transform_utils.intersect_trees(state.to_pure_dict(), params) |
| 239 | at.check_pytree_equality(expected=state.to_pure_dict(), got=params, check_shapes=True, check_dtypes=False) |
| 240 | state.replace_by_pure_dict(params) |
| 241 | return nnx.merge(graphdef, state) |
| 242 | |
| 243 | def load_pytorch(self, train_config, weight_path: str): |
| 244 | logger.info(f"train_config: {train_config}") |
| 245 | model = pi0_pytorch.PI0Pytorch(config=train_config.model) |
| 246 | safetensors.torch.load_model(model, weight_path) |
| 247 | return model |
| 248 | |
| 249 | @abc.abstractmethod |
| 250 | def inputs_spec(self, *, batch_size: int = 1) -> tuple[Observation, Actions]: |
| 251 | """Returns the input specification for the model. Values are jax.ShapeDtypeStruct.""" |
| 252 | |
| 253 | def fake_obs(self, batch_size: int = 1) -> Observation: |
| 254 | observation_spec, _ = self.inputs_spec(batch_size=batch_size) |
| 255 | return jax.tree.map(lambda x: jnp.ones(x.shape, x.dtype), observation_spec) |
| 256 | |
| 257 | def fake_act(self, batch_size: int = 1) -> Actions: |
| 258 | _, action_spec = self.inputs_spec(batch_size=batch_size) |
| 259 | return jax.tree.map(lambda x: jnp.ones(x.shape, x.dtype), action_spec) |
| 260 | |
| 261 | |
| 262 | @dataclasses.dataclass |
nothing calls this directly
no outgoing calls
no test coverage detected