Chapter 1 questions


why?


This was suggested by chatgpt but I have not got my answer yet as for why and how does this change. Why the result is “POSITIVE”?

Hi Everyone, I am following along the course on page Transformers, what can they do? - Hugging Face LLM Course. I am trying to generate text using HuggingFaceTB/SmolLM2-360M with the paramaters max_length=30, num_return_sequences=2.
It gives me the following error: ValueError: Greedy methods without beam search do not support num_return_sequences different than 1 (got 2).

Would anyone be able to help me understand what it means and how to resolve this issue?

I don’t understand the logic, but this solved the problem.

from transformers import pipeline

generator = pipeline("text-generation", model="HuggingFaceTB/SmolLM2-360M")
generator(
    "In this course, we will teach you how to",
    max_length=30,
    num_return_sequences=2,
    do_sample=True,
)

Can you provide certifcate of completion if i finished this course?

Hi @Pizofreude,

I just published two blog posts about recent RL algorithms for reasoning tasks such as GRPO and Dr. GRPO.
You can find them on Medium:

I hope you find it helpful in some way.

Thank you,
Jen

I am working on the Transformers, what can they do? section, on the Text Generation section. When I try to run

from transformers import pipeline

generator = pipeline("text-generation", model="HuggingFaceTB/SmolLM2-360M")
generator(
    "In this course, we will teach you how to",
    max_length=30,
    num_return_sequences=2,
)

I get this error: Both max_new_tokens(=256) andmax_length(=15) seem to have been set. max_new_tokens will take precedence.

I read the documentation and it states that max_length “corresponds to the length of the input prompt + max_new_tokens”. Should I just use max_new_tokens instead of max_length?

I tend to use max_new_tokens.

I’ve noticed that the GitHub page for this course doesn’t have a translation in my native language, which is spoken by around 100 million people. I was wondering if spending time translating the page into my local language would provide any benefits for me or others who might be interested in taking the course. If so, I might consider translating other courses as well

Hello! I’ve been trying to get the course commands to run locally on my Mac. I have Apple M4 Pro on 15.6.1, and I have activated the venv.

You can see my pip freeze here:

$ pip freeze | grep -E "transform|torch|tensor"     
safetensors==0.6.2
tensorboard==2.20.0
tensorboard-data-server==0.7.2
tensorflow==2.20.0
torch==2.8.0
torchaudio==2.8.0
torchvision==0.23.0
transformers==4.56.1

Python is the one inside the transformers-course/.env/bin/python, and

$ python --version
Python 3.13.7

Do you know what may be the issue here?

>>> from transformers import pipeline
libc++abi: terminating due to uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument
zsh: abort      python

I seem to have similar issues with a handful of random import commands… Thank you!

Do you know what may be the issue here?

Python 3.13.7

Python 3.13 or later sometimes cause issues with many libraries highly possibly…
I recommend 3.12 or 3.11.

Unfortunately the same issue applied to python 3.12…

$ python --version
Python 3.12.11
$ pip freeze | grep -E "transform|torch|tensor" 
safetensors==0.6.2
tensorboard==2.20.0
tensorboard-data-server==0.7.2
tensorflow==2.20.0
torch==2.8.0
torchaudio==2.8.0
torchvision==0.23.0
transformers==4.56.1

And python 3.11…

$ python3.11
Python 3.11.11 (main, Jul 13 2025, 04:58:55) [Clang 16.0.0 (clang-1600.0.26.6)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from transformers import pipeline
libc++abi: terminating due to uncaught exception of type std::__1::system_error: mutex lock failed: Invalid argument
zsh: abort      python3.11

Thank you for looking!

Some versions of TensorFlow seem to crash on Mac
Guess this is the only way?

pip uninstall -y tensorflow tf-keras tf_keras keras onnxruntime
export USE_TF=0  # prevents TF checks inside transformers
python -c "from transformers import pipeline; print('import ok')"

Yes, lowering the tensorflow version worked thank you!

This combination worked for me.

$ pip freeze | grep -E "tensor|numpy|protobuf" 
numpy==2.1.3
protobuf==5.29.5
safetensors==0.6.2
tensorboard==2.19.0
tensorboard-data-server==0.7.2
tensorflow==2.19.1

In section “A bit of Transformer history” this part

January 2022: InstructGPT, a version of GPT-3 that was trained to follow instructions better This list is far from comprehensive, and is just meant to highlight a few of the different kinds of Transformer models. Broadly, they can be grouped into three categories:

is not followed by, what I believe, is the correct one:

GPT-like (also called auto-regressive Transformer models)

BERT-like (also called auto-encoding Transformer models)

T5-like (also called sequence-to-sequence Transformer models)

Is saving my progress possible in this course and the others?

Any answers?

When I use the Colab notebook, I get an error on the second cell. Does anyone know how to fix it?

from transformers import pipeline

classifier = pipeline(“sentiment-analysis”)

classifier(“I’ve been waiting for a HuggingFace course my whole life.”)

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/usr/local/lib/python3.12/dist-packages/transformers/utils/import_utils.py in _getattr_(self, name)
2248 try:
→ 2249 module = self._get_module(self._class_to_module[name])
2250 value = getattr(module, name)

/usr/local/lib/python3.12/dist-packages/transformers/utils/import_utils.py in _get_module(self, module_name)
2482 except Exception as e:
→ 2483 raise e
2484

/usr/local/lib/python3.12/dist-packages/transformers/utils/import_utils.py in _get_module(self, module_name)
2480 try:
→ 2481 return importlib.import_module(“.” + module_name, self._name_)
2482 except Exception as e:

/usr/lib/python3.12/importlib/_init_.py in import_module(name, package)
89 level += 1
—> 90 return _bootstrap._gcd_import(name[level:], package, level)
91

/usr/lib/python3.12/importlib/_bootstrap.py in _gcd_import(name, package, level)

/usr/lib/python3.12/importlib/_bootstrap.py in _find_and_load(name, import_)

/usr/lib/python3.12/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

/usr/lib/python3.12/importlib/_bootstrap.py in _load_unlocked(spec)

/usr/lib/python3.12/importlib/_bootstrap_external.py in exec_module(self, module)

/usr/lib/python3.12/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

/usr/local/lib/python3.12/dist-packages/transformers/pipelines/_init_.py in
26 from ..feature_extraction_utils import FeatureExtractionMixin
—> 27 from ..image_processing_utils import BaseImageProcessor
28 from ..models.auto.configuration_auto import AutoConfig

/usr/local/lib/python3.12/dist-packages/transformers/image_processing_utils.py in
33 )
—> 34 from .processing_utils import ImagesKwargs, Unpack
35 from .utils import (

/usr/local/lib/python3.12/dist-packages/transformers/processing_utils.py in
62 from .utils.chat_template_utils import _get_template_variables, render_jinja_template
—> 63 from .utils.type_validators import (
64 device_validator,

/usr/local/lib/python3.12/dist-packages/transformers/utils/type_validators.py in
7 from ..tokenization_utils_base import PaddingStrategy, TruncationStrategy
----> 8 from ..video_utils import VideoMetadataType
9 from .generic import TensorType

/usr/local/lib/python3.12/dist-packages/transformers/video_utils.py in
48 if is_torchvision_available():
—> 49 from torchvision import io as torchvision_io
50

/usr/local/lib/python3.12/dist-packages/torchvision/_init_.py in
7 from . import extension # usort:skip # noqa: F401
----> 8 from torchvision import _meta_registrations, datasets, io, models, ops, transforms, utils # usort:skip
9

/usr/local/lib/python3.12/dist-packages/torchvision/models/_init_.py in
1 from .alexnet import *
----> 2 from .convnext import *
3 from .densenet import *

/usr/local/lib/python3.12/dist-packages/torchvision/models/convnext.py in
9 from ..ops.misc import Conv2dNormActivation, Permute
—> 10 from ..ops.stochastic_depth import StochasticDepth
11 from ..transforms._presets import ImageClassification

/usr/local/lib/python3.12/dist-packages/torchvision/ops/_init_.py in
22 from .misc import Conv2dNormActivation, Conv3dNormActivation, FrozenBatchNorm2d, MLP, Permute, SqueezeExcitation
—> 23 from .poolers import MultiScaleRoIAlign
24 from .ps_roi_align import ps_roi_align, PSRoIAlign

/usr/local/lib/python3.12/dist-packages/torchvision/ops/poolers.py in
9 from ..utils import _log_api_usage_once
—> 10 from .roi_align import roi_align
11

/usr/local/lib/python3.12/dist-packages/torchvision/ops/roi_align.py in
6 from torch import nn, Tensor
----> 7 from torch._dynamo.utils import is_compile_supported
8 from torch.jit.annotations import BroadcastingList2

/usr/local/lib/python3.12/dist-packages/torch/_dynamo/_init_.py in
12
—> 13 from . import (
14 aot_compile,

/usr/local/lib/python3.12/dist-packages/torch/_dynamo/aot_compile.py in
14 import torch.fx
—> 15 from torch._dynamo.convert_frame import GraphRuntimeEnv
16 from torch._dynamo.graph_utils import _graph_device_type

/usr/local/lib/python3.12/dist-packages/torch/_dynamo/convert_frame.py in
61 from torch._dynamo.distributed import get_compile_pg
—> 62 from torch._dynamo.symbolic_convert import TensorifyState
63 from torch._guards import compile_context, CompileContext, CompileId, tracing

/usr/local/lib/python3.12/dist-packages/torch/_dynamo/symbolic_convert.py in
53 from torch._dynamo.dynamo_profiler import DynamoProfilerState, FunctionTraceTiming
—> 54 from torch._dynamo.exc import ObservedException, TensorifyScalarRestartAnalysis
55 from torch._guards import InlinedCodeCache, tracing, TracingContext

/usr/local/lib/python3.12/dist-packages/torch/_dynamo/exc.py in
43 from . import config
—> 44 from .utils import counters
45

/usr/local/lib/python3.12/dist-packages/torch/_dynamo/utils.py in
69 import torch._functorch.config
—> 70 import torch.fx.experimental.symbolic_shapes
71 import torch.utils._pytree as pytree

/usr/local/lib/python3.12/dist-packages/torch/fx/experimental/symbolic_shapes.py in
79 from torch.utils._python_dispatch import is_traceable_wrapper_subclass
—> 80 from torch.utils._sympy.functions import (
81 Application,

/usr/local/lib/python3.12/dist-packages/torch/utils/_sympy/functions.py in
187 # not, this can potentially cause correctness issues.
→ 188 class FloorDiv(sympy.Function):
189 “”"

/usr/local/lib/python3.12/dist-packages/torch/utils/_sympy/functions.py in FloorDiv()
210
→ 211 def _sympystr(self, printer: sympy.printing.StrPrinter) → str:
212 base = printer.parenthesize(self.base, PRECEDENCE[“Atom”] - 0.5)

AttributeError: module ‘sympy’ has no attribute ‘printing’

The above exception was the direct cause of the following exception:

ModuleNotFoundError Traceback (most recent call last)

/tmp/ipykernel_3167/711506748.py in <cell line: 0>()
----> 1 from transformers import pipeline
2
3 classifier = pipeline(“sentiment-analysis”)
4 classifier(“I’ve been waiting for a HuggingFace course my whole life.”)

/usr/local/lib/python3.12/dist-packages/transformers/utils/import_utils.py in _getattr_(self, name)
2335 ) from e
2336 else:
→ 2337 raise ModuleNotFoundError(
2338 f"Could not import module ‘{name}’. Are this object’s requirements defined correctly?"
2339 ) from e

ModuleNotFoundError: Could not import module ‘pipeline’. Are this object’s requirements defined correctly?

Hmm, it works on my Colab environment, so maybe it’s something like this?:


I don’t think the three-line Course snippet itself is the main problem here.

This code is still a normal way to do a first Transformers smoke test:

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
classifier("I’ve been waiting for a Hugging Face course my whole life.")

The current Transformers docs still describe pipeline as the high-level inference API, and the installation docs still use a pipeline("sentiment-analysis") command as an install check:

So I would read your traceback as an environment / dependency-stack issue, not as “the Chapter 1 example is conceptually wrong”.

What looks suspicious in the traceback

The important part is not really the top-level pipeline("sentiment-analysis") line. The traceback eventually goes through something like this:

transformers
  -> transformers.pipelines
  -> transformers.video_utils
  -> torchvision.io
  -> torch._dynamo
  -> torch.fx.experimental.symbolic_shapes
  -> torch.utils._sympy.functions
  -> sympy.printing

and then fails with:

AttributeError: module 'sympy' has no attribute 'printing'

That means the failure happens while importing the PyTorch / torchvision / SymPy-related stack. The sentiment-analysis model has not really had a chance to run yet.

In other words, this is probably closer to:

“Transformers tried to import pipeline helpers, that pulled in torchvision / PyTorch internals, and the installed PyTorch + SymPy combination was not in a clean compatible state.”

rather than:

“The sentiment-analysis pipeline example is obsolete.”

I tried a clean Colab Free CPU runtime

For comparison, I tried the same basic snippet on my Colab environment without setting an HF_TOKEN.

It worked.

My environment was:

Package Version
Python 3.12.13
transformers 5.9.0
torch 2.11.0+cpu
torchvision 0.26.0+cpu
torchaudio 2.11.0+cpu
sympy 1.14.0
huggingface_hub 1.17.0
tokenizers 0.22.2
safetensors 0.7.0

The output was:

[{'label': 'POSITIVE', 'score': 0.9984577894210815}]

I also saw the usual Colab warning about not having an HF_TOKEN, but that was not fatal. Public models can still be downloaded without a token, although authenticated requests may have better rate limits.

So this does not look like a general “current Colab cannot run Chapter 1” problem.

Similar known pattern

There are a few similar-looking dependency-drift cases around this area:

Pattern Why it is relevant
AttributeError: module 'sympy' has no attribute 'printing' Similar failure shape: PyTorch symbolic-shape code reaches for sympy.printing and crashes.
pipeline import fails with torchvision::nms mismatch Text-only pipeline usage can still fail because torchvision gets imported during the Transformers import path.
Failed to import transformers.pipelines ... operator torchvision::nms does not exist Another example where from transformers import pipeline surfaces a lower-level torch / torchvision mismatch.
Colab past runtime FAQ Colab runtimes and preinstalled packages change over time, which is exactly the kind of thing that can break old course notebooks or old notebook sessions.
Course issue: Chapter 1 notebook drift Not the same root cause, but it shows that Course notebooks can drift as Transformers and hosted runtimes evolve.
Course issue: grouped_entities removed/deprecated Another Chapter 1 drift example, though this one is about a changed pipeline argument rather than your SymPy traceback.

The closest match to your exact traceback is the SymPy/PyTorch-looking sympy.printing problem. The closest structural match is the Transformers issues where a text pipeline fails because torchvision is imported underneath.

What I would try first

First, restart the runtime and run only the minimal snippet again.

In Colab:

Runtime -> Restart runtime

Then run:

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
classifier("I’ve been waiting for a Hugging Face course my whole life.")

If that still fails, collect the versions:

import sys
import importlib.metadata as md

print("python:", sys.version)

for pkg in ["transformers", "torch", "torchvision", "torchaudio", "sympy", "huggingface_hub", "tokenizers", "safetensors"]:
    try:
        print(f"{pkg}: {md.version(pkg)}")
    except md.PackageNotFoundError:
        print(f"{pkg}: not installed")

Small workaround to test the SymPy hypothesis

Because your error is specifically:

AttributeError: module 'sympy' has no attribute 'printing'

it may be worth testing whether explicitly importing the SymPy printing submodule changes anything:

import sympy.printing

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
classifier("I’ve been waiting for a Hugging Face course my whole life.")

If that works, I would treat it as an import-order / dependency-stack workaround, not as the final conceptual fix.

If the workaround does not help

Then I would try repairing the package stack.

Start small:

!pip install -U -q sympy

Then restart the runtime:

Runtime -> Restart runtime

and retry:

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
classifier("I’ve been waiting for a Hugging Face course my whole life.")

If that still fails, try reinstalling the relevant stack together:

!pip install -U -q transformers torch torchvision torchaudio sympy

Then restart the runtime again.

Optional: make the model explicit

The default pipeline currently chooses a small public sentiment model automatically, but for debugging it is often nicer to be explicit:

from transformers import pipeline

classifier = pipeline(
    "sentiment-analysis",
    model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
)

classifier("I’ve been waiting for a Hugging Face course my whole life.")

This avoids confusing “default model selection” with the real import problem.

My current guess

My guess is:

  1. the Course snippet is probably fine;
  2. your Colab session had a stale or inconsistent dependency state;
  3. the actual breakage is in the torch / torchvision / sympy import chain;
  4. a clean runtime, a SymPy update, or reinstalling the PyTorch-family packages together should probably fix it.

So I would not start by rewriting the Course example. I would first verify the environment versions and try a clean runtime.