
    hl)                         S SK r S SKrS SKJr  S SKJrJrJrJr  S SK	r	S SK
r
SSKJrJr  SSKJrJr  SSKJr  SSKJr  SS	KJrJr  SS
KJr  SSKJr  \	R8                  " SSSS9rS r " S S\5      rg)    N)BytesIO)AnyDictListOptional   )Opsget_current_ops)cupyh5py)
tensorflow)	Optimizer)
ArgsKwargsArrayXd)get_array_module   )ShimthinckerasT)entry_pointsc                     U R                  5         U $ ! [        [        4 a     Of = fS H  n[        X5      (       a  M  [	        S5      e   [        5       nUR                  S:X  a  SnO[        R                  R                  5       nU R                  n[        R                  " U5         U R                  " S0 UD6  U R                  U R                  5        U R                  U R                   5        [        U S5      (       a  U R#                  5         OU R%                  5         SSS5        U $ ! , (       d  f       U $ = f)zCall the required predict/compile/build APIs to initialize a model if it
is a subclass of tf.keras.Model. This is required to be able to call set_weights
on subclassed layers.)catalogue_nameeg_xeg_yeg_shapeaB  Keras subclassed models are not whole-model serializable by TensorFlow. To work around this, you must decorate your keras model subclasses with the 'keras_subclass' decorator. The decorator requires a single X/Y input of fake-data that can be used to initialize your subclass model properly when loading the saved version.cpuCPU_make_train_functionN )
get_configAttributeErrorNotImplementedErrorhasattr
ValueErrorr
   device_typetftestgpu_device_name
eg_compiledevicecompilebuildr   predictr   r   make_train_function)keras_model	prop_nameopsr*   compile_argss        P/home/james-whalen/.local/lib/python3.13/site-packages/thinc/shims/tensorflow.pymaybe_handshake_modelr4      s    /0  D	{..O  D  C
%((*))L	6	+l++../K,,-; 677,,.++- 
  
	 s    ''+A;D00
D?c                     ^  \ rS rSr% Sr\\S      \S'   SS\S\4U 4S jjjr	S r
S	\S
\4S jrS	\4S jrS	\4S jrS\4S jr SS\\\\4      4S jjrS r\R0                  S 5       rS rS rS\S\4S jrS rS rSr U =r!$ )TensorFlowShim?   zInterface between a TensorFlow model and a Thinc Model. This container is
*not* a Thinc Model subclass itself.

Reference for custom training:
https://www.tensorflow.org/tutorials/customization/custom_training_walkthrough
z	tf.Tensor	gradientsmodel	optimizerc                 4   > [         TU ]  XU5        S U l        g N)super__init__r8   )selfr9   configr:   	__class__s       r3   r>   TensorFlowShim.__init__I   s    	2    c                 x   ^ / mS[         4U4S jjnU R                  R                  US9  SR                  T5      $ )Nlinec                 (   > TR                  U 5        g r<   )append)rE   liness    r3   
accumulate*TensorFlowShim.__str__.<locals>.accumulateP   s    LLrC   )print_fn
)str_modelsummaryjoin)r?   rI   rH   s     @r3   __str__TensorFlowShim.__str__M   s:    	S 	 	Z0yyrC   Xis_trainc                 T    U(       a  U R                  U5      $ U R                  U5      $ r<   )begin_updater-   )r?   rS   rT   s      r3   __call__TensorFlowShim.__call__V   s$    $$Q''<<?"rC   c                 F   [         R                  R                  R                  5       n[         R                  R                  R	                  S5        U R
                  " UR                  0 UR                  D6n[         R                  R                  R	                  U5        U$ )Nr   )r&   r   backendlearning_phaseset_learning_phaserN   argskwargs)r?   rS   	old_phaseYs       r3   r-   TensorFlowShim.predict\   sj    HH$$335	
++A.KK,188,
++I6rC   c                 B  ^ ^^^ [         R                  R                  R                  S5        [         R                  " 5       mTR                  5         TR                  TR                  5        T R                  " TR                  0 TR                  D6mUUU U4S jnTU4$ )Nr   c                   > TR                  S S S 5        [        TR                  5      S:X  a  TR                  S   /nO[        TR                  S   5      nUR	                  T
R
                  R                  5        TR                  T	XS9nUS [        TR                  5       nUSS  nT
R                  bZ  [        U5      [        T
R                  5      :X  d   S5       e[        T
R                  U5       H  u  pVUR                  U5        M     O,U Vs/ s H  n[        R                  " U5      PM     snT
l        [        [        U5      0 S9$ s  snf )Nr   r   )output_gradientszgradients must match)r]   r^   )__exit__lenr]   listextendrN   trainable_variablesgradientr8   zip
assign_addr&   Variabler   tuple)d_outputwrt_tensorsall_gradientsdX	opt_gradsvariablenew_variablefrS   outputr?   tapes           r3   backprop-TensorFlowShim.begin_update.<locals>.backpropj   s   MM$d+166{a vvayk"166!9ot{{>>? MM * M }QVV-B%ab)I~~)9~T^^)<<T>TT<.1$..).L*H''5 /M ;D!D)Q"++a.)!D59R88 "Es    E)
r&   r   rZ   r\   GradientTape	__enter__watchr]   rN   r^   )r?   rS   ry   rw   rx   s   `` @@r3   rV   TensorFlowShim.begin_updatec   ss    
++A. 

166aff11	9 	92 xrC   c                    U R                   c  [        S5      e[        U R                   5      [        U R                  R                  5      :X  d   e/ n/ n/ n[        U R                   U R                  R                  5       H  u  pVUR                  5       nUR                  5       nUR                  UR                  UR                  45        UR                  UR                  5       5        UR                  UR                  5       5        M     [        US   5      nU" U R                  S4UR                  U5      UR                  U5      5      u  pSn[        U R                   U R                  R                  5       HB  u  pVUR                  S5      u  pXX-    R                  U5      nUR!                  U5        X-  nMD     S U l         g )NzcThere are no gradients for optimization. Be sure to call begin_update before calling finish_update.r   ztensorflow-shim)r8   r$   rf   rN   ri   rk   numpyrG   sizeshaperavelr   idconcatenatepopreshapeassign)r?   r:   paramsgradsshapesgradrt   paramxpflat_params
flat_gradsstartr   r   s                 r3   finish_updateTensorFlowShim.finish_update   sy   >>!1  4>>"c$++*I*I&JJJJ !$..$++2Q2QRNDNN$E::<DMM5::u{{34MM%++-(LL& S fQi("+WW'("..*@"..QVBW#
 !$..$++2Q2QRND **Q-KD5==eDEOOE"ME	 S
 rC   
state_dictc                     Uc  U R                  5       nU R                  R                   HF  n/ nUR                   H   nUR	                  XR
                     5        M"     UR                  U5        MH     g r<   )_create_state_dictrN   layersweightsrG   nameset_weights)r?   r   layercurrent_layer_weightsweights        r3   _load_weights_from_state_dict,TensorFlowShim._load_weights_from_state_dict   sd     002J[[''E$&!--%,,Z-DE (34	 (rC   c                     0 nU R                   R                   H2  nUR                   H  nUR                  5       XR                  '   M!     M4     U$ r<   )rN   r   r   r   r   )r?   r   r   r   s       r3   r   !TensorFlowShim._create_state_dict   sC    
[[''E--*0,,.
;;' ( ( rC   c              #     #    SU R                    S3n0 nUR                  5        H  u  pE[        US5      (       d  M  UR                  U5      (       d  M0  [        c"  [        U[        R                  5      (       d   eOj[        U[        R                  R                  R                  5      (       a  [        R                  " U5      n[        U[        R                  5      (       d   eXSUR                  US5      '   M     U(       a7  U R                  5       nU R                  U5        S v   U R                  U5        g S v   g 7f)Ntensorflow__
startswith )r   itemsr#   r   r   
isinstancer   ndarraycoreasnumpyreplacer   r   )r?   r   
key_prefixr   kvbackups          r3   
use_paramsTensorFlowShim.use_params   s     "477)1-

LLNDAq,''ALL,D,D<%a7777!!TYY^^%;%;<< LLO%a777789199Z45 # ,,.F..z:..v6s   7EEC.Ec                    U R                   R                  5       n[        R                  R                  R                  5         [        R                  R                  R                  U5      U l         U R                  5         g)zsimilar to tf.keras.models.clone_model()
But the tf.keras.models.clone_model changes the names of tf.Variables.
This method even preserves that
N)	rN   to_jsonr&   r   rZ   clear_sessionmodelsmodel_from_jsonr   )r?   model_json_configs     r3   _clone_modelTensorFlowShim._clone_model   sT    
 !KK//1
&&(hhoo556GH**,rC   c                 @   U R                   R                  5       nS U l         [        R                  R                  R                  5         [        R                  " U 5      n[        R                  R                  R                  U5      Ul         UR                  5         U$ r<   )rN   r   r&   r   rZ   r   copydeepcopyr   r   r   )r?   r   copieds      r3   r   TensorFlowShim.copy   sl     KK//1
&&(t$778IJ,,.rC   r%   	device_idc                 >   US:X  a0  [         R                  " S5         U R                  5         S S S 5        g US:X  a?  [         R                  " SR                  U5      5         U R                  5         S S S 5        g g ! , (       d  f       g = f! , (       d  f       g = f)Nr   z/CPUgpuz/GPU:{})r&   r*   r   format)r?   r%   r   s      r3   	to_deviceTensorFlowShim.to_device   st    %6"!!# #"E!9++I67!!# 87 " #" 87s   A="B=
B
Bc                    [        5       n [        R                  " US5       nU R                  R	                  USS9  S S S 5        UR                  5       $ ! , (       d  f       N= f! [         a)    [        U R                  S5      (       d  [        S5      e Of = f[        R                  U R                  R                  5        U R                  R                  U R                  R                  5       4$ )Nwh5)save_formatr   zXCouldn't serialize to h5, and model has no factory function for component serialization.)r   r   FilerN   savegetvaluer"   r#   r$   keras_model_fnsgetr   get_weights)r?   filelikerv   s      r3   to_bytesTensorFlowShim.to_bytes   s    9		8S)Q   5 *$$&& *) # 	4;;(899 <  :	 	DKK667{{))4;;+B+B+DDDs'   A' AA' 
A$ A' '0BBc                 Z   [        5       nUR                  S:X  a  SnO[        R                  R	                  5       n[        U[        [        45      (       a  [        R                  R                  R                  5         [        U5      nUR                  S5        [        R                  " US5       n[        R                  " U5         [        R                  R                   R#                  U5      U l        S S S 5         S S S 5        g Uu  pgU R$                  c  [&        R)                  U5      n[        R                  R                  R                  5         [        R                  " U5         [+        U R$                  S5      (       a3  U R$                  R,                  n	U" U	R.                  0 U	R0                  D6n
OU" 5       n
S S S 5        [3        W
5      U l        U R$                  R7                  U5        g ! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       N^= f)Nr   r   r   reg_args)r
   r%   r&   r'   r(   r   rM   bytesr   rZ   r   r   seekr   r   r*   r   
load_modelrN   r   r   r#   r   r]   r^   r4   _model_initializedr   )r?   datar1   r*   r   rv   r   model_weightsmodel_fnak	new_models              r3   
from_bytesTensorFlowShim.from_bytes   sv   "$??e#FWW,,.F dS%L))HH**,t}HMM!8S)QYYv&"$((//"<"<Q"?DK ' *)
 )-%;;&**>:HHH**,6"4;;	22%)[[%8%8B ("'' ?RYY ?I (
I # '<I&FD#. '& *) #"s1   2H
	/G88	H
.AH8
H	H


H
H*)rN   r   r8   )NNr<   )"__name__
__module____qualname____firstlineno____doc__r   r   __annotations__r   r>   rQ   r   boolrW   r-   rV   r   r   r   rM   r   r   r   
contextlibcontextmanagerr   r   r   intr   r   r   __static_attributes____classcell__)rA   s   @r3   r6   r6   ?   s     [)**c 3   #* # #   j   Dy @ :>	5"4W#56	5  *-$S $S $E / /rC   r6   ) r   r   ior   typingr   r   r   r   	cataloguer   backendsr	   r
   compatr   r   r   r&   
optimizersr   typesr   r   utilr   shimr   creater   r4   r6   r   rC   r3   <module>r      sW       , ,   +  % " ' # ""7G$G'TV/T V/rC   