Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit 39f6902

Browse files
committed
Add dropout options to optimize overfitting
1 parent 38c599b commit 39f6902

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

fish_speech/configs/text2semantic_finetune.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ model:
5959
norm_eps: 1e-5
6060
num_codebooks: 4 # single codebook
6161
codebook_size: 168 # codebook size 160 + 2 special tokens
62+
dropout: 0.1 # For small dataset, dropout helps to prevent overfitting
6263

6364
optimizer:
6465
_target_: torch.optim.AdamW

fish_speech/configs/text2semantic_finetune_lora.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ model:
5959
norm_eps: 1e-5
6060
num_codebooks: 4 # single codebook
6161
codebook_size: 168 # codebook size 160 + 2 special tokens
62+
dropout: 0.1 # For small dataset, dropout helps to prevent overfitting
6263

6364
lora_config:
6465
_target_: fish_speech.models.text2semantic.lit_module.LoraConfig

fish_speech/models/text2semantic/llama.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ModelArgs:
3131
rope_base: float = 10000
3232
norm_eps: float = 1e-5
3333
max_seq_len: int = 2048
34+
dropout: float = 0.0
3435

3536
# Additional decoding heads
3637
codebook_size: int = 160
@@ -260,6 +261,7 @@ def __init__(self, config: ModelArgs):
260261
self.wo = nn.Linear(config.dim, config.dim, bias=False)
261262
self.kv_cache = None
262263

264+
self.dropout = config.dropout
263265
self.n_head = config.n_head
264266
self.head_dim = config.head_dim
265267
self.n_local_heads = config.n_local_heads
@@ -301,7 +303,13 @@ def forward(
301303

302304
k = k.repeat_interleave(self.n_head // self.n_local_heads, dim=1)
303305
v = v.repeat_interleave(self.n_head // self.n_local_heads, dim=1)
304-
y = F.scaled_dot_product_attention(q, k, v, attn_mask=mask, dropout_p=0.0)
306+
y = F.scaled_dot_product_attention(
307+
q,
308+
k,
309+
v,
310+
attn_mask=mask,
311+
dropout_p=self.dropout if self.training else 0.0,
312+
)
305313

306314
y = y.transpose(1, 2).contiguous().view(bsz, seqlen, self.dim)
307315
else:
@@ -311,7 +319,7 @@ def forward(
311319

312320
# We don't need to transpose q, k, v here because flash_attn_varlen_func
313321
attn_output = self._flash_attention_forward(
314-
q, k, v, mask, seqlen, dropout=0.0
322+
q, k, v, mask, seqlen, dropout=self.dropout if self.training else 0.0
315323
)
316324

317325
y = attn_output.reshape(bsz, seqlen, self.dim).contiguous()

0 commit comments

Comments
 (0)