o
    ,wii                     @   sh   d Z ddl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
ZdZdZd	d
 Zdd ZdS )zFiddle extensions to handle TensorFlow code more elegantly.

Currently this affects codegen, graphviz, serialization, and other
debugging functions.
    N)import_manager)py_val_to_cst_converter)special_value_codegen)serialization)bfloat16bool
complex128	complex64float16float32float64int16int32int64int8qint16qint32qint8quint16quint8resourcestringuint16uint32uint64uint8variant))
tensorflowzimport tensorflow as tfc                 C   s   t | tjo
t| dS )z8Returns true if `value` is an eager TensorFlow `Tensor`.numpy)
isinstancetfTensorhasattr)value r$   V/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/fiddle/_src/extensions/tf.py	is_tensor(   s   r&   c            	      C   s   dd } t D ]!}ttj|}tjd|dd tdtj	| |d}t
|| qtD ]
\}}t|| q*ttdd }ttjd	d
 }ttjdd }dS )zfRegisters TensorFlow fiddle extensions.

  This allows for things like nicer handling of tf dtypes.
  c                 S   s   |  d| S )N.r$   )module
dtype_namer$   r$   r%   make_dtype_name3   s   zenable.<locals>.make_dtype_namer   F)compare_by_identity)r)   c              
   S   sZ   t jt j|tt ddt ||   t	d|| j
t	d|| j gdS )Nconstantr#   attrdtypeshapefuncargs)cstCall	Attributer    NameArgr   tolistr   kwarg_to_cstr/   r0   as_listr#   convert_childr$   r$   r%   convert_tensor_to_cstA   s   z%enable.<locals>.convert_tensor_to_cstc                 S   s   t j|tt | jdS )Nr-   )r4   r6   r    r7   namer<   r$   r$   r%   convert_dtype_to_cstM   s   z$enable.<locals>.convert_dtype_to_cstc                 S   sD   | j d u rd n|  }tjtj|ttddt||gdS )NTensorShaper-   r1   )rankr;   r4   r5   r6   r    r7   r8   )r#   r=   
shape_listr$   r$   r%   convert_tensor_shape_to_cstQ   s   z+enable.<locals>.convert_tensor_shape_to_cstN)	tf_dtypesgetattrr    dtypesr   register_constantr   SingleImportable	functoolspartialregister_exact_value_import_aliasesr   register_import_aliasr    register_py_val_to_cst_converterr&   DTyperA   )	r*   r)   	dtype_val
importable
module_strimport_stmtr>   r@   rD   r$   r$   r%   enable-   s$   



rU   )__doc__rJ   fiddle._src.codegenr   r   r   fiddle._src.experimentalr   libcstr4   r   r    rE   rM   r&   rU   r$   r$   r$   r%   <module>   s   