Skip to content

Studio: normalize OOXML namespaces without rewriting document text - #18012

Merged
danielhanchen merged 5 commits into
unslothai:mainfrom
DimitriChrysafis:fix/studio-strict-ooxml-namespaces
Nov 17, 2026
Merged

danielhanchen merged 5 commits into
unslothai:mainfrom
DimitriChrysafis:fix/studio-strict-ooxml-namespaces

Conversation

@DimitriChrysafis

@DimitriChrysafis DimitriChrysafis commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

RAG's XML reader changes document text when it contains a Strict OOXML namespace URL. A cell written with XlsxWriter as http://purl.oclc.org/ooxml/spreadsheetml/main is read back as http://schemas.openxmlformats.org/spreadsheetml/2006/main. The byte replacement runs on every XML archive part, so it also changes that text in an ODT paragraph.

The same normalization misses Strict workbooks and slides whose XML is UTF-16: the ASCII byte pattern is absent, the namespace lookups find nothing, and parsing returns no text. Transitional UTF-16 XML already works.

Normalize parsed element and attribute names using the existing Strict-to-Transitional map. Namespace events identify the actual URI after XML decoding, including character references. Cell text and attribute values stay as stored. Namespace events remain internal to the reader, preserving the existing stream units and element/depth limits.

Validation on macOS, Python 3.12.9:

  • Nine initial regression cases fail on main, with four passing encoding controls. Fifteen added cases now cover literal URLs, both UTF-16 byte orders, namespaced relationship attributes, escaped declarations, incremental feeds larger than 1 MiB, and upload-to-FTS search.
  • The Office parsing, document parsing, ingestion and retrieval suites pass: 369 passed, 1 skipped. The skip is the existing opt-in real-embedder test. Embeddings are stubbed in ingestion tests.
  • The standalone XlsxWriter-produced workbook now returns its exact original cell text.
  • Repository-pinned Ruff lint and formatting, duplicate-definition and import-hoist checks, and git diff --check pass.
python -m pytest studio/backend/tests/test_rag_office_formats.py \
  studio/backend/tests/test_rag_parsing.py studio/backend/tests/test_rag_ingestion.py \
  studio/backend/tests/test_rag_retrieval.py -q --tb=short --timeout=60 -n 4 --dist loadgroup

Parsed-name normalization adds CPU work. A local 25,000-row control adds about 7 ms for Transitional XML and 34 ms for Strict XML while keeping the output identical. No model download or GPU run.

Follow-up: entity guard in UTF-16 parts

The <!ENTITY refusal had the same encoding gap as the old namespace rewrite: it searched for ASCII bytes before decoding, so a UTF-16 archive part declaring an entity was parsed and its entity text indexed. The guard now also matches the UTF-16 LE and BE forms, and test_xml_entities_are_refused covers all three encodings (the two UTF-16 cases fail without the change).

Validation on the final head: RAG office, parsing, ingestion and retrieval suites 371 passed, 1 skipped; the office formats test file passes on ubuntu-latest, windows-latest and macos-15.

danielhanchen added a commit that referenced this pull request Nov 16, 2026
…t (fsdp2 red since accelerate 1.16) (#18025)

## Summary

`Repo tests (CPU, fsdp2)` fails on main since accelerate 1.16.0 reached
PyPI today (seen on #18012 and #18017, both on `20d05c20`):

```
tests/test_fsdp2_wrap_gloo.py::test_activation_checkpointed_blocks_keep_the_last_block_gathered
    assert r["blocks"] == N_LAYERS
E   assert 0 == 3
```

## Cause

With activation checkpointing on, accelerate wraps each decoder block in
a `CheckpointWrapper` and then calls `fully_shard`. Up to 1.15 it
sharded the block inside the wrapper. 1.16 shards the wrapper instead
and skips the block inside ("Shard the wrapper to keep FSDP hooks
outside compiled code", `fsdp2_prepare_model`).

`after_fsdp2_wrap` finds the blocks with `_decoder_blocks(model)` and
keeps the ones that are `FSDPModule`s. On 1.16 the block is no longer
one, and the wrapper's class is not a decoder layer class, so it found
no blocks. The fused LoRA gate does not depend on that list (it checks
that each fused layer's owning unit sits in the layer list, which the
wrapper does). What was lost is the per-block tuning: the last block was
no longer kept gathered after forward
(`set_reshard_after_forward(False)`), so with activation checkpointing
every step gathered it again at the start of backward.

The test is right. accelerate's change is reasonable, and Unsloth's
block lookup assumed the old layout.

## Change

- `unsloth/models/fsdp2.py`: new `_block_units`, which returns each
decoder block's FSDP unit: the block itself, or the checkpoint wrapper
around it when that wrapper is the `FSDPModule`
(`_checkpoint_wrapped_module is block`). `after_fsdp2_wrap` uses it for
the block list, the block count and the last-block reshard setting.
- New `tests/test_fsdp2_block_units.py` (in the `fsdp2` shard): both
layouts with stand-in classes, so the 1.16 layout is checked whatever
accelerate CI installs, plus a case where neither the block nor the
wrapper is sharded.

## Verification

- `tests/test_fsdp2_wrap_gloo.py` on accelerate 1.16.0: the checkpointed
test fails on main (`0 == 3`) and all 5 pass with this change. On
accelerate 1.15.0: 5 passed with this change.
- `tests/test_fsdp2_block_units.py`: 3 passed. Without the wrapper
lookup, the wrapper case fails.
- The whole `fsdp2` shard file set on accelerate 1.16.0: 247 passed, 16
skipped.
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-17T07:01:05.577346Z
We couldn’t complete this review. Comment @codex review to try again.
18bd92a Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: edad6ac885

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen marked this pull request as ready for review November 17, 2026 07:00
@danielhanchen
danielhanchen self-requested a review as a code owner November 17, 2026 07:00
@danielhanchen
danielhanchen merged commit b24e3c7 into unslothai:main Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants