num_repeats実行時にfew-shot生成器のRNG状態が全リピートで共有される問題を修正 - #297
Open
kiakiraki wants to merge 1 commit into
Open
Conversation
maybe_replace_random_seedはdataclasses.replaceの浅いコピーで random_seedのみ差し替えるため、few_shot_generatorは全リピートで 同一オブジェクトを共有していた。RandomFewShotGenerator / BalancedFewShotGeneratorは永続的なrandom.Randomを持つため、 各リピートのfew-shot例がシードではなく実行順序(それまでの サンプリング回数)に依存し、特定リピートのみの再実行で 一括実行と異なるfew-shot文脈になり再現性が壊れていた。 FewShotGeneratorにwith_seed_incrementを追加し、リピート毎に seed + incrementで構築した新しい生成器インスタンスに差し替える (datasetは参照共有)。保存されるconfigのfew_shot_generatorの seedも同様にincrementし、単一リピートの再実行で同じfew-shot例が 再現されるようにした。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
kiakiraki
marked this pull request as ready for review
July 19, 2026 02:58
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
問題
flexeval_lm --num_repeats N実行時、maybe_replace_random_seed(flexeval/scripts/flexeval_lm.py)はdataclasses.replaceの浅いコピーでrandom_seedのみを差し替えるため、few_shot_generatorは全リピートで同一オブジェクトを共有していました。RandomFewShotGenerator/BalancedFewShotGeneratorは永続的なrandom.Random(seed)を内部に持つため、次の問題が生じます。なお
eval_setup.random_seedはlanguage_model.set_random_seed()にのみ流れており、few-shot 生成器のseedとは元々独立です。修正内容
「リピート毎に異なる few-shot 例」という現状の観測挙動を保ちつつ、シードのみから再現可能にしました。
FewShotGeneratorにwith_seed_increment(seed_increment)を追加(デフォルトはreturn selfで、FixedFewShotGeneratorのような無状態の生成器はそのまま)RandomFewShotGenerator/BalancedFewShotGeneratorでオーバーライドし、seed + seed_incrementで構築した新インスタンスを返す(datasetは参照共有で deepcopy しない)maybe_replace_random_seedでリピート毎に生成器を差し替え、保存される config のfew_shot_generator.init_args.seedも同様に increment(run1/config.jsonから単体再実行しても同じ few-shot 例が再現される)run0 は increment=0 なので、
--num_repeats 1の実行と few-shot 例が一致します。LM 側のrandom_seed + indexと同じ意味論です。テスト
テストファーストで実施し、修正前のコードで回帰テストが fail することを確認済みです(例: 3リピートの生成器の distinct id 数が
assert 1 == 3で fail =全リピートが同一オブジェクトを共有)。with_seed_incrementの単体テスト(新インスタンス・dataset 参照共有・シードのみからの再現性・事前サンプリング回数に非依存であることの回帰テスト)を rand / balanced 両方に追加maybe_replace_random_seed/generate_eval_entriesのテスト(リピート毎に別インスタンス、config の seed increment、config に seed キーが無い場合の防御動作、few_shot_generator=Noneや Perplexity setup で無害であること)を追加tests/core/few_show_generator/+tests/scripts/test_flexeval_lm.py: 81 passedruff check/ruff format --check: パス影響範囲
--num_repeats >= 2かつ few-shot 生成器(Random / Balanced)を使う評価のみ。run0 以外の各リピートの few-shot 例が変わります(従来は実行順序依存の非再現な値だったため、シード決定的な値への変化は正しい方向の変化です)--num_repeats 1(デフォルト)の結果は一切変わりません🤖 Generated with Claude Code