33
44import pytest
55
6- from traceml_ai .integrations .huggingface import (
6+ torch = pytest .importorskip ("torch" )
7+ pytest .importorskip ("transformers" )
8+ pytest .importorskip ("accelerate" )
9+
10+ from transformers import ( # noqa: E402
11+ BertConfig ,
12+ BertForSequenceClassification ,
13+ Trainer ,
14+ TrainingArguments ,
15+ )
16+
17+ from traceml_ai .integrations .huggingface import ( # noqa: E402
718 TraceMLTrainer ,
819 TraceMLTrainerCallback ,
920 init ,
1021)
1122
12- try :
13- import torch
14- from transformers import (
15- BertConfig ,
16- BertForSequenceClassification ,
17- Trainer ,
18- TrainingArguments ,
19- )
20-
21- HAS_TRANSFORMERS = True
22- except ImportError :
23- HAS_TRANSFORMERS = False
24-
2523
26- class _TinyTokenizedDataset (
27- torch .utils .data .Dataset if HAS_TRANSFORMERS else object
28- ):
24+ class _TinyTokenizedDataset (torch .utils .data .Dataset ):
2925 """
3026 Small synthetic dataset for Trainer integration tests.
3127
@@ -149,7 +145,6 @@ def _reset_traceml_state() -> None:
149145 break
150146
151147
152- @pytest .mark .skipif (not HAS_TRANSFORMERS , reason = "transformers not installed" )
153148def test_hf_trainer_integration ():
154149 """
155150 Test that TraceMLTrainer (legacy thin-wrapper path) runs a few steps with
@@ -175,7 +170,6 @@ def test_hf_trainer_integration():
175170 assert TraceState .step >= 5 , "TraceState.step should have incremented"
176171
177172
178- @pytest .mark .skipif (not HAS_TRANSFORMERS , reason = "transformers not installed" )
179173def test_hf_trainer_callback_integration ():
180174 """
181175 Vanilla transformers.Trainer with TraceMLTrainerCallback should emit
@@ -313,7 +307,6 @@ def _counts(event_name: str) -> list:
313307 )
314308
315309
316- @pytest .mark .skipif (not HAS_TRANSFORMERS , reason = "transformers not installed" )
317310def test_hf_trainer_wrapper_equivalent_to_direct_callback ():
318311 """
319312 TraceMLTrainer is now a thin wrapper that auto-installs
@@ -367,7 +360,6 @@ def _run_with(make_trainer) -> tuple:
367360 assert wrapper_samples == max_steps
368361
369362
370- @pytest .mark .skipif (not HAS_TRANSFORMERS , reason = "transformers not installed" )
371363def test_hf_trainer_wrapper_dedups_user_supplied_callback ():
372364 """
373365 If a user passes their own TraceMLTrainerCallback in callbacks=[...] and
@@ -413,7 +405,6 @@ def test_hf_trainer_wrapper_dedups_user_supplied_callback():
413405 )
414406
415407
416- @pytest .mark .skipif (not HAS_TRANSFORMERS , reason = "transformers not installed" )
417408def test_hf_trainer_callback_noop_when_disabled (monkeypatch ):
418409 """
419410 With TRACEML_DISABLED=1 set after import, the callback must be a complete
@@ -457,7 +448,6 @@ def test_hf_trainer_callback_noop_when_disabled(monkeypatch):
457448 ), f"Expected no StepMemoryEvents when disabled, got { len (drained )} ."
458449
459450
460- @pytest .mark .skipif (not HAS_TRANSFORMERS , reason = "transformers not installed" )
461451def test_hf_init_enables_dataloader_and_h2d_patches (monkeypatch ):
462452 """
463453 init() must enable the process-wide patches the callback cannot install on
@@ -496,7 +486,6 @@ def test_hf_init_enables_dataloader_and_h2d_patches(monkeypatch):
496486DATALOADER_STREAM = "_traceml_internal:dataloader_next"
497487
498488
499- @pytest .mark .skipif (not HAS_TRANSFORMERS , reason = "transformers not installed" )
500489def test_hf_callback_run_emits_dataloader_fetch_events ():
501490 """
502491 COMPLETENESS guard: with huggingface.init() called, a vanilla Trainer +
0 commit comments