Skip to content

Add repetition_filter operator for degenerate rollout responses - #643

Open
Kingpython123 wants to merge 1 commit into
agentscope-ai:mainfrom
Kingpython123:feat/repetition-filter
Open

Kingpython123 wants to merge 1 commit into
agentscope-ai:mainfrom
Kingpython123:feat/repetition-filter

Conversation

@Kingpython123

Copy link
Copy Markdown

Motivation

Closes #620. Part of the operator wish list in #470.

None of the existing experience filters (reward_filter, reward_std_filter, dapo_dynamic_sampling, mask_response_truncated, invalid_reward_filter) look at the text of a response. Repetition collapse is a common failure mode of on-policy rollouts, especially for smaller models on long-horizon tasks: the policy keeps emitting the same pattern instead of terminating. Those trajectories consume rollout budget and carry almost no learning signal, yet they currently enter the training buffer like any other experience.

Implementation

New operator repetition_filter (trinity/buffer/operators/filters/repetition_filter.py), implemented on ExperienceOperatorV1 and registered in EXPERIENCE_OPERATORS.

  • Metric: duplicate n-gram ratio 1 - unique_ngrams / total_ngrams over sliding windows of ngram_size tokens. It is 0.0 for a fully diverse response and approaches 1.0 for a response that keeps repeating a short pattern (e.g. [7, 8, 9] * 20 scores 0.947 with ngram_size=4). No new dependencies.
  • Only generated tokens are inspected: tokens before prompt_length are ignored, and when action_mask is present (multi-turn experiences) tokens with a zero mask (environment observations) are ignored as well.
  • Decision: the experience is dropped when ratio > max_repeat_ratio (strict). Empty responses and responses with fewer than min_response_tokens generated tokens are always kept, since the ratio is noisy on very short sequences. Dropping (rather than masking) matches the wording of Good First Issue List #470; switching to zeroing action_mask would be a small change if preferred.
  • Defaults: ngram_size=4, max_repeat_ratio=0.5, min_response_tokens=32, as proposed in [FEATURE] Repetition filter operator for degenerate rollout responses #620. max_repeat_ratio=1.0 disables the filter. Invalid arguments raise ValueError at construction time.
  • Metrics: filtered_count, skipped_empty_count, skipped_short_count, repeat_ratio/mean, repeat_ratio/max (the last two only when at least one response was scored).
data_processor:
  experience_pipeline:
    operators:
      - name: repetition_filter
        args:
          ngram_size: 4
          max_repeat_ratio: 0.5
          min_response_tokens: 32

Docs

Added a "Built-in Operators" table to the operator development guide (docs/sphinx_doc/source/tutorial/develop_operator.md and the source_zh counterpart) listing all registered experience operators and their main arguments, including the new one.

Tests

tests/buffer/repetition_filter_test.py (CPU-only, 11 tests) covers:

  • the ratio function (diverse, repetitive, shorter than ngram_size, single duplicate);
  • keeping diverse responses and dropping repetitive ones, with the reported metrics;
  • empty and very short responses being kept without error;
  • the threshold being strict (ratio == max_repeat_ratio is kept);
  • prompt tokens and action_mask-excluded tokens not affecting the decision;
  • max_repeat_ratio=1.0 disabling the filter, argument validation, and creation through create_operators from the registry.
python -m pytest tests/buffer/repetition_filter_test.py -q
11 passed

pre-commit run passes on all changed files.

…tscope-ai#620)

Add a RepetitionFilter experience operator that drops experiences whose
generated response has collapsed into repetition. The response is scored
by its duplicate n-gram ratio (1 - unique_ngrams / total_ngrams) over the
model-generated tokens only, ignoring prompt tokens and tokens masked out
by action_mask. Experiences are dropped when the ratio is strictly greater
than max_repeat_ratio; empty and very short responses are always kept.

- Register as repetition_filter in EXPERIENCE_OPERATORS
- Add unit tests for the ratio function, the filter, action_mask handling
  and registry creation
- Document built-in operators in the operator development guide (en/zh)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Repetition filter operator for degenerate rollout responses

1 participant