o
    c²“iò  ã                   @   s€   d dl Z d dlmZmZmZmZ d dlZd dlZ	d dl
mZmZmZ d dlmZ d dlmZ edƒZedƒZG dd	„ d	eƒZdS )
é    N)ÚDictÚOptionalÚTypeVarÚUnion)ÚBatchFormatÚ_convert_batch_type_to_pandasÚ_convert_pandas_to_batch_type)Ú	Predictor)ÚDeveloperAPIÚ
TensorTypeÚTensorDtypec                   @   sL  e Zd Zejdeejee	ejf f de
eeee	ef f  deeee	ef f fdd„ƒZejdedejfdd„ƒZejed	eeee	ef f deeee	ef f fd
d„ƒƒZeedefdd„ƒƒZdejde
eeee	ef f  dejfdd„Zdeejee	ejf f de
eeee	ef f  deejee	ejf f fdd„ZdS )ÚDLPredictorÚnumpy_arraysÚdtypeÚreturnc                 C   ó   t ‚)aI  Converts a NumPy ndarray batch to the tensor type for the DL framework.

        Args:
            numpy_array: The numpy array to convert to a tensor.
            dtype: The tensor dtype to use when creating the DL tensor.
            ndarray: A (dict of) NumPy ndarray(s) that we wish to convert to a (dict of)
                tensor(s).
            dtype: A (dict of) tensor dtype(s) to use when creating the DL tensor; if
                None, the dtype will be inferred from the NumPy ndarray data.

        Returns:
            A deep learning framework specific tensor.
        ©ÚNotImplementedError)Úselfr   r   © r   úT/home/ubuntu/.local/lib/python3.10/site-packages/ray/train/_internal/dl_predictor.pyÚ_arrays_to_tensors   s   zDLPredictor._arrays_to_tensorsÚtensorc                 C   r   )zÏConverts tensor framework specific tensor to a numpy array.

        Args:
            tensor: A framework specific tensor.

        Returns:
            A numpy array representing the input tensor.
        r   )r   r   r   r   r   Ú_tensor_to_array)   s   zDLPredictor._tensor_to_arrayÚinputsc                 C   r   )zôInputs the tensor to the model for this Predictor and returns the result.

        Args:
            inputs: The tensor to input to the model.

        Returns:
            A tensor or dictionary of tensors containing the model output.
        r   )r   r   r   r   r   Ú
call_model6   s   zDLPredictor.call_modelc                 C   s   t jS ©N)r   ÚNUMPY)Úclsr   r   r   Úpreferred_batch_formatE   s   z"DLPredictor.preferred_batch_formatÚdatac                 C   s$   t |tj| jƒ}|  ||¡}t|ƒS r   )r   r   r   Ú_cast_tensor_columnsÚ_predict_numpyr   )r   r    r   Únumpy_inputÚnumpy_outputr   r   r   Ú_predict_pandasJ   s   ýzDLPredictor._predict_pandasc                    sj   t |tƒrt|ƒdkrtt| ¡ ƒƒ}ˆ  ||¡}ˆ  |¡}t |tƒr.‡ fdd„| ¡ D ƒS dˆ  	|¡iS )Né   c                    s   i | ]
\}}|ˆ   |¡“qS r   )r   )Ú.0ÚkÚv©r   r   r   Ú
<dictcomp>e   s    z.DLPredictor._predict_numpy.<locals>.<dictcomp>Úpredictions)
Ú
isinstanceÚdictÚlenÚnextÚiterÚvaluesr   r   Úitemsr   )r   r    r   Úmodel_inputÚmodel_outputr   r*   r   r"   W   s   

zDLPredictor._predict_numpyN)Ú__name__Ú
__module__Ú__qualname__ÚabcÚabstractmethodr   ÚnpÚndarrayr   Ústrr   r   r   r   r   r
   r   Úclassmethodr   r   ÚpdÚ	DataFramer%   r"   r   r   r   r   r      sF    þýüÿþþý
üþýür   )r9   Útypingr   r   r   r   Únumpyr;   Úpandasr?   Ú"ray.air.util.data_batch_conversionr   r   r   Úray.train.predictorr	   Úray.util.annotationsr
   r   r   r   r   r   r   r   Ú<module>   s    