Skip to content

Commit 14c134c

Browse files
authored
docs: configurator (plus removing dead code) (#226)
--------- Signed-off-by: Nina Xu <19981858+nina-xu@users.noreply.github.com>
1 parent 30ed5c2 commit 14c134c

9 files changed

Lines changed: 201 additions & 615 deletions

File tree

src/nemo_safe_synthesizer/config/autoconfig.py

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@
1111

1212
from __future__ import annotations
1313

14-
import inspect
1514
import math
1615
import re
17-
from dataclasses import dataclass
18-
from typing import TYPE_CHECKING, Any, Callable
16+
from typing import TYPE_CHECKING, Any
1917

2018
import pandas as pd
21-
from pydantic import GetCoreSchemaHandler
22-
from pydantic_core import core_schema
2319

2420
from ..defaults import DEFAULT_MAX_SEQ_LENGTH, MAX_ROPE_SCALING_FACTOR
2521
from ..llm.metadata import ModelMetadata
@@ -353,48 +349,3 @@ def resolve(self) -> SafeSynthesizerParameters:
353349
privacy_params.update(self._determine_delta())
354350

355351
return self._build_updated_params(training_params, data_params, privacy_params)
356-
357-
358-
@dataclass(frozen=True)
359-
class AutoParamsValidator:
360-
"""Pydantic validator that passes ``"auto"`` strings through unchanged.
361-
362-
For non-``"auto"`` values, delegates to ``value_func`` for validation.
363-
Used as a field-level validator on parameters that accept either a concrete
364-
value or the ``"auto"`` sentinel.
365-
"""
366-
367-
value_func: Callable[[Any], bool]
368-
"""Predicate that returns ``True`` if the value is acceptable."""
369-
370-
def validate(self, value, _info):
371-
"""Pass ``"auto"`` through unchanged; otherwise delegate to ``value_func``.
372-
373-
Args:
374-
value: The raw field value to validate.
375-
_info: Pydantic validation context (unused).
376-
377-
Returns:
378-
The validated value, or ``"auto"`` for deferred resolution by
379-
:class:`AutoConfigResolver`.
380-
381-
Raises:
382-
ValueError: If ``value`` is not ``"auto"`` and ``value_func``
383-
rejects it.
384-
"""
385-
if isinstance(value, str) and value == "auto":
386-
return value
387-
elif self.value_func(value):
388-
return value
389-
else:
390-
raise ValueError(f"AutoParam validation failed: {inspect.getsource(self.value_func)}, got {value}")
391-
392-
def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
393-
"""Register :meth:`validate` as a Pydantic after-validator.
394-
395-
Wraps the base schema from ``handler`` so that :meth:`validate`
396-
runs after normal type coercion.
397-
"""
398-
return core_schema.with_info_after_validator_function(
399-
self.validate, handler(source_type), field_name=handler.field_name
400-
)
Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,2 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
3-
4-
"""
5-
Configurator: A mini-library for Pydantic-based configuration systems with strong typing support.
6-
7-
This package provides a comprehensive set of tools for building type-safe, validated
8-
configuration systems using Pydantic v2. It's designed for developers who need
9-
sophisticated parameter management with excellent static analysis support.
10-
11-
Key Components:
12-
13-
Parameter Wrappers (parameter.py):
14-
- Parameter: Generic wrapper for configuration values with Pydantic integration
15-
- AutoParam: Parameter with automatic type-safe update capabilities
16-
- UnsetParam: For handling unset/default parameter states
17-
- ValidNoneParam: For parameters where None is a valid value
18-
19-
Validation & Collections (parameters.py):
20-
- Parameters: Abstract base class for organizing parameter collections
21-
- DependsOnValidator: Conditional field validation based on other fields
22-
- ValueValidator: Custom validation functions for parameter values
23-
- YAML serialization support for configuration persistence
24-
25-
Usage Examples:
26-
27-
Basic Parameter Usage:
28-
>>> from nemo_safe_synthesizer.configurator.parameter import Parameter, AutoParam
29-
>>>
30-
>>> # Simple parameter
31-
>>> max_size = Parameter(name="max_size", value=1000)
32-
>>>
33-
>>> # Auto-updating parameter
34-
>>> batch_size = AutoParam(name="batch_size", value=32)
35-
>>> batch_size.autoupdate(64) # Safe type-checked update
36-
37-
Parameter Collections:
38-
>>> from nemo_safe_synthesizer.configurator.parameters import Parameters
39-
>>> from nemo_safe_synthesizer.configurator.parameter import AutoParam
40-
>>> from typing import Annotated
41-
>>> from pydantic import Field
42-
>>> class DatabaseConfig(Parameters):
43-
... host: Annotated[AutoParam[str], Field(default=AutoParam(name="host", value="localhost")]
44-
... port: Annotated[AutoParam[int], Field(default=AutoParam(name="port", value=5432)]
45-
>>>
46-
>>> config = DatabaseConfig()
47-
>>> config.host
48-
AutoParam(name='host', value='localhost')
49-
50-
Conditional Validation:
51-
>>> from nemo_safe_synthesizer.configurator.parameters import Parameters
52-
>>> from nemo_safe_synthesizer.configurator.validators import DependsOnValidator
53-
>>> from nemo_safe_synthesizer.configurator.parameter import AutoParam
54-
>>> from typing import Annotated
55-
>>>
56-
>>> class AdvancedConfig(Parameters):
57-
... debug_mode: bool = False
58-
... verbose_logging: Annotated[
59-
... Parameter[bool],
60-
... DependsOnValidator(depends_on="debug_mode", depends_on_func=lambda x: x is True),
61-
... Field(default=Parameter(name="verbose_logging", value=False)),
62-
... ]
63-
"""

0 commit comments

Comments
 (0)