
    g&                     V    d dl Z 	 d dlZdZddlmZmZ d	dedefdZ	y# e$ r dZY w xY w)
    NT   )$GloballyAllowedTypesOpTypeImplFilterOperatorTypeUsageManagerFconfig_fileenable_type_reductionc                    t         j                  j                  |       st        d|  d      xr t        i }dr
t               nd}d}dfd}t        |       5 }|D cg c]  }|j                          c}D ]  } ||      rd |j                  d      D        \  }	}
}|
j                  d      D cg c]  }t        |       }}d	|v r&d
}t               }d}t        |      }||k  r3|j                  d|      }|j                  d	|      }|dk(  r|}d|cxk  r|k  rn n||| j                         }|j                  |       |dz   }d}|dkD  r0||k  r+||   d	k(  r|dz  }n||   dk(  r|dz  }|dz  }|dkD  r||k  r+|dk7  rt        d||d z         |r||| }|j                  |	||       |dz   }n0|dk7  r|n|}|j                  ||| j                                |dz   }||k  rn,|j                  d      D ch c]  }|j                          }}|D ]5  }|	|vr||i||	<   |||	   vr	|||	   |<   ||	   |   j!                  |       7  	 ddd       t        |      dk(  rrd}d}r6|sd}|t        d      t#              }||fS ||j%                         }||fS c c}w c c}w c c}w # 1 sw Y   hxY w)a  
    Parse the configuration file and return the required operators dictionary and an
    OpTypeImplFilterInterface instance.

    Configuration file lines can do the following:
    1. specify required operators
    2. specify globally allowed types for all operators
    3. specify what it means for no required operators to be specified

    1. Specifying required operators

    The basic format for specifying required operators is `domain;opset1,opset2;op1,op2...`
    e.g. `ai.onnx;11;Add,Cast,Clip,... for a single opset
         `ai.onnx;11,12;Add,Cast,Clip,... for multiple opsets

         note: Configuration information is accrued as the file is parsed. If an operator requires support from multiple
         opsets that can be done with one entry for each opset, or one entry with multiple opsets in it.

    If the configuration file is generated from ORT format models it may optionally contain JSON for per-operator
    type reduction. The required types are generally listed per input and/or output of the operator.
    The type information is in a map, with 'inputs' and 'outputs' keys. The value for 'inputs' or 'outputs' is a map
    between the index number of the input/output and the required list of types.

    For example, both the input and output types are relevant to ai.onnx:Cast.
    Type information for input 0 and output 0 could look like this:
        `{"inputs": {"0": ["float", "int32_t"]}, "outputs": {"0": ["float", "int64_t"]}}`

    which is added directly after the operator name in the configuration file.
    e.g.
        `ai.onnx;12;Add,Cast{"inputs": {"0": ["float", "int32_t"]}, "outputs": {"0": ["float", "int64_t"]}},Concat`

    If for example the types of inputs 0 and 1 were important, the entry may look like this (e.g. ai.onnx:Gather):
        `{"inputs": {"0": ["float", "int32_t"], "1": ["int32_t"]}}`

    Finally some operators do non-standard things and store their type information under a 'custom' key.
    ai.onnx.OneHot is an example of this, where the three input types are combined into a triple.
        `{"custom": [["float", "int64_t", "int64_t"], ["int64_t", "std::string", "int64_t"]]}`

    2. Specifying globally allowed types for all operators

    The format for specifying globally allowed types for all operators is:
        `!globally_allowed_types;T0,T1,...`

    Ti should be a C++ scalar type supported by ONNX and ORT.
    At most one globally allowed types specification is allowed.

    Specifying per-operator type information and specifying globally allowed types are mutually exclusive - it is an
    error to specify both.

    3. Specify what it means for no required operators to be specified

    By default, if no required operators are specified, NO operators are required.

    With the following line, if no required operators are specified, ALL operators are required:
        `!no_ops_specified_means_all_ops_are_required`

    :param config_file: Configuration file to parse
    :param enable_type_reduction: Set to True to use the type information in the config.
                                  If False the type information will be ignored.
                                  If the flatbuffers module is unavailable type information will be ignored as the
                                  type-based filtering has a dependency on the ORT flatbuffers schema.
    :return: required_ops: Dictionary of domain:opset:[ops] for required operators. If None, all operators are
                           required.
             op_type_impl_filter: OpTypeImplFilterInterface instance if type reduction is enabled, the flatbuffers
                                  module is available, and type reduction information is present. None otherwise.
    zConfiguration file z does not existFNc                    | r| j                  d      ry| j                  d      rOrLt        d      | j                  d      d   j                  d      D ch c]  }|j                          c}y| dk(  rdyy	c c}w )
N#Tz!globally_allowed_types;z.Globally allowed types were already specified.;r   ,z,!no_ops_specified_means_all_ops_are_requiredF)
startswithRuntimeErrorsplitstrip)linesegmentr   globally_allowed_types+no_ops_specified_means_all_ops_are_requireds     b/var/www/openai/venv/lib/python3.12/site-packages/onnxruntime/tools/reduced_build_config_parser.pyprocess_non_op_linez)parse_config.<locals>.process_non_op_line`   s    ts+??56$)5&'WXXIMTWYZI[IaIabeIf)gIfg'--/If)g&AA:>7 *hs   A?c              3   <   K   | ]  }|j                           y w)N)r   ).0r   s     r   	<genexpr>zparse_config.<locals>.<genexpr>x   s     /_Gs   r   r   {Tr   r   }z#Mismatched { and } in type string: zYSpecifying globally allowed types and per-op type reduction info together is unsupported.)ospathisfile
ValueErrorhave_flatbuffersr   openr   r   intsetlenfindaddr   restore_from_config_entryupdater   make_op_type_impl_filter)r   r   required_opsop_type_usage_managerhas_op_type_reduction_infor   config	orig_liner   domain	opset_stroperators_strsopsets	operatorscurend
next_commanext_open_braceoperatorinum_open_bracestype_strend_stropopsetop_type_impl_filterr   r   s    `                         @@r   parse_configrB      s   H 77>>+&.{m?KLL 2F6FL27/:O46UY!&!& 
k	f8>?9Y__&?D"4(/_tzzZ]/_,FI}&/ooc&:;&:c!f&:F; m#-1*  E	-(Ci!.!3!3C!=J&3&8&8c&BO!R'%(
 ?7Z7#0_#E#K#K#M!h/ ,a/*+-1a#g,Q/36 /1 4!.q!1S!8 /1 4FA .1a#g +a/"./TWdeteuWv/v"ww0'4_Q'GH1KKFT\^fg!e 1;b0@*c!mC&@&F&F&HI%kQ CiV 3@2E2Ec2JK2JBRXXZ2J	K-,19+=L(,v"662;L(/ (/66yA  { @ 
L <A"M)$(!!-2G2Sk  "-"FG]"^ ,,, #."7"P"P"R,,,m @
 <l Ly 
	sD   $K)K  ?K?KB>KA+K=KK
(?K KK)F)
r   flatbuffersr!   ort_format_modelr   r   ImportErrorstrboolrB        r   <module>rJ      sH    
`
z-c z-$ z-	  s    ((