+
    &jNo                        ^ RI t ^ RIHtHtHt ^ RIHtHtHtH	t	H
t
HtHt ^ RIHt ^ RIt^ RIHt ^RIHt ^RIHtHt R R ltR(R	 R
 llt]! RR7       ! R R4      4       t]! RR7       ! R R4      4       t ! R R4      t ! R R4      t ! R R]4      t ! R R4      t ]PB                  R 4       t" ! R R] 4      t# ! R R4      t$ ! R R]$4      t% ! R  R!]$4      t& ! R" R#]$4      t' ! R$ R%4      t(]! 4       3R& R' llt)R# ))    N)	dataclassfieldreplace)DictIterableListOptionalSequenceTupleUnion)tree_map)CHUNK_LENGTH)	Tokenizerget_tokenizerc                $    V ^8  d   QhR\         /#    return)float)formats   "l/Users/jameslopez/projects/CWCArchive/cwc-podcast/.venv/lib/python3.14/site-packages/mlx_whisper/decoding.py__annotate__r      s     < <u <    c                     V P                  R 4      p\        V4      \        \        P                  ! V4      4      ,          # )zutf-8)encodelenzlibcompress)text
text_bytess   & r   compression_ratior!      s-    W%Jz?Sz!:;;;r   c                    V ^8  d   QhRRR\         P                  R\        R\        \         P                  \        \
        ,          3,          /# )r   modelWhispermel	tokenizerr   )mxarrayr   r   r   dict)r   s   "r   r   r      sB     ;+ ;+;+88;+09;+
288T$Z ;+r   c                   Vf"   \        V P                  V P                  R7      pVP                  e   VP                  VP
                  9  d   \        R4      hVP                  ^8H  pV'       d
   VR,          pVP                  RR V P                  P                  V P                  P                  38w  d   V P                  V4      pVP                  ^ ,          p\        P                  ! VP                  ..V,          4      pV P!                  WQ4      R,          p\        P"                  ! VP                  R	,          \        P$                  ) \        P&                  R7      pRV\)        VP*                  4      &   Wg,          p\        P,                  ! VR	R7      p\        P.                  ! VR	R7      p	\0        P                  ! V	4      p	\3        V4       U
UUu. uFM  p
\5        VP*                  VP6                  4       UUu/ uF  w  rWW3,          P9                  4       bK  	  uppNKO  	  ppp
pV'       d   V^ ,          pV^ ,          pW3# u uppi u uppp
i )
aI  
Detect the spoken language in the audio, and return them as list of strings, along with the ids
of the most probable language tokens and the probability distribution over all language tokens.
This is performed outside the main decode loop in order to not interfere with kv-caching.

Returns
-------
language_tokens : mx.array, shape = (n_audio,)
    ids of the most probable language tokens, which appears after the startoftranscript token.
language_probs : List[Dict[str, float]], length = n_audio
    list of dictionaries containing the probability distribution over all languages.
N)num_languageszCThis model doesn't have language tokens so it can't perform lang id)dtype        axis)NNNr   )r   is_multilingualr+   languagelanguage_tokensot_sequence
ValueErrorndimshapedimsn_audio_ctxn_audio_stateencoderr'   r(   sotlogitsfullinffloat32listall_language_tokensargmaxsoftmaxnprangezipall_language_codesitem)r#   r%   r&   singlen_audioxr?   masklanguage_tokenslanguage_token_probsijclanguage_probss   &&&           r   detect_languagerV      s    !!!1D1D
	 	"##9+A+AAQ
 	
 XX]F$i yy~%**00%**2J2JKKmmC  iilG
9==/"W,-A\\!!$'F 776<<#bffWBJJ?D03Di++	,-
NFiiR0O::f2688$89 w
  A I999;W;WX	
X AD)..00X	
     )!,'***	
s   (I#I$IIT)frozenc                   h   a  ] tR t^Rt o RtRtRtRtRtRt	Rt
RtRtRtRtRtRtRtRtV 3R ltR	tV tR# )
DecodingOptions
transcribeNr-   z-1TFg      ?c                  < V ^8  d   Qh/ S[ ;R&   S[S[ ,          ;R&   S[;R&   S[S[,          ;R&   S[S[,          ;R&   S[S[,          ;R&   S[S[,          ;R&   S[S[,          ;R&   S[S[S[ S[S[,          3,          ,          ;R	&   S[S[S[ S[S[,          3,          ,          ;R
&   S[S[S[ S[S[,          3,          ,          ;R&   S[;R&   S[;R&   S[S[,          ;R&   S[;R&   # )r   taskr4   temperature
sample_lenbest_of	beam_sizepatiencelength_penaltypromptprefixsuppress_tokenssuppress_blankwithout_timestampsmax_initial_timestampfp16)strr	   r   intr   r   r   bool)r   __classdict__s   "r   r   DecodingOptions.__annotate__R   s       sm"    $  c]!  }#  uo$ " UO*# * U3S	>*+2+ , U3S	>*+2- 4 eC#$678?5 6 7 < $= > $E?0? D E r    )__name__
__module____qualname____firstlineno__r\   r4   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   rh   ri   __annotate_func____static_attributes____classdictcell__rm   s   @r   rY   rY   R   sk      D #H K $J!G#I $H '+N /3F.2F <@ON  %-0 DE  r   rY   c                      a  ] tR t^wt o Rt]! ]R7      tRt]	P                  t]	P                  t]	P                  t]	P                  tV 3R ltRtV tR# )DecodingResultN)default_factory c                   < V ^8  d   Qh/ S[ P                  ;R&   S[;R&   S[S[S[S[3,          ,          ;R&   S[S[,          ;R&   S[;R&   S[;R&   S[;R&   S[;R&   S[;R	&   # )
r   audio_featuresr4   rU   tokensr   avg_logprobno_speech_probr]   r!   )r'   r(   rj   r	   r   r   r   rk   )r   rm   s   "r   r   DecodingResult.__annotate__w   s     HH  M  T#u*-.5	 
 I3  N    "    % r   ro   )rp   rq   rr   rs   rU   r   rC   r~   r   rG   nanr   r   r]   r!   rt   ru   rv   rw   s   @r   ry   ry   w   sG      26Nd3FDKFFNK!vv  r   ry   c                   P   a  ] tR t^t o V 3R lR ltV 3R lR ltR tR tRtV t	R# )		Inferencec                   < V ^8  d   QhRR/# )r   r#   r$   ro   )r   rm   s   "r   r   Inference.__annotate__   s      i r   c                     Wn         R V n        R # N)r#   kv_cache)selfr#   s   &&r   __init__Inference.__init__   s     %
r   c                h   < V ^8  d   QhRS[ P                  RS[ P                  RS[ P                  /# )r   r~   r}   r   r'   r(   )r   rm   s   "r   r   r      s.     ) )RXX )rxx )BHH )r   c                    V P                   P                  WV P                  R7      w  q0n        pVP                  \        P
                  4      # )zAPerform a forward pass on the decoder and return per-token logitsr   )r#   decoderr   astyper'   rB   )r   r~   r}   r?   _s   &&&  r   r?   Inference.logits   s@    #'::#5#5T]] $6 $
 q }}RZZ((r   c                   a S\        \        \        S4      4      4      8w  d"   \        V3R lV P                  4      V n        R# R# )z9Update the key-value cache according to the updated beamsc                    < V S,          # r   ro   )rN   source_indicess   &r   <lambda>.Inference.rearrange_kv_cache.<locals>.<lambda>   s
    q/@r   N)rC   rH   r   r   r   )r   r   s   &fr   rearrange_kv_cacheInference.rearrange_kv_cache   s4     T%N(;"<==$%@$--PDM >r   c                    R V n         R # r   r   r   s   &r   resetInference.reset   s	    r   )r   r#   N)
rp   rq   rr   rs   r   r?   r   r   ru   rv   rw   s   @r   r   r      s)      ) )Q r   r   c                   2   a  ] tR t^t o V 3R lR ltRtV tR# )SequenceRankerc                   < V ^8  d   QhRS[ S[ S[P                  ,          ,          RS[ S[ S[,          ,          RS[ S[,          /# r   r~   sum_logprobsr   )r   r'   r(   r   rk   )r   rm   s   "r   r   SequenceRanker.__annotate__   s?     " "4>*":>tE{:K"	c"r   c                    \         h)z
Given a list of groups of samples and their cumulative log probabilities,
return the indices of the samples in each group to select as the final result
NotImplementedErrorr   r~   r   s   &&&r   rankSequenceRanker.rank   s
     "!r   ro   N)rp   rq   rr   rs   r   ru   rv   rw   s   @r   r   r      s     " "r   r   c                   H   a  ] tR t^t o RtV 3R lR ltV 3R lR ltRtV tR# )MaximumLikelihoodRankerz
Select the sample with the highest log probabilities, penalized using either
a simple length normalization or Google NMT paper's length penalty
c                0   < V ^8  d   QhRS[ S[,          /# )r   rb   )r	   r   )r   rm   s   "r   r   $MaximumLikelihoodRanker.__annotate__   s     - -x -r   c                    Wn         R # r   rb   )r   rb   s   &&r   r    MaximumLikelihoodRanker.__init__   s    ,r   c                v   < V ^8  d   QhRS[ S[ S[ S[,          ,          ,          RS[ S[ S[,          ,          /# r   r~   r   )r   rk   r   )r   rm   s   "r   r   r      s5     P P4T#Y0 PT%[@Q Pr   c           
        a  V 3R  lpV UUu. uF  qD Uu. uF  p\        V4      NK  	  upNK  	  ppp\        W&4       UUu. uF!  w  rx\        P                  ! V! Wx4      4      NK#  	  upp# u upi u uppi u uppi )c                    < . p\        W4       FN  w  r4SP                  f   TpM!^V,           ^,          SP                  ,          pVP                  W5,          4       KP  	  V# r   )rI   rb   append)logprobslengthsresultlogproblengthpenaltyr   s   &&    r   scores,MaximumLikelihoodRanker.rank.<locals>.scores   s[    F#&x#9&&.$G !"F
a/D4G4GGGg/0 $: Mr   )r   rI   rG   rE   )	r   r~   r   r   str   pls	   f&&      r   r   MaximumLikelihoodRanker.rank   sg    		 1771A&AqCFA&7474NO4NDA		&,'4NOO '7Os   A1A,A1'A7,A1r   N)	rp   rq   rr   rs   __doc__r   r   ru   rv   rw   s   @r   r   r      s      
- -P Pr   r   c                   J   a  ] tR t^t o R tV 3R lR ltV 3R lR ltRtV tR# )TokenDecoderc                    R# )z=Initialize any stateful variables for decoding a new sequenceNro   r   s   &r   r   TokenDecoder.reset   s    r   c                   < V ^8  d   QhRS[ P                  RS[ P                  RS[ P                  RS[S[ P                  S[S[ P                  3,          /# r   r~   r?   r   r   r'   r(   r   rl   )r   rm   s   "r   r   TokenDecoder.__annotate__   sM     " "hh"(*"@B"	rxxrxx'	("r   c                    \         h)a  Specify how to select the next token, based on the current trace and logits

Parameters
----------
tokens : mx.array, shape = (n_batch, current_sequence_length)
    all tokens in the context so far, including the prefix and sot_sequence tokens

logits : mx.array, shape = (n_batch, vocab_size)
    per-token logits of the probability distribution at the current step

sum_logprobs : mx.array, shape = (n_batch)
    cumulative log probabilities for each sequence

Returns
-------
tokens : mx.array, shape = (n_batch, current_sequence_length + 1)
    the tokens, appended with the selected next token

completed : bool
    True if all sequences has reached the end of text

sum_logprobs: mx.array, shape = (n_batch)
    updated cumulative log probabilities for each sequence

r   )r   r~   r?   r   s   &&&&r   updateTokenDecoder.update   s
    8 "!r   c          
         < V ^8  d   QhRS[ P                  RS[ P                  RS[S[S[S[ P                  ,          ,          S[S[S[,          ,          3,          /# r   )r'   r(   r   r
   r   r   )r   rm   s   "r   r   r      sN     " "hh".0hh"	x*+T$u+->>	?"r   c                    \         h)af  Finalize search and return the final candidate sequences

Parameters
----------
tokens : mx.array, shape = (n_audio, n_group, current_sequence_length)
    all tokens in the context so far, including the prefix and sot_sequence

sum_logprobs : mx.array, shape = (n_audio, n_group)
    cumulative log probabilities for each sequence

Returns
-------
tokens : Sequence[Sequence[mx.array]], length = n_audio
    sequence of mx.arrays containing candidate token sequences, for each audio input

sum_logprobs : List[List[float]], length = n_audio
    sequence of cumulative log probabilities corresponding to the above

r   r   s   &&&r   finalizeTokenDecoder.finalize   s
    , "!r   ro   N)	rp   rq   rr   rs   r   r   r   ru   rv   rw   s   @r   r   r      s     L" "<" "r   r   c                 L    \         P                  P                  W,          4      # r   )r'   randomcategorical)r?   temps   &&r   r   r      s    99  //r   c                   V   a  ] tR t^t o V 3R lR ltV 3R lR ltV 3R lR ltRtV tR# )	GreedyDecoderc                &   < V ^8  d   QhRS[ RS[/# )r   r]   eot)r   rk   )r   rm   s   "r   r   GreedyDecoder.__annotate__   s      E  r   c                    Wn         W n        R # r   )r]   r   )r   r]   r   s   &&&r   r   GreedyDecoder.__init__   s    &r   c                   < V ^8  d   QhRS[ P                  RS[ P                  RS[ P                  RS[S[ P                  S[S[ P                  3,          /# r   r   )r   rm   s   "r   r   r     sM     / /hh/(*/@B/	rxxrxx'	(/r   c                r   V P                   ^ 8X  d   VP                  RR7      pM\        W P                   4      pV\        P                  ! VRRR7      ,
          pV\        P
                  ! VP                  ^ ,          4      V3,          pW6VR,          V P                  8g  ,          ,          pVR,          V P                  8H  pV^V,
          ,          V P                  V,          ,           p\        P                  ! WR,          .RR7      p\        P                  ! VR,          V P                  8H  4      pWV3# )r   r.   Tr/   keepdimsr2   r1   r2   )r1   N)
r]   rE   r   r'   	logsumexparanger9   r   concatenateall)	r   r~   r?   r   next_tokensr   current_logprobseot_mask	completeds	   &&&&     r   r   GreedyDecoder.update  s     q  --R-0K%f.>.>?KBLLb4HH#BIIhnnQ.?$@+$MNF5MTXX,EFF%=DHH,!Q\2TXX5HHW)= >RHFF6%=DHH45	,..r   c                N   < V ^8  d   QhRS[ P                  RS[ P                  /# r   r   )r   rm   s   "r   r   r     s#     $ $rxx $rxx $r   c                R    \         P                  ! V. ROV P                  R7      pW3# )r   )constant_values)r   r   r   )r      )r'   padr   r   s   &&&r   r   GreedyDecoder.finalize  s"     8$((S##r   )r   r]   N)	rp   rq   rr   rs   r   r   r   ru   rv   rw   s   @r   r   r      s#      / /($ $r   r   c                   2   a  ] tR tRt o V 3R lR ltRtV tR# )LogitFilteri  c                h   < V ^8  d   QhRS[ P                  RS[ P                  RS[ P                  /# r   r?   r~   r   r   )r   rm   s   "r   r   LogitFilter.__annotate__  s.     " "BHH "bhh "288 "r   c                    \         h)aM  Apply any filtering or masking to logits

Parameters
----------
logits : mx.array, shape = (n_batch, vocab_size)
    per-token logits of the probability distribution at the current step

tokens : mx.array, shape = (n_batch, current_sequence_length)
    all tokens in the context so far, including the prefix and sot_sequence tokens

r   r   r?   r~   s   &&&r   applyLogitFilter.apply  s
     "!r   ro   N)rp   rq   rr   rs   r   ru   rv   rw   s   @r   r   r     s     " "r   r   c                   D   a  ] tR tRt o V 3R lR ltV 3R lR ltRtV tR# )SuppressBlanki.  c                ,   < V ^8  d   QhRS[ RS[RS[/# )r   r&   sample_beginn_vocab)r   rk   )r   rm   s   "r   r   SuppressBlank.__annotate__/  s"     # #) #3 # #r   c                    W n         \        P                  ! V\        P                  4      p\        P                  ) WAP                  R 4      VP                  .,           &   \        P                  ! V4      V n	        R#  N)
r  rG   zerosrB   rA   r   r   r'   r(   rO   )r   r&   r  r  rO   s   &&&& r   r   SuppressBlank.__init__/  sO    (xx,9;c"imm_45HHTN	r   c                h   < V ^8  d   QhRS[ P                  RS[ P                  RS[ P                  /# r   r   )r   rm   s   "r   r   r  5  s.      BHH bhh 288 r   c                n    VP                   ^,          V P                  8X  d   WP                  ,           # V# )r   )r9   r  rO   r   s   &&&r   r   SuppressBlank.apply5  s*    <<?d///II%%r   )rO   r  Nrp   rq   rr   rs   r   r   ru   rv   rw   s   @r   r   r   .  s     # # r   r   c                   D   a  ] tR tRt o V 3R lR ltV 3R lR ltRtV tR# )SuppressTokensi;  c                6   < V ^8  d   QhRS[ S[,          RS[/# )r   re   r  )r
   rk   )r   rm   s   "r   r   SuppressTokens.__annotate__<  s     # # # #r   c                    \         P                  ! V\         P                  4      p\         P                  ) V\	        V4      &   \
        P                  ! V4      V n        R # r   )rG   r  rB   rA   rC   r'   r(   rO   )r   re   r  rO   s   &&& r   r   SuppressTokens.__init__<  s:    xx,')vvgT/"#HHTN	r   c                h   < V ^8  d   QhRS[ P                  RS[ P                  RS[ P                  /# r   r   )r   rm   s   "r   r   r  A  s.     " "BHH "bhh "288 "r   c                &    WP                   ,           # r   rO   r   s   &&&r   r   SuppressTokens.applyA  s    		!!r   r  Nr  rw   s   @r   r  r  ;  s     # #
" "r   r  c                   D   a  ] tR tRt o V 3R lR ltV 3R lR ltRtV tR# )ApplyTimestampRulesiE  c                <   < V ^8  d   QhRS[ RS[RS[S[,          /# )r   r&   r  max_initial_timestamp_index)r   rk   r	   )r   rm   s   "r   r    ApplyTimestampRules.__annotate__F  s2     G GG G &.c]	Gr   c                *    Wn         W n        W0n        R # r   )r&   r  r  )r   r&   r  r  s   &&&&r   r   ApplyTimestampRules.__init__F  s     #(+F(r   c                h   < V ^8  d   QhRS[ P                  RS[ P                  RS[ P                  /# r   r   )r   rm   s   "r   r   r  P  s.     ; ;BHH ;bhh ;288 ;r   c                   \         P                  ! VP                  \         P                  4      pV P                  P
                  e+   \         P                  ) VRV P                  P
                  3&   VP                  4       p\        \        V4      4       EF  pW$,          V P                  R  p\        V4      ^8  ;'       d!    VR,          V P                  P                  8  p\        V4      ^8  ;'       g!    VR,          V P                  P                  8  pV'       d`   V'       d-   \         P                  ) W4V P                  P                  R 13&   M+\         P                  ) W4R V P                  P                  13&   \        V4       UU	u. uF#  w  rWP                  P                  8  g   K!  VNK%  	  p
pp	\        V
4      ^ 8  g   EK9  V
R,          pV'       d	   V'       d
   V^,          p\         P                  ) W4V P                  P                  V13&   EK  	  \        V^ ,          4      V P                  8X  d   \         P                  ) VRR V P                  P                  13&   V P                  eG   V P                  P                  V P                  ,           p\         P                  ) VRV^,           R 13&   \        P                   ! V4      pV\        P"                  ! VRRR7      ,
          pVRV P                  P                  R 13,          P#                  RRR7      pVRR V P                  P                  13,          P%                  RRR7      p\        P&                  ! W8  \        P                  ) VRR V P                  P                  13,          4      VRR V P                  P                  13&   W,           # u up	pi )Nr1   Tr   r2   r0   )rG   r  r9   rB   r&   no_timestampsrA   tolistrH   r   r  timestamp_beginr   	enumerater  r'   r(   r   maxwhere)r   r?   r~   rO   kseqlast_was_timestamppenultimate_was_timestamprR   v
timestampslast_timestamplast_allowedr   timestamp_logprobmax_text_token_logprobs   &&&             r   r   ApplyTimestampRules.applyP  s   xxbjj1>>''357VVGDDNN0001 s6{#A)D--/0CCAKK#b'T^^-K-K"K  C1IIB4>>+I+I I & ",ACDDNN::<<=57VVGD0dnn00001 (n,daNN4R4R0Rn   :" ",B%)B"a'NLNFF766GGH1 $4 vay>T...9;D4dnn44445 //;NN22T5U5UU  02vvgQq(**+ xx~BLLb4HH$Q(F(F(H%HISSd T 
 "*!-Mt~~/M/M-M*M!N!R!Rd "S "
 57HH6VVG4dnn444455
Q0$..00001
 }Is   O3O)r  r  r&   Nr  rw   s   @r   r  r  E  s     G G; ;r   r  c                      a  ] tR tRt o V 3R lR ltV 3R lR ltV 3R lR ltV 3R lR	 ltV 3R
 lR ltV 3R lR lt	V 3R lR lt
V 3R lR ltV 3R ltRtV tR# )DecodingTaski  c                $   < V ^8  d   QhRRRS[ /# )r   r#   r$   optionsrY   )r   rm   s   "r   r   DecodingTask.__annotate__  s     ; ;i ;/ ;r   c                   Wn         VP                  ;'       g    R p\        VP                  VP                  VVP
                  R7      pW@n        V P                  V4      V n        VP                  ;'       g    VP                  ;'       g    ^V n        VP                  P                  V n        VP                  ;'       g    VP                  P                  ^,          V n        VP                   V n        V P                  P"                  '       d   VP$                  V n        V P'                  4       V n        \+        V P(                  4      V n        V P(                  P/                  VP0                  4      V n        \5        V4      V n        \9        VP:                  4      V n        VP                  e   \?        R4      h\A        VPB                  VPD                  4      V n#        . V n$        V P                  PJ                  '       dO   V PH                  PM                  \O        V P                  V P,                  VP                  PP                  4      4       V P                  PR                  '       dH   V PH                  PM                  \U        V PW                  4       VP                  PP                  4      4       VP"                  '       g   \X        VP                  PZ                  ,          pRpVP\                  '       d'   \_        V P                  P\                  V,          4      pV PH                  PM                  \a        W@P,                  V4      4       R# R# )en)r+   r4   r\   Nz*Beam search decoder is not yet implemented)1r#   r4   r   r3   r+   r\   r&   _verify_optionsr4  r`   r_   n_groupr:   
n_text_ctxn_ctxr^   r6   rg   #sot_sequence_including_notimestamps_get_initial_tokensinitial_tokensr   r  indexr>   	sot_indexr   	inferencer   rb   sequence_rankerr   r   r]   r   r   logit_filtersrf   r   r   r  re   r  _get_suppress_tokensr   r;   rh   roundr  )r   r#   r4  r4   r&   	precisionr  s   &&&    r   r   DecodingTask.__init__  s   
##++t!!!--	
	 %.(,(<(<W(E#--EEEEA**//
&11OOUZZ5J5Ja5O(1(>(><<*** ) M MD*.*B*B*D!$T%8%8!9"1177	F #5)  7w7M7MN (%&RSS()<)<immLDL  <<&&&%%dnnd.?.?ASAST <<'''%%t88:EJJ<N<NO )))$uzz'='==I*.',,,.3LL66B/+ %%#002M *r   c                &   < V ^8  d   QhRS[ RS[ /# )r   r4  r   r5  )r   rm   s   "r   r   r6    s       ? r   c                ^   VP                   e   VP                  e   \        R4      hVP                  ^ 8X  d   VP                  e   \        R4      hVP                  e   VP                   f   \        R4      hVP
                  e'   ^ VP
                  u;8:  d   ^8:  g   M \        R4      hV# )Nz-beam_size and best_of can't be given togetherz4best_of with greedy sampling (T=0) is not compatiblez'patience requires beam_size to be givenz8length_penalty (alpha) should be a value between 0 and 1)r`   r_   r7   r]   ra   rb   )r   r4  s   &&r   r9  DecodingTask._verify_options  s    (W__-HLMM!#* !WXX'G,=,=,EFGG!!-'',1,WXXr   c                0   < V ^8  d   QhRS[ S[,          /# r   r   rk   )r   rm   s   "r   r   r6    s      U3Z r   c                   \        V P                  4      pV P                  P                  ;p'       d   \	        V\
        4      '       d1   V P                  P                  R VP                  4       ,           4      MTpV P                  e*   V P                  ^,          V P                  ,
          pW4) R pW,           pV P                  P                  ;p'       d   \	        V\
        4      '       d1   V P                  P                  R VP                  4       ,           4      MTpV P                  P                  .W`P                  ^,          ^,
          ) R ,           V,           p\        V4      # r  )rC   r6   r4  rd   
isinstancerj   r&   r   stripr^   r<  rc   sot_prevtuple)r   r~   rd   prefix_tokensmax_prefix_lenrc   prompt_tokenss   &      r   r>   DecodingTask._get_initial_tokens  s(   d''(\\(((6( fc** %%cFLLN&:; 
 *!%q4??!B -o.> ?+F\\(((6( fc** %%cFLLN&:;  (()**/A"5 6 89:  V}r   c                0   < V ^8  d   QhRS[ S[,          /# r   rM  )r   rm   s   "r   r   r6    s     3 3eCj 3r   c                \   V P                   P                  p\        V\        4      '       d*   VP	                  R 4       Uu. uF  p\        V4      NK  	  ppRV9   d@   V Uu. uF  q"^ 8  g   K  VNK  	  ppVP                  V P                  P                  4       M4Ve   \        V4      ^ 8X  d   . pM\        V\        4      '       g   Q R4       hVP                  V P                  P                  V P                  P                  V P                  P                  V P                  P                  V P                  P                  .4       V P                  P                   e&   VP#                  V P                  P                   4       \%        \'        \)        V4      4      4      # u upi u upi ),zsuppress_tokens must be a listr2   )r4  re   rO  rj   splitrk   extendr&   non_speech_tokensr   rC   rZ   	translater>   rQ  sot_lm	no_speechr   rR  sortedset)r   re   r   s   &  r   rE  !DecodingTask._get_suppress_tokens  sL   ,,66os++/>/D/DS/IJ/I!s1v/IOJ *9D/Q!Vqq/OD""4>>#C#CD$O(<(A Oot44V6VV4))((""''%%	
 >>##/""4>>#;#;<VC0122/ K Es    F$!F).F)c                4   < V ^8  d   QhRS[ P                  /# )r   r%   r   )r   rm   s   "r   r   r6    s      rxx r   c                "   V P                   P                  '       d    VP                  \        P                  4      pVP
                  RR V P                  P                  P                  V P                  P                  P                  38X  d   TpMV P                  P                  V4      pVP                  V P                   P                  '       d   \        P                  M\        P                  8w  d   \        RVP                   24      hV# )r   Nz'audio_features has an incorrect dtype: r0   )r4  ri   r   r'   float16r9   r#   r:   r;   r<   r=   r,   rB   	TypeError)r   r%   r}   s   && r   _get_audio_features DecodingTask._get_audio_features  s    <<**RZZ(C99RS>JJOO''JJOO))
 

 !N!ZZ//4N$,,2C2C2CBJJT9.:N:N9OP  r   c                N   < V ^8  d   QhRS[ P                  RS[P                  /# r   r}   r~   )r'   r(   rG   )r   rm   s   "r   r   r6  -  s#     % %rxx % %r   c                   V P                   P                  .VP                  ^ ,          ,          pRpV P                   P                  e   V P                   P                  R8X  d   V P                  P                  WP                  4      w  rTV Uu. uF  p\        WfP                  R7      NK  	  ppV P                   P                  f-   \        P                  ! V4      VRV P                  ^,           3&   W43# u upi )r   Nlang_id)keyr1   )r4  r4   r9   r\   r#   rV   r&   r$  getrG   r(   rA  )r   r}   r~   	languages
lang_probslang_tokensprobss   &&&    r   _detect_languageDecodingTask._detect_language-  s    \\**+n.B.B1.EE	
<<  (DLL,=,=,J&*jj&@&@'#K AKK
uU		2
IK||$$,020Eq$..1,,-$$ Ls   C8c                N   < V ^8  d   QhRS[ P                  RS[ P                  /# rj  r   )r   rm   s   "r   r   r6  <  s#     -5 -5 -5288 -5r   c                  a  VP                   ^ ,          p\        P                  ! V4      pV 3R lpV! W!W$4      w  r&rGS P                  P                  eL   \        P
                  ! VRS P                  3,          RR7      pVRS P                  P                  3,          p	M%\        P                  ! V\        P                  4      p	\        P                  ! WbWI4       \        ^S P                  4       Fg  p
VRRR13,          pVP                   R,          S P                  8  d    M7V! WW$4      w  rr\        P                  ! WV4       V'       d    M
TpTpTpKi  	  W$V	3# )r   c                    < SP                   P                  W4      pVR,          pSP                   F  pVP                  WR4      pK  	  SP                  P                  W%V4      w  r'pW'W43# )r1   r   )rB  r?   rD  r   r   r   )	inputsr}   r~   r   
pre_logitsr?   logit_filterr   r   s	   &&&&    r   _step&DecodingTask._main_loop.<locals>._step@  sr    ..vFJ  &F !% 2 2%++F; !3 /3ll.A.A/+F| l>>r   Nr1   r.   r2   )r9   r'   r  r&   r_  rF   rA  r@   r   
async_evalrH   r^   r<  )r   r}   r~   n_batchr   r{  r   ry  probs_at_sotno_speech_probsrR   rx  r   next_completednext_sum_logprobsr   s   f&&             r   
_main_loopDecodingTask._main_loop<  s)   ,,q/xx(	?  7<F7
3< >>##/::jDNN1B&C"ML*1dnn.F.F+FGO gggrvv6O
iGq$//*AArsF^F||B$**,@EA=K): MM.7HI F&I,L + _44r   c                J   < V ^8  d   QhRS[ P                  RS[S[,          /# )r   r%   r   )r'   r(   r   ry   )r   rm   s   "r   r   r6  k  s(     W
 W
rxx W
D$8 W
r   c                ,	   V P                   P                  4        V P                  P                  4        V P                  pVP                  ^ ,          pV P                  V4      p\        P                  ! V P                  4      p\        P                  ! WS\        V P                  4      34      pV P                  WE4      w  rgV P                  P                  R8X  d/   \        WFV4       UU	U
u. uF  w  rp
\        WV
R7      NK  	  up
p	p# V P                   ^8  dv   VR,          p\        P                  ! WSV P                   \        V P                  4      .4      pVP#                  W0P                   ,          \        V P                  4      34      pV P%                  WE4      w  r[pVRRV P                   1,          pVRRV P                   1,          pVP                  ^ ,          \        V4      u;8X  d	   V8X  g   Q h Q hVP#                  W0P                   R4      pVP#                  W0P                   4      pV P                  P'                  W[4      w  r[VRV P(                  R13,          p\        P*                  ! W[V4       VP-                  4       pVP-                  4       pVP-                  4       pV UUu. uF/  q Uu. uF   qRVP/                  VP0                  4       NK"  	  upNK1  	  pppV P2                  P5                  W[4      p\        W4       UUu. uF  w  pqV,          NK  	  pppV Uu. uF!  qP7                  V4      P9                  4       NK#  	  pp\        W4       UUu. uF  w  ppVV,          NK  	  ppp\        W[4       UUu. uF  w  ppV\        V4      ^,           ,          NK!  	  pppVVVVVV3p\        \;        \=        \        V4      4      4      ^8w  d&   \?        R\A        \=        \        V4      4       24      h\        V!   UU	UUUUu. uF9  w  prppp\        VV	VVVVV P                  PB                  \E        V4      R7      NK;  	  uppppp	p# u up
p	pi u upi u uppi u uppi u upi u uppi u uppi u uppppp	pi )	r   rl  )r}   r4   rU   N.zinconsistent result lengths: )r}   r4   r~   r   r   r   r]   r!   )r1   Nr1   r2   )#rB  r   r   r&   r9   rg  r'   r(   r?  broadcast_tor   rs  r4  r\   rI   ry   r:  reshaper  r   r  evalr!  r@  r   rC  r   decoderP  ra  mapRuntimeErrorrC   r]   r!   )r   r%   r&   rM   r}   r~   ro  rU   featuresr4   rr  r   r  r   r   selectedrR   textslpavg_logprobsfieldsr   r   r   s   &&                      r   runDecodingTask.runk  s   #~~	yy|#'#;#;C#@88D$7$783t7J7J3K)LM %)$9$9.$Q!	<<	)
 25"~2	2-H #+u2	  <<!J'F__$,,D4G4G0HIF ^^W||%;SATAT=U$VWF 150W-o (4<<8)/T\\/:##A&#o*>I'IIIIIr:#++G\\B  $||44VJT..001 	o6#**,)002DJKFq:1-qwwy}}-.:FK '',,VB474I"J4IDAqQ444I"JAGHA,,Q/557H8;H8S$T8Suq"RUU8S$T+.v+D%
+D%!RB#a&1*+D 	 %

 
 s3sF#$%*!>tCVDT?U>VWXX RUR
 
RMh+~ '!'- LL44"3D"9	R
 	
yJ ;K #KH$T%

s<   .Q#7Q/?&Q*%Q/Q53'Q;*R %R?R*Q/c                Z   < V ^8  d   Qh/ S[ ;R&   S[;R&   S[;R&   S[S[,          ;R&   # )r   rB  rC  r   rD  )r   r   r   r   r   )r   rm   s   "r   r   r6    s:      ##    $$	 r   )r   rB  r?  rD  r#   r<  r:  r4  r  r^   rC  rA  r6   r&   N)rp   rq   rr   rs   r   r9  r>  rE  rg  rs  r  r  rt   ru   rv   rw   s   @r   r2  r2    sa     ; ;z  83 3: (% %-5 -5^W
 W
{  r   r2  c                    V ^8  d   QhRRR\         P                  R\        R\        \        \
        \        ,          3,          /# )r   r#   r$   r%   r4  r   )r'   r(   rY   r   ry   r   )r   s   "r   r   r     sC      +  + +	 +  +
 >4//0 +r   c                    VP                   ^8H  ;p'       d
   VR,          pV'       d   \        V3/ VB p\        W4      P                  V4      pV'       d
   V^ ,          # T# )a  
Performs decoding of 30-second audio segment(s), provided as Mel spectrogram(s).

Parameters
----------
model: Whisper
    the Whisper model instance

mel: mx.array, shape = (80, 3000) or (*, 80, 3000)
    An array containing the Mel spectrogram(s)

options: DecodingOptions
    A dataclass that contains all necessary options for decoding 30-second segments

Returns
-------
result: Union[DecodingResult, List[DecodingResult]]
    The result(s) of decoding contained in `DecodingResult` dataclass instance(s)
N)r8   r   r2  r  )r#   r%   r4  kwargsrL   r   s   &&&,  r   r  r    sX    2 Qv$i',V,%)--c2F6!9*F*r   r   )*r   dataclassesr   r   r   typingr   r   r   r	   r
   r   r   mlx.corecorer'   numpyrG   	mlx.utilsr   audior   r&   r   r   r!   rV   rY   ry   r   r   r   r   compiler   r   r   r   r  r  r2  r  ro   r   r   <module>r     s    1 1 I I I     /<
;+| $! ! !H $	& 	& 	& ," "Pn P48" 8"v 0 0$L $>" " 
K 
"[ "F+ FRt
 t
t	  /0 +  +r   