nn
Module for PyTorch neural network utilities.
Classes:
| Name | Description |
|---|---|
ModuleDefaultDict |
Holds submodules in a dictionary and calls a factory function to supply missing values. |
TiedLinear |
A bias-free linear layer whose weight is shared with another module, read live on every call. |
ModuleDefaultDict
¶
Bases: ModuleDict, Generic[ModuleT]
Holds submodules in a dictionary and calls a factory function to supply missing values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Callable[[], ModuleT] | None
|
A no argument constructor for missing |
None
|
|
ModuleT
|
Initial dictionary values. |
{}
|
Methods:
| Name | Description |
|---|---|
__getitem__ |
Retrieve a module by key, creating it if missing. |
TiedLinear
¶
A bias-free linear layer whose weight is shared with another module, read live on every call.
TiedLinear does not copy or cache tied_module's weight; it reads tied_module.weight on every call. This keeps the two
layers tied even if tied_module is later re-wrapped by something like FSDP, which replaces the memory backing a weight but not
the Module object that owns it.
TiedLinear intentionally does not subclass torch.nn.Module. Module.__setattr__ auto-registers any Module-valued attribute
as a submodule, which would cause tied_module to be registered twice (once under its original owner, once under TiedLinear),
duplicating its weight in state_dict() and parameters(). The actual torch.nn.functional.linear call is performed by the
linear attribute, a real Module, so forward hooks can still be attached to observe the transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Module
|
The module whose |
required |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If |
Added in version v3.58.0. Native replacement for torchtune's `TiedLinear`, removing the `torchtune` dependency.
Methods:
| Name | Description |
|---|---|
__call__ |
Apply the tied linear transform. |
__call__
¶
Apply the tied linear transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
Input tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
torch.Tensor
|
The output tensor of shape |