Load program state from local file Args: model_path(str): The file prefix store the program var_list(list|tuple, optional): The Tensor list/tuple to load saved with [ save_params, save_persistables, save_vars ].
(
model_path: str, var_list: Sequence[Tensor] | None = None
)
| 1982 | |
| 1983 | |
| 1984 | def load_program_state( |
| 1985 | model_path: str, var_list: Sequence[Tensor] | None = None |
| 1986 | ) -> dict[str, npt.NDArray[Any]]: |
| 1987 | """ |
| 1988 | |
| 1989 | Load program state from local file |
| 1990 | |
| 1991 | Args: |
| 1992 | model_path(str): The file prefix store the program |
| 1993 | var_list(list|tuple, optional): The Tensor list/tuple to load saved with |
| 1994 | [ save_params, save_persistables, save_vars ]. |
| 1995 | Default: None. |
| 1996 | The var_list is only used to get name, |
| 1997 | will not be modified. |
| 1998 | Returns: |
| 1999 | state_dict(dict): the dict store Parameter and optimizer information |
| 2000 | |
| 2001 | Examples: |
| 2002 | |
| 2003 | .. code-block:: pycon |
| 2004 | |
| 2005 | >>> import paddle |
| 2006 | >>> import paddle.static as static |
| 2007 | |
| 2008 | >>> paddle.enable_static() |
| 2009 | |
| 2010 | >>> x = static.data(name="x", shape=[10, 10], dtype='float32') |
| 2011 | >>> linear1 = paddle.nn.Linear(10, 10) |
| 2012 | >>> linear2 = paddle.nn.Linear(10, 10) |
| 2013 | >>> y = linear1(x) |
| 2014 | >>> z = linear2(y) |
| 2015 | |
| 2016 | >>> place = paddle.CPUPlace() |
| 2017 | >>> exe = static.Executor(place) |
| 2018 | >>> exe.run(static.default_startup_program()) |
| 2019 | >>> prog = static.default_main_program() |
| 2020 | |
| 2021 | >>> static.save(prog, "./temp") |
| 2022 | >>> program_state = static.load_program_state("./temp") |
| 2023 | """ |
| 2024 | model_prefix = model_path |
| 2025 | if model_prefix.endswith(".pdparams"): |
| 2026 | model_prefix = model_prefix[:-9] |
| 2027 | elif model_prefix.endswith(".pdopt"): |
| 2028 | model_prefix = model_prefix[:-6] |
| 2029 | elif model_prefix.endswith(".pdmodel"): |
| 2030 | model_prefix = model_prefix[:-8] |
| 2031 | |
| 2032 | parameter_file_name = model_prefix + ".pdparams" |
| 2033 | if not os.path.exists(parameter_file_name): |
| 2034 | # model file saved with base.save is not found, try to load model file saved with |
| 2035 | # [save_vars, save_params, save_persistables] |
| 2036 | _logger.debug( |
| 2037 | f"{parameter_file_name} not found, try to load model file saved with [ save_params, save_persistables, save_vars ]" |
| 2038 | ) |
| 2039 | |
| 2040 | var_name_list = [] |
| 2041 | if var_list is None and os.path.isfile(model_path): |
nothing calls this directly
no test coverage detected