o
    }oi                     @   s   d dl Z d dlmZ edd\ZZer'd dlmZ dd Zdd Zee_	ee_
				
	 	dde jde jde jdededededefddZdS )    N)safe_import_fromcut_cross_entropylinear_cross_entropyc                 C   sf   ddl }z|dj}||}|| }td| d|   ||kW S  |jy2   td Y dS w )zCheck if pytorch-triton version is greater than or equal to the specified version.

        Args:
            version_str: Version string to check

        Returns:
            bool: True if pytorch-triton version >= specified version
        r   Nzpytorch-tritonz Current pytorch-triton version: z, Required triton version: zpytorch-triton not foundF)pkg_resourcesget_distributionversionparse_versionprintDistributionNotFound)version_strr   pytorch_triton_versioncurrentrequired r   Q/home/ubuntu/.local/lib/python3.10/site-packages/nemo/automodel/loss/linear_ce.pynew_is_triton_greater_or_equalL   s   	


r   c                   C   s   t dS )zCheck if pytorch-triton version is greater than or equal to 3.1.0

        Returns:
            bool: True if pytorch-triton version >= 3.1.0
        z3.1.0)r   r   r   r   r   $new_is_triton_greater_or_equal_3_2_0a   s   r   meanautohidden_states	lm_weightlabelsnum_items_in_batchignore_index	reductionlogit_softcappingaccuracy_thresholdc           	   
   C   sR   |dkrd}t | ||||dddd}|dkr'|du r#t||k }|| }|S )aC  
    Compute fused linear cross entropy loss that matches PyTorch's cross_entropy behavior.

    Args:
        hidden_states: Input hidden states
        lm_weight: Weight matrix for linear transformation
        labels: Target labels
        num_items_in_batch: Number of valid tokens (where labels != ignore_index)
        ignore_index: Value to ignore in labels (default: -100)
        reduction: Reduction method ('mean' or 'sum')
        logit_softcapping: Value for softcapping logits (0 means no capping)
        accuracy_threshold: Threshold for accuracy computation
    r   NsumF)targetsr   softcapr   shift
filter_epsr   )r   torchr   item)	r   r   r   r   r   r   r   r   lossr   r   r   fused_linear_cross_entropyn   s"   r&   )Nr   r   r   r   )r#   nemo.utils.import_utilsr   r   HAVE_LINEAR_LOSS_CEcut_cross_entropy.tl_utilstl_utilsr   r   is_triton_greater_or_equal is_triton_greater_or_equal_3_2_0Tensorintstrfloatr&   r   r   r   r   <module>   sD   >	