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 )zCheck 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   `/home/ubuntu/veenaModal/venv/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 )zConvert 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    


(