o
    }oit                     @   s   d dl mZmZ d dlmZ d dlZdd Zdd Zejej	fdd	Z
ejej	fd
dZG dd dejjZG dd dejjZedd ZdededefddZdS )    )contextmanagernullcontext)AnyNc                   C   s.   t  rt  t jkrt jjdt jdS t S )zM
    If the current autocast context is bfloat16,
    cast it to float32
    cudadtype)torchis_autocast_enabledget_autocast_gpu_dtypebfloat16ampautocastfloat32r    r   r   I/home/ubuntu/.local/lib/python3.10/site-packages/nemo/utils/cast_utils.pyavoid_bfloat16_autocast_context   s   r   c                   C   sp   t  r5t  t jkr5t j st j rt jjdt j	dS t j
 r,t jjdt jdS t jjdt j	dS t S )z{
    If the current autocast context is float16, cast it to bfloat16
    if available (unless we're in jit) or float32
    r   r   )r   r	   r
   float16jitis_scripting
is_tracingr   r   r   r   is_bf16_supportedr   r   r   r   r   r   avoid_float16_autocast_context!   s   
r   c                 C   s   | j |kr| j|dS | S )Nr   )r   to)x
from_dtypeto_dtyper   r   r   cast_tensor3   s   r   c                    sv   t | tjrt|  dS t | tr(i }|  D ]}t| |  d||< q|S t | tr9t fdd| D S d S )Nr   r   c                 3   s    | ]
}t | d V  qdS )r   N)cast_all).0yr   r   r   	<genexpr>A   s    zcast_all.<locals>.<genexpr>)
isinstancer   Tensorr   dictkeysr   tuple)r   r   r   new_dictkr   r   r   r   7   s   

r   c                       $   e Zd Z fddZdd Z  ZS )CastToFloatc                       t t|   || _d S N)superr*   __init__modselfr/   	__class__r   r   r.   E      
zCastToFloat.__init__c                 C   sz   t  r5|jt jkr5t jj|jjdd | j	|
t j
|j}W d    |S 1 s.w   Y  |S | j	|}|S )NFenabled)r   r	   r   r   r   r   devicetyper/   forwardr   )r1   r   retr   r   r   r9   I   s   
zCastToFloat.forward__name__
__module____qualname__r.   r9   __classcell__r   r   r2   r   r*   D       r*   c                       r)   )CastToFloatAllc                    r+   r,   )r-   rA   r.   r/   r0   r2   r   r   r.   S   r4   zCastToFloatAll.__init__c                 G   s~   t  r9|d j}t jj| jjdd | jjt	||t j
d }t	|t j
|dW  d    S 1 s2w   Y  d S | jj| S )Nr   Fr5   r   )r   r	   r   r   r   r7   r8   r/   r9   r   r   )r1   argsr   r:   r   r   r   r9   W   s   
$zCastToFloatAll.forwardr;   r   r   r2   r   rA   R   r@   rA   c                 c   s.    t | |}t| || | V  t| || dS )z$Temporarily monkeypatches an object.N)getattrsetattr)objectnamepatchpre_patched_valuer   r   r   monkeypatcheda   s
   
rI   r   type_returnc                 C   s"   z|| W S  t y   |  Y S w )a  Try to cast a value to int, if it fails, return the original value.

    Args:
        x (Any): The value to be casted.
        type_ (type): The type to cast to, must be a callable.

    Returns:
        Any: The casted value or the original value if casting fails.
    )	Exception)r   rJ   r   r   r   maybe_cast_to_typej   s
   

rM   )
contextlibr   r   typingr   r   r   r   r   r   r   r   nnModuler*   rA   rI   r8   rM   r   r   r   r   <module>   s   
