
    `ie                         d dl mZ d dlZd dlZd dlmZ  G d dej                  Z G d de          Z	 G d d	e          Z
dS )
    )pathN)memory_hookc                   n    e Zd ZdZd ZddZd Zd ZddZd Z	d Z
d	 Zej        fd
Zdej        fdZdS )LineProfileHookaZ  Code line CuPy memory profiler.

    This profiler shows line-by-line GPU memory consumption using traceback
    module. But, note that it can trace only CPython level, no Cython level.
    ref. https://github.com/cython/cython/issues/1755

    Example:
        Code example::

            from cupy.cuda import memory_hooks
            hook = memory_hooks.LineProfileHook()
            with hook:
                # some CuPy codes
            hook.print_report()

        Output example::

            _root (4.00KB, 4.00KB)
              lib/python3.6/unittest/__main__.py:18:<module> (4.00KB, 4.00KB)
                lib/python3.6/unittest/main.py:255:runTests (4.00KB, 4.00KB)
                  tests/cupy_tests/test.py:37:test (1.00KB, 1.00KB)
                  tests/cupy_tests/test.py:38:test (1.00KB, 1.00KB)
                  tests/cupy_tests/test.py:39:test (2.00KB, 2.00KB)

        Each line shows::

            {filename}:{lineno}:{func_name} ({used_bytes}, {acquired_bytes})

        where *used_bytes* is the memory bytes used from CuPy memory pool, and
        *acquired_bytes* is the actual memory bytes the CuPy memory pool
        acquired from GPU device.
        *_root* is a root node of the stack trace to show total memory usage.

    Args:
        max_depth (int): maximum depth to follow stack traces.
            Default is 0 (no limit).
    r   c                     i | _         t          d d           | _        t          j        t
                    | _        || _        d S N)_memory_framesMemoryFrame_rootr   abspath__file__	_filename
_max_depth)self	max_depths     w/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupy/cuda/memory_hooks/line_profile.py__init__zLineProfileHook.__init__1   s7      t,,
h//#    c                 2    |                      |           d S )N)
used_bytes_create_frame_tree)r   	device_idsizemem_sizes       r   malloc_preprocessz!LineProfileHook.malloc_preprocess8   s    844444r   c                 2    |                      |           d S )N)acquired_bytesr   )r   r   r   s      r   alloc_preprocessz LineProfileHook.alloc_preprocess<   s    x88888r   c                 \   | j         xj        |z  c_        | j         xj        |z  c_        | j         }t          |                                           D ]X\  }}d| j        cxk     r	|dz   k    rn n d S |                     ||          }|xj        |z  c_        |xj        |z  c_        |}Yd S )Nr      )r   r   r   	enumerate_extract_stackframesr   
_add_frame)r   r   r   parentdepth
stackframememory_frames          r   r   z"LineProfileHook._create_frame_tree?   s    
+
!!^3!!!*4+D+D+F+F!G!G 	" 	"E:4?////eai/////??6:>>L##z1##''>9''!FF	" 	"r   c                 b     t          j                    }d |D             } fd|D             }|S )Nc                 ,    g | ]}t          |          S  )
StackFrame).0sts     r   
<listcomp>z8LineProfileHook._extract_stackframes.<locals>.<listcomp>M   s    <<<"z"~~<<<r   c                 4    g | ]}|j         j        k    |S r+   )filenamer   )r-   r.   r   s     r   r/   z8LineProfileHook._extract_stackframes.<locals>.<listcomp>N   s1     G G Gt~(E(EB(E(E(Er   )	tracebackextract_stack)r   stackframess   ` r   r#   z$LineProfileHook._extract_stackframesK   sY    -//<<<<<G G G G$G G Gr   c                 ,    ||j         |j        |j        fS r   )r1   linenonamer   r%   r'   s      r   
_key_framezLineProfileHook._key_frameR   s     #!! 	!r   c                     |                      ||          }|| j        v r| j        |         }nt          ||          }|| j        |<   |S r   )r9   r	   r
   )r   r%   r'   keyr(   s        r   r$   zLineProfileHook._add_frameX   sT    oofj11$%%%.s3LL&vz::L'3D$r   c                     d| j                                         z  }|                    |           | j         j        D ]}|                     |d|           |                                 dS )z)Prints a report of line memory profiling.z_root (%s, %s)
r!   r&   fileN)r   humanized_byteswritechildren_print_frameflush)r   r>   linechilds       r   print_reportzLineProfileHook.print_reporta   so    !DJ$>$>$@$@@

4Z( 	9 	9Ee148888

r   c                 
   d|dz  z  }|j         }|                                \  }}||j        d|j        d|j        d|d|d}|                    |           |j        D ]}	|                     |	|dz   |           d S )	N    :z (z, z)
r!   r=   )r'   r?   r1   r6   r7   r@   rA   rB   )
r   r(   r&   r>   indentr.   r   r   rD   rE   s
             r   rB   zLineProfileHook._print_framei   s    	"$%1%A%A%C%C"
NFBKKKBGGGJJ( 	

4!* 	A 	AEe5194@@@@	A 	Ar   N)r   )r   r   )__name__
__module____qualname____doc__r7   r   r   r   r   r#   r9   r$   sysstdoutrF   rB   r+   r   r   r   r      s        $ $L D$ $ $ $5 5 59 9 9
" 
" 
" 
"  ! ! !   !$
     01sz 	A 	A 	A 	A 	A 	Ar   r   c                       e Zd ZdZd ZdS )r,   zCompatibility layer for outputs of traceback.extract_stack().

    Attributes:
        filename (string): filename
        lineno (int): line number
        name (string): function name
    c                     t          |t                    r)|d         | _        |d         | _        |d         | _        d S |j        | _        |j        | _        |j        | _        d S )Nr   r!   rI   )
isinstancetupler1   r6   r7   )r   objs     r   r   zStackFrame.__init__~   sX    c5!! 	!FDMa&DKADIIILDM*DKDIIIr   N)rL   rM   rN   rO   r   r+   r   r   r,   r,   u   s-         ! ! ! ! !r   r,   c                   *    e Zd ZdZd Zd Zd Zd ZdS )r
   a  A single stack frame along with sum of memory usage at the frame.

    Attributes:
        stackframe (FrameSummary): stackframe from traceback.extract_stack().
        parent (MemoryFrame): parent frame, that is, caller.
        children (list of MemoryFrame): child frames, that is, callees.
        used_bytes (int): memory bytes that users used from CuPy memory pool.
        acquired_bytes (int): memory bytes that CuPy memory pool acquired
            from GPU device.
    c                 h    || _         g | _        |                     |           d| _        d| _        d S )Nr   )r'   rA   _set_parentr   r   r8   s      r   r   zMemoryFrame.__init__   s9    $   r   c                 r    |                      | j                  }|                      | j                  }||fS r   )_humanized_sizer   r   )r   r   r   s      r   r?   zMemoryFrame.humanized_bytes   s8    ))$/::
--d.ABBN++r   c                 f    |r,||j         vr%|| _        |j                             |            d S d S d S r   )rA   r%   append)r   r%   s     r   rY   zMemoryFrame._set_parent   sI     	)fFO33 DKO""4(((((	) 	)33r   c                 B    dD ]}|dk     r	d||fz  c S |dz  }d|dfz  S )N) KMGTPEg      @z%3.2f%sBz%.2f%sBZr+   )r   r   units      r   r[   zMemoryFrame._humanized_size   sI    6 	 	Df}}!T4L0000FNDDD#;&&r   N)rL   rM   rN   rO   r   r?   rY   r[   r+   r   r   r
   r
      sZ        	 	     , , ,
) ) )
' ' ' ' 'r   r
   )osr   rP   r2   	cupy.cudar   
MemoryHookr   objectr,   r
   r+   r   r   <module>rl      s          



     ! ! ! ! ! !jA jA jA jA jAk, jA jA jAZ! ! ! ! ! ! ! !("' "' "' "' "'& "' "' "' "' "'r   