patches
Contains code that patches certain issues from other libraries that we expect will be resolved in the future.
PatchedModuleDict
Bases: ModuleDict
This class provides a workaround for key conflicts in PyTorch's ModuleDict by ensuring unique internal keys.
Source code in src/lighter/utils/patches.py
__contains__(key)
Checks if a module is associated with the given key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The key to check. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
True if the key exists, False otherwise. |
Source code in src/lighter/utils/patches.py
__delitem__(key)
Deletes the module associated with the given key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The key for which to delete the module. |
required |
__getitem__(key)
Retrieves the module associated with the given key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The key for which to retrieve the module. |
required |
Returns:
Type | Description |
---|---|
torch.nn.Module: The module associated with the key. |
Source code in src/lighter/utils/patches.py
__init__(modules=None)
Initializes the PatchedModuleDict with optional modules.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
modules
|
dict
|
A dictionary of modules to initialize the ModuleDict. |
None
|
__setitem__(key, module)
Sets the module for the given key, ensuring a unique internal key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
The key to associate with the module. |
required |
module
|
Module
|
The module to store. |
required |
Source code in src/lighter/utils/patches.py
items()
Returns the items (key, module) in the dictionary.
Returns:
Name | Type | Description |
---|---|---|
Generator |
A generator yielding key, module pairs. |
keys()
Returns the keys of the modules.
Returns:
Name | Type | Description |
---|---|---|
KeysView |
A view of the keys in the dictionary. |
values()
Returns the modules in the dictionary.
Returns:
Name | Type | Description |
---|---|---|
Generator |
A generator yielding modules. |