Converts the ``layer`` with distributed tensor (constructed from ``paddle.distributed.shard_tensor``) to a static graph. ``to_static`` returns a DistModel instance containing the static graph for distributed training, evaluation and prediction. Args: layer(paddle.nn.Lay
(
layer: Layer,
loader: ShardDataloader | DataLoader | None = None,
loss: Layer | Callable[..., Any] | None = None,
optimizer: Optimizer | None = None,
strategy: Strategy | None = None,
input_spec: list[list[DistributedInputSpec]] | None = None,
)
| 3652 | |
| 3653 | |
| 3654 | def to_static( |
| 3655 | layer: Layer, |
| 3656 | loader: ShardDataloader | DataLoader | None = None, |
| 3657 | loss: Layer | Callable[..., Any] | None = None, |
| 3658 | optimizer: Optimizer | None = None, |
| 3659 | strategy: Strategy | None = None, |
| 3660 | input_spec: list[list[DistributedInputSpec]] | None = None, |
| 3661 | ) -> DistModel: |
| 3662 | """ |
| 3663 | Converts the ``layer`` with distributed tensor (constructed from |
| 3664 | ``paddle.distributed.shard_tensor``) to a static graph. ``to_static`` |
| 3665 | returns a DistModel instance containing the static graph for |
| 3666 | distributed training, evaluation and prediction. |
| 3667 | |
| 3668 | Args: |
| 3669 | layer(paddle.nn.Layer): The layer in dygraph mode, the parameters |
| 3670 | or its inputs can be distributed tensors. |
| 3671 | loader(ShardDataloader|paddle.io.DataLoader): The data loader used in dygraph mode, |
| 3672 | used to infer inputs_spec and labels_spec. |
| 3673 | loss(Loss|Callable|None, optional): The loss function for training |
| 3674 | or evaluating the model. Can be a `paddle.nn.Layer` instance or |
| 3675 | any callable function. Default: None. |
| 3676 | optimizer(paddle.optimizer.Optimizer|_ShardOptimizer|None, optional): |
| 3677 | The optimizer for training. It can `paddle.optimizer.Optimizer` |
| 3678 | or `_ShardOptimizer` wrapped by `shard_optimizer`. Default: None. |
| 3679 | strategy(paddle.distributed.Strategy|None, optional): Configs for |
| 3680 | parallel strategies and optimization settings (e.g. sharding, |
| 3681 | pipeline parallelism). Default: None. |
| 3682 | input_spec(list[list[paddle.distributed.DistributedInputSpec]]|None, optional): |
| 3683 | The custom input specs specify the shape, dtype, and name information |
| 3684 | of model inputs and labels. If it is not None, the input specs and |
| 3685 | label specs will be inferred from the custom input specs. The custom |
| 3686 | input specs should be a list containing two sublists: the first |
| 3687 | sublist represents theinput specs, and the second sublist represents |
| 3688 | the label specs. Default: None. |
| 3689 | |
| 3690 | Returns: |
| 3691 | DistModel: A ``DistModel`` instance converted the input ``layer``. |
| 3692 | |
| 3693 | Examples: |
| 3694 | .. code-block:: pycon |
| 3695 | |
| 3696 | >>> import numpy as np |
| 3697 | >>> import paddle |
| 3698 | >>> import paddle.distributed as dist |
| 3699 | >>> from paddle import nn |
| 3700 | >>> from paddle.distributed import Replicate, Shard |
| 3701 | |
| 3702 | >>> BATCH_SIZE = 4 |
| 3703 | >>> BATCH_NUM = 4 |
| 3704 | >>> IMAGE_SIZE = 16 |
| 3705 | >>> CLASS_NUM = 8 |
| 3706 | >>> class RandomDataset(paddle.io.Dataset): # type: ignore[type-arg] |
| 3707 | ... def __init__(self, images, labels, num_samples): |
| 3708 | ... self.images = images |
| 3709 | ... self.labels = labels |
| 3710 | ... self.num_samples = num_samples |
| 3711 | ... |
nothing calls this directly
no test coverage detected