o
    bi                     @   s.  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r0d dlmZ 		ddejdeejj deej d	ejfd
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dZded deeee f d	eejeeejf f fd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                 C   sL   |du r|dur|j }t|tj}t| } |rtjj| |dS tj| |d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_ragged r   V/home/ubuntu/.local/lib/python3.10/site-packages/ray/air/_internal/tensorflow_utils.pyconvert_ndarray_to_tf_tensor   s   r   ndarraysdtypesc                    sh   t | tjr't  tr t dkrtd  tt   t	|  }|S  fd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                    s0   i | ]\}}|t |t tr | n d qS )r   )r   r   dict).0col_namecol_ndarrayr   r   r   
<dictcomp>D   s    z<convert_ndarray_batch_to_tf_tensor_batch.<locals>.<dictcomp>)r   npr   r   len
ValueErrornextitervaluesr   items)r   r   batchr   r!   r   (convert_ndarray_batch_to_tf_tensor_batch*   s   


r+   schema)zpyarrow.lib.Schemar
   columnsc                    s   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dt
tjjf dttdf f fdd	dt
tjjf d
tdtjffddttro| }}||dS fdd| D S )Nr   )TensorDtyper   r   c                    sF   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   res)r.   par   r   	get_dtype_   s   
z get_type_spec.<locals>.get_dtype.c                    sF   d}t | r|  } t | jr|d7 }|S t |  r!|| j7 }|S )Nr/   )r   r3   r0   element_shape)r   shape)r.   r7   tensor_extension_typesr   r   	get_shapei   s   


z get_type_spec.<locals>.get_shapenamec                   sR   |  | }} t dd |D dk}|rtj|| d}|S tj|| |d}|S )Nc                 s   s    | ]}|d u V  qd S r/   r   )r   dimr   r   r   	<genexpr>z   s    z9get_type_spec.<locals>.get_tensor_spec.<locals>.<genexpr>r   r   )r   r=   )sumr   r   
TensorSpec)r   r=   r:   r   r   )r8   r<   r   r   get_tensor_specs   s   z&get_type_spec.<locals>.get_tensor_specr=   c                    s&   i | ]\}}| v r|||d qS )rC   r   )r   r=   r   )r-   rB   r   r   r"      s
    z!get_type_spec.<locals>.<dictcomp>)pyarrowray.data.extensionsr.   r	   r   typer   zipnamestypesr   r#   r   r2   r   r   DTyper   intstrTypeSpecr)   )r,   r-   r   r=   r   r   )r.   r-   r8   r<   rB   r7   r;   r   get_type_specO   s,   (.


rN   )NNr/   )typingr   r   r   r   r   r   numpyr#   rD   
tensorflowr   "ray.air.util.data_batch_conversionr   $ray.air.util.tensor_extensions.arrowr	   ray.data._internal.pandas_blockr
   r   r   rJ   rM   Tensorr   rL   r+   rN   r   r   r   r   <module>   sD     


%