Skip to content

Commit 4c19f3b

Browse files
authored
Clean Trainer tests and datasets dep (#8268)
1 parent 068e6b5 commit 4c19f3b

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

.circleci/config.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ jobs:
7777
- v0.4-torch_and_tf-{{ checksum "setup.py" }}
7878
- v0.4-{{ checksum "setup.py" }}
7979
- run: pip install --upgrade pip
80-
- run: pip install git+https://github.com/huggingface/datasets
8180
- run: pip install .[sklearn,tf-cpu,torch,testing]
8281
- save_cache:
8382
key: v0.4-{{ checksum "setup.py" }}
@@ -102,7 +101,6 @@ jobs:
102101
- v0.4-torch-{{ checksum "setup.py" }}
103102
- v0.4-{{ checksum "setup.py" }}
104103
- run: pip install --upgrade pip
105-
- run: pip install git+https://github.com/huggingface/datasets
106104
- run: pip install .[sklearn,torch,testing]
107105
- save_cache:
108106
key: v0.4-torch-{{ checksum "setup.py" }}
@@ -129,7 +127,6 @@ jobs:
129127
- v0.4-tf-{{ checksum "setup.py" }}
130128
- v0.4-{{ checksum "setup.py" }}
131129
- run: pip install --upgrade pip
132-
- run: pip install git+https://github.com/huggingface/datasets
133130
- run: pip install .[sklearn,tf-cpu,testing]
134131
- save_cache:
135132
key: v0.4-tf-{{ checksum "setup.py" }}
@@ -154,7 +151,6 @@ jobs:
154151
- v0.4-flax-{{ checksum "setup.py" }}
155152
- v0.4-{{ checksum "setup.py" }}
156153
- run: pip install --upgrade pip
157-
- run: pip install git+https://github.com/huggingface/datasets
158154
- run: sudo pip install .[flax,sklearn,torch,testing]
159155
- save_cache:
160156
key: v0.4-flax-{{ checksum "setup.py" }}
@@ -179,7 +175,6 @@ jobs:
179175
- v0.4-torch-{{ checksum "setup.py" }}
180176
- v0.4-{{ checksum "setup.py" }}
181177
- run: pip install --upgrade pip
182-
- run: pip install git+https://github.com/huggingface/datasets
183178
- run: pip install .[sklearn,torch,testing]
184179
- save_cache:
185180
key: v0.4-torch-{{ checksum "setup.py" }}
@@ -204,7 +199,6 @@ jobs:
204199
- v0.4-tf-{{ checksum "setup.py" }}
205200
- v0.4-{{ checksum "setup.py" }}
206201
- run: pip install --upgrade pip
207-
- run: pip install git+https://github.com/huggingface/datasets
208202
- run: pip install .[sklearn,tf-cpu,testing]
209203
- save_cache:
210204
key: v0.4-tf-{{ checksum "setup.py" }}

tests/test_trainer.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
import tempfile
1919
import unittest
2020

21-
import datasets
2221
import numpy as np
2322

2423
from transformers import AutoTokenizer, EvaluationStrategy, PretrainedConfig, TrainingArguments, is_torch_available
2524
from transformers.file_utils import WEIGHTS_NAME
2625
from transformers.testing_utils import (
2726
get_tests_dir,
27+
require_datasets,
2828
require_optuna,
2929
require_sentencepiece,
3030
require_tokenizers,
@@ -340,7 +340,10 @@ def test_predict(self):
340340
self.assertTrue(np.array_equal(labels[0], trainer.eval_dataset.ys[0]))
341341
self.assertTrue(np.array_equal(labels[1], trainer.eval_dataset.ys[1]))
342342

343+
@require_datasets
343344
def test_trainer_with_datasets(self):
345+
import datasets
346+
344347
np.random.seed(42)
345348
x = np.random.normal(size=(64,)).astype(np.float32)
346349
y = 2.0 * x + 3.0 + np.random.normal(scale=0.1, size=(64,))
@@ -658,15 +661,17 @@ def model_init(trial):
658661
def hp_name(trial):
659662
return MyTrialShortNamer.shortname(trial.params)
660663

661-
trainer = get_regression_trainer(
662-
learning_rate=0.1,
663-
logging_steps=1,
664-
evaluation_strategy=EvaluationStrategy.EPOCH,
665-
num_train_epochs=4,
666-
disable_tqdm=True,
667-
load_best_model_at_end=True,
668-
logging_dir="runs",
669-
run_name="test",
670-
model_init=model_init,
671-
)
672-
trainer.hyperparameter_search(direction="minimize", hp_space=hp_space, hp_name=hp_name, n_trials=4)
664+
with tempfile.TemporaryDirectory() as tmp_dir:
665+
trainer = get_regression_trainer(
666+
output_dir=tmp_dir,
667+
learning_rate=0.1,
668+
logging_steps=1,
669+
evaluation_strategy=EvaluationStrategy.EPOCH,
670+
num_train_epochs=4,
671+
disable_tqdm=True,
672+
load_best_model_at_end=True,
673+
logging_dir="runs",
674+
run_name="test",
675+
model_init=model_init,
676+
)
677+
trainer.hyperparameter_search(direction="minimize", hp_space=hp_space, hp_name=hp_name, n_trials=4)

0 commit comments

Comments
 (0)