
    aqj                     ~    S r SSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
rSrSrSrSrSrS r\S	:X  a  \" 5         gg)
aj  Cossim debug : C++ qwen-tts vs Python Qwen3-TTS on the CustomVoice 1.7B path.

Inputs (relative to CWD = tests/) :
    ../examples/prompt.txt       target text fed to both pipelines
    --speaker flag               speaker preset key, default mirrors customvoice.sh

Default mode is greedy (do_sample=False on both sides). The speaker
preset is passed straight through model.generate as `speakers=[name]`
on the Python side, mirroring qwen_tts.inference.qwen3_tts_model.
generate_custom_voice. The speaker codec embedding row slips between
think_eos and codec_pad in the prefill, growing the prefill by one
codec vector. Cote C++ the same insertion happens inside prompt_builder.

Optional --instruct adds a style instruction in front of the prompt.
The 1.7B CustomVoice accepts it, the 0.6B does not.

Dumps land in cpp/customvoice/ (C++) and python/customvoice/ (Python).
    Nz/../models/qwen-talker-1.7b-customvoice-{q}.ggufz&../models/qwen-tokenizer-12hz-{q}.ggufz.../checkpoints/Qwen3-TTS-12Hz-1.7B-CustomVoicezcpp/customvoicezpython/customvoicec                     [         R                  " 5       n U R                  SSS9  U R                  SSSS9  U R                  SS	S
S9  U R                  S[        SS9  U R                  SSS9  U R                  SSSS9  U R                  S[        R
                  R                  [        S5      S9  U R                  S[        R
                  R                  [        S5      S9  U R                  S[        SS9  U R                  SSSS9  U R                  5       n[        R                  " [        5        [        R                  " [        5        [        R                  " [        R
                  R                  UR                  5      =(       d    SSS9  [        UR                   S S!S"9 nUR#                  5       R%                  5       nS S S 5        ['        S#[)        W5       S$US S%  [)        U5      S%:  a  S&OS	 35        ['        S'UR*                   35        UR,                  (       a  ['        S(UR,                   35        ['        S)UR.                   S*UR0                   S+UR2                   35        ['        S,5        [4        R6                  " UR0                  5        [8        R:                  R1                  UR0                  5        [        R<                  " UR>                  5        [        R@                  " 5         [4        RB                  RE                  5       (       a  S-OS.n['        S/U 35        [        RF                  RI                  [J        U[4        RL                  S0S19RO                  5       n[        RP                  RI                  [J        SS29nS3U S43nU" US5SS69nUS7   RS                  U5      n	U	RU                  5       S8:X  a  U	RW                  S95      n	['        S:[Y        U	RZ                  5       35        [        R\                  " [        R
                  R                  [        S;5      U	S9   5        S n
UR,                  (       a  S<UR,                   S=3nU" US5SS69nUS7   RS                  U5      nURU                  5       S8:X  a  URW                  S95      n['        S>[Y        URZ                  5       35        [        R\                  " [        R
                  R                  [        S?5      US9   5        U/n
[        R^                  " U[        5        S@ UR`                  l1        URd                  " ShU	/U
UR.                  /UR*                  /SUR2                  SA.[        Rf                  D6u  pUS9   n['        SB[Y        URZ                  5       SC35        [        R\                  " [        R
                  R                  [        SD5      U5        [        R\                  " [        R
                  R                  [        SE5      US9   5        URh                  Rk                  SFU0/5      u  nn[8        Rl                  " US9   [8        RL                  SG9n[n        Rp                  " UR                  UUSHSI9  [        Rr                  " [        R
                  R                  [        SJ5      U5        ['        SKURZ                  S9    SLU SMURZ                  S9   U-  SN SOUR                   35        [        R
                  Ru                  [        Rv                  5      (       d3  ['        SP[        Rv                   SQ35        [x        Rz                  " S85        [|        R                  UR                  SR9n[        R                  UR                  SR9nUU4 HM  n[        R
                  Ru                  U5      (       a  M)  ['        SSU 35        [x        Rz                  " S85        MO     ['        STUR                   SUU SVU 35        AUS-:X  a  [4        RB                  R                  5         [        Rv                  SWUSXUS[        UR0                  5      SUR*                  SUR.                  SY[        UR2                  5      SZ[        S[UR                  S\/nUR,                  (       a  SUR,                  /US]S]& ['        S^S_R                  U5       35        [        R                  " UUSS`9nUR                  S9:w  a   [x        Rz                  " UR                  5        [n        R"                  " UR                  5      u  nnUR                  S8:  a	  US S 2S94   nUR                  [8        RL                  5      n['        SaURZ                  S9    SLU SMURZ                  S9   U-  SN SOUR                   35        [        R                  " S;[        [        Sb5        [        R                  " [        R                  [        [        5        [        R                  " SD[        [        Sc5        [        R                  " SJ[        [        5      u  nn['        Sd[        R                  " UU5      Se 35        [        UR                  UR                  5      n['        Sf[        R                  " UR                  5       S U UR                  5       S U 5      Se SgU 35        g ! , (       d  f       G	N= f)iNz--promptz../examples/prompt.txt)defaultz	--speakervivianz6speaker preset key (lowercase), validated by the model)r   helpz
--instruct z>optional style instruction, empty disables the instruct prefixz--seed*   )typer   z--langenglishz--quantF32z2GGUF quantization suffix (F32, BF16, Q8_0, Q4_K_M)z--out-ptzcustomvoice-python.wavz	--out-cppzcustomvoice-cpp.wavz--max-new-tokens@   z--trace
store_truez3print per sample u and idx for the first 32 samples)actionr   .T)exist_okrzutf-8)encodingz[Input] Prompt: z chars: <   z...z[Input] Speaker: z[Input] Instruct: z[Input] Lang: z Seed: z MaxNewTokens: z[Input] Mode: greedycudacpuz[Python] Device: eager)
device_mapdtypeattn_implementation)fix_mistral_regexz<|im_start|>assistant
z!<|im_end|>
<|im_start|>assistant
pt)textreturn_tensorspadding	input_ids   r   z[Python] InputIds shape: zprompt-ids.binz<|im_start|>user
z<|im_end|>
z[Python] InstructIds shape: zinstruct-ids.binc                      g )N )aks     debug-customvoice-cossim.py<lambda>main.<locals>.<lambda>p   s    $    )r   instruct_ids	languagesspeakersnon_streaming_modemax_new_tokensz[Python] Codes shape: z (T_frames, num_code_groups)zcodes-full.binzcodes-step0.binaudio_codes)r   FLOAT)subtypezoutput-audio.binz[Python] Audio: z	 samples z Hz z.2fzs -> z[Cossim] FATAL: z  not found, build qwen-tts first)qz [Cossim] FATAL: GGUF not found: z[Quant] z -> z + z--modelz--codecz	--max-newz--dumpz-oz--greedyz[GGML] Cmd:  )inputr   z[GGML] Audio: 	PromptIDs	CodesFullz[Cossim] Audio cos: z.6fz[Cossim] WAV stft_cos: z
 samples: r"   )SargparseArgumentParseradd_argumentintospathjoinDUMP_PTDUMP_CPP
parse_argscc
ensure_dirmakedirsdirnameout_ptopenpromptreadstripprintlenspeakerinstructlangseedr-   torchmanual_seednprandom	set_tracetraceregister_qwen3_ttsr   is_available	AutoModelfrom_pretrainedCKPTfloat32evalAutoProcessortodim	unsqueezetupleshapesave_dump_i32install_hookstalker_validate_model_kwargsgenerateGEN_KWARGS_GREEDYspeech_tokenizerdecodeasarraysfwrite	save_dumpisfileBINsysexitMODEL_TformatquantMODEL_CDC_Tempty_cachestrout_cpp
subprocessrun
returncodendimastypecompare_exact_i32compare_stagesSTAGES_STANDARDpaircosminsizestft_cosravel)apargsfr   devicemodel	processorassistant_textinp_uttr   instruct_ids_arginstruct_textinp_insr)   talker_codes_list_codeswavsfsaudio_ptmodel_lm	model_cdcpcmdr   	audio_cppsraaabns                                 r%   mainr   &   s   		 	 	"BOOJ0HOIOOKQ  SOOLY  [OOHS"O=OOH	O:OOIM  OOOJWG_0`OaOOKXG\0]O^OO&S"O=OOI|N  P==?DMM'MM(KK,3dC	dkk3	1Qvvx~~ 
2	SYKxSb	{CIPRN5XZ:[
\]	dll^
,-}}"4==/23	N499+WTYYKtGZGZF[
\]	 "	dii IINN499LLzz..00VeF	fX
&'LL((mm#	 ) 
 df 
   000NI /tf4WXN~dDQG$''/I}}!''*		%eIOO&<%=
>?RWW\\'+;<ilK }},T]]O<H mDRVW{+..v6"'11!4L,U<3E3E-F,GHI
g/ABLQROT(>UG$
 +?ELL' >> +%99+,,** 

 a E	"5#5"66R
STRWW\\'+;<uERWW\\'+<=uQxH%%,,}e.D-EFHD"zz$q'4HHHT[[(B8LLg'9:HE	X^^A./yDPQARSUAUVY@ZZ_`d`k`k_l
mn77>>"&&!! (HIJ,H""TZZ"0I	"ww~~a  4QC89HHQK # 
HTZZLXJc)
=>

  	XYS^T\\TYYS,,-XT\\C }}"DMM2Br
	L#
()s$T2A||qGGDLL)MIr~~adO	  ,I	N9??1-.it4	PQ@RSU@UVY?ZZ_`d`l`l_m
no)8WkJb(((G<)8WkJWW'7;FB	 B 4
56BGGRWWA	#BKK
2A
2A$OPS#TT^_`^a
bcu 
2	1s   &k
k__main__)__doc__r7   r;   rz   rq   numpyrR   	soundfilerl   rP   cossim_commonrA   rs   rv   rZ   r?   r>   r   __name__r"   r(   r%   <module>r      sY   &  	  
    ?6>"Pdf zF r(   