
    `i-                     @    d dl Z d dlmZ  G d dej                  ZdS )    N)memory_hookc                   D    e Zd ZdZd Zej        dfdZd Zd Z	d Z
d ZdS )	DebugPrintHooka:  Memory hook that prints debug information.

    This memory hook outputs the debug information of input arguments of
    ``malloc`` and ``free`` methods involved in the hooked functions
    at postprocessing time (that is, just after each method is called).

    Example:
        The basic usage is to use it with ``with`` statement.

        Code example::

            >>> import cupy
            >>> from cupy.cuda import memory_hooks
            >>>
            >>> cupy.cuda.set_allocator(cupy.cuda.MemoryPool().malloc)
            >>> with memory_hooks.DebugPrintHook():
            ...     x = cupy.array([1, 2, 3])
            ...     del x  # doctest:+SKIP

        Output example::

            {"hook":"alloc","device_id":0,"mem_size":512,"mem_ptr":150496608256}
            {"hook":"malloc","device_id":0,"size":24,"mem_size":512,"mem_ptr":150496608256,"pmem_id":"0x7f39200c5278"}
            {"hook":"free","device_id":0,"mem_size":512,"mem_ptr":150496608256,"pmem_id":"0x7f39200c5278"}

        where the output format is JSONL (JSON Lines) and
        ``hook`` is the name of hook point, and
        ``device_id`` is the CUDA Device ID, and
        ``size`` is the requested memory size to allocate, and
        ``mem_size`` is the rounded memory size to be allocated, and
        ``mem_ptr`` is the memory pointer, and
        ``pmem_id`` is the pooled memory object ID.

    Attributes:
        file: Output file_like object that redirect to.
        flush: If ``True``, this hook forcibly flushes the text stream
            at the end of print. The default is ``True``.

    Tc                 "    || _         || _        d S )N)fileflush)selfr   r   s      v/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupy/cuda/memory_hooks/debug_print.py__init__zDebugPrintHook.__init__1   s    	


    c                     | j                             |           | j                             d           | j        r| j                                          d S d S )N
)r   writer   )r	   msgs     r
   _printzDebugPrintHook._print5   sV    		: 	IOO	 	r   c                 j    d}|d|d         |d         |d         fz  }|                      |           d S )Nz7{"hook":"%s","device_id":%d,"mem_size":%d,"mem_ptr":%d}alloc	device_idmem_sizemem_ptr)r   r	   kwargsr   s      r
   alloc_postprocessz DebugPrintHook.alloc_postprocess;   sH    ,,z"F9$57 	7Cr   c           
          d}|d|d         |d         |d         |d         t          |d                   fz  }|                     |           d S )NzP{"hook":"%s","device_id":%d,"size":%d,"mem_size":%d,"mem_ptr":%d,"pmem_id":"%s"}mallocr   sizer   r   pmem_idhexr   r   s      r
   malloc_postprocessz!DebugPrintHook.malloc_postprocessB   s_    ;&-vf~z"F9$5s6);L7M7MO 	OCr   c           	          d}|d|d         |d         |d         t          |d                   fz  }|                     |           d S )NzF{"hook":"%s","device_id":%d,"mem_size":%d,"mem_ptr":%d,"pmem_id":"%s"}freer   r   r   r   r   r   s      r
   free_postprocesszDebugPrintHook.free_postprocessI   sY    ;{+z"F9$5s6);L7M7MO 	OCr   N)__name__
__module____qualname____doc__namesysstdoutr   r   r   r    r#    r   r
   r   r      s}        & &P DJd              r   r   )r)   	cupy.cudar   
MemoryHookr   r+   r   r
   <module>r.      sa    



 ! ! ! ! ! !H H H H H[+ H H H H Hr   