
    bIg                    L    d dl mZ d dlmZ d dlZd dlmZmZ 	 	 	 	 	 	 	 	 ddZy)    )annotations)AnyN)
ModelProtoValueInfoProtoc                P   t               	 	 	 	 	 	 dd} || j                  j                          || j                  j                          || j                  j                         dfd}| j                  j                  D ]1  }|j
                  }||   }t        |      D ]  \  }}	 |||	||        3 | j                  j                  D ]1  }
|
j
                  }||   }t        |      D ]  \  }}	 ||
|	||        3 t        j                  j                  |        | S )aM  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                    |D ]`  }|j                   j                  j                  }|j                  D ]/  }|j	                  d      s| j                  |j                         1 b y )N	dim_param)typetensor_typeshapedimHasFieldaddr	   )dim_param_setvalue_infosinfor   r   s        Y/var/www/html/answerous/venv/lib/python3.12/site-packages/onnx/tools/update_model_dims.pyinit_dim_param_setz6update_inputs_outputs_dims.<locals>.init_dim_param_set1   sY       	5DII))//Eyy 5<<,!%%cmm45	5    c                   | j                   j                  j                  j                  |   }t	        |t
              r~|dk\  rJ|j                  d      r1|j                  |k7  r"t        d| d| d| d|j                   d	      ||_        y |dz   t        |      z   }|v rt        d	| d| d
      ||_
        y t	        |t              r||_
        y t        dt        |             )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	   )tensorr   jname	dim_protogenerated_dim_paramr   s         r   
update_dimz.update_inputs_outputs_dims.<locals>.update_dim>   s$   KK++1155a8	c3ax%%k2y7J7Jc7Q$;C5
1#TRVQWW~  @I  @S  @S  T  TU  V  '*	#&*Sj3q6&9#&-7$Gs$tf  UA  B  ':	#S!"%I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_dimsr7      s.   F "eM55.B5	5 }ekk&7&78}ekk&8&89}ekk&<&<=. ++## 3[[
":.. 	3FAsvsAz2	33 ++$$ 4kk$[1/ 	4FAsvsA{3	44 	LLU#Lr   )r/   r   r0   dict[str, list[Any]]r1   r8   r$   r   )	
__future__r   typingr   onnx.checkerr,   r   r   r7    r   r   <module>r=      sA    #   +VV$V &V 	Vr   