> ## Documentation Index
> Fetch the complete documentation index at: https://doc.blueapi.ir/llms.txt
> Use this file to discover all available pages before exploring further.

# Merge Validation: Post-Merge Checks and Outcomes

> Post-merge validation specification for EMEP. Covers NaN/Inf scanning, dtype invariants, norm envelopes, tokenizer round-trip, and small-batch forward pass with PASS, FAIL, and QUARANTINE outcomes.

Merge validation is the gate between the MergeEngine and candidate registration. This page specifies every post-merge check, the decision flow, and the three possible outcomes: PASS, FAIL, and QUARANTINE.

## Validation Pipeline

```mermaid theme={null}
flowchart TD
    START([START]) --> INPUT[Input: merged state_dict, tokenizer, config]
    INPUT --> NAN[NaN/Inf Scan]
    NAN -->|found| FAIL_NAN[FAIL: NaN or Inf detected]
    NAN -->|clean| DTYPE[Dtype Invariants]
    DTYPE -->|violation| FAIL_DTYPE[FAIL: dtype mismatch]
    DTYPE -->|pass| NORM[Norm Envelopes]
    NORM -->|out of range| QUARANTINE_NORM[QUARANTINE: norm anomaly]
    NORM -->|pass| LAYER[Layer-wise Sanity]
    LAYER -->|mismatch| QUARANTINE_LAYER[QUARANTINE: layer anomaly]
    LAYER -->|pass| TOKEN[Tokenizer Round-trip]
    TOKEN -->|fail| QUARANTINE_TOKEN[QUARANTINE: tokenizer drift]
    TOKEN -->|pass| FORWARD[Small-batch Forward Pass]
    FORWARD -->|crash| FAIL_FORWARD[FAIL: forward pass error]
    FORWARD -->|NaN/Inf in output| FAIL_OUTPUT[FAIL: output instability]
    FORWARD -->|pass| PASS[PASS]
    PASS --> END([END])
    FAIL_NAN --> END
    FAIL_DTYPE --> END
    FAIL_FORWARD --> END
    FAIL_OUTPUT --> END
    QUARANTINE_NORM --> END
    QUARANTINE_LAYER --> END
    QUARANTINE_TOKEN --> END
```

## Validation Checks

### NaN/Inf Scan

Iterate over every parameter in the merged state\_dict. Detect NaN or Inf values. This is a hard fail: any NaN or Inf produces FAIL immediately.

| Threshold      | Outcome  |
| -------------- | -------- |
| Any NaN or Inf | FAIL     |
| All finite     | Continue |

### Dtype Invariants

Verify that every parameter has the dtype declared in the merge config. Mixed dtypes are permitted only if the strategy explicitly declares them.

| Condition                     | Outcome  |
| ----------------------------- | -------- |
| Unexpected mixed dtype        | FAIL     |
| Expected mixed dtype          | Continue |
| Uniform dtype matching config | Continue |

### Norm Envelopes

Compare the L2 norm of each merged layer to the L2 norms of the source models. Flag outliers.

| Condition                                                  | Outcome    |
| ---------------------------------------------------------- | ---------- |
| Norm outside \[min\_source - margin, max\_source + margin] | QUARANTINE |
| Norm within envelope                                       | Continue   |

Margin is configurable, default 20%.

### Layer-wise Sanity

Verify that layer shapes match the expected architecture. Check that attention Q/K/V/O projections, MLP up/gate/down projections, and layernorm parameters all have the expected dimensions.

| Condition          | Outcome    |
| ------------------ | ---------- |
| Shape mismatch     | QUARANTINE |
| All shapes correct | Continue   |

### Tokenizer Round-trip

Encode and decode a fixed set of test strings. Verify that the output matches the input.

| Condition         | Outcome    |
| ----------------- | ---------- |
| Round-trip fails  | QUARANTINE |
| Round-trip passes | Continue   |

### Small-batch Forward Pass

Run a small batch of synthetic input IDs through the merged model. Verify that the forward pass completes and produces finite logits.

| Condition          | Outcome  |
| ------------------ | -------- |
| Crash or exception | FAIL     |
| NaN/Inf in logits  | FAIL     |
| Finite logits      | Continue |

## Outcome Definitions

| Outcome    | Meaning                                     | Next Action                                  |
| ---------- | ------------------------------------------- | -------------------------------------------- |
| PASS       | All checks passed. Candidate is valid.      | Register as CANDIDATE                        |
| FAIL       | Hard failure. Candidate is invalid.         | Discard, log failure, notify experiment      |
| QUARANTINE | Soft failure. Candidate may be recoverable. | Hold for manual review, do not auto-register |

## Cross-Links

* [Merge Engine](/merge/merge-engine) for the pipeline that feeds into validation.
* [Merge Strategies](/merge/merge-strategies) for what strategies produce candidates.
* [Tensor Operations](/merge/tensor-operations) for the ops that may introduce instability.
* [Numerical Testing](/testing/numerical-testing) for extended numerical validation.
