
    hl              	       n    S SK JrJrJrJr  S SKrS SKJrJr  S\S\\	\\   4   S\\	\\   4   S\4S jr
g)	    )AnyDictListSetN)
ModelProtoValueInfoProtomodel
input_dimsoutput_dimsreturnc           
        ^ [        5       mS[        [           S[        [           SS4S jnU" TU R
                  R                  5        U" TU R
                  R                  5        U" TU R
                  R                  5        S[        S[        S[        S	[        SS4
U4S
 jjnU R
                  R                   H0  nUR                  nX   n[        U5       H  u  pU" XYX5        M     M2     U R
                  R                   H0  n
U
R                  nX+   n[        U5       H  u  pU" XX5        M     M2     [        R                  R                  U 5        U $ )a  
This function updates the dimension sizes of the model's inputs and outputs to the values
provided in input_dims and output_dims. if the dim value provided is negative, a unique dim_param
will be set for that dimension.

Example. if we have the following shape for inputs and outputs:

* shape(input_1) = ('b', 3, 'w', 'h')
* shape(input_2) = ('b', 4)
* shape(output)  = ('b', 'd', 5)

The parameters can be provided as:

::

    input_dims = {
        "input_1": ['b', 3, 'w', 'h'],
        "input_2": ['b', 4],
    }
    output_dims = {
        "output": ['b', -1, 5]
    }

Putting it together:

::

    model = onnx.load('model.onnx')
    updated_model = update_inputs_outputs_dims(model, input_dims, output_dims)
    onnx.save(updated_model, 'model.onnx')
dim_param_setvalue_infosr   Nc                     U Hi  nUR                   R                  R                  nUR                   H6  nUR	                  S5      (       d  M  U R                  UR                  5        M8     Mk     g )N	dim_param)typetensor_typeshapedimHasFieldaddr   )r   r   infor   r   s        V/var/www/fran/franai/venv/lib/python3.13/site-packages/onnx/tools/update_model_dims.pyinit_dim_param_set6update_inputs_outputs_dims.<locals>.init_dim_param_set1   sR      DII))//Eyy<<,,!%%cmm4 !      tensorr   jnamec                   > U R                   R                  R                  R                  U   n[	        U[
        5      (       a  US:  aO  UR                  S5      (       a2  UR                  U:w  a"  [        SU SU SU SUR                   S3	5      eXl        g US-   [        U5      -   nUT;   a  [        S	U SU S
35      eXTl
        g [	        U[        5      (       a  Xl
        g [        S[        U5       35      e)Nr   	dim_valuez!Unable to set dimension value to z
 for axis z of z'. Contradicts existing dimension value ._z-Unable to generate unique dim_param for axis z,. Please manually provide a dim_param value.z@Only int or str is accepted as dimension value, incorrect type: )r   r   r   r   
isinstanceintr   r!   
ValueErrorstrr   )r   r   r   r   	dim_protogenerated_dim_paramr   s         r   
update_dim.update_inputs_outputs_dims.<locals>.update_dim>   s'   KK++1155a8	c3ax%%k22y7J7Jc7Q$;C5
1#TRVQWW~  @I  @S  @S  T  TU  V  '*#&*Sj3q6&9#&-7$Gs$tf  UA  B  ':#S!!"%RSWX[S\R]^ r   )setr   r'   r   r   graphinputoutput
value_infor   r%   r   	enumerateonnxcheckercheck_model)r	   r
   r   r   r*   input_
input_nameinput_dim_arrr   r   r/   output_nameoutput_dim_arrr   s                @r   update_inputs_outputs_dimsr:      s6   H "eM53x5.2>.B5	5 }ekk&7&78}ekk&8&89}ekk&<&<=>   3 4 . ++##[[
"..FAvA2 / $ ++$$kk$1/FAvA3 0 % 	LLU#Lr   )typingr   r   r   r   onnx.checkerr2   r   r   r'   r:    r   r   <module>r>      s[   
 ( '  +WWS$s)^$W c49n%W 	Wr   