
    Sih                    N    S SK Jr  S SKJr  S SKrS SKJrJr          SS jrg)    )annotations)AnyN)
ModelProtoValueInfoProtoc                V  ^ [        5       m      SS jnU" TU R                  R                  5        U" TU R                  R                  5        U" TU R                  R                  5        S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')
c                    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	   )dim_param_setvalue_infosinfor   r   s        [/var/www/html/ai-image-ml/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 !      c                  > 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	   	TypeError)tensorr   jname	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   )r   zset[str]r   zlist[ValueInfoProto]returnNone)
r    r   r   r   r!   r   r"   r   r'   r(   )
setgraphinputoutput
value_infor"   	enumerateonnxcheckercheck_model)model
input_dimsoutput_dimsr   r%   input_
input_nameinput_dim_arrr!   r   r,   output_nameoutput_dim_arrr   s                @r   update_inputs_outputs_dimsr:      s
   F "eM55.B5	5 }ekk&7&78}ekk&8&89}ekk&<&<=. ++##[[
"..FAvA2 / $ ++$$kk$1/FAvA3 0 % 	LLU#Lr   )r2   r   r3   dict[str, list[Any]]r4   r;   r'   r   )	
__future__r   typingr   onnx.checkerr/   r   r   r:    r   r   <module>r@      sA    #   +VV$V &V 	Vr   