
    ^j                         d Z ddlZddlmZmZmZmZ ddlZddlm	Z	 ddl
mZ  ej                  e      Z G d de      Zy)zClassification training task.    N)CallableDictOptionalUnion   )TrainingTaskc                   N    e Zd ZdZ	 	 	 ddej
                  deej
                  ef   dee	j                     dee	j                     def
 fdZ	 ddee   d	d fd
Z	 	 ddedee   d	ej
                  fdZde	j$                  de	j$                  d	eee	j$                  f   fdZ xZS )ClassificationTaska?  Standard supervised classification task.

    Simple task that performs a forward pass through the model and computes
    the classification loss.

    Args:
        model: The model to train
        criterion: Loss function (e.g., CrossEntropyLoss)
        device: Device for task tensors/buffers
        dtype: Dtype for task tensors/buffers
        verbose: Enable info logging

    Example:
        >>> task = ClassificationTask(model, nn.CrossEntropyLoss(), device=torch.device('cuda'))
        >>> result = task(input, target)
        >>> result['loss'].backward()
    model	criteriondevicedtypeverbosec                     t         |   |||       || _        || _        | j                  r=t        |dd       xs t        |      j                  }t        j                  d|        y y )N)r   r   r   __name__zClassificationTask: criterion=)
super__init__trainable_moduler   r   getattrtyper   _loggerinfo)selfr   r   r   r   r   	loss_name	__class__s          c/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/timm/task/classification.pyr   zClassificationTask.__init__    se     	eWE %"<<	:t<XY@X@XILL9)EF     
device_idsreturnc                 F    ddl m}  || j                  fd|i|| _        | S )aK  Prepare task for distributed training.

        Wraps the model in DistributedDataParallel (DDP).

        Args:
            device_ids: List of device IDs for DDP (e.g., [local_rank])
            **ddp_kwargs: Additional arguments passed to DistributedDataParallel

        Returns:
            self (for method chaining)
        r   )DistributedDataParallelr   )torch.nn.parallelr!   r   )r   r   
ddp_kwargsDDPs       r   prepare_distributedz&ClassificationTask.prepare_distributed0   s+      	E #D$9$9 _j _T^ _r   backendmodec                     t        j                  | j                  f||d|| _        | j                  | _        | j                  S )z5Compile the classification model before DDP wrapping.)r&   r'   )torchcompiler   
eval_model)r   r&   r'   compile_kwargss       r   r*   zClassificationTask.compileD   sB     !&d.C.C rW[_ rcq r//$$$r   inputtargetc                 R    | j                  |      }| j                  ||      }||dS )a2  Forward pass through model and compute classification loss.

        Args:
            input: Input tensor [B, C, H, W]
            target: Target labels [B]

        Returns:
            Dictionary containing:
                - 'loss': Classification loss
                - 'output': Model logits
        )lossoutput)r   r   )r   r-   r.   r1   r0   s        r   forwardzClassificationTask.forwardP   s6      &&u-~~ff- 
 	
r   )NNT)N)inductorN)r   
__module____qualname____doc__nnModuler   r   r   r)   r   r   boolr   listr%   strr*   Tensorr   r2   __classcell__)r   s   @r   r
   r
      s    , .2+/ G99G RYY01G U\\*	G
 EKK(G G$ *.  
	, &"&
%
% 3-
%
 

%
<<
 LL
 
c5<<	 	
r   r
   )r6   loggingtypingr   r   r   r   r)   torch.nnr7   taskr   	getLoggerr   r   r
    r   r   <module>rD      s:    #  2 2   
'

H
%Y
 Y
r   