Base Module¶
-
class
ablinfer.DispatchBase(config: Mapping[KT, VT_co] = None)[source]¶ Base class for dispatching to models.
This class cannot actually be instantiated; users are intended to instantiate a subclass that dispatches using the appropriate method (e.g.
ablinfer.docker.DispatchDocker).The general procedure when running is:
_validate_config()_validate_model_config()_make_fmap()_make_flags()_make_command()_run_processing()(dispatches required nodes to_clone()to copy them)_save_input()_run_command()_load_output()_run_processing()_cleanup()
Functions are expected to raise exceptions if they encounter any unfixable errors. If an exception is raised,
_cleanup_all()will be called.-
run(model: Mapping[KT, VT_co], model_config: Mapping[KT, VT_co], progress: Callable[[ablinfer.constants.DispatchStage, float, float, str], None] = <built-in function print>) → None[source]¶ Run the model.
This is the entry point
Parameters: - model – The model specification.
- model_config – The model configuration.
- progress – an optional function accepting a
DispatchStage, a float on [0,1] representing progress in the current stage, a float on [0,1] representing progress in the current operation, and a string with detailed info.
The following functions make up the actual functionality of the class, and at least the abstract ones must be overriden in derivative classes.
-
_validate_model_config() → None[source]¶ Validate the model configuration.
This first normalizes the model config, which also validates it. It then ensures that no inputs are null, no two outputs are the same, and no outputs are the same as inputs.
Note that when overriding this method without delegating here, the new method must normalize the model config by calling :func:
ablinfer.model.normalize_model_config.
-
_make_fmap() → Mapping[str, str][source]¶ Make the file map.
Returns: The created filemap, mapping the input/output name to its path as seen by whatever the command will be dispatched to (e.g. for Docker, this would be the path inside the container).
-
_make_flags(fmap: Mapping[str, str]) → List[str][source]¶ Construct the flags to pass to the command.
Parameters: fmap – The generated fmap.
-
_make_command(flags: List[str]) → List[str][source]¶ Make the final command.
Parameters: flags – A list of the flags to be included. Returns: The final command, to be passed to _run_command_().
-
_run_processing(inp: bool) → None[source]¶ Run pre/post processing.
Parameters: inp – Whether to do the input or output section.
-
_save_input(fmap: Mapping[str, str]) → None[source]¶ Save the input files at the appropriate locations for running the model.
See the
ablinfer.docker.DispatchDockerclass for an example. This function will need to be overridden to reflect whatever node format your subclass uses.
-
_run_command(cmd: List[str]) → None[source]¶ Run the actual command.
Must raise
DispatchExceptionif the called process runs into a problem.Parameters: cmd – The command to execute.
-
_load_output(fmap: Mapping[str, str]) → None[source]¶ Put the output files into the appropriate locations.
It is this function’s responsibility to set self._output_files as the files are created, so that they can be removed properly if the command fails.
Parameters: fmap – The file map.
-
_cleanup(error: Exception = None) → None[source]¶ Run successful cleanup.
This function should conduct all of the cleanup necessary for a successful run. This will also be called if the run fails. This means that this function should run checks of its own to see what actually needs to be cleaned up.
Parameters: error – The exception, if one occurred.