o
    
i	                     @   sn   d Z ddlmZmZmZmZ ddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ 		dd
edefddZdS )zW
A logger that queries CuPy metrics and passes that information to downstream loggers.
    )DictAnyOptionalION)Language)context_pools)has_cupy_gpu   )LoggerTcupyprefixreturnc                    sZ   zddl  W n ty   tdw tstdtjtjfdtdtdtf fdd	}|S )
a  Creates a logger that queries CuPy metrics and passes that information to downstream loggers.

    Args:
        prefix (str):
            All metric names are prefixed with this string using dot notation, e.g: `<prefix>.<metric>`.

    Returns:
        LoggerT: Logger instance.
    r   NzThe 'cupy' library could not be found - did you install it? Alternatively, specify the 'ConsoleLogger' in the 'training.logger' config section, instead of the 'CuPyLogger'.zThe 'cupy' library could be loaded but GPU support couldn't be initialized - do you have the necessary CUDA libraries installed?nlpstdoutstderrc                    sD   dt dtfdd dttttf  f fdd}dd	 }||fS )
Nbytesr   c                 S   s   | d S )Ng      0A )r   r   r   F/home/ubuntu/.local/lib/python3.10/site-packages/spacy_loggers/cupy.pyto_mib)   s   z4cupy_logger_v1.<locals>.setup_logger.<locals>.to_mibinfoc                    s   | d u rd S  j  j}t }|d|krd}n|d|kr$d}nd}||  d< | |  d< | |  d< | |  d< | |  d< d S )	Npytorch
tensorflowdefaultz.pool.sourcez.pool.acquired_mibz.pool.used_mibz.pool.free_mibz.pool.num_free_blocks)	cudaget_allocator__self__r   gettotal_bytes
used_bytes
free_bytesn_free_blocks)r   cupy_memory_pool
thinc_poolpool_source)r   r   r   r   r   log_step,   s   z6cupy_logger_v1.<locals>.setup_logger.<locals>.log_stepc                   S   s   d S )Nr   r   r   r   r   finalizeB   s   z6cupy_logger_v1.<locals>.setup_logger.<locals>.finalize)intfloatr   r   strr   )r   r   r   r$   r%   r   r   )r   r   setup_logger(   s   "z$cupy_logger_v1.<locals>.setup_logger)	r   ImportErrorr   
ValueErrorsysr   r   r   r   )r   r*   r   r)   r   cupy_logger_v1   s   &r.   )r   )__doc__typingr   r   r   r   r-   spacyr   thinc.backendsr   
thinc.utilr   utilr
   r(   r.   r   r   r   r   <module>   s    