huggingface / setfit

Efficient few-shot learning with Sentence Transformers
https://hf.co/docs/setfit
Apache License 2.0
2.13k stars 217 forks source link

model_config = model.config.to_dict() - AttributeError: 'dict' object has no attribute 'to_dict' #498

Open oObelix opened 6 months ago

oObelix commented 6 months ago

System Info

- `transformers` version: 4.38.2
- Platform: Linux-6.4.16-linuxkit-x86_64-with-glibc2.36
- Python version: 3.11.8
- Huggingface_hub version: 0.21.3
- Safetensors version: 0.4.2
- Accelerate version: 0.23.0
- Accelerate config:    not found
- PyTorch version (GPU?): 2.2.1+cu121 (False)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: <fill in>
- Using distributed or parallel set-up in script?: <fill in>

Python 3.11.8

accelerate==0.23.0
aiohttp==3.9.3
aiosignal==1.3.1
alembic==1.13.1
amqp==5.2.0
annotated-types==0.6.0
anyio==4.3.0
asyncpg==0.28.0
ats-ml-tools==0.7.18
attrs==23.2.0
beautifulsoup4==4.12.3
billiard==4.2.0
blinker==1.7.0
boto3==1.28.85
botocore==1.31.85
celery==5.3.4
certifi==2024.2.2
cffi==1.16.0
charset-normalizer==3.3.2
click==8.1.7
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.3.0
cloudpickle==3.0.0
contourpy==1.2.0
cryptography==42.0.5
cycler==0.12.1
databricks-cli==0.18.0
datasets==2.14.7
datasphere==0.6.8
DAWG-Python==0.7.2
dill==0.3.7
docker==6.1.3
docopt-ng==0.9.0
docutils==0.20.1
entrypoints==0.4
envzy==0.2.3
et-xmlfile==1.1.0
evaluate==0.4.1
fastapi==0.109.2
filelock==3.13.1
Flask==3.0.2
flower==2.0.1
fonttools==4.49.0
frozenlist==1.4.1
fsspec==2023.10.0
funcy==2.0
gitdb==4.0.11
GitPython==3.1.42
googleapis-common-protos==1.62.0
greenlet==2.0.2
grpcio==1.62.0
gunicorn==21.2.0
h11==0.14.0
httpcore==1.0.4
httpx==0.26.0
huggingface-hub==0.21.3
humanize==4.9.0
idna==3.6
importlib-metadata==6.11.0
iniconfig==2.0.0
itsdangerous==2.1.2
jaraco.classes==3.3.1
jeepney==0.8.0
Jinja2==3.1.3
jmespath==1.0.1
joblib==1.3.2
keyring==24.3.1
kiwisolver==1.4.5
kombu==5.3.5
mailbits==0.2.1
Mako==1.3.2
Markdown==3.5.2
markdown-it-py==3.0.0
MarkupSafe==2.1.5
matplotlib==3.8.3
mdurl==0.1.2
mlflow==2.9.2
more-itertools==10.2.0
mpmath==1.3.0
multidict==6.0.5
multiprocess==0.70.15
networkx==3.2.1
nh3==0.2.15
nltk==3.8.1
numpy==1.26.4
nvidia-cublas-cu12==12.1.3.1
nvidia-cuda-cupti-cu12==12.1.105
nvidia-cuda-nvrtc-cu12==12.1.105
nvidia-cuda-runtime-cu12==12.1.105
nvidia-cudnn-cu12==8.9.2.26
nvidia-cufft-cu12==11.0.2.54
nvidia-curand-cu12==10.3.2.106
nvidia-cusolver-cu12==11.4.5.107
nvidia-cusparse-cu12==12.1.0.106
nvidia-nccl-cu12==2.19.3
nvidia-nvjitlink-cu12==12.3.101
nvidia-nvtx-cu12==12.1.105
oauthlib==3.2.2
openpyxl==3.1.2
orjson==3.9.7
packaging==23.2
pandas==2.0.3
pillow==10.2.0
pkginfo==1.9.6
pluggy==1.4.0
prometheus_client==0.20.0
prompt-toolkit==3.0.43
protobuf==4.25.3
psutil==5.9.8
psycopg2-binary==2.9.8
pyarrow==14.0.2
pyarrow-hotfix==0.6
pycparser==2.21
pydantic==2.6.1
pydantic-settings==2.1.0
pydantic_core==2.16.2
Pygments==2.17.2
PyJWT==2.8.0
pymorphy3==1.2.1
pymorphy3-dicts-ru==2.4.417150.4580142
pyparsing==3.1.1
pypi-simple==1.5.0
pytest==7.4.4
python-dateutil==2.9.0
python-dotenv==1.0.1
python-multipart==0.0.6
pytz==2023.4
PyYAML==6.0.1
querystring-parser==1.2.4
readme_renderer==43.0
redis==5.0.1
regex==2023.12.25
requests==2.31.0
requests-toolbelt==1.0.0
responses==0.18.0
rfc3986==2.0.0
rich==13.7.1
s3transfer==0.7.0
safetensors==0.4.2
scikit-learn==1.4.0
scipy==1.12.0
SecretStorage==3.3.3
sentence-transformers==2.2.2
sentencepiece==0.2.0
setfit==1.0.3
six==1.16.0
smmap==5.0.1
sniffio==1.3.1
soupsieve==2.5
SQLAlchemy==2.0.23
sqlparse==0.4.4
starlette==0.36.3
sympy==1.12
tabulate==0.9.0
threadpoolctl==3.3.0
tokenizers==0.15.2
torch==2.2.1
torchvision==0.17.1
tornado==6.4
tqdm==4.66.2
transformers==4.38.2
triton==2.2.0
twine==4.0.2
typing_extensions==4.10.0
tzdata==2024.1
urllib3==2.0.7
uvicorn==0.23.2
vine==5.1.0
wcwidth==0.2.13
websocket-client==1.7.0
Werkzeug==3.0.1
xxhash==3.4.1
yarl==1.9.4
zipp==3.17.0

Who can help?

No response

Information

Tasks

Reproduction

    pretrained_model = 'cointegrated/rubert-tiny-sentiment-balanced'
    model = SetFitModel.from_pretrained(
        pretrained_model,
        cache_dir = tempfile.gettempdir()
    )
    trainer = SetFitTrainer(
        model=model,
        train_dataset=train_dataset,
        eval_dataset=test_dataset,
        loss_class=CosineSimilarityLoss,
        metric="accuracy",
        batch_size=16,
        num_iterations=20,
        num_epochs=2,
    )
 Traceback (most recent call last):
   File "/usr/local/lib/python3.11/site-packages/setfit/trainer.py", line 410, in train
     self.train_embeddings(*full_parameters, args=args)
   File "/usr/local/lib/python3.11/site-packages/setfit/trainer.py", line 462, in train_embeddings
     self._train_sentence_transformer(
   File "/usr/local/lib/python3.11/site-packages/setfit/trainer.py", line 570, in _train_sentence_transformer
     self.control = self.callback_handler.on_train_begin(args, self.state, self.control)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/transformers/trainer_callback.py", line 370, in on_train_begin
     return self.call_event("on_train_begin", args, state, control)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/transformers/trainer_callback.py", line 414, in call_event
     result = getattr(callback, event)(
              ^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/usr/local/lib/python3.11/site-packages/transformers/integrations/integration_utils.py", line 1035, in on_train_begin
     self.setup(args, state, model)
   File "/usr/local/lib/python3.11/site-packages/transformers/integrations/integration_utils.py", line 1009, in setup
     model_config = model.config.to_dict()
                    ^^^^^^^^^^^^^^^^^^^^
 AttributeError: 'dict' object has no attribute 'to_dict'

Expected behavior

Hello! How to pass this error? In my case all work locally on macOs, but didn't work on same data in Docker container.. Didn't work in container FROM --platform=linux/amd64 python:3.11-slim (Linux 027a6910b09d 6.4.16-linuxkit huggingface/transformers#1 SMP PREEMPT Sat Sep 23 13:36:48 UTC 2023 x86_64 GNU/Linux) but work on debian-buster-slim (Linux 12-slim 6.4.16-linuxkit huggingface/transformers#1 SMP PREEMPT Sat Sep 23 13:36:48 UTC 2023 aarch64 GNU/Linux).

RaidenE1 commented 6 months ago

I found this error on my macOS

ArthurZucker commented 6 months ago

cc @tomaarsen

davhin commented 5 months ago

gonna piggy back off this, I had a very similar error that was also about the model.config

File "/opt/conda/lib/python3.10/runpy.py", line 187, in _run_module_as_main mod_name, mod_spec, code = _get_module_details(mod_name, _Error) File "/opt/conda/lib/python3.10/runpy.py", line 110, in _get_module_details __import__(pkg_name) File "/home/jupyter/setfit_test.py", line 39, in <module> trainer.train() File "/opt/conda/lib/python3.10/site-packages/setfit/trainer.py", line 410, in train self.train_embeddings(*full_parameters, args=args) File "/opt/conda/lib/python3.10/site-packages/setfit/trainer.py", line 462, in train_embeddings self._train_sentence_transformer( File "/opt/conda/lib/python3.10/site-packages/setfit/trainer.py", line 570, in _train_sentence_transformer self.control = self.callback_handler.on_train_begin(args, self.state, self.control) File "/opt/conda/lib/python3.10/site-packages/transformers/trainer_callback.py", line 370, in on_train_begin return self.call_event("on_train_begin", args, state, control) File "/opt/conda/lib/python3.10/site-packages/transformers/trainer_callback.py", line 414, in call_event result = getattr(callback, event)( File "/opt/conda/lib/python3.10/site-packages/transformers/integrations/integration_utils.py", line 635, in on_train_begin model_config_json = model.config.to_json_string() AttributeError: 'dict' object has no attribute 'to_json_string'

when I tried to run the setfit example. What seems to have fixed this for me is to downgrade huggingface hub pip install huggingface_hub==0.20.3

with this version it is working both on my Mac with cpu as well as on google cloud with cuda. Hope that helps :)

Ulipenitz commented 5 months ago

I had the same issue with the NeptuneCallback. Worked locally, but got the error in a Docker container on the cloud.

It seems pretty weird to me, because there is no model passed to the on_train_beginhere and the modelis set to Noneby default in the callback here, so I think you can just overwrite the class method and delete these lines here. Or maybe you can catch the AttributeError like this:

if hasattr(model, "config") and model.config is not None:
    try:
        model_config = model.config.to_dict()
    except AttributeError:
        model_config = model.config
    combined_dict = {**model_config, **combined_dict}

This solved my case, because somehow there really was a dict with the model.config, but I could not see why this happened. I also had to rewrite the on_init_end and on_savemethods of the NeptuneCallback, because of conflicts like these.

timothepearce commented 5 months ago

In my case, this error popped up after enabling wandb logging.

timothepearce commented 5 months ago

@tomaarsen I submitted a draft PR on the transformers repository. I wonder if that is the best place to fix the issue.

louis-king99 commented 4 months ago

Getting the same issue as @davhin when running in a vertexAI notebook (google cloud), but not when run locally

rodigu commented 4 months ago

Same issue on Windows 10, Python 3.10.13

hermda02 commented 4 months ago

Same issue on Ubunutu 22.04 and Pop!_OS 22.04.

Issue arose following the SetFit quickstart guide. Crash on step 4:


trainer.train()
***** Running training *****
  Num unique pairs = 144
  Batch size = 32
  Num epochs = 10
  Total optimization steps = 50
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[17], line 1
----> 1 trainer.train()

File ~/projects/NLP/intentionclassification/.venv/lib/python3.10/site-packages/setfit/trainer.py:410, in Trainer.train(self, args, trial, **kwargs)
    405 train_parameters = self.dataset_to_parameters(self.train_dataset)
    406 full_parameters = (
    407     train_parameters + self.dataset_to_parameters(self.eval_dataset) if self.eval_dataset else train_parameters
    408 )
--> 410 self.train_embeddings(*full_parameters, args=args)
    411 self.train_classifier(*train_parameters, args=args)

File ~/projects/NLP/intentionclassification/.venv/lib/python3.10/site-packages/setfit/trainer.py:462, in Trainer.train_embeddings(self, x_train, y_train, x_eval, y_eval, args)
    459 logger.info(f"  Total optimization steps = {total_train_steps}")
    461 warmup_steps = math.ceil(total_train_steps * args.warmup_proportion)
--> 462 self._train_sentence_transformer(
    463     self.model.model_body,
    464     train_dataloader=train_dataloader,
    465     eval_dataloader=eval_dataloader,
    466     args=args,
    467     loss_func=loss_func,
    468     warmup_steps=warmup_steps,
    469 )

File ~/projects/NLP/intentionclassification/.venv/lib/python3.10/site-packages/setfit/trainer.py:570, in Trainer._train_sentence_transformer(self, model_body, train_dataloader, eval_dataloader, args, loss_func, warmup_steps)
    568 else:
    569     self.state.max_steps = len(train_dataloader) * args.embedding_num_epochs
--> 570 self.control = self.callback_handler.on_train_begin(args, self.state, self.control)
    571 steps_per_epoch = len(train_dataloader)
    573 if args.use_amp:

File ~/projects/NLP/intentionclassification/.venv/lib/python3.10/site-packages/transformers/trainer_callback.py:370, in CallbackHandler.on_train_begin(self, args, state, control)
    368 def on_train_begin(self, args: TrainingArguments, state: TrainerState, control: TrainerControl):
    369     control.should_training_stop = False
--> 370     return self.call_event("on_train_begin", args, state, control)

File ~/projects/NLP/intentionclassification/.venv/lib/python3.10/site-packages/transformers/trainer_callback.py:414, in CallbackHandler.call_event(self, event, args, state, control, **kwargs)
    412 def call_event(self, event, args, state, control, **kwargs):
    413     for callback in self.callbacks:
--> 414         result = getattr(callback, event)(
    415             args,
    416             state,
    417             control,
    418             model=self.model,
    419             tokenizer=self.tokenizer,
    420             optimizer=self.optimizer,
    421             lr_scheduler=self.lr_scheduler,
    422             train_dataloader=self.train_dataloader,
    423             eval_dataloader=self.eval_dataloader,
    424             **kwargs,
    425         )
    426         # A Callback can skip the return of `control` if it doesn't change it.
    427         if result is not None:

File ~/projects/NLP/intentionclassification/.venv/lib/python3.10/site-packages/transformers/integrations/integration_utils.py:1035, in MLflowCallback.on_train_begin(self, args, state, control, model, **kwargs)
   1033 def on_train_begin(self, args, state, control, model=None, **kwargs):
   1034     if not self._initialized:
-> 1035         self.setup(args, state, model)

File ~/projects/NLP/intentionclassification/.venv/lib/python3.10/site-packages/transformers/integrations/integration_utils.py:1009, in MLflowCallback.setup(self, args, state, model)
   1007 combined_dict = args.to_dict()
   1008 if hasattr(model, "config") and model.config is not None:
-> 1009     model_config = model.config.to_dict()
   1010     combined_dict = {**model_config, **combined_dict}
   1011 combined_dict = flatten_dict(combined_dict) if self._flatten_params else combined_dict

AttributeError: 'dict' object has no attribute 'to_dict'```