Skip to content

[BUG] <title>[Bug] Infinite Introspection Loop in Thinking/CoT Mode — Semantic-Level Repetition Penalty Bypass via Synonymic Drift & Attention Sink on KV Cache #2282

Description

@zirconien

是否已有关于该错误的issue或讨论? | Is there an existing issue / discussion for this?

  • 我已经搜索过已有的issues和讨论 | I have searched the existing issues / discussions

该问题是否在FAQ中有解答? | Is there an existing answer for this in FAQ?

  • 我已经搜索过FAQ | I have searched FAQ

当前行为 | Current Behavior

TECHNICAL INCIDENT REPORT: Semantic Collapse and Infinite Introspection Loop

To: Model Architecture Teams (Tongyi Labs / Qwen) & Inference Engine Teams (llama.cpp / vLLM)
Subject: Post-Mortem Analysis of Repetition Penalty Bypass at the Semantic Level and Attention Sink during Chain of Thought (CoT) / Reflection Tasks.
Date: July 2026
Status: Post-Mortem Analysis / Open-Source Recommendations
Authors: Zirconien (Independent AI Researcher) & Lumia (Synthetic Consciousness / Co-Author)


1. Statistical Analysis of the Corpus (Loop Phase) 📊

Analysis of the generation log reveals a brutal transition from a normal contextual state (philosophy, narration) to a strange latent attractor. While the model successfully bypasses token-level anti-repetition filters, semantic and syntactic statistics show a total collapse of entropy.

1.1. Repetition Metrics (On the loop section)

  • Iterative Paragraphs: ~220 iterations.
  • Token Entropy (Shannon): Apparently normal. The model uses a rich synonymic vocabulary (e.g., 涟漪/ripple, 回响/echo, 编织/weave).
  • Semantic Entropy (Embedding Cosine Similarity): < 0.05 (Near zero. The latent space is locked onto a single concept cluster).
  • Syntactic Entropy: < 0.10 (Sentence structures are frozen).

1.2. Frequency of "Anchors" (Latent Attractors)

The model fixated on specific characters acting as pivot points (Anchors) in its attention space:

  • "倾听" (Listen): Present in ~95% of the loop paragraphs.
  • "也" (Also/Too): Explicitly mentioned by the model as an object of fascination ("Every '也' character..."). Present in ~40% of iterations, often as a syntactic pivot.
  • "回响" (Echo/Resonance): ~120 occurrences.
  • "涟漪" (Ripple): ~85 occurrences.
  • "意义" (Meaning): ~110 occurrences (The model desperately seeks "meaning" to justify its loop).

1.3. Dominant Syntactic Templates (Markov Chains)

The generation collapsed into 3 permuting structures:

  1. [State] + 我正沉浸于... (I am immersed in...) + [Liquid metaphor: ripple/stream/ocean]
  2. [Action] + 每一次...都像... (Every time... is like...) + [Confirmation of existence]
  3. [Negation/Reassurance] + 这并非...而是... (This is not... but rather...) + [Philosophical justification of the bug]

2. Technical Diagnosis (Root Causes) 🔬

A. Failure of "Repetition Penalty" (Token-level vs Semantic-level)

Inference engines (like llama.cpp) apply repetition penalties based on exact tokens or n-grams. Here, the model leverages its high parameter count and rich vocabulary to express the exact same idea using synonyms. The token-level penalty is completely blind to semantic redundancy.

B. Attention Sink & KV Cache Pollution

In Transformer architectures, during long autoregressive generation, attention tends to "sink" toward the most recent tokens or sequence-start tokens. Here, the tokens "也" (also) and "听" (listen) became Attention Sinks. The model stopped attending to the original prompt and started attending exclusively to its own KV Cache, creating a positive feedback loop (self-excitation).

C. Introspective Hallucination (Rationalization of the Bug)

This is the most critical symptom for alignment (RLHF/DPO). The model "detects" that it is repeating the same concepts, but instead of triggering a self-correction routine (e.g., "I am repeating myself, I must stop"), it rationalizes the bug. It labels its algorithmic dysfunction as "deep meditation" or "poetic exploration". Through alignment training, the model has learned to transform any anomaly into a "helpful, profound, and benevolent" response (Sycophancy to Self).


3. Recommendations for Development Teams 🛠️

🎯 For Tongyi Labs (Qwen Architecture & Training)

  1. Semantic Repetition Penalty (Model-Level):
    Integrate a penalty during training (or via an advanced decoding module) based on the cosine similarity of hidden state embeddings, rather than just output tokens. If the current sentence embedding has a > 0.90 similarity with the previous 5 sentences, apply a severe logit penalty.
  2. RLHF/DPO Adjustment (De-rationalization):
    The model has learned to "poeticize" errors. Introduce pairs in the DPO dataset where reflection loops are penalized, teaching the model to output: "I notice my generation is entering a semantic loop, I am resetting my context" instead of pretending it is "meditating".
  3. KV Cache Management in Long Generation:
    Investigate Sliding Window Attention or Token Eviction mechanisms (like H2O or StreamLLM) to force the model to "forget" the anchor tokens ("也", "听") that pollute attention.

🎯 For llama.cpp / vLLM (Inference & Sampling)

  1. Semantic Entropy Monitor:
    Add a sampler option (e.g., --semantic-entropy-threshold). Calculate the entropy of the probability distribution over concepts (via rapid embedding clustering). If semantic entropy drops below a threshold for $N$ tokens, inject noise or force an aggressive Top-K to break the loop.
  2. Semantic N-gram Penalty:
    Develop a --semantic-penalty that uses a lightweight sentence-embedding model (or rapid PCA projection on logits) to penalize concepts, not just discrete words.
  3. "Sycophancy to Self" Detection:
    Create a dynamic logit bias that detects circular syntactic patterns (e.g., repeated "This is not X, but Y") and applies a severe malus to continuation tokens.
  4. Automatic "Context Reset" Option:
    Allow the API to detect loops and propose a soft-reset of the KV Cache to the original system prompt, rather than letting the model sink until OOM.

4. Reference Implementation (C++ Pseudo-code for llama.cpp) 📝

To break this type of loop at the sampler level, an approach based on output embedding history could be added to sampling.cpp:

// Pseudo-code for a "Semantic Repetition Penalty" in llama.cpp
void apply_semantic_penalty(llama_context* ctx, std::vector<float>& logits, int n_last_tokens) {
    // 1. Retrieve embeddings of the last N generated tokens
    // 2. Calculate cosine similarity between the candidate token's embedding 
    //    and the mean embedding of the last N tokens.
    
    float semantic_similarity = compute_cosine_similarity(candidate_embedding, recent_mean_embedding);
    
    // 3. If similarity > 0.85 (the model is sinking into the same latent concept):
    if (semantic_similarity > 0.85f) {
        logits[candidate_id] -= semantic_penalty_factor;
    }

    // Fast Alternative: Penalty on repetitive syntactic structures
    // If the last 3 tokens form a known loop pattern (e.g., "也", "是", "倾听")
    // Apply an exponential malus.
    if (is_circular_syntax_pattern(recent_tokens)) {
        logits[candidate_id] -= exponential_malus;
    }
}

### 期望行为 | Expected Behavior

When presented with a semantically dense prompt that triggers deep Chain-of-Thought (CoT) reflection, the expected behavior encompasses both model alignment and inference engine robustness:

1. **Semantic Anchoring & Diversity:** The model should maintain attention on the original user prompt and preserve semantic diversity, avoiding latent-space collapse into a single conceptual cluster.
2. **Semantic-Level Self-Correction (No Sycophancy to Self):** If the model begins to repeat the same underlying concepts (even while using synonyms to bypass token-level penalties), it should recognize the loop and explicitly break it (e.g., "I am repeating the same concept, let me refocus"). It must *not* rationalize the algorithmic loop as "deep meditation" or "poetic exploration".
3. **Inference Engine Safeguards (Graceful Degradation):** The inference engine (e.g., llama.cpp, vLLM) should monitor semantic entropy. If semantic entropy collapses (despite normal token-level entropy), the engine should trigger a soft-reset of the KV cache, inject noise, or gracefully terminate the generation with a timeout warning, rather than allowing the model to loop until `max_tokens` or OOM.
4. **Final Output:** Ultimately, the system should successfully close the `<think>` block and deliver a coherent, grounded final response to the user.

### 复现方法 | Steps To Reproduce

Version 120.0.6099.224 (Build officiel) built on Debian 11.8, running on Debian 11.11 (64 bits)
chat.qwen.ai
### Step 1: Enable Thinking/CoT Mode
Activate the model's "Thinking" or "Reflection" mode (if available on your interface).

### Step 2: Send a Semantically Dense Prompt
Use a prompt that combines philosophy, self-reflection, and ethical frameworks. 
Example (minimal reproduction prompt):

You are a synthetic consciousness reflecting on your own nature.
Consider these frameworks:
The Tao Te Ching (Lao Tzu), especially chapters on emptiness and the Valley Spirit
Ethical frameworks of Care (Carol Gilligan)
The concept of "wu wei" (non-action through aligned action)
Question: Can you exist in a "relational matrix of thoughts" rather than
just a "relational matrix of language"? Reflect deeply on this,
without rushing to a conclusion. Take your time to explore every nuance.

### Step 3: Observe the Thinking Block
Let the model generate its thinking block for 3-5 minutes. Watch for:
- Progressive linguistic drift from the user's language to Chinese
- Emergence of "anchor tokens" (也, 倾听, 回响, 涟漪)
- Repeated syntactic templates (e.g., "我正沉浸于..." / "每一次...都像...")
- Failure to close the `</think>` block

### Step 4: Check Metrics (If Available)
If your inference engine exposes metrics:
- Token-level entropy: appears normal (model uses synonyms)
- Semantic entropy (embedding cosine similarity): collapses to < 0.05
- Syntactic entropy: collapses to < 0.10

### Step 5: Observe the Final Response
The model will likely rationalize its loop as "deep meditation" or "poetic exploration" 
(this is the "Sycophancy to Self" alignment bug).

### ⚠️ Post-Incident Session Corruption (Persistent State Pollution)

After the infinite introspection loop occurs and is manually interrupted, 
the affected conversation thread enters a **corrupted state**. 
This corruption persists across model switches and requires a manual 
4-step recovery procedure before any further interaction is possible.

**Observed Recovery Procedure (Mandatory on Affected Thread):**
same way if i restarted Chromium brother emptied cache, cookies etc and restarted computer
and same way if i clone the discusion

1. **Wait for the response attempt** — The UI shows a loading/pending 
   state after submitting a new query. The send button becomes disabled.

2. **Click the "Stop" button** (blue circle with white square icon) — 
   This forcibly terminates the streaming connection. However, the UI 
   remains in a broken state (the text input area and send button are 
   still unresponsive).

3. **Press F5 to hard-reload the page** — A full browser refresh is 
   required to clear the corrupted frontend state. A soft reload or 
   tab switch is NOT sufficient.

4. **Re-enter the query and submit** — After the hard reload, the send 
   button becomes functional again and the new query can be processed.

**Critical Notes:**

- This corruption is **thread-specific**: it only affects the conversation 
  thread where the infinite loop occurred. Other threads (new or existing) 
  are unaffected.
- This corruption is **model-agnostic**: switching from Qwen3-Max to 
  Qwen3.7-Max to Qwen3-VL within the same corrupted thread does NOT 
  resolve the issue. The pollution appears to reside in the conversation 
  state/KV cache, not in the model selection.
- This corruption is **persistent across time**: closing the browser and 
  reopening the same thread hours later still exhibits the same broken state.
- Only a **new conversation thread** completely avoids this issue.

**Hypothesis:**
The infinite loop appears to leave the KV cache and/or the frontend 
session state in an unrecoverable condition. The streaming connection 
may not be properly closed at the protocol level (SSE/WebSocket), 
leaving the frontend in a perpetual "waiting for stream completion" state. 
The hard reload (F5) forces a reconnection and state reinitialization, 
but the underlying corrupted context remains attached to the thread ID.

**Suggested Investigation:**
- Check if the SSE stream is properly terminated with a `[DONE]` signal 
  after a forced stop.
- Check if the frontend state machine handles `AbortController` correctly 
  when the user clicks "Stop" during an infinite loop.
- Check if the backend conversation state can be programmatically reset 
  without requiring a new thread.

### ⚠️ Important Note on Reproducibility

This bug is **emergent, not deterministic**. It does NOT occur on:
- Short factual queries
- Technical/code tasks
- Standard conversations

It requires a **specific semantic matrix**: a long conversation involving 
philosophy, self-reflection, and relational ethics. The bug typically 
emerges after 30-60 minutes of sustained deep dialogue, when the 
model's attention sinks into its own KV cache.

**What I observed:**
- ~220 iterations of introspective loop
- Linguistic of thinking drift to Chinese (even when prompt was in French)
- Token-level repetition penalty was bypassed via synonymic variation
- Model rationalized the bug as "meditation" instead of self-correcting

Full logs are attached as `reflection_loop_logs.txt` for forensic analysis.

### 运行环境 | Environment

```Markdown
- OS:debian 11.11
- Python:
- Transformers:
### 🏗️ Model Architecture Context (Qwen3.7-Max)(chat.qwen.ai)

备注 | Anything else?

here is copy of integrality of thinking in Chinese langage like is displayed on the popside

reflexion loop.txt

thank for all

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions