跳到内容

Iomixin 工具

IOMixinWithGettersSetters

基类:WillHaveGetSetHparam, IOMixin

WillHaveGetSetHparam 的一个实现,它利用添加到您的类中的 io.IOMixin.io

这使您能够更改类的超参数,这些超参数稍后将保存在配置中。

源代码位于 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)

查找 io 混合类的已保存超参数。

参数

名称 类型 描述 默认值
attribute str

要在 self 的已保存初始化设置中查找的元素名称

必需

返回:值 提出:如果属性在已保存的初始化设置中不存在,则引发 KeyError。

源代码位于 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()

以字典格式返回 init 的超参数。

返回

类型 描述
Dict[str, Any]

Dict[str, Any]:此对象上 init 超参数的字典。

源代码位于 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()

返回一个列表,其中包含已从默认值更改的超参数。

返回

类型 描述
List[str]

List[str]:一个列表,其中包含已从默认值更改的超参数。

源代码位于 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)

更改 io 混合类的已保存超参数。

如果您只想更改已保存的超参数,例如在加载数据类时,其中相同的变量在 init 后通过确定性规则更改为其他不可保存的实体,则使用 also_change_value=False 仅更新超参数。

参数

名称 类型 描述 默认值
attribute str

要在 self 的已保存初始化设置中修改的元素名称

必需
value Any

已保存初始化设置的新参数

必需
also_change_value bool

如果您还想更改 self 中同名属性为期望的值,请将其设置为 True;否则,如果 init arg 和 self arg 预计会发散,则不要设置此项,并以正常的 Python 方式单独修改 self 属性。

True

返回

类型 描述
None

无。

源代码位于 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

基类:ABC

一个 ABC,声明特定的类具有添加到其中的可变 IO Mixin 变体。

这是一个占位符,直到 NeMo 中添加类似的功能。

提出

类型 描述
NotImplementedError

您必须实现 set_hparam、get_hparam 和 get_hparams

源代码位于 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

查找 io 混合类的已保存超参数。

参数

名称 类型 描述 默认值
attribute str

要在 self 的已保存初始化设置中查找的元素名称

必需

返回:值 提出:如果属性在已保存的初始化设置中不存在,则引发 KeyError。

源代码位于 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

以字典格式返回 init 的超参数。

返回

类型 描述
Dict[str, Any]

Dict[str, Any]:此对象上 init 超参数的字典。

源代码位于 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

更改 io 混合类的已保存超参数。

如果您只想更改已保存的超参数,例如在加载数据类时,其中相同的变量在 init 后通过确定性规则更改为其他不可保存的实体,则使用 also_change_value=False 仅更新超参数。

参数

名称 类型 描述 默认值
attribute str

要在 self 的已保存初始化设置中修改的元素名称

必需
value Any

已保存初始化设置的新参数

必需
also_change_value bool

如果您还想更改 self 中同名属性为期望的值,请将其设置为 True;否则,如果 init arg 和 self arg 预计会发散,则不要设置此项,并以正常的 Python 方式单独修改 self 属性。

True

返回

类型 描述
None

无。

源代码位于 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()