Ë
    ¨Êj8*  ã            	      óæ  — d Z ddlmZ ddlZddlmZ ddlZddlmZ  G d„ dej                  «      Z	 G d„ d	ej                  «      Z
d&d
„Zd'd„Z G d„ dej                  «      Z G d„ dej                  «      Z G d„ dej                  «      Z ej                   «       d(d)d„«       Zedk(  �rE ej&                  d«        edd¬«      Z ej*                  ddd«      Z ee«      Z ed ed„ ej5                  «       D «       «      d eej8                  «      «        ee ej*                  dd«      «      Z ed eej8                  «      «        ej*                  dd«      Zej?                  «       Z e ddxxx d  ej*                  d!d«      z  z  ccc  eee«       eee «      z
  jC                  «       j3                  d"«      Z" e#e"dkD  jI                  «       d   «      Z% ed#e%› d$de%z
  › d%�«       yy)*aP  VLAAI_ws: the weight-shared VLAAI stimulus-reconstruction network, as a single self-contained file.

Depends on PyTorch only (no einops, no pydantic, no SuperHuge package). Module names, parameter names and shapes are
identical to the network the experiments train (`decoders.Net("vlaai_ws", ...)`), so the saved weights in `models/*.pt`
and the `state_dict` of this file are interchangeable (checked: strict load in both directions, equal outputs).

Architecture (defaults = the configuration used in all experiments)
-------------------------------------------------------------------
input  x: (B, T, C)   EEG window, T = fs * window_s samples (160 for 5 s at 32 Hz), C = 58 channels
  1. proj      1x1 convolution C -> K (K = 64); this is the "patch" that makes weight sharing possible
  2. block     ONE block applied nb_blocks (4) times with the SAME weights:
                 for i in range(nb_blocks):
                     if use_skip and i > 0:  h = h + x_proj          # skip from the projected input
                     h = block(h)
               block = extractor -> dropout -> output_context -> linear mix (K -> K, with bias)
                 extractor:      3 x [ZeroPad right (k-1), Conv1d(K, K, k) with max-norm 2, LayerNorm([K, T]), ELU, Dropout]
                                 kernel sizes 3, 7, 11   (the 4th size of kernel_sizes=[3, 7, 11, 15] is never used:
                                 num_kernels=[K] is expanded to num_layers=3 and zip() stops at 3)
                 output_context: ZeroPad LEFT (16), Conv1d(K, K, 17) with max-norm 2, LayerNorm([K, T]), ELU, Dropout
  3. head      linear K -> F (no bias): one output channel per reconstruction target (env, onset, dom)
output y: (B, T, F), or (B, T) when F == 1

What is NOT in the network
--------------------------
* The network is NOT causal inside its window: the extractor pads on the right, so an output sample sees up to
  (2 + 6 + 10) = 18 samples of the future per block, 72 samples (2.25 s at 32 Hz) over the 4 blocks, limited only by
  the end of the window. Causality (the 1-second look-ahead rule) comes from HOW the output is read: `causal_reconstruct` below reads
  only the last hop-sized block that lies `future` samples before the window end, so every output sample uses at most
  `future + hop` = 24 samples (0.75 s at 32 Hz) of future EEG.
* The input length must equal fs * window_s: LayerNorm([K, T]) has a learnable (K, T) affine, so T is fixed by the weights.
* Loss, target construction, evidence (correlation of the reconstruction with each talker), the HMM and the fusion with
  the direct classifier are not here; see decoders.py, common_exp.py, score.py.
* The 10-s direct classifier is this backbone applied to the channel-concatenation [EEG (58), stream A (3), stream B (3)]
  with num_channels = 64, window_s = 10, nb_blocks = 3, followed by a (K -> 1) linear read-out (see direct_classifier.py).

Usage
-----
    model = VLAAIwsDecoder(num_channels=58, n_targets=3)           # 5-s window, 4 blocks, 64 filters, 3 targets
    y = model(torch.randn(8, 160, 58))                              # (8, 160, 3)
    sd = torch.load("models/pretrain_spk_vlaai_ws_s0.pt")           # head has 2 targets (env, onset)
    VLAAIwsDecoder(num_channels=58, n_targets=2).load_state_dict(sd)
    rec = causal_reconstruct(model, eeg_trial)                      # (T_trial, 3), <= 0.75 s look-ahead per sample
é    )ÚannotationsN)ÚSequence)Únnc                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )ÚMaxNormConv1dzÐConv1d whose filters are renormalised to L2 norm <= max_norm before every forward pass (as in the package).
    Note: like the original, the renormalisation overwrites weight.data in place, also in eval mode.c                óh   •— t         ‰| �  «        t        j                  |||«      | _        || _        y ©N)ÚsuperÚ__init__r   ÚConv1dÚ_convÚ	_max_norm)ÚselfÚin_channelsÚout_channelsÚkernel_sizeÚmax_normÚ	__class__s        €úc/root/disk_3tb/EEG-sAAD-Grand-Challenge-Training-Dataset/experiments/decoder_lodo/vlaai_ws_model.pyr   zMaxNormConv1d.__init__9   s)   ø€ Ü‰ÑÔÜ—Y‘Y˜{¨L¸+ÓFˆŒ
Ø!ˆ�ó    c                óð   — | j                   �Zt        j                  | j                  j                  j
                  dd| j                   ¬«      | j                  j                  _        | j                  |«      S )Né   r   )ÚpÚdimÚmaxnorm)r   ÚtorchÚrenormr   ÚweightÚdata©r   Úxs     r   ÚforwardzMaxNormConv1d.forward>   sS   € Ø�>‰>Ð%Ü%*§\¡\°$·*±*×2CÑ2C×2HÑ2HÈAÐSTÐ^b×^lÑ^lÔ%mˆD�J‰J×ÑÔ"Ø�z‰z˜!‹}Ðr   )g       @)r   Úintr   r#   r   r#   r   zfloat | None©r!   útorch.TensorÚreturnr%   ©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r"   Ú__classcell__©r   s   @r   r   r   5   s   ø„ ñhö"÷
r   r   c                  ó,   ‡ — e Zd ZdZdˆ fd„Zdd„Zˆ xZS )Ú
ChannelMixz|y[b, c, t] = sum_k x[b, k, t] W[k, c] + bias[c]  (an EinMix 'b k t -> b c t'; parameters `weight` (K, C), `bias` (1, C, 1)).c           	     óÐ  •— t         ‰| �  «        t        j                  t	        j
                  ||«      j                  t        j                  d|z  «       t        j                  d|z  «      «      «      | _	        t        j                  t	        j
                  d|d«      j                  t        j                  d|z  «       t        j                  d|z  «      «      «      | _
        y )Né   é   )r
   r   r   Ú	Parameterr   ÚemptyÚuniform_ÚmathÚsqrtr   Úbias©r   Úin_featuresÚout_featuresr   s      €r   r   zChannelMix.__init__G   sÓ   ø€ Ü‰ÑÔÜ—l‘l¤5§;¡;¨{¸LÓ#I×#RÑ#RÔTX×T]ÑT]Ð^_ÐbmÑ^mÓTnÐSnÔpt×pyÑpyÐz{ð  Jñ  {Jó  qKó  $Ló  MˆŒÜ—L‘L¤§¡¨Q°¸aÓ!@×!IÑ!IÌ4Ï9É9ÐUVÐYdÑUdÓKeÐJeÔgk×gpÑgpÐqrð  vAñ  rAó  hBó  "Có  Dˆ�	r   c                ó^   — t        j                  d|| j                  «      | j                  z   S )Nzbkt,kc->bct)r   Úeinsumr   r8   r    s     r   r"   zChannelMix.forwardL   s"   € Ü�|‰|˜M¨1¨d¯k©kÓ:¸T¿Y¹YÑFÐFr   ©r:   r#   r;   r#   r$   r'   r-   s   @r   r/   r/   D   s   ø„ ñ GõD÷
Gr   r/   c                ó’  — t        j                  «       }t        t        | |«      «      D ]š  \  }\  }}|j	                  t        j                  t        j
                  d|dz
  f«      t        |dk(  r|n| |dz
     ||«      t        j                  ||g«      t        j                  «       t        j                  |«      «      «       Œœ |S )Nr   r2   )
r   Ú
SequentialÚ	enumerateÚzipÚappendÚ	ZeroPad1dr   Ú	LayerNormÚELUÚDropout)	Únum_kernelsÚkernel_sizesr   Útime_dimÚdropoutÚlayersÚiÚn_outÚks	            r   Ú
_extractorrP   P   s¢   € Ü�]‰]‹_€FÜ"¤3 {°LÓ#AÖB‰ˆ‰:ˆE�1Ø�‰”b—m‘mÜ�L‰L˜!˜Q ™U˜Ó$Ü¨¨aª™+°[ÀÀQÁÑ5GÈÐPQÓRÜ�L‰L˜% Ð*Ó+Ü�F‰F‹HÜ�J‰J�wÓó
õ 	ð Cð €Mr   c           	     óî   — t        j                  t        j                  |dz
  df«      t        | | |«      t        j                  | |g«      t        j
                  «       t        j                  |«      «      S )Nr2   r   )r   r@   rD   r   rE   rF   rG   )Úchannelsr   rJ   rK   s       r   Ú_output_contextrS   ]   sZ   € Ü�=‰=Ü
�‰�k A‘o qÐ)Ó*Ü�h ¨+Ó6Ü
�‰�h Ð)Ó*Ü
�‰‹Ü
�
‰
�7Óóð r   c                  óF   ‡ — e Zd ZdZ	 	 	 d	 	 	 	 	 	 	 	 	 dˆ fd„Zdd„Zˆ xZS )ÚVLAAIwsBackbonez`proj (1x1 conv) + one weight-shared VLAAI block applied nb_blocks times. (B, T, C) -> (B, T, K).c
           
     ó¦  •— t         ‰
| �  «        t        t        ||z  «      «      | _        |||c| _        | _        | _        t        j                  ||d«      | _
        t        j                  t        |gt        |«      z  ||| j                  |	«      t        j                  |	«      t        ||| j                  |	«      t!        ||«      «      | _        y )Nr2   )r
   r   r#   ÚroundrJ   Ú	nb_blocksÚuse_skipr;   r   r   Úprojr@   rP   ÚlenrG   rS   r/   Úblock)r   Únum_channelsÚfsÚwindow_srX   rH   rI   Úoutput_context_kernel_sizerY   rK   r   s             €r   r   zVLAAIwsBackbone.__init__j   s¯   ø€ ô 	‰ÑÔÜœE " x¡-Ó0Ó1ˆŒØ;DÀhÐP[Ð8ˆŒ˜œ tÔ'8Ü—I‘I˜l¨K¸Ó;ˆŒ	Ü—]‘]Ü˜�}¤s¨<Ó'8Ñ8¸,ÈÐUY×UbÑUbÐdkÓlÜ�J‰J�wÓÜ˜KÐ)CÀTÇ]Á]ÐT[Ó\Ü�{ KÓ0ó	
ˆ�
r   c                óv  — |j                   d   | j                  k7  r(t        d|j                   d   › d| j                  › �«      ‚| j                  |j	                  dd«      «      }|}t        | j                  «      D ])  }| j                  r
|dkD  r||z   }| j                  |«      }Œ+ |j	                  dd«      S )Nr2   z
input has z$ samples, the network was built for r   r   )	ÚshaperJ   Ú
ValueErrorrZ   Ú	transposeÚrangerX   rY   r\   )r   r!   ÚhrM   s       r   r"   zVLAAIwsBackbone.forwardx   s¦   € Ø�7‰7�1‰:˜Ÿ™Ò&Ü˜z¨!¯'©'°!©*¨Ð5YÐZ^×ZgÑZgÐYhÐiÓjÐjØ�I‰I�a—k‘k ! QÓ'Ó(ˆØˆÜ�t—~‘~Ö&ˆAØ�}Š}  Q¢Ø˜‘E�Ø—
‘
˜1“‰Að 'ð �{‰{˜1˜aÓ Ð r   )	é:   é    g      @é   é@   )r1   é   é   é   Tg        )r]   r#   r^   r#   r_   ÚfloatrX   r#   rH   r#   rI   úSequence[int]r`   r#   rY   ÚboolrK   rn   r$   r'   r-   s   @r   rU   rU   g   sA   ø„ ÙjàefØtvØ9<ð
Ø!ð
Ø6Cð
Ønqð
àð
à16õ
÷	!r   rU   c                  ó,   ‡ — e Zd ZdZdˆ fd„Zdd„Zˆ xZS )Ú
LinearHeadzKy[b, t, f] = sum_k h[b, t, k] W[k, f] (no bias; parameter `weight` (K, F)).c           	     óø   •— t         ‰| �  «        t        j                  t	        j
                  ||«      j                  t        j                  d|z  «       t        j                  d|z  «      «      «      | _	        y )Nr1   )
r
   r   r   r3   r   r4   r5   r6   r7   r   r9   s      €r   r   zLinearHead.__init__‡   sq   ø€ Ü‰ÑÔÜ—l‘l¤5§;¡;¨{¸LÓ#I×#RÑ#RÔTX×T]ÑT]Ð^_ÐbmÑ^mÓTnÐSnÔpt×pyÑpyÐz{ð  Jñ  {Jó  qKó  $Ló  Mˆ�r   c                óD   — t        j                  d|| j                  «      S )Nzbtk,kf->btf)r   r=   r   )r   rf   s     r   r"   zLinearHead.forward‹   s   € Ü�|‰|˜M¨1¨d¯k©kÓ:Ð:r   r>   )rf   r%   r&   r%   r'   r-   s   @r   rr   rr   „   s   ø„ ÙUõM÷;r   rr   c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )ÚVLAAIwsDecoderzµBackbone + linear head: the stimulus-reconstruction decoder used in all experiments.

    forward(x): x (B, T, C) float EEG -> (B, T, n_targets), or (B, T) when n_targets == 1.
    c                óž   •— t         ‰| �  «        t        dd|i|¤Ž| _        t	        | j                  j
                  |«      | _        || _        y )Nr]   © )r
   r   rU   Úbackbonerr   r;   ÚheadÚ	n_targets)r   r]   r{   Úbackbone_kwargsr   s       €r   r   zVLAAIwsDecoder.__init__•   sA   ø€ Ü‰ÑÔÜ'ÑU°\ÐUÀ_ÑUˆŒÜ˜tŸ}™}×9Ñ9¸9ÓEˆŒ	Ø"ˆ�r   c                ón   — | j                  | j                  |«      «      }| j                  dk(  r|d   S |S )Nr2   ).r   )rz   ry   r{   )r   r!   Úys      r   r"   zVLAAIwsDecoder.forward›   s3   € Ø�I‰I�d—m‘m AÓ&Ó'ˆØ ŸN™N¨aÒ/ˆq�‰yÐ6°QÐ6r   )rg   r1   )r]   r#   r{   r#   r$   r'   r-   s   @r   rv   rv   �   s   ø„ ñö
#÷7r   rv   c           
     óP  — | j                   j                  }||z
  |z
  }|j                  \  }}t        | j	                  «       «      j
                  }	t        j                  |j                  ||«      ||j                  ||z   |«      g«      j                  |	t        j                  «      }
t        j                  d||«      }g }| j                  «        t        dt        |«      |«      D ]‘  }||||z    }t        j                  |j!                  «       D �cg c]
  }|
|||z    ‘Œ c}«      }|j#                    | |«      dd…|||z   …f   j$                  dg| j&                  dk(  rdn| j&                  f¢­Ž «       Œ“ t        j                  |«      d| j)                  «       S c c}w )a  Reconstruction of a whole trial with at most `future + hop` samples of look-ahead per output sample.

    The trial is zero-padded and cut into windows of T samples every `hop` samples; from each window only the
    block [T - future - hop, T - future) is kept. With T = 160, hop = 8, future = 16 (32 Hz): every output sample sees at
    most 24 samples = 0.75 s of future EEG, and a 1-s evidence window ending at t needs EEG only up to t + 0.5 s.
    eeg: (N, C) -> returns (N, n_targets) (or (N,) for one target).
    r   Néÿÿÿÿr2   rx   )ry   rJ   rb   ÚnextÚ
parametersÚdevicer   ÚcatÚ	new_zerosÚtoÚfloat32ÚarangeÚevalre   r[   ÚstackÚtolistrC   Úreshaper{   Úcpu)ÚmodelÚeegÚhopÚfutureÚbatchÚTÚpos0ÚnÚCÚdevr!   ÚstartsÚoutrM   ÚsÚaÚws                    r   Úcausal_reconstructr�       sq  € ð 	�‰×Ñ€AØˆv‰:˜Ñ€DØ�9‰9�D€A€qÜ
ˆu×ÑÓ!Ó
"×
)Ñ
)€CÜ�	‰	�3—=‘=  qÓ)¨3°·±¸fÀs¹lÈAÓ0NÐOÓP×SÑSÐTWÔY^×YfÑYfÓg€AÜ�\‰\˜!˜Q Ó$€FØ
€CØ	‡J�J„LÜ�1”c˜&“k 5Ö)ˆØ�1�Q˜‘YÐˆÜ�K‰K¨Q¯X©X¬ZÓ8©Z¨˜˜1˜Q ™Uš¨ZÑ8Ó9ˆØ�
‰
Ð7‘5˜“8šA˜t D¨3¡J˜Ð.Ñ/×7Ñ7¸ÐrÀEÇOÁOÐWXÒDX¹bÐ_d×_nÑ_nÐ^pÒrÕsð *ô �9‰9�S‹>˜"˜1Ð×!Ñ!Ó#Ð#ùò 9s   ÄF#
Ú__main__rg   r1   )r]   r{   ri   é    zparameters:c              #  ó<   K  — | ]  }|j                  «       –— Œ y ­wr	   )Únumel)Ú.0r   s     r   Ú	<genexpr>r£   ½   s   è ø€ Ð?±¨1˜QŸW™WŸY±ùs   ‚z	| output:iè  zcausal_reconstruct:iX  i�  é   éÈ   r€   zfirst changed output sample z2 for a perturbation starting at 400 -> look-ahead z samples (<= 24))rH   ro   rI   ro   r   r#   rJ   r#   rK   rn   r&   únn.Sequential)
rR   r#   r   r#   rJ   r#   rK   rn   r&   r¦   )é   é   i   )rŽ   rv   r�   r%   r�   r#   r‘   r#   r’   r#   r&   r%   )&r+   Ú
__future__r   r6   Útypingr   r   r   ÚModuler   r/   rP   rS   rU   rr   rv   Úno_gradr�   r(   Úmanual_seedÚmÚrandnr!   r~   ÚprintÚsumr‚   Útuplerb   ÚrecÚe0ÚcloneÚe1ÚabsÚdr#   ÚnonzeroÚfirstrx   r   r   Ú<module>r»      sÞ  ðñ*õV #ã Ý ã Ý ô�B—I‘Iô ô	G�—‘ô 	Gó
óô!�b—i‘iô !ô:;�—‘ô ;ô7�R—Y‘Yô 7ð" €‡�ƒó$ó ð$ð. ˆzÓØ€E×Ñ�aÔÙ B°!Ô4€AØˆ�‰�A�s˜BÓ€AÙ	ˆ!‹€AÙ	ˆ-™Ñ?°·±´Ó?Ó?ÀÉeÐTU×T[ÑT[ËnÔ]Ù
˜Q  §¡¨D°"Ó 5Ó
6€CÙ	Ð
¡ s§y¡yÓ!1Ô2à	ˆ�‰�S˜"Ó	€BØ	�‰‹€BØ€s€tƒH��K�E—K‘K  RÓ(Ñ(Ñ(ƒHÙ	˜A˜rÓ	"Ñ%7¸¸2Ó%>Ñ	>×CÑCÓE×IÑIÈ"ÓM€AÙ��Q‘—‘Ó! !Ñ$Ó%€EÙ	Ð(¨¨Ð/aÐbeÐhmÑbmÐanÐn~Ð
õ  Að r   