
    RЦi                     V    S r SSKJrJr  SSKrSSKJr   " S S\R                  5      rg)zBase training task abstraction.

This module provides the base TrainingTask class that encapsulates a complete
forward pass including loss computation. Tasks return a dictionary with loss
components and outputs for logging.
    )DictOptionalNc                      ^  \ rS rSrSr   SS\\R                     S\\R                     S\	4U 4S jjjr
U 4S jr SS\\   S	S 4S
 jjrS\R                  S\R                  S	\\\R                  4   4S jrSrU =r$ )TrainingTask   a  Base class for training tasks.

A training task encapsulates a complete forward pass including loss computation.
Tasks return a dictionary containing the training loss and other components for logging.

The returned dictionary must contain:
    - 'loss': The training loss for backward pass (required)
    - 'output': Model output/logits for metric computation (recommended)
    - Other task-specific loss components for logging (optional)

Args:
    device: Device for task tensors/buffers (defaults to cpu)
    dtype: Dtype for task tensors/buffers (defaults to torch default)
    verbose: Enable info logging

Example:
    >>> task = SomeTask(model, criterion, device=torch.device('cuda'))
    >>>
    >>> # Prepare for distributed training (if needed)
    >>> if distributed:
    >>>     task.prepare_distributed(device_ids=[local_rank])
    >>>
    >>> # Training loop
    >>> result = task(input, target)
    >>> result['loss'].backward()
devicedtypeverbosec                    > [         TU ]  5         Ub  UO[        R                  " S5      U l        Ub  UO[        R                  " 5       U l        X0l        g )Ncpu)super__init__torchr   get_default_dtyper	   r
   )selfr   r	   r
   	__class__s       M/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/task/task.pyr   TrainingTask.__init__)   sC     	 & 2fU8K#/UU5L5L5N
    c                    > [         R                  " S5      R                  " U0 UD6nUR                  U l        UR                  U l        [
        TU ]  " U0 UD6$ )zFMove task to device/dtype, keeping self.device and self.dtype in sync.r   )r   emptytor   r	   r   )r   argskwargsdummyr   s       r   r   TrainingTask.to4   sJ    A!!4262ll[[
wz4*6**r   
device_idsreturnc                     U $ )a  Prepare task for distributed training.

This method wraps trainable components in DistributedDataParallel (DDP)
while leaving non-trainable components (like frozen teacher models) unwrapped.

Should be called after task initialization but before training loop.

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)

Example:
    >>> task = LogitDistillationTask(student, teacher, criterion)
    >>> task.prepare_distributed(device_ids=[args.local_rank])
    >>> task = torch.compile(task)  # Compile after DDP
 )r   r   
ddp_kwargss      r   prepare_distributed TrainingTask.prepare_distributed;   s	    2 r   inputtargetc                     [         e)zPerform forward pass and compute loss.

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

Returns:
    Dictionary with at least 'loss' key containing the training loss
)NotImplementedError)r   r$   r%   s      r   forwardTrainingTask.forwardV   s
     "!r   )r   r	   r
   )NNT)N)__name__
__module____qualname____firstlineno____doc__r   r   r   r	   boolr   r   listr"   Tensorr   strr(   __static_attributes____classcell__)r   s   @r   r   r      s    : .2+/ 		U\\*	 EKK(	 		 	+ *.  
	6"<<" LL" 
c5<<	 	" "r   r   )	r.   typingr   r   r   torch.nnnnModuler   r    r   r   <module>r9      s'    "  W"299 W"r   