+
    jHàjÕ,  ã                  óN  € R t ^ RIHt ^ RIHtHtHt ^ RIHt ^ RI	H
t
 R#tRtRR$RR%/t0 R&mtR R	 ltR'tR
^./t ! R R]4      t] ! R R4      4       t] ! R R4      4       tR R ltR R ltR R ltRR/R R llt] ! R R4      4       tRRR^ /R R lltRRR^ /R  R! lltR"# )(zåPer-layer context-memory estimator + physics check.

The estimator is ADVISORY: fit's allocation is authoritative at launch and the touch generation is
ground truth after it. Unknown shapes round UP (never underestimate memory).
)Úannotations)Ú	dataclassÚfieldÚreplace)ÚEnum)Ú
GGUFHeaderg       @Úgemma3Úgemma2c               ó(   € V ^8„  d   QhRRRRRRRR/# )é   ÚperiodÚintÚn_layerÚdense_firstÚboolÚreturnz	list[int]© )Úformats   "ÚG/home/ubuntu/.hermes/hermes-agent/hermes_cli/local_runtime/estimator.pyÚ__annotate__r      s.   € ÷ Iñ I˜sð I¨Sð I¸tð IÈ	ñ Ió    c                óî   € V ^ 8:  d   . # V'       d+   \        V4       Uu. uF  q3V ,          ^ 8X  d   ^ M^NK  	  up# \        V4       Uu. uF  q3V ,          V ^,
          8  d   ^M^ NK  	  up# u upi u upi )z´Per-layer SWA/full split from a scalar period, matching llama.cpp's
`llama_hparams::set_swa_pattern()`: one full-attention layer per `period`-length cycle, the
rest sliding-window.)Úrange)r   r   r   Úis   &&& r   Ú_expand_swa_periodr      sm   € ð �„{Øˆ	ßÜ5:¸7´^ÓD±^°˜•Z 1”_‘¨!Ò+±^ÑDÐDÜ9>¸w¼ÓH¹°A�V•˜f q�jÔ(‰A¨aÒ/¹ÑHÐHùò EùÚHs   ŸA-Á	!A2Úqwen4expc                  ó"   € ] tR t^5tRtRtRtRtR# )Ú	LayerKindÚfullÚswaÚ	recurrentr   N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__ÚFULLÚSWAÚ	RECURRENTÚ__static_attributes__r   r   r   r   r   5   s   † Ø€DØ
€CØ„Ir   r   c                  ó  € ] tR t^;t$ RtR]R&   R]R&   R]R&   R]R&   R]R	&   ^ tR]R
&   RtR]R&   RtR]R&   ^ t	R]R&   Rt
R]R&   ]! ]R7      tR]R&   ^ tR]R&   ^ tR]R&   ^ tR]R&   ]R R l4       t]R R l4       t]R R l4       tR tR!# )"ÚModelProfilezqEverything the policy needs, decoupled from GGUF parsing so decision-table tests can
construct profiles directly.ÚstrÚnamer   Úweights_bytesÚembd_table_bytesÚn_ctx_trainzlist[tuple[LayerKind, int]]ÚlayersÚ
swa_windowFr   ÚmoeÚ ÚarchitectureÚn_vocabç      ð?ÚfloatÚkv_scale)Údefault_factoryzdict[int, int]Úffn_block_bytesÚ
lazy_bytesÚwindow_compute_per_tokenÚrecurrent_state_bytesc               ó   € V ^8„  d   QhRR/# ©r   r   r   r   )r   s   "r   r   ÚModelProfile.__annotate__^   s   € ÷ ?ñ ? cñ ?r   c                	óB   € \         P                  V P                  ^ 4      # )é    )Ú_WINDOW_COMPUTE_BYTESÚgetr4   ©Úselfs   &r   Úwindow_compute_bytesÚ!ModelProfile.window_compute_bytes]   s   € ä$×(Ñ(¨×):Ñ):¸AÓ>Ð>r   c               ó   € V ^8„  d   QhRR/# r?   r   )r   s   "r   r   r@   b   s   € ÷ Pñ P #ñ Pr   c                ó:   € \        R V P                   4       4      # )z.Uncapped per-token KV cost (full + SWA share).c              3  óX   "  € T F   w  rV\         P                  8w  g   K  Vx € K"  	  R # 5i)N©r   r'   )Ú.0ÚkindÚbs   &  r   Ú	<genexpr>Ú0ModelProfile.per_token_kv_f16.<locals>.<genexpr>d   ó!   é € ÐO¡K™˜°4¼9×;NÑ;NÑ3N—1’1£Kùó   ‚* 
*©Úsumr0   rE   s   &r   Úper_token_kv_f16ÚModelProfile.per_token_kv_f16a   s   € ô ÑO D§K¢KÓOÓOÐOr   c               ó   € V ^8„  d   QhRR/# r?   r   )r   s   "r   r   r@   g   s   € ÷ Pñ P sñ Pr   c                	ó:   € \        R  V P                   4       4      # )c              3  óX   "  € T F   w  rV\         P                  8X  g   K  ^x € K"  	  R# 5i)é   NrL   )rM   rN   Ú_s   &  r   rP   Ú5ModelProfile.recurrent_layer_count.<locals>.<genexpr>h   rR   rS   rT   rE   s   &r   Úrecurrent_layer_countÚ"ModelProfile.recurrent_layer_countf   s   € äÑO D§K¢KÓOÓOÐOr   r   N)r!   r"   r#   r$   Ú__doc__Ú__annotations__r1   r2   r4   r5   r8   r   Údictr:   r;   r<   r=   ÚpropertyrG   rV   r^   r(   r   r   r   r*   r*   ;   sÏ   ‡ ñ$ð ƒIð ÓØÓØÓØ'Ó'Ø€J�ÓØ€CˆÓØ€L�#ÓØ€GˆSÓð
 €HˆeÓá&+¸DÔ&A€O�^ÓAð €J�Óð %&Ð˜cÓ%ð "#Ð˜3Ó"àô?ó ð?ð ôPó ðPð ôPó ôPr   r*   c                  óŽ   € ] tR t^kt$ RtR]R&   R]R&   R]R&   RtR]R&   R	tR
]R&   R	tR
]R&   Rt	R]R&   Rt
R]R&   RtR]R&   RtR# )ÚHardwareBudgeta  Memory the physics check may budget against. Discrete cards may trust the device query;
unified-memory devices must budget from OS free memory minus headroom (device queries observed
off by 3x). Callers construct this accordingly; the estimator just consumes it.r   Úusable_vram_bytesÚtotal_device_bytesÚram_available_bytesFr   Úumar3   r+   Úgpu_nameÚplatformNz
int | NoneÚ
gpu_pci_idTÚ
lazy_readsÚcommit_room_bytesr   )r!   r"   r#   r$   r`   ra   ri   rj   rk   rl   rm   rn   r(   r   r   r   re   re   k   s]   ‡ ñWð ÓØÓØÓØ€CˆÓØ€HˆcÓØ€HˆcÓØ!€J�
Ó!ð €J�Óð %)Ð�z×(r   re   c               ó$   € V ^8„  d   QhRRRRRR/# )r   Úprofiler*   Úbudgetre   r   r   )r   s   "r   r   r   ‚   s!   € ÷ ñ �|ð ¨^ð Àñ r   c                óž   € V P                   '       d;   VP                  '       g)   \        W P                  V P                   ,           ^ R7      # V # )znThe profile as this machine loads it: on-demand tensors count as weights where the engine
reads them up front.)r-   r;   )r;   rm   r   r-   )rp   rq   s   &&r   Ú	as_loadedrs   ‚   s?   € ð ××Ð &×"3×"3Ð"3Ü�w×.CÑ.CÀg×FXÑFXÕ.XÐefÔgÐgØ€Nr   c               ó    € V ^8„  d   QhRRRR/# )r   Úheaderr   r   r*   r   )r   s   "r   r   r   Š   s   € ÷ ,<ñ ,<˜jð ,<¨\ñ ,<r   c                ó¢  € V P                  4       pV P                  V P                  r2V P                  ;'       g    TpV P                  ;'       g    TpV P
                  pVfD   V P                  ^ 8”  d3   \        V P                  V P                  V P                  \        9   4      pVR J;'       d/    V P                  ^ 8„  ;'       d    \        V4      \        V4      8H  p\        P                  V P                  R4      pV'       * ;'       d    V P                  ^ 8„  ;'       d    V^ 8„  p	\        R V 4       4      p
V	'       d   \!        W¨,          4      M^ p. p^ p\#        V4       FÄ  w  rïV^ 8X  d$   VP%                  \&        P(                  ^ 34       K/  V'       d   \+        Wn,          4      pMWÛ8  pV'       d   WE3MW#3w  pp\!        VVV,           ,          \,        ,          4      pTP%                  V'       d   \&        P.                  M\&        P0                  V34       V^,          pKÆ  	  \3        V P4                  V P6                  V P8                  ,
          V P:                  V P<                  WÀP                  V P>                  ^ 8„  V P                  V P@                  \C        V PD                  4      V P8                  V PF                  R7      # )Nç        c              3  ó6   "  € T F  q^ 8”  g   K  ^x € K  	  R# 5i)rB   Nr   )rM   Úhs   & r   rP   Ú$profile_from_gguf.<locals>.<genexpr>œ   s   é € Ð4¡(˜Q°!©e—q’q£(ùs   ‚�
)r,   r-   r.   r/   r0   r1   r2   r4   r5   r:   r;   r=   )$Úhead_counts_kvÚ
head_dim_kÚ
head_dim_vÚkey_length_swaÚvalue_length_swaÚsliding_window_patternÚsliding_window_pattern_periodr   r   r4   Ú_SWA_DENSE_FIRST_ARCHSÚsliding_windowÚlenÚ_SWA_LAYER_FRACTIONrD   rU   ÚroundÚ	enumerateÚappendr   r'   r   Ú_F16_BYTES_PER_ELEMr&   r%   r*   ÚpathÚtensor_bytesr;   r.   r/   Úexpert_countr5   rb   r:   r=   )ru   Úkv_headsÚdkÚdvÚdk_swaÚdv_swaÚpatternÚhas_patternÚswa_fractionÚhas_fractionÚn_attn_totalÚn_swar0   Ún_attn_seenr   ÚheadsÚis_swaÚlayer_dkÚlayer_dvÚ	per_tokens   &                   r   Úprofile_from_ggufrž   Š   sV  € Ø×$Ñ$Ó&€HØ×Ñ × 1Ñ 1ˆØ×"Ñ"×(Ð( b€FØ×$Ñ$×*Ð*¨€Fð ×+Ñ+€GØ‚˜6×?Ñ?À!ÔCÜ$ V×%IÑ%IÈ6Ï>É>Ø&,×&9Ñ&9Ô=SÑ&SóUˆà $Ð&÷ 4ð 4¨6×+@Ñ+@À1Ñ+D÷ 4ð 4Ü˜'“l¤c¨(£mÑ3ð ä&×*Ñ*¨6×+>Ñ+>ÀÓD€LØ"”?×UÐU v×'<Ñ'<¸qÑ'@×UÐUÀ\ÐTUÑEU€LÜÑ4¡(Ó4Ó4€Lß2>ŒE�,Õ-Ô.ÀA€Eà*,€FØ€KÜ˜hÖ'‰ˆØ�AŒ:Ø�M‰Mœ9×.Ñ.°Ð2Ô3ÙßÜ˜'�*Ó%‰Fð !Ñ(ˆFß17˜fÑ-¸b¸XÑˆ�(Ü˜% 8¨hÕ#6Õ7Ô:MÕMÓNˆ	Ø�‰¯”y—}’}´I·N±NÀIÐNÔOØ�qÕŠñ (ô Ø�[‰[¨×(;Ñ(;¸f×>OÑ>OÕ(OØ×0Ñ0Ø×&Ñ&¨v×BWÑBWØ×Ñ !Ñ#°&×2EÑ2EÈvÏ~É~Ü˜V×3Ñ3Ó4À×ARÑARØ$×:Ñ:ô<ð <r   c               ó    € V ^8„  d   QhRRRR/# )r   Úflash_attentionr   r   r7   r   )r   s   "r   r   r   ¹   s   € ÷ Rñ R Tð R¨eñ Rr   c                ó8   € V '       d   \         \        ,          # R# )u›   q8_0 with FA (every backend we ship); f16 on exotic non-FA fallbacks â€” the 64K guarantee
stands either way, the physics check just prices the doubled KV.r6   )Ú_Q8_BYTES_PER_ELEMr‰   ©r    s   &r   Úkv_dtype_factorr¤   ¹   s   € ÷ :IÔÔ!4Õ4ÐQÈcÐQr   r    Tc               ó(   € V ^8„  d   QhRRRRRRRR/# )r   rp   r*   Úwindowr   r    r   r   r   )r   s   "r   r   r   ¿   s/   € ÷ Uñ U�|ð U¨Sð UÀdð UÐVYñ Ur   c               ó   € \        V4      pRpV P                   F  w  rVV\        P                  8X  d   V\        ,          pK)  V\        P
                  8X  d,   WFV,          \        WP                  4      ,          ,          pKi  WFV,          V,          ,          pK�  	  \        W@P                  ,          4      V P                  V,          ,           # )zäMemory that grows with the window: full layers linear in T, SWA layers capped at the sliding
window, recurrent layers constant, KV scaled by profile.kv_scale (MTP draft context), plus the
posture's window-scaled compute buffers.rw   )r¤   r0   r   r'   Ú_RECURRENT_STATE_PER_LAYERr&   Úminr1   r   r8   r<   )rp   r¦   r    ÚfactorÚtotalrN   Úper_token_f16s   &&$    r   Ú	ctx_bytesr­   ¿   sž   € ô ˜_Ó-€FØ€EØ&Ÿ~œ~ÑˆØ”9×&Ñ&Ô&ØÔ/Õ/ŠEØ”Y—]‘]Ô"Ø VÕ+¬c°&×:LÑ:LÓ.MÕMÕMŠEà VÕ+¨fÕ4Õ4ŠEñ  .ô ˆu×'Ñ'Õ'Ó(¨7×+KÑ+KÈfÕ+TÕTÐTr   c                  ó:   € ] tR t^Ït$ RtR]R&   R]R&   R]R&   RtR# )	ÚPhysicsRefusalz{The only true refusal: weights + floor-KV + state exceed VRAM + RAM. The remedy is a
smaller quant, never a smaller window.r   Úneeded_bytesÚavailable_bytesr+   Úmessager   N)r!   r"   r#   r$   r`   ra   r(   r   r   r   r¯   r¯   Ï   s   ‡ ñ.ð ÓØÓØ‡Lr   r¯   Úoverhead_bytesc          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )	r   rp   r*   r¦   r   r    r   r³   r   r   )r   s   "r   r   r   Ù   s1   € ÷ &ñ &˜\ð &°3ð &ÈDð &Ø$'ð&Ø03ñ&r   c               ó`   € V P                   \        WVR7      ,           \        ^ V4      ,           # )zOComplete estimated footprint; the hardware budget already excludes its reserve.r£   )r-   r­   Úmax)rp   r¦   r    r³   s   &&$$r   Úfootprint_bytesr·   Ù   s-   € ð ×!Ñ!¤I¨gÈÔ$_Õ_Ü�!�^Ó$õ%ð &r   c               ó0   € V ^8„  d   QhRRRRRRRRR	RR
R/# )r   rp   r*   rq   re   Úfloorr   r    r   r³   r   zPhysicsRefusal | Noner   )r   s   "r   r   r   à   sB   € ÷ dñ d˜<ð d°ð dØðdØ26ðdà"%ðdà.Cñdr   c               ó  € \        T \        Y P                  ;'       g    T4      W4R 7      pVP                  VP                  ,           pWV8:  d   R# R	p\        WVV P                   RWW,          R RVR,           RWg,          R R2R7      # )
)r    r³   Nz	: needs ~z.1fz GiB at the i   zK floor but only ~uS    GiB of VRAM+RAM are available â€” try a smaller model or a supported smaller quant)r°   r±   r²   i   @)r·   r©   r/   rf   rh   r¯   r,   )rp   rq   r¹   r    r³   ÚneededÚ	availableÚgibs   &&&$$   r   Úphysics_checkr¾   à   s—   € ô ˜W¤c¨%×1DÑ1D×1MÐ1MÈÓ&NØ-<ô]€Fà×(Ñ(¨6×+EÑ+EÕE€IØÔÙØ
€CÜØØ—L‘L�> ¨6­<¸Ð*<¸LØ˜T•M�?Ð"4°Yµ_ÀSÐ4Ið Jbðbôdð dr   Ng      ñ?g«ªªªªªê?g      à?>   ÚlagunaÚ
cohere2moeÚsmallthinkerúmodern-berti  @ )r`   Ú
__future__r   Údataclassesr   r   r   Úenumr   Úhermes_cli.local_runtime.ggufr   r¢   r‰   r…   r‚   r   r¨   rC   r   r*   re   rs   rž   r¤   r­   r¯   r·   r¾   r   r   r   Ú<module>rÇ      s  ðñõ #ç 1Ñ 1Ý å 4ð Ð ØÐ ð   ¨°%Ð8Ð ò QÐ õIð %Ð ð $ RÐ(Ð ô�ô ð ÷,Pð ,Pó ð,Pð^ ÷)ð )ó ð)õ,õ,<õ^RðUÈT÷ Uð  ÷ð ó ðð&ÐSWð &Ø*+÷&ðdØ9=ðdà()÷dñ dr   