o
    e                  	   @   sf   d dl mZmZmZmZ d dlmZmZ d dlZdedee	ee f dee	ee f defdd	Z
dS )
    )AnyListDictSet)
ModelProtoValueInfoProtoNmodel
input_dimsoutput_dimsreturnc              
      s   t   dtt dtt ddfdd}| | jj | | jj | | jj dtdt	d	t
d
tddf
 fdd}| jjD ]}|j}|| }t|D ]\}}	|||	|| qIq<| jjD ]}
|
j}|| }t|D ]\}}	||
|	|| qgqZtj|  | S )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)
                and 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                 S   s:   |D ]}|j jj}|jD ]}|dr| |j qqd S )N	dim_param)typetensor_typeshapedimHasFieldaddr   )r   r   infor   r    r   LD:\Projects\ConvertPro\env\Lib\site-packages\onnx/tools/update_model_dims.pyinit_dim_param_set$   s   


z6update_inputs_outputs_dims.<locals>.init_dim_param_settensorr   jnamec                    s   | j jjj| }t|trD|dkr+|dr&|j|kr&td	||||j||_d S |d t
| }| v r?td	||||_d S t|t
rN||_d S tdt | )Nr   	dim_valuez_Unable to set dimension value to {} for axis {} of {}. Contradicts existing dimension value {}._zaUnable to generate unique dim_param for axis {} of {}. 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formatstrr   )r   r   r   r   Z	dim_protoZgenerated_dim_paramr   r   r   
update_dim/   s"   




z.update_inputs_outputs_dims.<locals>.update_dim)setr   r"   r   r   graphinputoutputZ
value_infor   r   r   	enumerateonnxcheckerZcheck_model)r   r	   r
   r   r$   r'   Z
input_nameZinput_dim_arrr   r   r(   Zoutput_nameZoutput_dim_arrr   r#   r   update_inputs_outputs_dims	   s(   "r,   )typingr   r   r   r   r*   r   r   Zonnx.checkerr"   r,   r   r   r   r   <module>   s   6