ó
    ¨Êj8*  ã            	      óê  • S r SSKJr  SSKrSSKJr  SSKrSSKJr   " S S\R                  5      r	 " S S	\R                  5      r
S&S
 jrS'S jr " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r\R                   " 5       S(S)S jj5       r\S:X  GaB  \R&                  " S5        \" SSS9r\R*                  " SSS5      r\" \5      r\" S\" S \R5                  5        5       5      S\" \R8                  5      5        \" \\R*                  " SS5      5      r\" S\" \R8                  5      5        \R*                  " SS5      r\R?                  5       r \ SS=== S \R*                  " S!S5      -  -  sss& \" \\5      \" \\ 5      -
  RC                  5       R3                  S"5      r"\#" \"S:„  RI                  5       S   5      r%\" S#\% S$S\%-
   S%35        gg)*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                  ó>   ^ • \ rS rSrSrSSU 4S jjjrSS jrSrU =r$ )	ÚMaxNormConv1dé5   zÌ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                óf   >• [         TU ]  5         [        R                  " XU5      U l        X@l        g ©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   ÚMaxNormConv1d.__init__9   s%   ø€ Ü‰ÑÔÜ—Y’Y˜{¸+ÓFˆŒ
Ø!�ó    c                óî   • U R                   bX  [        R                  " U R                  R                  R
                  SSU R                   S9U R                  R                  l        U R                  U5      $ )Né   r   )ÚpÚdimÚmaxnorm)r   ÚtorchÚrenormr   ÚweightÚdata©r   Úxs     r   ÚforwardÚMaxNormConv1d.forward>   sS   € Ø�>‰>Ñ%Ü%*§\¢\°$·*±*×2CÑ2C×2HÑ2HÈAÐSTÐ^b×^lÑ^lÑ%mˆD�J‰J×ÑÔ"Ø�z‰z˜!‹}Ðr   )r   r   )g       @)r   Úintr   r&   r   r&   r   zfloat | None©r#   útorch.TensorÚreturnr(   ©	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r$   Ú__static_attributes__Ú__classcell__©r   s   @r   r   r   5   s   ø† ñh÷"ñ "÷
ò r   r   c                  ó:   ^ • \ rS rSrSrSU 4S jjrSS jrSrU =r$ )Ú
ChannelMixéD   z|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U ]  5         [        R                  " [        R
                  " X5      R                  [        R                  " SU-  5      * [        R                  " SU-  5      5      5      U l	        [        R                  " [        R
                  " SUS5      R                  [        R                  " SU-  5      * [        R                  " SU-  5      5      5      U l
        g )Né   é   )r   r   r   Ú	Parameterr   ÚemptyÚuniform_ÚmathÚsqrtr    Úbias©r   Úin_featuresÚout_featuresr   s      €r   r   ÚChannelMix.__init__G   sÑ   ø€ Ü‰ÑÔÜ—l’l¤5§;¢;¨{Ó#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                ó^   • [         R                  " SXR                  5      U R                  -   $ )Nzbkt,kc->bct)r   Úeinsumr    r>   r"   s     r   r$   ÚChannelMix.forwardL   s    € Ü�|Š|˜M¨1¯k©kÓ:¸T¿Y¹YÑFÐFr   )r>   r    ©r@   r&   rA   r&   r'   r*   r2   s   @r   r4   r4   D   s   ø† ñ G÷D÷
Gò Gr   r4   c                óš  • [         R                  " 5       n[        [        X5      5       H�  u  nu  pxUR	                  [         R                  " [         R
                  " SUS-
  45      [        US:X  a  UOXS-
     Xx5      [         R                  " Xs/5      [         R                  " 5       [         R                  " U5      5      5        MŸ     U$ )Nr   r8   )
r   Ú
SequentialÚ	enumerateÚzipÚappendÚ	ZeroPad1dr   Ú	LayerNormÚELUÚDropout)	Únum_kernelsÚkernel_sizesr   Útime_dimÚdropoutÚlayersÚiÚn_outÚks	            r   Ú
_extractorrX   P   s—   € Ü�]Š]‹_€FÜ"¤3 {Ó#AÖB‰ˆ‰:ˆEØ�‰”b—m’mÜ�LŠL˜!˜Q ™U˜Ó$Ü¨¨a«™+°[ÀQÁÑ5GÈÓRÜ�LŠL˜%Ð*Ó+Ü�FŠF‹HÜ�JŠJ�wÓó
ö 	ñ Cð €Mr   c           	     óô   • [         R                  " [         R                  " US-
  S45      [        X U5      [         R                  " X/5      [         R
                  " 5       [         R                  " U5      5      $ )Nr8   r   )r   rH   rL   r   rM   rN   rO   )Úchannelsr   rR   rS   s       r   Ú_output_contextr[   ]   sV   € Ü�=Š=Ü
�Š�k A‘o qÐ)Ó*Ü�h¨+Ó6Ü
�Š�hÐ)Ó*Ü
�Š‹Ü
�
Š
�7Óóð r   c                  óV   ^ • \ rS rSrSr   S         SU 4S jjjrSS jrSrU =r$ )	ÚVLAAIwsBackboneég   z`proj (1x1 conv) + one weight-shared VLAAI block applied nb_blocks times. (B, T, C) -> (B, T, K).c
           
     ó   >• [         T
U ]  5         [        [        X#-  5      5      U l        XHUsU l        U l        U l        [        R                  " XS5      U l
        [        R                  " [        U/[        U5      -  XeU R                  U	5      [        R                  " U	5      [        XWU R                  U	5      [!        XU5      5      U l        g )Nr8   )r   r   r&   ÚroundrR   Ú	nb_blocksÚuse_skiprA   r   r   ÚprojrH   rX   ÚlenrO   r[   r4   Úblock)r   Únum_channelsÚfsÚwindow_sra   rP   rQ   Úoutput_context_kernel_sizerb   rS   r   s             €r   r   ÚVLAAIwsBackbone.__init__j   s¢   ø€ ô 	‰ÑÔÜœE "¡-Ó0Ó1ˆŒØ;DÐP[Ð8ˆŒ˜œ tÔ'8Ü—I’I˜l¸Ó;ˆŒ	Ü—]’]Ü˜�}¤s¨<Ó'8Ñ8¸,ÐUY×UbÑUbÐdkÓlÜ�JŠJ�wÓÜ˜KÀTÇ]Á]ÐT[Ó\Ü�{Ó0ó	
ˆ�
r   c                ó†  • UR                   S   U R                  :w  a(  [        SUR                   S    SU R                   35      eU R                  UR	                  SS5      5      nUn[        U R                  5       H/  nU R                  (       a
  US:”  a  X!-   nU R                  U5      nM1     UR	                  SS5      $ )Nr8   z
input has z$ samples, the network was built for r   r   )	ÚshaperR   Ú
ValueErrorrc   Ú	transposeÚrangera   rb   re   )r   r#   ÚhrU   s       r   r$   Ú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   )re   ra   rA   rc   rR   rb   )	é:   é    g      @é   é@   )r7   é   é   é   Tg        )rf   r&   rg   r&   rh   Úfloatra   r&   rP   r&   rQ   úSequence[int]ri   r&   rb   ÚboolrS   ry   r'   r*   r2   s   @r   r]   r]   g   sK   ø† ÙjàefØtvØ9<ð
Ø!ð
Ø6Cð
Ønqð
àð
à16÷
ð 
÷	!ò 	!r   r]   c                  ó:   ^ • \ rS rSrSrSU 4S jjrSS jrSrU =r$ )Ú
LinearHeadé„   zKy[b, t, f] = sum_k h[b, t, k] W[k, f] (no bias; parameter `weight` (K, F)).c           	     óþ   >• [         TU ]  5         [        R                  " [        R
                  " X5      R                  [        R                  " SU-  5      * [        R                  " SU-  5      5      5      U l	        g )Nr7   )
r   r   r   r9   r   r:   r;   r<   r=   r    r?   s      €r   r   ÚLinearHead.__init__‡   so   ø€ Ü‰ÑÔÜ—l’l¤5§;¢;¨{Ó#I×#RÑ#RÔTX×T]ÒT]Ð^_ÐbmÑ^mÓTnÐSnÔpt×pyÒpyÐz{ð  Jñ  {Jó  qKó  $Ló  Mˆ�r   c                óD   • [         R                  " SXR                  5      $ )Nzbtk,kf->btf)r   rD   r    )r   rp   s     r   r$   ÚLinearHead.forward‹   s   € Ü�|Š|˜M¨1¯k©kÓ:Ð:r   )r    rF   )rp   r(   r)   r(   r*   r2   s   @r   r}   r}   „   s   ø† ÙU÷M÷;ò ;r   r}   c                  ó>   ^ • \ rS rSrSrSSU 4S jjjrSS jrSrU =r$ )	ÚVLAAIwsDecoderé�   z­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U ]  5         [        SSU0UD6U l        [	        U R                  R
                  U5      U l        X l        g )Nrf   © )r   r   r]   Úbackboner}   rA   ÚheadÚ	n_targets)r   rf   rŠ   Úbackbone_kwargsr   s       €r   r   ÚVLAAIwsDecoder.__init__•   s?   ø€ Ü‰ÑÔÜ'ÑU°\ÐUÀ_ÑUˆŒÜ˜tŸ}™}×9Ñ9¸9ÓEˆŒ	Ø"�r   c                óp   • U R                  U R                  U5      5      nU R                  S:X  a  US   $ U$ )Nr8   ).r   )r‰   rˆ   rŠ   )r   r#   Úys      r   r$   ÚVLAAIwsDecoder.forward›   s3   € Ø�I‰I�d—m‘m AÓ&Ó'ˆØ ŸN™N¨aÓ/ˆq�‰yÐ6°QÐ6r   )rˆ   r‰   rŠ   )rr   r7   )rf   r&   rŠ   r&   r'   r*   r2   s   @r   r„   r„   �   s   ø† ñ÷
#ñ #÷7ò 7r   r„   c           
     óN  • U R                   R                  nXS-
  U-
  nUR                  u  px[        U R	                  5       5      R
                  n	[        R                  " UR                  Xh5      XR                  X2-   U5      /5      R                  U	[        R                  5      n
[        R                  " SXr5      n/ nU R                  5         [        S[        U5      U5       H’  nX½XÔ-    n[        R                  " UR!                  5        Vs/ s H	  oúXÿU-    PM     sn5      nUR#                  U " U5      SS2XfU-   24   R$                  " S/U R&                  S:X  a  SOU R&                  4Q76 5        M”     [        R                  " U5      SU R)                  5       $ s  snf )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éÿÿÿÿr8   r‡   )rˆ   rR   rl   ÚnextÚ
parametersÚdevicer   ÚcatÚ	new_zerosÚtoÚfloat32ÚarangeÚevalro   rd   ÚstackÚtolistrK   ÚreshaperŠ   Úcpu)ÚmodelÚeegÚhopÚfutureÚbatchÚTÚpos0ÚnÚCÚdevr#   ÚstartsÚoutrU   ÚsÚaÚws                    r   Úcausal_reconstructr®       sX  € ð 	�‰×Ñ€AØ‰:˜Ñ€DØ�9‰9�D€AÜ
ˆu×ÑÓ!Ó
"×
)Ñ
)€CÜ�	Š	�3—=‘= Ó)¨3·±¸f¹lÈAÓ0NÐOÓP×SÑSÐTWÔY^×YfÑYfÓg€AÜ�\Š\˜!˜QÓ$€FØ
€CØ	‡J�J„LÜ�1”c˜&“k 5Ö)ˆØ�Q‘YÐˆÜ�KŠK¨Q¯X©X¬ZÓ8ªZ¨˜1 ™U›©ZÑ8Ó9ˆØ�
‰
‘5˜“8šA˜t¨3¡J˜Ð.Ñ/×7Ò7¸ÐrÀEÇOÁOÐWXÓDX¹bÐ_d×_nÑ_nÐ^pÒrÖsñ *ô �9Š9�S‹>˜"˜1Ð×!Ñ!Ó#Ð#ùò 9s   ÄF"
Ú__main__rr   r7   )rf   rŠ   rt   é    zparameters:c              #  ó@   #   • U  H  oR                  5       v •  M     g 7fr
   )Únumel)Ú.0r   s     r   Ú	<genexpr>r´   ½   s   é € Ð?²¨1ŸW™WŸY˜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))rP   rz   rQ   rz   r   r&   rR   r&   rS   ry   r)   únn.Sequential)
rZ   r&   r   r&   rR   r&   rS   ry   r)   r·   )é   é   i   )rŸ   r„   r    r(   r¡   r&   r¢   r&   r£   r&   r)   r(   )&r/   Ú
__future__r   r<   Útypingr   r   r   ÚModuler   r4   rX   r[   r]   r}   r„   Úno_gradr®   r+   Úmanual_seedÚmÚrandnr#   rŽ   ÚprintÚsumr“   Útuplerl   ÚrecÚe0ÚcloneÚe1ÚabsÚdr&   ÚnonzeroÚfirstr‡   r   r   Ú<module>rÌ      sÒ  ðñ*õV #ã Ý ã Ý ô�B—I‘Iô ô	G�—‘ô 	Gô
ôô!�b—i‘iô !ô:;�—‘ô ;ô7�R—Y‘Yô 7ð" ‡‚ƒõ$ó ð$ð. ˆzÔØ	×Ò�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��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   