pytorch_pfn_extras.writing.Writer#

class pytorch_pfn_extras.writing.Writer(fs=None, out_dir='')#

Bases: object

Base class of snapshot writers.

Snapshot invokes __call__ of this class every time when taking a snapshot. This class determines how the actual saving function will be invoked.

Note

This extension first writes the serialized object to a temporary file and then rename it to the target file name. Thus, if the program stops right before the renaming, the temporary file might be left in the output directory.

Methods

__init__([fs, out_dir])

finalize()

Finalizes the writer.

initialize(out_dir)

save(filename, out_dir, target, savefun, ...)

Parameters:
  • fs (Any) –

  • out_dir (str) –

__call__(filename, out_dir, target, *, savefun=None, append=False)#

Does the actual writing to the file.

This method is invoked by a Snapshot object every time it takes a snapshot.

Parameters:
  • filename (str) – Name of the file into which the serialized target is saved. It is a concrete file name, i.e. not a pre-formatted template string.

  • out_dir (str) –

    Output directory. Corresponds to :py:attr:`ExtensionsManager.out

    <pytorch_pfn_extras.training.ExtensionsManager.out>`.

  • target (dict) – Serialized object which will be saved.

  • savefun (callable) – A callable that accepts a two positional arguments (an object to be serialized, file path) like torch.save.

  • append (bool) – Mode used to open the file. True to use the append mode, False to use the write mode (truncates the file if it already exists).

Return type:

None

__init__(fs=None, out_dir='')#
Parameters:
  • fs (Optional[Any]) –

  • out_dir (str) –

Return type:

None

finalize()#

Finalizes the writer.

Calling this method on already-finalized Writer does nothing.

Return type:

None

initialize(out_dir)#
Parameters:

out_dir (str) –

Return type:

None

save(filename, out_dir, target, savefun, append, **savefun_kwargs)#
Parameters:
  • filename (str) –

  • out_dir (str) –

  • target (Union[Sequence[Any], Mapping[str, Any]]) –

  • savefun (Callable[[...], None]) –

  • append (bool) –

  • savefun_kwargs (Any) –

Return type:

None