registry
optimus_dl.core.registry
¶
Registry system for dependency injection and component management.
This module provides a flexible registry pattern that allows components (models, optimizers, data loaders, etc.) to be registered and instantiated from configuration. The registry system is the foundation of Optimus-DL's modular architecture, enabling easy component swapping and configuration-driven instantiation.
Example
Basic usage:
RegistryConfig
dataclass
¶
Bases: dict[str, Any]
Flexible configuration base class for registry components.
This extends RegistryConfigStrict to allow arbitrary additional fields via dictionary inheritance. Use this when you need custom configuration parameters beyond static fields or for dynamic configurations.
Attributes:
| Name | Type | Description |
|---|---|---|
_name |
str | None
|
The registered name of the component to instantiate. |
extra_fields |
str | None
|
Additional fields can be added as dictionary keys. |
Source code in optimus_dl/core/registry.py
RegistryConfigStrict
dataclass
¶
Strict configuration base class for registry components.
This is a minimal configuration class that only requires a _name field.
Use this when you don't need additional configuration fields.
Attributes:
| Name | Type | Description |
|---|---|---|
_name |
str | None
|
The registered name of the component to instantiate. |
Source code in optimus_dl/core/registry.py
build(registry_name, cfg, cast_to=None, **kwargs)
¶
Build a component from a named registry.
This is a convenience function that builds a component from a registry by name. It's useful when you know the registry name but don't have the registry functions directly available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry_name
|
str
|
Name of the registry to build from (e.g., "model", "optimizer"). |
required |
cfg
|
CorrectCfg | None
|
Configuration object with |
required |
cast_to
|
type[T] | None
|
Optional type to cast the result to. |
None
|
**kwargs
|
Any
|
Additional arguments passed to the component constructor. |
{}
|
Returns:
| Type | Description |
|---|---|
T | Any | None
|
An instance of the registered component. |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the registry doesn't exist, the component name is not found, or if cast_to is provided and the object is not of that type. |
Example
Source code in optimus_dl/core/registry.py
make_registry(registry_name, base_class=None)
¶
Create or retrieve a component registry.
This function creates a new registry or retrieves an existing one. Each registry maintains a mapping of component names to their classes and configuration classes. The registry pattern enables dependency injection and configuration-driven component instantiation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry_name
|
str
|
Unique name for the registry (e.g., "model", "optimizer"). |
required |
base_class
|
type | type[Any] | None
|
Optional base class that all registered components must inherit from. Used for type checking. If None, any class can be registered. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
registry_dict |
dict
|
The actual registry dictionary (for inspection/debugging) |
register |
callable
|
Decorator function to register components |
build |
callable
|
Function to build instances from configuration |
Example
Note:
The registry is stored globally. Multiple calls with the same registry_name
will return the same registry instance.
Source code in optimus_dl/core/registry.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 | |
validate_and_cast(cls, cfg, path='')
¶
Recursively validates and casts config based on cls typings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Any
|
The class or type to validate against. |
required |
cfg
|
Any
|
The configuration object to validate and cast. |
required |
path
|
str
|
Path prefix to use in error messages, indicating the logical location of this config within a larger configuration structure. |
''
|
Returns:
| Type | Description |
|---|---|
Any
|
The casted and validated configuration, as an OmegaConf object if applicable. |