Skip to content

feat: add EPUB and optional MOBI dataset loading - #14453

Merged
danielhanchen merged 8 commits into
unslothai:mainfrom
HughScott2002:feat/ebook-datasets
Oct 22, 2026
Merged

danielhanchen merged 8 commits into
unslothai:mainfrom
HughScott2002:feat/ebook-datasets

Conversation

@HughScott2002

Copy link
Copy Markdown
Contributor

Summary

Add load_ebook_dataset: EPUB/MOBI → cleaned text → overlapping token chunks → Hugging Face Dataset.

Preserves reading order and paragraphs, supports optional metadata/chapter markers, and shares one reader mapping for extensions. Requires a fast tokenizer; MOBI uses optional Calibre.

Closes #14377.

Evidence

  • 110 targeted tests passed, including the real Calibre round trip and audit regressions.
  • 39 ebook tests passed in the installed training environment; 1,000 Unicode/whitespace probes passed.
  • Ruff and whitespace checks passed. Default-suite collection hits the same three missing-PyTorch errors on clean upstream main.

Merge Danger

Reversible, scoped to ebook preprocessing and its public exports. Model training was not run.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@danielhanchen

Copy link
Copy Markdown
Member

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T07:29:48.762040Z 6ac4369 Manual request
ℹ️ 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.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e18c3c0ff6

ℹ️ 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".

Comment on lines +195 to +196
if item.get("media-type") not in {"application/xhtml+xml", "text/html"}:
continue

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Follow XHTML fallbacks for foreign spine items

For an EPUB whose spine references a foreign content item (for example SVG) with a manifest fallback to XHTML, this branch skips the item without traversing the fallback. Those EPUBs consequently lose the chapter—and an otherwise text-bearing book is reported as unreadable—even though a supported XHTML representation is present, so resolve the fallback chain before applying this filter.

Useful? React with 👍 / 👎.

@danielhanchen danielhanchen Oct 21, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this: SVG spine items with an XHTML fallback are rare in real books. A book made only of them already fails with a clear "contains no readable text" error naming the file.

shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 21, 2026
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 21, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 21, 2026
@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 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cbe1cb7123

ℹ️ 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".

text = _html_text(read(name))
if text:
if chapter_markers:
text = "<|chapter_start|>\n" + text + "\n<|chapter_end|>"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep chapter markers intact across chunks

With chapter_markers=True, overlap=0, and a tokenizer that represents these literals with multiple tokens (the normal case unless they were registered as added tokens), _chunk_text can place a window boundary inside a marker; a byte tokenizer at chunk_size=8, for example, emits <|chapte, r_start|, and > in separate dataset rows. Trainers treat rows as separate examples or insert packing separators, so no complete delimiter survives; make marker spans indivisible and reject budgets that cannot hold one.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this: chapters are joined with a blank line, which the chunker prefers as a break, so at real chunk sizes (thousands of tokens) a window boundary inside a marker is very unlikely; chunk_size=8 is not a realistic setting.

Comment on lines +300 to +301
while char_start < len(text) and text[char_start].isspace():
char_start += 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve preformatted indentation at chunk starts

With overlap=0, whenever a <pre> block starts a chapter or crosses a token window, this loop advances char_start over the block's newline and indentation before slicing; <pre>first\n second</pre> with one-token chunks therefore emits a second row of second, not second. The new placeholder restoration only protects _html_text, while the end-to-end load_ebook_dataset path still changes code or poetry semantics here and at the later .strip(), so retain source indentation at continuation boundaries or carry preformatted spans into the chunker.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this: it only drops the indentation of the first line of a chunk that starts mid code block, which is a minor effect on training text, so I am leaving it.

@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 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cbe1cb7123

ℹ️ 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".

Comment on lines +87 to +90
def handle_starttag(self, tag, attrs):
if tag == "body":
self.handle_endtag("head")
attrs = dict(attrs)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Normalize namespace-prefixed XHTML tags

When a valid EPUB serializes the XHTML namespace with a prefix (for example, <x:p>), HTMLParser passes x:p here, so none of the body, ignored-tag, or block-tag comparisons match. A prefixed chapter containing a head and two paragraphs is consequently emitted like ClutterCSSFirstSecond, silently adding head contents and joining words; normalize qualified tag names to their local names before dispatching them.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this: EPUB chapters from real publishers and Calibre use the default XHTML namespace without a prefix, so prefixed tags are too rare to handle here.

columns.update({key: [] for key in ("source", "chunk_id", "title", "author", "genre")})
for book in files:
text, metadata = _read_book(book, chapter_markers)
if not text:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject whitespace-only books after pre restoration

When an otherwise empty chapter contains a whitespace-only <pre>, the verbatim restoration makes _html_text return a truthy whitespace string, so this check accepts it. With byte-level tokenizers that expose whitespace offsets, _chunk_text then yields no rows and the API returns an empty Dataset instead of the documented no-readable-text error (other tokenizers fail with an unrelated no-tokens error); check for non-whitespace content here.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this: it needs a book whose only content is a whitespace-only

, which does not occur in real ebooks.

Comment on lines +166 to +169
return re.sub(
"\ue000(\\d+)\ue001",
lambda m: unicodedata.normalize("NFC", parser.preformatted[int(m[1])]),
text,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Prevent private-use text from matching pre placeholders

When XHTML prose legitimately contains U+E000, digits, and U+E001 in sequence, this global substitution mistakes the author's text for an internal <pre> placeholder. With no corresponding pre block the valid book is rejected, while with one the sequence is silently replaced by unrelated preformatted text; use placeholders that are escaped or guaranteed not to occur in the input.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this: prose would need the exact private-use sequence U+E000, digits, U+E001, which real books do not contain.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: cbe1cb7123

ℹ️ 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".

@HughScott2002
HughScott2002 marked this pull request as ready for review October 21, 2026 15:11
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 22, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 6ac43695a5

ℹ️ 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 merged commit 8a7d528 into unslothai:main Oct 22, 2026
danielhanchen added a commit that referenced this pull request Oct 27, 2026
…processor (#15501)

On a host without an accelerator, `import unsloth` hands off to the CPU
backend (`unsloth/_cpu_init.py`, #14717). The GPU backend exports the
dataprep helpers from `unsloth` (`_gpu_init.py`), but the CPU backend
did not. So the documented `from unsloth import load_ebook_dataset`
(#14453) raises `ImportError: cannot import name 'load_ebook_dataset'`
on CPU-only hosts, and so do `RawTextDataLoader` and `TextPreprocessor`.

The CPU backend now exports all three. It loads `dataprep/raw_text.py`
and `dataprep/ebooks.py` by file, the way the MLX backend already does,
because `dataprep/__init__` also imports `synthetic.py`, which needs a
GPU device type. Neither file has relative imports.

`test_cpu_backend_init.py::test_no_accelerator_loads_the_cpu_backend`
now also checks the three names in its no-accelerator subprocess probe.
The GPU-only module list it asserts against is unchanged.

| | main | this PR |
|---|---|---|
| `test_cpu_backend_init.py` (CUDA hidden) | 1 failed, 6 passed, 1
skipped | 7 passed, 1 skipped |
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.

[Feature]Native Support for EPUB and MOBI File Formats in Unsloth

2 participants