
    &`i                        d dl mZmZmZmZmZmZ d dlZd dl	Z	d dl
Zd dlmZ d dlmZ erd dlmZ 	 	 ddej        deej        j                 deej                 d	ej        fd
Z	 ddeej        eeej        f         f         deeej        j        eeej        j        f         f                  d	eej        eeej        f         f         fdZded         deeee         f         d	eej        eeej        f         f         fdZdS )    )TYPE_CHECKINGDictListOptionalTupleUnionN)%_unwrap_ndarray_object_type_if_needed) get_arrow_extension_tensor_types)PandasBlockSchemandarraydtype	type_specreturnc                     |	||j         }t          |t          j                  }t	          |           } |r!t          j                            | |          S t          j        | |          S )a  Convert a NumPy ndarray to a TensorFlow Tensor.

    Args:
        ndarray: A NumPy ndarray that we wish to convert to a TensorFlow Tensor.
        dtype: A TensorFlow dtype for the created tensor; if None, the dtype will be
            inferred from the NumPy ndarray data.
        type_spec: A type spec that specifies the shape and dtype of the returned
            tensor. If you specify ``dtype``, the dtype stored in the type spec is
            ignored.

    Returns: A TensorFlow Tensor.
    Nr   )r   
isinstancetfRaggedTensorSpecr	   raggedconstantconvert_to_tensor)r   r   r   	is_raggeds       v/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/air/_internal/tensorflow_utils.pyconvert_ndarray_to_tf_tensorr      sm    " }.9b&9::I3G<<G :y!!'!777#G59999    ndarraysdtypesc                 n   t          | t          j                  ryt          t                    rSt	                    dk    rt          d           t          t                                                              t          |           }n fd| 
                                D             }|S )ay  Convert a NumPy ndarray batch to a TensorFlow Tensor batch.

    Args:
        ndarray: A (dict of) NumPy ndarray(s) that we wish to convert to a TensorFlow
            Tensor.
        dtype: A (dict of) TensorFlow dtype(s) for the created tensor; if None, the
            dtype will be inferred from the NumPy ndarray data.

    Returns: A (dict of) TensorFlow Tensor(s).
       z[When constructing a single-tensor batch, only a single dtype should be given, instead got: c           
      t    i | ]4\  }}|t          |t          t                    r|         n           5S )r   )r   r   dict).0col_namecol_ndarrayr   s      r   
<dictcomp>z<convert_ndarray_batch_to_tf_tensor_batch.<locals>.<dictcomp>D   s`     
 
 

 &+	 2*4VT*B*BNfX&&  
 
 
r   )r   npr   r!   len
ValueErrornextitervaluesr   items)r   r   batchs    ` r   (convert_ndarray_batch_to_tf_tensor_batchr.   *   s     (BJ'' 
fd## 	16{{a >5;> >   $v}}//00F,Xv>>
 
 
 

 *2)9)9
 
 
 Lr   schema)zpyarrow.lib.Schemar   columnsc                   	
 dd l 	ddlm t                      
t	          | t
                    rJ t          t          | j        | j	                            }dt          t          j        	j        f         dt          j        j        f	fddt          t          j        	j        f         dt"          t$          df         f	
fddt          t          j        	j        f         dt&          dt          j        ffd	t	          t&                    r|         }} ||
          S fd|                                D             S )Nr   )TensorDtyper   r   c                     t          | j                  r| j        } t          | j                  r|                                 } t          |           r| j        } t          j                            |           }|S N)	r   ListType
value_typeDataTypeto_pandas_dtypeelement_dtyper   r   as_dtype)r   resr2   pas     r   	get_dtypez get_type_spec.<locals>.get_dtype_   sx    eR[)) 	%$EeR[)) 	,))++Ee[)) 	('Ei  ''
r   .c                     d}t          |           r|                                 } t          | j                  r|dz  }nt          |           r
|| j        z  }|S )Nr4   )r   r8   r5   element_shape)r   shaper2   r<   tensor_extension_typess     r   	get_shapez get_type_spec.<locals>.get_shapei   sp    e344 	,))++EeR[)) 	)WEE{++ 	)U((Er   namec                     |            |           } }t          d |D                       dk    }|rt          j        ||           }nt          j        || |          }|S )Nc              3      K   | ]}|d u V  	d S r4    )r"   dims     r   	<genexpr>z9get_type_spec.<locals>.get_tensor_spec.<locals>.<genexpr>z   s&      55t555555r   r   r   )r   rC   )sumr   r   
TensorSpec)r   rC   r@   r   r   r=   rB   s        r   get_tensor_specz&get_type_spec.<locals>.get_tensor_specs   s     !y''5)9)9u 55u555559	 	E+E???IIe5tDDDIr   rC   c                 :    i | ]\  }}|v 	| ||           S )rL   rF   )r"   rC   r   r0   rK   s      r   r%   z!get_type_spec.<locals>.<dictcomp>   s?       D%7?? 	ooe$///??r   )pyarrowray.data.extensionsr2   r
   r   typer!   zipnamestypesr   r&   r   r7   r   r   DTyper   intstrTypeSpecr,   )r/   r0   r   rC   r   r2   r=   rB   rK   r<   rA   s    `   @@@@@@r   get_type_specrX   O   s    //////=??&$'''''6:FL&,''7 7Frx45 ")/       rx45 %S/        RXr{*+69	       '3 1vgeu40000    !<<>>   r   )NNr4   )typingr   r   r   r   r   r   numpyr&   rN   
tensorflowr   "ray.air.util.data_batch_conversionr	   $ray.air.util.tensor_extensions.arrowr
   ray.data._internal.pandas_blockr   r   r   rT   rW   Tensorr   rV   r.   rX   rF   r   r   <module>r`      s   D D D D D D D D D D D D D D D D          T T T T T T Q Q Q Q Q Q BAAAAAA
 (,'+: :Z:BIO$: $: Y	: : : :< LP" "BJS"*_ 556"U29?Dbio1E,FFGH" 29d3	>**+" " " "J:;<:3S	>": 2;S"+-../: : : : : :r   