o
    qoiR                     @   s  d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlZddlmZ	 ddl
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e	jedfe	jedfe	jedffZejj j!ejj j"ejj j#ejj j$ejj j%ejj j&ejj j'ejj j(ejj j)ejj j*ejj j+ejj j,ejj j-ejj j.ejj j/ejj j0ejj j1ejj j2fZ3dZ4ded	ejfddZ5ejj6e5dfejj7e5dfejj8e5d fejj9e5d!fejj:e5d"fejj;e5d#fejj<e5d$fejj=e5d%fejj>e5d&fejj?e5d'fejj@e5d(fejjAe5d)fejjBe5d*ffZCd+d, ZDd-d. ZEd/d0 ZFdS )1zFiddle extensions to handle JAX code more elegantly.

Currently this just affects codegen, graphviz, and other debugging functions.
    )daglish_extensions)import_manager)py_val_to_cst_converter)special_value_codegenN)numpynamereturnc                       t d fddS )N	jax.numpyc                       |  d  S )N. )jnp_namer   r   N/home/ubuntu/.local/lib/python3.10/site-packages/fiddle/_src/extensions/jax.py<lambda>        z&_make_jnp_importable.<locals>.<lambda>r   SingleImportabler   r   r   r   _make_jnp_importable      r   bool_uint8uint16uint32uint64int8int16int32int64bfloat16float16float32float64	complex64
complex128))zjax._src.nn.initializerszfrom jax.nn import initializers)r
   zfrom jax import numpy as jnpc                    r	   )Njaxc                    r   )Nz.nn.r   )jax_mod_namer   r   r   r   S   r   z)_make_jax_nn_importable.<locals>.<lambda>r   r   r   r   r   _make_jax_nn_importableQ   r   r(   relugelurelu6silu	soft_signsigmoidselulog_sigmoid	hard_tanh
hard_swish	hard_silutanhswishc                 C   s   t | tjS )z5Returns true if `value` is a JAX numpy `DeviceArray`.)
isinstancer&   Array)valuer   r   r   is_jnp_device_arrayg   s   r9   c              	   C   sT   t jt j|tt ddt ||  td|| j	j
td|| jgdS )Narray)r8   attrdtypendmin)funcargs)cstCall	AttributejnpNameArgtolistr   kwarg_to_cstr<   r   ndim)r8   convert_childr   r   r   convert_jnp_device_array_to_cstl   s   
rJ   c                  C   s   t D ]
\} }t| | qtD ]
\} }t| | qtD ]
\}}t|| qtt	t
 t D ]	\}}t| q0tD ]}t| t| q<dS )zfRegisters JAX fiddle extensions.

  This allows for things like nicer handling of jax.numpy dtypes.
  N)_jnp_type_importablesr   register_exact_value_nn_type_importables_import_aliasesr   register_import_aliasr    register_py_val_to_cst_converterr9   rJ   r   register_immutable_jax_initializers-register_function_with_immutable_return_value)r8   
importable
module_strimport_stmtr<   _initr   r   r   enablex   s   
rY   )G__doc__fiddle._srcr   fiddle._src.codegenr   r   r   r&   r   rC   libcstr@   str
Importabler   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   rK   nninitializersconstantdelta_orthogonalglorot_normalglorot_uniform	he_normal
he_uniformkaiming_normalkaiming_uniformlecun_normallecun_uniformnormalones
orthogonaluniformvariance_scalingxavier_normalxavier_uniformzerosrR   rN   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   rM   r9   rJ   rY   r   r   r   r   <module>   s~   