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

Function async_save

python/paddle/framework/io.py:97–163  ·  view source on GitHub ↗

async version of paddle.save. Note: currently only support dygraph mode. Note: any argument passed through configs will be overridden by default setting. Args: obj(Object) : The object to be saved. path(str|BytesIO) : The path/buffer of the object to

(
    obj: object,
    path: str | BytesIO,
    protocol: Literal[2, 3, 4] = 4,
    sync_other_task: bool = False,
    **configs: Unpack[_EmptyDict],
)

Source from the content-addressed store, hash-verified

95
96
97def async_save(
98 obj: object,
99 path: str | BytesIO,
100 protocol: Literal[2, 3, 4] = 4,
101 sync_other_task: bool = False,
102 **configs: Unpack[_EmptyDict],
103) -> None:
104 '''
105 async version of paddle.save.
106 Note:
107 currently only support dygraph mode.
108 Note:
109 any argument passed through configs will be overridden by default setting.
110 Args:
111 obj(Object) : The object to be saved.
112 path(str|BytesIO) : The path/buffer of the object to be saved.
113 If saved in the current directory, the input path string will be used as the file name.
114 protocol(int, optional): The protocol version of pickle module must be greater than 1 and less than 5.
115 Default: 4
116 sync_other_task(bool) : Determine whether to wait other async save task to be finished before this one be put in queue.
117 **configs(dict, optional): compatible argument to paddle.save, but will be overridden by default setting.
118 Examples:
119 .. code-block:: pycon
120 :name: code-example-1
121
122 import paddle
123 emb = paddle.nn.Embedding(10, 10)
124 layer_state_dict = emb.state_dict()
125
126 # call paddle.async_save with the same style of paddle.save
127 paddle.async_save(layer_state_dict, "emb.pdparams")
128 for i in range(10):
129 # do some calculations here
130 # wait if any async_save task has not been done
131 paddle.clear_async_task_queue()
132 '''
133 if not in_dygraph_mode():
134 raise ValueError(
135 "async_save currently is not supported in static mode."
136 )
137 if len(configs) > 0:
138 warnings.warn(
139 "configs are not supported in async mode, will be overridden by default settings."
140 )
141
142 # TODO: make this part async
143 def move_state_dict_to_cpu(sd):
144 for k, v in sd.items():
145 if isinstance(v, dict):
146 move_state_dict_to_cpu(v)
147 elif isinstance(v, core.eager.Tensor):
148 sd[k] = v.pin_memory() if core.is_compiled_with_cuda() else v
149
150 if isinstance(obj, dict):
151 move_state_dict_to_cpu(obj)
152 elif isinstance(obj, core.eager.Tensor):
153 obj = obj.pin_memory() if core.is_compiled_with_cuda() else obj
154 else:

Callers

nothing calls this directly

Calls 10

in_dygraph_modeFunction · 0.90
ValueErrorClass · 0.85
move_state_dict_to_cpuFunction · 0.85
TypeErrorClass · 0.85
pin_memoryMethod · 0.80
typeFunction · 0.50
warnMethod · 0.45
startMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected