ikkuna.export¶
Module Contents¶
-
class
ikkuna.export.Exporter(depth, module_filter=None, message_bus=<ikkuna.export.messages.MessageBus object>)[source]¶ Bases:
objectClass for managing publishing of data from model code.
An
Exporteris used in the model code by either explicitly registering modules for tracking withadd_modules()or by calling it with newly constructed modules which will then be returned as-is, but be registered in the process.e = Exporter(...) features = nn.Sequential([ nn.Linear(...), e(nn.Conv2d(...)), nn.ReLU() ])
Modules will be tracked recursively unless specified otherwise, meaning the following is possible:
e = Exporter(...) e.add_modules(extremely_complex_model) # e will now track all layers of extremely_complex_model
Three further changes to the training code are necessary
set_model()to have theExporterwire up the appropriate callbacks.set_loss()should be called with the loss function so that labels can be extracted during training if if anySubscribers rely on the'input_labels'messageepoch_finished()should be called if anySubscribers rely on the'epoch_finished'message
-
_modules¶ All tracked modules
Type: dict(torch.nn.Module, ikkuna.utils.NamedModule)
-
_model¶ Type: torch.nn.Module
-
_module_filter¶ Set of modules to capture when calling
add_modules(). Everything not in this list is ignoredType: list(torch.nn.Module)
-
message_bus¶
-
_add_module_by_name(named_module)[source]¶ Register a module with a name attached.
Parameters: named_module (ikkuna.utils.NamedModule) –
-
add_modules(module, recursive=True)[source]¶ Add modules to supervise. If the module has
weightand/orbiasmembers, updates to those will be tracked. Ignores any module in_module_filter.Parameters: - module (tuple(str, torch.nn.Module) or torch.nn.Module) –
- recursive (bool) – Descend recursively into the module tree
- depth (int) – Depth to which to traverse the tree. Modules below this level will be ignored
Raises: ValueError– Ifmoduleis neither a tuple, nor a (subclass of)torch.nn.Module
-
__call__(module, recursive=True)[source]¶ Shorthand for
add_modules()which returns its input unmodified.:param see
Exporter.add_modules():Returns: The input moduleReturn type: torch.nn.Module
-
new_input_data(*args)[source]¶ Callback for new training input to the network.
Parameters: *args (tuple) – Network inputs
-
new_output_and_labels(network_output, labels)[source]¶ Callback for final network output.
Parameters: data (torch.Tensor) – The final layer’s output
-
new_activations(module, in_, out_)[source]¶ Callback for newly arriving activations. Registered as a hook to the tracked modules. Will trigger export of all new activation and weight/bias data.
Parameters: - module (torch.nn.Module) –
- in (torch.Tensor) – Dunno what this is
- out (torch.Tensor) – The new activations
-
new_layer_gradients(module, gradients)[source]¶ Callback for newly arriving layer gradients (loss wrt layer output). Registered as a hook to the tracked modules.
Warning
Currently, only layers with one output are supported.
Parameters: - module (torch.nn.Module) –
- gradients (torch.Tensor) – The gradients of the loss w.r.t. layer output
Raises: RuntimeError– If the module has multiple outputs
-
new_parameter_gradients(module, gradients)[source]¶ Callback for newly arriving gradients wrt weight and/or bias. Registered as a hook to the tracked modules. Will trigger export of all new gradient data.
Parameters: - module (torch.nn.Module) –
- gradients (tuple(torch.Tensor, torch.Tensor)) – The gradients w.r.t weight and bias.
-
set_model(model)[source]¶ Set the model for direct access for some metrics.
Parameters: model (torch.nn.Module) –
-
set_loss(loss_function)[source]¶ Add hook to loss function to extract labels.
Parameters: loss_function (torch.nn._Loss) –
-
freeze_module(module)[source]¶ Convenience method for freezing training for a module.
Parameters: module (torch.nn.Module) – Module to freeze
Submodules¶
ikkuna.export.messages¶
-
ikkuna.export.messages.META_KINDS= {'batch_finished', 'batch_started', 'epoch_finished', 'epoch_started', 'input_data', 'input_labels', 'loss', 'network_output'}¶ Message kinds which are not tied to any specific module. These topics is just what comes with the library, others can be added to a specific
MessageBus
-
ikkuna.export.messages.DATA_KINDS= {'activations', 'bias_gradients', 'bias_updates', 'biases', 'layer_gradients', 'weight_gradients', 'weight_updates', 'weights'}¶ Message kinds which are tied to a specific module and always carry data. These topics is just what comes with the library, others can be added to a specific
MessageBus
-
class
ikkuna.export.messages.Message(tag, global_step, train_step, epoch, kind)[source]¶ Bases:
abc.ABCBase class for messages emitted from the
Exporter.These messages are assembled into
MessageBundleobjects in theSubscription.-
data¶ This field is optional for
NetworkMessage, but mandatory forModuleMessageType: torch.Tensor, tuple(torch.Tensor) or None
-
key¶ A key used for grouping messages into
MessageBundlesType: object
-
-
class
ikkuna.export.messages.NetworkMessage(tag, global_step, train_step, epoch, kind, data=None)[source]¶ Bases:
ikkuna.export.messages.MessageA message with meta information not tied to any specific module. Can still carry tensor data, if necessary.
-
data¶ Optional data. Can be used e.g. for input to the network, labels or network output
Type: torch.Tensor, tuple, float, int or None
-
key¶ A key used for grouping messages into
MessageBundlesType: object
-
-
class
ikkuna.export.messages.ModuleMessage(tag, global_step, train_step, epoch, kind, named_module, data)[source]¶ Bases:
ikkuna.export.messages.MessageA message tied to a specific module, with tensor data attached.
-
module¶ Module emitting this data
Type: torch.nn.Module
-
key¶ A key used for grouping messages into
MessageBundlesType: object
-
-
class
ikkuna.export.messages.MessageBundle(kinds)[source]¶ Bases:
objectData object for holding a set of artifacts for a module (or meta information) at one point during training. This data type can be used to buffer different kinds and check whether all expected kinds have been received for a module or meta information. The collection is enforced to be homogeneous with respect to global step, train step, epoch, and identifier (
Message.key)-
__init__(kinds)[source]¶ Parameters: kinds (str or list) – Single kind when a Subscriptionis used, or a list of Message kinds contained in this bundle for use withSynchronizedSubscription
-
key¶ An object denoting the common aspect of the collected messages (besides the steps). This can be the
NamedModuleemitting the data or a string such as'META'or other denoting these are messages which do not belong to a module.Type: str
-
kinds¶ Alias to
expected_kindsType: list(str)
-
data¶ The tensors received for each kind
Type: dict(str, torch.Tensor)
-
train_step¶ Sequence number (training step) of the received messages (should match across all msgs in one iteration)
Type: int
-
complete()[source]¶ Check if all expected messages have been received. This means the bundle can be released to subscribers.
Returns: Return type: bool
-
check_message(message)[source]¶ Check consistency of sequence number, step and epoch or set if not set yet. Check consistency of identifier and check for duplication.
Parameters: message (ikkuna.export.messages.Message) – Raises: ValueError– Ifmessage.(global_step|step|epoch|identifier)does not match the current(global_step|step|epoch|identifier)or in case a message ofmessage.kindhas already been received
-
add_message(message)[source]¶ Add a new message to this object. Will fail if the new messsage does not have the same sequence number and epoch.
Parameters: message (ikkuna.export.messages.Message) – Raises: ValueError– seecheck_message()
-
-
class
ikkuna.export.messages.MessageBus(name)[source]¶ Bases:
objectA class which receives messages, registers subscribers and relays the former to the latter.
-
register_subscriber(sub)[source]¶ Add a new subscriber to the set. Adding subscribers mutliple times will still only call them once per message.
Parameters: sub (ikkuna.export.subscriber.Subscriber) – Raises: ValueError– If any of the kinds the Subscriber is interested in wasn’t previously registered
-
publish_network_message(global_step, train_step, epoch, kind, data=None, tag='default')[source]¶ Publish an update of type
NetworkMessageto all registered subscribers.Parameters:
-
publish_module_message(global_step, train_step, epoch, kind, named_module, data, tag='default')[source]¶ Publish an update of type
ModuleMessageto all registered subscribers.Parameters: - global_step (int) – Global training step
- train_step (int) – Epoch-relative training step
- epoch (int) – Epoch index
- kind (str) – Kind of message
- named_module (ikkuna.utils.NamedModule) – The module in question
- data (torch.Tensor) – Payload
-
-
ikkuna.export.messages.get_default_bus()[source]¶ Get the default message bus which is always created when this module is loaded.
Returns: Return type: MessageBus