o
    zi!                     @   s   d dl Zd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZmZ d dlmZ dddZdddZddded
dfddZdddZdddZdddZdS )    N)PossibleUserWarning)	TrainerFn)MisconfigurationException)is_overridden)rank_zero_deprecationrank_zero_warnis_param_in_hook_signaturetrainer
pl.Trainerreturnc                 C   s   | j }| jjdu rtd| jjtjkrt| | t| | n&| jjtjkr,t	|d n| jjtj
kr9t	|d n| jjtjkrEt	|d t| t| dS )zChecks that the model is configured correctly before the run is started.

    Args:
        trainer: Lightning Trainer. Its `lightning_module` (the model) to check the configuration.

    NzNUnexpected: Trainer state fn must be set before validating loop configuration.valtestpredict)lightning_modulestatefn
ValueErrorr   FITTING%__verify_train_val_loop_configuration$__verify_manual_optimization_support
VALIDATING __verify_eval_loop_configurationTESTING
PREDICTING&__verify_configure_model_configuration"__warn_dataloader_iter_limitationsr
   model r   e/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/trainer/configuration_validator.py_verify_loop_configurations   s   

r!   r   pl.LightningModulec                 C   s   t d|}|stdt d|}|std| jjjj }t d|}|r+|s+td |r5|s5tdtd t	t
|d	d rHtd
t|j dt	t
|dd r[tdt|j dd S )Ntraining_stepzNo `training_step()` method defined. Lightning `Trainer` expects as minimum a `training_step()`, `train_dataloader()` and `configure_optimizers()` to be defined.configure_optimizerszNo `configure_optimizers()` method defined. Lightning `Trainer` expects as minimum a `training_step()`, `train_dataloader()` and `configure_optimizers()` to be defined.validation_stepzRYou passed in a `val_dataloader` but have no `validation_step`. Skipping val loop.zPYou defined a `validation_step` but have no `val_dataloader`. Skipping val loop.categorytraining_epoch_endz>Support for `training_epoch_end` has been removed in v2.0.0. `z` implements this method. You can use the `on_train_epoch_end` hook instead. To access outputs, save them in-memory as instance attributes. You can find migration examples in https://github.com/Lightning-AI/lightning/pull/16520.validation_epoch_endz@Support for `validation_epoch_end` has been removed in v2.0.0. `z` implements this method. You can use the `on_validation_epoch_end` hook instead. To access outputs, save them in-memory as instance attributes. You can find migration examples in https://github.com/Lightning-AI/lightning/pull/16520.)r   r   fit_loop
epoch_loopval_loop_data_source
is_definedr   r   callablegetattrNotImplementedErrortype__name__)r
   r   has_training_stephas_optimizershas_val_loaderhas_val_stepr   r   r    r   1   s8   


r   stagec                 C   s   |dkrdn| d}t || }|dkr,| jd u rtd|s(t d| s*tdd S d S |sA|dkr4dn|}td	| d
| d|dkrGdnd}tt| |d rbtd| dt| j d| dd S )Nr   r%   _stepr   z6`predict_step` cannot be None to run `Trainer.predict`forwardz3`Trainer.predict` requires `forward` method to run.validatezNo `z#()` method defined to run `Trainer.z`.r)   test_epoch_endzSupport for `z` has been removed in v2.0.0. `z.` implements this method. You can use the `on_z` hook instead. To access outputs, save them in-memory as instance attributes. You can find migration examples in https://github.com/Lightning-AI/lightning/pull/16520.)r   predict_stepr   r/   r0   r1   r2   r3   )r   r8   	step_namehas_steptrainer_methodepoch_end_namer   r   r    r   \   s&   

r   c                 C   sP   |j rd S | jd ur| jdkrtd| j d| jdkr&td| j dd S )Nr   zhAutomatic gradient clipping is not supported for manual optimization. Remove `Trainer(gradient_clip_val=z')` or switch to automatic optimization.   zrAutomatic gradient accumulation is not supported for manual optimization. Remove `Trainer(accumulate_grad_batches=)automatic_optimizationgradient_clip_valr   accumulate_grad_batchesr   r   r   r    r   w   s   
r   c                 C   s6   t dd | j| j| j| jfD rtdtd dS dS )z&Check if `dataloader_iter is enabled`.c                 s   s&    | ]}|d urt |dddV  qd S )Ndataloader_iterT)explicitr   ).0step_fnr   r   r    	<genexpr>   s    z5__warn_dataloader_iter_limitations.<locals>.<genexpr>a  You are using the `dataloader_iter` step flavor. If you consume the iterator more than once per step, the `batch_idx` argument in any hook that takes it will not match with the batch index of the last batch consumed. This might have unforeseen effects on callbacks or code that expects to get the correct index. This will also not work well with gradient accumulation. This feature is very experimental and subject to change. Here be dragons.r&   N)anyr#   r%   r=   	test_stepr   r   )r   r   r   r    r      s   
r   c                 C   sL   t d| r$t| j}t d| rtd| d| dtd| d d S d S )Nconfigure_sharded_modelconfigure_modelzBoth `z.configure_model`, and `zm.configure_sharded_model` are overridden. The latter is deprecated and it should be replaced with the former.zYou have overridden `z.configure_sharded_model` which is deprecated. Please override the `configure_model` hook instead. Instantiation with the newer hook will be created on the device right away and have the right data type depending on the precision setting in the Trainer.)r   r2   r3   RuntimeErrorr   )r   namer   r   r    r      s   



r   )r
   r   r   N)r
   r   r   r"   r   N)r   r"   r   N)pytorch_lightningpl#lightning_fabric.utilities.warningsr    pytorch_lightning.trainer.statesr   &pytorch_lightning.utilities.exceptionsr   )pytorch_lightning.utilities.model_helpersr   %pytorch_lightning.utilities.rank_zeror   r   +pytorch_lightning.utilities.signature_utilsr	   r!   r   strr   r   r   r   r   r   r   r    <module>   s   

+

