# Copyright The Lightning AI team.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.importinspectimportosfromcontextlibimportcontextmanager,nullcontextfromfunctoolsimportpartialfrompathlibimportPathfromtypingimportAny,Callable,cast,Dict,Generator,List,Mapping,Optional,overload,Sequence,Tuple,Unionimporttorchimporttorch.nnasnnfromlightning_utilities.core.apply_funcimportapply_to_collectionfromlightning_utilities.core.overridesimportis_overriddenfromlightning_utilities.core.rank_zeroimportrank_zero_warnfromtorchimportTensorfromtorch.optimimportOptimizerfromtorch.utils.dataimportBatchSampler,DataLoader,DistributedSampler,RandomSampler,SequentialSamplerfromlightning_fabric.loggersimportLoggerfromlightning_fabric.pluginsimportPrecision# avoid circular imports: # isort: splitfromlightning_fabric.accelerators.acceleratorimportAcceleratorfromlightning_fabric.connectorimport_Connector,_PLUGIN_INPUT,_PRECISION_INPUTfromlightning_fabric.strategiesimportDeepSpeedStrategy,FSDPStrategy,SingleDeviceStrategy,Strategy,XLAStrategyfromlightning_fabric.strategies.strategyimport_Sharded,TBroadcastfromlightning_fabric.utilitiesimportmove_data_to_devicefromlightning_fabric.utilities.apply_funcimportconvert_tensors_to_scalars,convert_to_tensorsfromlightning_fabric.utilities.dataimport(_auto_add_worker_init_fn,_replace_dunder_methods,_update_dataloader,has_iterable_dataset,)fromlightning_fabric.utilities.distributedimportDistributedSamplerWrapperfromlightning_fabric.utilities.seedimportseed_everythingfromlightning_fabric.utilities.warningsimportPossibleUserWarningfromlightning_fabric.wrappersimport_FabricDataLoader,_FabricModule,_FabricOptimizer
[docs]classFabric:"""Fabric accelerates your PyTorch training or inference code with minimal changes required. - Automatic placement of models and data onto the device. - Automatic support for mixed and double precision (smaller memory footprint). - Seamless switching between hardware (CPU, GPU, TPU) and distributed training strategies (data-parallel training, sharded training, etc.). - Automated spawning of processes, no launch utilities required. - Multi-node support. Args: accelerator: The hardware to run on. Possible choices are: ``"cpu"``, ``"cuda"``, ``"mps"``, ``"gpu"``, ``"tpu"``, ``"auto"``. strategy: Strategy for how to run across multiple devices. Possible choices are: ``"dp"``, ``"ddp"``, ``"ddp_spawn"``, ``"deepspeed"``, ``"fsdp"``. devices: Number of devices to train on (``int``), which GPUs to train on (``list`` or ``str``), or ``"auto"``. The value applies per node. num_nodes: Number of GPU nodes for distributed training. precision: Double precision (``64``), full precision (``32``), half precision (``16``), or bfloat16 precision (``"bf16"``). plugins: One or several custom plugins callbacks: A single callback or a list of callbacks. A callback can contain any arbitrary methods that can be invoked through :meth:`~lightning_fabric.fabric.Fabric.call` by the user. loggers: A single logger or a list of loggers. See :meth:`~lightning_fabric.fabric.Fabric.log` for more information. """def__init__(self,accelerator:Optional[Union[str,Accelerator]]=None,strategy:Optional[Union[str,Strategy]]=None,devices:Optional[Union[List[int],str,int]]=None,num_nodes:int=1,precision:_PRECISION_INPUT=32,plugins:Optional[Union[_PLUGIN_INPUT,List[_PLUGIN_INPUT]]]=None,callbacks:Optional[Union[List[Any],Any]]=None,loggers:Optional[Union[Logger,List[Logger]]]=None,)->None:self._connector=_Connector(accelerator=accelerator,strategy=strategy,devices=devices,num_nodes=num_nodes,precision=precision,plugins=plugins,)self._strategy:Strategy=self._connector.strategyself._accelerator:Accelerator=self._connector.acceleratorself._precision:Precision=self._strategy.precisioncallbacks=callbacksifcallbacksisnotNoneelse[]self._callbacks=callbacksifisinstance(callbacks,list)else[callbacks]loggers=loggersifloggersisnotNoneelse[]self._loggers=loggersifisinstance(loggers,list)else[loggers]self._models_setup:int=0self._prepare_run_method()if_is_using_cli():# when the CLI is used to launch the script, we need to set up the environment (init processes) here so# that the user can immediately use all functionality in strategiesself._strategy.setup_environment()@propertydefaccelerator(self)->Accelerator:returnself._accelerator@propertydefstrategy(self)->Strategy:returnself._strategy@propertydefdevice(self)->torch.device:"""The current device this process runs on. Use this to create tensors directly on the device if needed. """returnself._strategy.root_device@propertydefglobal_rank(self)->int:"""The global index of the current process across all devices and nodes."""returngetattr(self._strategy,"global_rank",0)@propertydeflocal_rank(self)->int:"""The index of the current process among the processes running on the local node."""returngetattr(self._strategy,"local_rank",0)@propertydefnode_rank(self)->int:"""The index of the current node."""returngetattr(self._strategy,"node_rank",0)@propertydefworld_size(self)->int:"""The total number of processes running across all devices and nodes."""returngetattr(self._strategy,"world_size",1)@propertydefis_global_zero(self)->bool:"""Whether this rank is rank zero."""returnself._strategy.is_global_zero@propertydefloggers(self)->List[Logger]:"""Returns all loggers passed to Fabric."""returnself._loggers@propertydeflogger(self)->Logger:"""Returns the first logger in the list passed to Fabric, which is considered the main logger."""returnself._loggers[0]
[docs]defrun(self,*args:Any,**kwargs:Any)->Any:"""All the code inside this run method gets accelerated by Fabric. You can pass arbitrary arguments to this function when overriding it. """
[docs]defsetup(self,module:nn.Module,*optimizers:Optimizer,move_to_device:bool=True,)->Any:# no specific return because the way we want our API to look does not play well with mypy"""Set up a model and its optimizers for accelerated training. Args: module: A :class:`torch.nn.Module` to set up *optimizers: The optimizer(s) to set up (no optimizers is also possible) move_to_device: If set ``True`` (default), moves the model to the correct device. Set this to ``False`` and alternatively use :meth:`to_device` manually. Returns: The tuple containing wrapped module and the optimizers, in the same order they were passed in. """self._validate_setup(module,optimizers)original_module=modulemodule=self._precision.convert_module(module)ifmove_to_device:module=self._move_model_to_device(model=module,optimizers=list(optimizers))# Let accelerator/plugin wrap and connect the models and optimizersifoptimizers:module,optimizers=self._strategy.setup_module_and_optimizers(# type: ignore[assignment]module,list(optimizers))else:module=self._strategy.setup_module(module)module=_FabricModule(module,self._precision,original_module=original_module)# Update the _DeviceDtypeModuleMixin's device parametermodule.to(self.deviceifmove_to_deviceelsenext(module.parameters()).device)optimizers=[_FabricOptimizer(optimizer=optimizer,strategy=self._strategy)foroptimizerinoptimizers]self._models_setup+=1ifhasattr(original_module,"_fabric"):# this is probably a LightningModuleoriginal_module._fabric=self# type: ignore[assignment]original_module._fabric_optimizers=optimizers# type: ignore[assignment]ifoptimizers:# join both types in a tuple for API conveniencereturntuple((module,*optimizers))returnmodule
[docs]defsetup_module(self,module:nn.Module,move_to_device:bool=True)->_FabricModule:"""Set up a model for accelerated training or inference. This is the same as calling ``.setup(model)`` with no optimizers. It is useful for inference or for certain strategies like `FSDP` that require setting up the module before the optimizer can be created and set up. See also :meth:`setup_optimizers`. Args: module: A :class:`torch.nn.Module` to set up move_to_device: If set ``True`` (default), moves the model to the correct device. Set this to ``False`` and alternatively use :meth:`to_device` manually. Returns: The wrapped model. """self._validate_setup_module(module)original_module=modulemodule=self._precision.convert_module(module)ifmove_to_device:module=self._move_model_to_device(model=module,optimizers=[])# Let strategy wrap and connect the module alonemodule=self._strategy.setup_module(module)module=_FabricModule(module,self._precision,original_module=original_module)ifnotisinstance(self._strategy,FSDPStrategy):# Update the _DeviceDtypeModuleMixin's device parametermodule.to(self.deviceifmove_to_deviceelsenext(module.parameters()).device)ifhasattr(original_module,"_fabric"):# this is probably a LightningModuleoriginal_module._fabric=self# type: ignore[assignment]self._models_setup+=1returnmodule
[docs]defsetup_optimizers(self,*optimizers:Optimizer)->Union[_FabricOptimizer,Tuple[_FabricOptimizer,...]]:"""Set up one or more optimizers for accelerated training. Some strategies do not allow setting up model and optimizer independently. For them, you should call ``.setup(model, optimizer, ...)`` instead to jointly set them up. Args: *optimizers: One or more optmizers to set up. Returns: The wrapped optimizer(s). """self._validate_setup_optimizers(optimizers)optimizers=[self._strategy.setup_optimizer(optimizer)foroptimizerinoptimizers]optimizers=[_FabricOptimizer(optimizer=optimizer,strategy=self._strategy)foroptimizerinoptimizers]returnoptimizers[0]iflen(optimizers)==1elsetuple(optimizers)
[docs]defsetup_dataloaders(self,*dataloaders:DataLoader,replace_sampler:bool=True,move_to_device:bool=True)->Union[DataLoader,List[DataLoader]]:"""Set up one or multiple dataloaders for accelerated training. If you need different settings for each dataloader, call this method individually for each one. Args: *dataloaders: A single dataloader or a sequence of dataloaders. replace_sampler: If set ``True`` (default), automatically wraps or replaces the sampler on the dataloader(s) for distributed training. If you have a custom sampler defined, set this to this argument to ``False``. move_to_device: If set ``True`` (default), moves the data returned by the dataloader(s) automatically to the correct device. Set this to ``False`` and alternatively use :meth:`to_device` manually on the returned data. Returns: The wrapped dataloaders, in the same order they were passed in. """self._validate_setup_dataloaders(dataloaders)dataloaders=[self._setup_dataloader(dataloader,replace_sampler=replace_sampler,move_to_device=move_to_device)fordataloaderindataloaders]dataloaders=dataloaders[0]iflen(dataloaders)==1elsedataloadersreturndataloaders# type: ignore[return-value]
def_setup_dataloader(self,dataloader:DataLoader,replace_sampler:bool=True,move_to_device:bool=True)->DataLoader:"""Set up a single dataloader for accelerated training. Args: dataloader: The dataloader to accelerate. replace_sampler: If set ``True`` (default), automatically wraps or replaces the sampler on the dataloader for distributed training. If you have a custom sampler defined, set this to this argument to ``False``. move_to_device: If set ``True`` (default), moves the data returned by the dataloader automatically to the correct device. Set this to ``False`` and alternatively use :meth:`to_device` manually on the returned data. Returns: The wrapped dataloader. """sampler=dataloader.samplerifreplace_samplerandself._requires_distributed_sampler(dataloader):sampler=self._get_distributed_sampler(dataloader,**self._strategy.distributed_sampler_kwargs)# the dataloader needs to be re-instantiated because we want to update the input arguments (e.g., sampler)dataloader=_update_dataloader(dataloader,sampler)# add worker_init_fn for correct seeding in worker processes_auto_add_worker_init_fn(dataloader,self.global_rank)dataloader=self._strategy.process_dataloader(dataloader)device=self.deviceifmove_to_deviceandnotisinstance(self._strategy,XLAStrategy)elseNonelite_dataloader=_FabricDataLoader(dataloader=dataloader,device=device)lite_dataloader=cast(DataLoader,lite_dataloader)returnlite_dataloader
[docs]defbackward(self,tensor:Tensor,*args:Any,model:Optional[_FabricModule]=None,**kwargs:Any)->None:"""Replaces ``loss.backward()`` in your training loop. Handles precision and automatically for you. Args: tensor: The tensor (loss) to back-propagate gradients from. *args: Optional positional arguments passed to the underlying backward function. model: Optional model instance for plugins that require the model for backward(). **kwargs: Optional named keyword arguments passed to the underlying backward function. Note: When using ``strategy="deepspeed"`` and multiple models were set up, it is required to pass in the model as argument here. """module=model._forward_moduleifmodelisnotNoneelsemodelifisinstance(self._strategy,DeepSpeedStrategy):ifmodelisNone:ifself._models_setup==0:raiseRuntimeError("No models were set up for backward. Did you forget to call `self.setup()`?")ifself._models_setup>1:raiseValueError("When using multiple models + deepspeed, please provide the model used to perform"" the optimization: `self.backward(loss, model=model)`")module=self._strategy.modelelse:# requires to attach the current `DeepSpeedEngine` for the `_FabricOptimizer.step` call.self._strategy._deepspeed_engine=moduleself._precision.backward(tensor,module,*args,**kwargs)
[docs]@contextmanagerdefautocast(self)->Generator[None,None,None]:"""A context manager to automatically convert operations for the chosen precision. Use this only if the `forward` method of your model does not cover all operations you wish to run with the chosen precision setting. """withself._precision.forward_context():yield
[docs]defto_device(self,obj:Union[nn.Module,Tensor,Any])->Union[nn.Module,Tensor,Any]:"""Move a :class:`torch.nn.Module` or a collection of tensors to the current device, if it is not already on that device. Args: obj: An object to move to the device. Can be an instance of :class:`torch.nn.Module`, a tensor, or a (nested) collection of tensors (e.g., a dictionary). Returns: A reference to the object that was moved to the new device. """ifisinstance(obj,nn.Module):self._accelerator.setup_device(self.device)self._strategy.module_to_device(obj)returnobjreturnmove_data_to_device(obj,device=self.device)
[docs]defprint(self,*args:Any,**kwargs:Any)->None:"""Print something only on the first process. Arguments passed to this method are forwarded to the Python built-in :func:`print` function. """ifself.local_rank==0:print(*args,**kwargs)
[docs]defbarrier(self,name:Optional[str]=None)->None:"""Wait for all processes to enter this call. Use this to synchronize all parallel processes, but only if necessary, otherwise the overhead of synchronization will cause your program to slow down. Example:: if self.global_rank == 0: # let process 0 download the dataset dataset.download_files() # let all processes wait before reading the dataset self.barrier() # now all processes can read the files and start training """self._strategy.barrier(name=name)
[docs]defall_gather(self,data:Union[Tensor,Dict,List,Tuple],group:Optional[Any]=None,sync_grads:bool=False)->Union[Tensor,Dict,List,Tuple]:r"""Gather tensors or collections of tensors from multiple processes. Args: data: int, float, tensor of shape (batch, ...), or a (possibly nested) collection thereof. group: the process group to gather results from. Defaults to all processes (world) sync_grads: flag that allows users to synchronize gradients for the all_gather operation Return: A tensor of shape (world_size, batch, ...), or if the input was a collection the output will also be a collection with tensors of this shape. """group=groupifgroupisnotNoneelsetorch.distributed.group.WORLDdata=convert_to_tensors(data,device=self.device)returnapply_to_collection(data,Tensor,self._strategy.all_gather,group=group,sync_grads=sync_grads)
[docs]@contextmanagerdefno_backward_sync(self,module:_FabricModule,enabled:bool=True)->Generator:"""Skip gradient synchronization during backward to avoid redundant communication overhead. Use this context manager when performing gradient accumulation to speed up training with multiple devices. Example:: # Accumulate gradient 8 batches at a time with self.no_backward_sync(model, enabled=(batch_idx % 8 != 0)): output = model(input) loss = ... self.backward(loss) ... For those strategies that don't support it, a warning is emitted. For single-device strategies, it is a no-op. Both the model's `.forward()` and the `self.backward()` call need to run under this context. Args: module: The module for which to control the gradient synchronization. enabled: Whether the context manager is enabled or not. ``True`` means skip the sync, ``False`` means do not skip. """ifnotisinstance(module,_FabricModule):raiseTypeError("You need to set up the model first before you can call `self.no_backward_sync()`:"" `model = self.setup(model, ...)`")ifnotenabledorisinstance(self._strategy,SingleDeviceStrategy):context=nullcontext()elifself._strategy._backward_sync_controlisNone:rank_zero_warn(f"The `{self._strategy.__class__.__name__}` does not support skipping the gradient synchronization."f" Remove `.no_backward_sync()` from your code or choose a different strategy.",category=PossibleUserWarning,)context=nullcontext()else:context=self._strategy._backward_sync_control.no_backward_sync(# type: ignore[assignment]module._forward_module)withcontext:yield
[docs]@contextmanagerdefsharded_model(self)->Generator:"""Shard the parameters of the model instantly when instantiating the layers. Use this context manager with strategies that support sharding the model parameters to save peak memory usage. Example:: with self.sharded_model(): model = MyModel() The context manager is strategy-agnostic and for the ones that don't do sharding, it is a no-op. """ifisinstance(self._strategy,_Sharded):withself._strategy.module_sharded_context():yieldelse:yield
[docs]defsave(self,content:Dict[str,Any],filepath:Union[str,Path])->None:"""Save checkpoint contents to a file. How and which processes save gets determined by the `strategy`. For example, the `ddp` strategy saves checkpoints only on process 0. Args: content: A dictionary with contents, i.e., the state dict of your model filepath: A path to where the file should be saved """self._strategy.save_checkpoint(content,filepath)
[docs]defload(self,filepath:Union[str,Path])->Any:"""Load a checkpoint from a file. How and which processes load gets determined by the `strategy` Args: filepath: A path to where the file is located """returnself._strategy.load_checkpoint(filepath)
deflaunch(self,function:Optional[Callable[["Fabric"],Any]]=None,*args:Any,**kwargs:Any)->Any:if_is_using_cli():raiseRuntimeError("This script was launched through the CLI, and processes have already been created. Calling "" `.launch()` again is not allowed.")iffunctionisnotNoneandnotinspect.signature(function).parameters:raiseTypeError("The function passed to `Fabric.launch()` needs to take at least one argument. The launcher will pass"" in the `Fabric` object so you can use it inside the function.")function=partial(self._run_with_setup,functionor_do_nothing)args=[self,*args]ifself._strategy.launcherisnotNone:returnself._strategy.launcher.launch(function,*args,**kwargs)returnfunction(*args,**kwargs)
[docs]defcall(self,hook_name:str,*args:Any,**kwargs:Any)->None:"""Trigger the callback methods with the given name and arguments. Not all objects registered via ``Fabric(callbacks=...)`` must implement a method with the given name. The ones that have a matching method name will get called. Args: hook_name: The name of the callback method. *args: Optional positional arguments that get passed down to the callback method. **kwargs: Optional keyword arguments that get passed down to the callback method. Example:: class MyCallback: def on_train_epoch_end(self, results): ... fabric = Fabric(callbacks=[MyCallback()]) fabric.call("on_train_epoch_end", results={...}) """forcallbackinself._callbacks:method=getattr(callback,hook_name,None)ifmethodisNone:continueifnotcallable(method):rank_zero_warn(f"Skipping the callback `{type(callback).__name__}.{hook_name}` because it is not callable.")continuemethod(*args,**kwargs)
[docs]deflog(self,name:str,value:Any,step:Optional[int]=None)->None:"""Log a scalar to all loggers that were added to Fabric. Args: name: The name of the metric to log. value: The metric value to collect. If the value is a :class:`torch.Tensor`, it gets detached from the graph automatically. step: Optional step number. Most Logger implementations auto-increment the step value by one with every log call. You can specify your own value here. """self.log_dict(metrics={name:value},step=step)
[docs]deflog_dict(self,metrics:Mapping[str,Any],step:Optional[int]=None)->None:"""Log multiple scalars at once to all loggers that were added to Fabric. Args: metrics: A dictionary where the key is the name of the metric and the value the scalar to be logged. Any :class:`torch.Tensor` in the dictionary get detached from the graph automatically. step: Optional step number. Most Logger implementations auto-increment this value by one with every log call. You can specify your own value here. """metrics=convert_tensors_to_scalars(metrics)forloggerinself._loggers:logger.log_metrics(metrics=metrics,step=step)
[docs]@staticmethoddefseed_everything(seed:Optional[int]=None,workers:Optional[bool]=None)->int:"""Helper function to seed everything without explicitly importing Lightning. See :func:`pytorch_lightning.seed_everything` for more details. """ifworkersisNone:# Lightning sets `workers=False` by default to avoid breaking reproducibility, but since this is a new# release, we can afford to do it.workers=Truereturnseed_everything(seed=seed,workers=workers)
def_run_impl(self,run_method:Callable,*args:Any,**kwargs:Any)->Any:run_method=partial(self._run_with_setup,run_method)ifself._strategy.launcherisnotNone:returnself._strategy.launcher.launch(run_method,*args,**kwargs)else:returnrun_method(*args,**kwargs)def_run_with_setup(self,run_function:Callable,*args:Any,**kwargs:Any)->Any:self._strategy.setup_environment()# apply sharded context to prevent OOMwithself.sharded_model(),_replace_dunder_methods(DataLoader,"dataset"),_replace_dunder_methods(BatchSampler):returnrun_function(*args,**kwargs)def_move_model_to_device(self,model:nn.Module,optimizers:List[Optimizer])->nn.Module:initial_device=next(model.parameters()).deviceifany(param.device!=initial_deviceforparaminmodel.parameters()):rank_zero_warn("The model passed to `Fabric.setup()` has parameters on different devices. Since `move_to_device=True`,"" all parameters will be moved to the new device. If this is not desired, set "" `Fabric.setup(..., move_to_device=False)`.",category=PossibleUserWarning,)ifisinstance(self._strategy,XLAStrategy):# When the user creates the optimizer, they reference the parameters on the CPU.# However, when running with TPU the parameters get copied and the reference in the optimizer# remains invalid. We need to update the references to point to the parameter tensors on the device.params_before_move=dict(model.named_parameters())model=self.to_device(model)# XLA makes a copy on the parameters, so the device is not the same before and after to_device.params_on_device=dict(model.named_parameters())mapping={param:params_on_device[name]forname,paraminparams_before_move.items()}foroptimizerinoptimizers:forparam_groupinoptimizer.param_groups:param_group["params"]=[mapping.get(p,p)forpinparam_group["params"]]else:model=self.to_device(model)returnmodeldef_requires_distributed_sampler(self,dataloader:DataLoader)->bool:return(getattr(self.strategy,"distributed_sampler_kwargs",None)isnotNoneandnotisinstance(dataloader.sampler,DistributedSampler)andnothas_iterable_dataset(dataloader))@staticmethoddef_get_distributed_sampler(dataloader:DataLoader,**kwargs:Any)->DistributedSampler:kwargs.setdefault("shuffle",isinstance(dataloader.sampler,RandomSampler))kwargs.setdefault("seed",int(os.getenv("PL_GLOBAL_SEED",0)))ifisinstance(dataloader.sampler,(RandomSampler,SequentialSampler)):returnDistributedSampler(dataloader.dataset,**kwargs)returnDistributedSamplerWrapper(dataloader.sampler,**kwargs)def_prepare_run_method(self)->None:ifis_overridden("run",self,Fabric)and_is_using_cli():raiseTypeError("Overriding `Fabric.run()` and launching from the CLI is not allowed. Run the script normally,"" or change your code to directly call `fabric = Fabric(...); fabric.setup(...)` etc.")# wrap the run method, so we can inject setup logic or spawn processes for the usersetattr(self,"run",partial(self._run_impl,self.run))def_validate_setup(self,module:nn.Module,optimizers:Sequence[Optimizer])->None:ifisinstance(module,_FabricModule):raiseValueError("A model should be passed only once to the `setup` method.")ifany(isinstance(opt,_FabricOptimizer)foroptinoptimizers):raiseValueError("An optimizer should be passed only once to the `setup` method.")ifisinstance(self._strategy,FSDPStrategy):raiseRuntimeError(f"The `{type(self).__name__}` requires the model and optimizer(s) to be set up separately."" Create and set up the model first through `model = self.setup_model(model)`. Then create the"" optimizer and set it up: `optimizer = self.setup_optimizer(optimizer)`.")def_validate_setup_module(self,module:nn.Module)->None:ifisinstance(module,_FabricModule):raiseValueError("A model should be passed only once to the `setup_module` method.")def_validate_setup_optimizers(self,optimizers:Sequence[Optimizer])->None:ifisinstance(self._strategy,(DeepSpeedStrategy,XLAStrategy)):raiseRuntimeError(f"The `{type(self._strategy).__name__}` requires the model and optimizer(s) to be set up jointly"" through `.setup(model, optimizer, ...)`.")ifnotoptimizers:raiseValueError("`setup_optimizers` requires at least one optimizer as input.")ifany(isinstance(opt,_FabricOptimizer)foroptinoptimizers):raiseValueError("An optimizer should be passed only once to the `setup_optimizers` method.")@staticmethoddef_validate_setup_dataloaders(dataloaders:Sequence[DataLoader])->None:ifnotdataloaders:raiseValueError("`setup_dataloaders` requires at least one dataloader as input.")ifany(isinstance(dl,_FabricDataLoader)fordlindataloaders):raiseValueError("A dataloader should be passed only once to the `setup_dataloaders` method.")ifany(notisinstance(dl,DataLoader)fordlindataloaders):raiseTypeError("Only PyTorch DataLoader are currently supported in `setup_dataloaders`.")
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. Read PyTorch Lightning's Privacy Policy.