import contextlib
import copy
import functools
import hashlib
import importlib
import inspect
import io
import keyword
import math
import os
import pickle
import tokenize
import unittest
from collections.abc import Callable
from contextvars import ContextVar
from dataclasses import dataclass
from graphlib import CycleError, TopologicalSorter
from types import FunctionType, ModuleType, UnionType
from typing import (
    Any,
    Generic,
    get_args,
    get_origin,
    get_type_hints,
    Literal,
    NoReturn,
    Optional,
    TYPE_CHECKING,
    TypeVar,
    Union,
)
from typing_extensions import deprecated

from torch._utils_internal import justknobs_check


# Types saved/loaded in configs
CONFIG_TYPES = (int, float, bool, type(None), str, list, set, tuple, dict)

# Immutable scalar types that don't need deepcopy when returned from configs.
# Everything else is defensively copied to prevent accidental mutation.
_IMMUTABLE_CONFIG_TYPES = (int, float, bool, type(None), str, tuple)


# Duplicated, because mypy needs these types statically
T = TypeVar("T", bound=int | float | bool | str | list | set | tuple | dict | None)


_UNSET_SENTINEL = object()


@dataclass(kw_only=True)
class _Config(Generic[T]):
    r"""Represents a config with richer behaviour than just a default value.
    ::
        i.e.
        foo = Config(justknob="//foo:bar", default=False)
        install_config_module(...)

    This configs must be installed with install_config_module to be used

    Precedence Order:
        alias: If set, the directly use the value of the alias.
        implies: An active implication overrides the target's ordinary value.
        env_name_force: If set, this environment variable has precedence over
            everything after this.
            If multiple env variables are given, the precedence order is from
            left to right.
        user_override: If a user sets a value (i.e. foo.bar=True), that
            has precedence over everything after this.  User overrides are thread-local.
        env_name_default: If set, this environment variable will override everything
            after this.
            If multiple env variables are given, the precedence order is from
            left to right.
        justknob: If this pytorch installation supports justknobs, that will
            override defaults, but will not override the user_override precedence.
        default: This value is the lowest precedence, and will be used if nothing is
            set.

    Environment Variables:
        These are interpreted to be either "0" or "1" to represent true and false.

    Arguments:
        justknob: the name of the feature / JK. In OSS this is unused.
        default: is the value to default this knob to in OSS.
        alias: The alias config to read instead.
        env_name_force: The environment variable, or list of, to read that is a FORCE
            environment variable. It overrides ordinary values, but not implications.
        env_name_default: The environment variable, or list of, to read that changes the
            default behaviour. I.e. user overrides take preference.
        implies: Maps source values to forced values for other settings in this
            module, using dotted names for nested targets. Sources and targets
            must have bool, int, str, None, or corresponding Literal/union types.
            Aliases, cycles, and conflicting active implications are rejected.
            Assignments update stored values without overriding active implications.
            Use ``config.patch`` for temporary changes that restore stored values.
            ``mock.patch.object`` is unsupported on targets with active implications.
    """

    default: T | object
    justknob: str | None = None
    env_name_default: list[str] | None = None
    env_name_force: list[str] | None = None
    value_type: type | None = None
    alias: str | None = None
    implies: dict[Any, dict[str, Any]] | None = None
    # Deprecation support
    deprecated: bool = False
    deprecation_message: str | None = None

    def __post_init__(self) -> None:
        self.env_name_default = _Config.string_or_list_of_string_to_list(
            self.env_name_default
        )
        self.env_name_force = _Config.string_or_list_of_string_to_list(
            self.env_name_force
        )
        if self.implies is not None and not isinstance(self.implies, dict):
            raise TypeError("implies must be a dictionary")

        if self.alias is not None:
            if (
                self.default is not _UNSET_SENTINEL
                or self.justknob is not None
                or self.env_name_default is not None
                or self.env_name_force is not None
                or self.implies is not None
            ):
                raise AssertionError(
                    "if alias is set, none of {default, justknob, \
                        env_name_default, env_name_force and implies} can be set"
                )

    @staticmethod
    def string_or_list_of_string_to_list(
        val: str | list[str] | None,
    ) -> list[str] | None:
        if val is None:
            return None
        if isinstance(val, str):
            return [val]
        if not isinstance(val, list):
            raise AssertionError(f"val is not a list, got {type(val)}")
        return val


# In runtime, we unbox the Config[T] to a T, but typechecker cannot see this,
# so in order to allow for this dynamic behavior to work correctly with
# typechecking we are going to lie to the typechecker that Config[T] returns
# a T.
if TYPE_CHECKING:

    def Config(
        default: T | object = _UNSET_SENTINEL,
        justknob: str | None = None,
        env_name_default: str | list[str] | None = None,
        env_name_force: str | list[str] | None = None,
        value_type: type | None = None,
        alias: str | None = None,
        # Deprecation support
        deprecated: bool = False,
        deprecation_message: str | None = None,
        *,
        implies: dict[Any, dict[str, Any]] | None = None,
    ) -> T: ...

else:

    def Config(
        default: T | object = _UNSET_SENTINEL,
        justknob: str | None = None,
        env_name_default: str | list[str] | None = None,
        env_name_force: str | list[str] | None = None,
        value_type: type | None = None,
        alias: str | None = None,
        # Deprecation support
        deprecated: bool = False,
        deprecation_message: str | None = None,
        *,
        implies: dict[Any, dict[str, Any]] | None = None,
    ) -> _Config[T]:
        return _Config(
            default=default,
            justknob=justknob,
            env_name_default=env_name_default,
            env_name_force=env_name_force,
            value_type=value_type,
            alias=alias,
            # Deprecation support
            deprecated=deprecated,
            deprecation_message=deprecation_message,
            implies=implies,
        )


def _read_env_variable(name: str) -> bool | str | None:
    value = os.environ.get(name)
    if value == "1":
        return True
    if value == "0":
        return False
    return value


def install_config_module(module: ModuleType) -> None:
    """
    Converts a module-level config into a `ConfigModule()`.

    See _config_typing.pyi for instructions on how to get the converted module to typecheck.
    """

    class ConfigModuleInstance(ConfigModule):
        # __annotations__ is written to by Sphinx autodoc
        _bypass_keys = {
            "_hash_dirty_var",
            "_hash_cache_var",
            "_get_dict_dirty_keys_var",
            "_get_dict_cache_var",
            "__annotations__",
        }

    def visit(
        source: ModuleType | type,
        dest: ModuleType | SubConfigProxy,
        prefix: str,
    ) -> None:
        """Walk the module structure and move everything to module._config"""
        type_hints = inspect.get_annotations(source)
        namespace = dict(vars(source))
        namespaces[prefix] = namespace
        for key, value in namespace.items():
            if (
                key.startswith("__")
                or isinstance(value, (ModuleType, FunctionType))
                or (
                    hasattr(value, "__module__")
                    and (
                        value.__module__ == "typing"
                        or value.__module__.startswith("collections.abc")
                    )
                )
                # Handle from torch.utils._config_module import Config
                or (isinstance(value, type) and issubclass(value, _Config))
            ):
                continue

            name = f"{prefix}{key}"
            annotated_type = type_hints.get(key, None)
            if isinstance(value, CONFIG_TYPES):
                config[name] = _ConfigEntry(
                    _Config(default=value, value_type=annotated_type), name
                )
                if dest is module:
                    delattr(module, key)
            elif isinstance(value, _Config):
                if annotated_type is not None and value.value_type is None:
                    value.value_type = annotated_type

                config[name] = _ConfigEntry(value, name)

                if dest is module:
                    delattr(module, key)
            elif isinstance(value, type):
                if value.__module__ != module.__name__:
                    raise AssertionError(
                        f"subconfig class {value} must be defined in module {module.__name__}"
                    )
                # a subconfig with `class Blah:` syntax
                proxy = SubConfigProxy(module, f"{name}.")
                visit(value, proxy, f"{name}.")
                if dest is module:
                    setattr(dest, key, proxy)
                else:
                    dest.__dict__[key] = proxy
            else:
                raise AssertionError(f"Unhandled config {key}={value} ({type(value)})")

    config: dict[str, _ConfigEntry] = {}
    namespaces: dict[str, dict[str, Any]] = {}

    compile_ignored_keys = get_assignments_with_compile_ignored_comments(module)

    visit(module, module, "")
    module._config = config  # type: ignore[attr-defined]
    module._compile_ignored_keys = compile_ignored_keys  # type: ignore[attr-defined]
    if any(entry.implies for entry in config.values()):

        class ImplicationConfigModuleInstance(_ImplicationConfigModule):
            _bypass_keys = ConfigModuleInstance._bypass_keys

        implications, sources = _install_implications(config, namespaces)
        module._implications = implications  # type: ignore[attr-defined]
        module._implication_sources = sources  # type: ignore[attr-defined]
        module._implication_dynamic_sources = {  # type: ignore[attr-defined]
            name for name in sources if config[name].justknob is not None
        }
        module._implication_hash = repr(sorted(implications.items())).encode()  # type: ignore[attr-defined]
        module.__class__ = ImplicationConfigModuleInstance
    else:
        module.__class__ = ConfigModuleInstance
    module._hash_dirty_var = ContextVar(f"{module.__name__}._hash_dirty", default=True)  # type: ignore[attr-defined]  # pyrefly: ignore[missing-attribute]
    module._hash_cache_var = ContextVar(  # pyrefly: ignore[missing-attribute]
        f"{module.__name__}._hash_cache", default=None
    )  # type: ignore[attr-defined]
    module._get_dict_dirty_keys_var = ContextVar(  # pyrefly: ignore[missing-attribute]
        f"{module.__name__}._get_dict_dirty_keys", default=None
    )  # type: ignore[attr-defined]
    module._get_dict_cache_var = ContextVar(  # pyrefly: ignore[missing-attribute]
        f"{module.__name__}._get_dict_cache", default=None
    )  # type: ignore[attr-defined]


COMPILE_IGNORED_MARKER = "@compile_ignored"


# Gets all the keys (i.e. assignments) with a @compile_ignored comment
def get_assignments_with_compile_ignored_comments(module: ModuleType) -> set[str]:
    source_code = inspect.getsource(module)
    assignments = set()

    # Tokenize the source code to retrieve comments
    tokens = tokenize.tokenize(io.BytesIO(source_code.encode("utf-8")).readline)
    current_comment = "", -1
    prev_name = ""

    for token in tokens:
        if token.type == tokenize.COMMENT:
            prev_name = ""
            maybe_current = token.string.strip()
            if COMPILE_IGNORED_MARKER in maybe_current:
                if current_comment != ("", -1):
                    raise AssertionError(f"unconsumed {COMPILE_IGNORED_MARKER}")
                current_comment = maybe_current, token.start[0]
        elif token.type == tokenize.NAME:
            # Only accept the first name token, to handle if you have
            # something like foo: Bar = ...
            if not prev_name:
                prev_name = token.string
        elif token.type == tokenize.OP and token.string == "=":
            # Check if the current assignment follows a comment
            # with COMPILE_IGNORED_MARKER
            if (
                COMPILE_IGNORED_MARKER in current_comment[0]
                and current_comment[1] == token.start[0] - 1
            ):
                assignments.add(prev_name)
                current_comment = "", -1  # reset
            prev_name = ""
    if current_comment != ("", -1):
        raise AssertionError(f"unconsumed {COMPILE_IGNORED_MARKER}")
    return assignments


_GetDictCacheKey = tuple[tuple[str, ...], tuple[str, ...], bool]
_ResolvedConfigChanges = tuple[list[tuple[ModuleType, str, Any]], set["ConfigModule"]]


@dataclass
class _ConfigEntry:
    # The default value specified in the configuration
    default: Any
    # The type of the configuration value
    value_type: type
    # The value specified by the user when they overrode the configuration
    # _UNSET_SENTINEL indicates the value is not set.
    user_override: ContextVar[object]
    # The justknob to check for this config
    justknob: str | None = None
    # environment variables are read at install time
    env_value_force: Any = _UNSET_SENTINEL
    env_value_default: Any = _UNSET_SENTINEL
    # Used to work around bad assumptions in unittest.mock.patch
    # The code to blame is
    # https://github.com/python/cpython/blob/94a7a4e22fb8f567090514785c69e65298acca42/Lib/unittest/mock.py#L1637
    # Essentially, mock.patch requires, that if __dict__ isn't accessible
    # (which it isn't), that after delattr is called on the object, the
    # object must throw when hasattr is called. Otherwise, it doesn't call
    # setattr again.
    # Technically we'll have an intermediate state of hiding the config while
    # mock.patch is unpatching itself, but it calls setattr after the delete
    # call so the final state is correct. It's just very unintuitive.
    # upstream bug - python/cpython#126886
    hide: bool = False
    alias: str | None = None
    implies: dict[Any, dict[str, Any]] | None = None
    # Deprecation support
    deprecated: bool = False
    deprecation_message: str | None = None
    _deprecation_warned: bool = False

    def __init__(self, config: _Config, name: str) -> None:
        self.default = config.default
        self.value_type = (
            config.value_type if config.value_type is not None else type(self.default)
        )
        self.justknob = config.justknob
        self.alias = config.alias
        self.implies = config.implies
        # Deprecation fields
        self.deprecated = config.deprecated
        self.deprecation_message = config.deprecation_message
        self._deprecation_warned = False

        self.user_override = ContextVar(name, default=_UNSET_SENTINEL)
        if config.env_name_default is not None:
            for val in config.env_name_default:
                if (env_value := _read_env_variable(val)) is not None:
                    self.env_value_default = env_value
                    break
        if config.env_name_force is not None:
            for val in config.env_name_force:
                if (env_value := _read_env_variable(val)) is not None:
                    self.env_value_force = env_value
                    break

        # Ensure justknobs and envvars are allowlisted types
        if self.justknob is not None and self.default is not None:
            if not isinstance(self.default, bool):
                raise AssertionError(
                    f"justknobs only support booleans, {self.default} is not a boolean"
                )
        if self.value_type is not None and (
            config.env_name_default is not None or config.env_name_force is not None
        ):
            if self.value_type not in (
                bool,
                str,
                Optional[bool],  # noqa: UP045
                Optional[str],  # noqa: UP045
            ):
                raise AssertionError(
                    f"envvar configs only support (optional) booleans or strings, {self.value_type} is neither"
                )


def _matches_implication_type(value: object, value_type: Any) -> bool:
    if type(value) not in (bool, int, str, type(None)):
        return False
    origin = get_origin(value_type)
    if origin is Literal:
        return any(type(value) is type(v) and value == v for v in get_args(value_type))
    if origin in (Union, UnionType):
        return any(_matches_implication_type(value, t) for t in get_args(value_type))
    return type(value) is value_type


def _validate_implication_source(name: str, value: object) -> None:
    if type(value) not in (bool, int, str, type(None)):
        raise TypeError(
            f"implication source {name} requires a plain bool, int, str, or None; got {type(value).__name__}"
        )


def _install_implications(
    config: dict[str, _ConfigEntry],
    namespaces: dict[str, dict[str, Any]],
) -> tuple[dict[str, tuple[tuple[str, Any, Any], ...]], set[str]]:
    def resolve_type(name: str) -> Any:
        entry = config[name]
        if isinstance(entry.value_type, str):
            annotations = ModuleType("_config_annotations")
            annotations.__annotations__ = {"value": entry.value_type}
            prefix = name[: name.rfind(".") + 1]
            entry.value_type = get_type_hints(
                annotations, namespaces[""], namespaces[prefix]
            )["value"]
        return entry.value_type

    implications: dict[str, list[tuple[str, Any, Any]]] = {}
    edges: dict[str, set[str]] = {}
    for source, entry in config.items():
        if not entry.implies:
            continue
        _validate_implication_source(source, entry.default)
        for value in (entry.env_value_default, entry.env_value_force):
            if value is not _UNSET_SENTINEL:
                _validate_implication_source(source, value)
        edges[source] = set()
        for condition, targets in entry.implies.items():
            if not _matches_implication_type(condition, resolve_type(source)):
                raise TypeError(
                    f"invalid implication condition {condition!r} for {source}: expected {entry.value_type}"
                )
            if not isinstance(targets, dict):
                raise TypeError(
                    f"implication targets for {source}={condition!r} must be a dictionary"
                )
            for target, value in targets.items():
                if not isinstance(target, str):
                    raise TypeError("implication targets must be dotted config names")
                if target not in config:
                    raise ValueError(
                        f"implication target {target!r} does not exist in this config module"
                    )
                target_entry = config[target]
                if target_entry.alias is not None:
                    raise ValueError(f"implication target {target} cannot be an alias")
                if not _matches_implication_type(value, resolve_type(target)):
                    raise TypeError(
                        f"invalid implied value {value!r} for {target}: expected {target_entry.value_type}"
                    )
                edges[source].add(target)
                implications.setdefault(target, []).append((source, condition, value))

    try:
        TopologicalSorter(edges).prepare()
    except CycleError as exc:
        raise ValueError(f"config implication cycle: {exc.args[1]}") from exc
    return {
        key: tuple(sorted(rules, key=repr)) for key, rules in implications.items()
    }, set(edges)


class ConfigModule(ModuleType):
    # NOTE: This should be kept in sync with _config_typing.pyi.

    # The actual configuration settings.  E.g., torch._dynamo.config.debug
    # would live as "debug" in the key, and torch._inductor.config.triton.cudagraphs
    # maps as "triton.cudagraphs". See discussion on the class for meaning of various sub items
    _config: dict[str, _ConfigEntry]
    _bypass_keys: set[str]
    _compile_ignored_keys: set[str]
    _hash_dirty_var: ContextVar[bool]
    _hash_cache_var: ContextVar[bytes | None]
    # Per-thread cache state, backed by ContextVar so each thread/context gets
    # its own dirty set and cache (config values are per-thread via ContextVar).
    # None means fully dirty (initial state or >_GET_DICT_DIRTY_KEYS_CAP keys
    # changed); empty set means the last _get_dict result is up to date.
    _get_dict_dirty_keys_var: ContextVar[set[str] | None]
    _get_dict_cache_var: ContextVar[dict[_GetDictCacheKey, dict[str, Any]]]

    def __init__(self) -> None:
        raise NotImplementedError(
            f"use {__name__}.install_config_module(sys.modules[__name__])"
        )

    def _warn_if_deprecated(self, name: str, config: _ConfigEntry) -> None:
        """Issue deprecation warning for config if not already warned."""
        if config.deprecated and not config._deprecation_warned:
            import warnings

            msg = f"{self.__name__}.{name} is deprecated"
            if config.deprecation_message:
                msg += f" and {config.deprecation_message}"
            msg += ". It will be removed in a future version of PyTorch."
            warnings.warn(msg, FutureWarning, stacklevel=3)
            config._deprecation_warned = True

    def __setattr__(self, name: str, value: object) -> None:
        if name in self._bypass_keys:
            super().__setattr__(name, value)
        elif name not in self._config:
            raise AttributeError(f"{self.__name__}.{name} does not exist")
        else:
            # Issue deprecation warning on write (once per config)
            config = self._config[name]
            self._warn_if_deprecated(name, config)

            if config.alias is not None:
                self._set_alias_val(config, value)
            else:
                config.user_override.set(value)
                self._hash_dirty_var.set(True)
                self._mark_get_dict_dirty(name)
                # Avoid a redundant instance-__dict__ write: hide defaults to False on
                # the class and is only ever set True by __delattr__ (the mock.patch
                # workaround), so only clear it when it is actually set.
                if config.hide:
                    config.hide = False

    def __getattr__(self, name: str) -> Any:
        try:
            config = self._config[name]

            if config.hide:
                raise AttributeError(f"{self.__name__}.{name} does not exist")

            # Issue deprecation warning on read (once per config)
            self._warn_if_deprecated(name, config)

            alias_val = self._get_alias_val(config)
            if alias_val is not _UNSET_SENTINEL:
                return alias_val

            if config.env_value_force is not _UNSET_SENTINEL:
                return config.env_value_force

            user_override = config.user_override.get()
            if user_override is not _UNSET_SENTINEL:
                return user_override

            if config.env_value_default is not _UNSET_SENTINEL:
                return config.env_value_default

            if config.justknob is not None:
                # JK only supports bools and ints
                return justknobs_check(name=config.justknob, default=config.default)

            # Reference types can still be modified, so copy them to
            # user_overrides to prevent accidental mutation of defaults.
            if not isinstance(config.default, _IMMUTABLE_CONFIG_TYPES):
                config.user_override.set(copy.deepcopy(config.default))
                return config.user_override.get()
            return config.default

        except KeyError as e:
            # make hasattr() work properly
            raise AttributeError(f"{self.__name__}.{name} does not exist") from e

    def __delattr__(self, name: str) -> None:
        self._hash_dirty_var.set(True)
        self._mark_get_dict_dirty(name)
        # must support delete because unittest.mock.patch deletes
        # then recreate things
        self._config[name].user_override.set(_UNSET_SENTINEL)
        self._config[name].hide = True

    def _get_alias_module_and_name(
        self, entry: _ConfigEntry
    ) -> tuple[ModuleType, str] | None:
        alias = entry.alias
        if alias is None:
            return None
        module_name, constant_name = alias.rsplit(".", 1)
        try:
            module = importlib.import_module(module_name)
        except ImportError as e:
            raise AttributeError(f"config alias {alias} does not exist") from e
        return module, constant_name

    def _get_alias_val(self, entry: _ConfigEntry) -> Any:
        data = self._get_alias_module_and_name(entry)
        if data is None:
            return _UNSET_SENTINEL
        module, constant_name = data
        constant_value = getattr(module, constant_name)
        return constant_value

    def _set_alias_val(self, entry: _ConfigEntry, val: Any) -> None:
        data = self._get_alias_module_and_name(entry)
        if data is None:
            raise AssertionError(
                "alias data should not be None when setting alias value"
            )
        module, constant_name = data
        setattr(module, constant_name, val)

    def _resolve_patch_changes(self, changes: dict[str, Any]) -> _ResolvedConfigChanges:
        targets = []
        modules = {self}
        for key, value in changes.items():
            module: ModuleType = self
            seen: set[tuple[ModuleType, str]] = set()
            while isinstance(module, ConfigModule):
                if (module, key) in seen:
                    raise ValueError(f"config alias cycle at {module.__name__}.{key}")
                seen.add((module, key))
                if key not in module._config:
                    raise AttributeError(f"{module.__name__}.{key} does not exist")
                modules.add(module)
                entry = module._config[key]
                module._warn_if_deprecated(key, entry)
                alias = module._get_alias_module_and_name(entry)
                if alias is None:
                    break
                module, key = alias
            targets.append((module, key, value))
        return targets, modules

    def _set_raw_values(
        self,
        changes: dict[str, Any],
        *,
        prior: dict[tuple[ModuleType, str], tuple[Any, bool]] | None = None,
    ) -> Callable[[], None]:
        targets: list[tuple[ModuleType, str, Any]] = []
        modules = {self}
        changed: dict[_ImplicationConfigModule, set[str]] = {}
        external_undo: list[tuple[ModuleType, str, Any]] = []

        def invalidate() -> None:
            for module in modules:
                module._hash_dirty_var.set(True)
            for module, key, _ in targets:
                if isinstance(module, ConfigModule):
                    module._mark_get_dict_dirty(key)
            for module, key in prior or ():
                if isinstance(module, ConfigModule):
                    module._mark_get_dict_dirty(key)

        def validate() -> None:
            for module, keys in changed.items():
                module._validate_active_implications(keys)

        def restore_hide(entry: _ConfigEntry, hide: bool) -> None:
            if entry.hide != hide:
                entry.hide = hide

        def restore_external() -> None:
            with contextlib.ExitStack() as stack:
                for module, key, value in reversed(external_undo):
                    stack.callback(setattr, module, key, value)

        try:
            with contextlib.ExitStack() as rollback:
                raw_rollback = contextlib.ExitStack()
                # Final token resets undo default materialization by external setters.
                rollback.callback(raw_rollback.close)
                rollback.callback(restore_external)
                rollback.callback(invalidate)
                for (module, key), (value, hide) in (prior or {}).items():
                    if isinstance(module, ConfigModule):
                        modules.add(module)
                        entry = module._config[key]
                        rollback.callback(entry.user_override.set, value)
                        rollback.callback(restore_hide, entry, hide)
                        raw_rollback.callback(entry.user_override.set, value)
                        raw_rollback.callback(restore_hide, entry, hide)
                    else:
                        external_undo.append((module, key, value))
                for name, value in changes.items():
                    resolved, touched = self._resolve_patch_changes({name: value})
                    targets.extend(resolved)
                    modules.update(touched)
                    module, key, value = resolved[0]
                    if isinstance(module, _ImplicationConfigModule):
                        changed.setdefault(module, set()).add(key)
                        if key in module._implication_sources:
                            _validate_implication_source(key, value)
                    if isinstance(module, ConfigModule):
                        entry = module._config[key]
                        # Snapshot before later alias imports can change this value.
                        prior_value = entry.user_override.get()
                        token = entry.user_override.set(prior_value)
                        rollback.callback(entry.user_override.set, prior_value)
                        rollback.callback(restore_hide, entry, entry.hide)
                        raw_rollback.callback(entry.user_override.reset, token)
                        raw_rollback.callback(restore_hide, entry, entry.hide)
                external_prior = {}
                for module, key, _ in targets:
                    if isinstance(module, ConfigModule):
                        continue
                    target = (module, key)
                    if target not in external_prior and (
                        prior is None or target not in prior
                    ):
                        external_prior[target] = getattr(module, key)
                for module, key, value in targets:
                    if isinstance(module, ConfigModule):
                        entry = module._config[key]
                        entry.user_override.set(value)
                        if entry.hide:
                            entry.hide = False
                invalidate()
                validate()
                for module, key, value in targets:
                    if isinstance(module, ConfigModule):
                        continue
                    if (module, key) in external_prior:
                        external_undo.append(
                            (module, key, external_prior.pop((module, key)))
                        )
                    invalidate()
                    setattr(module, key, value)
                if external_undo:
                    validate()
                undo = rollback.pop_all()
        finally:
            invalidate()

        def revert() -> None:
            try:
                undo.close()
            finally:
                invalidate()
            validate()

        return revert

    _GET_DICT_DIRTY_KEYS_CAP = 16

    def _mark_get_dict_dirty(self, name: str) -> None:
        dirty = self._get_dict_dirty_keys_var.get()
        if dirty is None:
            return
        dirty.add(name)
        if len(dirty) > self._GET_DICT_DIRTY_KEYS_CAP:
            self._get_dict_dirty_keys_var.set(None)

    def _is_default(self, name: str) -> bool:
        """
        Returns true if the config is at its default value.
        configs overridden by the env are not considered default.
        """
        config_val = self._config[name]
        # The config is not overridden by the user, and the env_value_default
        # is different from the default value (meaning user has set the env to
        # change the default value).
        not_set_env_default = (
            config_val.env_value_default is _UNSET_SENTINEL
            or config_val.env_value_default == config_val.default
        )
        not_set_env_force = (
            config_val.env_value_force is _UNSET_SENTINEL
            or config_val.env_value_force == config_val.default
        )

        unset = config_val.user_override.get() is _UNSET_SENTINEL
        # Handle reference types specially to avoid spammy warnings
        if not isinstance(config_val.default, _IMMUTABLE_CONFIG_TYPES):
            unset = unset or config_val.user_override.get() == config_val.default
        return unset and not_set_env_default and not_set_env_force

    def _get_dict(
        self,
        ignored_keys: list[str] | None = None,
        ignored_prefixes: list[str] | None = None,
        skip_default: bool = False,
        readonly_values: bool = False,
    ) -> dict[str, Any]:
        """Export a dictionary of current configuration keys and values.

        This function is designed to provide a single point which handles
        accessing config options and exporting them into a dictionary.
        This is used by a number of different user facing export methods
        which all have slightly different semantics re: how and what to
        skip.
        If a config is aliased, it skips this config.

        Arguments:
            ignored_keys are keys that should not be exported.
            ignored_prefixes are prefixes that if a key matches should
                not be exported
            skip_default does two things. One if a key has not been modified
                it skips it.
            readonly_values when True, enables caching of the result.
                The caller owns the returned dict but must not mutate
                its values. When False (default), no caching is used.
        """

        keys_to_update, config = None, None
        cache_key = (
            tuple(ignored_keys) if ignored_keys else (),
            tuple(ignored_prefixes) if ignored_prefixes else (),
            skip_default,
        )

        # Try to take a shortcut and only update dirty keys on top of a cached base.
        cache = self._get_dict_cache_var.get()
        if readonly_values:
            if cache is None:
                cache = {}
                self._get_dict_cache_var.set(cache)
            dirty_keys = self._get_dict_dirty_keys_var.get()
            cached = cache.get(cache_key)
            if cached is not None and dirty_keys is not None:
                # Shortcut: copy the cached base and update only dirty keys.
                # The cache entry itself is never mutated.
                keys_to_update = dirty_keys
                config = dict(cached)
            elif dirty_keys is None:
                # Fully dirty — clear entire cache and recompute
                cache = {}
                self._get_dict_cache_var.set(cache)
                self._get_dict_dirty_keys_var.set(set())

        # Recompute everything otherwise.
        if keys_to_update is None:
            keys_to_update = self._config.keys()
        if config is None:
            config = {}

        for key in keys_to_update:
            entry = self._config[key]
            if entry.alias is not None:
                config.pop(key, None)
                continue
            if ignored_keys and key in ignored_keys:
                config.pop(key, None)
                continue
            if ignored_prefixes:
                if any(key.startswith(prefix) for prefix in ignored_prefixes):
                    config.pop(key, None)
                    continue
            if skip_default and self._is_default(key):
                config.pop(key, None)
                continue

            # Read value directly, bypassing __getattr__ overhead
            # (deprecation warnings, alias resolution).
            user_override = entry.user_override.get()
            if entry.env_value_force is not _UNSET_SENTINEL:
                val = entry.env_value_force
            elif user_override is not _UNSET_SENTINEL:
                val = user_override
            elif entry.env_value_default is not _UNSET_SENTINEL:
                val = entry.env_value_default
            elif entry.justknob is not None:
                val = justknobs_check(name=entry.justknob, default=entry.default)
            else:
                val = entry.default
            if not isinstance(val, _IMMUTABLE_CONFIG_TYPES):
                val = copy.deepcopy(val)
            config[key] = val

        if readonly_values and cache_key not in cache:
            cache[cache_key] = dict(config)
        return config

    def get_type(self, config_name: str) -> type:
        return self._config[config_name].value_type

    def save_config(self) -> bytes:
        """Convert config to a pickled blob"""
        ignored_keys = getattr(self, "_save_config_ignore", [])
        return pickle.dumps(
            self._get_dict(ignored_keys=ignored_keys, readonly_values=True),
            protocol=2,
        )

    def save_config_portable(
        self, *, ignore_private_configs: bool = True, readonly_values: bool = False
    ) -> dict[str, Any]:
        """Convert config to portable format"""
        prefixes = []
        if ignore_private_configs:
            prefixes.append("_")
        prefixes.extend(getattr(self, "_cache_config_ignore_prefix", []))
        config = self._get_dict(
            ignored_prefixes=prefixes, readonly_values=readonly_values
        )
        source_values = (
            {key: config[key] for key in self._implication_sources if key in config}
            if isinstance(self, _ImplicationConfigModule)
            else None
        )
        serializer = getattr(self, "_cache_config_serializer", None)
        if serializer is not None:
            serializer(config)
        factory_keys = getattr(self, "_cache_config_factory_keys", [])
        if factory_keys:
            for key in factory_keys:
                if key in config and config[key] is not None:
                    instance = config[key]()
                    if hasattr(instance, "uuid"):
                        config[key] = instance.uuid()
                    else:
                        raise RuntimeError(
                            f"Config '{key}' is set to {config[key]} which does not "
                            f"implement uuid(). Implement uuid() for cache key "
                            f"participation."
                        )
        if isinstance(self, _ImplicationConfigModule):
            self._validate_serialized_sources(config, source_values)
        return config

    def codegen_config(self) -> str:
        """Convert config to Python statements that replicate current config.
        This does NOT include config settings that are at default values.
        """

        # additional imports required
        imports = set()

        def get_module_name(func: Callable, add_dot: bool) -> str:
            module_name = func.__module__
            if module_name == "builtins":
                module_name = ""
            if add_dot and module_name != "":
                module_name += "."
            return module_name

        def add_import(func: Callable) -> None:
            module_name = get_module_name(func, False)
            if module_name:
                imports.add(module_name)

        def list_of_callables_to_string(v: list | set) -> list[str]:
            return [f"{get_module_name(item, True)}{item.__name__}" for item in v]

        def importable_callable(v: Any) -> bool:
            # functools.partial has no attributes below but is a callable
            return callable(v) and hasattr(v, "__module__") and hasattr(v, "__name__")

        def importable_ref(func: Any) -> tuple[str, str] | None:
            try:
                module_name = getattr(func, "__module__", None)
                qualname = getattr(func, "__qualname__", None)
                if (
                    not callable(func)
                    or not isinstance(module_name, str)
                    or not isinstance(qualname, str)
                    or not module_name
                    or not qualname
                    or module_name == "__main__"
                    or "<" in qualname
                ):
                    return None
                names = module_name.split(".") + qualname.split(".")
                if any(
                    not name.isidentifier() or keyword.iskeyword(name) for name in names
                ):
                    return None
                resolved = importlib.import_module(module_name)
                for name in qualname.split("."):
                    resolved = getattr(resolved, name)
                if resolved is not func:
                    return None
                import_name = "" if module_name == "builtins" else module_name
                prefix = f"{import_name}." if import_name else ""
                return f"{prefix}{qualname}", import_name
            except Exception:
                return None

        def serialize_partial_arg(val: Any) -> str:
            if type(val) in (type(None), bool, int, str, bytes) or (
                type(val) is float and math.isfinite(val)
            ):
                return repr(val)
            if type(val) not in (list, tuple, dict):
                raise ValueError(
                    f"unsupported functools.partial argument type {type(val).__name__}"
                )

            if type(val) in (list, tuple):
                for item in val:
                    serialize_partial_arg(item)
            else:
                for key, item in val.items():
                    serialize_partial_arg(key)
                    serialize_partial_arg(item)
            return repr(val)

        def get_partial_line(mod, k, v) -> str:  # type: ignore[no-untyped-def]
            if type(v) is not functools.partial:
                return f"# {mod}.{k} omitted: unsupported partial subclass"
            func_info = importable_ref(v.func)
            if func_info is None:
                return f"# {mod}.{k} omitted: partial callable cannot be re-imported"
            func_ref, import_name = func_info
            try:
                parts = [func_ref]
                parts.extend(serialize_partial_arg(arg) for arg in v.args)
                if v.keywords:
                    parts.append(f"**{serialize_partial_arg(v.keywords)}")
                expression = f"functools.partial({', '.join(parts)})"
                compile(expression, "<config>", "eval")
            except Exception:
                return f"# {mod}.{k} omitted: partial arguments cannot be serialized"
            if import_name:
                imports.add(import_name)
            imports.add("functools")
            return f"{mod}.{k} = {expression}"

        def get_config_line(mod, k, v) -> str:  # type: ignore[no-untyped-def]
            """
            Return a string version of the config line.
            Handle v when v is a callable, or a list/dict of callables. Add import statements for callables if necessary.
            We assume that the value of a single config won't be a mix of callables and non-callables.

            Example output:
                import logging
                import _warnings
                torch._dynamo.config.reorderable_logging_functions = { _warnings.warn, logging.warn, print }
            """
            if isinstance(v, functools.partial):
                return get_partial_line(mod, k, v)
            elif importable_callable(v):
                add_import(v)
                return f"{mod}.{k} = {get_module_name(v, True)}{v.__name__}"
            elif isinstance(v, (list, set)) and all(
                importable_callable(item) for item in v
            ):
                for item in v:
                    add_import(item)
                v_list = list_of_callables_to_string(v)
                if isinstance(v, list):
                    return f"{mod}.{k} = {v_list}"
                else:
                    return f"{mod}.{k} = {{ {', '.join(v_list)} }}"
            else:
                return f"{mod}.{k} = {v!r}"

        lines = []
        mod = self.__name__
        values = self._get_dict(
            ignored_keys=getattr(self, "_save_config_ignore", []),
            skip_default=True,
            readonly_values=True,
        )
        if isinstance(self, _ImplicationConfigModule):
            self._validate_serialized_sources(values, skip_default=True)
            raw_values = {
                k: values.pop(k)
                for k in tuple(values)
                if k in self._implication_sources or k in self._implications
            }
            if raw_values:
                lines.append(f"{mod}.load_config({raw_values!r})")
        for k, v in values.items():
            lines.append(get_config_line(mod, k, v))
        for import_name in imports:
            lines.insert(0, f"import {import_name}")
        return "\n".join(lines)

    def get_hash(self) -> bytes:
        """Hashes the configs that are not compile_ignored"""
        if self._hash_dirty_var.get() or self._hash_cache_var.get() is None:
            dict_to_hash = self._get_dict(
                ignored_keys=list(self._compile_ignored_keys), readonly_values=True
            )
            string_to_hash = repr(sorted(dict_to_hash.items()))
            self._hash_cache_var.set(
                hashlib.md5(
                    string_to_hash.encode("utf-8"), usedforsecurity=False
                ).digest()
            )
            self._hash_dirty_var.set(False)
        result = self._hash_cache_var.get()
        if result is None:
            raise AssertionError(
                "_hash_cache_var should not be None after recomputation"
            )
        return result

    @deprecated(
        "`config.to_dict()` has been deprecated. It no longer changes the underlying config."
        " use `config.get_config_copy()` instead if you just want a copy of the config, or "
        "config.load_config if you need mutable access",
        category=FutureWarning,
    )
    def to_dict(self) -> dict[str, Any]:
        return self.get_config_copy()

    @deprecated(
        "`config.shallow_copy_dict()` has been deprecated. It no longer changes the underlying config."
        " use `config.get_config_copy()` instead if you just want a copy of the config, or "
        "config.load_config if you need mutable access",
        category=FutureWarning,
    )
    def shallow_copy_dict(self) -> dict[str, Any]:
        return self.get_config_copy()

    def load_config(self, maybe_pickled_config: bytes | dict[str, Any]) -> None:
        """Restore from a prior call to save_config() or shallow_copy_dict()"""
        if not isinstance(maybe_pickled_config, dict):
            config = pickle.loads(maybe_pickled_config)
        else:
            config = maybe_pickled_config
        applied = False
        for index, (k, v) in enumerate(config.items()):
            if k in self._config:
                if not applied and (
                    isinstance(self, _ImplicationConfigModule) or self._config[k].alias
                ):
                    # Preserve ordinary alias import order until implications need a batch.
                    _, modules = self._resolve_patch_changes({k: v})
                    if any(isinstance(m, _ImplicationConfigModule) for m in modules):
                        changes = {
                            key: value
                            for key, value in list(config.items())[index:]
                            if key in self._config
                        }
                        self._set_raw_values(changes)
                        applied = True
                if not applied:
                    setattr(self, k, v)
            else:
                from torch._dynamo.utils import warn_once

                warn_once(f"key {k} with value {v} is not understood by this config")

    def get_config_copy(self) -> dict[str, Any]:
        return self._get_dict()

    def get_serializable_config_copy(self) -> dict[str, Any]:
        return self._get_dict(ignored_keys=getattr(self, "_save_config_ignore", []))

    def patch(
        self,
        arg1: str | dict[str, Any] | None = None,
        arg2: Any = None,
        **kwargs: dict[str, Any],
    ) -> "ContextDecorator":
        """
        Decorator and/or context manager to make temporary changes to a config.  Note that patched settings are thread-local.

        As a decorator:

            @config.patch("name", val)
            @config.patch(name1=val1, name2=val2)
            @config.patch({"name1": val1, "name2", val2})
            def foo(...):
                ...

        As a context manager:

            with config.patch("name", val):
                ...

        For implication targets, this changes stored values; active implications
        still take precedence. On exit, stored values are restored before checking
        for conflicts. If changes to other sources leave conflicting implications,
        exit raises ValueError, including when unwinding an exception from the body.
        """
        changes: dict[str, Any]
        if arg1 is not None:
            if arg2 is not None:
                if not isinstance(arg1, str):
                    raise AssertionError(
                        "first argument must be a string when passing 2 positional args to patch"
                    )
                # patch("key", True) syntax
                changes = {arg1: arg2}
            else:
                if not isinstance(arg1, dict):
                    raise AssertionError(
                        "first argument must be a dict when passing a single positional arg to patch"
                    )
                # patch({"key": True}) syntax
                changes = arg1
            if kwargs:
                raise AssertionError(
                    "cannot pass both positional and keyword arguments to patch"
                )
        else:
            # patch(key=True) syntax
            changes = kwargs
            if arg2 is not None:
                raise AssertionError(
                    "second positional argument is only valid when first argument is a key string"
                )
        if not isinstance(changes, dict):
            raise AssertionError(f"expected `dict` got {type(changes)}")
        if isinstance(self, _ImplicationConfigModule):
            return _ImplicationConfigPatch(self, changes)
        config = self

        class ConfigPatch(ContextDecorator):
            def __init__(self) -> None:
                self.changes = changes
                self._prior: ContextVar[
                    tuple[dict[str, Any] | Callable[[], None], ...]
                ] = ContextVar(
                    f"{config.__name__}.ConfigPatch[{id(self)}]",
                    default=(),
                )

            def __enter__(self) -> None:
                has_alias = any(
                    config._config[key].alias
                    for key in self.changes
                    if key in config._config
                )
                prior: dict[str, Any] = {}
                raw_prior: dict[tuple[ModuleType, str], tuple[Any, bool]] = {}
                for key, value in self.changes.items():
                    if has_alias and key in config._config:
                        module, name = config, key
                        if config._config[key].alias:
                            targets, modules = config._resolve_patch_changes(
                                {key: value}
                            )
                            if any(
                                isinstance(m, _ImplicationConfigModule) for m in modules
                            ):
                                revert = config._set_raw_values(
                                    self.changes, prior=raw_prior
                                )
                                self._prior.set((*self._prior.get(), revert))
                                return
                            module, name, _ = targets[0]
                        if isinstance(module, ConfigModule):
                            entry = module._config[name]
                            raw_prior.setdefault(
                                (module, name), (entry.user_override.get(), entry.hide)
                            )
                        else:
                            prior[key] = config.__getattr__(key)
                            raw_prior.setdefault((module, name), (prior[key], False))
                            continue
                    # KeyError on invalid entry
                    prior[key] = config.__getattr__(key)
                prior_stack = self._prior.get()
                self._prior.set((*prior_stack, prior))
                try:
                    for k, v in self.changes.items():
                        config.__setattr__(k, v)
                except Exception:
                    self._prior.set(prior_stack)
                    raise

            def __exit__(self, exc_type, exc_val, exc_tb):  # type: ignore[no-untyped-def]
                prior_stack = self._prior.get()
                if not prior_stack:
                    raise AssertionError(
                        "prior should not be empty when exiting ConfigPatch"
                    )
                prior = prior_stack[-1]
                self._prior.set(prior_stack[:-1])
                if callable(prior):
                    prior()
                else:
                    for k, v in prior.items():
                        config.__setattr__(k, v)

        return ConfigPatch()

    def _make_closure_patcher(self, **changes: dict[str, Any]) -> Any:
        """
        A lower-overhead version of patch() for things on the critical path.

        Usage:

            # do this off the critical path
            change_fn = config.make_closure_patcher(foo=True)

            ...

            revert = change_fn()
            try:
              ...
            finally:
                revert()

        """
        config = self._config

        def change() -> Callable[[], None]:
            prior = {k: config[k].user_override.get() for k in changes}
            for k, v in changes.items():
                config[k].user_override.set(v)
                self._hash_dirty_var.set(True)
                self._mark_get_dict_dirty(k)

            def revert() -> None:
                for k, v in prior.items():
                    config[k].user_override.set(v)
                    self._hash_dirty_var.set(True)
                    self._mark_get_dict_dirty(k)

            return revert

        return change


class _ImplicationConfigModule(ConfigModule):
    _implications: dict[str, tuple[tuple[str, Any, Any], ...]]
    _implication_sources: set[str]
    _implication_dynamic_sources: set[str]
    _implication_hash: bytes

    def _get_implied_value(
        self, name: str, resolved: dict[str, Any] | None = None
    ) -> Any:
        if resolved is None:
            resolved = {}
        result = _UNSET_SENTINEL
        for source, condition, value in self._implications.get(name, ()):
            if source not in resolved:
                resolved[source] = self._get_resolved_value(source, resolved)
            selected = resolved[source]
            if type(selected) is not type(condition) or selected != condition:
                continue
            if result is not _UNSET_SENTINEL and (
                type(result) is not type(value) or result != value
            ):
                raise ValueError(f"conflicting config implications for {name}")
            result = value
        return result

    def _validate_active_implications(self, changes: set[str] | None = None) -> None:
        if changes is not None and not changes & self._implication_sources:
            return
        affected: dict[str, bool] = {}

        def is_affected(name: str) -> bool:
            if changes is None:
                return True
            if name not in affected:
                affected[name] = any(
                    source in changes or is_affected(source)
                    for source, _, _ in self._implications.get(name, ())
                )
            return affected[name]

        resolved: dict[str, Any] = {}
        for target in self._implications:
            if is_affected(target):
                self._get_implied_value(target, resolved)

    def _get_resolved_value(self, name: str, resolved: dict[str, Any]) -> Any:
        if name in self._implications and not self._config[name].hide:
            implied = self._get_implied_value(name, resolved)
            if implied is not _UNSET_SENTINEL:
                self._warn_if_deprecated(name, self._config[name])
                return implied
        return super().__getattr__(name)

    def __getattr__(self, name: str) -> Any:
        return self._get_resolved_value(name, {})

    def __setattr__(self, name: str, value: object) -> None:
        if name in self._implication_sources:
            self._set_raw_values({name: value})
        else:
            super().__setattr__(name, value)

    def _is_default(self, name: str) -> bool:
        return name not in self._implication_dynamic_sources and super()._is_default(
            name
        )

    def _get_dict(
        self,
        ignored_keys: list[str] | None = None,
        ignored_prefixes: list[str] | None = None,
        skip_default: bool = False,
        readonly_values: bool = False,
    ) -> dict[str, Any]:
        # JustKnobs can change without a config write to invalidate cached snapshots.
        for source in self._implication_dynamic_sources:
            self._mark_get_dict_dirty(source)
        return super()._get_dict(
            ignored_keys, ignored_prefixes, skip_default, readonly_values
        )

    def _validate_serialized_sources(
        self,
        values: dict[str, Any],
        source_values: dict[str, Any] | None = None,
        *,
        skip_default: bool = False,
    ) -> None:
        for source in self._implication_sources:
            if skip_default and source not in values and self._is_default(source):
                continue
            saved = values.get(source, _UNSET_SENTINEL)
            expected = (
                source_values.get(source, _UNSET_SENTINEL)
                if source_values is not None
                else saved
            )
            if (
                saved is _UNSET_SENTINEL
                or type(saved) is not type(expected)
                or saved != expected
            ):
                raise ValueError(
                    f"implication source {source} must be serialized unchanged"
                )

    def save_config(self) -> bytes:
        values = self._get_dict(
            ignored_keys=getattr(self, "_save_config_ignore", []), readonly_values=True
        )
        self._validate_serialized_sources(values)
        return pickle.dumps(values, protocol=2)

    def get_hash(self) -> bytes:
        if self._implication_sources & self._compile_ignored_keys:
            raise ValueError("implication sources must participate in config hashing")
        if self._implication_dynamic_sources:
            self._hash_dirty_var.set(True)
        return hashlib.md5(
            super().get_hash() + self._implication_hash, usedforsecurity=False
        ).digest()

    def _make_closure_patcher(self, **changes: dict[str, Any]) -> Any:
        # Opt-in modules validate on apply and revert; persistent conflicting changes raise.
        return functools.partial(self._set_raw_values, changes)


class ContextDecorator(contextlib.ContextDecorator):
    """
    Same as contextlib.ContextDecorator, but with support for
    `unittest.TestCase`
    """

    def __enter__(self) -> None:
        raise NotImplementedError("NYI")

    def __exit__(self, exc_type, exc_val, exc_tb) -> NoReturn:  # type: ignore[no-untyped-def]
        raise NotImplementedError("NYI")

    def __call__(self, func: Callable[[Any], Any]) -> Any:
        if isinstance(func, type) and issubclass(func, unittest.TestCase):

            class _TestCase(func):  # type: ignore[valid-type, misc]
                @classmethod
                def setUpClass(cls) -> None:
                    self.__enter__()
                    try:
                        super().setUpClass()
                    except Exception:
                        self.__exit__(None, None, None)
                        raise

                @classmethod
                def tearDownClass(cls) -> None:
                    try:
                        super().tearDownClass()
                    finally:
                        self.__exit__(None, None, None)

            _TestCase.__name__ = func.__name__
            _TestCase.__qualname__ = func.__qualname__
            _TestCase.__module__ = func.__module__

            return _TestCase

        return super().__call__(func)


class _ImplicationConfigPatch(ContextDecorator):
    def __init__(
        self, config: _ImplicationConfigModule, changes: dict[str, Any]
    ) -> None:
        self.config = config
        self.changes = changes
        self._prior: ContextVar[tuple[Callable[[], None], ...]] = ContextVar(
            f"{config.__name__}.ImplicationConfigPatch[{id(self)}]", default=()
        )

    def __enter__(self) -> None:
        revert = self.config._set_raw_values(self.changes)
        self._prior.set((*self._prior.get(), revert))

    def __exit__(self, exc_type, exc_val, exc_tb) -> None:
        prior = self._prior.get()
        if not prior:
            raise AssertionError("prior should not be empty when exiting config patch")
        self._prior.set(prior[:-1])
        prior[-1]()


class SubConfigProxy:
    """
    Shim to redirect to main config.
    `config.triton.cudagraphs` maps to _config["triton.cudagraphs"]
    """

    def __init__(self, config: object, prefix: str) -> None:
        # `super().__setattr__` to bypass custom `__setattr__`
        super().__setattr__("_config", config)
        super().__setattr__("_prefix", prefix)

    def __setattr__(self, name: str, value: object) -> None:
        return self._config.__setattr__(self._prefix + name, value)

    def __getattr__(self, name: str) -> Any:
        return self._config.__getattr__(self._prefix + name)

    def __delattr__(self, name: str) -> None:
        return self._config.__delattr__(self._prefix + name)


def get_tristate_env(name: str, default: Any = None) -> bool | None:
    value = os.environ.get(name)
    if value == "1":
        return True
    if value == "0":
        return False
    return default


def inherit_fields_from(parent_cls):
    def wrapper(child_cls):
        for k, v in parent_cls.__dict__.items():
            # copy fields that are not private and not overridden
            if not k.startswith("_") and k not in child_cls.__dict__:
                setattr(child_cls, k, v)
        return child_cls

    return wrapper
