MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / load_program_state

Function load_program_state

python/paddle/static/io.py:1984–2166  ·  view source on GitHub ↗

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
)

Source from the content-addressed store, hash-verified

1982
1983
1984def 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):

Callers

nothing calls this directly

Calls 15

global_blockMethod · 0.95
_load_program_scopeFunction · 0.90
ProgramClass · 0.90
_pickle_loads_macFunction · 0.90
_pack_loaded_dictFunction · 0.90
ValueErrorClass · 0.85
clone_var_to_blockFunction · 0.85
_safe_load_pickleFunction · 0.85
existsMethod · 0.80
CPUPlaceMethod · 0.80
debugMethod · 0.45

Tested by

no test coverage detected