Module#
API#
- class warp_nn.modules.module.Module(*args, requires_grad: bool = True, **kwargs)[source]#
Bases:
ABCBase abstract class for all the modules.
Modules can contain other modules (sub-modules), organized in a nested tree structure. Such sub-modules can be assigned as regular attributes to the parent module.
Important
Sub-modules assigned as regular attributes to the parent module are not registered automatically. Therefore, it is necessary to call the
__post_init__()method before exiting the initialization of the module (i.e. at the end of the class constructor).- Parameters:
args – Additional positional arguments.
requires_grad – Whether the parameters and the cached output arrays of the module require gradients.
kwargs – Additional keyword arguments.
- __call__(*args, **kwargs) Any[source]#
Forward pass of the module.
- Raises:
NotImplementedError – If the module subclass does not implement the method.
- __post_init__() None[source]#
Register sub-modules, parameters and buffers.
Important
A module subclass must call this method to register sub-modules, parameters and buffers assigned as regular attributes to it, unless they have already been registered manually.
- buffers( ) list[Buffer | array][source]#
Get the registered buffers.
The buffers will be returned in the order that they were registered.
- Parameters:
include_submodules – Whether to include the buffers of the registered sub-modules.
as_array – Whether to return the buffers as Warp arrays or as
Bufferinstances.
- Returns:
A list of buffers.
- eval() Module[source]#
Set the module and its registered sub-modules in evaluation mode.
This is equivalent to
module.train(False).- Returns:
The module itself.
- load_state_dict( ) None[source]#
Load a state dictionary into the module.
- Parameters:
state_dict – The state dictionary to load into the module.
- Raises:
NotImplementedError – If the state dictionary contains an unsupported type.
- modules() list[Module][source]#
Get the registered modules.
The modules will be returned in the order that they were registered.
- Returns:
A list of modules.
- named_buffers() list[str, Buffer][source]#
Get the registered buffers and their names.
The buffers will be returned in the order that they were registered.
- Returns:
A list of (name, buffer) pairs.
- named_modules() list[str, Module][source]#
Get the registered modules and their names.
The modules will be returned in the order that they were registered.
- Returns:
A tuple of (name, module) pairs.
- named_parameters() list[str, Parameter][source]#
Get the registered parameters and their names.
The parameters will be returned in the order that they were registered.
- Returns:
A list of (name, parameter) pairs.
- parameters( ) list[Parameter | array][source]#
Get the registered parameters.
The parameters will be returned in the order that they were registered.
- Parameters:
include_submodules – Whether to include the parameters of registered the sub-modules.
as_array – Whether to return the parameters as Warp arrays or as
Parameterinstances.
- Returns:
A list of parameters.
- register_buffer( ) Buffer[source]#
Register a buffer to the module.
The buffers will be registered in the order that this method is called.
- Parameters:
name – The name of the buffer.
buffer – The buffer to register.
- Returns:
The buffer itself.
- raises:
TypeError: If the buffer is not a Buffer subclass. KeyError: If a buffer or parameter with the same name already exists.
- register_module( ) Module[source]#
Register a module to the module.
The modules will be registered in the order that this method is called.
- Parameters:
name – The name of the module.
module – The module to register.
- Returns:
The module itself.
- raises:
TypeError: If the module is not a Module subclass. KeyError: If the module with the same name already exists.
- register_parameter( ) Parameter[source]#
Register a parameter to the module.
The parameters will be registered in the order that this method is called.
- Parameters:
name – The name of the parameter.
parameter – The parameter to register.
- Returns:
The parameter itself.
- raises:
TypeError: If the parameter is not a Parameter subclass. KeyError: If a parameter or buffer with the same name already exists.
- state_dict( ) dict[str, array][source]#
Get the state dictionary, which is a reference to all the parameters and buffers of the modules and sub-modules.
- Parameters:
destination – The destination dictionary to store the state dictionary. This argument is used for internal recursion and should not be set by the user.
prefix – The prefix to add to the names of the parameters and modules. This argument is used for internal recursion and should not be set by the user.
- Returns:
The state dictionary.
- to(
- device: Device,
Move the module to the specified device.
- Parameters:
device – The device to move the module to.
- Returns:
The module itself.
- train(mode: bool = True) Module[source]#
Set the module and its registered sub-modules in training or evaluation mode.
The mode only affects modules that behave differently during training and evaluation (e.g.
DropoutorBatchNorm). Modules are in training mode by default.- Parameters:
mode – Whether to set training mode (true) or evaluation mode (false).
- Returns:
The module itself.
- property cache: Cache[source]#
Sub-cache of the module in the global cache (see
cache).It holds the data cached by the module (e.g. the output arrays of the forward pass), which is cleared when the module is moved to another device.
- property config: KernelConfig[source]#
Kernel configuration with which the module was created.
It is read when the module is created (see
kernel_config()).
- property kernels: Kernels[source]#
Kernels of the module by kernel configuration (see
Kernels).The module sets its kernel factory (usually in the constructor), and selects its kernels for the kernel configurations adapted to the launch shapes (see
adapt()). Unlike the data incache, the kernels are kept when the cache is disabled or cleared, and when the module is moved to another device.Note
Not every built-in module uses it: the modules whose kernels are not adapted to the launch shapes (e.g. the convolution, pooling and normalization layers) create their kernels on their own, and keep the kernel factory unset.
# constructor self.kernels.factory = partial(create_kernels, include_bias=bias) # forward pass config = self.config.adapt(shape, self.device, policy=AdaptationPolicy.MATMUL) kernel = self.kernels.select(config=config)
- class warp_nn.modules.module.Kernels[source]#
Kernels of a module by kernel configuration, created the first time a configuration is requested.
Instances are created by the modules (see
Module.kernels), and should not be instantiated directly.- __contains__(
- config: KernelConfig,
Whether the kernels for a kernel configuration have been created.
- Parameters:
config – Kernel configuration.
- Returns:
Whether the kernels for the kernel configuration have been created.
- __len__() int[source]#
Number of kernel configurations whose kernels have been created.
- Returns:
The number of kernel configurations whose kernels have been created.
- select(
- *,
- config: KernelConfig,
- key: Hashable | None = None,
Select the kernel for a kernel configuration, creating (and keeping) the kernels on first request.
Select the kernels with the configurations adapted to the launch shapes (see
adapt()), so that the launch shapes with the same adapted configuration share them. If the factory creates a collection of kernels (e.g. kernel overloads indexed by number of dimensions and data type), give the key of the kernel to select it from the collection.- Parameters:
config – Kernel configuration (usually adapted to the launch shape).
key – Key of the kernel in the collection created by the factory (e.g.
(ndim, dtype)), which must then be subscriptable. IfNone, what the factory created is returned.
- Returns:
The kernel (or what the factory created, if no key is given) for the kernel configuration.
- Raises:
RuntimeError – If the kernel factory has not been set.
- property factory: Callable[[...], Any] | None[source]#
Kernel factory, or
Noneif it has not been set.It is a function that creates the kernels for a kernel configuration, called as
factory(config=config)(bind its other arguments withfunctools.partial()). It should create them in unique Warp modules (@wp.kernel(module="unique")), so that the kernels of each configuration compile on their own. Setting it discards the kernels created by the previous factory (if any). Once set, it cannot be unset.- Raises:
TypeError – If the factory set is not callable.