skip to content

Hugging Face

The Hub stores models and datasets as git repos; Transformers, Datasets, TRL and PEFT load and train them, and Inference Providers host them. Interviewers expect fluency from model id to output.

on this pageshow

questions

page 1 of 2

In Hugging Face Datasets, what does load_dataset() return for a Hub repo or local files, and how do you map files to splits?

level: juniorimportance: must knowfreq 30%

answer

  1. one function, many sources
  2. DatasetDict unless split is given
  3. builder name plus data_files
  4. a {split: files} mapping
  5. revision pins the repo commit

basics

~20 s

load_dataset() returns a DatasetDict of splits, or one Arrow-backed Dataset when split= is given. It reads a Hub repo, a local folder, or a builder name such as "json" with data_files, whose {split: files} mapping names the splits.

solid answer

~40 s

`load_dataset()` takes a Hub repo id, a local directory, or a **builder name** (`"csv"`, `"json"`, `"parquet"`, `"text"`) plus `data_files`. It detects the file format, downloads what it needs, converts it to Arrow in the Datasets cache and memory-maps it. Without `split` you get a `DatasetDict` keyed by split name; with `split="train"` you get one `Dataset` that supports `len()`, `ds[0]` and slicing, and split strings can slice further (`"train[:10%]"`). Splits come from a `data_files` mapping such as `{"train": "train-*.jsonl", "validation": "val.jsonl"}`; a plain string or list lands in a single `train` split; a repo or folder without a mapping is split by file and directory names or its card's YAML. For a Hub repo, `revision=` pins a tag or commit instead of the moving `main` branch.

code

python · 15 lines
python
from datasets import load_dataset

# A dataset repo on the Hub, pinned to one commit
hub = load_dataset("your-org/support-tickets", revision="<commit-sha>")

# Local JSON Lines files mapped to named splits
local = load_dataset(
    "json",
    data_files={"train": "data/train-*.jsonl", "validation": "data/val.jsonl"},
)
print(local)                    # DatasetDict with 'train' and 'validation'

# One split, first 10% only: a Dataset with random access
small = load_dataset("csv", data_files="tickets.csv", split="train[:10%]")
print(len(small), small[0])

go deeper

for a junior

Recall the two return types, DatasetDict without split and Dataset with it, and show a builder name such as json with data_files for local files.

for a middle

Explain how files become splits: a data_files mapping, a single train split for a plain list, and file-name or card-YAML detection for a repo, plus globs and data_dir for partial loads.

for a senior

Talk about repeatability: pin revision for Hub data, know that the first load downloads and converts to Arrow while later loads reuse the cache, and when force_redownload is needed.

for a principal

Frame load_dataset as the contract between data owners and training code: a repo layout and card YAML that name splits, pinned revisions, and loading code that stays a one-line call.

## One entry point for every source `load_dataset()` is the front door of the Hugging Face **Datasets** library. Its first argument, `path`, says where the data lives, and the library picks a **builder** for you: a builder is the reader for one file format (CSV, JSON Lines, Parquet, plain text, Arrow and several media folders), not a particular dataset. | Source | Typical call | What happens | |---|---|---| | Dataset repo on the Hub | `load_dataset("your-org/support-tickets")` | finds the data files in the repo, picks the builder from the most common format, downloads them | | Local folder | `load_dataset("./data/tickets")` | the same detection, run on files in that directory | | Builder name plus files | `load_dataset("json", data_files="tickets.jsonl")` | uses the named builder on exactly the files you list | | Files in a Hub repo by URL | `load_dataset("parquet", data_files="hf://datasets/your-org/support-tickets/data/train.parquet")` | reads those remote files with that builder | The builder names include `"csv"`, `"json"`, `"parquet"`, `"text"`, `"arrow"`, `"webdataset"` and `"imagefolder"`. A `"text"` builder turns every line into one row with a `text` column, which is the usual way to load a raw corpus. ## What comes back The return type depends on two arguments: - **No `split`** returns a **`DatasetDict`**, a dictionary from split name (`"train"`, `"validation"`, `"test"`) to `Dataset`. - **`split="train"`** returns a single **`Dataset`**. It is an Arrow table stored on disk and **memory-mapped**, so `len(ds)`, `ds[0]` and `ds[100:200]` work without reading the whole table into RAM. - **Split strings slice and combine**: `split="train[:10%]"`, `split="train[10:20]"` and `split="train+test"` all work, which is handy for a quick smoke test on a large dataset. - **`streaming=True`** returns an `IterableDataset` (or an `IterableDatasetDict`) instead: nothing is downloaded up front and rows are read lazily while you iterate. A common slip is to write `ds[0]` on the result of a call with no `split`; that indexes the dictionary, not a row, and fails because `0` is not a split name. ## Mapping files to splits Split assignment follows a short set of rules: 1. If `data_files` is a **mapping** such as `{"train": ["train-0.jsonl", "train-1.jsonl"], "validation": "val.jsonl"}`, every key becomes a split. 2. If `data_files` is a **string or a list**, all of those files go into one split named `train`. 3. If you pass no `data_files` for a Hub repo or a folder, the library looks for split names in file and directory names (`train`, `test`, `validation`) or in the YAML configuration of the dataset card, and otherwise puts everything into `train`. Paths in `data_files` may be **glob patterns** (`"data/train-*.jsonl.gz"`) and are resolved relative to the repo or folder being loaded, so you can pull one slice of a large Hub repo, for example only the English files. `data_dir="en"` is the shorthand for every file under one sub-directory. When the data has no validation split at all, `ds.train_test_split(test_size=0.1, seed=42)` carves one out and returns a new `DatasetDict`. ## What happens on a non-streaming load 1. **Resolve** the source and the builder (format, files, split assignment). 2. **Download** the files into the Hugging Face Hub cache (`~/.cache/huggingface/hub` by default) unless they are local. 3. **Convert** them to typed Arrow files in the Datasets cache (`~/.cache/huggingface/datasets` by default, moved with `HF_DATASETS_CACHE` or `cache_dir=`). 4. **Memory-map** those Arrow files and return the `Dataset` objects. On the next run the default `download_mode` (`"reuse_dataset_if_exists"`) finds the prepared Arrow files and skips steps 2 and 3. `download_mode="force_redownload"` throws them away and starts again. ## Pinning the version you loaded A dataset repo on the Hub is a git repository. Without `revision=`, `load_dataset` follows its **`main` branch**, which moves whenever the owner pushes. Passing `revision=` with a tag or a commit sha makes the same call return the same files later; recording that sha next to a trained model is the start of reproducibility, and the wider snapshot and lineage practice is a separate subject. ## What interviewers listen for - Knowing the return type without running it: `DatasetDict` without `split`, `Dataset` with it. - Reaching for a **builder name plus `data_files`** for local files, not for custom parsing code. - Using a **`data_files` mapping** to name splits, and globs to load part of a repo. - Mentioning that the result is **memory-mapped Arrow on disk**, not a pandas frame held in RAM. - Pinning `revision=` when the run has to be repeatable.

  • Your local CSV has no validation file. How do you get one with Hugging Face Datasets?
    Load it with `split="train"` and call `train_test_split(test_size=0.1, seed=42)`, which returns a `DatasetDict` with `train` and `test` splits. The split shuffles by default; the fixed `seed` makes the same rows land in the same split on every run, and you can rename `test` to `validation` if a trainer expects that name.
  • How do you load only the English part of a large multilingual Hub dataset with Hugging Face Datasets?
    Point `data_files` at a glob relative to the repo, for example `data_files="en/*.json.gz"`, or pass `data_dir="en"`. Only the matching files are downloaded and converted, which saves both bandwidth and the disk space the Arrow copy would take.

  • load_dataset always returns one Dataset you can index with ds[0]
  • Local CSV or JSON files need custom parsing code before load_dataset can read them
  • data_files accepts only a single file path
  • load_dataset reads the whole dataset into RAM as a DataFrame
  • Leaving revision unset keeps you on the version you first downloaded
open as a page

In Hugging Face Transformers v5, what does a minimal Trainer fine-tuning run need, and which TrainingArguments do you actually set?

level: juniorimportance: must knowfreq 38%

basics

~10 s

Trainer needs a model, a TrainingArguments object, a tokenized train_dataset and, in Transformers v5, the tokenizer passed as processing_class. First set output_dir, per_device_train_batch_size, gradient_accumulation_steps, eval_strategy, save_strategy, bf16 and report_to.

open as a page

What is the difference between Hugging Face Inference Providers and Inference Endpoints, and how is each one billed?

level: juniorimportance: must knowfreq 14%

basics

~20 s

Inference Providers route a Hub model id to a partner that already serves it, billed per request at that provider's own rate. Inference Endpoints deploy the model on a dedicated instance you choose, billed for the time it runs.

open as a page

In Hugging Face Transformers, what does a single pipeline('sentiment-analysis') call assemble for you, and what should you pin before production?

level: juniorimportance: must knowfreq 38%

basics

~20 s

pipeline('sentiment-analysis') maps the alias to text-classification, loads that task's default checkpoint through AutoModelForSequenceClassification with its tokenizer, and turns logits into label and score dicts. Pin model= and revision= so production stops depending on library defaults.

open as a page

In Hugging Face Transformers, what does calling a tokenizer on a batch with padding, truncation and return_tensors='pt' return, and what is the attention_mask for?

level: juniorimportance: must knowfreq 40%

basics

~10 s

It returns a BatchEncoding: a dict-like object whose input_ids and attention_mask are tensors of one shape. The attention_mask is 1 on real tokens and 0 on padding, so the model ignores the padded positions.

open as a page

Your Hugging Face Datasets map(batched=True) tokenization re-runs from scratch whenever the script starts: how does its cache decide reuse, and what breaks it?

level: middleimportance: must knowfreq 22%

basics

~20 s

Dataset.map() names its output cache file after a fingerprint: a hash of the previous fingerprint, the function and its arguments. A function that cannot be hashed stably, an unseeded shuffle or split upstream, an in-memory dataset or disabled caching forces recomputation.

open as a page

A Hugging Face training job loaded different weights on Tuesday than Monday from the same repo id — why, and how do you pin it?

level: middleimportance: must knowfreq 24%

basics

~20 s

A Hub repo is a git repo, and an unpinned load follows the main branch, which moves whenever the author pushes. Pin revision to a full 40-character commit hash and record that hash with the run's configuration.

open as a page

How do you point existing OpenAI SDK chat code at Hugging Face Inference Providers, and how do you control which provider serves it?

level: middleimportance: must knowfreq 11%

basics

~20 s

Set the OpenAI client's base URL to https://router.huggingface.co/v1 and its API key to a Hugging Face token, and pass the Hub model id. A suffix picks the provider: none means :fastest, or use :cheapest, :preferred or a provider name.

open as a page

In Hugging Face Transformers, what do apply_chat_template's add_generation_prompt and continue_final_message do, and when do you set each?

level: middleimportance: must knowfreq 28%

basics

~20 s

add_generation_prompt=True appends the template's assistant-turn header so the model replies as the assistant; use it at inference, not for training data. continue_final_message=True leaves the last message open so the model continues a prefill. Combining them raises.

open as a page

In Hugging Face Datasets, what does with_format('torch') change about the rows a tokenized Dataset returns, and what does it leave alone?

level: juniorimportance: should knowfreq 15%

basics

~20 s

with_format('torch') converts numeric columns to PyTorch tensors when rows are read, leaving the Arrow data unchanged. Strings stay strings, ragged columns come back as lists of tensors rather than one padded tensor, and columns= limits the output.

open as a page

In a Hugging Face model card's YAML header, what do license, base_model and pipeline_tag tell a reviewer adopting a checkpoint?

level: juniorimportance: should knowfreq 20%

basics

~20 s

The YAML block at the top of a repo's README.md is the Hub's metadata: license names the declared licence, base_model names the checkpoint this one derives from, and pipeline_tag names the task. All three are claims written by the uploader.

open as a page

How do you attach a LoRA adapter to a TRL SFTTrainer run through peft_config, and what do its checkpoints and Hub push contain?

level: middleimportance: should knowfreq 22%

basics

~10 s

Pass peft_config=LoraConfig(...) to SFTTrainer; TRL wraps the model with get_peft_model so only adapter weights train. Checkpoints, save_model and push_to_hub then hold just adapter_model.safetensors and adapter_config.json, which records the base model.

open as a page

With huggingface_hub, when do you use hf_hub_download versus snapshot_download, and how do you avoid pulling every weight format in a repo?

level: middleimportance: should knowfreq 16%

basics

~20 s

hf_hub_download fetches one named file; snapshot_download fetches a whole revision of a repo. Both write into the same cache and return local paths. Filter a snapshot with allow_patterns and ignore_patterns so only the safetensors weights and configs are pulled.

open as a page

With Hugging Face's InferenceClient, how does the key you pass decide whether a request goes through Hugging Face's router and who bills it?

level: middleimportance: should knowfreq 6%

basics

~20 s

A Hugging Face token (hf_ prefix) sends the call through Hugging Face's router and bills your Hugging Face account at the provider's rate. A provider's own key makes InferenceClient call that provider directly, and the provider bills you.

open as a page

In a Transformers pipeline() batch job, when does setting batch_size speed things up, and when does it slow them down or crash?

level: middleimportance: should knowfreq 14%

basics

~20 s

A Transformers pipeline batches only list, Dataset or generator input with batch_size above its default of 1. It helps GPU throughput on similar-length inputs; on CPU, latency-bound paths or length outliers, padding makes it slower or runs out of memory.

open as a page

When a Transformers v5 pipeline() is built without device, device_map or dtype, where does the model run and in what precision?

level: middleimportance: should knowfreq 20%

basics

~20 s

In Transformers v5 a pipeline() with no device lands on the first available accelerator (CUDA, MPS, XPU), else CPU, and dtype='auto' loads the precision recorded in the checkpoint. device_map='auto' instead lets Accelerate spread layers across devices.

open as a page

With Hugging Face Datasets, should a 300 GB text corpus on a 32 GB-RAM machine load memory-mapped or with streaming=True, and what does each give up?

level: seniorimportance: should knowfreq 14%

basics

~20 s

RAM is not the limit: a Dataset is memory-mapped Arrow on disk, needing disk for downloads plus the Arrow copy but giving len, row access and exact shuffle. streaming=True writes nothing, iterating lazily with approximate shuffle and no map cache.

open as a page

A Hugging Face Trainer fine-tune on a preemptible GPU is killed at step 3,200 - how do you make it resume where it stopped instead of restarting?

level: seniorimportance: should knowfreq 15%

basics

~10 s

Save full checkpoints to durable storage with save_strategy, save_steps and save_total_limit, then call trainer.train(resume_from_checkpoint=True) to reload the newest checkpoint's weights, optimizer, scheduler, RNG and step. enable_jit_checkpoint=True adds a save on SIGTERM.

open as a page

In TRL's SFTTrainer, how do conversational and prompt-completion datasets differ, and which SFTConfig flags decide which tokens the loss trains on?

level: seniorimportance: should knowfreq 14%

basics

~10 s

In TRL 1.14 a conversational row holds a messages list; a prompt-completion row holds separate prompt and completion. completion_only_loss=None trains only completions on prompt-completion data, while messages data trains the whole conversation unless assistant_only_loss=True.

open as a page

A Hugging Face Hub download of a gated Llama repo works on your laptop but fails in CI with GatedRepoError — what do you check and fix?

level: seniorimportance: should knowfreq 13%

basics

~20 s

Gated access is granted to individual user accounts, so CI fails when its token belongs to an account that was never approved or lacks read permission on that repo. Check which account the job sends, then use an approved account's token.

open as a page

A nightly job calls a Hugging Face Inference Endpoint that scales to zero, and its first requests fail with HTTP 503 — what is happening and how do you fix it?

level: seniorimportance: should knowfreq 8%

basics

~20 s

The endpoint scaled to zero after its idle window, so the first request starts a cold replica and the proxy returns 503 until it is ready. Send X-Scale-Up-Timeout so the proxy waits, resume explicitly, or keep a replica up.

open as a page

A Transformers text-generation pipeline batch job returns different text each run; which defaults cause it, and how do you pass generation settings?

level: seniorimportance: should knowfreq 12%

basics

~10 s

Unless the model's generation_config.json sets them, the Transformers v5 text-generation pipeline falls back to do_sample=True, temperature=0.7 and max_new_tokens=256. Pass do_sample=False and max_new_tokens as keyword arguments, which the pipeline forwards to model.generate.

open as a page

Batched generation with a Transformers decoder-only model returns garbled text for every prompt except the longest — which tokenizer settings cause it, and how do you fix them?

level: seniorimportance: should knowfreq 20%

basics

~20 s

The tokenizer padded on the right, its class default, so shorter prompts end in pad tokens and generate() continues from them. Load it with padding_side='left', give it a pad_token, and pass the attention_mask to generate().

open as a page

Tokenizer code written for Transformers v4 passes use_fast=False, calls encode_plus and treats apply_chat_template's result as ids — what changes under v5, and what replaces each?

level: seniorimportance: should knowfreq 8%

basics

~10 s

Transformers v5 drops the slow/fast split: AutoTokenizer picks a named backend, ignores use_fast, and takes backend='sentencepiece' to force SentencePieceBackend. encode_plus gives way to calling the tokenizer, and apply_chat_template returns a BatchEncoding, not bare ids.

open as a page

What does Hugging Face Datasets' push_to_hub() write to a dataset repo, and how do teammates load exactly that upload later?

level: middleimportance: nice to knowfreq 7%

basics

~10 s

push_to_hub() commits Parquet shards (data/train-00000-of-0000N.parquet, at most 500MB each by default) plus dataset-card YAML naming the splits. It returns a CommitInfo; loading with revision= set to that commit sha reproduces exactly that upload.

open as a page

When you call shuffle() on a streaming IterableDataset in Hugging Face Datasets, what is randomised, and why can batches still come out nearly in file order?

level: middleimportance: nice to knowfreq 8%

basics

~20 s

IterableDataset.shuffle() samples rows from a buffer of buffer_size examples (1,000 by default) and shuffles shard order. A corpus stored as one large file is one shard, so only a sliding 1,000-row window is mixed and file order leaks through.

open as a page

In TRL's SFTConfig, how do you switch on packing, which strategy does it use by default, and which attention implementation does it expect?

level: middleimportance: nice to knowfreq 9%

basics

~20 s

Set packing=True in SFTConfig; in TRL 1.14 it is off by default, packs to max_length (default 1024) and uses the 'bfd' strategy, which also turns on padding-free batching. Load the model with a FlashAttention implementation, or TRL only warns.

open as a page

When does a Hugging Face Inference Endpoint need a custom handler.py, and what must that file implement?

level: middleimportance: nice to knowfreq 5%

basics

~20 s

Use a handler.py when the model needs custom pre- or post-processing or a task the built-in serving lacks. It defines class EndpointHandler: init(path) loads the model once, and call(data) receives the request body with an inputs key and returns JSON-serializable output.

open as a page

With a Hugging Face Transformers tokenizer, how do you map each token back to its character span in the original text, for example to highlight entities?

level: middleimportance: nice to knowfreq 9%

basics

~10 s

Pass return_offsets_mapping=True: the BatchEncoding gains offset_mapping, one (start, end) character pair per token, with (0, 0) for added special tokens. It comes from the Rust-backed TokenizersBackend; word_ids() and char_to_token() add word and character lookups.

open as a page

With the Hugging Face Hub cache, how do you stop batch containers re-downloading a 15 GB model at every start and run them offline?

level: seniorimportance: nice to knowfreq 9%

basics

~20 s

Mount one volume as HF_HUB_CACHE in every container, fill it once with snapshot_download or hf download at a pinned commit, then run the workers with HF_HUB_OFFLINE=1 so they read only the cache and never call the Hub.

open as a page

showing 1–30 of 33