o
    ϯi                     @   sd   d dl Z d dlmZ d dlZd dlmZ dejde jee	f fddZ
ddejd	e	defd
dZdS )    N)defaultdict)nnmodelreturnc                 C   sj   t t}|  D ]*\}}| }|d}tdt|d D ]}d|d| }||  |7  < qq|S )ax  
    Count parameters of a model and its submodules.

    Args:
        model: a torch module

    Returns:
        dict (str-> int): the key is either a parameter name or a module name.
        The value is the number of elements in the parameter, or in all
        parameters of the module. The key "" corresponds to the total
        number of parameters of the model.
    .r      N)r   intnamed_parametersnumelsplitrangelenjoin)r   rnameprmsizekprefix r   M/home/ubuntu/.local/lib/python3.10/site-packages/fvcore/nn/parameter_count.pyparameter_count
   s   
r      	max_depthc                    s   t |  dd |  D g dtdtfdddtdtdd	f fd
dd df dd tj}dt_tjddgdd}|t_|S )a	  
    Format the parameter count of the model (and its submodules or parameters)
    in a nice table. It looks like this:

    ::

        | name                            | #elements or shape   |
        |:--------------------------------|:---------------------|
        | model                           | 37.9M                |
        |  backbone                       |  31.5M               |
        |   backbone.fpn_lateral3         |   0.1M               |
        |    backbone.fpn_lateral3.weight |    (256, 512, 1, 1)  |
        |    backbone.fpn_lateral3.bias   |    (256,)            |
        |   backbone.fpn_output3          |   0.6M               |
        |    backbone.fpn_output3.weight  |    (256, 256, 3, 3)  |
        |    backbone.fpn_output3.bias    |    (256,)            |
        |   backbone.fpn_lateral4         |   0.3M               |
        |    backbone.fpn_lateral4.weight |    (256, 1024, 1, 1) |
        |    backbone.fpn_lateral4.bias   |    (256,)            |
        |   backbone.fpn_output4          |   0.6M               |
        |    backbone.fpn_output4.weight  |    (256, 256, 3, 3)  |
        |    backbone.fpn_output4.bias    |    (256,)            |
        |   backbone.fpn_lateral5         |   0.5M               |
        |    backbone.fpn_lateral5.weight |    (256, 2048, 1, 1) |
        |    backbone.fpn_lateral5.bias   |    (256,)            |
        |   backbone.fpn_output5          |   0.6M               |
        |    backbone.fpn_output5.weight  |    (256, 256, 3, 3)  |
        |    backbone.fpn_output5.bias    |    (256,)            |
        |   backbone.top_block            |   5.3M               |
        |    backbone.top_block.p6        |    4.7M              |
        |    backbone.top_block.p7        |    0.6M              |
        |   backbone.bottom_up            |   23.5M              |
        |    backbone.bottom_up.stem      |    9.4K              |
        |    backbone.bottom_up.res2      |    0.2M              |
        |    backbone.bottom_up.res3      |    1.2M              |
        |    backbone.bottom_up.res4      |    7.1M              |
        |    backbone.bottom_up.res5      |    14.9M             |
        |    ......                       |    .....             |

    Args:
        model: a torch module
        max_depth (int): maximum depth to recursively print submodules or
            parameters

    Returns:
        str: the table to be printed
    c                 S   s   i | ]
\}}|t |jqS r   )tupleshape).0r   vr   r   r   
<dictcomp>S   s    z)parameter_count_table.<locals>.<dictcomp>xr   c                 S   sJ   | dkrd | d S | dkrd | d S | dkr!d | d	 S t| S )
Ng    חAz{:.1f}Gg    eAg     j@z{:.1f}Mg    .Ag      Y@z{:.1f}Kg     @@)formatstr)r   r   r   r   format_sizeZ   s   z*parameter_count_table.<locals>.format_sizelvlr   Nc                    s   | krd S    D ]@\}}|d| krJ||rJd| d  }|v r4|| |t|  f q
|| || f | d |d  q
d S )Nr    r   )itemscount
startswithappendr!   )r#   r   r   r   indentr&   fillr"   r   param_shapetabler   r   r+   c   s    z#parameter_count_table.<locals>.fillr    r   Tr   z#elements or shapepipe)headerstablefmt)r   r	   r   r!   r(   poptabulatePRESERVE_WHITESPACE)r   r   old_wstabr   r*   r   parameter_count_table!   s    0$	

r7   )r   )typingcollectionsr   r3   torchr   ModuleDefaultDictr!   r   r   r7   r   r   r   r   <module>   s   