
    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)
a  Cossim debug : C++ qwen-tts vs Python Qwen3-TTS on the VoiceDesign 1.7B path.

Inputs (relative to CWD = tests/) :
    ../examples/prompt.txt       target text fed to both pipelines
    --instruct flag              style instruction, default mirrors tts.sh

Default mode is greedy (do_sample=False on both sides). Cote Python the
utterance and the instruction are tokenized separately and passed to
model.generate as input_ids and instruct_ids, mirroring exactly what
qwen_tts.inference.qwen3_tts_model.generate_voice_design does. Cote C++
the utterance is piped on stdin and the instruction is passed via
--instruct, the prompt builder wraps and tokenizes them in the same
order.

Dumps land in cpp/tts/ (C++) and python/tts/ (Python).
    Nz/../models/qwen-talker-1.7b-voicedesign-{q}.ggufz&../models/qwen-tokenizer-12hz-{q}.ggufz.../checkpoints/Qwen3-TTS-12Hz-1.7B-VoiceDesignzcpp/ttsz
python/ttsc                     [         R                  " 5       n U R                  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        ['        S&UR,                   S'UR.                   S(UR0                   35        ['        S)5        [2        R4                  " UR.                  5        [6        R8                  R/                  UR.                  5        [        R:                  " UR<                  5        [        R>                  " 5         [2        R@                  RC                  5       (       a  S*OS+n['        S,U 35        [        RD                  RG                  [H        U[2        RJ                  S-S.9RM                  5       n[        RN                  RG                  [H        SS/9nS0U S13nU" US2SS39nUS4   RQ                  U5      n	U	RS                  5       S5:X  a  U	RU                  S65      n	['        S7[W        U	RX                  5       35        [        RZ                  " [        R
                  R                  [        S85      U	S6   5        S9UR*                   S:3n
U" U
S2SS39nUS4   RQ                  U5      nURS                  5       S5:X  a  URU                  S65      n['        S;[W        URX                  5       35        [        RZ                  " [        R
                  R                  [        S<5      US6   5        [        R\                  " U[        5        S= UR^                  l0        URb                  " SdU	/U/UR,                  /SUR0                  S>.[        Rd                  D6u  pUS6   n['        S?[W        URX                  5       S@35        [        RZ                  " [        R
                  R                  [        SA5      U5        [        RZ                  " [        R
                  R                  [        SB5      US6   5        URf                  Ri                  SCU0/5      u  nn[6        Rj                  " US6   [6        RJ                  SD9n[l        Rn                  " UR                  UUSESF9  [        Rp                  " [        R
                  R                  [        SG5      U5        ['        SHURX                  S6    SIU SJURX                  S6   U-  SK SLUR                   35        [        R
                  Rs                  [        Rt                  5      (       d3  ['        SM[        Rt                   SN35        [v        Rx                  " S55        [z        R}                  UR~                  SO9n[        R}                  UR~                  SO9nUU4 HM  n[        R
                  Rs                  U5      (       a  M)  ['        SPU 35        [v        Rx                  " S55        MO     ['        SQUR~                   SRU SSU 35        AUS*:X  a  [2        R@                  R                  5         [        Rt                  STUSUUS[        UR.                  5      SUR*                  SUR,                  SV[        UR0                  5      SW[        SXUR                  SY/n['        SZS[R                  U5       35        [        R                  " UUSS\9nUR                  S6:w  a   [v        Rx                  " UR                  5        [l        R"                  " UR                  5      u  nnUR                  S5:  a	  US S 2S64   nUR                  [6        RJ                  5      n['        S]URX                  S6    SIU SJURX                  S6   U-  SK SLUR                   35        [        R                  " S8[        [        S^5        [        R                  " [        R                  [        [        5        [        R                  " SA[        [        S_5        [        R                  " SG[        [        5      u  nn['        S`[        R                  " UU5      Sa 35        [        UR                  UR                  5      n['        Sb[        R                  " UR                  5       S U UR                  5       S U 5      Sa ScU 35        g ! , (       d  f       GN= f)eNz--promptz../examples/prompt.txt)defaultz
--instructz!male, young adult, moderate pitchz"natural language style instruction)r   helpz--seed*   )typer   z--langenglishz--quantF32z2GGUF quantization suffix (F32, BF16, Q8_0, Q4_K_M)z--out-ptztts-python.wavz	--out-cppztts-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] 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-tts-cossim.py<lambda>main.<locals>.<lambda>g   s    $    )r   instruct_ids	languages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!   )R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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_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OOL0S=  ?OOHS"O=OOH	O:OOIM  OOOJWGW0XOYOOKX}0UOVOO&S"O=OOI|N  P==?DMM'MM(KK,3dC	dkk3	1Qvvx~~ 
2	SYKxSb	{CIPRN5XZ:[
\]	t}}o
./	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 )|DM-dSG;'**62LQ#--a0	(|/A/A)B(C
DERWW\\'+=>QP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)
=>

  	hic$))ndmmdiic$--.hdllC 
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ce 
2	1s   i
i__main__)__doc__r4   r8   rv   rm   numpyrN   	soundfilerh   rL   cossim_commonr>   ro   rr   rV   r<   r;   r   __name__r!   r'   r$   <module>r      sY   "  	  
    ?6>FdR zF r'   