Serialize default main program according to feed_vars and fetch_vars. Args: feed_vars(Tensor | list[Tensor]): Tensor needed by inference. fetch_vars(Tensor | list[Tensor]): Tensor returned by inference. kwargs: Supported keys including ``program``. Attention please
(
feed_vars: Tensor | list[Tensor],
fetch_vars: Tensor | list[Tensor],
**kwargs: Unpack[_SerializeProgramKwargs],
)
| 303 | |
| 304 | @static_only |
| 305 | def serialize_program( |
| 306 | feed_vars: Tensor | list[Tensor], |
| 307 | fetch_vars: Tensor | list[Tensor], |
| 308 | **kwargs: Unpack[_SerializeProgramKwargs], |
| 309 | ) -> bytes: |
| 310 | """ |
| 311 | |
| 312 | Serialize default main program according to feed_vars and fetch_vars. |
| 313 | |
| 314 | Args: |
| 315 | feed_vars(Tensor | list[Tensor]): Tensor needed by inference. |
| 316 | fetch_vars(Tensor | list[Tensor]): Tensor returned by inference. |
| 317 | kwargs: Supported keys including ``program``. Attention please, kwargs is used for backward compatibility mainly. |
| 318 | |
| 319 | - program(Program): specify a program if you don't want to use default main program. |
| 320 | - legacy_format(bool): whether to save inference program in legacy format. Defaults to False. |
| 321 | |
| 322 | Returns: |
| 323 | bytes: serialized program. |
| 324 | |
| 325 | Examples: |
| 326 | .. code-block:: pycon |
| 327 | |
| 328 | >>> # doctest: +SKIP("paddle.static.serialize_program doesn't support PIR mode") |
| 329 | >>> import paddle |
| 330 | >>> paddle.enable_static() |
| 331 | |
| 332 | >>> path_prefix = "./infer_model" |
| 333 | |
| 334 | # User defined network, here a softmax regression example |
| 335 | >>> image = paddle.static.data(name='img', shape=[None, 28, 28], dtype='float32') |
| 336 | >>> label = paddle.static.data(name='label', shape=[None, 1], dtype='int64') |
| 337 | >>> predict = paddle.static.nn.fc(image, 10, activation='softmax') |
| 338 | |
| 339 | >>> loss = paddle.nn.functional.cross_entropy(predict, label) |
| 340 | |
| 341 | >>> exe = paddle.static.Executor(paddle.CPUPlace()) |
| 342 | >>> exe.run(paddle.static.default_startup_program()) |
| 343 | |
| 344 | # serialize the default main program to bytes. |
| 345 | >>> serialized_program = paddle.static.serialize_program([image], [predict]) |
| 346 | |
| 347 | # deserialize bytes to program |
| 348 | >>> deserialized_program = paddle.static.deserialize_program(serialized_program) |
| 349 | |
| 350 | """ |
| 351 | # verify feed_vars |
| 352 | _check_vars('feed_vars', feed_vars) |
| 353 | # verify fetch_vars |
| 354 | _check_vars('fetch_vars', fetch_vars) |
| 355 | |
| 356 | program = _get_valid_program(kwargs.get('program', None)) |
| 357 | program = normalize_program(program, feed_vars, fetch_vars) |
| 358 | legacy_format = kwargs.get('legacy_format', False) |
| 359 | return _serialize_program(program, legacy_format=legacy_format) |
| 360 | |
| 361 | |
| 362 | def _serialize_program(program: Program, legacy_format: bool = False) -> bytes: |
nothing calls this directly
no test coverage detected