o
    i	                     @   sJ   d dl Zd dlZdedefddZdefddZdejjdefd	d
Z	dS )    Nnumberreturnc                 C   s   | dksJ g d}t | dkrtt| d nd}t t|d }t|t|}d|d  }| d|  } |d }| dd||  S )	ab  Return human_readable_count

    Originated from:
    https://github.com/PyTorchLightning/pytorch-lightning/blob/master/pytorch_lightning/core/memory.py

    Abbreviates an integer number with K, M, B, T for thousands, millions,
    billions and trillions, respectively.
    Examples:
        >>> get_human_readable_count(123)
        '123  '
        >>> get_human_readable_count(1234)  # (one thousand)
        '1 K'
        >>> get_human_readable_count(2e6)   # (two million)
        '2 M'
        >>> get_human_readable_count(3e9)   # (three billion)
        '3 B'
        >>> get_human_readable_count(4e12)  # (four trillion)
        '4 T'
        >>> get_human_readable_count(5e15)  # (more than trillion)
        '5,000 T'
    Args:
        number: a positive integer number
    Return:
        A string formatted according to the pattern described above.
    r   ) KMBT      
   z.2fr   )intnpfloorlog10ceilminlen)r   labels
num_digits
num_groupsshiftindex r   T/home/ubuntu/.local/lib/python3.10/site-packages/funasr/train_utils/model_summary.pyget_human_readable_count   s   $r   c                 C   s   t t| dd  d S )N   )r   str)dtyper   r   r   to_bytes*   s   r    modelc                 C   s   d}|t | 7 }d\}}|  D ]%\}}td||j|j|j|j|  || 7 }|jr5|| 7 }qd|d | }t	|}t	|}|d7 }|d| j
j d7 }|d	| d7 }|d
| d| d7 }tt|  j}|d| 7 }|S )NzModel structure:
)r   r   zDname: {}, dtype: {}, device: {}, trainable: {}, shape: {}, numel: {}z{:.1f}g      Y@z

Model summary:
z    Class Name: 
z&    Total Number of model parameters: z$    Number of trainable parameters: z (z%)
z
    Type: )r   named_parametersprintformatr   devicerequires_gradshapenumelr   	__class____name__nextiter
parameters)r!   message
tot_params
num_paramsnameparampercent_trainabler   r   r   r   model_summary/   s.   r5   )
numpyr   torchr   r   r   r    nnModuler5   r   r   r   r   <module>   s
    %