Skip to content

Iomixin utils

IOMixinWithGettersSetters

Bases: WillHaveGetSetHparam, IOMixin

An implementation of WillHaveGetSetHparam which makes use of the io.IOMixin.io added to your classes.

This enables you to mutate the hyper-parameters of your classes which will later be saved in configs.

Source code in bionemo/llm/utils/iomixin_utils.py
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class IOMixinWithGettersSetters(WillHaveGetSetHparam, io.IOMixin):
    """An implementation of WillHaveGetSetHparam which makes use of the io.IOMixin.__io__ added to your classes.

    This enables you to mutate the hyper-parameters of your classes which will later be saved in configs.
    """

    def set_hparam(self, attribute: str, value: Any, also_change_value: bool = True) -> None:
        """Mutates the saved hyper-parameter for the io mixed class.

        If you would like to only change the saved hyper-param
            for example in the case of loading a dataclass where the same variables are mutated to other non-savable
            entities by deterministic rules after init, then use `also_change_value=False` to only update the
            hyper-parameter.

        Args:
            attribute: The element name to modify within the saved init settings for self
            value: New parameter for the saved init settings
            also_change_value: If you also want to mutate the attribute of this same name in self to be the desired
                value, set this to True, otherwise if the init arg and self arg are expected to be divergent, then
                do not set this and modify the self attribute separately in the normal pythonic way.

        Returns:
            None.
        """
        # Change the attribute of self and also change the io tracker so it gets updated in the config
        if also_change_value:
            setattr(self, attribute, value)
        setattr(self.__io__, attribute, value)

    def get_hparam(self, attribute: str) -> Any:
        """Looks up the saved hyper-parameter for the io mixed class.

        Args:
            attribute: The element name to look up within the saved init settings for self
        Returns:
            Value
        Raises:
            KeyError if the attribute does not exist in the saved init settings.
        """
        if attribute not in dir(self.__io__):
            raise KeyError(
                f"Attribute '{attribute}' not found in hyper-parameters. Options: {sorted(self.get_hparams().keys())}"
            )
        return getattr(self.__io__, attribute)

    def get_non_default_hparams(self) -> List[str]:
        """Returns a list of hyper-parameters that have been changed from their default values.

        Returns:
            List[str]: A list of hyper-parameters that have been changed from their default values.
        """
        return [k for k in self.__io__.__dict__["__argument_history__"].keys() if k != "__fn_or_cls__"]

    def get_hparams(self) -> Dict[str, Any]:
        """Returns the hyper-parameters of init in a dictionary format.

        Returns:
            Dict[str, Any]: A dictionary of the init hyper-parameters on this object.
        """
        return {k: getattr(self.__io__, k) for k in self.get_non_default_hparams()}

get_hparam(attribute)

Looks up the saved hyper-parameter for the io mixed class.

Parameters:

Name Type Description Default
attribute str

The element name to look up within the saved init settings for self

required

Returns: Value Raises: KeyError if the attribute does not exist in the saved init settings.

Source code in bionemo/llm/utils/iomixin_utils.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def get_hparam(self, attribute: str) -> Any:
    """Looks up the saved hyper-parameter for the io mixed class.

    Args:
        attribute: The element name to look up within the saved init settings for self
    Returns:
        Value
    Raises:
        KeyError if the attribute does not exist in the saved init settings.
    """
    if attribute not in dir(self.__io__):
        raise KeyError(
            f"Attribute '{attribute}' not found in hyper-parameters. Options: {sorted(self.get_hparams().keys())}"
        )
    return getattr(self.__io__, attribute)

get_hparams()

Returns the hyper-parameters of init in a dictionary format.

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: A dictionary of the init hyper-parameters on this object.

Source code in bionemo/llm/utils/iomixin_utils.py
128
129
130
131
132
133
134
def get_hparams(self) -> Dict[str, Any]:
    """Returns the hyper-parameters of init in a dictionary format.

    Returns:
        Dict[str, Any]: A dictionary of the init hyper-parameters on this object.
    """
    return {k: getattr(self.__io__, k) for k in self.get_non_default_hparams()}

get_non_default_hparams()

Returns a list of hyper-parameters that have been changed from their default values.

Returns:

Type Description
List[str]

List[str]: A list of hyper-parameters that have been changed from their default values.

Source code in bionemo/llm/utils/iomixin_utils.py
120
121
122
123
124
125
126
def get_non_default_hparams(self) -> List[str]:
    """Returns a list of hyper-parameters that have been changed from their default values.

    Returns:
        List[str]: A list of hyper-parameters that have been changed from their default values.
    """
    return [k for k in self.__io__.__dict__["__argument_history__"].keys() if k != "__fn_or_cls__"]

set_hparam(attribute, value, also_change_value=True)

Mutates the saved hyper-parameter for the io mixed class.

If you would like to only change the saved hyper-param for example in the case of loading a dataclass where the same variables are mutated to other non-savable entities by deterministic rules after init, then use also_change_value=False to only update the hyper-parameter.

Parameters:

Name Type Description Default
attribute str

The element name to modify within the saved init settings for self

required
value Any

New parameter for the saved init settings

required
also_change_value bool

If you also want to mutate the attribute of this same name in self to be the desired value, set this to True, otherwise if the init arg and self arg are expected to be divergent, then do not set this and modify the self attribute separately in the normal pythonic way.

True

Returns:

Type Description
None

None.

Source code in bionemo/llm/utils/iomixin_utils.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def set_hparam(self, attribute: str, value: Any, also_change_value: bool = True) -> None:
    """Mutates the saved hyper-parameter for the io mixed class.

    If you would like to only change the saved hyper-param
        for example in the case of loading a dataclass where the same variables are mutated to other non-savable
        entities by deterministic rules after init, then use `also_change_value=False` to only update the
        hyper-parameter.

    Args:
        attribute: The element name to modify within the saved init settings for self
        value: New parameter for the saved init settings
        also_change_value: If you also want to mutate the attribute of this same name in self to be the desired
            value, set this to True, otherwise if the init arg and self arg are expected to be divergent, then
            do not set this and modify the self attribute separately in the normal pythonic way.

    Returns:
        None.
    """
    # Change the attribute of self and also change the io tracker so it gets updated in the config
    if also_change_value:
        setattr(self, attribute, value)
    setattr(self.__io__, attribute, value)

WillHaveGetSetHparam

Bases: ABC

An ABC that states that a particular class will have our mutatable IO Mixin variant added to it.

This is a placeholder until a similar piece of functionality is added in NeMo.

Raises:

Type Description
NotImplementedError

You must implement set_hparam, get_hparam, and get_hparams

Source code in bionemo/llm/utils/iomixin_utils.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class WillHaveGetSetHparam(ABC):
    """An ABC that states that a particular class _will_ have our mutatable IO Mixin variant added to it.

    This is a placeholder until a similar piece of functionality is added in NeMo.


    Raises:
        NotImplementedError: You must implement set_hparam, get_hparam, and get_hparams
    """

    @abstractmethod
    def set_hparam(self, attribute: str, value: Any, also_change_value: bool = True) -> None:
        """Mutates the saved hyper-parameter for the io mixed class.

        If you would like to only change the saved hyper-param
            for example in the case of loading a dataclass where the same variables are mutated to other non-savable
            entities by deterministic rules after init, then use `also_change_value=False` to only update the
            hyper-parameter.

        Args:
            attribute: The element name to modify within the saved init settings for self
            value: New parameter for the saved init settings
            also_change_value: If you also want to mutate the attribute of this same name in self to be the desired
                value, set this to True, otherwise if the init arg and self arg are expected to be divergent, then
                do not set this and modify the self attribute separately in the normal pythonic way.

        Returns:
            None.
        """
        raise NotImplementedError()

    @abstractmethod
    def get_hparam(self, attribute: str) -> Any:
        """Looks up the saved hyper-parameter for the io mixed class.

        Args:
            attribute: The element name to look up within the saved init settings for self
        Returns:
            Value
        Raises:
            KeyError if the attribute does not exist in the saved init settings.
        """
        raise NotImplementedError()

    @abstractmethod
    def get_hparams(self) -> Dict[str, Any]:
        """Returns the hyper-parameters of init in a dictionary format.

        Returns:
            Dict[str, Any]: A dictionary of the init hyper-parameters on this object.
        """
        raise NotImplementedError()

get_hparam(attribute) abstractmethod

Looks up the saved hyper-parameter for the io mixed class.

Parameters:

Name Type Description Default
attribute str

The element name to look up within the saved init settings for self

required

Returns: Value Raises: KeyError if the attribute does not exist in the saved init settings.

Source code in bionemo/llm/utils/iomixin_utils.py
52
53
54
55
56
57
58
59
60
61
62
63
@abstractmethod
def get_hparam(self, attribute: str) -> Any:
    """Looks up the saved hyper-parameter for the io mixed class.

    Args:
        attribute: The element name to look up within the saved init settings for self
    Returns:
        Value
    Raises:
        KeyError if the attribute does not exist in the saved init settings.
    """
    raise NotImplementedError()

get_hparams() abstractmethod

Returns the hyper-parameters of init in a dictionary format.

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: A dictionary of the init hyper-parameters on this object.

Source code in bionemo/llm/utils/iomixin_utils.py
65
66
67
68
69
70
71
72
@abstractmethod
def get_hparams(self) -> Dict[str, Any]:
    """Returns the hyper-parameters of init in a dictionary format.

    Returns:
        Dict[str, Any]: A dictionary of the init hyper-parameters on this object.
    """
    raise NotImplementedError()

set_hparam(attribute, value, also_change_value=True) abstractmethod

Mutates the saved hyper-parameter for the io mixed class.

If you would like to only change the saved hyper-param for example in the case of loading a dataclass where the same variables are mutated to other non-savable entities by deterministic rules after init, then use also_change_value=False to only update the hyper-parameter.

Parameters:

Name Type Description Default
attribute str

The element name to modify within the saved init settings for self

required
value Any

New parameter for the saved init settings

required
also_change_value bool

If you also want to mutate the attribute of this same name in self to be the desired value, set this to True, otherwise if the init arg and self arg are expected to be divergent, then do not set this and modify the self attribute separately in the normal pythonic way.

True

Returns:

Type Description
None

None.

Source code in bionemo/llm/utils/iomixin_utils.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@abstractmethod
def set_hparam(self, attribute: str, value: Any, also_change_value: bool = True) -> None:
    """Mutates the saved hyper-parameter for the io mixed class.

    If you would like to only change the saved hyper-param
        for example in the case of loading a dataclass where the same variables are mutated to other non-savable
        entities by deterministic rules after init, then use `also_change_value=False` to only update the
        hyper-parameter.

    Args:
        attribute: The element name to modify within the saved init settings for self
        value: New parameter for the saved init settings
        also_change_value: If you also want to mutate the attribute of this same name in self to be the desired
            value, set this to True, otherwise if the init arg and self arg are expected to be divergent, then
            do not set this and modify the self attribute separately in the normal pythonic way.

    Returns:
        None.
    """
    raise NotImplementedError()