Repository navigation
Hugging Face Compatibility
Full signatures and usage: HF dataset compatibility · HF Hub compatibility.
Use from datashoal import hf_compat to call the HF-style operations needed for calibration. Direct imports of DataShoal leave other packages alone.
hf_compat.load_dataset(path, name=None, *, split=None, streaming=False, data_files=None, revision="main", local_only=True, token=None, readers=None)
- For explicit shards, use a format such as
"json","parquet","arrow","csv"or"text"and supplydata_filesas a path, ordered sequence or split-to-paths mapping. A single file path also works aspath. - For a local directory, supply
splitand optionally a config name. DataShoal discovers sorted split shards or follows the file sequence from a saved split’sstate.json. - For a Hub dataset, pass a repository ID, config and split. Set
local_only=Falsefor remote access. Explicit repository-relativedata_filesare resolved at one commit. -
split="train[:128]"selects an integer prefix."train[:10%]"materializes the split to compute its length, even when requesting a stream. - A selected split or single file returns a
DatasetorIterableDataset. A multi-split load without a selected split returnsDatasetDictorIterableDatasetDict. Both mappings implement split-widemapandfilter.
from datashoal import hf_compat
ds = hf_compat.Dataset.from_dict({"text": ["alpha", "beta"]})
mapped = ds.map(lambda r: {"length": len(r["text"])})
assert mapped[0] == {"text": "alpha", "length": 5}
assert mapped.select([1])["length"] == [4]Dataset.from_list(rows) and Dataset.from_dict(columns) own copies of their input. String indexing returns a column list, integer indexing a row dictionary, and slicing a dictionary of column lists. Read column_names to discover fields. select(indices), shuffle(seed=...), filter(fn) and map(fn) return materialized datasets.
Compatibility map merges the callback output into the existing row. It supports remove_columns, fn_kwargs, and batched=True with batch_size. Batched callbacks receive a mapping of column lists and must yield equal-length output columns after merging. Materialized transforms run in one process and in memory. Streaming transforms are lazy. Streaming take(n) returns another iterable dataset; use list(stream.take(n)) for a list.
hf_hub_download(repo_id, filename, ...) and snapshot_download(repo_id, ...) return string paths. Both accept repo_type, revision, token and local_files_only=True. upload_file(path_or_fileobj=..., path_in_repo=..., repo_id=..., ...) accepts a local file path and returns the commit ID.
For an application that imports the upstream packages by name, install just the requested shims before importing the consumer:
from datashoal import hf_compat
hf_compat.install("datasets", "huggingface_hub")
from datasets import Dataset, load_dataset
from huggingface_hub import hf_hub_downloadThe shim exposes the classes and functions listed on this page. Consumers using additional upstream submodules, formatting modes, Hub APIs or transform caching need the corresponding upstream packages. Installing over an already imported upstream module raises RuntimeError. Start a fresh process and install the shim before the first upstream import.
hf_compat.safe_open(filename, framework="pt", device="cpu") follows the safetensors.safe_open spelling for code that reads a local shard by name. It requires PyTorch; importing hf_compat does not import it. No safetensors import shim is installed, so call it through hf_compat. The handle is a context manager with keys(), metadata(), get_tensor(name) and get_slice(name).
import torch
from datashoal import SafeTensors as st, hf_compat
st.save("model.safetensors", {"scale": torch.tensor([[1.0, 2.0], [3.0, 4.0]])})
with hf_compat.safe_open("model.safetensors", framework="pt") as handle:
assert handle.keys() == ["scale"]
assert handle.get_slice("scale")[0:1].tolist() == [[1.0, 2.0]]Each get_tensor maps only that tensor's bytes. get_slice(name)[start:stop] reads leading rows; other indexing materializes the tensor first. Only framework="pt" is supported. Indexed PyTorch reads
Get started
- Installation and quickstart
- Python API reference
- Native templates
- Native extension ABI
- Loading reference
Prepare calibration data
Manage artifacts
Operate and contribute