Quick Start

This chapter runs your first inference with Pipeline. The pipeline downloads the tokenizer and weights and caches them. Every example is ungated.


Sentiment classification (start here)

distilbert/distilbert-base-uncased-finetuned-sst-2-english is DistilBERT fine-tuned on SST-2—the usual official classification demo:

from transformers import pipeline

clf = pipeline(
    "text-classification",
    model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
)
print(clf("This movie was a delight from start to finish."))
print(clf("I would not recommend this film to anyone."))

Typical output:

[{'label': 'POSITIVE', 'score': 0.99...}]
[{'label': 'NEGATIVE', 'score': 0.99...}]

What happens:

  1. from_pretrained fetches config.json, the vocab, and model.safetensors
  2. The tokenizer turns text into input_ids / attention_mask
  3. The model emits logits; the pipeline applies softmax and maps labels

To fine-tune the same architecture yourself, use the base distilbert/distilbert-base-uncased plus rotten_tomatoes—see Fine-tuning.


Text generation (small models)

DistilGPT2 is CPU-friendly for continuation. For instruction chat or Chinese, switch to Qwen:

from transformers import pipeline

gen = pipeline(
    "text-generation",
    model="distilbert/distilgpt2",
    max_new_tokens=40,
)
print(gen("In a distant future, robots and humans")[0]["generated_text"])

Instruct model (~0.5B; a GPU is more comfortable):

chat = pipeline(
    "text-generation",
    model="Qwen/Qwen2.5-0.5B-Instruct",
    device_map="auto",
    dtype="auto",
    max_new_tokens=64,
)
print(chat("Explain attention in one sentence.")[0]["generated_text"])

On the Hub, Qwen and DeepSeek Instruct checkpoints are also CN-friendly—check gated status and VRAM first.


Devices

On NVIDIA GPUs, Pipeline accepts device=0. For automatic placement, use the lower-level API:

from transformers import AutoModelForCausalLM, AutoTokenizer

tok = AutoTokenizer.from_pretrained("distilbert/distilgpt2")
model = AutoModelForCausalLM.from_pretrained(
    "distilbert/distilgpt2",
    device_map="auto",
    dtype="auto",
)

If you only want chat and not weight formats, use Ollama. To control decoding, continue with Text Generation.


How long is the first download?

DistilBERT / DistilGPT2 are tens to ~150 MB; Whisper-tiny is about 150 MB; Qwen2.5-0.5B is about 1 GB. After the progress bar finishes, files live under HF_HOME and the next run is nearly instant.


Checklist

  1. Create a venv; install torch and transformers datasets evaluate accelerate
  2. Run the classification pipeline
  3. Run DistilGPT2 generation and watch max_new_tokens
  4. (Optional) hf auth login so you can upload later
  5. Skim the model card

Next steps

评论