engine#
Engine is a coordinator for running algorithms and resolving ordering conflicts among them for composition.
The order in which algorithms are run matters significantly during composition. For example, the
SelectiveBackprop
algorithm runs on the Event.AFTER_DATALOADER
event and must run before
any data augmentations. Engine
runs re-ordering passes to resolve such ordering issues or conflicts.
These orderings are enforced by algorithm passes. The default passes registered to the Engine are found in
composer.core.passes
. To register a new pass, use Engine.register_pass()
, e.g.
from composer import Engine, Algorithm, Event
from typing import Sequence
def run_last(algorithms: Sequence[Algorithm], event: Event) -> Sequence[Algorithm]:
algorithms = sorted(algorithms, key=lambda x: isinstance(x, MyAlgorithm))
Engine.register_pass(run_last)
Note
Note
The design of
Engine
is subject to change in future releases to accommodate more complexity as we investigate composition of algorithms.
Trace#
Traces record whether an algorithm ran at a particular step and event combination and also the order of such executions.
These are logged with the key <algorithm_name>/<event>
.
For example, the algorithm LayerFreezing
, which runs at the end of every epoch on Event.EPOCH_END
,
will emit a series of traces:
[STEP=0][layer_freezing/INIT=0]
[STEP=1][layer_freezing/EPOCH_START=0]
[STEP=1][layer_freezing/BATCH_START=0]
...
[STEP=2][layer_freezing/BATCH_START=0]
...
[STEP=3][layer_freezing/BATCH_START=0]
...
[STEP=3][layer_freezing/EPOCH_END=1] # <-- layer freezing ran on step 3 here!
Classes
Coordinator for running algorithms and resolving ordering conflicts among them for composition. |
|
Record of an algorithm's execution. |
Attributes
- class composer.core.engine.Engine(state, logger)[source]#
Coordinator for running algorithms and resolving ordering conflicts among them for composition.
- Parameters
- close()[source]#
Shutdown the engine.
As part of the shutdown procedure,
Callback.close()
andCallback.post_close()
are invoked for each callback. Note thatCallback.post_close()
is invoked only for callbacks that did not raise an exception duringCallback.close()
.This method does not re-raise any exceptions from
Callback.close()
andCallback.post_close()
. Instead, these exceptions are logged as errors.
- register_pass(algorithm_pass, index=- 1)[source]#
Registers an algorithm pass with the Engine.
- Parameters
algorithm_pass (passes.AlgorithmPass) โ A method that maps a list of algorithms to a list of algorithms.
index (int, optional) โ The index to insert into the list of passes. If -1 (default), the pass will be insert to the end of the list.
- run_event(event)[source]#
Runs the sequence of algorithms and callbacks (see
Callback
).Filters algorithms by calling each oneโs
Algorithm.match()
method, internally checks for conflicting algorithms, then runs each algorithmโsAlgorithm.apply()
method to make in-place changes to thestate
.The default order of execution for algorithms is determined by the provided list. However,
Engine
makes changes to this order internally to resolve ordering conflicts.Returns
Traces
of the execution, a dictionary with keys formatted as<algorithm_name>/<event>
(e.g.,Blurpool/INIT
), and values are an instance ofTrace
.Callbacks are always run after algorithms and do not return a trace.
This method can be called with either the
Event
enum member values or a string of the event name.Examples
>>> engine = Engine(state, logger) >>> engine.run_event(Event.BEFORE_LOSS) OrderedDict() >>> # calling with a string of the event name also works >>> engine.run_event('before_loss') OrderedDict()
- run_marker_only_event(event)[source]#
Runs the marker for an event if the profiler is enabled.
This is primarily used to complete the dataloader marker at the end of the dataloader. In this scenario, the dataloader marker has started from Event.BEFORE_DATALOADER, but Event.AFTER_DATALOADER cannot be called as no batch was yielded from the dataloader.