[docs]defrun(model:lightning.pytorch.LightningModule,datamodule:lightning.pytorch.LightningDataModule,device_manager:DeviceManager,)->list[Prediction]|list[list[Prediction]]|PredictionSplit|None:"""Run inference on input data, output predictions. Parameters ---------- model Neural network model (e.g. pasa). datamodule The lightning DataModule to use for training **and** validation. device_manager An internal device representation, to be used for training and validation. This representation can be converted into a pytorch device or a lightning accelerator setup. Returns ------- Depending on the return type of the DataModule's ``predict_dataloader()`` method: * if :py:class:`torch.utils.data.DataLoader`, then returns a :py:class:`list` of predictions. * if :py:class:`list` of :py:class:`torch.utils.data.DataLoader`, then returns a list of lists of predictions, each list corresponding to the iteration over one of the dataloaders. * if :py:class:`dict` of :py:class:`str` to :py:class:`torch.utils.data.DataLoader`, then returns a dictionary mapping names to lists of predictions. * if ``None``, then returns ``None``. Raises ------ TypeError If the DataModule's ``predict_dataloader()`` method does not return any of the types described above. """fromlightning.pytorch.loggers.loggerimportDummyLoggercollector=_JSONMetadataCollector()accelerator,devices=device_manager.lightning_accelerator()trainer=lightning.pytorch.Trainer(accelerator=accelerator,devices=devices,logger=DummyLogger(),callbacks=[collector],)dataloaders=datamodule.predict_dataloader()ifisinstance(dataloaders,torch.utils.data.DataLoader):logger.info("Running prediction on a single dataloader...")trainer.predict(model,dataloaders,return_predictions=False)returncollector.reset()ifisinstance(dataloaders,list):retval_list=[]fork,dataloaderinenumerate(dataloaders):logger.info(f"Running prediction on split `{k}`...")trainer.predict(model,dataloader,return_predictions=False)retval_list.append(collector.reset())returnretval_list# type: ignoreifisinstance(dataloaders,dict):retval_dict={}forname,dataloaderindataloaders.items():logger.info(f"Running prediction on `{name}` split...")trainer.predict(model,dataloader,return_predictions=False)retval_dict[name]=collector.reset()returnretval_dict# type: ignoreifdataloadersisNone:logger.warning("Datamodule did not return any prediction dataloaders!")returnNone# if you get to this point, then the user is returning something that is# not supported - complain!raiseTypeError(f"Datamodule returned strangely typed prediction "f"dataloaders: `{type(dataloaders)}` - Please write code "f"to support this use-case.",)