SprintControl

This is the Sprint interface implementation, concretely for Sprint PythonControl, Sprint PythonSegmentOrder and Sprint SprintNnPythonLayer. For reference, in Sprint code, see:

  • src/Nn/PythonControl.cc
  • src/Tools/NnTrainer/python_control_demo.py

This interface will behave similar to SprintExternInterface. See SprintErrorSignals for the other end. It can also be used as a PythonSegmentOrdering interface. It also supports SprintNnPythonLayer.

SprintControl.print(*args, **kwargs)[source]

print replacement.

Parameters:
  • args
  • kwargs
SprintControl.init(name, reference, config, sprint_unit=None, version_number=None, callback=None, **kwargs)[source]

This will be called by Sprint PythonControl. But we also call it ourselves e.g. in getSegmentList() and SprintNnPythonLayer. In this specific module, we expect that there is “c2p_fd” and “p2c_fd” in the config string to communicate with the parent process, which is usually handled by SprintErrorSignals.

Parameters:
  • name (str) – this specifies the caller. e.g. “Sprint.PythonControl”
  • reference – this is any object to identify the specific instance of the caller, if there are multiple.
  • config (str) – this will be passed over from Sprint. you can configure that via –*.pymod-config.
  • sprint_unit (str) – if this is called by Sprint PythonControl, this will specify which specific part of Sprint is using this PythonControl, because there can be multiple parts. E.g. there is “FeedForwardTrainer”, “SegmentwiseNnTrainer” and “NnTrainer.pythonControl”.
  • version_number (int|None) – if this is called by Sprint PythonControl, this will set the version number. only newer Sprint versions will set this.
  • callback (function|None) – if this is called by Sprint PythonControl, this might provide a callback. Only newer Sprint versions will provide this to init(). This callback can be used for many different actions. It’s supposed to be called like callback(action, **other_args), where action is a string. See Sprint PythonControl code about the possible actions and arguments.
  • kwargs – all remaining args are specific for each caller.
SprintControl.getSegmentList(corpusName, segmentList, config, **kwargs)[source]

Sprint will directly call this function.

class SprintControl.SprintNnPythonLayer(config, **kwargs)[source]

Sprint will directly call this class, i.e. create an instance of it. It implements the Sprint NN PythonLayer interface.

finalize(self)[source]

Called by Sprint at exit.

setInputDimension(self, stream, size)[source]
Parameters:
  • stream (int) –
  • size (int) –
setOutputDimension(self, size)[source]
Parameters:size (int) –
initializeNetworkParameters(self)[source]

Called by Sprint for param init.

loadNetworkParameters(self, filename)[source]
Parameters:filename (str) –
saveNetworkParameters(self, filename)[source]
Parameters:filename (str) –
isTrainable(self)[source]
Return type:bool
getNumberOfFreeParameters(self)[source]
Return type:int
forward(self, input)[source]
Parameters:input – tuple of input matrices of format (input_size,time). we ignore them.
Returns:single output matrix of format (output_size,time)
backpropagate(self, errorSignalIn)[source]
Parameters:errorSignalIn (numpy.ndarray) – matrix of format (output_size,time)
Returns:tuple of matrices of format (input_size,time)
Return type:numpy.ndarray
class SprintControl.PythonControl(c2p_fd, p2c_fd, **kwargs)[source]

This will send data to RETURNN over a pipe. We expect that we are child process and the parent process has spawned us,

An instance of this class is also the interface for multiple Sprint interfaces, i.e.:
  • PythonControl (standalone via NnTrainer tool)
  • PythonControl (via SegmentwiseNnTrainer)
  • implicitly PythonSegmentOrder (see code above)
Parameters:
  • c2p_fd (int) – child-to-parent file descriptor
  • p2c_fd (int) – parent-to-child file descriptor
Version = 1[source]
instance = None[source]
classmethod create(**kwargs)[source]
Parameters:kwargs – passed to PythonControl
Return type:PythonControl
init_processing(self, input_dim=None, output_dim=None, **kwargs)[source]

This is called via Sprint when we use PythonControl to iterate the corpus, i.e. we set –*.action=python-control in Sprint in the NN trainer tool. We expect that we use the Sprint callback to calculate loss and error signal. This is called on the first segment. input_dim/output_dim are set iff we extract features/alignments.

Parameters:
  • input_dim (int|None) –
  • output_dim (int|None) –
process_segment(self, name, orthography, features=None, alignment=None, soft_alignment=None, **kwargs)[source]

This is called via Sprint when we use PythonControl to iterate the corpus.

Parameters:
  • name (str) – segment name
  • orthography (str) – segment orth
  • features (numpy.ndarray|None) –
  • alignment (numpy.ndarray|None) –
  • soft_alignment (numpy.ndarray|None) –
close(self)[source]

Close pipe.

handle_next(self)[source]

Called by self.run_control_loop. We catch some message from our parent process, handle it and send back the result.

run_control_loop(self, callback, **kwargs)[source]

Called by Sprint when we are in PythonControl run_control_loop mode. Also called by us via self.run_threaded_control_loop().

exit(self, **kwargs)[source]

Called by Sprint.

check_control_loop_running(self)[source]

Called by Sprint.

run_threaded_control_loop(self)[source]

Called by Sprint.

own_threaded_callback(self, cmd, *args)[source]

This is used if we run our own control loop via run_threaded_control_loop.

own_tcb_version(self)[source]
Returns:version string
Return type:str
own_tcb_get_loss_and_error_signal(self, seg_name, seg_len, posteriors)[source]
Parameters:
  • seg_name
  • seg_len
  • posteriors
Returns:

init_segment(self, segment_name)[source]

Called by Sprint PythonControl in FeedForwardTrainer/SegmentwiseNnTrainer.

notify_segment_loss(self, segment_name, loss)[source]

Called by Sprint PythonControl in FeedForwardTrainer/SegmentwiseNnTrainer.

get_current_seg_posteriors(self, seg_len)[source]
Parameters:seg_len (int) – just for double checking, the length of the current segment
Returns:matrix (time,label)
set_current_seg_error_signal(self, seg_len, error_signal)[source]
Parameters:
  • seg_len (int) – just for double checking, the length of the current segment
  • error_signal – matrix (time,label)
set_current_seg_loss(self, seg_name, loss)[source]
Parameters:
  • seg_name (str|None) – just for double checking, the name of the current segment. might be None
  • loss (float) – the loss of the current seg
segment_list_iterator(self)[source]
Returns:yields segment names
Return type:typing.Iterator[str]