
    g                     `    d dl Z d dlmZ d dlZd Zd Zdej                  j                  fdZy)    N)abcc                 J   fdg d}d}t        |       D ]  \  }}|j                  t        j                  j                  k(  rBt        |t        |            D ])  }|j                   d| }|dz  }||   }	| ||	      z  }+ p|j                  t        j                  j                  k(  sN|j                  t        j                  j                  k(  s'|j                  t        j                  j                  k(  rR|j                  }d }	||z  }d}
|t        |      k  r||   ||   }	n||v r||   ||   }	d}
 ||	      }|
s0||z  }7|j                  t        j                  j                  k(  s`|j                         D ]  \  }}	|vs ||	         S )Nc                 <   |yd}t        |t        j                        r&t        |      D ]  \  }}| |  d| |      z  } |S t        |t        j                        r+|j                         D ]  \  }}| |  d| |      z  } |S j                  |        y)z;Returns number of expanded inputs that _add_input processedr   _   )
isinstancer   Sequence	enumerateMappingitemsappend)nameinputnum_expanded_non_none_inputsivalkey
_add_inputinput_namess         ]/var/www/openai/venv/lib/python3.12/site-packages/onnxruntime/tools/pytorch_export_helpers.pyr   z1_parse_inputs_for_onnx_export.<locals>._add_input   s     ='($eS\\* $E*3 -
dV1QC=#0NN, + 0/s{{+ "KKMS,
dV1SE?C0PP, *
 0/ 	4      r   r   r   TF)r
   kindinspect	ParameterVAR_POSITIONALrangelenr   POSITIONAL_ONLYPOSITIONAL_OR_KEYWORDKEYWORD_ONLYVAR_KEYWORDr   )all_input_parametersinputskwargsvar_positional_idx'num_expanded_non_none_positional_inputs	input_idxinput_parameterargs_ir   inpis_positional"num_expanded_non_none_inputs_localr   r   s               @@r   _parse_inputs_for_onnx_exportr-   
   s   "H K./+&/0D&E"	?7#4#4#C#CC	3v;7)../q1C0DE"a'"Vn7:dC;PP7	 8   G$5$5$E$EE##w'8'8'N'NN##w'8'8'E'EE #''DC++I M3v;&6)+<+HY'F4L$<Tl %1;D#1F.7;]]7!!W%6%6%B%BB#\\^	c{*tS) ,9 'F@ r   c                     d }d }|D cg c]  } ||      r ||      n| }}|| D cg c]"  }||v s |||         r |||         n||   $ c}z  }|s|j                  i        t        |      S c c}w c c}w )z5Flatten args and kwargs in a single tuple of tensors.c                 :    t        |       t        t        t        hv S N)typeintboolfloatvalues    r   is_primitive_typez0_flatten_module_input.<locals>.is_primitive_type\   s    E{sD%000r   c                 ,    t        j                  |       S r0   )torchtensorr5   s    r   	to_tensorz(_flatten_module_input.<locals>.to_tensor_   s    ||E""r   )r   tuple)namesargsr$   r7   r;   argretr   s           r   _flatten_module_inputrA   X   s    1# IM
M.s39S><C
MafafY]jnrxjx#4VD\#B	&,tTaf C 

2: Ns   A5	A:A:modulec                     t        j                  | j                        j                  j	                         }t        |||      }t        |||      }||fS )a  
    Infer the input names and order from the arguments used to execute a PyTorch module for usage exporting
    the model via torch.onnx.export.
    Assumes model is on CPU. Use `module.to(torch.device('cpu'))` if it isn't.

    Example usage:
    input_names, inputs_as_tuple = infer_input_info(module, ...)
    torch.onnx.export(module, inputs_as_type, 'model.onnx', input_names=input_names, output_names=[...], ...)

    :param module: Module
    :param inputs: Positional inputs
    :param kwargs: Keyword argument inputs
    :return: Tuple of ordered input names and input values. These can be used directly with torch.onnx.export as the
            `input_names` and `inputs` arguments.
    )r   	signatureforward
parametersvaluesr-   rA   )rB   r#   r$   module_parametersr   inputs_as_tuples         r   infer_input_inforJ   o   sP       ))&..9DDKKM/0A66RK+KHO''r   )	r   collectionsr   r9   r-   rA   nnModulerJ    r   r   <module>rO      s0      K\.(UXX__ (r   