§
    9²¸jg—  ã                   óV  — d dl mZ d dlmZ d dlmZmZmZ d dlZddl	m
Z
mZmZmZ ddlmZmZmZmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZmZm Z  ddl!m"Z" erddl#m$Z$  e j%        e&¦  «        Z'e G d„ de¦  «        ¦   «         Z( G d„ de¦  «        Z) G d„ de¦  «        Z*dS )é    )ÚCallable)Ú	dataclass)ÚTYPE_CHECKINGÚAnyÚOptionalNé   )ÚDynamicCacheÚQuantizedCacheÚQuantizedLayerÚStaticCache)ÚGenerateDecoderOnlyOutputÚGenerationConfigÚGenerationMixinÚLogitsProcessorÚLogitsProcessorList)Ú ALL_STATIC_CACHE_IMPLEMENTATIONS)Ú!LOGITS_PROCESSOR_INPUTS_DOCSTRING)ÚStoppingCriteriaList)ÚALL_CACHE_NAMESÚGenerateNonBeamOutput)Úadd_start_docstringsÚis_diffusers_availableÚloggingé   )Ú,VibeVoiceAcousticTokenizerConv1dPaddingCache)ÚBaseStreamerc                   ó>   — e Zd ZU dZdZeej                 dz  ed<   dS )ÚVibeVoiceGenerateOutputz®
    Outputs of VibeVoiceForConditionalGeneration.generate.

    Args:
        audio (`list(torch.FloatTensor)` of length `batch_size`):
            The generated audio.
    NÚaudio)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚlistÚtorchÚFloatTensorÚ__annotations__© ó    ú‚/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/models/vibevoice/generation_vibevoice.pyr   r   .   s;   € € € € € € ðð ð -1€Eˆ4�Ô!Ô" TÑ)Ð0Ð0Ñ0Ð0Ð0r)   r   c                   ó’   — e Zd ZdZddee         dedej        fd„Z e	e
¦  «        dej        dej        d	ej        fd
„¦   «         ZdS )Ú!VibeVoiceTokenConstraintProcessora£  
    Constrains token generation to only diffusion-related tokens during audio generation, as the role of the
    language model is to emit:
    - another audio-diffusion placeholder (which triggers the diffusion head to synthesize the next acoustic latent)
    - or an EOS token (which signals the end of the audio generation).
    The actual audio comes from the diffusion head, not from sampling the vocabulary.
    NÚvalid_token_idsÚ
vocab_sizeÚdevicec                 óŠ   — t          j        |ft          d¦  «        ¦  «        }d||<   |                     |¦  «        | _        d S )Nz-infg        )r%   ÚfullÚfloatÚtoÚlogits_mask)Úselfr-   r.   r/   r4   s        r*   Ú__init__z*VibeVoiceTokenConstraintProcessor.__init__D   s?   € å”j * µ°f±´Ñ>Ô>ˆØ'*ˆ�OÑ$Ø&Ÿ>š>¨&Ñ1Ô1ˆÔÐÐr)   Ú	input_idsÚscoresÚreturnc                 ó   — || j         z   S ©N)r4   )r5   r7   r8   s      r*   Ú__call__z*VibeVoiceTokenConstraintProcessor.__call__J   s   € à˜Ô(Ñ(Ð(r)   r;   )r    r!   r"   r#   r$   Úintr%   r/   r6   r   r   Ú
LongTensorr&   r<   r(   r)   r*   r,   r,   ;   sž   € € € € € ðð ð2ð 2¨¨S¬	ð 2¸sð 2ÈEÌLð 2ð 2ð 2ð 2ð ÐÐ;Ñ<Ô<ð) %Ô"2ð )¸EÔ<Mð )ÐRWÔRcð )ð )ð )ñ =Ô<ð)ð )ð )r)   r,   c                   óP  ‡ — e Zd ZdZdefˆ fd„Zdedz  deeef         fˆ fd„Z	e
defd„¦   «         Zdeded	ej        deej        ef         fd
„Z	 d1dededededededdfd„Z	 d1dededededz  dedefd„Zdedz  fd„Zdej        dej        deddfd„Zdej        dej        dej        dz  dej        dededefd„Zd ej        d!ej        d"ed#ed$edej        fd%„Zd&ej        dej        ded'edz  def
d(„Z	 	 d2d*ej        d+ed,e ded-e!d.e"d/         de#ej        z  fd0„Z$ˆ xZ%S )3ÚVibeVoiceGenerationMixinap  
    Generation mixin for VibeVoice.

    The classifier-free guidance (CFG) logic (the negative/unconditional branch and the diffusion sampling)
    is adapted from the original VibeVoice implementation, where it all lives inline in a single `generate`
    method:
    https://github.com/vibevoice-community/VibeVoice/blob/07cb79feadd2d3fd7f47530d4c964a12857936a0/vibevoice/modular/modeling_vibevoice_inference.py#L327
    Here it is factored into helpers, and the negative-branch helpers mirror what the positive branch does in
    `_sample` (prepare inputs -> forward -> update model kwargs -> append the new token).
    r9   c                 óþ   •—  t          ¦   «         j        |i |¤Ž}| j        j        | j        j        | j        j        | j        j        g}|                     t          || j        j	        | j
        ¬¦  «        ¦  «         |S )N©r/   )ÚsuperÚ_get_logits_processorÚconfigÚaudio_bos_token_idÚaudio_eos_token_idÚaudio_token_idÚeos_token_idÚappendr,   r.   r/   )r5   ÚargsÚkwargsÚ
processorsÚvalid_tokensÚ	__class__s        €r*   rD   z.VibeVoiceGenerationMixin._get_logits_processor[   sz   ø€ Ø2•U‘W”WÔ2°DÐC¸FÐCÐCˆ
àŒKÔ*ØŒKÔ*ØŒKÔ&ØŒKÔ$ð	
ˆð 	×ÒÕ;¸LÈ$Ì+ÔJ`ÐimÔitÐuÑuÔuÑvÔvÐvØÐr)   Úgeneration_configNc                 óV  •— |                      dd¦  «        }|                      dd¦  «        }|                      dd¦  «        } t          ¦   «         j        |fi |¤Ž\  }}|pt          |dd¦  «        }|€|                      |¦  «        }|€t          d¦  «        ‚t          |d¦  «        r t          |d¦  «        rt          |d	¦  «        s%t          d
t          |¦  «        j        › d�¦  «        ‚||_	        ||_
        |pt          |dd¦  «        pd|_        |j        €d|_        ||fS )a¢  
        This method overrides [~generation.utils.GenerationMixin._prepare_generation_config].

        It extracts VibeVoice-specific parameters for the generation config.

        VibeVoice-specific parameters include:
        - `noise_scheduler`: A custom noise scheduler instance. Optional: if not provided, a default one is built
            from `noise_scheduler_class`/`noise_scheduler_config` on the generation config (requires `diffusers`).
        - `monitor_progress`: Whether to display a progress bar tracking audio generation. Defaults to `False`.
        - `num_diffusion_steps`: Number of diffusion steps for audio latent sampling. Defaults to `10`.
        Úmonitor_progressFÚnum_diffusion_stepsNÚnoise_schedulera2  VibeVoice generation requires a `noise_scheduler`. Either pass one to `generate(...)`, e.g. `diffusers.DPMSolverMultistepScheduler(beta_schedule='squaredcos_cap_v2', prediction_type='v_prediction')`, or set `noise_scheduler_class` (and optionally `noise_scheduler_config`) on the model's generation config.Úset_timestepsÚstepÚ	timestepszThe provided noise_scheduler (z†) is not compatible with VibeVoice generation. It must implement `set_timesteps` and `step` methods, and have a `timesteps` attribute.é
   g      ð?)ÚpoprC   Ú_prepare_generation_configÚgetattrÚ_build_default_noise_schedulerÚ
ValueErrorÚhasattrÚtyper    rT   rR   rS   Úguidance_scale)	r5   rP   rL   rR   rS   Únoise_scheduler_kwargÚmodel_kwargsrT   rO   s	           €r*   rZ   z3VibeVoiceGenerationMixin._prepare_generation_configf   sˆ  ø€ ð  "Ÿ:š:Ð&8¸%Ñ@Ô@ÐØ$ŸjšjÐ)>ÀÑEÔEÐØ &§
¢
Ð+<¸dÑ CÔ CÐà*L­%©'¬'Ô*LÐM^Ð*iÐ*iÐbhÐ*iÐ*iÑ'Ð˜<ð 0Ðfµ7Ð;LÐN_ÐaeÑ3fÔ3fˆØÐ"Ø"×AÒAÐBSÑTÔTˆOØÐ"Ýð}ñô ð õ �O _Ñ5Ô5ð	å˜¨Ñ0Ô0ð	õ ˜¨Ñ5Ô5ð	õ
 ðvµ°oÑ1FÔ1FÔ1Oð vð vð vñô ð ð -<ÐÔ)Ø-=ÐÔ*àÐ`¥7Ð+<Ð>SÐUYÑ#ZÔ#ZÐ`Ð^`ð 	Ô-ð Ô+Ð3Ø/2ÐÔ,Ø  ,Ð.Ð.r)   c                 ó  — t          | dd¦  «        }t          ¦   «         st          d|› d�¦  «        ‚dd l}	 t          ||¦  «        }n!# t          $ r t          d|› d�¦  «        ‚w xY wt          | dd	d
dœ¦  «        } |di |¤ŽS )NÚnoise_scheduler_classÚDPMSolverMultistepSchedulerz(The default VibeVoice noise scheduler (`z�`) requires `diffusers`. Install it with `pip install diffusers`, or pass a custom `noise_scheduler` instance to `generate(...)`.r   z Could not find noise scheduler `zŒ` in `diffusers`. Set `noise_scheduler_class` on the generation config to a valid `diffusers` scheduler, or pass a custom `noise_scheduler`.Únoise_scheduler_configÚsquaredcos_cap_v2Úv_prediction)Úbeta_scheduleÚprediction_typer(   )r[   r   ÚImportErrorÚ	diffusersÚAttributeErrorr]   )rP   Úscheduler_class_namerl   Úscheduler_classÚscheduler_configs        r*   r\   z7VibeVoiceGenerationMixin._build_default_noise_scheduler�   sû   € å&Ð'8Ð:QÐSpÑqÔqÐå%Ñ'Ô'ð 	ÝðpÐ;Oð pð pð pñô ð ð
 	ÐÐÐð	Ý% iÐ1EÑFÔFˆOˆOøÝð 	ð 	ð 	ÝðqÐ3Gð qð qð qñô ð ð	øøøõ
 #ØØ$Ø1ÀnÐUÐUñ
ô 
Ðð
 ˆÐ2Ð2Ð!1Ð2Ð2Ð2s   ¸A	 Á	A'Ú
batch_sizer/   c           	      óz  — t          j        |df| j        j        t           j        |¬¦  «        t          j        |dft           j        |¬¦  «        dœ} | j        |fi |¤Ž\  }}|                      d|| j        j        ¬¦  «        \  }}}|                      |d|¬¦  «         |d         }| 	                    d	¦  «        du o|j
        du}	| 	                    d
¦  «        du o|j        du}
|                      ||	|
d|d         |j        d         ¬¦  «        }|                      ||d||j
        dz
  d¬¦  «         ||fS )aa  
        Set up the unconditional branch used for classifier-free guidance (CFG).

        This mirrors the positive branch's own setup in `generate`/`_sample` (resolve the generation config,
        prepare the model inputs and KV cache), applied to the negative branch. See the class docstring for the
        original implementation.

        Returns the initial negative `input_ids` and prepared `model_kwargs` for the negative pass.
        The negative sequence starts with a single `audio_bos_token_id` token and its KV cache is
        sized to match the positive generation's maximum length.
        é   ©Údtyper/   )r7   Úattention_maskN)rb   Úbos_token_idTrB   r7   Ú
max_lengthÚ
min_length)rP   Úhas_default_max_lengthÚhas_default_min_lengthÚmodel_input_nameÚinputs_tensorÚinput_ids_lengthÚ#_previous_max_negative_cache_length)Úmax_length_attr_name)r%   r1   rE   rF   ÚlongÚonesrZ   Ú_prepare_model_inputsÚ_prepare_special_tokensÚgetrx   ry   Ú_prepare_generated_lengthÚshapeÚ_prepare_cache_for_generation)r5   rq   rP   r/   Únegative_kwargsÚnegative_generation_configÚnegative_model_kwargsÚ_Únegative_input_idsÚnegative_has_default_max_lengthÚnegative_has_default_min_lengths              r*   Ú_prepare_negative_generationz5VibeVoiceGenerationMixin._prepare_negative_generation·   s²  € õ& œ Z° O°T´[Ô5SÕ[`Ô[eÐntÐuÑuÔuÝ#œj¨*°a¨ÅÄ
ÐSYÐZÑZÔZð
ð 
ˆð
 =\¸DÔ<[Øð=
ð =
Ø!0ð=
ð =
Ñ9Ð"Ð$9ð '+×&@Ò&@ØÐ4À4Ä;ÔCað 'Añ '
ô '
Ñ#ˆˆ1Ð#ð 	×$Ò$Ð%?ÀÈfÐ$ÑUÔUÐUØ,¨[Ô9Ðà×Ò Ñ-Ô-°Ð5ÐkÐ:TÔ:_ÐgkÐ:kð 	(ð ×Ò Ñ-Ô-°Ð5ÐkÐ:TÔ:_ÐgkÐ:kð 	(ð &*×%CÒ%CØ8Ø#BØ#BØ(Ø)¨+Ô6Ø/Ô5°aÔ8ð &Dñ &
ô &
Ð"ð 	×*Ò*Ø&Ø!ØØØ&Ô1°AÑ5Ø!Fð 	+ñ 	
ô 	
ð 	
ð "Ð#8Ð8Ð8r)   Ú_previous_max_cache_lengthrb   Úgeneration_modeÚmax_cache_lengthr€   c                 ó  — d}|                      |¦  «        }|�@|j        �t          d|› d�¦  «        ‚t          |t          ¦  «        rt          d¦  «        ‚dS |j        du rdS |j        t          v rb|j        �t          ||j        ¦  «        }t          |j	        |j
        ¦  «        |z  }	|                      |j        |	||j        |¬¦  «        ||<   dS |j        dk    rh|j        �|j        ni }
|
                     d	| j                             d
¬¦  «        ¦  «         |
                     dd¦  «        }t%          dd|i|
¤Ž||<   dS d	| j                             d
¬¦  «        i}|j        dk    rd
|d<   t'          di |¤Ž||<   dS )a  
        This method overrides [~generation.utils.GenerationMixin._prepare_cache_for_generation].

        The base implementation memorizes the longest static cache length requested so far on the fixed
        `self._previous_max_cache_length` attribute, so that later calls are served by a cache of that same shape
        (avoiding a `torch.compile` recompilation). VibeVoice runs a second, independent generation pass for the
        classifier-free guidance negative branch, whose cache is much shorter than the positive branch's (the
        negative branch starts from a single token instead of the full prompt). The added `max_length_attr_name`
        argument lets the caller pick which attribute holds that memo (`"_previous_max_cache_length"` for the
        positive branch, `"_previous_max_negative_cache_length"` for the negative one), so the negative cache is
        not over-allocated to the positive branch's length. Only the code paths exercised by VibeVoice are kept:
        decoder-only dynamic and static caches.
        Úpast_key_valuesNzMPassing both `cache_implementation` (used to initialize certain caches) and `zB` (a Cache object) is unsupported. Please use only one of the two.z]Passing a tuple of `past_key_values` is not supported anymore. Please use a `Cache` instance.F)Úcache_implementationrq   Úmax_cache_lenÚprefill_chunk_sizer€   Ú	quantizedrE   T©ÚdecoderÚbackendÚquantoÚ	offloadedÚ
offloadingr(   )r…   r–   r]   Ú
isinstanceÚtupleÚ	use_cacher   r—   ÚmaxÚ	num_beamsÚnum_return_sequencesÚ_prepare_static_cacher˜   Úcache_configÚ
setdefaultrE   Úget_text_configrY   r
   r	   )r5   rP   rb   r’   rq   r“   r€   Ú
cache_nameÚuser_defined_cacheÚcache_batch_sizer§   rœ   Údynamic_cache_kwargss                r*   rˆ   z6VibeVoiceGenerationMixin._prepare_cache_for_generationô   s  € ð, 'ˆ
ð *×-Ò-¨jÑ9Ô9ÐØÐ)Ø Ô5ÐAÝ ðTÐdnð Tð Tð Tñô ð õ Ð,­eÑ4Ô4ð Ý Øsñô ð ð ˆFð Ô&¨%Ð/Ð/ØˆFàÔ1Õ5UÐUÐUð !Ô.Ð:Ý#&Ð'7Ð9JÔ9XÑ#YÔ#YÐ Ý"Ð#4Ô#>Ð@QÔ@fÑgÔgÐjtÑtÐØ'+×'AÒ'AØ%6Ô%KØ+Ø.Ø#4Ô#GØ%9ð (Bñ (ô (ˆL˜Ñ$Ð$Ð$ð Ô3°{ÒBÐBØ=NÔ=[Ð=gÐ,Ô9Ð9ÐmoˆLØ×#Ò# H¨d¬k×.IÒ.IÐRVÐ.IÑ.WÔ.WÑXÔXÐXØ"×&Ò& y°(Ñ;Ô;ˆGÝ'5Ð'VÐ'V¸gÐ'VÈÐ'VÐ'VˆL˜Ñ$Ð$Ð$ð %-¨d¬k×.IÒ.IÐRVÐ.IÑ.WÔ.WÐ#XÐ Ø Ô5¸ÒDÐDØ59Ð$ \Ñ2Ý'3Ð'KÐ'KÐ6JÐ'KÐ'KˆL˜Ñ$Ð$Ð$r)   r–   r—   r˜   c                 óJ  — d|v }t          | |d¦  «        }t          ||¦  «        }t          | j                             d¬¦  «        ||¬¦  «        }	|�?|                      ¦   «         }
|
�)|
\  }}|	                     |||| j        | j        ¬¦  «         t          | ||¦  «         |	S )ao  
        This method overrides [~generation.utils.GenerationMixin._prepare_static_cache].

        Same behavior as the base method (a fresh `StaticCache` per call, sized to the longest length requested so
        far so that `torch.compile` does not recompile on later calls), except the memorized length is stored under
        the attribute named by `max_length_attr_name` instead of always `self._previous_max_cache_length`. This lets
        the CFG negative branch track its own (much shorter) length. Only the decoder-only path used by VibeVoice
        is kept.

        Returns the resulting cache object.
        rž   éÿÿÿÿTrš   )rE   r—   rŸ   N)rq   Ú	num_headsÚhead_dimru   r/   )
r[   r£   r   rE   r©   Ú_get_static_cache_init_shapeÚearly_initializationru   r/   Úsetattr)r5   r–   rq   r—   r˜   r€   Úoffload_cacheÚprevious_max_lenÚeffective_lengthÚcacheÚ
init_shaper°   r±   s                r*   r¦   z.VibeVoiceGenerationMixin._prepare_static_cache8  sÙ   € ð& $Ð';Ð;ˆÝ" 4Ð)=¸rÑBÔBÐÝ˜}Ð.>Ñ?Ô?ÐåØ”;×.Ò.°tÐ.Ñ<Ô<Ø*Ø$ð
ñ 
ô 
ˆð
 Ð)ð ×:Ò:Ñ<Ô<ˆJØÐ%Ø&0Ñ#�	˜8Ø×*Ò*Ø)Ø'Ø%Øœ*Øœ;ð +ñ ô ð õ 	�Ð*Ð,<Ñ=Ô=Ð=Øˆr)   Úcompile_configc                 óð   — |p|                       ¦   «         }t          | d¦  «        rt          | dd¦  «        |k    r5|| _        t	          j        | j        fi |                     ¦   «         ¤Ž| _        | j        S )a}  
        Return a `torch.compile`'d version of `self.__call__` dedicated to the CFG negative branch.

        This mirrors [`~PreTrainedModel.get_compiled_call`] but stores the compiled callable under a
        separate attribute (`self._negative_compiled_call`). The negative branch keeps its own
        `StaticCache`, with a different length than the positive branch's.
        Ú_negative_compiled_callÚ_last_negative_compile_configN)	Ú_default_compile_configr^   r[   r½   r%   Úcompiler<   Úto_dictr¼   )r5   rº   s     r*   Ú_get_negative_compiled_callz4VibeVoiceGenerationMixin._get_negative_compiled_callf  s   € ð (ÐI¨4×+GÒ+GÑ+IÔ+Iˆå˜Ð7Ñ8Ô8ð	då�tÐ<¸dÑCÔCÀ~ÒUÐUà1?ˆDÔ.Ý+0¬=¸¼Ð+cÐ+cÈ.×J`ÒJ`ÑJbÔJbÐ+cÐ+cˆDÔ(ØÔ+Ð+r)   Údiffusion_start_maskr�   r‹   c                 óš  — |d         }t          j        |¦  «        }d|dd…df<   t          j        |                     d¦  «        ||¦  «        |d<   |dd…df         }t          j        |t          j        || j        j        ¦  «        |¦  «        |dd…df<   |                     d¦  «        ��|                     dddd¦  «        }|d         j	        D ]÷}t          |t          ¦  «        rŒ|j        �Ö|j        �Ï|                     |j        j        ¦  «        }	t          j        |	|j        dd…dd…dd…dd…f         |j        dd…dd…dd…dd…f         ¦  «        |j        dd…dd…dd…dd…f<   t          j        |	|j        dd…dd…dd…dd…f         |j        dd…dd…dd…dd…f         ¦  «        |j        dd…dd…dd…dd…f<   ŒödS dS )zÊ
        When `audio_bos_token_id` is generated for a subset of sequences, reset those sequences'
        negative KV cache to a single-token context so the unconditional CFG pass starts fresh.
        rv   rs   Nr¯   r•   r   )r%   Ú
zeros_likeÚwhereÚ	unsqueezeÚ	full_likerE   rF   r…   ÚviewÚlayersr    r   ÚkeysÚvaluesr3   r/   )
r5   rÂ   r�   r‹   rv   Úreset_attention_maskÚlast_input_idÚmask_4dÚlayerÚlayer_mask_4ds
             r*   Ú%_reset_negative_cache_for_audio_startz>VibeVoiceGenerationMixin._reset_negative_cache_for_audio_startw  sB  € ð /Ð/?Ô@ˆå$Ô/°Ñ?Ô?ÐØ&'Ð˜Q˜Q˜Q ˜UÑ#å27´+Ø ×*Ò*¨2Ñ.Ô.Ð0DÀnñ3
ô 3
ÐÐ.Ñ/ð +¨1¨1¨1¨b¨5Ô1ˆå$)¤KØ ¥%¤/°-ÀÄÔA_Ñ"`Ô"`Ðboñ%
ô %
Ð˜1˜1˜1˜b˜5Ñ!ð !×$Ò$Ð%6Ñ7Ô7ÑCØ*×/Ò/°°A°q¸!Ñ<Ô<ˆGà.Ð/@ÔAÔHð 
ð 
�Ý˜e¥^Ñ4Ô4ð ØØ”:Ð)¨e¬lÐ.FØ$+§J¢J¨u¬zÔ/@Ñ$AÔ$A�MÝ/4¬{Ø% u¤z°!°!°!°Q°Q°Q¸¸!¸¸Q¸Q¸Q°,Ô'?ÀÄÈAÈAÈAÈqÈqÈqÐRTÐRUÐRUÐWXÐWXÐWXÈLÔAYñ0ô 0�E”J˜q˜q˜q ! ! ! R S S¨!¨!¨!˜|Ñ,õ 27´Ø% u¤|°A°A°A°q°q°q¸!¸A¸#¸q¸q¸q°LÔ'AÀ5Ä<ÐPQÐPQÐPQÐSTÐSTÐSTÐVXÐVYÐVYÐ[\Ð[\Ð[\ÐP\ÔC]ñ2ô 2�E”L    A A A r s s¨A¨A¨A Ñ.øð DÐCð
ð 
r)   Údiffusion_maskÚnext_tokensÚinputs_embedsÚnegative_forwardc                 óV  — |                      dd¦  «        }|rdnd} | j        |fd|i|¤Ž}	|	                      d¦  «        €|�
||	d<   d|	d<    |di |	¤ddi¤Ž}
|
j        |d	dd…f         }|                      |
|d
¬¦  «        }t	          j        ||dd…df         gd	¬¦  «        }|||fS )aî  
        Advance the negative (unconditional) CFG branch by one step.

        This mirrors the positive branch's per-step logic in `_sample`: build the model inputs with
        `prepare_inputs_for_generation`, run the forward pass, advance the cache/kwargs with
        `_update_model_kwargs_for_generation`, and append the new token. It is run here as a separate branch with
        its own `negative_input_ids` and KV cache. See the class docstring for the original implementation.
        r¢   Trs   NÚnext_sequence_lengthrÔ   r7   Úreturn_dictr¯   F©Úis_encoder_decoder©Údimr(   )r…   Úprepare_inputs_for_generationÚlast_hidden_stateÚ#_update_model_kwargs_for_generationr%   Úcat)r5   rÒ   rÓ   rÔ   r�   r‹   rÕ   r¢   r×   Únegative_model_inputsÚnegative_outputsÚnegative_conditions               r*   Ú_step_negative_branchz.VibeVoiceGenerationMixin._step_negative_branch�  s%  € ð" *×-Ò-¨k¸4Ñ@Ô@ˆ	Ø$-Ð7˜q˜q°4Ðà B Ô BØð!
ð !
Ø5Ið!
ØMbð!
ð !
Ðð !×$Ò$ _Ñ5Ô5Ð=À-ÐB[Ø5BÐ! /Ñ2Ø15Ð! +Ñ.à+Ð+ÐVÐVÐ.CÐVÐVÐQUÐVÐVÐVÐØ-Ô?ÀÐPRÐTUÐTUÐTUÐ@UÔVÐà $× HÒ HØÐ3Èð !Iñ !
ô !
Ðõ #œYÐ(:¸KÈÈÈÈ4ÈÔ<PÐ'QÐWYÐZÑZÔZÐØ!Ð#5Ð7LÐLÐLr)   Úpositive_conditionrã   rT   rS   r`   c                 óx  — t          | j        j                             ¦   «         ¦  «        j        }t          j        ||gd¬¦  «                             |¦  «        }t          j        |j	        d         | j
        j        j        ¦  «                             |¦  «        }|                     |¬¦  «         t          |¦  «        dz  }	|j        D ]Ñ}
t          j        |d|	…         |d|	…         gd¬¦  «        }| j                             ||
                     |j	        d         ¦  «                             |¦  «        |¬¦  «        }t          j        ||	d¬¦  «        \  }}||||z
  z  z   }t          j        ||gd¬¦  «        }|                     ||
|¦  «        j        }ŒÒ|d|	…                              d¦  «        S )a3  
        Run the diffusion denoising loop with classifier-free guidance. Adapted from `sample_speech_tokens` in the
        original implementation: https://github.com/vibevoice-community/VibeVoice/blob/07cb79feadd2d3fd7f47530d4c964a12857936a0/vibevoice/modular/modeling_vibevoice_inference.py#L700
        r   rÛ   )Únum_inference_stepsr   N)Ú	conditionrs   )ÚnextÚmodelÚdiffusion_headÚ
parametersr/   r%   rà   r3   Úrandnr‡   rE   Úaudio_configÚhidden_sizerU   ÚlenrW   ÚrepeatÚsplitrV   Úprev_samplerÆ   )r5   rå   rã   rT   rS   r`   Údiffusion_head_devicerè   Únoisy_audio_latentÚhalfÚtimestepÚcombinedÚepsÚcond_epsÚ
uncond_epsÚ
guided_epss                   r*   Ú_sample_audio_latentz-VibeVoiceGenerationMixin._sample_audio_latentÃ  s»  € õ !% T¤ZÔ%>×%IÒ%IÑ%KÔ%KÑ LÔ LÔ SÐÝ”IÐ1Ð3EÐFÈAÐNÑNÔN×QÒQÐRgÑhÔhˆ	Ý"œ[¨¬¸Ô);¸T¼[Ô=UÔ=aÑbÔb×eÒeÐfoÑpÔpÐØ×%Ò%Ð:MÐ%ÑNÔNÐNÝÐ%Ñ&Ô&¨!Ñ+ˆà'Ô1ð 		eð 		eˆHÝ”yÐ"4°U°d°UÔ";Ð=OÐPUÐQUÐPUÔ=VÐ!WÐ]^Ð_Ñ_Ô_ˆHØ”*×+Ò+Ø˜(Ÿ/š/¨(¬.¸Ô*;Ñ<Ô<×?Ò?ÀÑIÔIÐU^ð ,ñ ô ˆCõ $)¤;¨s°D¸aÐ#@Ñ#@Ô#@Ñ ˆH�jà# n¸À:Ñ8MÑ&NÑNˆJÝ”)˜Z¨Ð4¸!Ð<Ñ<Ô<ˆCØ!0×!5Ò!5°c¸8ÐEWÑ!XÔ!XÔ!dÐÐØ! % 4 %Ô(×2Ò2°1Ñ5Ô5Ð5r)   Úaudio_latentÚacoustic_cachec                 óÔ  — || j         j                             |j        ¦  «        z  | j         j                             |j        ¦  «        z
  }|                     ¦   «         sCt          j        |g|j        dd…         ¢R Ž                      |j        |j	        ¦  «        }|||<   n|}| j         j
                             |                     | j         j
        j        ¦  «        |d¬¦  «        S )z+Decode `audio_latent` to an audio waveform.rs   NT©Úpadding_cacher¢   )rê   Úlatent_scaling_factorr3   r/   Úlatent_bias_factorÚallr%   Úzerosr‡   ru   Úaudio_towerÚdecode)r5   rþ   rÒ   rq   rÿ   Úscaled_latentÚpadded_latents          r*   Ú_decode_audio_latentz-VibeVoiceGenerationMixin._decode_audio_latentâ  só   € ð % t¤zÔ'G×'JÒ'JØÔñ(
ô (
ñ 
àŒJÔ)×,Ò,¨\Ô-@ÑAÔAñBˆð ×!Ò!Ñ#Ô#ð 	*Ý!œK¨
ÐM°]Ô5HÈÈÈÔ5LÐMÐMÐM×PÒPØÔ$ mÔ&9ñô ˆMð -:ˆM˜.Ñ)Ð)à)ˆMØŒzÔ%×,Ò,Ø×Ò˜TœZÔ3Ô:Ñ;Ô;Ø(Øð -ñ 
ô 
ð 	
r)   Fr7   Úlogits_processorÚstopping_criteriaÚsynced_gpusÚstreamerr   c           	      ó¼  ‡— |j         }|j        }	|j        }
|j        }|j        }|j        }t          d„ |D ¦   «         ¦  «        }|j        }|r|rdnd}|r|rdnd}|r|	rdnd}|r|
rdnd}|j        d         }d}t          j
        |t          j        |j        ¬¦  «        }|�|                     |j        ¦  «        }|                      ‰|¦  «        r|                      |j        ¦  «        n| j        }d}|                      ||‰|j         ¬¦  «        }|j        }|j        }|j        }|rt.                               d¦  «         d	\  }}}d
„ t3          |¦  «        D ¦   «         } |j        d         }!|                      |||j        ¬¦  «        \  }"}#|                      ‰|¦  «        r|                      |j        ¦  «        n| j        }$|j        d         }%‰d                              d¬¦  «        }&t          j        |j        |&z
  t          j        |&|j        |%z
  ¦  «        ¦  «        }'|r7tA          j!        tE          |' #                    ¦   «         ¦  «        dd¬¦  «        }(nd}(|  $                    |||j        ¬¦  «        �r?|(�|( %                    d¦  «         |r…‰d         rdnd}) | j&        |fd|)i‰¤Ž}*|* '                    dd¦  «         |* '                    dd¦  «         ||*d<   |  (                    ¦   «         5   |di |*¤ddi¤Ž}ddd¦  «         n# 1 swxY w Y   d}|  )                    |‰| j*        j+        ¬¦  «        Š|r|rŒå|j,        dd…ddd…f                              dt          j-        |j        ¬¦  «        }+ |||+¦  «        },|r*|r||,fz  }|r||+fz  }|	r||j.        fz  }|
r||j/        fz  }t          j0        |,d¬¦  «        }-|r|-|z  |d|z
  z  z   }-t          j1        ||-dd…df         gd¬¦  «        }|�'| 2                    |- 3                    ¦   «         ¦  «          |  4                    ¦   «         |-¦  «         5                    d¦  «        }.| 6                    ¦   «         |-| j*        j7        k    z  }/|  8                    |/|"|#¦  «         | 6                    ¦   «         |-| j*        j9        k    z  }0|0                     ¦   «         �r¢|  :                    |0|-||"|#|$¦  «        \  }1}"}#|j;        |0ddd…f         }2|  <                    |2|1|||j=        ¦  «        }3|  >                    |3|0||¦  «        }4|4j?        }t�          |0 A                    d¬¦  «         B                    d¦  «        ¦  «        D ]=\  }5}6| |6 C                    ¦   «                   D                    |4jE        |5         ¦  «         Œ>| jF         G                    |4jE        |d¬¦  «        }7|7jH        |0                     |7jH        j        ¦  «                 }8| jF         I                    |3¦  «        }9| jF         J                    |8¦  «        }:|9|:                     |9j        ¦  «        z   };|;                     |.j        ¦  «        |.|0<   |7j?        }|.}|!dz  }!| |||¦  «         z  }| #                    ¦   «         dk    }~|  $                    |||j        ¬¦  «        �°?|(�|( K                    ¦   «          |�| L                    ¦   «          d„ | D ¦   «         }<|r`d}=t          ˆfd„tš          D ¦   «         ¦  «        r(t�          ˆfd „tš          D ¦   «         ¦  «        }>‰|>         }=tŸ          ||||||=|<¬!¦  «        S |<S )"z±
        This method overrides [~generation.utils.GenerationMixin._sample].
        To ease maintenance, modifications are marked with the comment "VibeVoice specific".
        c              3   ó6   K  — | ]}t          |d ¦  «        V — ŒdS )rI   N)r^   )Ú.0Úcriterias     r*   ú	<genexpr>z3VibeVoiceGenerationMixin._sample.<locals>.<genexpr>  s,   è è € Ð'lÐ'lÈh­°¸.Ñ(IÔ(IÐ'lÐ'lÐ'lÐ'lÐ'lÐ'lr)   r(   Nr   Frt   )Úis_first_iterationzˆVibeVoice generation does not support sampling-based token selection. Tokens will be selected using argmax regardless of do_sample=True.)NNNc                 ó   — g | ]}g ‘ŒS r(   r(   )r  rŒ   s     r*   ú
<listcomp>z4VibeVoiceGenerationMixin._sample.<locals>.<listcomp>>  s   € Ð6Ð6Ð6˜q˜Ð6Ð6Ð6r)   rs   rB   r¯   rv   rÛ   zGenerating audioz tokens)ÚtotalÚdescÚunitr¢   r×   Úinput_valuesÚpadding_maskrÔ   rØ   TrÙ   )Úcopyru   r/   )Úas_tupler  c                 óB   — g | ]}|rt          j        |d ¬¦  «        nd‘ŒS )r¯   rÛ   N)r%   rà   )r  Úchunkss     r*   r  z4VibeVoiceGenerationMixin._sample.<locals>.<listcomp>×  s1   € ÐdÐdÐdÈV¸ÐH�5œ9 V°Ð4Ñ4Ô4Ð4ÀDÐdÐdÐdr)   c              3   ó    •K  — | ]}|‰v V — Œ	d S r;   r(   ©r  Ú	cache_keyrb   s     €r*   r  z3VibeVoiceGenerationMixin._sample.<locals>.<genexpr>Ü  s(   øè è € ÐNÐN°�9 Ð,ÐNÐNÐNÐNÐNÐNr)   c              3   ó$   •K  — | ]
}|‰v ¯|V — Œd S r;   r(   r"  s     €r*   r  z3VibeVoiceGenerationMixin._sample.<locals>.<genexpr>Ý  s.   øè è € Ð iÐ i¨yÈyÐ\hÐOhÐOh ÐOhÐOhÐOhÐOhÐ iÐ ir)   )Ú	sequencesr8   ÚlogitsÚ
attentionsÚhidden_statesr•   r   )PÚ_pad_token_tensorÚoutput_attentionsÚoutput_hidden_statesÚoutput_scoresÚoutput_logitsÚreturn_dict_in_generateÚanyÚ	do_sampler‡   r%   r‚   r�   r/   r3   Ú_valid_auto_compile_criteriaÚget_compiled_callrº   r<   Ú_prefillÚis_assistantrT   rR   rS   ÚloggerÚwarning_onceÚranger�   rÁ   ÚsumÚminrx   rÇ   r   Útqdmr=   r£   Ú_has_unfinished_sequencesÚupdaterÝ   rY   Ú_optimize_model_for_decoderß   rE   rÚ   r&  Úfloat32r'  r(  Úargmaxrà   ÚputÚcpuÚget_input_embeddingsrÆ   ÚboolrF   rÑ   rH   rä   rÞ   rý   r`   r  r  Ú	enumerateÚnonzerorÈ   ÚitemrJ   r   rê   Úsemantic_tokenizer_encoderÚlatentsÚmulti_modal_projectorÚsemantic_connectorÚcloseÚendr   ré   r   )?r5   r7   r  r  rP   r  r  rb   Úpad_token_idr*  r+  r,  r-  r.  Úhas_eos_stopping_criteriar0  r8   Ú
raw_logitsÚdecoder_attentionsÚdecoder_hidden_statesrq   Úthis_peer_finishedÚunfinished_sequencesÚmodel_forwardÚprefill_consumedÚoutputsrT   rR   rS   rÿ   Úsemantic_cacherÔ   Úaudio_chunksÚcur_lenr�   r‹   rÕ   Úinitial_lengthÚinitial_length_per_sampleÚmax_step_per_sampleÚprogress_barr×   Úmodel_inputsÚnext_token_logitsÚnext_token_scoresrÓ   Únext_inputs_embedsrÂ   rÒ   rã   rå   rþ   Úaudio_outputÚiÚ
sample_idxÚsemantic_outputsÚsemantic_featuresÚacoustic_embedÚsemantic_embedÚdiffusion_embedsÚgenerated_audior¸   r#  s?          `                                                       r*   Ú_samplez VibeVoiceGenerationMixin._sampleú  s‚	  ø€ ð )Ô:ˆØ-Ô?ÐØ0ÔEÐØ)Ô7ˆØ)Ô7ˆØ"3Ô"KÐÝ$'Ð'lÐ'lÐZkÐ'lÑ'lÔ'lÑ$lÔ$lÐ!Ø%Ô/ˆ	ð 0ÐL°MÐL��ÈˆØ3ÐP¸ÐP�R�RÈDˆ
Ø$;Ð\Ð@QÐ\˜R˜RÐX\ÐØ'>Ð bÐCWÐ b  Ð^bÐð ”_ QÔ'ˆ
Ø"ÐÝ$œz¨*½E¼JÈyÔO_Ð`Ñ`Ô`Ðð Ð#Ø'Ÿ?š?¨9Ô+;Ñ<Ô<ˆLð ×0Ò0°Ð?PÑQÔQðˆD×"Ò"Ð#4Ô#CÑDÔDÐDà”ð 	ð !ÐØ—-’-ØØØØ#4Ô#AÐAð	  ñ 
ô 
ˆð ,Ô;ˆØ,Ô=ÐØ/ÔCÐØð 	Ý×ÒðUñô ð ð 9IÑ5ˆ˜¨Ø6Ð6¥E¨*Ñ$5Ô$5Ð6Ñ6Ô6ˆØ”/ !Ô$ˆð 59×4UÒ4UØÐ)°)Ô2Bð 5Vñ 5
ô 5
Ñ1ÐÐ1ð
 ×0Ò0°Ð?PÑQÔQðˆD×,Ò,Ð->Ô-MÑNÔNÐNà”ð 	ð #œ¨Ô,ˆØ$0Ð1AÔ$B×$FÒ$FÈ2Ð$FÑ$NÔ$NÐ!Ý#œiØÔ(Ð+DÑDÝŒOÐ5Ð7HÔ7SÐVdÑ7dÑeÔeñ
ô 
Ðð ð 	 Ý"œ<­cÐ2E×2IÒ2IÑ2KÔ2KÑ.LÔ.LÐSeÐluÐvÑvÔvˆLˆLàˆLð ×,Ò,Ð-?ÀÐU^ÔUeÐ,ÑfÔfñ t	àÐ'Ø×#Ò# AÑ&Ô&Ð&ð  ð NØ,8¸Ô,EÐ'O q qÈ4Ð$ØA˜tÔAØð ð  Ø4Hð ØLXð ð  �ð
 × Ò  °Ñ6Ô6Ð6à× Ò  °Ñ6Ô6Ð6Ø0=�˜_Ñ-à×4Ò4Ñ6Ô6ð Nð NØ+˜mÐMÐM¨lÐMÐMÈÐMÐMÐM�GðNð Nð Nñ Nô Nð Nð Nð Nð Nð Nð Nøøøð Nð Nð Nð Nà#ÐØ×CÒCØØØ#'¤;Ô#Að Dñ ô ˆLð
 ð Ð1ð Øð !(¤¨q¨q¨q°"°a°a°a¨xÔ 8× ;Ò ;ÀÍUÌ]ÐclÔcsÐ ;Ñ tÔ tÐð !1Ð 0°Ð<MÑ NÔ NÐð 'ð FØ ð 3ØÐ0Ð2Ñ2�FØ ð 7ØÐ#4Ð"6Ñ6�JØ$ð @Ø&¨7Ô+=Ð*?Ñ?Ð&Ø'ð FØ)¨gÔ.CÐ-EÑEÐ)õ  œ,Ð'8¸bÐAÑAÔAˆKð )ð mØ)Ð,@Ñ@À<ÐSTÐWkÑSkÑClÑl�õ œ	 9¨k¸!¸!¸!¸T¸'Ô.BÐ"CÈÐLÑLÔLˆIØÐ#Ø—’˜[Ÿ_š_Ñ.Ô.Ñ/Ô/Ð/ð "= ×!:Ò!:Ñ!<Ô!<¸[Ñ!IÔ!I×!SÒ!SÐTUÑ!VÔ!VÐð $8×#<Ò#<Ñ#>Ô#>À+ÐQUÔQ\ÔQoÒBoÑ#pÐ Ø×6Ò6Ð7KÐM_ÐavÑwÔwÐwð 2×6Ò6Ñ8Ô8¸KÈ4Ì;ÔKeÒ<eÑfˆNØ×!Ò!Ñ#Ô#ñ !@ØPT×PjÒPjØ"ØØ!Ø&Ø)Ø$ñQô QÑMÐ"Ð$6Ð8Mð &-Ô%>¸~ÈrÐSTÐSTÐSTÐ?TÔ%UÐ"Ø#×8Ò8Ø&Ø&Ø#Ø'Ø%Ô4ñ ô  �ð  $×8Ò8¸À~ÐWaÐcqÑrÔr�Ø!-Ô!;�Ý%.¨~×/EÒ/EÈuÐ/EÑ/UÔ/U×/ZÒ/ZÐ[]Ñ/^Ô/^Ñ%_Ô%_ð Rð R‘M�A�zØ  §¢Ñ!2Ô!2Ô3×:Ò:¸<Ô;MÈaÔ;PÑQÔQÐQÐQð $(¤:×#HÒ#HØ Ô&Ø"0Ø"ð $Iñ $ô $Ð ð
 %5Ô$<¸^×=NÒ=NÐO_ÔOgÔOnÑ=oÔ=oÔ$pÐ!Ø!%¤×!AÒ!AÀ,Ñ!OÔ!O�Ø!%¤×!>Ò!>Ð?PÑ!QÔ!Q�Ø#1°N×4EÒ4EÀnÔF[Ñ4\Ô4\Ñ#\Ð Ø5E×5HÒ5HÐI[ÔIbÑ5cÔ5cÐ" >Ñ2Ø!1Ô!?�à.ˆMØ�q‰LˆGð $8Ð;LÐ;LÈYÐX^Ñ;_Ô;_Ð:_Ñ#_Ð Ø!5×!9Ò!9Ñ!;Ô!;¸qÒ!@Ðð ði ×,Ò,Ð-?ÀÐU^ÔUeÐ,ÑfÔfñ t	ðn Ð#Ø×ÒÑ Ô Ð ð ÐØ�LŠL‰NŒNˆNð eÐdÐWcÐdÑdÔdˆð #ð 	#ØˆEÝÐNÐNÐNÐN½oÐNÑNÔNÑNÔNð 0Ý Ð iÐ iÐ iÐ i½OÐ iÑ iÔ iÑiÔi�	Ø$ YÔ/�å*Ø#ØØ!Ø-Ø3Ø %Ø%ðñ ô ð ð #Ð"s   Ë=LÌLÌL)r‘   )FN)&r    r!   r"   r#   r   rD   r   r¡   ÚdictrZ   Ústaticmethodr\   r=   r%   r/   r>   r�   r   Ústrrˆ   r   r¦   rÁ   ÚTensorrÑ   r&   r   rä   r2   rý   r   r  r   rC  r   r   rk  Ú__classcell__)rO   s   @r*   r@   r@   O   sÚ  ø€ € € € € ð	ð 	ð	Ð8Kð 	ð 	ð 	ð 	ð 	ð 	ð5/Ø!1°DÑ!8ð5/à	Ð Ð%Ô	&ð5/ð 5/ð 5/ð 5/ð 5/ð 5/ðn ð3Ð:Jð 3ð 3ð 3ñ „\ð3ð2;9àð;9ð ,ð;9ð ”ð	;9ð
 
ˆuÔ Ð%Ô	&ð;9ð ;9ð ;9ð ;9ðH %AðBLð BLà+ðBLð ðBLð ð	BLð
 ðBLð ðBLð "ðBLð 
ðBLð BLð BLð BLðT %Að,ð ,à!ð,ð ð,ð ð	,ð
   $™Jð,ð "ð,ð 
ð,ð ,ð ,ð ,ð\,Ð:JÈTÑ:Qð ,ð ,ð ,ð ,ð"$à#œlð$ð "Ô,ð$ð  $ð	$ð
 
ð$ð $ð $ð $ðL$Màœð$Mð Ô%ð$Mð Ô(¨4Ñ/ð	$Mð
 "Ô,ð$Mð  $ð$Mð #ð$Mð 
ð$Mð $Mð $Mð $MðL6à!Ô-ð6ð "Ô-ð6ð ð	6ð
 !ð6ð ð6ð 
Ô	ð6ð 6ð 6ð 6ð>
àÔ'ð
ð œð
ð ð	
ð
 EÀtÑKð
ð 
ð
ð 
ð 
ð 
ð< "Ø-1ðr#ð r#àÔ#ðr#ð .ðr#ð 0ð	r#ð
 ,ðr#ð ðr#ð ˜>Ô*ðr#ð 
 Ô!1Ñ	1ðr#ð r#ð r#ð r#ð r#ð r#ð r#ð r#r)   r@   )+Úcollections.abcr   Údataclassesr   Útypingr   r   r   r%   Úcache_utilsr	   r
   r   r   Ú
generationr   r   r   r   r   Úgeneration.configuration_utilsr   Úgeneration.logits_processr   Úgeneration.stopping_criteriar   Úgeneration.utilsr   r   Úutilsr   r   r   ÚBvibevoice_acoustic_tokenizer.modeling_vibevoice_acoustic_tokenizerr   Úgeneration.streamersr   Ú
get_loggerr    r5  r   r,   r@   r(   r)   r*   ú<module>r~     s  ðð %Ð $Ð $Ð $Ð $Ð $Ø !Ð !Ð !Ð !Ð !Ð !Ø /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ð /à €€€à TÐ TÐ TÐ TÐ TÐ TÐ TÐ TÐ TÐ TÐ TÐ Tðð ð ð ð ð ð ð ð ð ð ð ð ð ð OÐ NÐ NÐ NÐ NÐ NØ JÐ JÐ JÐ JÐ JÐ JØ @Ð @Ð @Ð @Ð @Ð @Ø FÐ FÐ FÐ FÐ FÐ FÐ FÐ FØ JÐ JÐ JÐ JÐ JÐ JÐ JÐ JÐ JÐ Jðð ð ð ð ð ð
 ð 5Ø4Ð4Ð4Ð4Ð4Ð4ð 
ˆÔ	˜HÑ	%Ô	%€ð ð	1ð 	1ð 	1ð 	1ð 	1Ð7ñ 	1ô 	1ñ „ð	1ð)ð )ð )ð )ð )¨ñ )ô )ð )ð(]
#ð ]
#ð ]
#ð ]
#ð ]
#˜ñ ]
#ô ]
#ð ]
#ð ]
#ð ]
#r)   