Skip to content

feat(hffs): read file ranges and streams from Xet storage - #5165

Open
XciD wants to merge 8 commits into
mainfrom
feat/hffs-xet-range-reads
Open

XciD wants to merge 8 commits into
mainfrom
feat/hffs-xet-range-reads

Conversation

@XciD

@XciD XciD commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

HfFileSystem.get_file already downloads Xet files with hf_xet (#4990), but range reads (HfFileSystemFile._fetch_range) and streaming reads (HfFileSystemStreamFile) still send a GET to the resolve URL. For a Xet file, the Hub redirects that GET to the Xet bridge, which reconstructs the bytes server side. This is the path datasets streaming and pyarrow/parquet range reads take today.

This PR reads these files from Xet storage directly when the file has a xet_hash:

  • HfFileSystem._read_xet streams the bytes [start, end) of a file with hf_xet download_stream.
  • Files of the same repo revision share one download stream group, so they share its connection pool (each group builds its own HTTP clients). The group is rebuilt when get_xet_session() returns a new session, after abort_xet_session or in a forked process. A KeyboardInterrupt during a read aborts the session, like xet_get.
  • HfFileSystemFile._fetch_range joins the stream for the requested range.
  • HfFileSystemStreamFile streams from self.loc, so the existing retry (reopen at the current position) works the same way. It needs the file hash and size, so it now calls info() (often served from the dircache) when hf_xet is available. When the read position is at the end of the file, it returns b"" without a request, like the HTTP 416 case.
  • xet_download_group_kwargs in utils/_xet.py holds the connection arguments (endpoint, cached token, refresh route, headers without auth), now shared by xet_get and the stream group.

Files without a xet_hash keep the HTTP path. hf_xet>=1.6.0 is already required, and new_download_stream_group / download_stream exist in 1.6.0.

The two stream tests that checked f.response now check the stream iterator, so they cover both paths.


Note

Medium Risk
Changes how datasets streaming and parquet-style range reads fetch Xet files, with new shared stream-group caching and fork/session edge cases; HTTP fallback remains for files without xet_hash.

Overview
HfFileSystem now reads Xet-backed files through hf_xet for range reads and streaming, instead of HTTP GETs to the Hub resolve URL (which previously hit the Xet bridge server-side).

When a file has an xet_hash, HfFileSystem._read_xet streams [start, end) via download_stream. Buffered opens use this in _fetch_range; stream opens (block_size=0) wire _open_connection to the same path, with existing retry behavior keyed off _stream_iterator rather than the HTTP response. Stream groups are cached per process and repo revision (_xet_stream_groups), rebuilt on session/token change or after abort_xet_session on KeyboardInterrupt; the cache key includes PID for fork safety.

xet_download_group_kwargs in utils/_xet.py centralizes Xet connection/token kwargs (cached per revision); xet_get now uses it instead of inlining refresh_xet_connection_info and header stripping. Non-Xet files keep the HTTP path unchanged.

Tests assert Xet is used for range/stream reads without HTTP mocks, and stream retry tests target _stream_iterator.

Reviewed by Cursor Bugbot for commit eb02168. Bugbot is set up for automated code reviews on this repo. Configure here.

@XciD
XciD requested review from Wauplin and hanouticelina October 14, 2026 16:20
@XciD
XciD marked this pull request as ready for review October 14, 2026 16:21
@bot-ci-comment

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@cursor cursor 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.

Stale Bugbot comment from a previous run.

Comment thread src/huggingface_hub/hf_file_system.py Resolved
Comment thread src/huggingface_hub/hf_file_system.py Resolved

@cursor cursor 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.

Stale Bugbot comment from a previous run.

Comment thread src/huggingface_hub/hf_file_system.py Resolved

@cursor cursor 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.

Stale Bugbot comment from a previous run.

Comment thread src/huggingface_hub/hf_file_system.py Resolved

@cursor cursor 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.

Stale Bugbot comment from a previous run.

Comment thread src/huggingface_hub/hf_file_system.py Resolved
Comment thread src/huggingface_hub/hf_file_system.py Resolved

@cursor cursor 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.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 6715c4a. Configure here.

Comment thread src/huggingface_hub/hf_file_system.py Resolved

@hanouticelina hanouticelina left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

mostly looks good to me! i asked an agent (Opus 5.5) to run some benchmarks and here's what it found:

Streaming Xet files with block_size=0 is much slower to start: the first bytes come back after 9–27s with 0.4–2GB peak memory, vs under 1s and 55MB on main. datasets streams WebDataset this way (iter_archive opens each tar with block_size=0 and reads it with tarfile "r|*"), so every shard would wait that long before its first sample. Reading a whole small file is slightly slower too (1.8s vs 1.6s for 21MB). hf_xet 1.7.0 behaves the same, and lowering HF_XET_RECONSTRUCTION_MIN_RECONSTRUCTION_FETCH_SIZE from 256MB to 16mb only brings the tar case down to 5–6s (the 2GB parquet still takes 15–16s).

block_size=0 read main PR, hf_xet 1.6.0 PR, hf_xet 1.7.0
First sample of a 488MB WebDataset tar 0.88–0.93s, 56MB 16.5–25.7s, 579–705MB 17.0–18.3s, 652–693MB
First 4 bytes of a 2.15GB parquet 0.53–0.75s, 55MB 9.2–26.7s, 365–2043MB 12.9–25.4s, 419–1944MB
Whole 21MB parquet 1.56–1.70s, 99MB 1.78–1.84s, 130MB 1.78–1.90s, 131MB

Time and peak RSS, min–max over 3 runs, each in a fresh process. PR head eb02168a vs main 50f5b91f, Python 3.13, Apple M3 Max.

with these numbers, maybe we should keep streaming on HTTP for now and ship only the range reads, and switch once download_stream returns the first bytes quickly? wdyt?
also, since the dataset viewer already runs the v1.33 backport in prod, have you seen any impact there?

@lhoestq

lhoestq commented Oct 19, 2026

Copy link
Copy Markdown
Member

I can't see a difference in the viewer throughput (jobs/sec) so it must be negligible in the back end side

and yea +1 on streaming, it would be cool to improve it before replacing http

@davanstrien

Copy link
Copy Markdown
Member

Didn't look closely at the implementation but ran into this while benchmarking Zephon, DatologyAI's training data loader, on Hub datasets.

Currently at startup, every rank reads every Parquet footer to get per-file row counts. Each footer is a range GET on resolve/, so each one counts against the per-user resolver limit. For HuggingFaceM4/FineVision DoclingMatix (1,922 shards):

resolve/ requests Time
1 process 1,926 30 s
8 ranks on one node 15,408 48 s

IIUC, that's more than the Free (5,000) and PRO (12,000) limits per 5-minute window which may cause issues loading data early on.

With this PR, the same footer reads through HfFileSystem would be a directory listing plus a Xet token per revision, then range reads from Xet storage, with no resolve/ requests at all. Zephon uses its own HTTP client today, but this would give them (and anything else that reads footers through HfFileSystem) a straightforward way off the resolver limit. I'm opening an issue on Zephon that gives some other suggestions for using the Hub more efficiently but this would also be helpful for that (and possibly for Polars etc too but didn't look at that closely yet)

@lhoestq

lhoestq commented Oct 26, 2026

Copy link
Copy Markdown
Member

a simple fix for them would be to get the metadata once per node instead of per rank, i.e. get them from the main process instead of from every dataloader process

@XciD

XciD commented Oct 26, 2026

Copy link
Copy Markdown
Member Author

I've opened https://github.com/huggingface/xet-core/pull/1061/changes, this gives better perf for streaming

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.

4 participants