o
    }oiL                     @   s  d Z ddlZddlZddlZddlmZ ddlmZ ddl	m
Z
mZmZ ddlmZ ddlT ddlmZ ded	ejfd
dZejedfejedfejedfejedfejedfejedfejedfejedfejedfejedfejedfej edffZ!ej"j#ej"j$ej"j%ej"j&ej"j'ej"j(ej"j)ej"j*ej"j+ej"j,ej"j-fZ.dZ/ded	ejfddZ0ej1e0dfej2e0dfej3e0dfej4e0dfej5e0dfej6e0d fej7e0d!fej8e0d"ffZ9d#d$ Z:d%d& Z;d'd( Z<dS ))zFiddle extensions to handle PyTorch code more elegantly.

This module provides extensions for better handling of PyTorch types and functions
in codegen, graphviz, and other debugging functions.
    N)daglish_extensions)import_managerpy_val_to_cst_converterspecial_value_codegen)serialization)*)	to_confignamereturnc                       t d fddS )zMake a torch importable.torchc                       |  d  S )N. )
torch_namer	   r   O/home/ubuntu/.local/lib/python3.10/site-packages/nemo/lightning/io/fdl_torch.py<lambda>$       z(_make_torch_importable.<locals>.<lambda>r   SingleImportabler   r   r   r   _make_torch_importable"      r   booluint8int8int16int32int64float16bfloat16float32float64	complex64
complex128))ztorch.nn.initzfrom torch.nn import initc                    r   )zMake a torch.nn importable.r   c                    r   )Nz.nn.r   )torch_mod_namer   r   r   r   I   r   z+_make_torch_nn_importable.<locals>.<lambda>r   r   r   r   r   _make_torch_nn_importableG   r   r&   ReLUGELUReLU6SiLUSigmoidSELUHardtanhTanhc                 C   s   t | tjS )z,Returns true if `value` is a PyTorch Tensor.)
isinstancer   Tensor)valuer   r   r   is_torch_tensorX   s   r2   c                 C   sB   t jt j|tt ddt ||  td|| j	gdS )z'Convert a PyTorch tensor to a CST node.tensor)r1   attrdtype)funcargs)
cstCall	Attributer   NameArgtolistr   kwarg_to_cstr5   )r1   convert_childr   r   r   convert_torch_tensor_to_cst]   s   r@   c            
      C   s   t D ]
\} }t| | qtD ]
\} }t| | qtD ]
\}}t|| qtt	t
 t D ]\}}t| t|d\}}tj||dd q0tD ]}t| t| qMddd}	tjjtj_|	tj_dS )zjRegisters PyTorch fiddle extensions.

    This allows for things like nicer handling of torch dtypes.
    r   T)compare_by_identityNc                 S   s*   t |tjr| ||S | t|||S )z,Serialize a value to a Fiddle configuration.)r/   typesBuiltinFunctionType_pyref_original_serializer   )selfr1   current_path	all_pathsr   r   r   _modified_serialize   s   z#enable.<locals>._modified_serialize)N)_torch_type_importablesr   register_exact_value_nn_type_importables_import_aliasesr   register_import_aliasr    register_py_val_to_cst_converterr2   r@   r   register_immutablestrsplitr   register_constant_torch_initializers-register_function_with_immutable_return_valueSerialization
_serializerE   )
r1   
importable
module_strimport_stmtr5   _libsymbolinitrI   r   r   r   enableh   s"   


r_   )=__doc__rB   libcstr8   r   torch.nnnnfiddle._srcr   fiddle._src.codegenr   r   r   fiddle._src.experimentalr   nemo.lightning.io.artifactnemo.lightning.io.to_configr   rQ   
Importabler   r   r   r   r   r   r   r   r    r!   r"   r#   r$   rJ   r^   	constant_dirac_xavier_normal_xavier_uniform_kaiming_normal_kaiming_uniform_normal_ones_orthogonal_uniform_zeros_rT   rM   r&   r'   r(   r)   r*   r+   r,   r-   r.   rL   r2   r@   r_   r   r   r   r   <module>   sd   