Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eval_on_start triggers AttributeError in JupyterLab #32689

Closed
2 of 4 tasks
matteosantama opened this issue Aug 14, 2024 · 1 comment · Fixed by #32849
Closed
2 of 4 tasks

eval_on_start triggers AttributeError in JupyterLab #32689

matteosantama opened this issue Aug 14, 2024 · 1 comment · Fixed by #32849

Comments

@matteosantama
Copy link

matteosantama commented Aug 14, 2024

System Info

transformers: '4.44.0'
Python: '3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:45:18) [GCC 12.3.0]'

Who can help?

@muellerz @SunMarc

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

Here is a self-contained script that triggers the error:

model_id = "distilbert/distilbert-base-uncased"

model = AutoModelForSequenceClassification.from_pretrained(
    model_id, num_labels=2
)
tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=True)

ds = (
    Dataset.from_dict({"text": ["Hello world!"], "labels": [0]})
    .map(
        lambda b: tokenizer(b["text"], truncation=True),
        batched=True,
    )
)


trainer = Trainer(
    model=model,
    args=TrainingArguments(
        output_dir="temp",
        eval_strategy="steps",
        save_strategy="no",
        eval_on_start=True,  # <-- setting to False avoids the error
    ),
    tokenizer=tokenizer,
    train_dataset=ds,
    eval_dataset=ds,
    compute_metrics=lambda _: {"metric": 1.0},
)
trainer.train()

The error I get is

AttributeError: 'NotebookTrainingTracker' object has no attribute 'value'

which seems related to the fact that I'm running the code in a Jupyter notebook.

Expected behavior

I should be able to use the eval_on_start parameter without triggering an error.

@fshp971
Copy link
Contributor

fshp971 commented Aug 16, 2024

Hi,

I have found that the cause of this exception is that in Jupyter Notebook, for a transformers.Trainer, when its argument eval_on_start is set to True, running Trainer.train() will lead to the following function execution order:

  1. NotebookProgressCallback.on_train_begin()
  2. NotebookProgressCallback.on_prediction_step()
  3. NotebookProgressCallback.on_train_end()

Running NotebookProgressCallback.on_prediction_step() directly after NotebookProgressCallback.on_train_begin() will result in two attributes of NotebookProgressBar, i.e., value and label, being used before assigned (see methods update(), update_bar() and display() of NotebookProgressBar in transformers/utils/notebook.py for details).

I have open a PR to fix this.

ArthurZucker pushed a commit that referenced this issue Aug 22, 2024
… `eval_on_start=True` in Jupyter Notebook. (#32849)

fix: `AttributeError` raised when using `Trainer` with `eval_on_start=True` in Jupyter Notebook.
zucchini-nlp pushed a commit to zucchini-nlp/transformers that referenced this issue Aug 30, 2024
…ainer` with `eval_on_start=True` in Jupyter Notebook. (huggingface#32849)

fix: `AttributeError` raised when using `Trainer` with `eval_on_start=True` in Jupyter Notebook.
zucchini-nlp pushed a commit to zucchini-nlp/transformers that referenced this issue Aug 30, 2024
…ainer` with `eval_on_start=True` in Jupyter Notebook. (huggingface#32849)

fix: `AttributeError` raised when using `Trainer` with `eval_on_start=True` in Jupyter Notebook.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants