|
| 1 | +# Copyright The FMS HF Tuning Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# SPDX-License-Identifier: Apache-2.0 |
| 16 | +# https://spdx.dev/learn/handling-license-info/ |
| 17 | + |
| 18 | +# Standard |
| 19 | +import copy |
| 20 | +import json |
| 21 | +import os |
| 22 | +import tempfile |
| 23 | + |
| 24 | +# Third Party |
| 25 | +from transformers.utils.import_utils import _is_package_available |
| 26 | +import pytest |
| 27 | + |
| 28 | +# First Party |
| 29 | +from tests.test_sft_trainer import ( |
| 30 | + DATA_ARGS, |
| 31 | + MODEL_ARGS, |
| 32 | + TRAIN_ARGS, |
| 33 | + _get_checkpoint_path, |
| 34 | + _test_run_inference, |
| 35 | + _validate_training, |
| 36 | +) |
| 37 | + |
| 38 | +# Local |
| 39 | +from tuning import sft_trainer |
| 40 | +from tuning.config.tracker_configs import MLflowConfig, TrackerConfigFactory |
| 41 | + |
| 42 | +mlflow_not_available = not _is_package_available("mlflow") |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.skipif(mlflow_not_available, reason="Requires mlflow to be installed") |
| 46 | +def test_run_with_mlflow_tracker_name_but_no_args(): |
| 47 | + """Ensure that train() raises error with mlflow tracker name but no args""" |
| 48 | + |
| 49 | + with tempfile.TemporaryDirectory() as tempdir: |
| 50 | + train_args = copy.deepcopy(TRAIN_ARGS) |
| 51 | + train_args.output_dir = tempdir |
| 52 | + |
| 53 | + train_args.trackers = ["mlflow"] |
| 54 | + |
| 55 | + with pytest.raises( |
| 56 | + ValueError, |
| 57 | + match="mlflow tracker requested but mlflow_uri is not specified.", |
| 58 | + ): |
| 59 | + sft_trainer.train(MODEL_ARGS, DATA_ARGS, train_args) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.skipif(mlflow_not_available, reason="Requires mlflow to be installed") |
| 63 | +def test_e2e_run_with_mlflow_tracker(): |
| 64 | + """Ensure that training succeeds with mlflow tracker""" |
| 65 | + |
| 66 | + # mlflow performs a cleanup at callback close time which happens post the |
| 67 | + # delete of this directory so we run into two issues |
| 68 | + # 1. the temp directory cannot be cleared as it has open pointer by mlflow |
| 69 | + # 2. mlflow complaints that it cannot find a run which it just created. |
| 70 | + # this is a race condition which is fixed with mkdtemp() which doesn't delete |
| 71 | + tempdir = tempfile.mkdtemp() |
| 72 | + |
| 73 | + train_args = copy.deepcopy(TRAIN_ARGS) |
| 74 | + train_args.output_dir = tempdir |
| 75 | + |
| 76 | + # This should not mean file logger is not present. |
| 77 | + # code will add it by default |
| 78 | + # The below validate_training check will test for that too. |
| 79 | + train_args.trackers = ["mlflow"] |
| 80 | + |
| 81 | + mlflow_path = os.path.join(tempdir, "mlflow") |
| 82 | + |
| 83 | + tracker_configs = TrackerConfigFactory( |
| 84 | + mlflow_config=MLflowConfig( |
| 85 | + mlflow_experiment="unit_test", |
| 86 | + mlflow_tracking_uri=f"file://{mlflow_path}", |
| 87 | + ) |
| 88 | + ) |
| 89 | + |
| 90 | + sft_trainer.train( |
| 91 | + MODEL_ARGS, DATA_ARGS, train_args, tracker_configs=tracker_configs |
| 92 | + ) |
| 93 | + |
| 94 | + # validate ft tuning configs |
| 95 | + _validate_training(tempdir) |
| 96 | + |
| 97 | + assert os.path.exists(mlflow_path) and os.path.isdir(mlflow_path) |
| 98 | + |
| 99 | + # validate inference |
| 100 | + _test_run_inference(checkpoint_path=_get_checkpoint_path(tempdir)) |
| 101 | + |
| 102 | + |
| 103 | +@pytest.mark.skipif(mlflow_not_available, reason="Requires mlflow to be installed") |
| 104 | +def test_e2e_run_with_mlflow_runuri_export_default_path(): |
| 105 | + """Ensure that mlflow outputs run uri in the output dir by default""" |
| 106 | + |
| 107 | + tempdir = tempfile.mkdtemp() |
| 108 | + train_args = copy.deepcopy(TRAIN_ARGS) |
| 109 | + train_args.output_dir = tempdir |
| 110 | + |
| 111 | + train_args.trackers = ["mlflow"] |
| 112 | + |
| 113 | + mlflow_path = os.path.join(tempdir, "mlflow") |
| 114 | + |
| 115 | + tracker_configs = TrackerConfigFactory( |
| 116 | + mlflow_config=MLflowConfig( |
| 117 | + mlflow_experiment="unit_test", |
| 118 | + mlflow_tracking_uri=f"file://{mlflow_path}", |
| 119 | + ) |
| 120 | + ) |
| 121 | + |
| 122 | + sft_trainer.train( |
| 123 | + MODEL_ARGS, DATA_ARGS, train_args, tracker_configs=tracker_configs |
| 124 | + ) |
| 125 | + |
| 126 | + # validate ft tuning configs |
| 127 | + _validate_training(tempdir) |
| 128 | + |
| 129 | + assert os.path.exists(mlflow_path) and os.path.isdir(mlflow_path) |
| 130 | + |
| 131 | + run_uri_file = os.path.join(tempdir, "mlflow_tracker.json") |
| 132 | + |
| 133 | + assert os.path.exists(run_uri_file) is True |
| 134 | + assert os.path.getsize(run_uri_file) > 0 |
| 135 | + |
| 136 | + with open(run_uri_file, "r", encoding="utf-8") as f: |
| 137 | + content = json.loads(f.read()) |
| 138 | + assert "run_uri" in content |
0 commit comments