# tff.program.FileProgramStateManager

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/federated/blob/v0.87.0 Version 2.0, January 2004 Licensed under the Apache License, Version 2.0 (the) |

A [`tff.program.ProgramStateManager`](https://www.tensorflow.org/federated/api_docs/python/tff/program/ProgramStateManager) that is backed by a file system.

Inherits From: [`ProgramStateManager`](https://www.tensorflow.org/federated/api_docs/python/tff/program/ProgramStateManager)

    tff.program.FileProgramStateManager(
        root_dir: Union[str, os.PathLike[str]],
        prefix: str = 'program_state_',
        keep_total: int = 5,
        keep_first: bool = True,
        keep_every_k: int = 1
    )

A [`tff.program.FileProgramStateManager`](https://www.tensorflow.org/federated/api_docs/python/tff/program/FileProgramStateManager) is a utility for saving and loading
program state to a file system in a federated program and is used to implement
fault tolerance. In particular, it is intended to only restart the same
simulation and run with the same version of TensorFlow Federated.

Program state is saved to the file system using the SavedModel (see
[`tf.saved_model`](https://www.tensorflow.org/api_docs/python/tf/saved_model)) format. When the program state is saved, each
[`tff.program.MaterializableValueReference`](https://www.tensorflow.org/federated/api_docs/python/tff/program/MaterializableValueReference) is materialized and each
`tff.Serializable` is serialized. The structure of the program state is
discarded, but is required to load the program state.

> [!NOTE]
> **Note:** The SavedModel format can only contain values that can be converted to a [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) (see [`tf.convert_to_tensor`](https://www.tensorflow.org/api_docs/python/tf/convert_to_tensor)), releasing any other values will result in an error.

See <https://www.tensorflow.org/guide/saved_model> for more information about
the SavedModel format.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `root_dir` | A path on the file system to save program state. If this path does not exist it will be created. |
| `prefix` | A string to use as the prefix for filenames. |
| `keep_total` | An integer representing the total number of program states to keep. If the value is zero or smaller, there will be no limitation on how many program states to keep; if keep_every_k is 1, then all states will be kept. |
| `keep_first` | A boolean indicating if the first program state should be kept, irrespective of whether it is the oldest program state or not. This is desirable in settings where you would like to ensure full reproducibility of the simulation, especially in settings where model weights or optimizer states are initialized randomly. By loading from the initial program state, one can avoid re-initializing and obtaining different results. |
| `keep_every_k` | An integer representing how often program states should be kept. The latest version will always be kept. Defaults to 1. Even when keep_total is zero or negative, this setting will still be applied. To keep all states, set this to 1. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | If `root_dir` is an empty string. |

<br />

## Methods

### `get_versions`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    get_versions()

Returns a list of saved versions or `None`.

<br />

<br />

<br />

| Returns ||
|---|---|
| A list of saved versions or `None` if there is no saved program state. ||

<br />

### `load`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    load(
        version, structure
    ) -> https://www.tensorflow.org/federated/api_docs/python/tff/program#ProgramStateStructure

Returns the program state for the given `version`.

<br />

<br />

<br />

| Args ||
|---|---|
| `version` | A integer representing the version of a saved program state. |
| `structure` | The structure of the saved program state for the given `version` used to support serialization and deserialization of user-defined classes in the structure. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ProgramStateNotFoundError` | If there is no program state for the given `version`. |

<br />

### `load_latest`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    load_latest(
        structure
    )

Returns the latest saved program state and version or (`None`, 0).

<br />

<br />

<br />

| Args ||
|---|---|
| `structure` | The structure of the saved program state for the given `version` used to support serialization and deserailization of user-defined classes in the structure. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A tuple of the latest saved (program state, version) or (`None`, 0) if there is no latest saved program state. ||

<br />

### `remove_all`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    remove_all()

Removes all program states.

### `save`

[View source](https://github.com/tensorflow/federated/blob/v0.87.0
Version 2.0, January 2004
Licensed under the Apache License, Version 2.0 (the)

    save(
        program_state, version
    )

Saves `program_state` for the given `version`.

<br />

<br />

<br />

| Args ||
|---|---|
| `program_state` | A [`tff.program.ProgramStateStructure`](https://www.tensorflow.org/federated/api_docs/python/tff/program#ProgramStateStructure) to save. |
| `version` | A strictly increasing integer representing the version of a saved `program_state`. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ProgramStateExistsError` | If there is already program state for the given `version`. |

<br />