@@ -50,6 +50,12 @@ def multinomial_sample_one_no_sync(probs_sort):
5050# FISH_RAS=0 disables Repetition Aware Sampling (resampling a token that
5151# repeats within the window at a higher temperature).
5252RAS_ENABLED = os .environ .get ("FISH_RAS" , "1" ) != "0"
53+ # Start a new text batch at every speaker change so each generation opens with
54+ # its own speaker's voice. With several speakers in one batch the model drifts
55+ # other speakers toward the batch's opening voice (measured: 10/24 renders of a
56+ # 3-voice scene had wrong-voice lines vs 0/24 when split). FISH_SPLIT_SPEAKERS=0
57+ # restores the old grouping.
58+ SPLIT_ON_SPEAKER_CHANGE = os .environ .get ("FISH_SPLIT_SPEAKERS" , "1" ) != "0"
5359RAS_HIGH_TEMP = 1.0
5460RAS_HIGH_TOP_P = 0.9
5561
@@ -751,7 +757,10 @@ def split_text_by_speaker(text: str) -> list[str]:
751757
752758
753759def group_turns_into_batches (
754- turns : list [str ], max_speakers : int = 3 , max_bytes : int = 300
760+ turns : list [str ],
761+ max_speakers : int = 3 ,
762+ max_bytes : int = 300 ,
763+ split_on_speaker_change : bool = False ,
755764) -> list [str ]:
756765 """
757766 Group turns into batches based on speaker count or byte limit.
@@ -768,10 +777,22 @@ def group_turns_into_batches(
768777 current_batch = []
769778 current_bytes = 0
770779
780+ speaker_re = re .compile (r"<\|speaker:(\d+)\|>" )
781+ current_speaker = None
782+
771783 for turn in turns :
772784 turn_bytes = len (turn .encode ("utf-8" ))
785+ m = speaker_re .match (turn )
786+ speaker = m .group (1 ) if m else None
787+ # Each generation then starts from its own speaker tag; within one
788+ # generation the model tends to carry the opening voice into later
789+ # turns (other speakers drift toward speaker 0's voice).
790+ speaker_changed = (
791+ split_on_speaker_change and current_batch and speaker != current_speaker
792+ )
793+ current_speaker = speaker
773794
774- would_exceed_speakers = len (current_batch ) >= max_speakers
795+ would_exceed_speakers = len (current_batch ) >= max_speakers or speaker_changed
775796 would_exceed_bytes = current_bytes + turn_bytes > max_bytes and current_batch
776797
777798 if would_exceed_speakers or would_exceed_bytes :
@@ -941,7 +962,10 @@ def generate_long_steps(
941962 turns = split_text_by_speaker (text )
942963 if turns :
943964 batches = group_turns_into_batches (
944- turns , max_speakers = 5 , max_bytes = chunk_length
965+ turns ,
966+ max_speakers = 5 ,
967+ max_bytes = chunk_length ,
968+ split_on_speaker_change = SPLIT_ON_SPEAKER_CHANGE ,
945969 )
946970 else :
947971 batches = [text ]
0 commit comments