From 681616b37cd7481d0f250e4257367a5477f03cf5 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 17 Aug 2026 09:09:43 +0000 Subject: [PATCH 1/2] Apply TrainingArguments.report_to when building the internal trainer The reporting callbacks of the internal Sentence Transformers trainer were created from the default 'all' setting rather than from the SetFit TrainingArguments, so integrations that the user did not request were still initialized and executed. Co-authored-by: Tony Coder <407243179@qq.com> --- src/setfit/trainer.py | 6 +++++- tests/test_trainer.py | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/setfit/trainer.py b/src/setfit/trainer.py index ba035d45..c0acaf7f 100644 --- a/src/setfit/trainer.py +++ b/src/setfit/trainer.py @@ -49,7 +49,11 @@ def __init__( self.logs_prefix = "embedding" super().__init__( model=setfit_model.model_body, - args=SentenceTransformerTrainingArguments(output_dir=setfit_args.output_dir), + # `report_to` must be set before the superclass initializes the reporting callbacks, otherwise + # integrations that the user excluded are still loaded and executed. + args=SentenceTransformerTrainingArguments( + output_dir=setfit_args.output_dir, report_to=setfit_args.report_to + ), **kwargs, ) self._apply_training_arguments(setfit_args) diff --git a/tests/test_trainer.py b/tests/test_trainer.py index 923d8527..b3fe6d9b 100644 --- a/tests/test_trainer.py +++ b/tests/test_trainer.py @@ -8,11 +8,11 @@ import torch from datasets import Dataset, load_dataset from sentence_transformers import losses -from transformers import TrainerCallback +from transformers import TrainerCallback, integrations from transformers.testing_utils import require_optuna from transformers.utils.hp_naming import TrialShortNamer -from setfit import logging +from setfit import logging, training_args from setfit.losses import SupConLoss from setfit.modeling import SetFitModel from setfit.trainer import Trainer @@ -557,6 +557,24 @@ class TestCallback(TrainerCallback): assert callback not in trainer.st_trainer.callback_handler.callbacks +def test_trainer_report_to(model: SetFitModel, monkeypatch: pytest.MonkeyPatch): + class DummyReportCallback(TrainerCallback): + pass + + monkeypatch.setattr(training_args, "get_available_reporting_integrations", lambda: ["dummy"]) + monkeypatch.setitem(integrations.INTEGRATION_TO_CALLBACK, "dummy", DummyReportCallback) + + def reports_to_dummy(args: TrainingArguments) -> bool: + trainer = Trainer(model=model, args=args) + return any( + isinstance(callback, DummyReportCallback) for callback in trainer.st_trainer.callback_handler.callbacks + ) + + assert reports_to_dummy(TrainingArguments(report_to="all")) + assert reports_to_dummy(TrainingArguments(report_to="dummy")) + assert not reports_to_dummy(TrainingArguments(report_to="none")) + + def test_trainer_warn_freeze(model: SetFitModel): trainer = Trainer(model) with pytest.warns( From 2e29bd7696247b84039d4b1a37755dacc63ff7b9 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 17 Aug 2026 09:22:21 +0000 Subject: [PATCH 2/2] Reference issue #621 in the report_to regression test Co-authored-by: Tony Coder <407243179@qq.com> --- tests/test_trainer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_trainer.py b/tests/test_trainer.py index b3fe6d9b..e4c5cd11 100644 --- a/tests/test_trainer.py +++ b/tests/test_trainer.py @@ -558,6 +558,7 @@ class TestCallback(TrainerCallback): def test_trainer_report_to(model: SetFitModel, monkeypatch: pytest.MonkeyPatch): + # Issue #621: integrations that were not requested via `report_to` must not be loaded class DummyReportCallback(TrainerCallback): pass