parameters: (func, args=(), nprocs=-1, join=True, daemon=False, **options)
return_statement: return context
docstring: Start multiple processes with ``spawn`` method for parallel training.
  
      .. note::
          ``spawn`` now only supports GPU or XPU collective mode. The collective mode
          of GPU and XPU cannot be started at the same time, so the option `gpus` and
          `xpus` cannot be configured at the same time.
  
      Args:
          func (function): The target function is called by spawned process.
              This function need to be able to pickled, so it must be defined
              at the top level of a module.
          args (list|tuple, optional): Arguments passed to ``func``.
          nprocs (int, optional): Number of processed to start. Default: -1.
              when nprocs is -1, the available device will be obtained from
              the environment variable when the model is executed: If use GPU,
              the currently available device ID is obtained from the environment
              variable CUDA_VISIBLE_DEVICES; If use XPU, the currently available
              device ID is obtained from the environment variable XPU_VISIBLE_DEVICES.
          join (bool, optional): Perform a blocking join on all spawned processes.
              Default: True.
          daemon (bool, optional): The spawned processes' daemon flag. Default: False.
          **options(dict, optional): Other initial parallel execution environment
              configuration options. The following options are currently supported:
              (1) start_method (string): the way to start a process.
              The start method can be ``spawn`` , ``fork`` , ``forkserver`` .
              Because the CUDA runtime does not support the ``fork`` start method,
              when use CUDA in subprocesses, we should start process by ``spawn``
              or ``forkserver`` method. Default: "spawn" ;
              (2) gpus (string): The training process will run on the
              selected gpus, such as "0,1,2,3". Default: None;
              (3) xpus (string): The training process will run on the
              selected xpus, such as "0,1,2,3". Default: None;
              (4) ips (string): Paddle cluster nodes ips, such as
              "192.168.0.16,192.168.0.17". Default: "127.0.0.1" .
  
      Returns:
          ``MultiprocessContext`` object, it hold the spawned processes.
  
      Examples:
          .. code-block:: python
  
              from __future__ import print_function
  
              import paddle
              import paddle.nn as nn
              import paddle.optimizer as opt
              import paddle.distributed as dist
  
              class LinearNet(nn.Layer):
                  def __init__(self):
                      super(LinearNet, self).__init__()
                      self._linear1 = nn.Linear(10, 10)
                      self._linear2 = nn.Linear(10, 1)
  
                  def forward(self, x):
                      return self._linear2(self._linear1(x))
  
              def train(print_result=False):
                  # 1. initialize parallel environment
                  dist.init_parallel_env()
  
                  # 2. create data parallel layer & optimizer
                  layer = LinearNet()
                  dp_layer = paddle.DataParallel(layer)
  
                  loss_fn = nn.MSELoss()
                  adam = opt.Adam(
                      learning_rate=0.001, parameters=dp_layer.parameters())
  
                  # 3. run layer
                  inputs = paddle.randn([10, 10], 'float32')
                  outputs = dp_layer(inputs)
                  labels = paddle.randn([10, 1], 'float32')
                  loss = loss_fn(outputs, labels)
  
                  if print_result is True:
                      print("loss:", loss.numpy())
  
                  loss.backward()
  
                  adam.step()
                  adam.clear_grad()
  
              # Usage 1: only pass function.
              # If your training method no need any argument, and
              # use all visible devices for parallel training.
              if __name__ == '__main__':
                  dist.spawn(train)
  
              # Usage 2: pass function and arguments.
              # If your training method need some arguments, and
              # use all visible devices for parallel training.
              if __name__ == '__main__':
                  dist.spawn(train, args=(True,))
  
              # Usage 3: pass function, arguments and nprocs.
              # If your training method need some arguments, and
              # only use part of visible devices for parallel training.
              # If your machine hold 8 cards {0,1,2,3,4,5,6,7},
              # this case will use cards {0,1}; If you set
              # CUDA_VISIBLE_DEVICES=4,5,6,7, this case will use
              # cards {4,5}
              if __name__ == '__main__':
                  dist.spawn(train, args=(True,), nprocs=2)
  
              # Usage 4: pass function, arguments, nprocs and gpus.
              # If your training method need some arguments, and
              # only use part of visible devices for parallel training,
              # but you can't set your machine's environment variable
              # CUDA_VISIBLE_DEVICES, such as it is None or all cards
              # {0,1,2,3,4,5,6,7}, you can pass `gpus` to
              # select the GPU cards you want to use. For example,
              # this case will use cards {4,5} if your machine hold 8 cards.
              if __name__ == '__main__':
                  dist.spawn(train, args=(True,), nprocs=2, gpus='4,5')
docstring_summary: Start multiple processes with ``spawn`` method for parallel training.
docstring_tokens: Start multiple processes with spawn method for parallel training .
function: def spawn(func, args=(), nprocs=-1, join=True, daemon=False, **options):
      """
      Start multiple processes with ``spawn`` method for parallel training.
  
      .. note::
          ``spawn`` now only supports GPU or XPU collective mode. The collective mode
          of GPU and XPU cannot be started at the same time, so the option `gpus` and
          `xpus` cannot be configured at the same time.
  
      Args:
          func (function): The target function is called by spawned process.
              This function need to be able to pickled, so it must be defined
              at the top level of a module.
          args (list|tuple, optional): Arguments passed to ``func``.
          nprocs (int, optional): Number of processed to start. Default: -1.
              when nprocs is -1, the available device will be obtained from
              the environment variable when the model is executed: If use GPU,
              the currently available device ID is obtained from the environment
              variable CUDA_VISIBLE_DEVICES; If use XPU, the currently available
              device ID is obtained from the environment variable XPU_VISIBLE_DEVICES.
          join (bool, optional): Perform a blocking join on all spawned processes.
              Default: True.
          daemon (bool, optional): The spawned processes' daemon flag. Default: False.
          **options(dict, optional): Other initial parallel execution environment
              configuration options. The following options are currently supported:
              (1) start_method (string): the way to start a process.
              The start method can be ``spawn`` , ``fork`` , ``forkserver`` .
              Because the CUDA runtime does not support the ``fork`` start method,
              when use CUDA in subprocesses, we should start process by ``spawn``
              or ``forkserver`` method. Default: "spawn" ;
              (2) gpus (string): The training process will run on the
              selected gpus, such as "0,1,2,3". Default: None;
              (3) xpus (string): The training process will run on the
              selected xpus, such as "0,1,2,3". Default: None;
              (4) ips (string): Paddle cluster nodes ips, such as
              "192.168.0.16,192.168.0.17". Default: "127.0.0.1" .
  
      Returns:
          ``MultiprocessContext`` object, it hold the spawned processes.
  
      Examples:
          .. code-block:: python
  
              from __future__ import print_function
  
              import paddle
              import paddle.nn as nn
              import paddle.optimizer as opt
              import paddle.distributed as dist
  
              class LinearNet(nn.Layer):
                  def __init__(self):
                      super(LinearNet, self).__init__()
                      self._linear1 = nn.Linear(10, 10)
                      self._linear2 = nn.Linear(10, 1)
  
                  def forward(self, x):
                      return self._linear2(self._linear1(x))
  
              def train(print_result=False):
                  # 1. initialize parallel environment
                  dist.init_parallel_env()
  
                  # 2. create data parallel layer & optimizer
                  layer = LinearNet()
                  dp_layer = paddle.DataParallel(layer)
  
                  loss_fn = nn.MSELoss()
                  adam = opt.Adam(
                      learning_rate=0.001, parameters=dp_layer.parameters())
  
                  # 3. run layer
                  inputs = paddle.randn([10, 10], 'float32')
                  outputs = dp_layer(inputs)
                  labels = paddle.randn([10, 1], 'float32')
                  loss = loss_fn(outputs, labels)
  
                  if print_result is True:
                      print("loss:", loss.numpy())
  
                  loss.backward()
  
                  adam.step()
                  adam.clear_grad()
  
              # Usage 1: only pass function.
              # If your training method no need any argument, and
              # use all visible devices for parallel training.
              if __name__ == '__main__':
                  dist.spawn(train)
  
              # Usage 2: pass function and arguments.
              # If your training method need some arguments, and
              # use all visible devices for parallel training.
              if __name__ == '__main__':
                  dist.spawn(train, args=(True,))
  
              # Usage 3: pass function, arguments and nprocs.
              # If your training method need some arguments, and
              # only use part of visible devices for parallel training.
              # If your machine hold 8 cards {0,1,2,3,4,5,6,7},
              # this case will use cards {0,1}; If you set
              # CUDA_VISIBLE_DEVICES=4,5,6,7, this case will use
              # cards {4,5}
              if __name__ == '__main__':
                  dist.spawn(train, args=(True,), nprocs=2)
  
              # Usage 4: pass function, arguments, nprocs and gpus.
              # If your training method need some arguments, and
              # only use part of visible devices for parallel training,
              # but you can't set your machine's environment variable
              # CUDA_VISIBLE_DEVICES, such as it is None or all cards
              # {0,1,2,3,4,5,6,7}, you can pass `gpus` to
              # select the GPU cards you want to use. For example,
              # this case will use cards {4,5} if your machine hold 8 cards.
              if __name__ == '__main__':
                  dist.spawn(train, args=(True,), nprocs=2, gpus='4,5')
      """
      # NOTE(chenweihang): [ why only supports python3.4+ ? ]
      # Python supported setting the child process startup method
      # since 3.4. The previous version can only use the default startup 
      # method, while the default startup method of Unix is fork, which 
      # cannot support CUDA runtime multi-process
      _py_supported_check()
  
      # Give an error hint when the users enter a configuration option 
      # that does not exist
      _options_valid_check(options)
  
      # get default nprocs
      if nprocs == -1:
          nprocs = _get_default_nprocs()
  
      # NOTE(chenweihang): [ why need get cluster info before run? ]
      # when using `paddle.distributed.spawn` start parallel training, 
      # we should get cluster info before starting subprocess, and pass 
      # correct info to each subprocess
      procs_env_list = _get_subprocess_env_list(nprocs, options)
  
      # start processes
      # NOTE(chenweihang): [ why default start method is spawn? ]
      # The CUDA runtime does not support the fork start method, 
      # either the spawn or forkserver start method are required 
      # to use CUDA in subprocesses.
      start_method = options.get('start_method', None)
      if start_method is None:
          start_method = 'spawn'
      mp = multiprocessing.get_context(start_method)
  
      error_queues = []
      return_queues = []
      processes = []
      for i in range(nprocs):
          error_queue = mp.SimpleQueue()
          return_queue = mp.SimpleQueue()
          process = mp.Process(
              target=_func_wrapper,
              args=(func, args, error_queue, return_queue, procs_env_list[i],
                    options['backend']))
          process.daemon = daemon
          process.start()
          error_queues.append(error_queue)
          return_queues.append(return_queue)
          processes.append(process)
  
      context = MultiprocessContext(processes, error_queues, return_queues)
      if not join:
          return context
  
      # loop until all process end
      while not context.join():
          pass
  
      # finally return context
      return context
function_tokens: def spawn ( func , args = ( ) , nprocs = - 1 , join = True , daemon = False , * * options ) : # NOTE(chenweihang): [ why only supports python3.4+ ? ] # Python supported setting the child process startup method # since 3.4. The previous version can only use the default startup  # method, while the default startup method of Unix is fork, which  # cannot support CUDA runtime multi-process _py_supported_check ( ) # Give an error hint when the users enter a configuration option  # that does not exist _options_valid_check ( options ) # get default nprocs if nprocs == - 1 : nprocs = _get_default_nprocs ( ) # NOTE(chenweihang): [ why need get cluster info before run? ] # when using `paddle.distributed.spawn` start parallel training,  # we should get cluster info before starting subprocess, and pass  # correct info to each subprocess procs_env_list = _get_subprocess_env_list ( nprocs , options ) # start processes # NOTE(chenweihang): [ why default start method is spawn? ] # The CUDA runtime does not support the fork start method,  # either the spawn or forkserver start method are required  # to use CUDA in subprocesses. start_method = options . get ( 'start_method' , None ) if start_method is None : start_method = 'spawn' mp = multiprocessing . get_context ( start_method ) error_queues = [ ] return_queues = [ ] processes = [ ] for i in range ( nprocs ) : error_queue = mp . SimpleQueue ( ) return_queue = mp . SimpleQueue ( ) process = mp . Process ( target = _func_wrapper , args = ( func , args , error_queue , return_queue , procs_env_list [ i ] , options [ 'backend' ] ) ) process . daemon = daemon process . start ( ) error_queues . append ( error_queue ) return_queues . append ( return_queue ) processes . append ( process ) context = MultiprocessContext ( processes , error_queues , return_queues ) if not join : return context # loop until all process end while not context . join ( ) : pass # finally return context return context