o
    `Û·i³  ã                   @   sœ   d dl mZ d dlmZmZmZ d dlZd dlm	Z	m
Z
 d dlmZ e
ƒ \ZZe	ƒ \ZZZedd„ ƒZedd	„ ƒZed
eee ef dee fdd„ƒZdS )é    )Údeque)ÚAnyÚListÚUnionN)Útry_import_tfÚtry_import_torch)ÚDeveloperAPIc                 C   s<   t rt  | ¡rt  | ¡S trt | ¡rtj | ¡S t | ¡S )zŒCheck if a value is NaN.

    Args:
        value: The value to check.

    Returns:
        True if the value is NaN, False otherwise.
    )ÚtorchÚ	is_tensorÚisnanÚtfÚmathÚis_nanÚnp©Úvalue© r   úY/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/rllib/utils/metrics/stats/utils.pyÚ
safe_isnan   s
   


r   c                 C   s:   t rt| t jƒr|  ¡  ¡  ¡ S trt | ¡r|  ¡ S | S )z®Convert a single value to CPU if it's a tensor.

    TensorFlow tensors are always converted to numpy/python values.
    PyTorch tensors are converted to python scalars.
    )	r	   Ú
isinstanceÚTensorÚdetachÚcpuÚitemr   r
   Únumpyr   r   r   r   Úsingle_value_to_cpu   s
   r   ÚvaluesÚreturnc                 C   sF   | sg S t rt| d t jƒrt  t| ƒ¡}| ¡  ¡ }| ¡ S t| ƒS )aµ  Convert a list or deque of GPU tensors to CPU scalars in a single operation.

    This function efficiently processes multiple PyTorch GPU tensors together by
    stacking them and performing a single .cpu() call. Assumes all values are either
    PyTorch tensors (on same device) or already CPU values.

    Args:
        values: A list or deque of values that may be GPU tensors.

    Returns:
        A list of CPU scalar values.
    r   )r	   r   r   ÚstackÚlistr   r   Útolist)r   ÚstackedÚ
cpu_tensorr   r   r   Úbatch_values_to_cpu,   s   r#   )Úcollectionsr   Útypingr   r   r   r   r   Úray.rllib.utils.frameworkr   r   Úray.util.annotationsr   r	   Ú_r   r   r   r#   r   r   r   r   Ú<module>   s    


(