a
    ßñhè  ã                   @   sÔ   d dl mZmZmZmZmZmZmZ d dlZd dl	Z	d dl
mZmZmZ edƒZdZG dd„ deƒZde	jjeeeedf   eee  dd	œd
d„Zeeeeee	jf ef dœdd„Zddœdd„Zdd„ ZdS )é    )ÚAnyÚTypeVarÚOptionalÚTupleÚListÚ
NamedTupleÚUnionN)Ú	TupleTypeÚOptionalTypeÚListTypeÚTé   c                   @   s   e Zd ZU eed< eed< dS )ÚInflatableArgÚvalueÚfmtN)Ú__name__Ú
__module__Ú__qualname__r   Ú__annotations__Ústr© r   r   úZ/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torch/utils/bundled_inputs.pyr      s   
r   .)ÚmodelÚinputsÚ_receive_inflate_exprÚreturnc              	   C   sš  t | tjjƒstdƒ‚dd„ | jjjdd… D ƒ}tt	|ƒƒ}t
|ƒ}| j d|g ¡ | j d|d¡ t| dƒr‚|dur€td	ƒ‚nä|du r”td
ƒ‚nÒg }g }t|ƒD ]x\}}	g }
| d¡ t|	ƒD ]@\}}t|d|› d|› d�ƒ\}}|
 |¡ | d|› d�¡ qÂ| t|
ƒ¡ | d¡ q¤| d¡ d |¡}|du�rF| |¡ || _t d¡ |¡}|  |¡ |  t d¡¡ |  t d¡¡ |  t d¡¡ dS )a×  Add bundled sample inputs to a model.

    Models with bundled inputs can be invoked in a uniform manner by
    benchmarking and code coverage tools.

    Augmented models will support the following methods:

      `get_all_bundled_inputs() -> List[Tuple[Any, ...]]`
        Returns a list of tuples suitable for passing to the model like
        `for inp in model.get_all_bundled_inputs(): model(*inp)`

      `get_num_bundled_inputs() -> int`
        Equivalent to `len(model.get_all_bundled_inputs())`,
        but slightly easier to call from C++.

      `run_on_bundled_input(idx: int) -> Any`
        Run the model on bundled input number `idx`

    Inputs can be specified in one of two ways:

      - The model can define `_generate_bundled_inputs`
        get_all_bundled_inputs will simply call this method
        and cache the value.
      - The `inputs` argument to this function can be a list of tuples,
        of the same form that will be returned by get_all_bundled_inputs.
        This function will attempt to optimize arguments so that (e.g.)
        arguments like `torch.zeros(1000)` will be represented compactly.
        Only top-level arguments will be optimized.
        Tensors in lists or tuples will not.
    zOnly ScriptModule is supported.c                 S   s   g | ]
}|j ‘qS r   )Útype)Ú.0Úargr   r   r   Ú
<listcomp>8   ó    z5augment_model_with_bundled_inputs.<locals>.<listcomp>é   NÚ_bundled_inputs_deflatedZ_bundled_inputs_inflatedZ_generate_bundled_inputszCinputs is not None, but _generate_bundled_inputs is already definedzKinputs must be specified if _generate_bundled_inputs is not already definedú(z	deflated[z][ú]z    ú,z),Ú Ú
z°
            def _generate_bundled_inputs(self):
                deflated = self._bundled_inputs_deflated
                return [
            {}
                ]
            a8  
        def get_all_bundled_inputs(self):
            if self._bundled_inputs_inflated is None:
                self._bundled_inputs_inflated = self._generate_bundled_inputs()
            all_inputs = self._bundled_inputs_inflated
            assert all_inputs is not None
            return all_inputs
        zi
        def get_num_bundled_inputs(self):
            return len(self.get_all_bundled_inputs())
        zx
        def run_on_bundled_input(self, idx: int):
            return self(*self.get_all_bundled_inputs()[idx])
        )Ú
isinstanceÚtorchÚjitÚScriptModuleÚ	ExceptionZforwardZschemaÚ	argumentsr   r	   r
   Z_cZ_register_attributeÚhasattrÚ	enumerateÚappendÚ_inflate_exprÚtupleÚjoinr"   ÚtextwrapÚdedentÚformatZdefine)r   r   r   Zforward_arg_typesZdeflated_inputs_typeZinflated_inputs_typeZdeflated_inputsÚpartsZinp_idxÚargsZdeflated_argsZarg_idxr   ZdeflatedZinflaterÚexprZ
definitionr   r   r   Ú!augment_model_with_bundled_inputs   sN    #
ÿÿ






ú

r:   )r   Úrefr   c                 C   sô   t | tƒr| j| j |¡fS t | tjƒrè|  ¡  ¡ t	kr@| |fS |  
¡ r`|  ¡ t	kr`|  ¡ |fS tjtjfD ]Z}| j
|d�rl| |  ¡ d k ¡  ¡ rlt |  ¡ d g¡j|  ¡ Ž |› d|› d�f  S qltd|› d|  ¡  ¡ › d�ƒ‚n| |fS d S )N)Zmemory_formatr   z.contiguous(memory_format=ú)z$Bundled input argument at position 'z ' is a tensor with storage size z6. You probably don't want to bundle this as an input. )r(   r   r   r   r6   r)   ÚTensorZstorageÚsizeÚMAX_RAW_TENSOR_SIZEZis_contiguousZnumelÚcloneZcontiguous_formatZchannels_lastÚflattenÚallÚitemZtensorÚexpandr,   )r   r;   r   r   r   r   r1   y   s$    
$ÿ

ÿÿr1   ©Údtypec                 G   s    t jd| d�j|Ž }t|dd�S )z9Generate a tensor that will be inflated with torch.randn.r!   rE   ztorch.randn_like({})©r   r   )r)   ÚzerosrD   r   )rF   r>   Zstubr   r   r   Úbundle_randn™   s    rI   c                 C   s   t | dd�S )z3Wrap a tensor to allow bundling regardless of size.z{}rG   )r   )Útr   r   r   Úbundle_large_tensorŸ   s    rK   )NN)Útypingr   r   r   r   r   r   r   r4   r)   Ztorch._Cr	   r
   r   r   r?   r   r*   r+   r   r:   r=   r1   rI   rK   r   r   r   r   Ú<module>   s"   $  ý
üg$ 