
    2Vh*                     Z    d dl Z d dlZd dlZd dlZd dlZ ej                         d        Zy)    Nc                      ddl m ddl m ddlm t        j                  j                        fd} dfd	}| j                  _	        |j                  j                  _        y)	z<Patches `tf2onnx` to ensure compatibility with numpy>=2.0.0.r   )AttributeProto)TensorProto)tf2onnxc                 <   t         j                  t         j                  t         j                  t         j                  t         j
                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  d}t        |      }d}|r$d}t        |      D ]  \  }}|j                  |j                        }|%t!        |j"                        t!        | j$                        z  rQ	 g }|j&                  D ]5  }|j)                         s n#|j+                  |j-                  d             7 j/                  d|j0                  t3        |j4                        t3        |             |r;t3        |j4                        t3        |      k(  rj7                  d|j                  d|j0                         |j                  d	k(  rB|j9                  d
      }	j:                  j=                  |	      }
t        j                  |d|
i}n|j                  dk(  r|d   }|dd } |t?        |      |      }n|j                  dk(  rC|j9                  d      }j:                  j=                  |      }
 || }|jA                  |
      }ns|j                  dv r|j9                  d      } |||      }nI|j                  dk(  r5|j9                  d      }j:                  j=                  |      }
 ||d|
i}n || }j:                  jC                  |j0                        }|}|j"                  d   }|j0                  }j/                  d||       | jE                  ||      ||<   j/                  d||       | jG                  |      }|r|D ]  }| jI                  |||        d} |r$|S # tJ        $ r2}tM        jN                         }j7                  d||       Y d}~Od}~ww xY w)a  
        We call tensorflow transform with constant folding but in some cases
        tensorflow does fold all constants. Since there are a bunch of ops in
        onnx that use attributes where tensorflow has dynamic inputs, we badly
        want constant folding to work. For cases where tensorflow missed
        something, make another pass over the graph and fix want we care about.
        )AddGreaterEqualCastConcatV2LessListDiffMulPackRangeSqrtSubTFN)as_listzop name %s, %s, %szfolding node type=z, name=r
   todtyper   r   out_idx)r   axis)r   r   Tidxr   z%create const node [%s] replacing [%s]z,replace old output [%s] with new output [%s]zexception: %s, details: %s)(npaddgreater_equalasarrayconcatenateless	setdiff1dmultiplystackarangesqrtsubtractlist	enumerategettypesetoutputoutputsinputsis_constappendget_tensor_valuedebugnameleninputinfoget_attr_intutilsmap_onnx_to_numpy_typetupleastype	make_name
make_constfind_output_consumersreplace_input	Exception	traceback
format_exc)gopsfunc_mapkeep_lookingidxopfuncr-   nodedstnp_typevalr   valuesout_typer   new_node_namenew_output_nameold_output_nameold_node_name	consumersconsumerextbloggerr   s                           L/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/export/tf2onnx_lib.pypatched_rewrite_constant_foldz4patch_tf2onnx.<locals>.patched_rewrite_constant_fold   s    66,,JJGG;;HHYYGG;;
 3i L$S> RFR||BGG,<ryy>C		N2LFF "		 L#}}!d&;&;E&;&JKL
 LL,BHHF	 #bhh-3v;">?AwwP 77f,"$//$"7C&-mm&J&J3&OG"$**f"DG"DCWW
2#)":D%+CR[F"&uV}d";CWW
2')y'AH&-mm&J&J ('G #'-C"%**W"5CWW0#%??6#:D"&vD"9CWW/$&OOF$;E&-mm&J&J %'G #'">g">C"&-C(/(?(?(H*7*,))A,(*C))
 $%<<s#CCJ++ %&$;$;O$L	$,5 " !$,o!"" (,_RF n 
 ! F"--/BKK <b"EEFs   J>O  	P)'PPNc                 :   | j                   d   }|| |j                  v r|j                  |    S ||j                  j                  k7  r|S d } ||j                  j
                        |j                  kD  r!t        j                  |      }| j                  j                         dz   t        |j                        z   }dD ]  }|j                  |d      } |xj                  dz  c_        |j                  j                  |j                  |<   ||j                  | <   d|j                  _        |j                  j!                  d       |j                  j"                  j%                         }d|_        ||_        j*                  |j                  _        |S )	z
        Return onnx attr for value property of node.
        Attr is modified to point to external tensor data stored in
        external_tensor_storage, if included.
        valuec                 v    t        t        d      rt        j                  |       S t        j                  |       S )Nproduct)hasattrr   r]   prod)xs    rX   r_   z;patch_tf2onnx.<locals>.patched_get_value_attr.<locals>.prod   s(    r9%zz!}$wwqz!    _z~"#%&*:<>?/\{|}   ra   raw_datalocation)_attrnode_to_modified_value_attrr)   TENSORtdimsexternal_tensor_size_thresholdcopydeepcopyr2   stripstrname_counterreplacerd   name_to_tensor_data
ClearFieldexternal_datar   keyr[   EXTERNALdata_location)	selfexternal_tensor_storagear_   tensor_namecre   r   r   s	          rX   patched_get_value_attrz-patch_tf2onnx.<locals>.patched_get_value_attr   su    JJw#//KKK*FFtLL"*aff8M8M.MH	" N%DDE a A		!-::;< 
 ( :)11!S9:#00A50 $77D IJ#??EACCLCCNN:&ss((,,.H%HL(HN + 4 4ACCra   )N)onnxr   r   keras.src.utils.module_utilsr   logging	getLogger__name__tfonnxrewrite_constant_foldgraphNodeget_value_attr)rY   r}   r   r   rW   r   s     @@@@rX   patch_tf2onnxr   	   sR     $ 4w//0Fob,\ ,IGNN((>GMM%ra   )rl   	functoolsr   r@   numpyr   	lru_cacher    ra   rX   <module>r      s5         j? j?ra   