コード例 #1
0
def is_ipex_available():
    def get_major_and_minor_from_version(full_version):
        return str(version.parse(full_version).major) + "." + str(version.parse(full_version).minor)

    if not is_torch_available() or importlib.util.find_spec("intel_extension_for_pytorch") is None:
        return False
    _ipex_version = "N/A"
    try:
        _ipex_version = importlib_metadata.version("intel_extension_for_pytorch")
    except importlib_metadata.PackageNotFoundError:
        return False
    torch_major_and_minor = get_major_and_minor_from_version(_torch_version)
    ipex_major_and_minor = get_major_and_minor_from_version(_ipex_version)
    if torch_major_and_minor != ipex_major_and_minor:
        logger.warning(
            f"Intel Extension for PyTorch {ipex_major_and_minor} needs to work with PyTorch {ipex_major_and_minor}.*,"
            f" but PyTorch {_torch_version} is found. Please switch to the matching version and run again."
        )
        return False
    return True
コード例 #2
0
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

from transformers.testing_utils import TestCasePlus
from transformers.utils.versions import (
    importlib_metadata,
    require_version,
    require_version_core,
    require_version_examples,
)


numpy_ver = importlib_metadata.version("numpy")
python_ver = ".".join([str(x) for x in sys.version_info[:3]])


class DependencyVersionCheckTest(TestCasePlus):
    def test_core(self):
        # lt + different version strings
        require_version_core("numpy<1000.4.5")
        require_version_core("numpy<1000.4")
        require_version_core("numpy<1000")

        # le
        require_version_core("numpy<=1000.4.5")
        require_version_core(f"numpy<={numpy_ver}")

        # eq
コード例 #3
0
logger = logging.get_logger(__name__)  # pylint: disable=invalid-name

ENV_VARS_TRUE_VALUES = {"1", "ON", "YES", "TRUE"}
ENV_VARS_TRUE_AND_AUTO_VALUES = ENV_VARS_TRUE_VALUES.union({"AUTO"})

USE_TF = os.environ.get("USE_TF", "AUTO").upper()
USE_TORCH = os.environ.get("USE_TORCH", "AUTO").upper()
USE_JAX = os.environ.get("USE_FLAX", "AUTO").upper()

_torch_version = "N/A"
if USE_TORCH in ENV_VARS_TRUE_AND_AUTO_VALUES and USE_TF not in ENV_VARS_TRUE_VALUES:
    _torch_available = importlib.util.find_spec("torch") is not None
    if _torch_available:
        try:
            _torch_version = importlib_metadata.version("torch")
            logger.info(f"PyTorch version {_torch_version} available.")
        except importlib_metadata.PackageNotFoundError:
            _torch_available = False
else:
    logger.info("Disabling PyTorch because USE_TF is set")
    _torch_available = False

_tf_version = "N/A"
if USE_TF in ENV_VARS_TRUE_AND_AUTO_VALUES and USE_TORCH not in ENV_VARS_TRUE_VALUES:
    _tf_available = importlib.util.find_spec("tensorflow") is not None
    if _tf_available:
        candidates = (
            "tensorflow",
            "tensorflow-cpu",
            "tensorflow-gpu",
コード例 #4
0
def run_hp_search_sigopt(trainer, n_trials: int, direction: str,
                         **kwargs) -> BestRun:
    import sigopt
    from transformers.utils.versions import importlib_metadata

    if importlib_metadata.version("sigopt") >= "8.0.0":
        sigopt.set_project("huggingface")

        experiment = sigopt.create_experiment(
            name="huggingface-tune",
            type="offline",
            parameters=trainer.hp_space(None),
            metrics=[
                dict(name="objective",
                     objective=direction,
                     strategy="optimize")
            ],
            parallel_bandwidth=1,
            budget=n_trials,
        )

        logger.info(
            f"created experiment: https://app.sigopt.com/experiment/{experiment.id}"
        )

        for run in experiment.loop():
            with run:
                trainer.objective = None
                trainer.train(resume_from_checkpoint=None, trial=run.run)
                # If there hasn't been any evaluation during the training loop.
                if getattr(trainer, "objective", None) is None:
                    metrics = trainer.evaluate()
                    trainer.objective = trainer.compute_objective(metrics)
                run.log_metric("objective", trainer.objective)

        best = list(experiment.get_best_runs())[0]
        best_run = BestRun(best.id, best.values["objective"].value,
                           best.assignments)
    else:
        from sigopt import Connection

        conn = Connection()
        proxies = kwargs.pop("proxies", None)
        if proxies is not None:
            conn.set_proxies(proxies)

        experiment = conn.experiments().create(
            name="huggingface-tune",
            parameters=trainer.hp_space(None),
            metrics=[
                dict(name="objective",
                     objective=direction,
                     strategy="optimize")
            ],
            parallel_bandwidth=1,
            observation_budget=n_trials,
            project="huggingface",
        )
        logger.info(
            f"created experiment: https://app.sigopt.com/experiment/{experiment.id}"
        )

        while experiment.progress.observation_count < experiment.observation_budget:
            suggestion = conn.experiments(experiment.id).suggestions().create()
            trainer.objective = None
            trainer.train(resume_from_checkpoint=None, trial=suggestion)
            # If there hasn't been any evaluation during the training loop.
            if getattr(trainer, "objective", None) is None:
                metrics = trainer.evaluate()
                trainer.objective = trainer.compute_objective(metrics)

            values = [dict(name="objective", value=trainer.objective)]
            obs = conn.experiments(experiment.id).observations().create(
                suggestion=suggestion.id, values=values)
            logger.info(
                f"[suggestion_id, observation_id]: [{suggestion.id}, {obs.id}]"
            )
            experiment = conn.experiments(experiment.id).fetch()

        best = list(
            conn.experiments(
                experiment.id).best_assignments().fetch().iterate_pages())[0]
        best_run = BestRun(best.id, best.value, best.assignments)
    return best_run