generate.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. import os
  2. import queue
  3. import string
  4. import threading
  5. import time
  6. from dataclasses import dataclass
  7. from pathlib import Path
  8. from typing import Literal, Optional, Tuple, Union
  9. import click
  10. import hydra
  11. import numpy as np
  12. import torch
  13. import torch._dynamo.config
  14. import torch._inductor.config
  15. from hydra import compose, initialize
  16. from hydra.utils import instantiate
  17. from loguru import logger
  18. from tqdm import tqdm
  19. from transformers import AutoTokenizer
  20. from fish_speech.datasets.text import CODEBOOK_EOS_TOKEN_ID, CODEBOOK_PAD_TOKEN_ID
  21. from fish_speech.text.clean import clean_text
  22. os.environ["TOKENIZERS_PARALLELISM"] = "false"
  23. torch._inductor.config.coordinate_descent_tuning = True
  24. torch._inductor.config.triton.unique_kernel_names = True
  25. if hasattr(torch._inductor.config, "fx_graph_cache"):
  26. # Experimental feature to reduce compilation times, will be on by default in future
  27. torch._inductor.config.fx_graph_cache = True
  28. from fish_speech.models.text2semantic.llama import DualARTransformer, NaiveTransformer
  29. def multinomial_sample_one_no_sync(
  30. probs_sort,
  31. ): # Does multinomial sampling without a cuda synchronization
  32. q = torch.empty_like(probs_sort).exponential_(1)
  33. return torch.argmax(probs_sort / q, dim=-1, keepdim=True).to(dtype=torch.int)
  34. def logits_to_probs(
  35. logits,
  36. previous_tokens: Optional[torch.Tensor] = None,
  37. temperature: torch.Tensor = 1.0,
  38. top_p: torch.Tensor = 1.0,
  39. repetition_penalty: torch.Tensor = 1.0,
  40. ) -> torch.Tensor:
  41. # Apply repetition penalty
  42. if previous_tokens is not None:
  43. previous_tokens = previous_tokens.long()
  44. score = torch.gather(logits, dim=0, index=previous_tokens)
  45. score = torch.where(
  46. score < 0, score * repetition_penalty, score / repetition_penalty
  47. )
  48. logits.scatter_(dim=0, index=previous_tokens, src=score)
  49. # Apply top-p sampling
  50. sorted_logits, sorted_indices = torch.sort(logits, descending=True)
  51. cum_probs = torch.cumsum(torch.nn.functional.softmax(sorted_logits, dim=-1), dim=-1)
  52. sorted_indices_to_remove = cum_probs > top_p
  53. sorted_indices_to_remove[0] = False # keep at least one option
  54. indices_to_remove = sorted_indices_to_remove.scatter(
  55. dim=0, index=sorted_indices, src=sorted_indices_to_remove
  56. )
  57. logits = logits.masked_fill(indices_to_remove, -float("Inf"))
  58. logits = logits / max(temperature, 1e-5)
  59. probs = torch.nn.functional.softmax(logits, dim=-1)
  60. return probs
  61. def sample(
  62. logits,
  63. previous_tokens: Optional[torch.Tensor] = None,
  64. **sampling_kwargs,
  65. ) -> Tuple[torch.Tensor, torch.Tensor]:
  66. probs = logits_to_probs(
  67. logits=logits[0, -1], previous_tokens=previous_tokens, **sampling_kwargs
  68. )
  69. idx_next = multinomial_sample_one_no_sync(probs)
  70. return idx_next, probs
  71. def decode_one_token_ar(
  72. model: DualARTransformer,
  73. x: torch.Tensor,
  74. input_pos: torch.Tensor,
  75. previous_tokens: torch.Tensor = None,
  76. **sampling_kwargs,
  77. ) -> torch.Tensor:
  78. x = model.forward_generate(x, input_pos)
  79. codebooks = [
  80. sample(
  81. x.logits,
  82. previous_tokens=None, # Disable repetition penalty for the token codebook
  83. **sampling_kwargs,
  84. )[0]
  85. ]
  86. x = x.hidden_states
  87. # Cleanup the cache
  88. for layer in model.fast_layers:
  89. layer.attention.kv_cache.k_cache.fill_(0)
  90. layer.attention.kv_cache.v_cache.fill_(0)
  91. for codebook_idx in range(model.config.num_codebooks):
  92. input_pos = torch.tensor([codebook_idx], device=x.device, dtype=torch.long)
  93. logits = model.forward_generate_fast(x, input_pos)
  94. a = sample(
  95. logits,
  96. previous_tokens=(
  97. previous_tokens[codebook_idx + 1]
  98. if previous_tokens is not None
  99. else None
  100. ),
  101. **sampling_kwargs,
  102. )[0]
  103. x = model.fast_embeddings(a)
  104. codebooks.append(a)
  105. return torch.stack(codebooks, dim=0)
  106. def decode_one_token_naive(
  107. model: NaiveTransformer,
  108. x: torch.Tensor,
  109. input_pos: torch.Tensor,
  110. previous_tokens: torch.Tensor = None,
  111. **sampling_kwargs,
  112. ) -> torch.Tensor:
  113. x = model.forward_generate(x, input_pos)
  114. codebooks = [
  115. sample(
  116. x.token_logits,
  117. previous_tokens=None, # Disable repetition penalty for the token codebook
  118. **sampling_kwargs,
  119. )[0]
  120. ]
  121. for i in range(model.config.num_codebooks):
  122. codebooks.append(
  123. sample(
  124. x.codebook_logits[:, :, i],
  125. previous_tokens=(
  126. previous_tokens[i + 1] if previous_tokens is not None else None
  127. ),
  128. **sampling_kwargs,
  129. )[0]
  130. )
  131. return torch.stack(codebooks, dim=0)
  132. def decode_n_tokens(
  133. model: NaiveTransformer,
  134. cur_token: torch.Tensor,
  135. input_pos: torch.Tensor,
  136. num_new_tokens: int,
  137. eos_token_id: int = 2,
  138. im_end_id: int = 4,
  139. decode_one_token=decode_one_token_naive,
  140. **sampling_kwargs,
  141. ):
  142. previous_tokens = torch.zeros(
  143. (model.config.num_codebooks + 1, model.config.max_seq_len),
  144. dtype=torch.int,
  145. device=cur_token.device,
  146. )
  147. for i in tqdm(range(num_new_tokens)):
  148. # We need to get windowed repeat penalty
  149. win_size = 16
  150. if i < win_size:
  151. window = previous_tokens[:, :win_size]
  152. else:
  153. window = previous_tokens[:, i - win_size : i]
  154. with torch.backends.cuda.sdp_kernel(
  155. enable_flash=False, enable_mem_efficient=False, enable_math=True
  156. ): # Actually better for Inductor to codegen attention here
  157. next_token = decode_one_token(
  158. model=model,
  159. x=cur_token,
  160. input_pos=input_pos,
  161. previous_tokens=window,
  162. **sampling_kwargs,
  163. )
  164. input_pos += 1
  165. cur_token = next_token.view(1, model.config.num_codebooks + 1, -1)
  166. previous_tokens[:, i : i + 1] = next_token.view(
  167. model.config.num_codebooks + 1, -1
  168. )
  169. if (
  170. cur_token[0, 0, -1] == eos_token_id
  171. or cur_token[0, 0, -1] == im_end_id
  172. or (cur_token[0, 1:, -1] == CODEBOOK_EOS_TOKEN_ID).any()
  173. ):
  174. break
  175. return previous_tokens[:, : i + 1]
  176. @torch.no_grad()
  177. @torch.inference_mode()
  178. def generate(
  179. *,
  180. model: NaiveTransformer,
  181. prompt: torch.Tensor,
  182. max_new_tokens: int,
  183. eos_token_id: int = 2,
  184. im_end_id: int = 4,
  185. decode_one_token=decode_one_token_naive,
  186. **sampling_kwargs,
  187. ) -> torch.Tensor:
  188. """
  189. Takes a conditioning sequence (prompt) as input and continues to generate as many tokens as requested.
  190. """
  191. # create an empty tensor of the expected final shape and fill in the current tokens
  192. T = prompt.size(1)
  193. if max_new_tokens:
  194. if T + max_new_tokens > model.config.max_seq_len:
  195. max_new_tokens = model.config.max_seq_len - T
  196. logger.info(f"Truncating max_new_tokens to {max_new_tokens}")
  197. T_new = T + max_new_tokens
  198. else:
  199. T_new = model.config.max_seq_len
  200. max_new_tokens = T_new - T
  201. device, dtype = prompt.device, prompt.dtype
  202. with torch.device(device):
  203. model.setup_caches(
  204. max_batch_size=1, max_seq_len=T_new, dtype=next(model.parameters()).dtype
  205. )
  206. codebook_dim = 1 + model.config.num_codebooks
  207. # create an empty tensor of the expected final shape and fill in the current tokens
  208. empty = torch.empty((codebook_dim, T_new), dtype=dtype, device=device)
  209. empty[:, :T] = prompt
  210. seq = empty
  211. input_pos = torch.arange(0, T, device=device)
  212. # Use non-accelerated version for now, to avoid compilation overhead
  213. prefill_decode = (
  214. decode_one_token_naive
  215. if isinstance(model, NaiveTransformer)
  216. else decode_one_token_ar
  217. )
  218. next_token = prefill_decode(
  219. model, prompt.view(1, codebook_dim, -1), input_pos, **sampling_kwargs
  220. )
  221. seq[:, T : T + 1] = next_token
  222. input_pos = torch.tensor([T], device=device, dtype=torch.int)
  223. x = decode_n_tokens(
  224. model,
  225. next_token.view(1, codebook_dim, -1),
  226. input_pos,
  227. max_new_tokens - 1,
  228. eos_token_id=eos_token_id,
  229. im_end_id=im_end_id,
  230. decode_one_token=decode_one_token,
  231. **sampling_kwargs,
  232. )
  233. # x = torch.cat(generated_tokens, dim=1)
  234. seq = seq[:, : T + 1 + x.size(1)]
  235. seq[:, T + 1 :] = x
  236. return seq
  237. def encode_tokens(
  238. tokenizer,
  239. string,
  240. bos=True,
  241. device="cuda",
  242. prompt_tokens=None,
  243. speaker=None,
  244. num_codebooks=4,
  245. ):
  246. string = clean_text(string)
  247. if speaker is None:
  248. speaker = "assistant"
  249. string = (
  250. f"<|im_start|>user<|im_sep|>{string}<|im_end|><|im_start|>{speaker}<|im_sep|>"
  251. )
  252. if bos:
  253. string = f"<|begin_of_sequence|>{string}"
  254. new_tokens = tokenizer.encode(
  255. string,
  256. add_special_tokens=False,
  257. max_length=10**6,
  258. truncation=False,
  259. )
  260. tokens = torch.tensor([new_tokens], dtype=torch.int, device=device)
  261. # Codebooks
  262. zeros = (
  263. torch.ones((num_codebooks, tokens.size(1)), dtype=torch.int, device=device)
  264. * CODEBOOK_PAD_TOKEN_ID
  265. )
  266. prompt = torch.cat((tokens, zeros), dim=0)
  267. if prompt_tokens is None:
  268. return prompt
  269. # Get prompt tokens
  270. if prompt_tokens.ndim == 3:
  271. assert (
  272. prompt_tokens.shape[0] == 1
  273. ), f"3 dim prompt tokens should have shape (1, num_codebooks, seq_len)"
  274. prompt_tokens = prompt_tokens[0]
  275. assert prompt_tokens.ndim == 2
  276. data = prompt_tokens + 2
  277. if prompt_tokens.shape[0] > num_codebooks:
  278. logger.warning(
  279. f"Prompt tokens shape {prompt_tokens.shape} is larger than num_codebooks {num_codebooks}, getting first {num_codebooks} codebooks"
  280. )
  281. data = data[:num_codebooks]
  282. # Add eos token for each codebook
  283. data = torch.cat(
  284. (
  285. data,
  286. torch.ones((data.size(0), 1), dtype=torch.int, device=device)
  287. * CODEBOOK_EOS_TOKEN_ID,
  288. ),
  289. dim=1,
  290. )
  291. # Since 1.0, we use <|semantic|>
  292. s0_token_id = tokenizer.convert_tokens_to_ids("<|semantic|>")
  293. end_token_id = tokenizer.convert_tokens_to_ids("<|im_end|>")
  294. main_token_ids = (
  295. torch.ones((1, data.size(1)), dtype=torch.int, device=device) * s0_token_id
  296. )
  297. main_token_ids[0, -1] = end_token_id
  298. data = torch.cat((main_token_ids, data), dim=0)
  299. prompt = torch.cat((prompt, data), dim=1)
  300. return prompt
  301. def load_model(
  302. config_name, checkpoint_path, device, precision, max_length, compile=False
  303. ):
  304. hydra.core.global_hydra.GlobalHydra.instance().clear()
  305. with initialize(version_base="1.3", config_path="../../fish_speech/configs/model"):
  306. cfg = compose(
  307. config_name=config_name, overrides=[f"config.max_seq_len={max_length}"]
  308. )
  309. model: Union[NaiveTransformer, DualARTransformer] = instantiate(cfg)
  310. if "int8" in str(checkpoint_path):
  311. logger.info("Using int8 weight-only quantization!")
  312. from .quantize import WeightOnlyInt8QuantHandler
  313. simple_quantizer = WeightOnlyInt8QuantHandler(model)
  314. model = simple_quantizer.convert_for_runtime()
  315. if "int4" in str(checkpoint_path):
  316. logger.info("Using int4 quantization!")
  317. path_comps = checkpoint_path.name.split(".")
  318. assert path_comps[-2].startswith("g")
  319. groupsize = int(path_comps[-2][1:])
  320. from .quantize import WeightOnlyInt4QuantHandler
  321. simple_quantizer = WeightOnlyInt4QuantHandler(model, groupsize)
  322. model = simple_quantizer.convert_for_runtime()
  323. checkpoint = torch.load(str(checkpoint_path), map_location="cpu")
  324. if "state_dict" in checkpoint:
  325. checkpoint = checkpoint["state_dict"]
  326. if any(k.startswith("model.") for k in checkpoint):
  327. checkpoint = {
  328. k.replace("model.", ""): v
  329. for k, v in checkpoint.items()
  330. if k.startswith("model.")
  331. }
  332. model.load_state_dict(checkpoint, assign=True)
  333. model = model.to(device=device, dtype=precision)
  334. logger.info("Restored model from checkpoint")
  335. if isinstance(model, DualARTransformer):
  336. decode_one_token = decode_one_token_ar
  337. logger.info("Using DualARTransformer")
  338. else:
  339. decode_one_token = decode_one_token_naive
  340. logger.info("Using NaiveTransformer")
  341. if compile:
  342. logger.info("Compiling function...")
  343. decode_one_token = torch.compile(
  344. decode_one_token, mode="reduce-overhead", fullgraph=True
  345. )
  346. return model.eval(), decode_one_token
  347. def split_text(text, min_length):
  348. text = clean_text(text)
  349. segments = []
  350. curr = ""
  351. def clean_add(curr):
  352. curr = curr.strip()
  353. if curr and not all(c.isspace() or c in string.punctuation for c in curr):
  354. segments.append(curr)
  355. def is_float(value):
  356. try:
  357. float(value)
  358. return True
  359. except ValueError:
  360. return False
  361. for index, char in enumerate(text):
  362. curr += char
  363. if char not in [".", "!", "?"]:
  364. continue
  365. if len(curr) >= min_length and not is_float(text[index - 1 : index + 2]):
  366. clean_add(curr)
  367. curr = ""
  368. clean_add(curr)
  369. return segments
  370. @dataclass
  371. class GenerateResponse:
  372. action: Literal["sample", "next"]
  373. codes: Optional[torch.Tensor] = None
  374. text: Optional[str] = None
  375. def generate_long(
  376. *,
  377. model,
  378. tokenizer: callable,
  379. device: str | torch.device,
  380. decode_one_token: callable,
  381. text: str,
  382. num_samples: int = 1,
  383. max_new_tokens: int = 0,
  384. top_p: int = 0.7,
  385. repetition_penalty: float = 1.5,
  386. temperature: float = 0.7,
  387. compile: bool = False,
  388. iterative_prompt: bool = True,
  389. max_length: int = 2048,
  390. chunk_length: int = 30,
  391. speaker: Optional[str] = None,
  392. prompt_text: Optional[str] = None,
  393. prompt_tokens: Optional[torch.Tensor] = None,
  394. ):
  395. assert 0 < top_p <= 1, "top_p must be in (0, 1]"
  396. assert 0 < repetition_penalty < 2, "repetition_penalty must be in (0, 2)"
  397. assert 0 < temperature < 2, "temperature must be in (0, 2)"
  398. model_size = sum(p.numel() for p in model.parameters() if p.requires_grad)
  399. im_end_id = tokenizer.convert_tokens_to_ids("<|im_end|>")
  400. use_prompt = prompt_text is not None and prompt_tokens is not None
  401. encoded = []
  402. texts = split_text(text, chunk_length) if iterative_prompt else [text]
  403. if use_prompt:
  404. encoded_prompts = encode_tokens(
  405. tokenizer,
  406. prompt_text,
  407. prompt_tokens=prompt_tokens,
  408. bos=True,
  409. device=device,
  410. speaker=speaker,
  411. num_codebooks=model.config.num_codebooks,
  412. )
  413. for idx, text in enumerate(texts):
  414. encoded.append(
  415. encode_tokens(
  416. tokenizer,
  417. string=text,
  418. bos=idx == 0 and not use_prompt,
  419. device=device,
  420. speaker=speaker,
  421. num_codebooks=model.config.num_codebooks,
  422. )
  423. )
  424. logger.info(f"Encoded text: {text}")
  425. # Move temperature, top_p, repetition_penalty to device
  426. # This is important so that changing params doesn't trigger recompile
  427. temperature = torch.tensor(temperature, device=device, dtype=torch.float)
  428. top_p = torch.tensor(top_p, device=device, dtype=torch.float)
  429. repetition_penalty = torch.tensor(
  430. repetition_penalty, device=device, dtype=torch.float
  431. )
  432. for sample_idx in range(num_samples):
  433. if torch.cuda.is_available():
  434. torch.cuda.synchronize()
  435. global_encoded = []
  436. seg_idx = 0
  437. while seg_idx < len(encoded):
  438. logger.info(
  439. f"Generating sentence {seg_idx + 1}/{len(encoded)} of sample {sample_idx + 1}/{num_samples}"
  440. )
  441. seg = encoded[seg_idx]
  442. global_encoded.append(seg)
  443. lengths = reversed([seg.size(1) for seg in global_encoded])
  444. # Pick last 2000 tokens
  445. count = 0
  446. for i, length in enumerate(lengths):
  447. count += length
  448. if count + length > max_length - 1024:
  449. break
  450. if i != 0 and i % 2 == 0:
  451. i -= 1
  452. # Rotate the list, always make sure first segment is included to avoid drift
  453. if i < len(global_encoded) - 2:
  454. partial_encoded = global_encoded[:2] + global_encoded[-i:]
  455. else:
  456. partial_encoded = global_encoded
  457. if use_prompt:
  458. partial_encoded = [encoded_prompts] + partial_encoded
  459. cat_encoded = torch.cat(partial_encoded, dim=1)
  460. prompt_length = cat_encoded.size(1)
  461. t0 = time.perf_counter()
  462. y = generate(
  463. model=model,
  464. prompt=cat_encoded,
  465. max_new_tokens=max_new_tokens,
  466. eos_token_id=tokenizer.eos_token_id,
  467. im_end_id=im_end_id,
  468. decode_one_token=decode_one_token,
  469. temperature=temperature,
  470. top_p=top_p,
  471. repetition_penalty=repetition_penalty,
  472. )
  473. if sample_idx == 0 and seg_idx == 0 and compile:
  474. logger.info(f"Compilation time: {time.perf_counter() - t0:.2f} seconds")
  475. if torch.cuda.is_available():
  476. torch.cuda.synchronize()
  477. t = time.perf_counter() - t0
  478. tokens_generated = y.size(1) - prompt_length
  479. tokens_sec = tokens_generated / t
  480. logger.info(
  481. f"Generated {tokens_generated} tokens in {t:.02f} seconds, {tokens_sec:.02f} tokens/sec"
  482. )
  483. logger.info(
  484. f"Bandwidth achieved: {model_size * tokens_sec / 1e9:.02f} GB/s"
  485. )
  486. if torch.cuda.is_available():
  487. logger.info(
  488. f"GPU Memory used: {torch.cuda.max_memory_reserved() / 1e9:.02f} GB"
  489. )
  490. # Put the generated tokens
  491. # since there is <im_end> and <eos> tokens, we remove last 2 tokens
  492. codes = y[1:, prompt_length:-2].clone()
  493. codes = codes - 2
  494. assert (codes >= 0).all(), f"Negative code found"
  495. decoded = y[:, prompt_length:-1].clone()
  496. if decoded[0, -1] != im_end_id: # <im_end>
  497. val = [[im_end_id]] + [[CODEBOOK_EOS_TOKEN_ID]] * (decoded.size(0) - 1)
  498. decoded = torch.cat(
  499. (decoded, torch.tensor(val, device=device, dtype=torch.int)), dim=1
  500. )
  501. # But for global encoding, we should keep the <im_end> token
  502. global_encoded.append(decoded)
  503. assert (codes >= 0).all(), f"Negative code found: {codes}"
  504. yield GenerateResponse(action="sample", codes=codes, text=texts[seg_idx])
  505. seg_idx += 1
  506. # This indicates the end of the current sample
  507. yield GenerateResponse(action="next")
  508. @dataclass
  509. class WrappedGenerateResponse:
  510. status: Literal["success", "error"]
  511. response: Optional[GenerateResponse | Exception] = None
  512. @dataclass
  513. class GenerateRequest:
  514. request: dict
  515. response_queue: queue.Queue
  516. def launch_thread_safe_queue(
  517. config_name,
  518. checkpoint_path,
  519. device,
  520. precision,
  521. max_length: int,
  522. compile: bool = False,
  523. ):
  524. input_queue = queue.Queue()
  525. init_event = threading.Event()
  526. def worker():
  527. model, decode_one_token = load_model(
  528. config_name, checkpoint_path, device, precision, max_length, compile=compile
  529. )
  530. init_event.set()
  531. while True:
  532. item: GenerateRequest | None = input_queue.get()
  533. if item is None:
  534. break
  535. kwargs = item.request
  536. response_queue = item.response_queue
  537. try:
  538. for chunk in generate_long(
  539. model=model, decode_one_token=decode_one_token, **kwargs
  540. ):
  541. response_queue.put(
  542. WrappedGenerateResponse(status="success", response=chunk)
  543. )
  544. except Exception as e:
  545. response_queue.put(WrappedGenerateResponse(status="error", response=e))
  546. threading.Thread(target=worker, daemon=True).start()
  547. init_event.wait()
  548. return input_queue
  549. @click.command()
  550. @click.option(
  551. "--text",
  552. type=str,
  553. default="你说的对, 但是原神是一款由米哈游自主研发的开放世界手游.",
  554. )
  555. @click.option("--prompt-text", type=str, default=None)
  556. @click.option(
  557. "--prompt-tokens", type=click.Path(path_type=Path, exists=True), default=None
  558. )
  559. @click.option("--num-samples", type=int, default=1)
  560. @click.option("--max-new-tokens", type=int, default=0)
  561. @click.option("--top-p", type=float, default=0.7)
  562. @click.option("--repetition-penalty", type=float, default=1.5)
  563. @click.option("--temperature", type=float, default=0.7)
  564. @click.option(
  565. "--checkpoint-path",
  566. type=click.Path(path_type=Path, exists=True),
  567. default="checkpoints/text2semantic-sft-medium-v1-4k.pth",
  568. )
  569. @click.option("--config-name", type=str, default="dual_ar_2_codebook_medium")
  570. @click.option("--tokenizer", type=str, default="fishaudio/fish-speech-1")
  571. @click.option("--compile/--no-compile", default=False)
  572. @click.option("--seed", type=int, default=42)
  573. @click.option("--speaker", type=str, default=None)
  574. @click.option("--half/--no-half", default=False)
  575. @click.option("--iterative-prompt/--no-iterative-prompt", default=True)
  576. @click.option("--max-length", type=int, default=2048)
  577. @click.option("--chunk-length", type=int, default=30)
  578. def main(
  579. text: str,
  580. prompt_text: Optional[str],
  581. prompt_tokens: Optional[Path],
  582. num_samples: int,
  583. max_new_tokens: int,
  584. top_p: int,
  585. repetition_penalty: float,
  586. temperature: float,
  587. checkpoint_path: Path,
  588. config_name: str,
  589. tokenizer: str,
  590. compile: bool,
  591. seed: int,
  592. speaker: Optional[str],
  593. half: bool,
  594. iterative_prompt: bool,
  595. max_length: int,
  596. chunk_length: int,
  597. ) -> None:
  598. device = "cuda"
  599. precision = torch.half if half else torch.bfloat16
  600. logger.info("Loading model ...")
  601. t0 = time.time()
  602. model, decode_one_token = load_model(
  603. config_name, checkpoint_path, device, precision, max_length, compile=compile
  604. )
  605. if torch.cuda.is_available():
  606. torch.cuda.synchronize()
  607. logger.info(f"Time to load model: {time.time() - t0:.02f} seconds")
  608. prompt_tokens = (
  609. torch.from_numpy(np.load(prompt_tokens)).to(device)
  610. if prompt_tokens is not None
  611. else None
  612. )
  613. tokenizer = AutoTokenizer.from_pretrained(tokenizer)
  614. torch.manual_seed(seed)
  615. if torch.cuda.is_available():
  616. torch.cuda.manual_seed(seed)
  617. generator = generate_long(
  618. model=model,
  619. device=device,
  620. decode_one_token=decode_one_token,
  621. text=text,
  622. num_samples=num_samples,
  623. max_new_tokens=max_new_tokens,
  624. top_p=top_p,
  625. repetition_penalty=repetition_penalty,
  626. temperature=temperature,
  627. tokenizer=tokenizer,
  628. compile=compile,
  629. speaker=speaker,
  630. iterative_prompt=iterative_prompt,
  631. max_length=max_length,
  632. chunk_length=chunk_length,
  633. prompt_text=prompt_text,
  634. prompt_tokens=prompt_tokens,
  635. )
  636. idx = 0
  637. codes = []
  638. for response in generator:
  639. if response.action == "sample":
  640. codes.append(response.codes)
  641. logger.info(f"Sampled text: {response.text}")
  642. elif response.action == "next":
  643. if codes:
  644. np.save(f"codes_{idx}.npy", torch.cat(codes, dim=1).cpu().numpy())
  645. logger.info(f"Saved codes to codes_{idx}.npy")
  646. logger.info(f"Next sample")
  647. idx += 1
  648. else:
  649. logger.error(f"Error: {response}")
  650. if __name__ == "__main__":
  651. main()