How to Inspect Function and Class Signatures in Python?#
Motivation#
There are two motivations for why we want to inspect function and class signatures in Python.
Code Introspection in Open Source Projects: When we are working on open source projects, we often need to inspect the function and class signatures of the codebase. This is especially true when we are working on a large codebase with many functions and classes. In this case, we need to inspect the function and class signatures to understand how the codebase is structured and how the functions and classes are used.
Sometimes there are nested abstractions, a child class \(\mathcal{C}_N\) that inherits from a parent class \(\mathcal{C}_{N-1}\), which in turn inherits from another parent class \(\mathcal{C}_{N-2}\), and so on. Sometimes the child class does not immediately show what types of arguments the constructor can take. In this case, we need to inspect the parent classes to understand the constructor signature of the child class.
Agent-Based Function Calling: In agent-based programming, where automated agents are tasked with executing functions or interacting with libraries, the risk of ‘hallucination’—where an agent attempts to invoke non-existent methods or improperly structured calls from a external module/library—is a notable concern. By equipping agents with the capability to query the actual signatures of libraries’ classes or functions, we can significantly mitigate this risk. This ensures that agents operate based on accurate, real-time information, thereby improving the reliability and effectiveness of automated tasks.
Construct Hypothetical Function, Child and Parent Classes#
class ParentClass:
"""This is the parent class."""
parent_class_attr = "a parent class attribute"
def __init__(self, parent_instance_attr: str) -> None:
self.parent_instance_attr = parent_instance_attr
def parent_method(self) -> str:
"""This is a method in the parent class."""
return "Parent method called"
class ChildClass(ParentClass):
"""This is a subclass of ParentClass."""
# Class attribute
class_attr = "a class attribute"
# Private and protected attributes
_protected_attr = "a protected attribute"
__private_attr = "a private attribute"
def __init__(self, instance_attr: str, parent_instance_attr: str) -> None:
"""Initialize the instance."""
super().__init__(parent_instance_attr)
# Instance attribute
self.instance_attr = instance_attr
self.instance_not_in_constructor_attr = "an instance attribute not in the constructor"
self._private_instance_attr = "a private instance attribute"
@property
def read_only_attr(self) -> str:
"""This is a read-only attribute."""
return "You can read me, but you cannot change me."
def instance_method(self, arg: str) -> str:
"""This is an instance method."""
return f"Instance method called with argument: {arg}"
@classmethod
def class_method(cls, arg: str) -> str:
"""This is a class method."""
return f"Class method called with argument: {arg}"
@staticmethod
def static_method(arg: str) -> str:
"""This is a static method."""
return f"Static method called with argument: {arg}"
def __str__(self) -> str:
"""Return a string representation of the instance."""
return f"MyClass(instance_attr={self.instance_attr})"
instance_child = ChildClass(instance_attr="an instance attribute", parent_instance_attr="a parent instance attribute")
class_child = ChildClass
instance_parent = ParentClass(parent_instance_attr="a parent instance attribute")
class_parent = ParentClass
def func(a: int, b: str, c: List[int], d: Tuple[str, str], e: Union[int, str], **kwargs: Any) -> str:
return a, b, c, d, e, kwargs
Inspect All Members#
@overload
def get_members_of_function_or_method(
func_or_class: Type[object], predicate: Optional[Callable[[Any], bool]] = None
) -> List[Tuple[str, Any]]: ...
@overload
def get_members_of_function_or_method(
func_or_class: Callable[..., Any], predicate: Optional[Callable[[Any], bool]] = None
) -> List[Tuple[str, Any]]: ...
def get_members_of_function_or_method(
func_or_class: Union[Type[object], Callable[..., Any]], predicate: Optional[Callable[[Any], bool]] = None
) -> List[Tuple[str, Any]]:
return inspect.getmembers(func_or_class, predicate)
def loop_through_members(members: List[Tuple[str, Any]], filter: Optional[str] = None) -> None:
if filter is not None:
members = [member for member in members if filter in member[0]]
for member in members:
name, value = member
print(f"{name}: {value}")
Our initial goal is to get all signatures and type annotations of a class or function. We can use the inspect module to achieve this. The getmembers function returns all members of a class or module. We can then filter out the functions and classes and inspect their signatures.
However, for our purpose, it may be overkill since it retrieves all members
within a module, the scope is very broad, for example, inspecting just the func
defined will also return all __globals__, which may not be what we want.
func_all_members = get_members_of_function_or_method(func, predicate=None)
loop_through_members(func_all_members)
__annotate__: <function func.__annotate__ at 0x7fb2f6d1e980>
__annotations__: {'a': <class 'int'>, 'b': <class 'str'>, 'c': typing.List[int], 'd': typing.Tuple[str, str], 'e': int | str, 'kwargs': typing.Any, 'return': <class 'str'>}
__builtins__: {'__name__': 'builtins', '__doc__': "Built-in functions, types, exceptions, and other objects.\n\nThis module provides direct access to all 'built-in'\nidentifiers of Python; for example, builtins.len is\nthe full name for the built-in function len().\n\nThis module is not normally accessed explicitly by most\napplications, but can be useful in modules that provide\nobjects with the same name as a built-in value, but in\nwhich the built-in of that name is also needed.", '__package__': '', '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>, origin='built-in'), '__build_class__': <built-in function __build_class__>, '__import__': <built-in function __import__>, 'abs': <built-in function abs>, 'all': <built-in function all>, 'any': <built-in function any>, 'ascii': <built-in function ascii>, 'bin': <built-in function bin>, 'breakpoint': <built-in function breakpoint>, 'callable': <built-in function callable>, 'chr': <built-in function chr>, 'compile': <built-in function compile>, 'delattr': <built-in function delattr>, 'dir': <built-in function dir>, 'divmod': <built-in function divmod>, 'eval': <built-in function eval>, 'exec': <built-in function exec>, 'format': <built-in function format>, 'getattr': <built-in function getattr>, 'globals': <built-in function globals>, 'hasattr': <built-in function hasattr>, 'hash': <built-in function hash>, 'hex': <built-in function hex>, 'id': <built-in function id>, 'input': <bound method Kernel.raw_input of <ipykernel.ipkernel.IPythonKernel object at 0x7fb3364956a0>>, 'isinstance': <built-in function isinstance>, 'issubclass': <built-in function issubclass>, 'iter': <built-in function iter>, 'aiter': <built-in function aiter>, 'len': <built-in function len>, 'locals': <built-in function locals>, 'max': <built-in function max>, 'min': <built-in function min>, 'next': <built-in function next>, 'anext': <built-in function anext>, 'oct': <built-in function oct>, 'ord': <built-in function ord>, 'pow': <built-in function pow>, 'print': <built-in function print>, 'repr': <built-in function repr>, 'round': <built-in function round>, 'setattr': <built-in function setattr>, 'sorted': <built-in function sorted>, 'sum': <built-in function sum>, 'vars': <built-in function vars>, 'None': None, 'Ellipsis': Ellipsis, 'NotImplemented': NotImplemented, 'False': False, 'True': True, 'bool': <class 'bool'>, 'memoryview': <class 'memoryview'>, 'bytearray': <class 'bytearray'>, 'bytes': <class 'bytes'>, 'classmethod': <class 'classmethod'>, 'complex': <class 'complex'>, 'dict': <class 'dict'>, 'enumerate': <class 'enumerate'>, 'filter': <class 'filter'>, 'float': <class 'float'>, 'frozenset': <class 'frozenset'>, 'property': <class 'property'>, 'int': <class 'int'>, 'list': <class 'list'>, 'map': <class 'map'>, 'object': <class 'object'>, 'range': <class 'range'>, 'reversed': <class 'reversed'>, 'set': <class 'set'>, 'slice': <class 'slice'>, 'staticmethod': <class 'staticmethod'>, 'str': <class 'str'>, 'super': <class 'super'>, 'tuple': <class 'tuple'>, 'type': <class 'type'>, 'zip': <class 'zip'>, '__debug__': True, 'BaseException': <class 'BaseException'>, 'BaseExceptionGroup': <class 'BaseExceptionGroup'>, 'Exception': <class 'Exception'>, 'GeneratorExit': <class 'GeneratorExit'>, 'KeyboardInterrupt': <class 'KeyboardInterrupt'>, 'SystemExit': <class 'SystemExit'>, 'ArithmeticError': <class 'ArithmeticError'>, 'AssertionError': <class 'AssertionError'>, 'AttributeError': <class 'AttributeError'>, 'BufferError': <class 'BufferError'>, 'EOFError': <class 'EOFError'>, 'ImportError': <class 'ImportError'>, 'LookupError': <class 'LookupError'>, 'MemoryError': <class 'MemoryError'>, 'NameError': <class 'NameError'>, 'OSError': <class 'OSError'>, 'ReferenceError': <class 'ReferenceError'>, 'RuntimeError': <class 'RuntimeError'>, 'StopAsyncIteration': <class 'StopAsyncIteration'>, 'StopIteration': <class 'StopIteration'>, 'SyntaxError': <class 'SyntaxError'>, 'SystemError': <class 'SystemError'>, 'TypeError': <class 'TypeError'>, 'ValueError': <class 'ValueError'>, 'Warning': <class 'Warning'>, 'FloatingPointError': <class 'FloatingPointError'>, 'OverflowError': <class 'OverflowError'>, 'ZeroDivisionError': <class 'ZeroDivisionError'>, 'BytesWarning': <class 'BytesWarning'>, 'DeprecationWarning': <class 'DeprecationWarning'>, 'EncodingWarning': <class 'EncodingWarning'>, 'FutureWarning': <class 'FutureWarning'>, 'ImportWarning': <class 'ImportWarning'>, 'PendingDeprecationWarning': <class 'PendingDeprecationWarning'>, 'ResourceWarning': <class 'ResourceWarning'>, 'RuntimeWarning': <class 'RuntimeWarning'>, 'SyntaxWarning': <class 'SyntaxWarning'>, 'UnicodeWarning': <class 'UnicodeWarning'>, 'UserWarning': <class 'UserWarning'>, 'BlockingIOError': <class 'BlockingIOError'>, 'ChildProcessError': <class 'ChildProcessError'>, 'ConnectionError': <class 'ConnectionError'>, 'FileExistsError': <class 'FileExistsError'>, 'FileNotFoundError': <class 'FileNotFoundError'>, 'InterruptedError': <class 'InterruptedError'>, 'IsADirectoryError': <class 'IsADirectoryError'>, 'NotADirectoryError': <class 'NotADirectoryError'>, 'PermissionError': <class 'PermissionError'>, 'ProcessLookupError': <class 'ProcessLookupError'>, 'TimeoutError': <class 'TimeoutError'>, 'IndentationError': <class 'IndentationError'>, '_IncompleteInputError': <class '_IncompleteInputError'>, 'IndexError': <class 'IndexError'>, 'KeyError': <class 'KeyError'>, 'ModuleNotFoundError': <class 'ModuleNotFoundError'>, 'NotImplementedError': <class 'NotImplementedError'>, 'PythonFinalizationError': <class 'PythonFinalizationError'>, 'RecursionError': <class 'RecursionError'>, 'UnboundLocalError': <class 'UnboundLocalError'>, 'UnicodeError': <class 'UnicodeError'>, 'BrokenPipeError': <class 'BrokenPipeError'>, 'ConnectionAbortedError': <class 'ConnectionAbortedError'>, 'ConnectionRefusedError': <class 'ConnectionRefusedError'>, 'ConnectionResetError': <class 'ConnectionResetError'>, 'TabError': <class 'TabError'>, 'UnicodeDecodeError': <class 'UnicodeDecodeError'>, 'UnicodeEncodeError': <class 'UnicodeEncodeError'>, 'UnicodeTranslateError': <class 'UnicodeTranslateError'>, 'ExceptionGroup': <class 'ExceptionGroup'>, 'EnvironmentError': <class 'OSError'>, 'IOError': <class 'OSError'>, 'open': <built-in function open>, 'copyright': Copyright (c) 2001 Python Software Foundation.
All Rights Reserved.
Copyright (c) 2000 BeOpen.com.
All Rights Reserved.
Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.
Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved., 'credits': Thanks to CWI, CNRI, BeOpen, Zope Corporation, the Python Software
Foundation, and a cast of thousands for supporting Python
development. See www.python.org for more information., 'license': Type license() to see the full license text, 'help': Type help() for interactive help, or help(object) for help about object., 'execfile': <function execfile at 0x7fb3343c5640>, 'runfile': <function runfile at 0x7fb3342dfab0>, '__IPYTHON__': True, 'display': <function display at 0x7fb3374f6e50>, 'get_ipython': <bound method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x7fb3341a3620>>}
__call__: <method-wrapper '__call__' of function object at 0x7fb2f6d1e6c0>
__class__: <class 'function'>
__closure__: None
__code__: <code object func at 0x7fb21299d7d0, file "/tmp/ipykernel_4509/2139551385.py", line 1>
__defaults__: None
__delattr__: <method-wrapper '__delattr__' of function object at 0x7fb2f6d1e6c0>
__dict__: {}
__dir__: <built-in method __dir__ of function object at 0x7fb2f6d1e6c0>
__doc__: None
__eq__: <method-wrapper '__eq__' of function object at 0x7fb2f6d1e6c0>
__format__: <built-in method __format__ of function object at 0x7fb2f6d1e6c0>
__ge__: <method-wrapper '__ge__' of function object at 0x7fb2f6d1e6c0>
__get__: <method-wrapper '__get__' of function object at 0x7fb2f6d1e6c0>
__getattribute__: <method-wrapper '__getattribute__' of function object at 0x7fb2f6d1e6c0>
__getstate__: <built-in method __getstate__ of function object at 0x7fb2f6d1e6c0>
__globals__: {'__name__': '__main__', '__doc__': 'Automatically created module for IPython interactive environment', '__package__': None, '__loader__': None, '__spec__': None, '__builtin__': <module 'builtins' (built-in)>, '__builtins__': <module 'builtins' (built-in)>, '_ih': ['', 'import inspect\nfrom dataclasses import field, make_dataclass\nfrom inspect import Parameter, Signature\nfrom typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type, Union, _GenericAlias, get_type_hints, overload\n\nfrom pydantic import BaseModel\nfrom rich.pretty import pprint\nfrom transformers import GPT2LMHeadModel, Trainer, TrainingArguments', 'class ParentClass:\n """This is the parent class."""\n\n parent_class_attr = "a parent class attribute"\n\n def __init__(self, parent_instance_attr: str) -> None:\n self.parent_instance_attr = parent_instance_attr\n\n def parent_method(self) -> str:\n """This is a method in the parent class."""\n return "Parent method called"\n\n\nclass ChildClass(ParentClass):\n """This is a subclass of ParentClass."""\n\n # Class attribute\n class_attr = "a class attribute"\n\n # Private and protected attributes\n _protected_attr = "a protected attribute"\n __private_attr = "a private attribute"\n\n def __init__(self, instance_attr: str, parent_instance_attr: str) -> None:\n """Initialize the instance."""\n super().__init__(parent_instance_attr)\n # Instance attribute\n self.instance_attr = instance_attr\n self.instance_not_in_constructor_attr = "an instance attribute not in the constructor"\n self._private_instance_attr = "a private instance attribute"\n\n @property\n def read_only_attr(self) -> str:\n """This is a read-only attribute."""\n return "You can read me, but you cannot change me."\n\n def instance_method(self, arg: str) -> str:\n """This is an instance method."""\n return f"Instance method called with argument: {arg}"\n\n @classmethod\n def class_method(cls, arg: str) -> str:\n """This is a class method."""\n return f"Class method called with argument: {arg}"\n\n @staticmethod\n def static_method(arg: str) -> str:\n """This is a static method."""\n return f"Static method called with argument: {arg}"\n\n def __str__(self) -> str:\n """Return a string representation of the instance."""\n return f"MyClass(instance_attr={self.instance_attr})"', 'instance_child = ChildClass(instance_attr="an instance attribute", parent_instance_attr="a parent instance attribute")\nclass_child = ChildClass\n\ninstance_parent = ParentClass(parent_instance_attr="a parent instance attribute")\nclass_parent = ParentClass', 'def func(a: int, b: str, c: List[int], d: Tuple[str, str], e: Union[int, str], **kwargs: Any) -> str:\n return a, b, c, d, e, kwargs', '@overload\ndef get_members_of_function_or_method(\n func_or_class: Type[object], predicate: Optional[Callable[[Any], bool]] = None\n) -> List[Tuple[str, Any]]: ...\n\n\n@overload\ndef get_members_of_function_or_method(\n func_or_class: Callable[..., Any], predicate: Optional[Callable[[Any], bool]] = None\n) -> List[Tuple[str, Any]]: ...\n\n\ndef get_members_of_function_or_method(\n func_or_class: Union[Type[object], Callable[..., Any]], predicate: Optional[Callable[[Any], bool]] = None\n) -> List[Tuple[str, Any]]:\n return inspect.getmembers(func_or_class, predicate)\n\n\ndef loop_through_members(members: List[Tuple[str, Any]], filter: Optional[str] = None) -> None:\n if filter is not None:\n members = [member for member in members if filter in member[0]]\n for member in members:\n name, value = member\n print(f"{name}: {value}")', 'func_all_members = get_members_of_function_or_method(func, predicate=None)\nloop_through_members(func_all_members)'], '_oh': {}, '_dh': [PosixPath('/home/runner/work/omniverse/omniverse/omniverse/playbook')], 'In': ['', 'import inspect\nfrom dataclasses import field, make_dataclass\nfrom inspect import Parameter, Signature\nfrom typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type, Union, _GenericAlias, get_type_hints, overload\n\nfrom pydantic import BaseModel\nfrom rich.pretty import pprint\nfrom transformers import GPT2LMHeadModel, Trainer, TrainingArguments', 'class ParentClass:\n """This is the parent class."""\n\n parent_class_attr = "a parent class attribute"\n\n def __init__(self, parent_instance_attr: str) -> None:\n self.parent_instance_attr = parent_instance_attr\n\n def parent_method(self) -> str:\n """This is a method in the parent class."""\n return "Parent method called"\n\n\nclass ChildClass(ParentClass):\n """This is a subclass of ParentClass."""\n\n # Class attribute\n class_attr = "a class attribute"\n\n # Private and protected attributes\n _protected_attr = "a protected attribute"\n __private_attr = "a private attribute"\n\n def __init__(self, instance_attr: str, parent_instance_attr: str) -> None:\n """Initialize the instance."""\n super().__init__(parent_instance_attr)\n # Instance attribute\n self.instance_attr = instance_attr\n self.instance_not_in_constructor_attr = "an instance attribute not in the constructor"\n self._private_instance_attr = "a private instance attribute"\n\n @property\n def read_only_attr(self) -> str:\n """This is a read-only attribute."""\n return "You can read me, but you cannot change me."\n\n def instance_method(self, arg: str) -> str:\n """This is an instance method."""\n return f"Instance method called with argument: {arg}"\n\n @classmethod\n def class_method(cls, arg: str) -> str:\n """This is a class method."""\n return f"Class method called with argument: {arg}"\n\n @staticmethod\n def static_method(arg: str) -> str:\n """This is a static method."""\n return f"Static method called with argument: {arg}"\n\n def __str__(self) -> str:\n """Return a string representation of the instance."""\n return f"MyClass(instance_attr={self.instance_attr})"', 'instance_child = ChildClass(instance_attr="an instance attribute", parent_instance_attr="a parent instance attribute")\nclass_child = ChildClass\n\ninstance_parent = ParentClass(parent_instance_attr="a parent instance attribute")\nclass_parent = ParentClass', 'def func(a: int, b: str, c: List[int], d: Tuple[str, str], e: Union[int, str], **kwargs: Any) -> str:\n return a, b, c, d, e, kwargs', '@overload\ndef get_members_of_function_or_method(\n func_or_class: Type[object], predicate: Optional[Callable[[Any], bool]] = None\n) -> List[Tuple[str, Any]]: ...\n\n\n@overload\ndef get_members_of_function_or_method(\n func_or_class: Callable[..., Any], predicate: Optional[Callable[[Any], bool]] = None\n) -> List[Tuple[str, Any]]: ...\n\n\ndef get_members_of_function_or_method(\n func_or_class: Union[Type[object], Callable[..., Any]], predicate: Optional[Callable[[Any], bool]] = None\n) -> List[Tuple[str, Any]]:\n return inspect.getmembers(func_or_class, predicate)\n\n\ndef loop_through_members(members: List[Tuple[str, Any]], filter: Optional[str] = None) -> None:\n if filter is not None:\n members = [member for member in members if filter in member[0]]\n for member in members:\n name, value = member\n print(f"{name}: {value}")', 'func_all_members = get_members_of_function_or_method(func, predicate=None)\nloop_through_members(func_all_members)'], 'Out': {}, 'get_ipython': <bound method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x7fb3341a3620>>, 'exit': <IPython.core.autocall.ZMQExitAutocall object at 0x7fb32cf812b0>, 'quit': <IPython.core.autocall.ZMQExitAutocall object at 0x7fb32cf812b0>, 'open': <function open at 0x7fb337353cc0>, '_': '', '__': '', '___': '', '_i': '@overload\ndef get_members_of_function_or_method(\n func_or_class: Type[object], predicate: Optional[Callable[[Any], bool]] = None\n) -> List[Tuple[str, Any]]: ...\n\n\n@overload\ndef get_members_of_function_or_method(\n func_or_class: Callable[..., Any], predicate: Optional[Callable[[Any], bool]] = None\n) -> List[Tuple[str, Any]]: ...\n\n\ndef get_members_of_function_or_method(\n func_or_class: Union[Type[object], Callable[..., Any]], predicate: Optional[Callable[[Any], bool]] = None\n) -> List[Tuple[str, Any]]:\n return inspect.getmembers(func_or_class, predicate)\n\n\ndef loop_through_members(members: List[Tuple[str, Any]], filter: Optional[str] = None) -> None:\n if filter is not None:\n members = [member for member in members if filter in member[0]]\n for member in members:\n name, value = member\n print(f"{name}: {value}")', '_ii': 'def func(a: int, b: str, c: List[int], d: Tuple[str, str], e: Union[int, str], **kwargs: Any) -> str:\n return a, b, c, d, e, kwargs', '_iii': 'instance_child = ChildClass(instance_attr="an instance attribute", parent_instance_attr="a parent instance attribute")\nclass_child = ChildClass\n\ninstance_parent = ParentClass(parent_instance_attr="a parent instance attribute")\nclass_parent = ParentClass', '_i1': 'import inspect\nfrom dataclasses import field, make_dataclass\nfrom inspect import Parameter, Signature\nfrom typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type, Union, _GenericAlias, get_type_hints, overload\n\nfrom pydantic import BaseModel\nfrom rich.pretty import pprint\nfrom transformers import GPT2LMHeadModel, Trainer, TrainingArguments', 'inspect': <module 'inspect' from '/home/runner/.local/share/uv/python/cpython-3.14.7-linux-x86_64-gnu/lib/python3.14/inspect.py'>, 'field': <function field at 0x7fb3381017a0>, 'make_dataclass': <function make_dataclass at 0x7fb338130720>, 'Parameter': <class 'inspect.Parameter'>, 'Signature': <class 'inspect.Signature'>, 'Any': typing.Any, 'Callable': typing.Callable, 'Dict': typing.Dict, 'List': typing.List, 'Optional': typing.Optional, 'Set': typing.Set, 'Tuple': typing.Tuple, 'Type': typing.Type, 'Union': <class 'typing.Union'>, '_GenericAlias': <class 'typing._GenericAlias'>, 'get_type_hints': <function get_type_hints at 0x7fb3389c97a0>, 'overload': <function overload at 0x7fb3389c9f30>, 'BaseModel': <class 'pydantic.main.BaseModel'>, 'pprint': <function pprint at 0x7fb32c6f5d20>, 'GPT2LMHeadModel': <class 'transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel'>, 'Trainer': <class 'transformers.trainer.Trainer'>, 'TrainingArguments': <class 'transformers.training_args.TrainingArguments'>, '_i2': 'class ParentClass:\n """This is the parent class."""\n\n parent_class_attr = "a parent class attribute"\n\n def __init__(self, parent_instance_attr: str) -> None:\n self.parent_instance_attr = parent_instance_attr\n\n def parent_method(self) -> str:\n """This is a method in the parent class."""\n return "Parent method called"\n\n\nclass ChildClass(ParentClass):\n """This is a subclass of ParentClass."""\n\n # Class attribute\n class_attr = "a class attribute"\n\n # Private and protected attributes\n _protected_attr = "a protected attribute"\n __private_attr = "a private attribute"\n\n def __init__(self, instance_attr: str, parent_instance_attr: str) -> None:\n """Initialize the instance."""\n super().__init__(parent_instance_attr)\n # Instance attribute\n self.instance_attr = instance_attr\n self.instance_not_in_constructor_attr = "an instance attribute not in the constructor"\n self._private_instance_attr = "a private instance attribute"\n\n @property\n def read_only_attr(self) -> str:\n """This is a read-only attribute."""\n return "You can read me, but you cannot change me."\n\n def instance_method(self, arg: str) -> str:\n """This is an instance method."""\n return f"Instance method called with argument: {arg}"\n\n @classmethod\n def class_method(cls, arg: str) -> str:\n """This is a class method."""\n return f"Class method called with argument: {arg}"\n\n @staticmethod\n def static_method(arg: str) -> str:\n """This is a static method."""\n return f"Static method called with argument: {arg}"\n\n def __str__(self) -> str:\n """Return a string representation of the instance."""\n return f"MyClass(instance_attr={self.instance_attr})"', 'ParentClass': <class '__main__.ParentClass'>, 'ChildClass': <class '__main__.ChildClass'>, '_i3': 'instance_child = ChildClass(instance_attr="an instance attribute", parent_instance_attr="a parent instance attribute")\nclass_child = ChildClass\n\ninstance_parent = ParentClass(parent_instance_attr="a parent instance attribute")\nclass_parent = ParentClass', 'instance_child': <__main__.ChildClass object at 0x7fb21281cec0>, 'class_child': <class '__main__.ChildClass'>, 'instance_parent': <__main__.ParentClass object at 0x7fb21281d010>, 'class_parent': <class '__main__.ParentClass'>, '_i4': 'def func(a: int, b: str, c: List[int], d: Tuple[str, str], e: Union[int, str], **kwargs: Any) -> str:\n return a, b, c, d, e, kwargs', 'func': <function func at 0x7fb2f6d1e6c0>, '_i5': '@overload\ndef get_members_of_function_or_method(\n func_or_class: Type[object], predicate: Optional[Callable[[Any], bool]] = None\n) -> List[Tuple[str, Any]]: ...\n\n\n@overload\ndef get_members_of_function_or_method(\n func_or_class: Callable[..., Any], predicate: Optional[Callable[[Any], bool]] = None\n) -> List[Tuple[str, Any]]: ...\n\n\ndef get_members_of_function_or_method(\n func_or_class: Union[Type[object], Callable[..., Any]], predicate: Optional[Callable[[Any], bool]] = None\n) -> List[Tuple[str, Any]]:\n return inspect.getmembers(func_or_class, predicate)\n\n\ndef loop_through_members(members: List[Tuple[str, Any]], filter: Optional[str] = None) -> None:\n if filter is not None:\n members = [member for member in members if filter in member[0]]\n for member in members:\n name, value = member\n print(f"{name}: {value}")', 'get_members_of_function_or_method': <function get_members_of_function_or_method at 0x7fb2f6d1e560>, 'loop_through_members': <function loop_through_members at 0x7fb2f6d1e400>, '_i6': 'func_all_members = get_members_of_function_or_method(func, predicate=None)\nloop_through_members(func_all_members)', 'func_all_members': [('__annotate__', <function func.__annotate__ at 0x7fb2f6d1e980>), ('__annotations__', {'a': <class 'int'>, 'b': <class 'str'>, 'c': typing.List[int], 'd': typing.Tuple[str, str], 'e': int | str, 'kwargs': typing.Any, 'return': <class 'str'>}), ('__builtins__', {'__name__': 'builtins', '__doc__': "Built-in functions, types, exceptions, and other objects.\n\nThis module provides direct access to all 'built-in'\nidentifiers of Python; for example, builtins.len is\nthe full name for the built-in function len().\n\nThis module is not normally accessed explicitly by most\napplications, but can be useful in modules that provide\nobjects with the same name as a built-in value, but in\nwhich the built-in of that name is also needed.", '__package__': '', '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>, origin='built-in'), '__build_class__': <built-in function __build_class__>, '__import__': <built-in function __import__>, 'abs': <built-in function abs>, 'all': <built-in function all>, 'any': <built-in function any>, 'ascii': <built-in function ascii>, 'bin': <built-in function bin>, 'breakpoint': <built-in function breakpoint>, 'callable': <built-in function callable>, 'chr': <built-in function chr>, 'compile': <built-in function compile>, 'delattr': <built-in function delattr>, 'dir': <built-in function dir>, 'divmod': <built-in function divmod>, 'eval': <built-in function eval>, 'exec': <built-in function exec>, 'format': <built-in function format>, 'getattr': <built-in function getattr>, 'globals': <built-in function globals>, 'hasattr': <built-in function hasattr>, 'hash': <built-in function hash>, 'hex': <built-in function hex>, 'id': <built-in function id>, 'input': <bound method Kernel.raw_input of <ipykernel.ipkernel.IPythonKernel object at 0x7fb3364956a0>>, 'isinstance': <built-in function isinstance>, 'issubclass': <built-in function issubclass>, 'iter': <built-in function iter>, 'aiter': <built-in function aiter>, 'len': <built-in function len>, 'locals': <built-in function locals>, 'max': <built-in function max>, 'min': <built-in function min>, 'next': <built-in function next>, 'anext': <built-in function anext>, 'oct': <built-in function oct>, 'ord': <built-in function ord>, 'pow': <built-in function pow>, 'print': <built-in function print>, 'repr': <built-in function repr>, 'round': <built-in function round>, 'setattr': <built-in function setattr>, 'sorted': <built-in function sorted>, 'sum': <built-in function sum>, 'vars': <built-in function vars>, 'None': None, 'Ellipsis': Ellipsis, 'NotImplemented': NotImplemented, 'False': False, 'True': True, 'bool': <class 'bool'>, 'memoryview': <class 'memoryview'>, 'bytearray': <class 'bytearray'>, 'bytes': <class 'bytes'>, 'classmethod': <class 'classmethod'>, 'complex': <class 'complex'>, 'dict': <class 'dict'>, 'enumerate': <class 'enumerate'>, 'filter': <class 'filter'>, 'float': <class 'float'>, 'frozenset': <class 'frozenset'>, 'property': <class 'property'>, 'int': <class 'int'>, 'list': <class 'list'>, 'map': <class 'map'>, 'object': <class 'object'>, 'range': <class 'range'>, 'reversed': <class 'reversed'>, 'set': <class 'set'>, 'slice': <class 'slice'>, 'staticmethod': <class 'staticmethod'>, 'str': <class 'str'>, 'super': <class 'super'>, 'tuple': <class 'tuple'>, 'type': <class 'type'>, 'zip': <class 'zip'>, '__debug__': True, 'BaseException': <class 'BaseException'>, 'BaseExceptionGroup': <class 'BaseExceptionGroup'>, 'Exception': <class 'Exception'>, 'GeneratorExit': <class 'GeneratorExit'>, 'KeyboardInterrupt': <class 'KeyboardInterrupt'>, 'SystemExit': <class 'SystemExit'>, 'ArithmeticError': <class 'ArithmeticError'>, 'AssertionError': <class 'AssertionError'>, 'AttributeError': <class 'AttributeError'>, 'BufferError': <class 'BufferError'>, 'EOFError': <class 'EOFError'>, 'ImportError': <class 'ImportError'>, 'LookupError': <class 'LookupError'>, 'MemoryError': <class 'MemoryError'>, 'NameError': <class 'NameError'>, 'OSError': <class 'OSError'>, 'ReferenceError': <class 'ReferenceError'>, 'RuntimeError': <class 'RuntimeError'>, 'StopAsyncIteration': <class 'StopAsyncIteration'>, 'StopIteration': <class 'StopIteration'>, 'SyntaxError': <class 'SyntaxError'>, 'SystemError': <class 'SystemError'>, 'TypeError': <class 'TypeError'>, 'ValueError': <class 'ValueError'>, 'Warning': <class 'Warning'>, 'FloatingPointError': <class 'FloatingPointError'>, 'OverflowError': <class 'OverflowError'>, 'ZeroDivisionError': <class 'ZeroDivisionError'>, 'BytesWarning': <class 'BytesWarning'>, 'DeprecationWarning': <class 'DeprecationWarning'>, 'EncodingWarning': <class 'EncodingWarning'>, 'FutureWarning': <class 'FutureWarning'>, 'ImportWarning': <class 'ImportWarning'>, 'PendingDeprecationWarning': <class 'PendingDeprecationWarning'>, 'ResourceWarning': <class 'ResourceWarning'>, 'RuntimeWarning': <class 'RuntimeWarning'>, 'SyntaxWarning': <class 'SyntaxWarning'>, 'UnicodeWarning': <class 'UnicodeWarning'>, 'UserWarning': <class 'UserWarning'>, 'BlockingIOError': <class 'BlockingIOError'>, 'ChildProcessError': <class 'ChildProcessError'>, 'ConnectionError': <class 'ConnectionError'>, 'FileExistsError': <class 'FileExistsError'>, 'FileNotFoundError': <class 'FileNotFoundError'>, 'InterruptedError': <class 'InterruptedError'>, 'IsADirectoryError': <class 'IsADirectoryError'>, 'NotADirectoryError': <class 'NotADirectoryError'>, 'PermissionError': <class 'PermissionError'>, 'ProcessLookupError': <class 'ProcessLookupError'>, 'TimeoutError': <class 'TimeoutError'>, 'IndentationError': <class 'IndentationError'>, '_IncompleteInputError': <class '_IncompleteInputError'>, 'IndexError': <class 'IndexError'>, 'KeyError': <class 'KeyError'>, 'ModuleNotFoundError': <class 'ModuleNotFoundError'>, 'NotImplementedError': <class 'NotImplementedError'>, 'PythonFinalizationError': <class 'PythonFinalizationError'>, 'RecursionError': <class 'RecursionError'>, 'UnboundLocalError': <class 'UnboundLocalError'>, 'UnicodeError': <class 'UnicodeError'>, 'BrokenPipeError': <class 'BrokenPipeError'>, 'ConnectionAbortedError': <class 'ConnectionAbortedError'>, 'ConnectionRefusedError': <class 'ConnectionRefusedError'>, 'ConnectionResetError': <class 'ConnectionResetError'>, 'TabError': <class 'TabError'>, 'UnicodeDecodeError': <class 'UnicodeDecodeError'>, 'UnicodeEncodeError': <class 'UnicodeEncodeError'>, 'UnicodeTranslateError': <class 'UnicodeTranslateError'>, 'ExceptionGroup': <class 'ExceptionGroup'>, 'EnvironmentError': <class 'OSError'>, 'IOError': <class 'OSError'>, 'open': <built-in function open>, 'copyright': Copyright (c) 2001 Python Software Foundation.
All Rights Reserved.
Copyright (c) 2000 BeOpen.com.
All Rights Reserved.
Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.
Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved., 'credits': Thanks to CWI, CNRI, BeOpen, Zope Corporation, the Python Software
Foundation, and a cast of thousands for supporting Python
development. See www.python.org for more information., 'license': Type license() to see the full license text, 'help': Type help() for interactive help, or help(object) for help about object., 'execfile': <function execfile at 0x7fb3343c5640>, 'runfile': <function runfile at 0x7fb3342dfab0>, '__IPYTHON__': True, 'display': <function display at 0x7fb3374f6e50>, 'get_ipython': <bound method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x7fb3341a3620>>}), ('__call__', <method-wrapper '__call__' of function object at 0x7fb2f6d1e6c0>), ('__class__', <class 'function'>), ('__closure__', None), ('__code__', <code object func at 0x7fb21299d7d0, file "/tmp/ipykernel_4509/2139551385.py", line 1>), ('__defaults__', None), ('__delattr__', <method-wrapper '__delattr__' of function object at 0x7fb2f6d1e6c0>), ('__dict__', {}), ('__dir__', <built-in method __dir__ of function object at 0x7fb2f6d1e6c0>), ('__doc__', None), ('__eq__', <method-wrapper '__eq__' of function object at 0x7fb2f6d1e6c0>), ('__format__', <built-in method __format__ of function object at 0x7fb2f6d1e6c0>), ('__ge__', <method-wrapper '__ge__' of function object at 0x7fb2f6d1e6c0>), ('__get__', <method-wrapper '__get__' of function object at 0x7fb2f6d1e6c0>), ('__getattribute__', <method-wrapper '__getattribute__' of function object at 0x7fb2f6d1e6c0>), ('__getstate__', <built-in method __getstate__ of function object at 0x7fb2f6d1e6c0>), ('__globals__', {...}), ('__gt__', <method-wrapper '__gt__' of function object at 0x7fb2f6d1e6c0>), ('__hash__', <method-wrapper '__hash__' of function object at 0x7fb2f6d1e6c0>), ('__init__', <method-wrapper '__init__' of function object at 0x7fb2f6d1e6c0>), ('__init_subclass__', <built-in method __init_subclass__ of type object at 0x158aa28>), ('__kwdefaults__', None), ('__le__', <method-wrapper '__le__' of function object at 0x7fb2f6d1e6c0>), ('__lt__', <method-wrapper '__lt__' of function object at 0x7fb2f6d1e6c0>), ('__module__', '__main__'), ('__name__', 'func'), ('__ne__', <method-wrapper '__ne__' of function object at 0x7fb2f6d1e6c0>), ('__new__', <built-in method __new__ of type object at 0x158aa28>), ('__qualname__', 'func'), ('__reduce__', <built-in method __reduce__ of function object at 0x7fb2f6d1e6c0>), ('__reduce_ex__', <built-in method __reduce_ex__ of function object at 0x7fb2f6d1e6c0>), ('__repr__', <method-wrapper '__repr__' of function object at 0x7fb2f6d1e6c0>), ('__setattr__', <method-wrapper '__setattr__' of function object at 0x7fb2f6d1e6c0>), ('__sizeof__', <built-in method __sizeof__ of function object at 0x7fb2f6d1e6c0>), ('__str__', <method-wrapper '__str__' of function object at 0x7fb2f6d1e6c0>), ('__subclasshook__', <built-in method __subclasshook__ of type object at 0x158aa28>), ('__type_params__', ())]}
__gt__: <method-wrapper '__gt__' of function object at 0x7fb2f6d1e6c0>
__hash__: <method-wrapper '__hash__' of function object at 0x7fb2f6d1e6c0>
__init__: <method-wrapper '__init__' of function object at 0x7fb2f6d1e6c0>
__init_subclass__: <built-in method __init_subclass__ of type object at 0x158aa28>
__kwdefaults__: None
__le__: <method-wrapper '__le__' of function object at 0x7fb2f6d1e6c0>
__lt__: <method-wrapper '__lt__' of function object at 0x7fb2f6d1e6c0>
__module__: __main__
__name__: func
__ne__: <method-wrapper '__ne__' of function object at 0x7fb2f6d1e6c0>
__new__: <built-in method __new__ of type object at 0x158aa28>
__qualname__: func
__reduce__: <built-in method __reduce__ of function object at 0x7fb2f6d1e6c0>
__reduce_ex__: <built-in method __reduce_ex__ of function object at 0x7fb2f6d1e6c0>
__repr__: <method-wrapper '__repr__' of function object at 0x7fb2f6d1e6c0>
__setattr__: <method-wrapper '__setattr__' of function object at 0x7fb2f6d1e6c0>
__sizeof__: <built-in method __sizeof__ of function object at 0x7fb2f6d1e6c0>
__str__: <method-wrapper '__str__' of function object at 0x7fb2f6d1e6c0>
__subclasshook__: <built-in method __subclasshook__ of type object at 0x158aa28>
__type_params__: ()
And to get the signature, we can just filter '__annotations__'.
loop_through_members(func_all_members, filter="__annotations__")
__annotations__: {'a': <class 'int'>, 'b': <class 'str'>, 'c': typing.List[int], 'd': typing.Tuple[str, str], 'e': int | str, 'kwargs': typing.Any, 'return': <class 'str'>}
class_child_all_members = get_members_of_function_or_method(class_child, predicate=None)
loop_through_members(class_child_all_members)
_ChildClass__private_attr: a private attribute
__class__: <class 'type'>
__delattr__: <slot wrapper '__delattr__' of 'object' objects>
__dict__: {'__module__': '__main__', '__firstlineno__': 14, '__doc__': 'This is a subclass of ParentClass.', 'class_attr': 'a class attribute', '_protected_attr': 'a protected attribute', '_ChildClass__private_attr': 'a private attribute', '__init__': <function ChildClass.__init__ at 0x7fb2f6d1f270>, 'read_only_attr': <property object at 0x7fb2f6ffb9c0>, 'instance_method': <function ChildClass.instance_method at 0x7fb2f6d1efb0>, 'class_method': <classmethod(<function ChildClass.class_method at 0x7fb2f6d1ee50>)>, 'static_method': <staticmethod(<function ChildClass.static_method at 0x7fb2f6d1eae0>)>, '__str__': <function ChildClass.__str__ at 0x7fb2f6d1eb90>, '__static_attributes__': ('_private_instance_attr', 'instance_attr', 'instance_not_in_constructor_attr')}
__dir__: <method '__dir__' of 'object' objects>
__doc__: This is a subclass of ParentClass.
__eq__: <slot wrapper '__eq__' of 'object' objects>
__firstlineno__: 14
__format__: <method '__format__' of 'object' objects>
__ge__: <slot wrapper '__ge__' of 'object' objects>
__getattribute__: <slot wrapper '__getattribute__' of 'object' objects>
__getstate__: <method '__getstate__' of 'object' objects>
__gt__: <slot wrapper '__gt__' of 'object' objects>
__hash__: <slot wrapper '__hash__' of 'object' objects>
__init__: <function ChildClass.__init__ at 0x7fb2f6d1f270>
__init_subclass__: <built-in method __init_subclass__ of type object at 0x126c7a40>
__le__: <slot wrapper '__le__' of 'object' objects>
__lt__: <slot wrapper '__lt__' of 'object' objects>
__module__: __main__
__ne__: <slot wrapper '__ne__' of 'object' objects>
__new__: <built-in method __new__ of type object at 0x1596880>
__reduce__: <method '__reduce__' of 'object' objects>
__reduce_ex__: <method '__reduce_ex__' of 'object' objects>
__repr__: <slot wrapper '__repr__' of 'object' objects>
__setattr__: <slot wrapper '__setattr__' of 'object' objects>
__sizeof__: <method '__sizeof__' of 'object' objects>
__static_attributes__: ('_private_instance_attr', 'instance_attr', 'instance_not_in_constructor_attr')
__str__: <function ChildClass.__str__ at 0x7fb2f6d1eb90>
__subclasshook__: <built-in method __subclasshook__ of type object at 0x126c7a40>
__weakref__: <attribute '__weakref__' of 'ParentClass' objects>
_protected_attr: a protected attribute
class_attr: a class attribute
class_method: <bound method ChildClass.class_method of <class '__main__.ChildClass'>>
instance_method: <function ChildClass.instance_method at 0x7fb2f6d1efb0>
parent_class_attr: a parent class attribute
parent_method: <function ParentClass.parent_method at 0x7fb2f6d1f3d0>
read_only_attr: <property object at 0x7fb2f6ffb9c0>
static_method: <function ChildClass.static_method at 0x7fb2f6d1eae0>
instance_child_all_members = get_members_of_function_or_method(instance_child, predicate=None)
loop_through_members(instance_child_all_members)
_ChildClass__private_attr: a private attribute
__class__: <class '__main__.ChildClass'>
__delattr__: <method-wrapper '__delattr__' of ChildClass object at 0x7fb21281cec0>
__dict__: {'parent_instance_attr': 'a parent instance attribute', 'instance_attr': 'an instance attribute', 'instance_not_in_constructor_attr': 'an instance attribute not in the constructor', '_private_instance_attr': 'a private instance attribute'}
__dir__: <built-in method __dir__ of ChildClass object at 0x7fb21281cec0>
__doc__: This is a subclass of ParentClass.
__eq__: <method-wrapper '__eq__' of ChildClass object at 0x7fb21281cec0>
__firstlineno__: 14
__format__: <built-in method __format__ of ChildClass object at 0x7fb21281cec0>
__ge__: <method-wrapper '__ge__' of ChildClass object at 0x7fb21281cec0>
__getattribute__: <method-wrapper '__getattribute__' of ChildClass object at 0x7fb21281cec0>
__getstate__: <built-in method __getstate__ of ChildClass object at 0x7fb21281cec0>
__gt__: <method-wrapper '__gt__' of ChildClass object at 0x7fb21281cec0>
__hash__: <method-wrapper '__hash__' of ChildClass object at 0x7fb21281cec0>
__init__: <bound method ChildClass.__init__ of <__main__.ChildClass object at 0x7fb21281cec0>>
__init_subclass__: <built-in method __init_subclass__ of type object at 0x126c7a40>
__le__: <method-wrapper '__le__' of ChildClass object at 0x7fb21281cec0>
__lt__: <method-wrapper '__lt__' of ChildClass object at 0x7fb21281cec0>
__module__: __main__
__ne__: <method-wrapper '__ne__' of ChildClass object at 0x7fb21281cec0>
__new__: <built-in method __new__ of type object at 0x1596880>
__reduce__: <built-in method __reduce__ of ChildClass object at 0x7fb21281cec0>
__reduce_ex__: <built-in method __reduce_ex__ of ChildClass object at 0x7fb21281cec0>
__repr__: <method-wrapper '__repr__' of ChildClass object at 0x7fb21281cec0>
__setattr__: <method-wrapper '__setattr__' of ChildClass object at 0x7fb21281cec0>
__sizeof__: <built-in method __sizeof__ of ChildClass object at 0x7fb21281cec0>
__static_attributes__: ('_private_instance_attr', 'instance_attr', 'instance_not_in_constructor_attr')
__str__: <bound method ChildClass.__str__ of <__main__.ChildClass object at 0x7fb21281cec0>>
__subclasshook__: <built-in method __subclasshook__ of type object at 0x126c7a40>
__weakref__: None
_private_instance_attr: a private instance attribute
_protected_attr: a protected attribute
class_attr: a class attribute
class_method: <bound method ChildClass.class_method of <class '__main__.ChildClass'>>
instance_attr: an instance attribute
instance_method: <bound method ChildClass.instance_method of <__main__.ChildClass object at 0x7fb21281cec0>>
instance_not_in_constructor_attr: an instance attribute not in the constructor
parent_class_attr: a parent class attribute
parent_instance_attr: a parent instance attribute
parent_method: <bound method ParentClass.parent_method of <__main__.ChildClass object at 0x7fb21281cec0>>
read_only_attr: You can read me, but you cannot change me.
static_method: <function ChildClass.static_method at 0x7fb2f6d1eae0>
trainer_all_members = get_members_of_function_or_method(Trainer, predicate=None)
loop_through_members(trainer_all_members)
__backends: ['torch', 'accelerate']
__class__: <class 'type'>
__delattr__: <slot wrapper '__delattr__' of 'object' objects>
__dict__: {'__module__': 'transformers.trainer', '__firstlineno__': 251, '__doc__': "\nTrainer is a simple but feature-complete training and eval loop for PyTorch, optimized for 🤗 Transformers.\n\nArgs:\n model ([`PreTrainedModel`] or `torch.nn.Module`, *optional*):\n The model to train, evaluate or use for predictions. If not provided, a `model_init` must be passed.\n\n <Tip>\n\n [`Trainer`] is optimized to work with the [`PreTrainedModel`] provided by the library. You can still use\n your own models defined as `torch.nn.Module` as long as they work the same way as the 🤗 Transformers\n models.\n\n </Tip>\n\n args ([`TrainingArguments`], *optional*):\n The arguments to tweak for training. Will default to a basic instance of [`TrainingArguments`] with the\n `output_dir` set to a directory named *tmp_trainer* in the current directory if not provided.\n data_collator (`DataCollator`, *optional*):\n The function to use to form a batch from a list of elements of `train_dataset` or `eval_dataset`. Will\n default to [`default_data_collator`] if no `processing_class` is provided, an instance of\n [`DataCollatorWithPadding`] otherwise if the processing_class is a feature extractor or tokenizer.\n train_dataset (`torch.utils.data.Dataset` | `torch.utils.data.IterableDataset` | `datasets.Dataset`, *optional*):\n The dataset to use for training. If it is a [`~datasets.Dataset`], columns not accepted by the\n `model.forward()` method are automatically removed.\n\n Note that if it's a `torch.utils.data.IterableDataset` with some randomization and you are training in a\n distributed fashion, your iterable dataset should either use a internal attribute `generator` that is a\n `torch.Generator` for the randomization that must be identical on all processes (and the Trainer will\n manually set the seed of this `generator` at each epoch) or have a `set_epoch()` method that internally\n sets the seed of the RNGs used.\n eval_dataset (`torch.utils.data.Dataset` | dict[str, `torch.utils.data.Dataset`] | `datasets.Dataset`, *optional*):\n The dataset to use for evaluation. If it is a [`~datasets.Dataset`], columns not accepted by the\n `model.forward()` method are automatically removed. If it is a dictionary, it will evaluate on each\n dataset prepending the dictionary key to the metric name.\n processing_class (`PreTrainedTokenizerBase` or `BaseImageProcessor` or `FeatureExtractionMixin` or `ProcessorMixin`, *optional*):\n Processing class used to process the data. If provided, will be used to automatically process the inputs\n for the model, and it will be saved along the model to make it easier to rerun an interrupted training or\n reuse the fine-tuned model.\n model_init (`Callable[[], PreTrainedModel]`, *optional*):\n A function that instantiates the model to be used. If provided, each call to [`~Trainer.train`] will start\n from a new instance of the model as given by this function.\n\n The function may have zero argument, or a single one containing the optuna/Ray Tune trial object, to\n be able to choose different architectures according to hyperparameters (such as layer count, sizes of\n inner layers, dropout probabilities etc).\n compute_loss_func (`Callable`, *optional*):\n A function that accepts the raw model outputs, labels, and the number of items in the entire accumulated\n batch (batch_size * gradient_accumulation_steps) and returns the loss. For example, see the default [loss function](https://github.com/huggingface/transformers/blob/052e652d6d53c2b26ffde87e039b723949a53493/src/transformers/trainer.py#L3618) used by [`Trainer`].\n compute_metrics (`Callable[[EvalPrediction], Dict]`, *optional*):\n The function that will be used to compute metrics at evaluation. Must take a [`EvalPrediction`] and return\n a dictionary string to metric values. *Note* When passing TrainingArgs with `batch_eval_metrics` set to\n `True`, your compute_metrics function must take a boolean `compute_result` argument. This will be triggered\n after the last eval batch to signal that the function needs to calculate and return the global summary\n statistics rather than accumulating the batch-level statistics\n callbacks (List of [`TrainerCallback`], *optional*):\n A list of callbacks to customize the training loop. Will add those to the list of default callbacks\n detailed in [here](callback).\n\n If you want to remove one of the default callbacks used, use the [`Trainer.remove_callback`] method.\n optimizers (`tuple[torch.optim.Optimizer, torch.optim.lr_scheduler.LambdaLR]`, *optional*, defaults to `(None, None)`):\n A tuple containing the optimizer and the scheduler to use. Will default to an instance of [`AdamW`] on your\n model and a scheduler given by [`get_linear_schedule_with_warmup`] controlled by `args`.\n optimizer_cls_and_kwargs (`tuple[Type[torch.optim.Optimizer], dict[str, Any]]`, *optional*):\n A tuple containing the optimizer class and keyword arguments to use.\n Overrides `optim` and `optim_args` in `args`. Incompatible with the `optimizers` argument.\n\n Unlike `optimizers`, this argument avoids the need to place model parameters on the correct devices before initializing the Trainer.\n preprocess_logits_for_metrics (`Callable[[torch.Tensor, torch.Tensor], torch.Tensor]`, *optional*):\n A function that preprocess the logits right before caching them at each evaluation step. Must take two\n tensors, the logits and the labels, and return the logits once processed as desired. The modifications made\n by this function will be reflected in the predictions received by `compute_metrics`.\n\n Note that the labels (second parameter) will be `None` if the dataset does not have them.\n\nImportant attributes:\n\n - **model** -- Always points to the core model. If using a transformers model, it will be a [`PreTrainedModel`]\n subclass.\n - **model_wrapped** -- Always points to the most external model in case one or more other modules wrap the\n original model. This is the model that should be used for the forward pass. For example, under `DeepSpeed`,\n the inner model is wrapped in `DeepSpeed` and then again in `torch.nn.DistributedDataParallel`. If the inner\n model hasn't been wrapped, then `self.model_wrapped` is the same as `self.model`.\n - **is_model_parallel** -- Whether or not a model has been switched to a model parallel mode (different from\n data parallelism, this means some of the model layers are split on different GPUs).\n - **place_model_on_device** -- Whether or not to automatically place the model on the device. Defaults to\n `True` unless model parallel, DeepSpeed, FSDP, full fp16/bf16 eval, or SageMaker MP is active. Can be\n overridden by subclassing `TrainingArguments` and overriding the `place_model_on_device` property.\n - **is_in_train** -- Whether or not a model is currently running `train` (e.g. when `evaluate` is called while\n in `train`)\n\n", 'get_learning_rates': <function get_learning_rates at 0x7fb213771dd0>, 'get_num_trainable_parameters': <function get_num_trainable_parameters at 0x7fb213771c70>, 'get_optimizer_group': <function get_optimizer_group at 0x7fb213771f30>, 'log_metrics': <function log_metrics at 0x7fb2137719b0>, 'metrics_format': <function metrics_format at 0x7fb213771900>, 'save_metrics': <function save_metrics at 0x7fb213771a60>, 'save_state': <function save_state at 0x7fb213771b10>, '__init__': <function Trainer.__init__ at 0x7fb212ab14e0>, '_validate_args': <function Trainer._validate_args at 0x7fb212985850>, '_build_accelerator_args': <function Trainer._build_accelerator_args at 0x7fb2129859b0>, 'create_accelerator_and_postprocess': <function Trainer.create_accelerator_and_postprocess at 0x7fb212985b10>, 'get_train_dataloader': <function Trainer.get_train_dataloader at 0x7fb212985c70>, 'get_eval_dataloader': <function Trainer.get_eval_dataloader at 0x7fb212985dd0>, 'get_test_dataloader': <function Trainer.get_test_dataloader at 0x7fb212985f30>, 'num_examples': <function Trainer.num_examples at 0x7fb212986090>, '_get_dataloader': <function Trainer._get_dataloader at 0x7fb2129861f0>, '_get_train_sampler': <function Trainer._get_train_sampler at 0x7fb212986350>, '_get_eval_sampler': <function Trainer._get_eval_sampler at 0x7fb2129864b0>, '_set_signature_columns_if_needed': <function Trainer._set_signature_columns_if_needed at 0x7fb212986610>, '_remove_unused_columns': <function Trainer._remove_unused_columns at 0x7fb212986770>, '_get_collator_with_removed_columns': <function Trainer._get_collator_with_removed_columns at 0x7fb2129868d0>, 'create_optimizer_and_scheduler': <function Trainer.create_optimizer_and_scheduler at 0x7fb212986a30>, 'create_optimizer': <function Trainer.create_optimizer at 0x7fb212986b90>, 'create_scheduler': <function Trainer.create_scheduler at 0x7fb212986cf0>, 'get_optimizer_cls_and_kwargs': <staticmethod(<function Trainer.get_optimizer_cls_and_kwargs at 0x7fb212986e50>)>, 'get_decay_parameter_names': <function Trainer.get_decay_parameter_names at 0x7fb21281b320>, '_get_learning_rate': <function Trainer._get_learning_rate at 0x7fb21281a820>, 'train': <function Trainer.train at 0x7fb212986fb0>, '_inner_training_loop': <function Trainer._inner_training_loop at 0x7fb212987110>, '_init_training_state': <function Trainer._init_training_state at 0x7fb212987270>, '_prepare_for_training': <function Trainer._prepare_for_training at 0x7fb212987320>, '_run_epoch': <function Trainer._run_epoch at 0x7fb2129873d0>, '_finalize_training': <function Trainer._finalize_training at 0x7fb212987480>, 'training_step': <function Trainer.training_step at 0x7fb2129875e0>, 'compute_loss': <function Trainer.compute_loss at 0x7fb212987740>, 'compute_loss_context_manager': <function Trainer.compute_loss_context_manager at 0x7fb2129878a0>, 'autocast_smart_context_manager': <function Trainer.autocast_smart_context_manager at 0x7fb212987a00>, '_maybe_log_save_evaluate': <function Trainer._maybe_log_save_evaluate at 0x7fb212987b60>, 'get_batch_samples': <function Trainer.get_batch_samples at 0x7fb212987cc0>, '_get_num_items_in_batch': <function Trainer._get_num_items_in_batch at 0x7fb212987e20>, '_prepare_input': <function Trainer._prepare_input at 0x7fb21296c040>, '_prepare_inputs': <function Trainer._prepare_inputs at 0x7fb21296c1a0>, '_prepare_context_parallel_inputs': <function Trainer._prepare_context_parallel_inputs at 0x7fb21296c300>, 'set_initial_training_values': <function Trainer.set_initial_training_values at 0x7fb21296c460>, 'get_total_train_batch_size': <function Trainer.get_total_train_batch_size at 0x7fb21296c5c0>, 'get_sp_size': <function Trainer.get_sp_size at 0x7fb21296c720>, 'get_cp_size': <function Trainer.get_cp_size at 0x7fb21296c880>, 'get_tp_size': <function Trainer.get_tp_size at 0x7fb21296c9e0>, '_wrap_model': <function Trainer._wrap_model at 0x7fb21296cb40>, '_update_auto_batch_size': <function Trainer._update_auto_batch_size at 0x7fb21296cbf0>, '_track_num_input_tokens': <function Trainer._track_num_input_tokens at 0x7fb21296cca0>, '_clip_grad_norm': <function Trainer._clip_grad_norm at 0x7fb21296cd50>, '_get_grad_norm': <function Trainer._get_grad_norm at 0x7fb21296ce00>, 'evaluate': <function Trainer.evaluate at 0x7fb21296cf60>, 'evaluation_loop': <function Trainer.evaluation_loop at 0x7fb21296d0c0>, 'predict': <function Trainer.predict at 0x7fb21296d220>, 'prediction_step': <function Trainer.prediction_step at 0x7fb21296d380>, '_evaluate': <function Trainer._evaluate at 0x7fb21296d4e0>, '_get_output_dir': <function Trainer._get_output_dir at 0x7fb21296d640>, '_save_checkpoint': <function Trainer._save_checkpoint at 0x7fb21296d7a0>, '_determine_best_metric': <function Trainer._determine_best_metric at 0x7fb21296d900>, '_save_rng_state': <function Trainer._save_rng_state at 0x7fb21296da60>, '_save_optimizer_and_scheduler': <function Trainer._save_optimizer_and_scheduler at 0x7fb21296dbc0>, '_save_scaler': <function Trainer._save_scaler at 0x7fb21296dd20>, '_load_from_checkpoint': <function Trainer._load_from_checkpoint at 0x7fb21296de80>, '_load_best_model': <function Trainer._load_best_model at 0x7fb21296dfe0>, '_load_rng_state': <function Trainer._load_rng_state at 0x7fb21296e140>, '_load_optimizer_and_scheduler': <function Trainer._load_optimizer_and_scheduler at 0x7fb21296e2a0>, '_load_scaler': <function Trainer._load_scaler at 0x7fb21296e400>, '_load_callback_state': <function Trainer._load_callback_state at 0x7fb21296e560>, '_issue_warnings_after_load': <function Trainer._issue_warnings_after_load at 0x7fb21296e6c0>, 'save_model': <function Trainer.save_model at 0x7fb21296e820>, '_save': <function Trainer._save at 0x7fb21296e980>, 'log': <function Trainer.log at 0x7fb21296eae0>, 'store_flos': <function Trainer.store_flos at 0x7fb21296ec40>, 'floating_point_ops': <function Trainer.floating_point_ops at 0x7fb21296eda0>, 'init_hf_repo': <function Trainer.init_hf_repo at 0x7fb21296ef00>, 'create_model_card': <function Trainer.create_model_card at 0x7fb21296f060>, 'push_to_hub': <function Trainer.push_to_hub at 0x7fb21296f1c0>, '_push_from_checkpoint': <function Trainer._push_from_checkpoint at 0x7fb21296f320>, '_finish_current_push': <function Trainer._finish_current_push at 0x7fb21296f480>, 'hyperparameter_search': <function Trainer.hyperparameter_search at 0x7fb21296f5e0>, 'call_model_init': <function Trainer.call_model_init at 0x7fb21296f740>, '_hp_search_setup': <function Trainer._hp_search_setup at 0x7fb21296f8a0>, '_report_to_hp_search': <function Trainer._report_to_hp_search at 0x7fb21296fa00>, '_tune_save_checkpoint': <function Trainer._tune_save_checkpoint at 0x7fb21296fb60>, 'add_callback': <function Trainer.add_callback at 0x7fb21296fcc0>, 'pop_callback': <function Trainer.pop_callback at 0x7fb21296fe20>, 'remove_callback': <function Trainer.remove_callback at 0x7fb212968040>, 'is_local_process_zero': <function Trainer.is_local_process_zero at 0x7fb2129681a0>, 'is_world_process_zero': <function Trainer.is_world_process_zero at 0x7fb212968300>, '_move_model_to_device': <function Trainer._move_model_to_device at 0x7fb212968460>, '__static_attributes__': ('_attn_mask_causal_checked', '_created_lr_scheduler', '_eval_dataloaders', '_globalstep_last_logged', '_initial_num_input_tokens_seen', '_loggers_initialized', '_loss_shifts_labels', '_memory_tracker', '_signature_columns', '_total_loss_scalar', '_tr_loss', '_train_batch_size', '_trial', 'accelerator', 'args', 'callback_handler', 'can_return_loss', 'compute_loss_func', 'compute_metrics', 'compute_objective', 'control', 'current_flos', 'current_gradient_accumulation_steps', 'data_collator', 'deepspeed', 'eval_dataset', 'gather_function', 'hp_name', 'hp_search_backend', 'hp_space', 'hub_model_id', 'is_deepspeed_enabled', 'is_fsdp_enabled', 'is_fsdp_xla_enabled', 'is_fsdp_xla_v1_enabled', 'is_fsdp_xla_v2_enabled', 'is_in_train', 'is_model_parallel', 'label_names', 'label_smoother', 'lr_scheduler', 'model', 'model_accepts_loss_kwargs', 'model_init', 'model_preparation_time', 'model_wrapped', 'neftune_hook_handle', 'neftune_noise_alpha', 'objective', 'optimizer', 'optimizer_cls_and_kwargs', 'place_model_on_device', 'preprocess_logits_for_metrics', 'processing_class', 'push_in_progress', 'state', 'train_dataset'), '__dict__': <attribute '__dict__' of 'Trainer' objects>, '__weakref__': <attribute '__weakref__' of 'Trainer' objects>, '__backends': ['torch', 'accelerate']}
__dir__: <method '__dir__' of 'object' objects>
__doc__:
Trainer is a simple but feature-complete training and eval loop for PyTorch, optimized for 🤗 Transformers.
Args:
model ([`PreTrainedModel`] or `torch.nn.Module`, *optional*):
The model to train, evaluate or use for predictions. If not provided, a `model_init` must be passed.
<Tip>
[`Trainer`] is optimized to work with the [`PreTrainedModel`] provided by the library. You can still use
your own models defined as `torch.nn.Module` as long as they work the same way as the 🤗 Transformers
models.
</Tip>
args ([`TrainingArguments`], *optional*):
The arguments to tweak for training. Will default to a basic instance of [`TrainingArguments`] with the
`output_dir` set to a directory named *tmp_trainer* in the current directory if not provided.
data_collator (`DataCollator`, *optional*):
The function to use to form a batch from a list of elements of `train_dataset` or `eval_dataset`. Will
default to [`default_data_collator`] if no `processing_class` is provided, an instance of
[`DataCollatorWithPadding`] otherwise if the processing_class is a feature extractor or tokenizer.
train_dataset (`torch.utils.data.Dataset` | `torch.utils.data.IterableDataset` | `datasets.Dataset`, *optional*):
The dataset to use for training. If it is a [`~datasets.Dataset`], columns not accepted by the
`model.forward()` method are automatically removed.
Note that if it's a `torch.utils.data.IterableDataset` with some randomization and you are training in a
distributed fashion, your iterable dataset should either use a internal attribute `generator` that is a
`torch.Generator` for the randomization that must be identical on all processes (and the Trainer will
manually set the seed of this `generator` at each epoch) or have a `set_epoch()` method that internally
sets the seed of the RNGs used.
eval_dataset (`torch.utils.data.Dataset` | dict[str, `torch.utils.data.Dataset`] | `datasets.Dataset`, *optional*):
The dataset to use for evaluation. If it is a [`~datasets.Dataset`], columns not accepted by the
`model.forward()` method are automatically removed. If it is a dictionary, it will evaluate on each
dataset prepending the dictionary key to the metric name.
processing_class (`PreTrainedTokenizerBase` or `BaseImageProcessor` or `FeatureExtractionMixin` or `ProcessorMixin`, *optional*):
Processing class used to process the data. If provided, will be used to automatically process the inputs
for the model, and it will be saved along the model to make it easier to rerun an interrupted training or
reuse the fine-tuned model.
model_init (`Callable[[], PreTrainedModel]`, *optional*):
A function that instantiates the model to be used. If provided, each call to [`~Trainer.train`] will start
from a new instance of the model as given by this function.
The function may have zero argument, or a single one containing the optuna/Ray Tune trial object, to
be able to choose different architectures according to hyperparameters (such as layer count, sizes of
inner layers, dropout probabilities etc).
compute_loss_func (`Callable`, *optional*):
A function that accepts the raw model outputs, labels, and the number of items in the entire accumulated
batch (batch_size * gradient_accumulation_steps) and returns the loss. For example, see the default [loss function](https://github.com/huggingface/transformers/blob/052e652d6d53c2b26ffde87e039b723949a53493/src/transformers/trainer.py#L3618) used by [`Trainer`].
compute_metrics (`Callable[[EvalPrediction], Dict]`, *optional*):
The function that will be used to compute metrics at evaluation. Must take a [`EvalPrediction`] and return
a dictionary string to metric values. *Note* When passing TrainingArgs with `batch_eval_metrics` set to
`True`, your compute_metrics function must take a boolean `compute_result` argument. This will be triggered
after the last eval batch to signal that the function needs to calculate and return the global summary
statistics rather than accumulating the batch-level statistics
callbacks (List of [`TrainerCallback`], *optional*):
A list of callbacks to customize the training loop. Will add those to the list of default callbacks
detailed in [here](callback).
If you want to remove one of the default callbacks used, use the [`Trainer.remove_callback`] method.
optimizers (`tuple[torch.optim.Optimizer, torch.optim.lr_scheduler.LambdaLR]`, *optional*, defaults to `(None, None)`):
A tuple containing the optimizer and the scheduler to use. Will default to an instance of [`AdamW`] on your
model and a scheduler given by [`get_linear_schedule_with_warmup`] controlled by `args`.
optimizer_cls_and_kwargs (`tuple[Type[torch.optim.Optimizer], dict[str, Any]]`, *optional*):
A tuple containing the optimizer class and keyword arguments to use.
Overrides `optim` and `optim_args` in `args`. Incompatible with the `optimizers` argument.
Unlike `optimizers`, this argument avoids the need to place model parameters on the correct devices before initializing the Trainer.
preprocess_logits_for_metrics (`Callable[[torch.Tensor, torch.Tensor], torch.Tensor]`, *optional*):
A function that preprocess the logits right before caching them at each evaluation step. Must take two
tensors, the logits and the labels, and return the logits once processed as desired. The modifications made
by this function will be reflected in the predictions received by `compute_metrics`.
Note that the labels (second parameter) will be `None` if the dataset does not have them.
Important attributes:
- **model** -- Always points to the core model. If using a transformers model, it will be a [`PreTrainedModel`]
subclass.
- **model_wrapped** -- Always points to the most external model in case one or more other modules wrap the
original model. This is the model that should be used for the forward pass. For example, under `DeepSpeed`,
the inner model is wrapped in `DeepSpeed` and then again in `torch.nn.DistributedDataParallel`. If the inner
model hasn't been wrapped, then `self.model_wrapped` is the same as `self.model`.
- **is_model_parallel** -- Whether or not a model has been switched to a model parallel mode (different from
data parallelism, this means some of the model layers are split on different GPUs).
- **place_model_on_device** -- Whether or not to automatically place the model on the device. Defaults to
`True` unless model parallel, DeepSpeed, FSDP, full fp16/bf16 eval, or SageMaker MP is active. Can be
overridden by subclassing `TrainingArguments` and overriding the `place_model_on_device` property.
- **is_in_train** -- Whether or not a model is currently running `train` (e.g. when `evaluate` is called while
in `train`)
__eq__: <slot wrapper '__eq__' of 'object' objects>
__firstlineno__: 251
__format__: <method '__format__' of 'object' objects>
__ge__: <slot wrapper '__ge__' of 'object' objects>
__getattribute__: <slot wrapper '__getattribute__' of 'object' objects>
__getstate__: <method '__getstate__' of 'object' objects>
__gt__: <slot wrapper '__gt__' of 'object' objects>
__hash__: <slot wrapper '__hash__' of 'object' objects>
__init__: <function Trainer.__init__ at 0x7fb212ab14e0>
__init_subclass__: <built-in method __init_subclass__ of type object at 0x129ed820>
__le__: <slot wrapper '__le__' of 'object' objects>
__lt__: <slot wrapper '__lt__' of 'object' objects>
__module__: transformers.trainer
__ne__: <slot wrapper '__ne__' of 'object' objects>
__new__: <built-in method __new__ of type object at 0x1596880>
__reduce__: <method '__reduce__' of 'object' objects>
__reduce_ex__: <method '__reduce_ex__' of 'object' objects>
__repr__: <slot wrapper '__repr__' of 'object' objects>
__setattr__: <slot wrapper '__setattr__' of 'object' objects>
__sizeof__: <method '__sizeof__' of 'object' objects>
__static_attributes__: ('_attn_mask_causal_checked', '_created_lr_scheduler', '_eval_dataloaders', '_globalstep_last_logged', '_initial_num_input_tokens_seen', '_loggers_initialized', '_loss_shifts_labels', '_memory_tracker', '_signature_columns', '_total_loss_scalar', '_tr_loss', '_train_batch_size', '_trial', 'accelerator', 'args', 'callback_handler', 'can_return_loss', 'compute_loss_func', 'compute_metrics', 'compute_objective', 'control', 'current_flos', 'current_gradient_accumulation_steps', 'data_collator', 'deepspeed', 'eval_dataset', 'gather_function', 'hp_name', 'hp_search_backend', 'hp_space', 'hub_model_id', 'is_deepspeed_enabled', 'is_fsdp_enabled', 'is_fsdp_xla_enabled', 'is_fsdp_xla_v1_enabled', 'is_fsdp_xla_v2_enabled', 'is_in_train', 'is_model_parallel', 'label_names', 'label_smoother', 'lr_scheduler', 'model', 'model_accepts_loss_kwargs', 'model_init', 'model_preparation_time', 'model_wrapped', 'neftune_hook_handle', 'neftune_noise_alpha', 'objective', 'optimizer', 'optimizer_cls_and_kwargs', 'place_model_on_device', 'preprocess_logits_for_metrics', 'processing_class', 'push_in_progress', 'state', 'train_dataset')
__str__: <slot wrapper '__str__' of 'object' objects>
__subclasshook__: <built-in method __subclasshook__ of type object at 0x129ed820>
__weakref__: <attribute '__weakref__' of 'Trainer' objects>
_build_accelerator_args: <function Trainer._build_accelerator_args at 0x7fb2129859b0>
_clip_grad_norm: <function Trainer._clip_grad_norm at 0x7fb21296cd50>
_determine_best_metric: <function Trainer._determine_best_metric at 0x7fb21296d900>
_evaluate: <function Trainer._evaluate at 0x7fb21296d4e0>
_finalize_training: <function Trainer._finalize_training at 0x7fb212987480>
_finish_current_push: <function Trainer._finish_current_push at 0x7fb21296f480>
_get_collator_with_removed_columns: <function Trainer._get_collator_with_removed_columns at 0x7fb2129868d0>
_get_dataloader: <function Trainer._get_dataloader at 0x7fb2129861f0>
_get_eval_sampler: <function Trainer._get_eval_sampler at 0x7fb2129864b0>
_get_grad_norm: <function Trainer._get_grad_norm at 0x7fb21296ce00>
_get_learning_rate: <function Trainer._get_learning_rate at 0x7fb21281a820>
_get_num_items_in_batch: <function Trainer._get_num_items_in_batch at 0x7fb212987e20>
_get_output_dir: <function Trainer._get_output_dir at 0x7fb21296d640>
_get_train_sampler: <function Trainer._get_train_sampler at 0x7fb212986350>
_hp_search_setup: <function Trainer._hp_search_setup at 0x7fb21296f8a0>
_init_training_state: <function Trainer._init_training_state at 0x7fb212987270>
_inner_training_loop: <function Trainer._inner_training_loop at 0x7fb212987110>
_issue_warnings_after_load: <function Trainer._issue_warnings_after_load at 0x7fb21296e6c0>
_load_best_model: <function Trainer._load_best_model at 0x7fb21296dfe0>
_load_callback_state: <function Trainer._load_callback_state at 0x7fb21296e560>
_load_from_checkpoint: <function Trainer._load_from_checkpoint at 0x7fb21296de80>
_load_optimizer_and_scheduler: <function Trainer._load_optimizer_and_scheduler at 0x7fb21296e2a0>
_load_rng_state: <function Trainer._load_rng_state at 0x7fb21296e140>
_load_scaler: <function Trainer._load_scaler at 0x7fb21296e400>
_maybe_log_save_evaluate: <function Trainer._maybe_log_save_evaluate at 0x7fb212987b60>
_move_model_to_device: <function Trainer._move_model_to_device at 0x7fb212968460>
_prepare_context_parallel_inputs: <function Trainer._prepare_context_parallel_inputs at 0x7fb21296c300>
_prepare_for_training: <function Trainer._prepare_for_training at 0x7fb212987320>
_prepare_input: <function Trainer._prepare_input at 0x7fb21296c040>
_prepare_inputs: <function Trainer._prepare_inputs at 0x7fb21296c1a0>
_push_from_checkpoint: <function Trainer._push_from_checkpoint at 0x7fb21296f320>
_remove_unused_columns: <function Trainer._remove_unused_columns at 0x7fb212986770>
_report_to_hp_search: <function Trainer._report_to_hp_search at 0x7fb21296fa00>
_run_epoch: <function Trainer._run_epoch at 0x7fb2129873d0>
_save: <function Trainer._save at 0x7fb21296e980>
_save_checkpoint: <function Trainer._save_checkpoint at 0x7fb21296d7a0>
_save_optimizer_and_scheduler: <function Trainer._save_optimizer_and_scheduler at 0x7fb21296dbc0>
_save_rng_state: <function Trainer._save_rng_state at 0x7fb21296da60>
_save_scaler: <function Trainer._save_scaler at 0x7fb21296dd20>
_set_signature_columns_if_needed: <function Trainer._set_signature_columns_if_needed at 0x7fb212986610>
_track_num_input_tokens: <function Trainer._track_num_input_tokens at 0x7fb21296cca0>
_tune_save_checkpoint: <function Trainer._tune_save_checkpoint at 0x7fb21296fb60>
_update_auto_batch_size: <function Trainer._update_auto_batch_size at 0x7fb21296cbf0>
_validate_args: <function Trainer._validate_args at 0x7fb212985850>
_wrap_model: <function Trainer._wrap_model at 0x7fb21296cb40>
add_callback: <function Trainer.add_callback at 0x7fb21296fcc0>
autocast_smart_context_manager: <function Trainer.autocast_smart_context_manager at 0x7fb212987a00>
call_model_init: <function Trainer.call_model_init at 0x7fb21296f740>
compute_loss: <function Trainer.compute_loss at 0x7fb212987740>
compute_loss_context_manager: <function Trainer.compute_loss_context_manager at 0x7fb2129878a0>
create_accelerator_and_postprocess: <function Trainer.create_accelerator_and_postprocess at 0x7fb212985b10>
create_model_card: <function Trainer.create_model_card at 0x7fb21296f060>
create_optimizer: <function Trainer.create_optimizer at 0x7fb212986b90>
create_optimizer_and_scheduler: <function Trainer.create_optimizer_and_scheduler at 0x7fb212986a30>
create_scheduler: <function Trainer.create_scheduler at 0x7fb212986cf0>
evaluate: <function Trainer.evaluate at 0x7fb21296cf60>
evaluation_loop: <function Trainer.evaluation_loop at 0x7fb21296d0c0>
floating_point_ops: <function Trainer.floating_point_ops at 0x7fb21296eda0>
get_batch_samples: <function Trainer.get_batch_samples at 0x7fb212987cc0>
get_cp_size: <function Trainer.get_cp_size at 0x7fb21296c880>
get_decay_parameter_names: <function Trainer.get_decay_parameter_names at 0x7fb21281b320>
get_eval_dataloader: <function Trainer.get_eval_dataloader at 0x7fb212985dd0>
get_learning_rates: <function get_learning_rates at 0x7fb213771dd0>
get_num_trainable_parameters: <function get_num_trainable_parameters at 0x7fb213771c70>
get_optimizer_cls_and_kwargs: <function Trainer.get_optimizer_cls_and_kwargs at 0x7fb212986e50>
get_optimizer_group: <function get_optimizer_group at 0x7fb213771f30>
get_sp_size: <function Trainer.get_sp_size at 0x7fb21296c720>
get_test_dataloader: <function Trainer.get_test_dataloader at 0x7fb212985f30>
get_total_train_batch_size: <function Trainer.get_total_train_batch_size at 0x7fb21296c5c0>
get_tp_size: <function Trainer.get_tp_size at 0x7fb21296c9e0>
get_train_dataloader: <function Trainer.get_train_dataloader at 0x7fb212985c70>
hyperparameter_search: <function Trainer.hyperparameter_search at 0x7fb21296f5e0>
init_hf_repo: <function Trainer.init_hf_repo at 0x7fb21296ef00>
is_local_process_zero: <function Trainer.is_local_process_zero at 0x7fb2129681a0>
is_world_process_zero: <function Trainer.is_world_process_zero at 0x7fb212968300>
log: <function Trainer.log at 0x7fb21296eae0>
log_metrics: <function log_metrics at 0x7fb2137719b0>
metrics_format: <function metrics_format at 0x7fb213771900>
num_examples: <function Trainer.num_examples at 0x7fb212986090>
pop_callback: <function Trainer.pop_callback at 0x7fb21296fe20>
predict: <function Trainer.predict at 0x7fb21296d220>
prediction_step: <function Trainer.prediction_step at 0x7fb21296d380>
push_to_hub: <function Trainer.push_to_hub at 0x7fb21296f1c0>
remove_callback: <function Trainer.remove_callback at 0x7fb212968040>
save_metrics: <function save_metrics at 0x7fb213771a60>
save_model: <function Trainer.save_model at 0x7fb21296e820>
save_state: <function save_state at 0x7fb213771b10>
set_initial_training_values: <function Trainer.set_initial_training_values at 0x7fb21296c460>
store_flos: <function Trainer.store_flos at 0x7fb21296ec40>
train: <function Trainer.train at 0x7fb212986fb0>
training_step: <function Trainer.training_step at 0x7fb2129875e0>
Retrieve All Methods of a Class#
There are a few ways to do it.
Using __dict__#
child_class_methods_using_dict = list(ChildClass.__dict__.keys())
pprint(sorted(child_class_methods_using_dict))
assert "parent_method" not in child_class_methods_using_dict
assert "read_only_attr" in child_class_methods_using_dict
assert "class_method" in child_class_methods_using_dict
[ │ '_ChildClass__private_attr', │ '__doc__', │ '__firstlineno__', │ '__init__', │ '__module__', │ '__static_attributes__', │ '__str__', │ '_protected_attr', │ 'class_attr', │ 'class_method', │ 'instance_method', │ 'read_only_attr', │ 'static_method' ]
Notice that the parent class methods are not included!
pprint(instance_child.__class__.__dict__.keys() == ChildClass.__dict__.keys())
True
Using vars#
vars and __dict__ are equivalent, but people are preferring the former due
to some efficiency reasons, which can be found in this post.
child_class_methods_using_vars = list(vars(ChildClass).keys())
pprint(sorted(child_class_methods_using_vars))
assert "parent_method" not in child_class_methods_using_vars
assert "read_only_attr" in child_class_methods_using_vars
assert "class_method" in child_class_methods_using_vars
assert set(child_class_methods_using_dict) == set(child_class_methods_using_vars)
[ │ '_ChildClass__private_attr', │ '__doc__', │ '__firstlineno__', │ '__init__', │ '__module__', │ '__static_attributes__', │ '__str__', │ '_protected_attr', │ 'class_attr', │ 'class_method', │ 'instance_method', │ 'read_only_attr', │ 'static_method' ]
Using dir#
To include the base/parent class methods, we can use dir instead.
child_class_methods_using_dir = list(dir(ChildClass))
pprint(sorted(child_class_methods_using_dir))
assert "parent_method" in child_class_methods_using_dir
assert "read_only_attr" in child_class_methods_using_dir
assert "class_method" in child_class_methods_using_dir
[ │ '_ChildClass__private_attr', │ '__class__', │ '__delattr__', │ '__dict__', │ '__dir__', │ '__doc__', │ '__eq__', │ '__firstlineno__', │ '__format__', │ '__ge__', │ '__getattribute__', │ '__getstate__', │ '__gt__', │ '__hash__', │ '__init__', │ '__init_subclass__', │ '__le__', │ '__lt__', │ '__module__', │ '__ne__', │ '__new__', │ '__reduce__', │ '__reduce_ex__', │ '__repr__', │ '__setattr__', │ '__sizeof__', │ '__static_attributes__', │ '__str__', │ '__subclasshook__', │ '__weakref__', │ '_protected_attr', │ 'class_attr', │ 'class_method', │ 'instance_method', │ 'parent_class_attr', │ 'parent_method', │ 'read_only_attr', │ 'static_method' ]
Using inspect.getmembers#
We use inspect.getmembers to get all members of a class, and then filter out
via the predicate inspect.isroutine, a stronger filter than inspect.isfunction
or inspect.ismethod.
We attach the source code of inspect.isroutine here for reference.
def isroutine(object):
"""Return true if the object is any kind of function or method."""
return (isbuiltin(object)
or isfunction(object)
or ismethod(object)
or ismethoddescriptor(object))
predicate = inspect.isroutine
child_class_methods_using_getmembers = list(get_members_of_function_or_method(ChildClass, predicate=predicate))
pprint(sorted(child_class_methods_using_getmembers))
[ │ ('__delattr__', <slot wrapper '__delattr__' of 'object' objects>), │ ('__dir__', <method '__dir__' of 'object' objects>), │ ('__eq__', <slot wrapper '__eq__' of 'object' objects>), │ ('__format__', <method '__format__' of 'object' objects>), │ ('__ge__', <slot wrapper '__ge__' of 'object' objects>), │ ('__getattribute__', <slot wrapper '__getattribute__' of 'object' objects>), │ ('__getstate__', <method '__getstate__' of 'object' objects>), │ ('__gt__', <slot wrapper '__gt__' of 'object' objects>), │ ('__hash__', <slot wrapper '__hash__' of 'object' objects>), │ ('__init__', <function ChildClass.__init__ at 0x7fb2f6d1f270>), │ ('__init_subclass__', <built-in method __init_subclass__ of type object at 0x126c7a40>), │ ('__le__', <slot wrapper '__le__' of 'object' objects>), │ ('__lt__', <slot wrapper '__lt__' of 'object' objects>), │ ('__ne__', <slot wrapper '__ne__' of 'object' objects>), │ ('__new__', <built-in method __new__ of type object at 0x1596880>), │ ('__reduce__', <method '__reduce__' of 'object' objects>), │ ('__reduce_ex__', <method '__reduce_ex__' of 'object' objects>), │ ('__repr__', <slot wrapper '__repr__' of 'object' objects>), │ ('__setattr__', <slot wrapper '__setattr__' of 'object' objects>), │ ('__sizeof__', <method '__sizeof__' of 'object' objects>), │ ('__str__', <function ChildClass.__str__ at 0x7fb2f6d1eb90>), │ ('__subclasshook__', <built-in method __subclasshook__ of type object at 0x126c7a40>), │ ('class_method', <bound method ChildClass.class_method of <class '__main__.ChildClass'>>), │ ('instance_method', <function ChildClass.instance_method at 0x7fb2f6d1efb0>), │ ('parent_method', <function ParentClass.parent_method at 0x7fb2f6d1f3d0>), │ ('static_method', <function ChildClass.static_method at 0x7fb2f6d1eae0>) ]
Of course, the reason to retrieve all methods is a convenience if we want to inspect all methods at once. And if we can obtain all methods, we can then iteratively inspect each method’s signature.
Method Resolution Order#
The above examples do not take into account complicated cases, such as when the class is a subclass of multiple classes, in which case if you just print out the methods of the class, you will have a hard time to know which methods are from which class. You can do so via more filtering, but this is beyond the scope of this notebook.
predicate = inspect.isroutine
GPT2LMHeadModel_methods_using_getmembers = list(get_members_of_function_or_method(GPT2LMHeadModel, predicate=predicate))
pprint(sorted(GPT2LMHeadModel_methods_using_getmembers))
[ │ ('__call__', <function Module._wrapped_call_impl at 0x7fb244e377f0>), │ ('__delattr__', <function Module.__delattr__ at 0x7fb244e37e20>), │ ('__dir__', <function Module.__dir__ at 0x7fb244e3a1f0>), │ ('__eq__', <slot wrapper '__eq__' of 'object' objects>), │ ('__format__', <method '__format__' of 'object' objects>), │ ('__ge__', <slot wrapper '__ge__' of 'object' objects>), │ ('__getattr__', <function Module.__getattr__ at 0x7fb244e37b60>), │ ('__getattribute__', <slot wrapper '__getattribute__' of 'object' objects>), │ ('__getstate__', <function Module.__getstate__ at 0x7fb244e37950>), │ ('__gt__', <slot wrapper '__gt__' of 'object' objects>), │ ('__hash__', <slot wrapper '__hash__' of 'object' objects>), │ ('__init__', <function GPT2LMHeadModel.__init__ at 0x7fb213b01a60>), │ ( │ │ '__init_subclass__', │ │ <bound method PreTrainedModel.__init_subclass__ of <class 'transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel'>> │ ), │ ('__le__', <slot wrapper '__le__' of 'object' objects>), │ ('__lt__', <slot wrapper '__lt__' of 'object' objects>), │ ('__ne__', <slot wrapper '__ne__' of 'object' objects>), │ ('__new__', <built-in method __new__ of type object at 0x1596880>), │ ('__reduce__', <method '__reduce__' of 'object' objects>), │ ('__reduce_ex__', <method '__reduce_ex__' of 'object' objects>), │ ('__repr__', <function Module.__repr__ at 0x7fb244e3a140>), │ ('__setattr__', <function Module.__setattr__ at 0x7fb244e37cc0>), │ ('__setstate__', <function Module.__setstate__ at 0x7fb244e37a00>), │ ('__sizeof__', <method '__sizeof__' of 'object' objects>), │ ('__str__', <slot wrapper '__str__' of 'object' objects>), │ ('__subclasshook__', <built-in method __subclasshook__ of type object at 0x121c05f0>), │ ('_adjust_bias', <function PreTrainedModel._adjust_bias at 0x7fb213dcb950>), │ ( │ │ '_adjust_missing_and_unexpected_keys', │ │ <function PreTrainedModel._adjust_missing_and_unexpected_keys at 0x7fb213dce770> │ ), │ ('_apply', <function Module._apply at 0x7fb244e357a0>), │ ('_assisted_decoding', <function GenerationMixin._assisted_decoding at 0x7fb215d6c1a0>), │ ( │ │ '_backward_compatibility_gradient_checkpointing', │ │ <function PreTrainedModel._backward_compatibility_gradient_checkpointing at 0x7fb213dc94e0> │ ), │ ('_beam_search', <function GenerationMixin._beam_search at 0x7fb215d6c040>), │ ( │ │ '_beam_search_has_unfinished_sequences', │ │ <function GenerationMixin._beam_search_has_unfinished_sequences at 0x7fb215f67a00> │ ), │ ('_call_impl', <function Module._call_impl at 0x7fb244e378a0>), │ ( │ │ '_can_set_attn_implementation', │ │ <bound method PreTrainedModel._can_set_attn_implementation of <class 'transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel'>> │ ), │ ( │ │ '_can_set_experts_implementation', │ │ <bound method PreTrainedModel._can_set_experts_implementation of <class 'transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel'>> │ ), │ ( │ │ '_check_and_adjust_attn_implementation', │ │ <function PreTrainedModel._check_and_adjust_attn_implementation at 0x7fb213dca1f0> │ ), │ ( │ │ '_check_and_adjust_experts_implementation', │ │ <function PreTrainedModel._check_and_adjust_experts_implementation at 0x7fb213dca350> │ ), │ ('_check_early_stop_heuristic', <function GenerationMixin._check_early_stop_heuristic at 0x7fb215f678a0>), │ ( │ │ '_copy_lm_head_original_to_resized', │ │ <function PreTrainedModel._copy_lm_head_original_to_resized at 0x7fb213dcc250> │ ), │ ('_default_compile_config', <function PreTrainedModel._default_compile_config at 0x7fb213dce140>), │ ('_dispatch_accelerate_model', <function PeftAdapterMixin._dispatch_accelerate_model at 0x7fb213ea5d20>), │ ('_expand_inputs_for_generation', <function GenerationMixin._expand_inputs_for_generation at 0x7fb215f64f60>), │ ( │ │ '_extract_generation_mode_kwargs', │ │ <function GenerationMixin._extract_generation_mode_kwargs at 0x7fb215f66cf0> │ ), │ ('_finalize_model_loading', <function PreTrainedModel._finalize_model_loading at 0x7fb213dcd7a0>), │ ('_flash_attn_can_dispatch', <function PreTrainedModel._flash_attn_can_dispatch at 0x7fb213dc9c70>), │ ('_flash_attn_import_error', <function PreTrainedModel._flash_attn_import_error at 0x7fb213dc9b10>), │ ('_flatten_beam_dim', <function GenerationMixin._flatten_beam_dim at 0x7fb215f67480>), │ ('_flex_attn_can_dispatch', <function PreTrainedModel._flex_attn_can_dispatch at 0x7fb213dca090>), │ ( │ │ '_from_config', │ │ <bound method PreTrainedModel._from_config of <class 'transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel'>> │ ), │ ('_gather_beams', <function GenerationMixin._gather_beams at 0x7fb215f67740>), │ ('_get_backward_hooks', <function Module._get_backward_hooks at 0x7fb244e371c0>), │ ('_get_backward_pre_hooks', <function Module._get_backward_pre_hooks at 0x7fb244e37270>), │ ('_get_candidate_generator', <function GenerationMixin._get_candidate_generator at 0x7fb215f65220>), │ ('_get_deprecated_gen_repo', <function GenerationMixin._get_deprecated_gen_repo at 0x7fb215f66b90>), │ ('_get_dtype_plan', <function PreTrainedModel._get_dtype_plan at 0x7fb213dcd220>), │ ('_get_files_timestamps', <function PushToHubMixin._get_files_timestamps at 0x7fb2f7084670>), │ ('_get_logits_processor', <function GenerationMixin._get_logits_processor at 0x7fb215f65380>), │ ('_get_name', <function Module._get_name at 0x7fb244e39e80>), │ ('_get_resized_embeddings', <function PreTrainedModel._get_resized_embeddings at 0x7fb213dcbcc0>), │ ('_get_resized_lm_head', <function PreTrainedModel._get_resized_lm_head at 0x7fb213dcbe20>), │ ( │ │ '_get_running_beams_for_next_iteration', │ │ <function GenerationMixin._get_running_beams_for_next_iteration at 0x7fb215f67cc0> │ ), │ ('_get_static_cache_init_shape', <function GenerationMixin._get_static_cache_init_shape at 0x7fb215f65fe0>), │ ('_get_stopping_criteria', <function GenerationMixin._get_stopping_criteria at 0x7fb215f654e0>), │ ('_get_top_k_continuations', <function GenerationMixin._get_top_k_continuations at 0x7fb215f67b60>), │ ('_grouped_mm_can_dispatch', <function PreTrainedModel._grouped_mm_can_dispatch at 0x7fb213dc9f30>), │ ('_has_unfinished_sequences', <function GenerationMixin._has_unfinished_sequences at 0x7fb215f67060>), │ ( │ │ '_init_added_embeddings_weights_with_mean', │ │ <function PreTrainedModel._init_added_embeddings_weights_with_mean at 0x7fb213dcbed0> │ ), │ ( │ │ '_init_added_lm_head_bias_with_mean', │ │ <function PreTrainedModel._init_added_lm_head_bias_with_mean at 0x7fb213dcc1a0> │ ), │ ( │ │ '_init_added_lm_head_weights_with_mean', │ │ <function PreTrainedModel._init_added_lm_head_weights_with_mean at 0x7fb213dcc0f0> │ ), │ ('_init_weights', <function GPT2PreTrainedModel._init_weights at 0x7fb213b00f60>), │ ('_initialize_missing_keys', <function PreTrainedModel._initialize_missing_keys at 0x7fb213dce610>), │ ('_initialize_weights', <function PreTrainedModel._initialize_weights at 0x7fb213dcb3d0>), │ ('_load_from_state_dict', <function Module._load_from_state_dict at 0x7fb244e38930>), │ ('_load_pretrained_model', <function PreTrainedModel._load_pretrained_model at 0x7fb213dcd640>), │ ( │ │ '_maybe_initialize_input_ids_for_generation', │ │ <function GenerationMixin._maybe_initialize_input_ids_for_generation at 0x7fb215f64930> │ ), │ ( │ │ '_maybe_warn_non_full_backward_hook', │ │ <function Module._maybe_warn_non_full_backward_hook at 0x7fb244e373d0> │ ), │ ( │ │ '_merge_criteria_processor_list', │ │ <function GenerationMixin._merge_criteria_processor_list at 0x7fb215f65640> │ ), │ ( │ │ '_move_missing_keys_from_meta_to_device', │ │ <function PreTrainedModel._move_missing_keys_from_meta_to_device at 0x7fb213dce4b0> │ ), │ ('_named_members', <function Module._named_members at 0x7fb244e38bf0>), │ ('_optimize_model_for_decode', <function GenerationMixin._optimize_model_for_decode at 0x7fb215f66a30>), │ ('_prefill', <function GenerationMixin._prefill at 0x7fb215d6c300>), │ ( │ │ '_prepare_attention_mask_for_generation', │ │ <function GenerationMixin._prepare_attention_mask_for_generation at 0x7fb215f64b40> │ ), │ ('_prepare_cache_for_generation', <function GenerationMixin._prepare_cache_for_generation at 0x7fb215f66400>), │ ( │ │ '_prepare_decoder_input_ids_for_generation', │ │ <function GenerationMixin._prepare_decoder_input_ids_for_generation at 0x7fb215f64e00> │ ), │ ( │ │ '_prepare_encoder_decoder_kwargs_for_generation', │ │ <function GenerationMixin._prepare_encoder_decoder_kwargs_for_generation at 0x7fb215f64ca0> │ ), │ ('_prepare_generated_length', <function GenerationMixin._prepare_generated_length at 0x7fb215f65d20>), │ ('_prepare_generation_config', <function GenerationMixin._prepare_generation_config at 0x7fb215f65e80>), │ ('_prepare_model_inputs', <function GenerationMixin._prepare_model_inputs at 0x7fb215f647d0>), │ ( │ │ '_prepare_position_ids_for_generation', │ │ <function GenerationMixin._prepare_position_ids_for_generation at 0x7fb215f649e0> │ ), │ ('_prepare_special_tokens', <function GenerationMixin._prepare_special_tokens at 0x7fb215f666c0>), │ ('_prepare_static_cache', <function GenerationMixin._prepare_static_cache at 0x7fb215f66140>), │ ( │ │ '_register_load_state_dict_pre_hook', │ │ <function Module._register_load_state_dict_pre_hook at 0x7fb244e38670> │ ), │ ('_register_state_dict_hook', <function Module._register_state_dict_hook at 0x7fb244e37ed0>), │ ('_replicate_for_data_parallel', <function Module._replicate_for_data_parallel at 0x7fb244e3a2a0>), │ ('_resize_token_embeddings', <function PreTrainedModel._resize_token_embeddings at 0x7fb213dcbb60>), │ ('_sample', <function GenerationMixin._sample at 0x7fb215f67320>), │ ('_save_to_state_dict', <function Module._save_to_state_dict at 0x7fb244e38250>), │ ('_sdpa_can_dispatch', <function PreTrainedModel._sdpa_can_dispatch at 0x7fb213dc9dd0>), │ ('_set_gradient_checkpointing', <function PreTrainedModel._set_gradient_checkpointing at 0x7fb213dcc7d0>), │ ('_slow_forward', <function Module._slow_forward at 0x7fb244e37740>), │ ( │ │ '_supports_default_dynamic_cache', │ │ <bound method GenerationMixin._supports_default_dynamic_cache of <class 'transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel'>> │ ), │ ('_supports_logits_to_keep', <function GenerationMixin._supports_logits_to_keep at 0x7fb215f66560>), │ ('_unflatten_beam_dim', <function GenerationMixin._unflatten_beam_dim at 0x7fb215f675e0>), │ ('_update_finished_beams', <function GenerationMixin._update_finished_beams at 0x7fb215f67e20>), │ ( │ │ '_update_model_kwargs_for_generation', │ │ <function GenerationMixin._update_model_kwargs_for_generation at 0x7fb215f650c0> │ ), │ ('_upload_modified_files', <function PushToHubMixin._upload_modified_files at 0x7fb2f70847d0>), │ ('_valid_auto_compile_criteria', <function GenerationMixin._valid_auto_compile_criteria at 0x7fb215f66820>), │ ('_validate_generated_length', <function GenerationMixin._validate_generated_length at 0x7fb215f65bc0>), │ ('_validate_generation_mode', <function GenerationMixin._validate_generation_mode at 0x7fb215f65900>), │ ('_validate_model_kwargs', <function GenerationMixin._validate_model_kwargs at 0x7fb215f65a60>), │ ('_wrapped_call_impl', <function Module._wrapped_call_impl at 0x7fb244e377f0>), │ ('active_adapters', <function PeftAdapterMixin.active_adapters at 0x7fb213ea5a60>), │ ('add_adapter', <function PeftAdapterMixin.add_adapter at 0x7fb213ea54e0>), │ ('add_model_tags', <function PreTrainedModel.add_model_tags at 0x7fb213dc9640>), │ ('add_module', <function Module.add_module at 0x7fb244e34d50>), │ ('adjust_generation_fn', <function GenerationMixin.adjust_generation_fn at 0x7fb215f643b0>), │ ('apply', <function Module.apply at 0x7fb244e35900>), │ ('bfloat16', <function Module.bfloat16 at 0x7fb244e366c0>), │ ('buffers', <function Module.buffers at 0x7fb244e39010>), │ ( │ │ 'can_generate', │ │ <bound method PreTrainedModel.can_generate of <class 'transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel'>> │ ), │ ('children', <function Module.children at 0x7fb244e392d0>), │ ('compile', <function Module.compile at 0x7fb244e3a400>), │ ('compute_transition_scores', <function GenerationMixin.compute_transition_scores at 0x7fb215f657a0>), │ ( │ │ 'continuous_batching_context_manager', │ │ <function ContinuousMixin.continuous_batching_context_manager at 0x7fb215f41590> │ ), │ ('cpu', <function Module.cpu at 0x7fb244e35fe0>), │ ( │ │ 'create_extended_attention_mask_for_decoder', │ │ <function ModuleUtilsMixin.create_extended_attention_mask_for_decoder at 0x7fb213dc3e20> │ ), │ ('cuda', <function Module.cuda at 0x7fb213dccd50>), │ ('delete_adapter', <function PeftAdapterMixin.delete_adapter at 0x7fb213ea5e80>), │ ('dequantize', <function PreTrainedModel.dequantize at 0x7fb213dc9430>), │ ( │ │ 'destroy_cached_continuous_batching_manager', │ │ <function ContinuousMixin.destroy_cached_continuous_batching_manager at 0x7fb215f412d0> │ ), │ ('disable_adapters', <function PeftAdapterMixin.disable_adapters at 0x7fb213ea57a0>), │ ('disable_input_require_grads', <function PreTrainedModel.disable_input_require_grads at 0x7fb213dcacf0>), │ ('double', <function Module.double at 0x7fb244e36400>), │ ('enable_adapters', <function PeftAdapterMixin.enable_adapters at 0x7fb213ea5900>), │ ('enable_input_require_grads', <function PreTrainedModel.enable_input_require_grads at 0x7fb213dcac40>), │ ('enable_peft_hotswap', <function PeftAdapterMixin.enable_peft_hotswap at 0x7fb213ea5380>), │ ('eval', <function PreTrainedModel.eval at 0x7fb213dcecf0>), │ ('extra_repr', <function Module.extra_repr at 0x7fb244e39fe0>), │ ('float', <function PreTrainedModel.float at 0x7fb213dccf60>), │ ('forward', <function GPT2LMHeadModel.forward at 0x7fb213b01dd0>), │ ( │ │ 'from_pretrained', │ │ <bound method PreTrainedModel.from_pretrained of <class 'transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel'>> │ ), │ ('generate', <function GenerationMixin.generate at 0x7fb215f66f00>), │ ('generate_batch', <function ContinuousMixin.generate_batch at 0x7fb215f417a0>), │ ('get_adapter_state_dict', <function PeftAdapterMixin.get_adapter_state_dict at 0x7fb213ea5bc0>), │ ('get_buffer', <function Module.get_buffer at 0x7fb244e35430>), │ ('get_compiled_call', <function PreTrainedModel.get_compiled_call at 0x7fb213dce2a0>), │ ( │ │ 'get_correct_attn_implementation', │ │ <function PreTrainedModel.get_correct_attn_implementation at 0x7fb213dca4b0> │ ), │ ( │ │ 'get_correct_experts_implementation', │ │ <function PreTrainedModel.get_correct_experts_implementation at 0x7fb213dca610> │ ), │ ('get_decoder', <function PreTrainedModel.get_decoder at 0x7fb213dcb060>), │ ('get_encoder', <function PreTrainedModel.get_encoder at 0x7fb213dcae50>), │ ( │ │ 'get_expanded_tied_weights_keys', │ │ <function PreTrainedModel.get_expanded_tied_weights_keys at 0x7fb213dcb740> │ ), │ ('get_extended_attention_mask', <function ModuleUtilsMixin.get_extended_attention_mask at 0x7fb213dc8040>), │ ('get_extra_state', <function Module.get_extra_state at 0x7fb244e35590>), │ ( │ │ 'get_init_context', │ │ <bound method PreTrainedModel.get_init_context of <class 'transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel'>> │ ), │ ('get_input_embeddings', <function EmbeddingAccessMixin.get_input_embeddings at 0x7fb213dc8300>), │ ('get_memory_footprint', <function PreTrainedModel.get_memory_footprint at 0x7fb213dccca0>), │ ('get_output_embeddings', <function EmbeddingAccessMixin.get_output_embeddings at 0x7fb213dc8510>), │ ('get_parameter', <function Module.get_parameter at 0x7fb244e352d0>), │ ('get_parameter_or_buffer', <function PreTrainedModel.get_parameter_or_buffer at 0x7fb213dce980>), │ ('get_position_embeddings', <function PreTrainedModel.get_position_embeddings at 0x7fb213dcc510>), │ ('get_submodule', <function Module.get_submodule at 0x7fb244e35010>), │ ( │ │ 'gradient_checkpointing_disable', │ │ <function PreTrainedModel.gradient_checkpointing_disable at 0x7fb213dcc880> │ ), │ ('gradient_checkpointing_enable', <function PreTrainedModel.gradient_checkpointing_enable at 0x7fb213dcc670>), │ ('half', <function PreTrainedModel.half at 0x7fb213dcceb0>), │ ('heal_tokens', <function GenerationMixin.heal_tokens at 0x7fb215f671c0>), │ ('init_continuous_batching', <function ContinuousMixin.init_continuous_batching at 0x7fb215f41170>), │ ('init_weights', <function PreTrainedModel.init_weights at 0x7fb213dcc5c0>), │ ('initialize_weights', <function PreTrainedModel.initialize_weights at 0x7fb213dcb5e0>), │ ('invert_attention_mask', <function ModuleUtilsMixin.invert_attention_mask at 0x7fb213dc3d70>), │ ('ipu', <function Module.ipu at 0x7fb244e35bc0>), │ ( │ │ 'is_backend_compatible', │ │ <bound method PreTrainedModel.is_backend_compatible of <class 'transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel'>> │ ), │ ( │ │ 'is_custom_code', │ │ <bound method PreTrainedModel.is_custom_code of <class 'transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel'>> │ ), │ ( │ │ 'is_remote_code', │ │ <bound method PreTrainedModel.is_remote_code of <class 'transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel'>> │ ), │ ('load_adapter', <function PeftAdapterMixin.load_adapter at 0x7fb213ea5220>), │ ('load_custom_generate', <function GenerationMixin.load_custom_generate at 0x7fb215f64510>), │ ('load_state_dict', <function Module.load_state_dict at 0x7fb244e38a90>), │ ( │ │ 'mark_tied_weights_as_initialized', │ │ <function PreTrainedModel.mark_tied_weights_as_initialized at 0x7fb213dce820> │ ), │ ('modules', <function Module.modules at 0x7fb244e39590>), │ ('mtia', <function Module.mtia at 0x7fb244e35e80>), │ ('named_buffers', <function Module.named_buffers at 0x7fb244e39170>), │ ('named_children', <function Module.named_children at 0x7fb244e39430>), │ ('named_modules', <function Module.named_modules at 0x7fb244e396f0>), │ ('named_non_persistent_buffers', <function PreTrainedModel.named_non_persistent_buffers at 0x7fb213dceae0>), │ ('named_parameters', <function Module.named_parameters at 0x7fb244e38eb0>), │ ('num_parameters', <function ModuleUtilsMixin.num_parameters at 0x7fb213dc81a0>), │ ('parameters', <function Module.parameters at 0x7fb244e38d50>), │ ('post_init', <function PreTrainedModel.post_init at 0x7fb213dc8ca0>), │ ('prepare_inputs_for_generation', <function GenerationMixin.prepare_inputs_for_generation at 0x7fb215f64670>), │ ('push_to_hub', <function PushToHubMixin.push_to_hub at 0x7fb213dc3950>), │ ('register_backward_hook', <function Module.register_backward_hook at 0x7fb244e36fb0>), │ ('register_buffer', <function Module.register_buffer at 0x7fb244e34a90>), │ ( │ │ 'register_for_auto_class', │ │ <bound method PreTrainedModel.register_for_auto_class of <class 'transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel'>> │ ), │ ('register_forward_hook', <function Module.register_forward_hook at 0x7fb244e37690>), │ ('register_forward_pre_hook', <function Module.register_forward_pre_hook at 0x7fb244e37530>), │ ('register_full_backward_hook', <function Module.register_full_backward_hook at 0x7fb244e37110>), │ ('register_full_backward_pre_hook', <function Module.register_full_backward_pre_hook at 0x7fb244e36e50>), │ ( │ │ 'register_load_state_dict_post_hook', │ │ <function Module.register_load_state_dict_post_hook at 0x7fb244e387d0> │ ), │ ('register_load_state_dict_pre_hook', <function Module.register_load_state_dict_pre_hook at 0x7fb244e38720>), │ ('register_module', <function Module.register_module at 0x7fb244e34eb0>), │ ('register_parameter', <function Module.register_parameter at 0x7fb244e34bf0>), │ ('register_state_dict_post_hook', <function Module.register_state_dict_post_hook at 0x7fb244e38040>), │ ('register_state_dict_pre_hook', <function Module.register_state_dict_pre_hook at 0x7fb244e380f0>), │ ('requires_grad_', <function Module.requires_grad_ at 0x7fb244e39b10>), │ ('resize_position_embeddings', <function PreTrainedModel.resize_position_embeddings at 0x7fb213dcc3b0>), │ ('resize_token_embeddings', <function PreTrainedModel.resize_token_embeddings at 0x7fb213dcbab0>), │ ('retrieve_modules_from_names', <function PreTrainedModel.retrieve_modules_from_names at 0x7fb213dcd850>), │ ('save_pretrained', <function PreTrainedModel.save_pretrained at 0x7fb213dccb40>), │ ('set_adapter', <function PeftAdapterMixin.set_adapter at 0x7fb213ea5640>), │ ('set_attn_implementation', <function PreTrainedModel.set_attn_implementation at 0x7fb213dcaa30>), │ ('set_decoder', <function PreTrainedModel.set_decoder at 0x7fb213dcb110>), │ ('set_encoder', <function PreTrainedModel.set_encoder at 0x7fb213dcafb0>), │ ('set_experts_implementation', <function PreTrainedModel.set_experts_implementation at 0x7fb213dcab90>), │ ('set_extra_state', <function Module.set_extra_state at 0x7fb244e356f0>), │ ('set_input_embeddings', <function EmbeddingAccessMixin.set_input_embeddings at 0x7fb213dc8460>), │ ('set_output_embeddings', <function EmbeddingAccessMixin.set_output_embeddings at 0x7fb213dc85c0>), │ ('set_submodule', <function Module.set_submodule at 0x7fb244e35170>), │ ('set_use_kernels', <function PreTrainedModel.set_use_kernels at 0x7fb213dcd380>), │ ('share_memory', <function Module.share_memory at 0x7fb244e39dd0>), │ ('state_dict', <function Module.state_dict at 0x7fb244e385c0>), │ ('tie_weights', <function PreTrainedModel.tie_weights at 0x7fb213dcb8a0>), │ ('to', <function Module.to at 0x7fb213dcce00>), │ ('to_empty', <function Module.to_empty at 0x7fb244e36820>), │ ('train', <function PreTrainedModel.train at 0x7fb213dcec40>), │ ('type', <function Module.type at 0x7fb244e36140>), │ ( │ │ 'warn_if_padding_and_no_attention_mask', │ │ <function PreTrainedModel.warn_if_padding_and_no_attention_mask at 0x7fb213dcd9b0> │ ), │ ('xpu', <function Module.xpu at 0x7fb244e35d20>), │ ('zero_grad', <function Module.zero_grad at 0x7fb244e39c70>) ]
You can get the method resolution order (MRO) of a class via cls.__mro__.
inspect.getmro(GPT2LMHeadModel)
(transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel,
transformers.models.gpt2.modeling_gpt2.GPT2PreTrainedModel,
transformers.modeling_utils.PreTrainedModel,
torch.nn.modules.module.Module,
transformers.modeling_utils.EmbeddingAccessMixin,
transformers.modeling_utils.ModuleUtilsMixin,
transformers.utils.hub.PushToHubMixin,
transformers.integrations.peft.PeftAdapterMixin,
transformers.generation.utils.GenerationMixin,
transformers.generation.continuous_batching.continuous_api.ContinuousMixin,
object)
A pseudocode to get all signatures of a class via MRO is as follows:
def get_all_args(cls: Type[object]) -> Dict[str, inspect.Parameter]:
mro = inspect.getmro(cls)
all_args = {}
for base_class in mro[::-1]: # reverse to start from topmost class
if base_class is object: # skip the base 'object' class
continue
sig = inspect.signature(base_class.__init__)
all_args.update(sig.parameters)
return all_args
Get Class and Instance Attributes#
pprint(list(class_child.__dict__.keys())) # class attributes
pprint(list(instance_child.__dict__.keys())) # instance attributes
[ │ '__module__', │ '__firstlineno__', │ '__doc__', │ 'class_attr', │ '_protected_attr', │ '_ChildClass__private_attr', │ '__init__', │ 'read_only_attr', │ 'instance_method', │ 'class_method', │ 'static_method', │ '__str__', │ '__static_attributes__' ]
['parent_instance_attr', 'instance_attr', 'instance_not_in_constructor_attr', '_private_instance_attr']
union_class_and_instance_attributes = list(set(class_child.__dict__.keys()).union(set(instance_child.__dict__.keys())))
pprint(union_class_and_instance_attributes)
[ │ 'instance_attr', │ 'read_only_attr', │ '_private_instance_attr', │ '_protected_attr', │ '__init__', │ '__str__', │ '__doc__', │ 'parent_instance_attr', │ '__firstlineno__', │ 'class_attr', │ 'class_method', │ '__module__', │ '_ChildClass__private_attr', │ '__static_attributes__', │ 'instance_method', │ 'static_method', │ 'instance_not_in_constructor_attr' ]
Get Signature and Type Annotations of a Function#
func_sig: Signature = inspect.signature(func)
pprint(func_sig.parameters)
pprint(func_sig.return_annotation)
mappingproxy({ │ 'a': <Parameter "a: int">, │ 'b': <Parameter "b: str">, │ 'c': <Parameter "c: List[int]">, │ 'd': <Parameter "d: Tuple[str, str]">, │ 'e': <Parameter "e: int | str">, │ 'kwargs': <Parameter "**kwargs: Any"> })
<class 'str'>
Here are the 4 key properties of the Parameter object
of the Signature object.
@property
def name(self):
return self._name
@property
def default(self):
return self._default
@property
def annotation(self):
return self._annotation
@property
def kind(self):
return self._kind
We will also use get_type_hints to get the type hints of a function
instead of using the annotations property of inspect.Signature. The reason
can be found in the docstring of get_type_hints:
def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
"""Return type hints for an object.
This is often the same as obj.__annotations__, but it handles
forward references encoded as string literals, adds Optional[t] if a
default value equal to None is set and recursively replaces all
'Annotated[T, ...]' with 'T' (unless 'include_extras=True').
...
"""
def no_type_hints(a, b, c, d, e, **kwargs):
return a, b, c, d, e, kwargs
get_type_hints(no_type_hints), inspect.signature(no_type_hints)
({}, <Signature (a, b, c, d, e, **kwargs)>)
How to know if a parameter is optional or not? We can use the inspect.Parameter.empty
property.
for name, value in inspect.signature(func).parameters.items():
print(value.default)
print(value.default is inspect.Parameter.empty)
<class 'inspect._empty'>
True
<class 'inspect._empty'>
True
<class 'inspect._empty'>
True
<class 'inspect._empty'>
True
<class 'inspect._empty'>
True
<class 'inspect._empty'>
True
We will also use __mro__ to get the method resolution order of a class
because __bases__ only returns the immediate parent class.
ChildClass.__bases__, GPT2LMHeadModel.__bases__[0].__bases__[0].__bases__
((__main__.ParentClass,),
(torch.nn.modules.module.Module,
transformers.modeling_utils.EmbeddingAccessMixin,
transformers.modeling_utils.ModuleUtilsMixin,
transformers.utils.hub.PushToHubMixin,
transformers.integrations.peft.PeftAdapterMixin))
list(reversed(inspect.getmro(GPT2LMHeadModel)))
[object,
transformers.generation.continuous_batching.continuous_api.ContinuousMixin,
transformers.generation.utils.GenerationMixin,
transformers.integrations.peft.PeftAdapterMixin,
transformers.utils.hub.PushToHubMixin,
transformers.modeling_utils.ModuleUtilsMixin,
transformers.modeling_utils.EmbeddingAccessMixin,
torch.nn.modules.module.Module,
transformers.modeling_utils.PreTrainedModel,
transformers.models.gpt2.modeling_gpt2.GPT2PreTrainedModel,
transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel]
def get_base_classes(cls: Type[Any], include_self: bool = False) -> Set[Type[Any]]:
"""
Get the base classes of a class and all its base classes.
"""
return set(cls.__mro__[0:-1] if include_self else cls.__mro__[1:-1])
pprint(get_base_classes(GPT2LMHeadModel, include_self=True))
{ │ <class 'transformers.generation.utils.GenerationMixin'>, │ <class 'transformers.generation.continuous_batching.continuous_api.ContinuousMixin'>, │ <class 'transformers.models.gpt2.modeling_gpt2.GPT2PreTrainedModel'>, │ <class 'torch.nn.modules.module.Module'>, │ <class 'transformers.modeling_utils.EmbeddingAccessMixin'>, │ <class 'transformers.modeling_utils.ModuleUtilsMixin'>, │ <class 'transformers.integrations.peft.PeftAdapterMixin'>, │ <class 'transformers.utils.hub.PushToHubMixin'>, │ <class 'transformers.modeling_utils.PreTrainedModel'>, │ <class 'transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel'> }
def get_default(param: Parameter) -> Any:
"""Return the parameter's default value or None if not specified."""
return param.default if param.default is not param.empty else None
def get_field_annotations(func_or_method: Callable[..., Any]) -> Tuple[List[Tuple[str, Any, Any]], Dict[str, Any]]:
if not inspect.isroutine(func_or_method):
raise ValueError("Expected a function or method")
required_fields = []
optional_fields = []
annotations = {}
try:
sig: Signature = inspect.signature(func_or_method)
type_hints: Dict[str, Any] = get_type_hints(func_or_method)
except ValueError:
raise ValueError("Object does not support signature or type hints extraction.") from None
for name, param in sig.parameters.items():
if name == "self":
continue
type_hint = type_hints.get(name, Any)
annotations[name] = type_hint
if param.default is param.empty:
required_fields.append((name, type_hint, Ellipsis))
else:
default_value = param.default
optional_fields.append((name, type_hint, default_value))
fields = required_fields + optional_fields
return fields, annotations
# TODO: Tuple[str, Any, Any] should be Tuple[str, Any, ellipsis]
def get_constructor_field_annotations(
cls: Type[Any], include_bases: bool = True
) -> Tuple[List[Tuple[str, Any, Any]], Dict[str, Any]]:
fields = []
annotations = {}
classes_to_inspect = [cls] + list(get_base_classes(cls, include_self=False)) if include_bases else [cls]
for c in reversed(classes_to_inspect): # Reverse to respect MRO
if hasattr(c, "__init__"):
class_fields, class_annotations = get_field_annotations(c.__init__)
# Update fields and annotations with those from the current class,
# avoiding duplicates.
for field in class_fields:
if field[0] not in annotations:
fields.append(field) # noqa: PERF401
annotations.update(class_annotations)
return fields, annotations
fields, annotations = get_constructor_field_annotations(TrainingArguments, include_bases=False)
for field in fields:
assert len(field) == 3
print(f"{field[0]}, {field[1]}, {field[2]}")
assert get_field_annotations(TrainingArguments.__init__) == (fields, annotations)
output_dir, str | None, None
per_device_train_batch_size, <class 'int'>, 8
num_train_epochs, <class 'float'>, 3.0
max_steps, <class 'int'>, -1
learning_rate, <class 'float'>, 5e-05
lr_scheduler_type, transformers.trainer_utils.SchedulerType | str, linear
lr_scheduler_kwargs, dict | str | None, None
warmup_steps, <class 'float'>, 0
optim, transformers.training_args.OptimizerNames | str, adamw_torch_fused
optim_args, str | None, None
weight_decay, <class 'float'>, 0.0
adam_beta1, <class 'float'>, 0.9
adam_beta2, <class 'float'>, 0.999
adam_epsilon, <class 'float'>, 1e-08
optim_target_modules, None | str | list[str], None
gradient_accumulation_steps, <class 'int'>, 1
average_tokens_across_devices, <class 'bool'>, True
max_grad_norm, <class 'float'>, 1.0
label_smoothing_factor, <class 'float'>, 0.0
bf16, <class 'bool'>, False
fp16, <class 'bool'>, False
bf16_full_eval, <class 'bool'>, False
fp16_full_eval, <class 'bool'>, False
tf32, bool | None, None
gradient_checkpointing, <class 'bool'>, False
gradient_checkpointing_kwargs, dict[str, typing.Any] | str | None, None
torch_compile, <class 'bool'>, False
torch_compile_backend, str | None, None
torch_compile_mode, str | None, None
use_liger_kernel, <class 'bool'>, False
liger_kernel_config, dict[str, bool] | None, None
use_cache, <class 'bool'>, False
neftune_noise_alpha, float | None, None
torch_empty_cache_steps, int | None, None
auto_find_batch_size, <class 'bool'>, False
logging_strategy, transformers.trainer_utils.IntervalStrategy | str, steps
logging_steps, <class 'float'>, 500
logging_first_step, <class 'bool'>, False
log_on_each_node, <class 'bool'>, True
logging_nan_inf_filter, <class 'bool'>, True
include_num_input_tokens_seen, str | bool, no
log_level, <class 'str'>, passive
log_level_replica, <class 'str'>, warning
disable_tqdm, bool | None, None
report_to, None | str | list[str], none
run_name, str | None, None
project, <class 'str'>, huggingface
trackio_space_id, str | None, None
trackio_bucket_id, str | None, None
trackio_static_space_id, str | None | typing.Literal[False], None
eval_strategy, transformers.trainer_utils.IntervalStrategy | str, no
eval_steps, float | None, None
eval_delay, <class 'float'>, 0
per_device_eval_batch_size, <class 'int'>, 8
prediction_loss_only, <class 'bool'>, False
eval_on_start, <class 'bool'>, False
eval_do_concat_batches, <class 'bool'>, True
eval_use_gather_object, <class 'bool'>, False
eval_accumulation_steps, int | None, None
include_for_metrics, list[str], <factory>
batch_eval_metrics, <class 'bool'>, False
save_only_model, <class 'bool'>, False
save_strategy, transformers.trainer_utils.SaveStrategy | str, steps
save_steps, <class 'float'>, 500
save_on_each_node, <class 'bool'>, False
save_total_limit, int | None, None
enable_jit_checkpoint, <class 'bool'>, False
push_to_hub, <class 'bool'>, False
hub_token, str | None, None
hub_private_repo, bool | None, None
hub_model_id, str | None, None
hub_strategy, transformers.trainer_utils.HubStrategy | str, every_save
hub_always_push, <class 'bool'>, False
hub_revision, str | None, None
load_best_model_at_end, <class 'bool'>, False
metric_for_best_model, str | None, None
greater_is_better, bool | None, None
ignore_data_skip, <class 'bool'>, False
restore_callback_states_from_checkpoint, <class 'bool'>, False
full_determinism, <class 'bool'>, False
seed, <class 'int'>, 42
data_seed, int | None, None
use_cpu, <class 'bool'>, False
accelerator_config, dict | str | None, None
parallelism_config, typing.Any | None, None
dataloader_drop_last, <class 'bool'>, False
dataloader_num_workers, <class 'int'>, 0
dataloader_pin_memory, <class 'bool'>, True
dataloader_persistent_workers, <class 'bool'>, False
dataloader_prefetch_factor, int | None, None
remove_unused_columns, <class 'bool'>, True
label_names, list[str] | None, None
train_sampling_strategy, <class 'str'>, random
length_column_name, <class 'str'>, length
ddp_find_unused_parameters, bool | None, None
ddp_bucket_cap_mb, int | None, None
ddp_broadcast_buffers, bool | None, None
ddp_static_graph, bool | None, None
ddp_backend, str | None, None
ddp_timeout, <class 'int'>, 1800
fsdp, str | None, None
fsdp_config, dict[str, typing.Any] | str | None, None
deepspeed, dict | str | None, None
debug, str | list[transformers.debug_utils.DebugOption],
skip_memory_metrics, <class 'bool'>, True
do_train, <class 'bool'>, False
do_eval, <class 'bool'>, False
do_predict, <class 'bool'>, False
resume_from_checkpoint, str | None, None
warmup_ratio, float | None, None
logging_dir, str | None, None
local_rank, <class 'int'>, -1
Warning: it does not play too well with dataclass and pydantic classes
because they have more complex bells and whistles. However, because of the perks
of dataclass and pydantic, we can just use
property
like model_fields to get all fields and their types.
As we can see from above, we did not handle lr_scheduler_kwargs well:
lr_scheduler_kwargs, typing.Optional[typing.Dict], <factory>
where <factory> is the default value of the parameter. But it is actually
referring to the default_factory of the dataclass field, which can be a default
dict etc.
def type_hint_to_str(type_hint: Any) -> str:
"""
Convert a type hint into its string representation.
"""
if hasattr(type_hint, "__name__"):
return type_hint.__name__
elif hasattr(type_hint, "_name") and type_hint._name is not None:
return str(type_hint._name)
elif type(type_hint) == _GenericAlias: # For Python 3.8+
# Handles complex types, e.g., List[int], Union[str, int]
origin = type_hint_to_str(type_hint.__origin__)
args = ", ".join(type_hint_to_str(arg) for arg in type_hint.__args__)
return f"{origin}[{args}]"
else:
# Fallback for unhandled types
return str(type_hint)
def create_config_class_str(fields: List[Tuple[str, Any, Any]]) -> str:
lines = ["class Config:"]
if not fields:
lines.append(" ...")
else:
init_params = ["self"]
init_body = []
for name, type_hint, default in fields:
type_hint_str = type_hint_to_str(type_hint)
if default is Ellipsis: # Required argument
param_str = f"{name}: {type_hint_str}"
elif default is field:
param_str = f"{name}: {type_hint_str} = field(default_factory=dict)"
else:
default_repr = repr(default) if default is not None else "None"
param_str = f"{name}: {type_hint_str} = {default_repr}"
init_params.append(param_str)
init_body.append(f" self.{name} = {name}")
lines.append(f" def __init__({', '.join(init_params)}):")
lines.extend(init_body)
return "\n".join(lines)
config_class_str = create_config_class_str(fields)
print(config_class_str)
class Config:
def __init__(self, output_dir: Union = None, per_device_train_batch_size: int = 8, num_train_epochs: float = 3.0, max_steps: int = -1, learning_rate: float = 5e-05, lr_scheduler_type: Union = 'linear', lr_scheduler_kwargs: Union = None, warmup_steps: float = 0, optim: Union = 'adamw_torch_fused', optim_args: Union = None, weight_decay: float = 0.0, adam_beta1: float = 0.9, adam_beta2: float = 0.999, adam_epsilon: float = 1e-08, optim_target_modules: Union = None, gradient_accumulation_steps: int = 1, average_tokens_across_devices: bool = True, max_grad_norm: float = 1.0, label_smoothing_factor: float = 0.0, bf16: bool = False, fp16: bool = False, bf16_full_eval: bool = False, fp16_full_eval: bool = False, tf32: Union = None, gradient_checkpointing: bool = False, gradient_checkpointing_kwargs: Union = None, torch_compile: bool = False, torch_compile_backend: Union = None, torch_compile_mode: Union = None, use_liger_kernel: bool = False, liger_kernel_config: Union = None, use_cache: bool = False, neftune_noise_alpha: Union = None, torch_empty_cache_steps: Union = None, auto_find_batch_size: bool = False, logging_strategy: Union = 'steps', logging_steps: float = 500, logging_first_step: bool = False, log_on_each_node: bool = True, logging_nan_inf_filter: bool = True, include_num_input_tokens_seen: Union = 'no', log_level: str = 'passive', log_level_replica: str = 'warning', disable_tqdm: Union = None, report_to: Union = 'none', run_name: Union = None, project: str = 'huggingface', trackio_space_id: Union = None, trackio_bucket_id: Union = None, trackio_static_space_id: Union = None, eval_strategy: Union = 'no', eval_steps: Union = None, eval_delay: float = 0, per_device_eval_batch_size: int = 8, prediction_loss_only: bool = False, eval_on_start: bool = False, eval_do_concat_batches: bool = True, eval_use_gather_object: bool = False, eval_accumulation_steps: Union = None, include_for_metrics: list = <factory>, batch_eval_metrics: bool = False, save_only_model: bool = False, save_strategy: Union = 'steps', save_steps: float = 500, save_on_each_node: bool = False, save_total_limit: Union = None, enable_jit_checkpoint: bool = False, push_to_hub: bool = False, hub_token: Union = None, hub_private_repo: Union = None, hub_model_id: Union = None, hub_strategy: Union = 'every_save', hub_always_push: bool = False, hub_revision: Union = None, load_best_model_at_end: bool = False, metric_for_best_model: Union = None, greater_is_better: Union = None, ignore_data_skip: bool = False, restore_callback_states_from_checkpoint: bool = False, full_determinism: bool = False, seed: int = 42, data_seed: Union = None, use_cpu: bool = False, accelerator_config: Union = None, parallelism_config: Union = None, dataloader_drop_last: bool = False, dataloader_num_workers: int = 0, dataloader_pin_memory: bool = True, dataloader_persistent_workers: bool = False, dataloader_prefetch_factor: Union = None, remove_unused_columns: bool = True, label_names: Union = None, train_sampling_strategy: str = 'random', length_column_name: str = 'length', ddp_find_unused_parameters: Union = None, ddp_bucket_cap_mb: Union = None, ddp_broadcast_buffers: Union = None, ddp_static_graph: Union = None, ddp_backend: Union = None, ddp_timeout: int = 1800, fsdp: Union = None, fsdp_config: Union = None, deepspeed: Union = None, debug: Union = '', skip_memory_metrics: bool = True, do_train: bool = False, do_eval: bool = False, do_predict: bool = False, resume_from_checkpoint: Union = None, warmup_ratio: Union = None, logging_dir: Union = None, local_rank: int = -1):
self.output_dir = output_dir
self.per_device_train_batch_size = per_device_train_batch_size
self.num_train_epochs = num_train_epochs
self.max_steps = max_steps
self.learning_rate = learning_rate
self.lr_scheduler_type = lr_scheduler_type
self.lr_scheduler_kwargs = lr_scheduler_kwargs
self.warmup_steps = warmup_steps
self.optim = optim
self.optim_args = optim_args
self.weight_decay = weight_decay
self.adam_beta1 = adam_beta1
self.adam_beta2 = adam_beta2
self.adam_epsilon = adam_epsilon
self.optim_target_modules = optim_target_modules
self.gradient_accumulation_steps = gradient_accumulation_steps
self.average_tokens_across_devices = average_tokens_across_devices
self.max_grad_norm = max_grad_norm
self.label_smoothing_factor = label_smoothing_factor
self.bf16 = bf16
self.fp16 = fp16
self.bf16_full_eval = bf16_full_eval
self.fp16_full_eval = fp16_full_eval
self.tf32 = tf32
self.gradient_checkpointing = gradient_checkpointing
self.gradient_checkpointing_kwargs = gradient_checkpointing_kwargs
self.torch_compile = torch_compile
self.torch_compile_backend = torch_compile_backend
self.torch_compile_mode = torch_compile_mode
self.use_liger_kernel = use_liger_kernel
self.liger_kernel_config = liger_kernel_config
self.use_cache = use_cache
self.neftune_noise_alpha = neftune_noise_alpha
self.torch_empty_cache_steps = torch_empty_cache_steps
self.auto_find_batch_size = auto_find_batch_size
self.logging_strategy = logging_strategy
self.logging_steps = logging_steps
self.logging_first_step = logging_first_step
self.log_on_each_node = log_on_each_node
self.logging_nan_inf_filter = logging_nan_inf_filter
self.include_num_input_tokens_seen = include_num_input_tokens_seen
self.log_level = log_level
self.log_level_replica = log_level_replica
self.disable_tqdm = disable_tqdm
self.report_to = report_to
self.run_name = run_name
self.project = project
self.trackio_space_id = trackio_space_id
self.trackio_bucket_id = trackio_bucket_id
self.trackio_static_space_id = trackio_static_space_id
self.eval_strategy = eval_strategy
self.eval_steps = eval_steps
self.eval_delay = eval_delay
self.per_device_eval_batch_size = per_device_eval_batch_size
self.prediction_loss_only = prediction_loss_only
self.eval_on_start = eval_on_start
self.eval_do_concat_batches = eval_do_concat_batches
self.eval_use_gather_object = eval_use_gather_object
self.eval_accumulation_steps = eval_accumulation_steps
self.include_for_metrics = include_for_metrics
self.batch_eval_metrics = batch_eval_metrics
self.save_only_model = save_only_model
self.save_strategy = save_strategy
self.save_steps = save_steps
self.save_on_each_node = save_on_each_node
self.save_total_limit = save_total_limit
self.enable_jit_checkpoint = enable_jit_checkpoint
self.push_to_hub = push_to_hub
self.hub_token = hub_token
self.hub_private_repo = hub_private_repo
self.hub_model_id = hub_model_id
self.hub_strategy = hub_strategy
self.hub_always_push = hub_always_push
self.hub_revision = hub_revision
self.load_best_model_at_end = load_best_model_at_end
self.metric_for_best_model = metric_for_best_model
self.greater_is_better = greater_is_better
self.ignore_data_skip = ignore_data_skip
self.restore_callback_states_from_checkpoint = restore_callback_states_from_checkpoint
self.full_determinism = full_determinism
self.seed = seed
self.data_seed = data_seed
self.use_cpu = use_cpu
self.accelerator_config = accelerator_config
self.parallelism_config = parallelism_config
self.dataloader_drop_last = dataloader_drop_last
self.dataloader_num_workers = dataloader_num_workers
self.dataloader_pin_memory = dataloader_pin_memory
self.dataloader_persistent_workers = dataloader_persistent_workers
self.dataloader_prefetch_factor = dataloader_prefetch_factor
self.remove_unused_columns = remove_unused_columns
self.label_names = label_names
self.train_sampling_strategy = train_sampling_strategy
self.length_column_name = length_column_name
self.ddp_find_unused_parameters = ddp_find_unused_parameters
self.ddp_bucket_cap_mb = ddp_bucket_cap_mb
self.ddp_broadcast_buffers = ddp_broadcast_buffers
self.ddp_static_graph = ddp_static_graph
self.ddp_backend = ddp_backend
self.ddp_timeout = ddp_timeout
self.fsdp = fsdp
self.fsdp_config = fsdp_config
self.deepspeed = deepspeed
self.debug = debug
self.skip_memory_metrics = skip_memory_metrics
self.do_train = do_train
self.do_eval = do_eval
self.do_predict = do_predict
self.resume_from_checkpoint = resume_from_checkpoint
self.warmup_ratio = warmup_ratio
self.logging_dir = logging_dir
self.local_rank = local_rank
Using this as is will yield a SyntaxError because of the <factory> issue
highlighted above. We can use on a “normal” class Trainer.
fields, annotations = get_constructor_field_annotations(Trainer, include_bases=False)
config_class_str = create_config_class_str(fields)
print(config_class_str)
class Config:
def __init__(self, model: Union = None, args: Union = None, data_collator: Union = None, train_dataset: Union = None, eval_dataset: Union = None, processing_class: Union = None, model_init: Union = None, compute_loss_func: Union = None, compute_metrics: Union = None, callbacks: Union = None, optimizers: tuple = (None, None), optimizer_cls_and_kwargs: Union = None, preprocess_logits_for_metrics: Union = None):
self.model = model
self.args = args
self.data_collator = data_collator
self.train_dataset = train_dataset
self.eval_dataset = eval_dataset
self.processing_class = processing_class
self.model_init = model_init
self.compute_loss_func = compute_loss_func
self.compute_metrics = compute_metrics
self.callbacks = callbacks
self.optimizers = optimizers
self.optimizer_cls_and_kwargs = optimizer_cls_and_kwargs
self.preprocess_logits_for_metrics = preprocess_logits_for_metrics
import transformers
import typing
import torch
from transformers import DataCollator
NoneType = type(None)
config_class_str = create_config_class_str(fields)
# Execute the generated class definition string
namespace = {}
exec(config_class_str, globals(), namespace)
# Extract the newly created class from the namespace
ConfigClass = namespace["Config"]
inspect.signature(ConfigClass.__init__)
<Signature (self, model: <class 'Union'> = None, args: <class 'Union'> = None, data_collator: <class 'Union'> = None, train_dataset: <class 'Union'> = None, eval_dataset: <class 'Union'> = None, processing_class: <class 'Union'> = None, model_init: <class 'Union'> = None, compute_loss_func: <class 'Union'> = None, compute_metrics: <class 'Union'> = None, callbacks: <class 'Union'> = None, optimizers: tuple = (None, None), optimizer_cls_and_kwargs: <class 'Union'> = None, preprocess_logits_for_metrics: <class 'Union'> = None)>