
    j                        d dl Z d dlmZ d dlZd dlZd dlmZ d dlmZ g dZej	        j
        j        Zdej        dej        fdZdej        dej        fd	Z	 	 	 dd
ej        dedz  dedz  dee         dz  deddfdZ	 	 ddedededededeej                 fdZd
edeej                 deej        z  dej        deeeej                 f         f
dZd
ej        dedej        dedef
dZdS )    N)Any)nn)download_from_url)load_pretrained_paramsconv_sequence_ptset_device_and_dtypeexport_model_to_onnx_copy_tensor_bf16_to_float32_CompiledModulexreturnc                 N    |                                                                  S N)clonedetachr   s    ]/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/doctr/models/utils/pytorch.pyr
   r
      s    7799    c                 X    | j         t          j        k    r|                                 n| S r   )dtypetorchbfloat16floatr   s    r   r   r   !   s"    5>11177999q8r   modelpath_or_urlhash_prefixignore_keyskwargsc                    |t          j        d           dS t          j        |          rt	          |f|dd|n|}t          j        |d          }t                    dk    rpD ]}||v r|                    |           | 	                    |d	          \  }}	t          fd
||	z   D                       rt          dd|	 d| z             dS | 	                    |           dS )a"  Load a set of parameters onto a model

    >>> from doctr.models import load_pretrained_params
    >>> load_pretrained_params(model, "https://yoursource.com/yourcheckpoint-yourhash.pt")

    Args:
        model: the PyTorch model to be loaded
        path_or_url: the path or URL to the model parameters (checkpoint)
        hash_prefix: first characters of SHA256 expected hash
        ignore_keys: list of weights to be ignored from the state_dict
        **kwargs: additional arguments to be passed to `doctr.utils.data.download_from_url`
    Nz<No model URL or Path provided, using default initialization.models)r   cache_subdircpu)map_locationr   F)strictc              3       K   | ]}|vV  	d S r    ).0kr   s     r   	<genexpr>z)load_pretrained_params.<locals>.<genexpr>L   s(      LLq#LLLLLLr   z5Unable to load state_dict, due to non-matching keys.
zUnexpected keys: z
Missing keys: )loggingwarning
validatorsurlr   r   loadlenpopload_state_dictany
ValueError)
r   r   r   r   r   archive_path
state_dictkeymissing_keysunexpected_keyss
      `      r   r   r   &   sT   & VWWW >+&&	+`;X``Y_```  Lu===J 3{#3#3a#7#7 	$ 	$Cj  s###(-(=(=jQV(=(W(W%oLLLL\O-KLLLLL 	HUoUU|UUV  	 	 	j)))))r   Fin_channelsout_channelsrelubnc                    |                     d|           |d<   t          j        | |fi |g}|r'|                    t          j        |                     |r(|                    t          j        d                     |S )a  Builds a convolutional-based layer sequence

    >>> from torch.nn import Sequential
    >>> from doctr.models import conv_sequence
    >>> module = Sequential(conv_sequence(3, 32, True, True, kernel_size=3))

    Args:
        in_channels: number of input channels
        out_channels: number of output channels
        relu: whether ReLU should be used
        bn: should a batch normalization layer be added
        **kwargs: additional arguments to be passed to the convolutional layer

    Returns:
        list of layers
    biasT)inplace)getr   Conv2dappendBatchNorm2dReLU)r:   r;   r<   r=   r   conv_seqs         r   r   r   V   s    0 ZZB//F6N!#;!O!O!O!O PH	 6|44555 /---...Or   batchesdevicer   c                 R    |                                fd|D             fS )aW  Set the device and dtype of a model and its batches

    >>> import torch
    >>> from torch import nn
    >>> from doctr.models.utils import set_device_and_dtype
    >>> model = nn.Sequential(nn.Linear(8, 8), nn.ReLU(), nn.Linear(8, 4))
    >>> batches = [torch.rand(8) for _ in range(2)]
    >>> model, batches = set_device_and_dtype(model, batches, device="cuda", dtype=torch.float16)

    Args:
        model: the model to be set
        batches: the batches to be set
        device: the device to be used
        dtype: the dtype to be used

    Returns:
        the model and batches set
    rH   r   c                 >    g | ]}|                                S )rJ   to)r(   batchrH   r   s     r   
<listcomp>z(set_device_and_dtype.<locals>.<listcomp>   s+    1l1l1l[`%((&PU(2V2V1l1l1lr   rL   )r   rG   rH   r   s     ``r   r   r   {   s:    * 8868//1l1l1l1l1ldk1l1l1lllr   
model_namedummy_inputc           
          t          j        j        | || dfdgdgddiddiddddd	| t          j        d
| d           | dS )aJ  Export model to ONNX format.

    >>> import torch
    >>> from doctr.models.classification import resnet18
    >>> from doctr.models.utils import export_model_to_onnx
    >>> model = resnet18(pretrained=True)
    >>> export_model_to_onnx(model, "my_model", dummy_input=torch.randn(1, 3, 32, 32))

    Args:
        model: the PyTorch model to be exported
        model_name: the name for the exported model
        dummy_input: the dummy input to the model
        kwargs: additional arguments to be passed to torch.onnx.export

    Returns:
        the path to the exported model
    z.onnxinputlogitsr   
batch_size)rS   rT   TF)input_namesoutput_namesdynamic_axesexport_paramsdynamoverbosezModel exported to )r   onnxexportr+   info)r   rP   rQ   r   s       r   r	   r	      s    $ 
J IZ !<0Q<MNN     L7j777888r   )NNN)FF)r+   typingr   r   r-   r   doctr.utils.datar   __all___dynamo
eval_frameOptimizedModuler   Tensorr
   r   Modulestrlistr   intboolr   rH   r   tupler   r	   r'   r   r   <module>rl      sE                     . . . . . .   -*:EL U\    9 9 9 9 9 9 #"$(	-* -*9-*t-* t-* cT!	-*
 -* 
-* -* -* -*f 	" """ " 		"
 " 
")_" " " "Jmmel+m585<5GmPUP[m
3U\""#m m m m0 	  s    ad  il            r   