+
    –°³j¬_  ã                  óº  € ^ RI Ht ^ RIt^ RIHtHtHt ^ RIt^ RIH	t	 ^ RIH
t
 ^ RIHt ]'       d   ^ RIHtHtHt R R ltR	 R
 ltR R ltR R ltR R ltR-R R lltR R ltR-R R lltR.R R lltR R ltR R lt ! R R]	P8                  4      t ! R R ]	P8                  4      tR-R! R" lltR-R# R$ llt R% R& lt!R' R( lt"R/R) R* llt#R/R+ R, llt$R# )0é    )ÚannotationsN)ÚAnyÚNoReturnÚTYPE_CHECKING)ÚTensor)ÚNamedMemberAccessor)ÚCallableÚIterableÚSequencec               ó   € V ^8„  d   QhRR/# )é   Úreturnr   © )Úformats   "Úa/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torch/_functorch/make_functional.pyÚ__annotate__r      s   € ÷ ñ  Xñ ó    c                 ó   € \        R 4      h)a$  make_functional(module): we don't yet support models that do parameter tying (also sometimes known as weight sharing). Please try to rewrite your model by replacing all instances of the tied parameter with another and/or comment your support in https://github.com/pytorch/functorch/issues/446)ÚRuntimeErrorr   r   r   Úraise_parameter_tying_errorr      s   € Ü
ð	:óð r   c               ó$   € V ^8„  d   QhRRRRRR/# )r   Únamed_paramsz0dict[str, Tensor] | Iterable[tuple[str, Tensor]]Útied_named_paramsr   údict[str, list[str]]r   )r   s   "r   r   r   $   s&   € ÷  ,ñ  ,ØBð ,àGð ,ð ñ ,r   c                óî  € \        V 4      p\        V4      p\        VP                  4       4      p\        VP                  4       4      pVP                  V4      '       g   \	        RV RV 24      h/ pVP                  4        F  w  rxV. 3Wh&   K  	  VP                  4        F8  w  rxW†9  d   \	        RV R24      hWh,          ^,          P                  V4       K:  	  \        VP                  4       4      # )aC  
named_params is a dictionary of tensors: {'A': A, 'B': B}
tied_named_params is another dictionary of tensors {'A': A, 'B': B, 'B_tied': B}
with potentially tied (or 'duplicated') tensors

This function creates a mapping from the names in named_params to the
names in tied_named_params: {'A': ['A'], 'B': ['B', 'B_tied']}.
ztensors_dict_keys z+ is not a subset of tied_tensors_dict_keys ztensor for key 'z ' not found in tensor_to_mapping)ÚdictÚsetÚkeysÚissubsetÚAssertionErrorÚitemsÚappendÚvalues)	r   r   Únamed_params_dictÚtied_named_params_dictÚtensors_dict_keysÚtied_tensors_dict_keysÚtensor_to_mappingÚkeyÚtensors	   &&       r   Úcreate_names_mapr+   $   s  € ô ˜\Ó*ÐÜ!Ð"3Ó4ÐäÐ-×2Ñ2Ó4Ó5ÐÜ Ð!7×!<Ñ!<Ó!>Ó?ÐØ×%Ñ%Ð&<×=Ò=ÜØ Ð!2Ð 3ð 4&Ø&<Ð%=ð?ó
ð 	
ð
 >@ÐØ(×.Ñ.Ö0‰ˆØ%(¨" IÐÓ!ñ 1à-×3Ñ3Ö5‰ˆØÔ*Ü Ø" 3 %Ð'GÐHóð ð 	Õ! !Õ$×+Ñ+¨CÖ0ñ 6ô Ð!×(Ñ(Ó*Ó+Ð+r   c               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   Úmodú	nn.ModuleÚnamed_membersz+Callable[..., Iterable[tuple[str, Tensor]]]ÚsubclasszCallable[[Tensor], Tensor]r   ú@tuple[tuple[Tensor, ...], tuple[str, ...], dict[str, list[str]]]r   )r   s   "r   r   r   G   s1   € ÷ $ñ $Ø	ð$à>ð$ð )ð$ð Fñ	$r   c                óT  € \        V! R R7      4      p\        V! RR7      4      p\        WC4      p/ p\        V 4      pV FD  w  r‰W–9  d!   V! \        P                  ! V	RR7      4      Wi&   Wi,          p
VP                  WŠ4       KF  	  \        V4      ^ 8X  d   RRrËM\        V!  w  r¼WËV3# )F)Úremove_duplicateTÚmeta)Údevicer   )Útupler+   r   ÚtorchÚ
empty_likeÚ
set_tensorÚlenÚzip)r-   r/   r0   Úall_named_membersÚunique_named_membersÚ	names_mapÚmemoÚaccessorÚnameÚpÚreplacementÚnamesÚparamss   &&&          r   Ú_extract_membersrF   G   s®   € ô
 ™m¸UÔCÓDÐÜ ¡ÀÔ!EÓFÐÜ Ð!5ÓI€Ið €DÜ" 3Ó'€HÛ$‰ˆØŒ=Ùœu×/Ò/°¸&ÔAÓBˆD‰GØ•gˆØ×Ñ˜DÖ.ñ	 %ô ÐÓ  AÔ%Ø˜B‰väÐ1Ñ2‰ˆØ˜)Ð#Ð#r   c               ó    € V ^8„  d   QhRRRR/# ©r   r-   r.   r   r1   r   )r   s   "r   r   r   a   s    € ÷ Eñ EØ	ðEàEñEr   c                óJ   € \        W P                  \        P                  4      # )a>  
This function removes all the Parameters from the model and
return them as a tuple as well as their original attribute names.
The weights must be re-loaded with `load_weights` before the model
can be used again.
Note that this function modifies the model in place and after this
call, mod.parameters() will be empty.
)rF   Únamed_parametersÚnnÚ	Parameter©r-   s   &r   Úextract_weightsrN   a   s   € ô ˜C×!5Ñ!5´r·|±|ÓDÐDr   c               ó    € V ^8„  d   QhRRRR/# rH   r   )r   s   "r   r   r   o   s    € ÷ Añ AØ	ðAàEñAr   c                ó0   € \        W P                  R  4      # )c                ó   € V # ©Nr   )Úxs   &r   Ú<lambda>Ú!extract_buffers.<locals>.<lambda>r   s   € ¹ar   )rF   Únamed_buffersrM   s   &r   Úextract_buffersrW   o   s   € ô ˜C×!2Ñ!2±KÓ@Ð@r   c          
     ó,   € V ^8„  d   QhRRRRRRRRR	R
/# )r   r-   r.   rD   úSequence[str]rE   úSequence[Tensor]Ú	as_paramsÚboolr   ÚNoner   )r   s   "r   r   r   u   s:   € ÷ (ñ (Ø	ð(àð(ð ð(ð ð	(ð
 
ñ(r   c                ó¢   € \        V 4      pV'       d&   V Uu. uF  p\        P                  ! V4      NK  	  ppVP                  W4       R# u upi )zù
Reload a set of weights so that `mod` can be used again to perform a forward pass.
Note that the `params` are regular Tensors (that can have history) and so are left
as Tensors. This means that mod.parameters() will still be empty after this call.
N)r   rK   rL   Úset_tensors)r-   rD   rE   r[   r@   rB   s   &&&&  r   Úload_weightsr`   u   s@   € ô # 3Ó'€HßÙ+1Ó2©6 a”"—,’,˜q–/©6ˆÐ2Ø×Ñ˜Ö'ùò 3s   ˜Ac               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   r-   r.   r>   r   ÚelemsúIterable[Tensor]r   zlist[Tensor]r   )r   s   "r   r   r   †   s,   € ÷ ñ Ø	ðØ3ðØ<Lðàñr   c                ó  € . p\        V 4      p\        VP                  4       V4       FV  w  w  rVp\        V4       F?  w  r‰V^ 8X  d#   VP	                  VP                  W—4      4       K.  VP                  W—4       KA  	  KX  	  V# )r   )r   r;   r!   Ú	enumerater"   Úswap_tensorr9   )
r-   r>   rb   Úresultr@   Ú_Ú
attr_namesÚelemÚiÚ	attr_names
   &&&       r   Ú_swap_staterm   †   sv   € ð €FÜ" 3Ó'€HÜ!$ Y§_¡_Ó%6¸Ö!>Ñ‰ˆ˜Ü% jÖ1‰LˆAØ�AŒvØ—‘˜h×2Ñ2°9ÓCÖDà×#Ñ# IÖ4ó	 2ñ "?ð €Mr   c          
     ó,   € V ^8„  d   QhRRRRRRRRR	R
/# )r   r-   r.   rD   rY   ÚbuffersrZ   r[   r\   r   r]   r   )r   s   "r   r   r   ”   s:   € ÷ )ñ )Ø	ð)àð)ð ð)ð ð	)ð
 
ñ)r   c                ó>   € \        V 4      pVP                  W4       R # rR   )r   r_   )r-   rD   ro   r[   r@   s   &&&& r   Úload_buffersrq   ”   s   € ô # 3Ó'€HØ×Ñ˜Ö(r   c               ó0   € V ^8„  d   QhRRRRRRRRRRR	R/# )
r   Úmodelr.   ÚweightsrZ   Úweight_namesrY   ro   Úbuffer_namesr   r   )r   s   "r   r   r   ž   sD   € ÷ ñ Øðàðð  ðð ð	ð
  ðð ñr   c                óF  € \        V4      \        V4      8w  d$   \        R\        V4       R\        V4       24      h\        WV4       \        V4      ^ 8”  dI   \        V4      \        V4      8w  d$   \        R\        V4       R\        V4       24      h\        WV4       V # )zÚload_state(model, weights, weight_names, buffers=(), buffer_names=()) -> model

load_state takes `weights` and `buffers` and assigns them to the model.
This is the inverse operation of `make_functional_deprecated_v1`.
zlen(weight_names)=z != len(weights)=zlen(buffer_names)=z != len(buffers)=)r:   r    r`   rq   )rs   rt   ru   ro   rv   s   &&&&&r   Ú
load_staterx   ž   sŸ   € ô ˆ<ÓœC ›LÔ(ÜØ ¤ \Ó!2Ð 3Ð3DÄSÈÃ\ÀNÐSó
ð 	
ô � gÔ.Ü
ˆ7ƒ|�aÔÜˆ|Ó¤ G£Ô,Ü Ø$¤S¨Ó%6Ð$7Ð7HÌÈWËÈÐWóð ô 	�U¨'Ô2Ø€Lr   c               ó    € V ^8„  d   QhRRRR/# )r   rs   r.   r   ú>tuple[tuple[Tensor, ...], Callable[..., Any], tuple[str, ...]]r   )r   s   "r   r   r   ¸   s   € ÷ )%ñ )%Øð)%àCñ)%r   c                ó¨   a a€ \        S P                  4       4      p\        V4      ^ 8”  d   \        R4      h\	        S 4      w  popR VV 3R llpW$S3# )aÊ  make_functional_deprecated_v1(model) -> weights, func, weight_names

Given an nn.Module, make_functional_deprecated_v1 extracts the state (weights)
and returns a functional version of the model, `func`. This makes
it so that it is possible use transforms over the parameters of
`model`.

`func` can be invoked as follows:
```
x = torch.randn(4, 3)
model = nn.Linear(3, 3)
weights, func, _ = make_functional_deprecated_v1(model)
func(weights, (x,))
```

And here is an example of applying the grad transform:
```
x = torch.randn(4, 3)
model = nn.Linear(3, 3)
weights, _, func = make_functional_deprecated_v1(model)
grad_weights = grad(func)(weights, (x,))
```

To put the state back into a model, use `load_state`.
z€make_functional_deprecated_v1(model): `model` has buffers. Please use make_functional_with_buffers_deprecated_v1(model) instead.c               ó$   € V ^8„  d   QhRRRRRR/# )r   rt   útuple[Tensor, ...]Údataútuple[Any, ...]r   r   r   )r   s   "r   r   Ú3make_functional_deprecated_v1.<locals>.__annotate__Ü   s"   € ÷ $ñ $Ð'ð $¨ð $À3ñ $r   c                óV   <€ \         P                  ! S4      p\        VSV 4       V! V!  # rR   )ÚcopyÚdeepcopyr`   )rt   r~   Úmutable_modelÚdescriptorsrs   s   && €€r   ÚfunÚ*make_functional_deprecated_v1.<locals>.funÜ   s(   ø€ ÜŸš eÓ,ˆÜ�] K°Ô9Ù˜dÑ#Ð#r   )Úlistro   r:   r   rN   )rs   ro   rt   rh   r†   r…   s   f    @r   Úmake_functional_deprecated_v1r‰   ¸   s^   ù€ ô8 �5—=‘=“?Ó#€GÜ
ˆ7ƒ|�aÔÜðIó
ð 	
ô .¨eÓ4Ñ€Gˆ[˜!÷$ñ $ð
 ˜Ð$Ð$r   c               ó    € V ^8„  d   QhRRRR/# )r   rs   r.   r   zctuple[tuple[Tensor, ...], tuple[Tensor, ...], Callable[..., Any], tuple[str, ...], tuple[str, ...]]r   )r   s   "r   r   r   ä   s"   € ÷ .Fñ .FØð.Fðñ.Fr   c                óf   a aa€ \        S 4      w  pop\        S 4      w  popR VV V3R llpWVSS3# )a  make_functional_with_buffers_deprecated_v1(model) -> weights, buffers, func, weight_names, buffer_names

Given an nn.Module, make_functional_with_buffers_deprecated_v1 extracts the state (weights and buffers)
and returns a functional version of the model, `func`.

`func` can be invoked as follows:
```
x = torch.randn(4, 3)
model = nn.Linear(3, 3)
weights, buffers, func, _, _ = make_functional_with_buffers_deprecated_v1(model)
func(weights, buffers, (x,))
```

And here is an example of applying the grad transform:
```
x = torch.randn(4, 3)
model = nn.Linear(3, 3)
weights, buffers, func, _, _ = make_functional_with_buffers_deprecated_v1(model)
func(weights, buffers, (x,))
grad_weights = grad(func)(weights, buffers, (x,))
```

To put the state back into a model, use `load_state`.
c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   rt   r}   ro   r~   r   r   r   r   )r   s   "r   r   Ú@make_functional_with_buffers_deprecated_v1.<locals>.__annotate__  s0   € ÷ $ñ $Ø#ð$à#ð$ð ð$ð 
ñ	$r   c                óp   <€ \         P                  ! S4      p\        VSV 4       \        VSV4       V! V!  # rR   )r‚   rƒ   r`   rq   )rt   ro   r~   r„   Úbuf_descriptorsrs   Úweight_descriptorss   &&& €€€r   r†   Ú7make_functional_with_buffers_deprecated_v1.<locals>.fun  s7   ø€ ô
 Ÿš eÓ,ˆÜ�]Ð$6¸Ô@Ü�] O°WÔ=Ù˜dÑ#Ð#r   )rN   rW   )rs   rt   rh   ro   r†   r�   r�   s   f    @@r   Ú*make_functional_with_buffers_deprecated_v1r’   ä   sE   ú€ ôB &5°UÓ%;Ñ"€GÐ Ü"1°%Ó"8Ñ€Gˆ_˜a÷$ò $ð ˜SÐ"4°oÐEÐEr   c                  óX   a € ] tR tRtRtR V 3R llt]R
R R ll4       tR R ltR	t	V ;t
# )ÚFunctionalModuleWithBuffersi  zO
This is the callable object returned by :func:`make_functional_with_buffers`.
c               ó0   € V ^8„  d   QhRRRRRRRRRRR	R
/# )r   Ústateless_modelr.   Úparam_namesútuple[str, ...]rv   Úparam_names_mapr   Úbuffer_names_mapr   r]   r   )r   s   "r   r   Ú(FunctionalModuleWithBuffers.__annotate__  sF   € ÷ 4ñ 4à"ð4ð %ð4ð &ð	4ð
 .ð4ð /ð4ð 
ñ4r   c                	óž   <€ \         SV `  4        Wn        W n        W0n        \        V4      V n        V P                  P                  V4       R # rR   )ÚsuperÚ__init__r–   r—   rv   r   Úall_names_mapÚupdate)Úselfr–   r—   rv   r™   rš   Ú	__class__s   &&&&&&€r   rž   Ú$FunctionalModuleWithBuffers.__init__  sD   ø€ ô 	‰ÑÔØ.ÔØ&ÔØ(Ôä! /Ó2ˆÔØ×Ñ×!Ñ!Ð"2Ö3r   c               ó$   € V ^8„  d   QhRRRRRR/# ©r   rs   r.   Údisable_autograd_trackingr\   r   úJtuple[FunctionalModuleWithBuffers, tuple[Tensor, ...], tuple[Tensor, ...]]r   )r   s   "r   r   r›   +  s$   € ÷ 
ñ 
Øð
Ø59ð
à	Sñ
r   c                	óÊ   € \         P                  ! V 4      p\        V4      w  r4p\        V4      w  rgpV'       d   V F  p	V	P	                  R 4       K  	  \        W$WuV4      VV3# ©F)r‚   rƒ   rN   rW   Úrequires_grad_r”   )
rs   r¦   Ú
model_copyrE   r—   r™   ro   rv   rš   Úparams
   &&        r   Ú_create_fromÚ(FunctionalModuleWithBuffers._create_from*  sp   € ô
 —]’] 5Ó)ˆ
Ü/>¸zÓ/JÑ,ˆ˜_Ü2AÀ*Ó2MÑ/ˆÐ/ß$Û�Ø×$Ñ$ UÖ+ñ  ô (Ø¨ÐHXóð Øð
ð 	
r   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )r   rE   rc   ro   Úargsr   Úkwargsr   r   )r   s   "r   r   r›   =  sC   € ÷ Mñ Mà ðMð "ðMð ð	Mð
 ðMð 
ñMr   c                	ó*  € \        V P                  V P                  \        V4      \        V4      ,           4      p V P                  ! V/ VB \        V P                  V P                  V4       #   \        T P                  T P                  T4       i ; irR   )rm   r–   rŸ   r6   )r¡   rE   ro   r°   r±   Ú	old_states   &&&*, r   ÚforwardÚ#FunctionalModuleWithBuffers.forward=  s|   € ô  Ø× Ñ Ø×ÑÜ�&‹MœE '›NÕ*ó
ˆ	ð
	MØ×'Ò'¨Ð8°Ñ8ô ˜×,Ñ,¨d×.@Ñ.@À)ÕLøŒK˜×,Ñ,¨d×.@Ñ.@À)ÕLús   ¼A/ Á/#B)rŸ   rv   r—   r–   r©   ©Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__rž   Ústaticmethodr­   r´   Ú__static_attributes__Ú__classcell__©r¢   s   @r   r”   r”     s2   ø† ñ÷4ð 4ð  ö
ó ð
÷$Mó Mr   r”   c                  óX   a € ] tR tRtRtR V 3R llt]R
R R ll4       tR R ltR	t	V ;t
# )ÚFunctionalModuleiQ  zB
This is the callable object returned by :func:`make_functional`.
c               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   r–   r.   r—   r˜   r>   r   r   r]   r   )r   s   "r   r   ÚFunctionalModule.__annotate__V  s2   € ÷ 	#ñ 	#à"ð	#ð %ð	#ð (ð		#ð
 
ñ	#r   c                	óH   <€ \         SV `  4        Wn        W n        W0n        R # rR   )r�   rž   r–   r—   r>   )r¡   r–   r—   r>   r¢   s   &&&&€r   rž   ÚFunctionalModule.__init__V  s!   ø€ ô 	‰ÑÔØ.ÔØ&ÔØ"Žr   c               ó$   € V ^8„  d   QhRRRRRR/# ©r   rs   r.   r¦   r\   r   z+tuple[FunctionalModule, tuple[Tensor, ...]]r   )r   s   "r   r   rÃ   b  s)   € ÷ 	Lñ 	LØð	LØ59ð	Là	4ñ	Lr   c                	óª   € \         P                  ! V 4      p\        V4      w  r4pV'       d   V F  pVP                  R 4       K  	  \	        W$V4      V3# r©   )r‚   rƒ   rN   rª   rÁ   )rs   r¦   r«   rE   r—   r>   r¬   s   &&     r   r­   ÚFunctionalModule._create_froma  sN   € ô
 —]’] 5Ó)ˆ
Ü)8¸Ó)DÑ&ˆ˜Yß$Û�Ø×$Ñ$ UÖ+ñ  ä 
¸ÓCÀVÐKÐKr   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   rE   rc   r°   r   r±   r   r   )r   s   "r   r   rÃ   m  s/   € ÷ Iñ IÐ.ð I°sð IÀcð IÈcñ Ir   c                	óø   € \        V P                  V P                  V4      p V P                  ! V/ VB \        V P                  V P                  V4       #   \        T P                  T P                  T4       i ; irR   )rm   r–   r>   )r¡   rE   r°   r±   r³   s   &&*, r   r´   ÚFunctionalModule.forwardm  sb   € ä × 4Ñ 4°d·n±nÀfÓMˆ	ð	IØ×'Ò'¨Ð8°Ñ8ô ˜×,Ñ,¨d¯n©n¸iÕHøŒK˜×,Ñ,¨d¯n©n¸iÕHús   £A Á#A9)r>   r—   r–   r©   r¶   r¿   s   @r   rÁ   rÁ   Q  s4   ø† ñ÷	#ð 	#ð ö	Ló ð	L÷Ió Ir   rÁ   c               ó$   € V ^8„  d   QhRRRRRR/# rÇ   r   )r   s   "r   r   r   w  s)   € ÷ Eñ EØðEØ15ðEà0ñEr   c                ó–   € \        V P                  4       4      p\        V4      ^ 8”  d   \        R4      h\        P                  WR7      # )a  make_functional(model, disable_autograd_tracking=False) -> func, params

Given a ``torch.nn.Module``, :func:`make_functional` extracts the state
(params) and returns a functional version of the model, ``func``. This
makes it so that it is possible use transforms over the parameters of
``model``.

``func`` can be invoked as follows:

.. code-block:: python

    import torch
    import torch.nn as nn
    from functorch import make_functional

    x = torch.randn(4, 3)
    model = nn.Linear(3, 3)
    func, params = make_functional(model)
    func(params, x)

And here is an example of applying the grad transform over the parameters
of a model.

.. code-block:: python

    import torch
    import torch.nn as nn
    from functorch import make_functional, grad

    x = torch.randn(4, 3)
    t = torch.randn(4, 3)
    model = nn.Linear(3, 3)
    func, params = make_functional(model)


    def compute_loss(params, x, t):
        y = func(params, x)
        return nn.functional.mse_loss(y, t)


    grad_weights = grad(compute_loss)(params, x, t)

If the model has any buffers, please use :func:`make_functional_with_buffers` instead.

Args:
    model (torch.nn.Module): Input model.
    disable_autograd_tracking (bool): Flag to disable gradients tracking for output parameters.
        The returned params are unrelated to the set of params from the original model. If False (default),
        the params will have ``requires_grad=True`` on them (aka they will be trackable with regular
        PyTorch autograd), matching the requires_grad-ness of the params from the original model.
        Otherwise, the returned params will have ``requires_grad=False``. Default, False.
        If you plan on using regular PyTorch autograd (e.g., if you want to call ``.backward()`` or
        ``torch.autograd.grad()``, then set ``disable_autograd_tracking=False``.
        Otherwise, if you're only planning on using functorch's gradient transforms,
        then please set ``disable_autograd_tracking=True`` to avoid unnecessarily tracking
        history with PyTorch autograd.

zdmake_functional(model): `model` has buffers. Please use make_functional_with_buffers(model) instead.©r¦   )rˆ   ro   r:   r   rÁ   r­   )rs   r¦   ro   s   && r   Úmake_functionalrÐ   w  sP   € ôz �5—=‘=“?Ó#€GÜ
ˆ7ƒ|�aÔÜð;ó
ð 	
ô ×(Ñ(Øð )ó ð r   c               ó$   € V ^8„  d   QhRRRRRR/# r¥   r   )r   s   "r   r   r   ¿  s$   € ÷ <ñ <Øð<Ø15ð<àOñ<r   c                ó.   € \         P                  WR7      # )a$  make_functional_with_buffers(model, disable_autograd_tracking=False) -> func, params, buffers

Given a ``torch.nn.Module``, make_functional_with_buffers extracts the
state (params and buffers) and returns a functional version of the model
``func`` that can be invoked like a function.

``func`` can be invoked as follows:

.. code-block:: python

    import torch
    import torch.nn as nn
    from functorch import make_functional_with_buffers

    x = torch.randn(4, 3)
    model = nn.Linear(3, 3)
    func, params, buffers = make_functional_with_buffers(model)
    func(params, buffers, x)

And here is an example of applying the grad transform over the parameters
of a model:

.. code-block:: python

    import torch
    import torch.nn as nn
    from functorch import make_functional_with_buffers, grad

    x = torch.randn(4, 3)
    t = torch.randn(4, 3)
    model = nn.Linear(3, 3)
    func, params, buffers = make_functional_with_buffers(model)


    def compute_loss(params, buffers, x, t):
        y = func(params, buffers, x)
        return nn.functional.mse_loss(y, t)


    grad_weights = grad(compute_loss)(params, buffers, x, t)

Args:
    model (torch.nn.Module): Input model.
    disable_autograd_tracking (bool): Flag to disable gradients tracking for output parameters.
        The returned params are unrelated to the set of params from the original model. If False (default),
        the params will have ``requires_grad=True`` on them (aka they will be trackable with regular
        PyTorch autograd), matching the requires_grad-ness of the params from the original model.
        Otherwise, the returned params will have ``requires_grad=False``. Default, False.
        If you plan on using regular PyTorch autograd (e.g., if you want to call ``.backward()`` or
        ``torch.autograd.grad()``, then set ``disable_autograd_tracking=False``.
        Otherwise, if you're only planning on using functorch's gradient transforms,
        then please set ``disable_autograd_tracking=True`` to avoid unnecessarily tracking
        history with PyTorch autograd.

rÏ   )r”   r­   )rs   r¦   s   &&r   Úmake_functional_with_buffersrÓ   ¿  s    € ôt '×3Ñ3Øð 4ó ð r   c               ó    € V ^8„  d   QhRRRR/# )r   Útuple_of_tuple_of_tensorsztuple[tuple[Tensor, ...], ...]r   r}   r   )r   s   "r   r   r   þ  s   € ÷ ñ Ø=ðàñr   c                ó†   € \        \        V !  4      p \         ;QJ d    . R  V  4       F  NK  	  5pV# ! R  V  4       4      pV# )c              3  ój   "  € T F)  p\         P                  ! V4      P                  4       x € K+  	  R # 5irR   ©r7   ÚstackÚdetach©Ú.0Úshardss   & r   Ú	<genexpr>Ú"transpose_stack.<locals>.<genexpr>  s)   é € ð Ù3L¨Œ�Š�FÓ×"Ñ"×$Ð$Ó3Lùó   ‚13)r6   r;   )rÕ   Úresultss   & r   Útranspose_stackrâ   þ  sR   € ô !&¤cÐ+DÑ&EÓ FÐßŒeñ Ù3Ló�e€Gð €Nð ñ Ù3Lóó €Gð €Nr   c               ó    € V ^8„  d   QhRRRR/# )r   ÚmodelszSequence[nn.Module]r   r§   r   )r   s   "r   r   r     s   € ÷ ;%ñ ;%Øð;%àOñ;%r   c                óˆ  a€ \        V 4      ^ 8X  d   \        R4      h\        ;QJ d    R V  4       F  '       d   K   RM	  RM! R V  4       4      '       gF   \        ;QJ d    R V  4       F  '       d   K   RM	  RM! R V  4       4      '       g   \        R4      h\        V ^ ,          4      o\        ;QJ d    V3R lV  4       F  '       d   K   RM	  RM! V3R lV  4       4      '       g   \        R4      h\	        V  Uu. uF  p\        V4      NK  	  up!  w  r#p\        V4      p\        V4      pV^ ,          W43# u upi )	aÀ  combine_state_for_ensemble(models) -> func, params, buffers

Prepares a list of torch.nn.Modules for ensembling with :func:`vmap`.

Given a list of ``M`` ``nn.Modules`` of the same class, stacks all of their
parameters and buffers together to make ``params`` and ``buffers``.
Each parameter and buffer in the result will have an additional dimension
of size ``M``.

:func:`combine_state_for_ensemble` also returns ``func``, a functional
version of one of the models in :attr:`models`. One cannot directly run
``func(params, buffers, *args, **kwargs)`` directly, you probably want to
use ``vmap(func, ...)(params, buffers, *args, **kwargs)``

Here's an example of how to ensemble over a very simple model:

.. code-block:: python

    num_models = 5
    batch_size = 64
    in_features, out_features = 3, 3
    models = [torch.nn.Linear(in_features, out_features) for i in range(num_models)]
    data = torch.randn(batch_size, 3)

    fmodel, params, buffers = combine_state_for_ensemble(models)
    output = vmap(fmodel, (0, 0, None))(params, buffers, data)

    assert output.shape == (num_models, batch_size, out_features)

.. warning::
    All of the modules being stacked together must be the same (except for
    the values of their parameters/buffers). For example, they should be in the
    same mode (training vs eval).

    This API is subject to change -- we're investigating better ways to
    create ensembles and would love your feedback how to improve this.
z?combine_state_for_ensemble: Expected at least one model, got 0.c              3  ó8   "  € T F  qP                   x € K  	  R # 5irR   ©Útraining©rÜ   Úms   & r   rÞ   Ú-combine_state_for_ensemble.<locals>.<genexpr>4  s   é € Ð+¡F˜q—
–
£Fùs   ‚FTc              3  óB   "  € T F  qP                   '       * x € K  	  R # 5irR   rç   ré   s   & r   rÞ   rë   4  s   é € Ð2RÉ6Àa·z±z·>±>Ë6ùs   ‚zTcombine_state_for_ensemble: Expected all models to have the same training/eval mode.c              3  ó>   <"  € T F  p\        V4      SJ x € K  	  R # 5irR   )Útype)rÜ   rê   Ú
model0_typs   & €r   rÞ   rë   :  s   øé € Ð5©f¨Œt�A‹w˜*Õ$«fùs   ƒzHcombine_state_for_ensemble: Expected all models to be of the same class.)r:   r   Úallrî   r;   rÓ   râ   )rä   rs   ÚfuncsrE   ro   rï   s   &    @r   Úcombine_state_for_ensemblerò     s  ø€ ôP ˆ6ƒ{�aÔÜØMó
ð 	
÷ ‹CÑ+¡FÓ+�C�CŠCÑ+¡FÓ+×+Ò+¯s«sÑ2RÉ6Ó2R¯s¯sªsÑ2RÉ6Ó2R×/RÒ/RÜð0ó
ð 	
ô �f˜Q•i“€Jß‹3Ô5©fÓ5�3�3Š3Ô5©fÓ5×5Ò5ÜØVó
ð 	
ô !Ù;AÓ	B¹6°%Ô
& uÖ
-¹6Ñ	BñÑ€E�7ô ˜VÓ$€FÜ˜gÓ&€GØ��8�VÐ$Ð$ùò	 
Cs   ÄD?c               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   Úmodel_classútype[nn.Module]Úensemble_shapeútuple[()] | tuple[int, ...]r5   útorch.types.Devicer   zMCallable[..., tuple[tuple[Tensor, ...], Callable[..., Any], tuple[str, ...]]]r   )r   s   "r   r   r   F  s1   € ÷ ñ Ø ðà/ðð ðð Sñ	r   c                ó"   a aa€ R  VVV 3R llpV# )c               ó$   € V ^8„  d   QhRRRRRR/# )r   r°   r   r±   r   rz   r   )r   s   "r   r   Ú%functional_init.<locals>.__annotate__K  s$   € ÷ "ñ "Øð"Ø!ð"à	Gñ"r   c                 óx  <a a€ \        S
4      ^8¼  d   \        R4      h\        S
4      ^ 8X  d#   S! S / SB P                  S	4      p\        V4      # S
^ ,          pV^ 8:  d   \        RV R24      h\        ;QJ d#    . V V	VV3R l\        V4       4       F  NK  	  5M! V V	VV3R l\        V4       4       4      p\        S! S / SB 4      w  rVp\        ;QJ d    . R V 4       F  NK  	  5M! R V 4       4      p\	        \        V!  4      p\        ;QJ d    . R V 4       F  NK  	  5M! R V 4       4      pW†V3# )r   ú,NYI: ensemble_shape with more than 1 elementúnum_models ú should be > 0c              3  óR   <"  € T F  pS! S/ SB P                  S4      x € K  	  R # 5irR   ©Úto©rÜ   rh   r°   r5   r±   rô   s   & €€€€r   rÞ   Ú3functional_init.<locals>.wrapped.<locals>.<genexpr>W  ó-   øé € ð 
Ù=N¸‰K˜Ð( Ñ(×+Ñ+¨F×3Ð3Ó=Nùó   ƒ$'c              3  óF   "  € T F  p\        V4      ^ ,          x € K  	  R# 5i)r   N)r‰   ©rÜ   rs   s   & r   rÞ   r  [  s   é € ÐTÉVÀEÔ5°eÓ<¸Q×?Ò?ËVùó   ‚!c              3  ój   "  € T F)  p\         P                  ! V4      P                  4       x € K+  	  R # 5irR   rØ   rÛ   s   & r   rÞ   r  ]  ó%   é € ÐKÁ7¸œŸš FÓ+×2Ñ2×4Ð4Ã7ùrà   )r:   Ú
ValueErrorr  r‰   r6   Úranger;   )r°   r±   rs   Ú
num_modelsrä   rh   ÚfnrD   rt   r5   rö   rô   s   jl       €€€r   ÚwrappedÚ functional_init.<locals>.wrappedK  s  ú€ ô ˆ~Ó !Ô#ÜÐKÓLÐLÜˆ~Ó !Ô#Ù Ð0¨Ñ0×3Ñ3°FÓ;ˆEÜ0°Ó7Ð7Ø# AÕ&ˆ
Ø˜Œ?Ü˜{¨:¨,°nÐEÓFÐFç”÷ 
Ü=BÀ:Ô=Nó
—‘÷ 
Ü=BÀ:Ô=Nó
ó 
ˆô 5±[À$Ð5QÈ&Ñ5QÓR‰ˆˆuß”%ÑTÉVÓT—%‘%ÑTÉVÓTÓTˆÜœ˜W™Ó&ˆß”%ÑKÁ7ÓK—%‘%ÑKÁ7ÓKÓKˆØ˜EÐ!Ð!r   r   ©rô   rö   r5   r  s   fff r   Úfunctional_initr  F  s   ú€ ÷
"ò "ð* €Nr   c               ó(   € V ^8„  d   QhRRRRRRRR/# )	r   rô   rõ   rö   r÷   r5   rø   r   z³Callable[..., tuple[tuple[Tensor, ...], tuple[Tensor, ...], Callable[..., Any], tuple[str, ...], tuple[str, ...]] | tuple[tuple[Tensor, ...], Callable[..., Any], tuple[str, ...]]]r   )r   s   "r   r   r   c  s0   € ÷ :ñ :Ø ð:à/ð:ð ð:ð
ñ	:r   c                ó"   a aa€ R  VVV 3R llpV# )c               ó$   € V ^8„  d   QhRRRRRR/# )r   r°   r   r±   r   z¤tuple[tuple[Tensor, ...], tuple[Tensor, ...], Callable[..., Any], tuple[str, ...], tuple[str, ...]] | tuple[tuple[Tensor, ...], Callable[..., Any], tuple[str, ...]]r   )r   s   "r   r   Ú2functional_init_with_buffers.<locals>.__annotate__r  s,   € ÷ )@ñ )@Øð)@Ø!ð)@ð	Iñ)@r   c                 ó  <a a€ \        S4      ^8¼  d   \        R4      h\        S4      ^ 8X  d#   S! S / SB P                  S4      p\        V4      # S^ ,          pV^ 8:  d   \        RV R24      h\        ;QJ d#    . V VVV3R l\        V4       4       F  NK  	  5M! V VVV3R l\        V4       4       4      p\        S! S / SB 4      w  ppppp\        \        ;QJ d    . R V 4       F  NK  	  5M! R V 4       4      !  w  rš\	        \        V	!  4      p	\        ;QJ d    . R V	 4       F  NK  	  5M! R V	 4       4      p	\	        \        V
!  4      p
\        ;QJ d    . R V
 4       F  NK  	  5M! R V
 4       4      p
WšWgV3# )r   rý   rþ   rÿ   c              3  óR   <"  € T F  pS! S/ SB P                  S4      x € K  	  R # 5irR   r  r  s   & €€€€r   rÞ   Ú@functional_init_with_buffers.<locals>.wrapped.<locals>.<genexpr>‡  r  r  c              3  óF   "  € T F  p\        V4      R ,          x € K  	  R# 5i):Nr   NN)r’   r  s   & r   rÞ   r  ’  s$   é € ð á#�Eô ;¸5ÓAÀ"×EÒEÛ#ùr	  c              3  ój   "  € T F)  p\         P                  ! V4      P                  4       x € K+  	  R # 5irR   rØ   rÛ   s   & r   rÞ   r  ˜  r  rà   c              3  ój   "  € T F)  p\         P                  ! V4      P                  4       x € K+  	  R # 5irR   rØ   rÛ   s   & r   rÞ   r  š  r  rà   )r:   r  r  r‰   r6   r  r’   r;   )r°   r±   rs   r  rä   rh   r  ru   rv   rt   ro   r5   rö   rô   s   jl         €€€r   r  Ú-functional_init_with_buffers.<locals>.wrappedr  sm  ú€ ô ˆ~Ó !Ô#ÜÐKÓLÐLÜˆ~Ó !Ô#Ù Ð0¨Ñ0×3Ñ3°FÓ;ˆEÜ0°Ó7Ð7Ø# AÕ&ˆ
Ø˜Œ?Ü˜{¨:¨,°nÐEÓFÐFç”÷ 
Ü=BÀ:Ô=Nó
—‘÷ 
Ü=BÀ:Ô=Nó
ó 
ˆô 7±{ÀDÐ7SÈFÑ7SÓTñ	
ØØØØØäßŒUñ á#ó�U‰Uñ á#óó ñ
Ñˆô œ˜W™Ó&ˆß”%ÑKÁ7ÓK—%‘%ÑKÁ7ÓKÓKˆÜœ˜W™Ó&ˆß”%ÑKÁ7ÓK—%‘%ÑKÁ7ÓKÓKˆØ °<Ð?Ð?r   r   r  s   fff r   Úfunctional_init_with_buffersr  c  s   ú€ ÷)@ò )@ðV €Nr   r©   )r   r   )r   Úcpu)%Ú
__future__r   r‚   Útypingr   r   r   r7   Útorch.nnrK   r   Ú%torch.nn.utils._named_member_accessorr   Úcollections.abcr	   r
   r   r   r+   rF   rN   rW   r`   rm   rq   rx   r‰   r’   ÚModuler”   rÁ   rÐ   rÓ   râ   rò   r  r  r   r   r   Ú<module>r'     s´   ðõ #ã ß /Ñ /ã Ý Ý Ý E÷ ß<Ñ<õõ ,õF$õ4EõA÷(õ"÷)÷õ4)%õX.Fôb9M "§)¡)ô 9Môx#I�r—y‘yô #I÷LE÷P<õ~õ;%÷|÷::ñ :r   