Hugging Face Transformers Interview Questions Hindi mein

Hugging Face Transformers कोर्स के 33 सवाल, हर सवाल के सही जवाब और explanation के साथ। जवाब देखने से पहले ख़ुद सोचें।

Mental Model and Setup

1. Which Hugging Face library is mainly responsible for loading and processing datasets efficiently?

  1. transformers
  2. accelerate
  3. datasets
  4. peft
जवाब देखें

जवाब: datasets

`datasets` loads data from the Hub or local files into Arrow-backed tables and provides `map`, `filter` and splits. transformers runs models, accelerate handles devices and peft handles adapter fine-tuning.

पाठ: What Hugging Face Actually Is

2. Why does a decoder-only model use a causal mask in self-attention?

  1. So that each token can only attend to itself and earlier tokens while generating
  2. To make attention cost linear in sequence length
  3. To remove positional information from the embeddings
  4. To let the model read the whole input bidirectionally
जवाब देखें

जवाब: So that each token can only attend to itself and earlier tokens while generating

Generation predicts the next token, so a position must not peek at future tokens. The causal mask hides them; it does not change the quadratic cost.

पाठ: The Transformer in Ten Minutes

3. You run the same `from_pretrained("some-model")` line twice on one machine. Why is the second run much faster?

  1. The second run downloads a smaller version of the weights
  2. transformers compiles the model to C++ after the first run
  3. The Hub caches the model in your browser
  4. The files are reused from the local Hugging Face cache
जवाब देखें

जवाब: The files are reused from the local Hugging Face cache

Downloaded files are stored in the local cache (by default under ~/.cache/huggingface, or HF_HOME), so later loads read from disk.

पाठ: Setting Up Your Environment

Pipelines: Results in Minutes

4. In a text-classification pipeline, which stage converts the model's logits into a label and a score?

  1. Preprocessing
  2. Postprocessing
  3. Tokenization
  4. The forward pass
जवाब देखें

जवाब: Postprocessing

The model returns logits. Postprocessing applies softmax and maps the highest index to a label name from the model config.

पाठ: Your First Pipeline

5. You must sort support tickets into labels that change every month and you have no labelled data. Which pipeline fits best?

  1. text-generation
  2. token-classification
  3. summarization
  4. zero-shot-classification
जवाब देखें

जवाब: zero-shot-classification

Zero-shot classification accepts candidate labels at call time and needs no task-specific training data.

पाठ: A Tour of Pipeline Tasks

6. What does `device_map="auto"` do when loading a large model?

  1. Places model layers across the available GPUs and CPU memory automatically
  2. Converts the model to 4-bit precision
  3. Downloads the smallest variant of the model
  4. Runs the model on the Hub's servers instead of locally
जवाब देखें

जवाब: Places model layers across the available GPUs and CPU memory automatically

device_map="auto" uses accelerate to place layers on available devices. Quantisation and remote inference are separate features.

पाठ: Devices, Batching and Precision in Pipelines

Tokenizers: Text to Numbers

7. What does a byte-pair encoding (BPE) tokenizer do repeatedly while it is being trained?

  1. Removes the least frequent words from the corpus
  2. Splits every word into single characters at inference time only
  3. Merges the most frequent adjacent pair of symbols into a new token
  4. Assigns random ids to every word in the dictionary
जवाब देखें

जवाब: Merges the most frequent adjacent pair of symbols into a new token

BPE training starts from small units and keeps merging the most frequent adjacent pair until the vocabulary reaches its target size.

पाठ: Why Models Read Subwords

8. In a BatchEncoding, what is the purpose of `attention_mask`?

  1. It stores the attention weights computed by the model
  2. It marks which positions are real tokens (1) and which are padding (0)
  3. It lists the special tokens used by the tokenizer
  4. It masks random tokens for masked-language-model training
जवाब देखें

जवाब: It marks which positions are real tokens (1) and which are padding (0)

attention_mask is an input, not an output: 1 means attend to this position, 0 means it is padding and should be ignored.

पाठ: AutoTokenizer: Encode and Decode

9. You tokenize a batch with `padding=True`. What length does each row end up with?

  1. The model's maximum context length
  2. A fixed 512 tokens
  3. The length of the shortest sequence in the batch
  4. The length of the longest sequence in the batch
जवाब देखें

जवाब: The length of the longest sequence in the batch

padding=True means padding="longest": rows are padded to the longest sequence in that batch. Use padding="max_length" for a fixed size.

पाठ: Padding, Truncation and Batches

Models, Auto Classes and Outputs

10. You want a model that outputs one score per class for a whole sentence. Which class do you load?

  1. AutoModelForSequenceClassification
  2. AutoModel
  3. AutoModelForTokenClassification
  4. AutoTokenizer
जवाब देखें

जवाब: AutoModelForSequenceClassification

Sequence classification puts a head on the pooled sentence representation. Token classification produces one prediction per token, and AutoModel has no head at all.

पाठ: Auto Classes, Configs and Heads

11. Why wrap inference in `torch.inference_mode()` or `torch.no_grad()`?

  1. It turns logits into probabilities
  2. It moves the model to the GPU
  3. It stops PyTorch from recording operations for gradients, saving memory and time
  4. It switches dropout off
जवाब देखें

जवाब: It stops PyTorch from recording operations for gradients, saving memory and time

These context managers disable gradient tracking. Dropout is controlled by model.eval(), and softmax is a separate step.

पाठ: Running a Model by Hand

12. Which Auto class should you use to load a decoder-only chat model for text generation?

  1. AutoModelForSeq2SeqLM
  2. AutoModelForCausalLM
  3. AutoModelForMaskedLM
  4. AutoModelForSequenceClassification
जवाब देखें

जवाब: AutoModelForCausalLM

Decoder-only models are causal language models. Seq2SeqLM is for encoder-decoder models such as T5 and BART.

पाठ: Picking the Right Model Family

Text Generation and Chat Models

13. Why does `model.generate()` keep a KV cache?

  1. To store the vocabulary on the GPU
  2. To remember previous conversations across program runs
  3. To make sampling random
  4. To avoid recomputing keys and values for earlier tokens at every step
जवाब देखें

जवाब: To avoid recomputing keys and values for earlier tokens at every step

Each step only adds one token, so cached keys and values of earlier positions are reused instead of recomputed.

पाठ: How generate() Writes Text

14. What happens when you lower the sampling temperature from 1.0 to 0.3?

  1. The distribution sharpens, so the most likely tokens are chosen far more often
  2. The model generates fewer tokens
  3. Every token becomes equally likely
  4. Beam search is switched on
जवाब देखें

जवाब: The distribution sharpens, so the most likely tokens are chosen far more often

Dividing logits by a small temperature widens the gaps between them, so softmax concentrates probability on the top tokens.

पाठ: Decoding Strategies: Greedy, Sampling, Beams

15. What does `add_generation_prompt=True` add to a rendered chat prompt?

  1. A system message with safety rules
  2. The model's end-of-sequence token
  3. The marker that starts the assistant's turn, so the model replies instead of continuing the user
  4. A random seed for sampling
जवाब देखें

जवाब: The marker that starts the assistant's turn, so the model replies instead of continuing the user

It appends the assistant-turn header defined by the template, cueing the model to answer.

पाठ: Chat Models and Chat Templates

The Hub and the datasets Library

16. Why pin a model to a specific commit hash with `revision`?

  1. It makes the download faster
  2. So your code keeps loading exactly the weights you tested even if the repo is updated
  3. It is required for private models
  4. It converts the weights to safetensors
जवाब देखें

जवाब: So your code keeps loading exactly the weights you tested even if the repo is updated

A commit hash is immutable, so the same files are loaded every time. Branch names such as main can move.

पाठ: Working with Hub Repositories

17. A teammate loads your saved folder but gets garbage predictions. The weights loaded without error. What is the most likely mistake?

  1. The folder name has a hyphen
  2. You used safetensors instead of pickle
  3. The model was saved in eval mode
  4. They used a different tokenizer than the one the model was trained with
जवाब देखें

जवाब: They used a different tokenizer than the one the model was trained with

Ids only make sense with the matching vocabulary. Always save and load the tokenizer together with the model.

पाठ: Saving and Sharing Your Models

18. Why can the datasets library work with a dataset larger than your laptop's RAM?

  1. Arrow files are memory-mapped from disk, so rows are read on demand
  2. It compresses the data into GPU memory
  3. It automatically drops rows that do not fit
  4. It always streams data from the Hub over the network
जवाब देखें

जवाब: Arrow files are memory-mapped from disk, so rows are read on demand

Datasets are stored as Arrow files and memory-mapped. Streaming is a separate, optional mode.

पाठ: Loading Data with the datasets Library

19. With `ds.map(fn, batched=True)`, what does `fn` receive?

  1. One row as a dictionary of single values
  2. A PyTorch tensor of token ids
  3. A dictionary whose values are lists, one list per column for a batch of rows
  4. The whole dataset as a pandas DataFrame
जवाब देखें

जवाब: A dictionary whose values are lists, one list per column for a batch of rows

In batched mode each column arrives as a list covering the batch, which is what fast tokenizers process efficiently.

पाठ: Transforming Datasets with map and filter

Fine-Tuning with Trainer and Accelerate

20. You have 3,000 labelled customer emails in five categories and one GPU. What is the most sensible first approach?

  1. Train a new Transformer from scratch
  2. Fine-tune a small pretrained encoder such as a DistilBERT or BERT checkpoint
  3. Write regular expressions for each category
  4. Collect a billion more emails before starting
जवाब देखें

जवाब: Fine-tune a small pretrained encoder such as a DistilBERT or BERT checkpoint

A small pretrained encoder fine-tuned on a few thousand examples is fast, cheap and typically strong for fixed-label classification.

पाठ: Why Fine-Tuning Works

21. Your batches contain a `labels` column. Where does the loss come from when you use Trainer with a model from AutoModelForSequenceClassification?

  1. You must always write a custom loss function
  2. The data collator computes it
  3. TrainingArguments computes it from metric_for_best_model
  4. The model's forward pass computes it and returns it in the outputs
जवाब देखें

जवाब: The model's forward pass computes it and returns it in the outputs

Task models compute an appropriate loss (cross-entropy for classification) when labels are passed, and Trainer backpropagates it.

पाठ: Training with Trainer

22. A spam filter is 98% accurate, but only 2% of emails are spam. Which metric reveals whether it actually catches spam?

  1. Recall (or F1) on the spam class
  2. Training loss
  3. Accuracy on the training set
  4. Number of epochs
जवाब देखें

जवाब: Recall (or F1) on the spam class

A model that predicts "not spam" for everything is also 98% accurate. Recall and F1 on the minority class show whether spam is caught.

पाठ: Evaluation and Metrics

23. In an Accelerate training loop, what replaces `loss.backward()`?

  1. optimizer.backward(loss)
  2. model.backward(loss)
  3. accelerator.backward(loss)
  4. torch.backward(loss, device="auto")
जवाब देखें

जवाब: accelerator.backward(loss)

accelerator.backward handles scaling for mixed precision and distributed gradient synchronisation.

पाठ: Custom Training Loops with Accelerate

Efficient Fine-Tuning and Quantization

24. In LoRA, which weights are updated during training?

  1. Only the small low-rank matrices added to selected layers
  2. All of the pretrained weights
  3. Only the embedding layer
  4. Only the tokenizer vocabulary
जवाब देखें

जवाब: Only the small low-rank matrices added to selected layers

LoRA freezes the base model and trains the low-rank A and B matrices, which is why memory use and file size drop so much.

पाठ: LoRA and Parameter-Efficient Fine-Tuning

25. Roughly how much memory do the weights of a 7-billion-parameter model need in bfloat16?

  1. About 3.5 GB
  2. About 28 GB
  3. About 7 GB
  4. About 14 GB
जवाब देखें

जवाब: About 14 GB

bfloat16 uses 2 bytes per parameter: 7e9 x 2 bytes is 14 GB, about 13 GiB, before activations and KV cache.

पाठ: Quantization: Fitting Big Models on Small GPUs

26. In QLoRA, what is stored in 4-bit precision?

  1. The LoRA adapter weights
  2. The frozen base model weights
  3. The optimizer state only
  4. The training dataset
जवाब देखें

जवाब: The frozen base model weights

The frozen base model is quantized to 4-bit to save memory; the small adapters are trained in higher precision.

पाठ: QLoRA: Fine-Tuning on One Consumer GPU

Production: Performance, Security and Pitfalls

27. Which change is usually the largest and cheapest speed win for a fixed classification task?

  1. Raising max_new_tokens
  2. Switching from bfloat16 to float32
  3. Using a smaller model fine-tuned for the task
  4. Turning off batching
जवाब देखें

जवाब: Using a smaller model fine-tuned for the task

A small task-specific model does far less computation per request. float32 is slower than bfloat16, and turning off batching reduces throughput.

पाठ: Making Inference Faster

28. What is continuous batching, as used by LLM inference servers?

  1. Adding new requests to a running batch as soon as other sequences finish
  2. Training the model continuously on user requests
  3. Waiting until exactly 32 requests arrive before running
  4. Splitting one request across many GPUs
जवाब देखें

जवाब: Adding new requests to a running batch as soon as other sequences finish

Continuous batching keeps the GPU busy by swapping finished sequences out and new requests in at each generation step.

पाठ: Serving Models in Production

29. Why is safetensors considered safer than pickle-based `.bin` weight files?

  1. It encrypts the weights
  2. It only stores tensors and metadata, so loading cannot execute arbitrary code
  3. It is always smaller on disk
  4. It requires a Hub token to open
जवाब देखें

जवाब: It only stores tensors and metadata, so loading cannot execute arbitrary code

Pickle can encode code that runs on load. safetensors is a simple tensor format with no code execution path.

पाठ: Security: Remote Code, Pickles and Tokens

30. A fine-tuned classifier scores 99% on the test set but performs poorly on new user data. Which pitfall is the most likely cause?

  1. Using safetensors instead of pickle
  2. Setting padding_side to left
  3. Running inference in bfloat16
  4. Duplicate or near-duplicate examples shared between train and test splits
जवाब देखें

जवाब: Duplicate or near-duplicate examples shared between train and test splits

Leakage between splits makes the test set measure memorisation. Deduplicate before splitting.

पाठ: Common Pitfalls and How to Spot Them

Revision and Interview Prep

31. Which pair correctly matches a task to its Auto class?

  1. Translation with T5 -> AutoModelForCausalLM
  2. Chat with a decoder-only model -> AutoModelForCausalLM
  3. Sentence classification -> AutoModelForTokenClassification
  4. Named-entity recognition -> AutoModelForSeq2SeqLM
जवाब देखें

जवाब: Chat with a decoder-only model -> AutoModelForCausalLM

Decoder-only chat models are causal LMs. T5 uses Seq2SeqLM, sentence classification uses SequenceClassification and NER uses TokenClassification.

पाठ: Cheat Sheet: The Whole Course on One Page

32. An interviewer asks why your fine-tuning results changed when you reran the same script a month later, with identical code and data, loading the base model by name only. What is the best answer?

  1. The tokenizer cache expired
  2. Trainer randomises weights on every run
  3. The model id pointed to a branch that moved; pinning a commit revision would have kept the old weights
  4. GPUs produce different results every day
जवाब देखें

जवाब: The model id pointed to a branch that moved; pinning a commit revision would have kept the old weights

Loading by name tracks the main branch. Pinning a commit hash keeps the exact files and makes runs reproducible.

पाठ: Common Interview and Exam Questions

33. You must run a 7B chat model on one 16 GB GPU and adapt it to your college's FAQ data. Which plan is most realistic?

  1. Load it in 4-bit and train LoRA adapters (QLoRA), then evaluate on held-out FAQs
  2. Full fine-tuning in float32
  3. Train a 7B model from scratch on the FAQ data
  4. Use zero-shot classification on the FAQ answers
जवाब देखें

जवाब: Load it in 4-bit and train LoRA adapters (QLoRA), then evaluate on held-out FAQs

4-bit weights for a 7B model take a few GB, leaving room for LoRA training. Full float32 fine-tuning needs many times more memory.

पाठ: Mixed Self-Check

← कोर्स पर वापस जाएँ