id
int64
0
190k
prompt
stringlengths
21
13.4M
docstring
stringlengths
1
12k
18,428
from typing import Dict, List, Tuple from tokenizers import Regex, Tokenizer, decoders, normalizers, pre_tokenizers, processors from tokenizers.models import BPE, Unigram, WordPiece from .file_utils import requires_backends def check_number_comma(piece: str) -> bool: return len(piece) < 2 or piece[-1] != "," or not piece[-2].isdigit()
null
18,429
from typing import Dict, List, Tuple from tokenizers import Regex, Tokenizer, decoders, normalizers, pre_tokenizers, processors from tokenizers.models import BPE, Unigram, WordPiece from .file_utils import requires_backends SLOW_TO_FAST_CONVERTERS = { "SbertTokenizer": BertConverter, } The provided code snippet includes necessary dependencies for implementing the `convert_slow_tokenizer` function. Write a Python function `def convert_slow_tokenizer(transformer_tokenizer) -> Tokenizer` to solve the following problem: Utilities to convert a slow tokenizer instance in a fast tokenizer instance. Args: transformer_tokenizer ([`~tokenization_utils_base.PreTrainedTokenizer`]): Instance of a slow tokenizer to convert in the backend tokenizer for [`~tokenization_utils_base.PreTrainedTokenizerFast`]. Return: A instance of [`~tokenizers.Tokenizer`] to be used as the backend tokenizer of a [`~tokenization_utils_base.PreTrainedTokenizerFast`] Here is the function: def convert_slow_tokenizer(transformer_tokenizer) -> Tokenizer: """ Utilities to convert a slow tokenizer instance in a fast tokenizer instance. Args: transformer_tokenizer ([`~tokenization_utils_base.PreTrainedTokenizer`]): Instance of a slow tokenizer to convert in the backend tokenizer for [`~tokenization_utils_base.PreTrainedTokenizerFast`]. Return: A instance of [`~tokenizers.Tokenizer`] to be used as the backend tokenizer of a [`~tokenization_utils_base.PreTrainedTokenizerFast`] """ tokenizer_class_name = transformer_tokenizer.__class__.__name__ if tokenizer_class_name not in SLOW_TO_FAST_CONVERTERS: raise ValueError( f"An instance of tokenizer class {tokenizer_class_name} cannot be converted in a Fast tokenizer instance. " f"No converter was found. Currently available slow->fast convertors: {list(SLOW_TO_FAST_CONVERTERS.keys())}" ) converter_class = SLOW_TO_FAST_CONVERTERS[tokenizer_class_name] return converter_class(transformer_tokenizer).converted()
Utilities to convert a slow tokenizer instance in a fast tokenizer instance. Args: transformer_tokenizer ([`~tokenization_utils_base.PreTrainedTokenizer`]): Instance of a slow tokenizer to convert in the backend tokenizer for [`~tokenization_utils_base.PreTrainedTokenizerFast`]. Return: A instance of [`~tokenizers.Tokenizer`] to be used as the backend tokenizer of a [`~tokenization_utils_base.PreTrainedTokenizerFast`]
18,430
import argparse import os from ... import models from .convert_slow_tokenizer import SLOW_TO_FAST_CONVERTERS from .utils import logging logger = logging.get_logger(__name__) TOKENIZER_CLASSES = {name: getattr(models, name + "Fast") for name in SLOW_TO_FAST_CONVERTERS} def convert_slow_checkpoint_to_fast(tokenizer_name, checkpoint_name, dump_path, force_download): if tokenizer_name is not None and tokenizer_name not in TOKENIZER_CLASSES: raise ValueError(f"Unrecognized tokenizer name, should be one of {list(TOKENIZER_CLASSES.keys())}.") if tokenizer_name is None: tokenizer_names = TOKENIZER_CLASSES else: tokenizer_names = {tokenizer_name: getattr(models, tokenizer_name + "Fast")} logger.info(f"Loading tokenizer classes: {tokenizer_names}") for tokenizer_name in tokenizer_names: tokenizer_class = TOKENIZER_CLASSES[tokenizer_name] add_prefix = True if checkpoint_name is None: checkpoint_names = list(tokenizer_class.max_model_input_sizes.keys()) else: checkpoint_names = [checkpoint_name] logger.info(f"For tokenizer {tokenizer_class.__class__.__name__} loading checkpoints: {checkpoint_names}") for checkpoint in checkpoint_names: logger.info(f"Loading {tokenizer_class.__class__.__name__} {checkpoint}") # Load tokenizer tokenizer = tokenizer_class.from_pretrained(checkpoint, force_download=force_download) # Save fast tokenizer logger.info(f"Save fast tokenizer to {dump_path} with prefix {checkpoint} add_prefix {add_prefix}") # For organization names we create sub-directories if "/" in checkpoint: checkpoint_directory, checkpoint_prefix_name = checkpoint.split("/") dump_path_full = os.path.join(dump_path, checkpoint_directory) elif add_prefix: checkpoint_prefix_name = checkpoint dump_path_full = dump_path else: checkpoint_prefix_name = None dump_path_full = dump_path logger.info(f"=> {dump_path_full} with prefix {checkpoint_prefix_name}, add_prefix {add_prefix}") if checkpoint in list(tokenizer.pretrained_vocab_files_map.values())[0]: file_path = list(tokenizer.pretrained_vocab_files_map.values())[0][checkpoint] next_char = file_path.split(checkpoint)[-1][0] if next_char == "/": dump_path_full = os.path.join(dump_path_full, checkpoint_prefix_name) checkpoint_prefix_name = None logger.info(f"=> {dump_path_full} with prefix {checkpoint_prefix_name}, add_prefix {add_prefix}") file_names = tokenizer.save_pretrained( dump_path_full, legacy_format=False, filename_prefix=checkpoint_prefix_name ) logger.info(f"=> File names {file_names}") for file_name in file_names: if not file_name.endswith("tokenizer.json"): os.remove(file_name) logger.info(f"=> removing {file_name}")
null
18,431
import os from pathlib import Path from typing import Any, Dict, List, Optional, Union from dataclasses import dataclass from .auto.modeling_auto import ( MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, ) from .file_utils import ( is_datasets_available, is_tf_available, is_tokenizers_available, is_torch_available, ) from .training_args import ParallelMode from .utils import logging def _listify(obj): if obj is None: return [] elif isinstance(obj, str): return [obj] else: return obj
null
18,432
import os from pathlib import Path from typing import Any, Dict, List, Optional, Union from dataclasses import dataclass from .auto.modeling_auto import ( MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, ) from .file_utils import ( is_datasets_available, is_tf_available, is_tokenizers_available, is_torch_available, ) from .training_args import ParallelMode from .utils import logging def _insert_values_as_list(metadata, name, values): if values is None: return metadata if isinstance(values, str): values = [values] values = [v for v in values if v is not None] if len(values) == 0: return metadata metadata[name] = values return metadata
null
18,433
import os from pathlib import Path from typing import Any, Dict, List, Optional, Union from dataclasses import dataclass from .auto.modeling_auto import ( MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, ) from .file_utils import ( is_datasets_available, is_tf_available, is_tokenizers_available, is_torch_available, ) from .training_args import ParallelMode from .utils import logging METRIC_TAGS = [ "accuracy", "bleu", "f1", "matthews_correlation", "pearsonr", "precision", "recall", "rouge", "sacrebleu", "spearmanr", ] def infer_metric_tags_from_eval_results(eval_results): if eval_results is None: return {} result = {} for key in eval_results.keys(): if key.lower().replace(" ", "_") in METRIC_TAGS: result[key.lower().replace(" ", "_")] = key elif key.lower() == "rouge1": result["rouge"] = key return result
null
18,434
import os from pathlib import Path from typing import Any, Dict, List, Optional, Union from dataclasses import dataclass from .auto.modeling_auto import ( MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, ) from .file_utils import ( is_datasets_available, is_tf_available, is_tokenizers_available, is_torch_available, ) from .training_args import ParallelMode from .utils import logging def _insert_value(metadata, name, value): if value is None: return metadata metadata[name] = value return metadata
null
18,435
import os from pathlib import Path from typing import Any, Dict, List, Optional, Union from dataclasses import dataclass from .auto.modeling_auto import ( MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, ) from .file_utils import ( is_datasets_available, is_tf_available, is_tokenizers_available, is_torch_available, ) from .training_args import ParallelMode from .utils import logging def is_datasets_available(): return _datasets_available def is_hf_dataset(dataset): if not is_datasets_available(): return False from datasets import Dataset return isinstance(dataset, Dataset)
null
18,436
import os from pathlib import Path from typing import Any, Dict, List, Optional, Union from dataclasses import dataclass from .auto.modeling_auto import ( MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, ) from .file_utils import ( is_datasets_available, is_tf_available, is_tokenizers_available, is_torch_available, ) from .training_args import ParallelMode from .utils import logging def _get_mapping_values(mapping): result = [] for v in mapping.values(): if isinstance(v, (tuple, list)): result += list(v) else: result.append(v) return result
null
18,437
import os from pathlib import Path from typing import Any, Dict, List, Optional, Union from dataclasses import dataclass from .auto.modeling_auto import ( MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, ) from .file_utils import ( is_datasets_available, is_tf_available, is_tokenizers_available, is_torch_available, ) from .training_args import ParallelMode from .utils import logging The provided code snippet includes necessary dependencies for implementing the `parse_keras_history` function. Write a Python function `def parse_keras_history(logs)` to solve the following problem: Parse the `logs` of either a `tf.keras.History` object returned by `model.fit()` or an accumulated logs `dict` passed to the `PushToHubCallback`. Returns lines and logs compatible with those returned by `parse_log_history`. Here is the function: def parse_keras_history(logs): """ Parse the `logs` of either a `tf.keras.History` object returned by `model.fit()` or an accumulated logs `dict` passed to the `PushToHubCallback`. Returns lines and logs compatible with those returned by `parse_log_history`. """ if hasattr(logs, "history"): # This looks like a `History` object logs.history["epoch"] = logs.epoch logs = logs.history else: # Training logs is a list of dicts, let's invert it to a dict of lists to match a History object logs = {log_key: [single_dict[log_key] for single_dict in logs] for log_key in logs[0]} lines = [] for i in range(len(logs["epoch"])): epoch_dict = {log_key: log_value_list[i] for log_key, log_value_list in logs.items()} values = dict() for k, v in epoch_dict.items(): if k.startswith("val_"): k = "validation_" + k[4:] elif k != "epoch": k = "train_" + k splits = k.split("_") name = " ".join([part.capitalize() for part in splits]) values[name] = v lines.append(values) eval_results = lines[-1] return logs, lines, eval_results
Parse the `logs` of either a `tf.keras.History` object returned by `model.fit()` or an accumulated logs `dict` passed to the `PushToHubCallback`. Returns lines and logs compatible with those returned by `parse_log_history`.
18,438
import os from pathlib import Path from typing import Any, Dict, List, Optional, Union from dataclasses import dataclass from .auto.modeling_auto import ( MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, ) from .file_utils import ( is_datasets_available, is_tf_available, is_tokenizers_available, is_torch_available, ) from .training_args import ParallelMode from .utils import logging The provided code snippet includes necessary dependencies for implementing the `parse_log_history` function. Write a Python function `def parse_log_history(log_history)` to solve the following problem: Parse the `log_history` of a Trainer to get the intermediate and final evaluation results. Here is the function: def parse_log_history(log_history): """ Parse the `log_history` of a Trainer to get the intermediate and final evaluation results. """ idx = 0 while idx < len(log_history) and "train_runtime" not in log_history[idx]: idx += 1 # If there are no training logs if idx == len(log_history): idx -= 1 while idx >= 0 and "eval_loss" not in log_history[idx]: idx -= 1 if idx > 0: return None, None, log_history[idx] else: return None, None, None # From now one we can assume we have training logs: train_log = log_history[idx] lines = [] training_loss = "No log" for i in range(idx): if "loss" in log_history[i]: training_loss = log_history[i]["loss"] if "eval_loss" in log_history[i]: metrics = log_history[i].copy() _ = metrics.pop("total_flos", None) epoch = metrics.pop("epoch", None) step = metrics.pop("step", None) _ = metrics.pop("eval_runtime", None) _ = metrics.pop("eval_samples_per_second", None) _ = metrics.pop("eval_steps_per_second", None) values = {"Training Loss": training_loss, "Epoch": epoch, "Step": step} for k, v in metrics.items(): if k == "eval_loss": values["Validation Loss"] = v else: splits = k.split("_") name = " ".join([part.capitalize() for part in splits[1:]]) values[name] = v lines.append(values) idx = len(log_history) - 1 while idx >= 0 and "eval_loss" not in log_history[idx]: idx -= 1 if idx > 0: eval_results = {} for key, value in log_history[idx].items(): if key.startswith("eval_"): key = key[5:] if key not in ["runtime", "samples_per_second", "steps_per_second", "epoch", "step"]: camel_cased_key = " ".join([part.capitalize() for part in key.split("_")]) eval_results[camel_cased_key] = value return train_log, lines, eval_results else: return train_log, lines, None
Parse the `log_history` of a Trainer to get the intermediate and final evaluation results.
18,439
import os from pathlib import Path from typing import Any, Dict, List, Optional, Union from dataclasses import dataclass from .auto.modeling_auto import ( MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, ) from .file_utils import ( is_datasets_available, is_tf_available, is_tokenizers_available, is_torch_available, ) from .training_args import ParallelMode from .utils import logging def extract_hyperparameters_from_keras(model): import tensorflow as tf hyperparameters = dict() if hasattr(model, "optimizer") and model.optimizer is not None: hyperparameters["optimizer"] = model.optimizer.get_config() else: hyperparameters["optimizer"] = None hyperparameters["training_precision"] = tf.keras.mixed_precision.global_policy().name return hyperparameters
null
18,440
import os from pathlib import Path from typing import Any, Dict, List, Optional, Union from dataclasses import dataclass from .auto.modeling_auto import ( MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, ) from .file_utils import ( is_datasets_available, is_tf_available, is_tokenizers_available, is_torch_available, ) from .training_args import ParallelMode from .utils import logging def _maybe_round(v, decimals=4): if isinstance(v, float) and len(str(v).split(".")) > 1 and len(str(v).split(".")[1]) > decimals: return f"{v:.{decimals}f}" return str(v) def _regular_table_line(values, col_widths): values_with_space = [f"| {v}" + " " * (w - len(v) + 1) for v, w in zip(values, col_widths)] return "".join(values_with_space) + "|\n" def _second_table_line(col_widths): values = ["|:" + "-" * w + ":" for w in col_widths] return "".join(values) + "|\n" The provided code snippet includes necessary dependencies for implementing the `make_markdown_table` function. Write a Python function `def make_markdown_table(lines)` to solve the following problem: Create a nice Markdown table from the results in `lines`. Here is the function: def make_markdown_table(lines): """ Create a nice Markdown table from the results in `lines`. """ if lines is None or len(lines) == 0: return "" col_widths = {key: len(str(key)) for key in lines[0].keys()} for line in lines: for key, value in line.items(): if col_widths[key] < len(_maybe_round(value)): col_widths[key] = len(_maybe_round(value)) table = _regular_table_line(list(lines[0].keys()), list(col_widths.values())) table += _second_table_line(list(col_widths.values())) for line in lines: table += _regular_table_line([_maybe_round(v) for v in line.values()], list(col_widths.values())) return table
Create a nice Markdown table from the results in `lines`.
18,441
import os from pathlib import Path from typing import Any, Dict, List, Optional, Union from dataclasses import dataclass from .auto.modeling_auto import ( MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, ) from .file_utils import ( is_datasets_available, is_tf_available, is_tokenizers_available, is_torch_available, ) from .training_args import ParallelMode from .utils import logging _TRAINING_ARGS_KEYS = [ "learning_rate", "train_batch_size", "eval_batch_size", "seed", ] class ParallelMode(Enum): def extract_hyperparameters_from_trainer(trainer): hyperparameters = {k: getattr(trainer.args, k) for k in _TRAINING_ARGS_KEYS} if trainer.args.parallel_mode not in [ParallelMode.NOT_PARALLEL, ParallelMode.NOT_DISTRIBUTED]: hyperparameters["distributed_type"] = ( "multi-GPU" if trainer.args.parallel_mode == ParallelMode.DISTRIBUTED else trainer.args.parallel_mode.value ) if trainer.args.world_size > 1: hyperparameters["num_devices"] = trainer.args.world_size if trainer.args.gradient_accumulation_steps > 1: hyperparameters["gradient_accumulation_steps"] = trainer.args.gradient_accumulation_steps total_train_batch_size = ( trainer.args.train_batch_size * trainer.args.world_size * trainer.args.gradient_accumulation_steps ) if total_train_batch_size != hyperparameters["train_batch_size"]: hyperparameters["total_train_batch_size"] = total_train_batch_size total_eval_batch_size = trainer.args.eval_batch_size * trainer.args.world_size if total_eval_batch_size != hyperparameters["eval_batch_size"]: hyperparameters["total_eval_batch_size"] = total_eval_batch_size if trainer.args.adafactor: hyperparameters["optimizer"] = "Adafactor" else: hyperparameters[ "optimizer" ] = f"Adam with betas=({trainer.args.adam_beta1},{trainer.args.adam_beta2}) and epsilon={trainer.args.adam_epsilon}" hyperparameters["lr_scheduler_type"] = trainer.args.lr_scheduler_type.value if trainer.args.warmup_ratio != 0.0: hyperparameters["lr_scheduler_warmup_ratio"] = trainer.args.warmup_ratio if trainer.args.warmup_steps != 0.0: hyperparameters["lr_scheduler_warmup_steps"] = trainer.args.warmup_steps if trainer.args.max_steps != -1: hyperparameters["training_steps"] = trainer.args.max_steps else: hyperparameters["num_epochs"] = trainer.args.num_train_epochs if trainer.args.fp16: if trainer.use_amp: hyperparameters["mixed_precision_training"] = "Native AMP" elif trainer.use_apex: hyperparameters["mixed_precision_training"] = f"Apex, opt level {trainer.args.fp16_opt_level}" if trainer.args.label_smoothing_factor != 0.0: hyperparameters["label_smoothing_factor"] = trainer.args.label_smoothing_factor return hyperparameters
null
18,442
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging _pyctcdecode_available = importlib.util.find_spec("pyctcdecode") is not None def is_pyctcdecode_available(): return _pyctcdecode_available
null
18,443
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging _librosa_available = importlib.util.find_spec("librosa") is not None def is_librosa_available(): return _librosa_available
null
18,444
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_torch_available(): def is_torch_cuda_available(): if is_torch_available(): import torch return torch.cuda.is_available() else: return False
null
18,445
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_torch_available(): return _torch_available def is_torch_bf16_available(): if not is_torch_available(): return False import torch # since currently no utility function is available we build our own. # some bits come from https://github.com/pytorch/pytorch/blob/2289a12f21c54da93bf5d696e3f9aea83dd9c10d/torch/testing/_internal/common_cuda.py#L51 # with additional check for torch version # to succeed: # 1. the hardware needs to support bf16 (arch >= Ampere) # 2. torch >= 1.10 (1.9 should be enough for AMP API has changed in 1.10, so using 1.10 as minimal) # 3. CUDA >= 11 # 4. torch.autocast exists # XXX: one problem here is that it may give invalid results on mixed gpus setup, so it's # really only correct for the 0th gpu (or currently set default device if different from 0) if not torch.cuda.is_available() or torch.version.cuda is None: return False if torch.cuda.get_device_properties(torch.cuda.current_device()).major < 8: return False if int(torch.version.cuda.split(".")[0]) < 11: return False if version.parse(torch.__version__) < version.parse("1.10"): return False if not hasattr(torch, "autocast"): return False return True
null
18,446
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_torch_available(): return _torch_available def is_torch_tf32_available(): if not is_torch_available(): return False import torch if not torch.cuda.is_available() or torch.version.cuda is None: return False if torch.cuda.get_device_properties(torch.cuda.current_device()).major < 8: return False if int(torch.version.cuda.split(".")[0]) < 11: return False if version.parse(torch.__version__) < version.parse("1.7"): return False return True
null
18,447
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_torch_onnx_dict_inputs_support_available(): return _torch_onnx_dict_inputs_support_available
null
18,448
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging try: _coloredlogs_available = importlib_metadata.version("coloredlogs") logger.debug(f"Successfully imported sympy version {_coloredlogs_available}") except importlib_metadata.PackageNotFoundError: _coloredlogs_available = False def is_coloredlogs_available(): return _coloredlogs_available
null
18,449
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging _keras2onnx_available = importlib.util.find_spec("keras2onnx") is not None def is_keras2onnx_available(): return _keras2onnx_available
null
18,450
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging _onnx_available = importlib.util.find_spec("onnxruntime") is not None def is_onnx_available(): return _onnx_available
null
18,451
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging _detectron2_available = importlib.util.find_spec("detectron2") is not None def is_detectron2_available(): return _detectron2_available
null
18,452
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_rjieba_available(): return importlib.util.find_spec("rjieba") is not None
null
18,453
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_psutil_available(): return importlib.util.find_spec("psutil") is not None
null
18,454
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_py3nvml_available(): return importlib.util.find_spec("py3nvml") is not None
null
18,455
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_apex_available(): return importlib.util.find_spec("apex") is not None
null
18,456
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging _faiss_available = importlib.util.find_spec("faiss") is not None def is_faiss_available(): return _faiss_available
null
18,457
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_scipy_available(): return importlib.util.find_spec("scipy") is not None def is_sklearn_available(): if importlib.util.find_spec("sklearn") is None: return False return is_scipy_available() and importlib.util.find_spec("sklearn.metrics")
null
18,458
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_sentencepiece_available(): return importlib.util.find_spec("sentencepiece") is not None
null
18,459
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_protobuf_available(): if importlib.util.find_spec("google") is None: return False return importlib.util.find_spec("google.protobuf") is not None
null
18,460
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_tokenizers_available(): return importlib.util.find_spec("tokenizers") is not None
null
18,461
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_vision_available(): return importlib.util.find_spec("PIL") is not None
null
18,462
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_pytesseract_available(): return importlib.util.find_spec("pytesseract") is not None
null
18,463
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging if ( os.path.isdir(old_default_cache_path) and not os.path.isdir(default_cache_path) and "PYTORCH_PRETRAINED_BERT_CACHE" not in os.environ and "PYTORCH_TRANSFORMERS_CACHE" not in os.environ and "TRANSFORMERS_CACHE" not in os.environ ): shutil.move(old_default_cache_path, default_cache_path) def is_in_notebook(): try: # Test adapted from tqdm.autonotebook: https://github.com/tqdm/tqdm/blob/master/tqdm/autonotebook.py get_ipython = sys.modules["IPython"].get_ipython if "IPKernelApp" not in get_ipython().config: raise ImportError("console") if "VSCODE_PID" in os.environ: raise ImportError("vscode") return importlib.util.find_spec("IPython") is not None except (AttributeError, ImportError, KeyError): return False
null
18,464
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging _scatter_available = importlib.util.find_spec("torch_scatter") is not None def is_scatter_available(): return _scatter_available
null
18,465
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging _pytorch_quantization_available = importlib.util.find_spec("pytorch_quantization") is not None def is_pytorch_quantization_available(): return _pytorch_quantization_available
null
18,466
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging _tensorflow_probability_available = importlib.util.find_spec("tensorflow_probability") is not None def is_tensorflow_probability_available(): return _tensorflow_probability_available
null
18,467
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_pandas_available(): return importlib.util.find_spec("pandas") is not None
null
18,468
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging if ( os.path.isdir(old_default_cache_path) and not os.path.isdir(default_cache_path) and "PYTORCH_PRETRAINED_BERT_CACHE" not in os.environ and "PYTORCH_TRANSFORMERS_CACHE" not in os.environ and "TRANSFORMERS_CACHE" not in os.environ ): shutil.move(old_default_cache_path, default_cache_path) def is_sagemaker_mp_enabled(): # Get the sagemaker specific mp parameters from smp_options variable. smp_options = os.getenv("SM_HP_MP_PARAMETERS", "{}") try: # Parse it and check the field "partitions" is included, it is required for model parallel. smp_options = json.loads(smp_options) if "partitions" not in smp_options: return False except json.JSONDecodeError: return False # Get the sagemaker specific framework parameters from mpi_options variable. mpi_options = os.getenv("SM_FRAMEWORK_PARAMS", "{}") try: # Parse it and check the field "sagemaker_distributed_dataparallel_enabled". mpi_options = json.loads(mpi_options) if not mpi_options.get("sagemaker_mpi_enabled", False): return False except json.JSONDecodeError: return False # Lastly, check if the `smdistributed` module is present. return importlib.util.find_spec("smdistributed") is not None
null
18,469
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging _soundfile_available = importlib.util.find_spec("soundfile") is not None def is_soundfile_availble(): return _soundfile_available
null
18,470
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging _timm_available = importlib.util.find_spec("timm") is not None def is_timm_available(): return _timm_available
null
18,471
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging _torchaudio_available = importlib.util.find_spec("torchaudio") is not None def is_torchaudio_available(): return _torchaudio_available
null
18,472
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging _torchaudio_available = importlib.util.find_spec("torchaudio") is not None def is_speech_available(): # For now this depends on torchaudio but the exact dependency might evolve in the future. return _torchaudio_available
null
18,473
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging _phonemizer_available = importlib.util.find_spec("phonemizer") is not None def is_phonemizer_available(): return _phonemizer_available
null
18,474
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging if _torch_available: torch_version = version.parse(importlib_metadata.version("torch")) _torch_fx_available = (torch_version.major, torch_version.minor) == ( TORCH_FX_REQUIRED_VERSION.major, TORCH_FX_REQUIRED_VERSION.minor, ) _torch_onnx_dict_inputs_support_available = torch_version >= TORCH_ONNX_DICT_INPUTS_MINIMUM_VERSION def torch_only_method(fn): def wrapper(*args, **kwargs): if not _torch_available: raise ImportError( "You need to install pytorch to use this method or class, " "or activate it with environment variables USE_TORCH=1 and USE_TF=0." ) else: return fn(*args, **kwargs) return wrapper
null
18,475
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging BACKENDS_MAPPING = OrderedDict( [ ("datasets", (is_datasets_available, DATASETS_IMPORT_ERROR)), ("detectron2", (is_detectron2_available, DETECTRON2_IMPORT_ERROR)), ("faiss", (is_faiss_available, FAISS_IMPORT_ERROR)), ("flax", (is_flax_available, FLAX_IMPORT_ERROR)), ("pandas", (is_pandas_available, PANDAS_IMPORT_ERROR)), ("phonemizer", (is_phonemizer_available, PHONEMIZER_IMPORT_ERROR)), ("protobuf", (is_protobuf_available, PROTOBUF_IMPORT_ERROR)), ("pyctcdecode", (is_pyctcdecode_available, PYCTCDECODE_IMPORT_ERROR)), ("pytesseract", (is_pytesseract_available, PYTESSERACT_IMPORT_ERROR)), ("scatter", (is_scatter_available, SCATTER_IMPORT_ERROR)), ("pytorch_quantization", (is_pytorch_quantization_available, PYTORCH_QUANTIZATION_IMPORT_ERROR)), ("sentencepiece", (is_sentencepiece_available, SENTENCEPIECE_IMPORT_ERROR)), ("sklearn", (is_sklearn_available, SKLEARN_IMPORT_ERROR)), ("speech", (is_speech_available, SPEECH_IMPORT_ERROR)), ("tensorflow_probability", (is_tensorflow_probability_available, TENSORFLOW_PROBABILITY_IMPORT_ERROR)), ("tf", (is_tf_available, TENSORFLOW_IMPORT_ERROR)), ("timm", (is_timm_available, TIMM_IMPORT_ERROR)), ("tokenizers", (is_tokenizers_available, TOKENIZERS_IMPORT_ERROR)), ("torch", (is_torch_available, PYTORCH_IMPORT_ERROR)), ("vision", (is_vision_available, VISION_IMPORT_ERROR)), ("scipy", (is_scipy_available, SCIPY_IMPORT_ERROR)), ] ) def requires_backends(obj, backends): if not isinstance(backends, (list, tuple)): backends = [backends] name = obj.__name__ if hasattr(obj, "__name__") else obj.__class__.__name__ if not all(BACKENDS_MAPPING[backend][0]() for backend in backends): raise ImportError("".join([BACKENDS_MAPPING[backend][1].format(name) for backend in backends]))
null
18,476
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def add_start_docstrings(*docstr): def docstring_decorator(fn): fn.__doc__ = "".join(docstr) + (fn.__doc__ if fn.__doc__ is not None else "") return fn return docstring_decorator
null
18,477
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def add_start_docstrings_to_model_forward(*docstr): def docstring_decorator(fn): docstring = "".join(docstr) + (fn.__doc__ if fn.__doc__ is not None else "") class_name = f"[`{fn.__qualname__.split('.')[0]}`]" intro = f" The {class_name} forward method, overrides the `__call__` special method." note = r""" <Tip> Although the recipe for forward pass needs to be defined within this function, one should call the [`Module`] instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them. </Tip> """ fn.__doc__ = intro + note + docstring return fn return docstring_decorator
null
18,478
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def add_end_docstrings(*docstr): def docstring_decorator(fn): fn.__doc__ = fn.__doc__ + "".join(docstr) return fn return docstring_decorator
null
18,479
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def _prepare_output_docstrings(output_type, config_class, min_indent=None): """ Prepares the return part of the docstring using `output_type`. """ output_docstring = output_type.__doc__ # Remove the head of the docstring to keep the list of args only lines = output_docstring.split("\n") i = 0 while i < len(lines) and re.search(r"^\s*(Args|Parameters):\s*$", lines[i]) is None: i += 1 if i < len(lines): params_docstring = "\n".join(lines[(i + 1) :]) params_docstring = _convert_output_args_doc(params_docstring) # Add the return introduction full_output_type = f"{output_type.__module__}.{output_type.__name__}" intro = TF_RETURN_INTRODUCTION if output_type.__name__.startswith("TF") else PT_RETURN_INTRODUCTION intro = intro.format(full_output_type=full_output_type, config_class=config_class) result = intro + params_docstring # Apply minimum indent if necessary if min_indent is not None: lines = result.split("\n") # Find the indent of the first nonempty line i = 0 while len(lines[i]) == 0: i += 1 indent = len(_get_indent(lines[i])) # If too small, add indentation to all nonempty lines if indent < min_indent: to_add = " " * (min_indent - indent) lines = [(f"{to_add}{line}" if len(line) > 0 else line) for line in lines] result = "\n".join(lines) return result PT_SAMPLE_DOCSTRINGS = { "SequenceClassification": PT_SEQUENCE_CLASSIFICATION_SAMPLE, "QuestionAnswering": PT_QUESTION_ANSWERING_SAMPLE, "TokenClassification": PT_TOKEN_CLASSIFICATION_SAMPLE, "MultipleChoice": PT_MULTIPLE_CHOICE_SAMPLE, "MaskedLM": PT_MASKED_LM_SAMPLE, "LMHead": PT_CAUSAL_LM_SAMPLE, "BaseModel": PT_BASE_MODEL_SAMPLE, "SpeechBaseModel": PT_SPEECH_BASE_MODEL_SAMPLE, "CTC": PT_SPEECH_CTC_SAMPLE, "AudioClassification": PT_SPEECH_SEQ_CLASS_SAMPLE, "AudioFrameClassification": PT_SPEECH_FRAME_CLASS_SAMPLE, "AudioXVector": PT_SPEECH_XVECTOR_SAMPLE, } TF_SAMPLE_DOCSTRINGS = { "SequenceClassification": TF_SEQUENCE_CLASSIFICATION_SAMPLE, "QuestionAnswering": TF_QUESTION_ANSWERING_SAMPLE, "TokenClassification": TF_TOKEN_CLASSIFICATION_SAMPLE, "MultipleChoice": TF_MULTIPLE_CHOICE_SAMPLE, "MaskedLM": TF_MASKED_LM_SAMPLE, "LMHead": TF_CAUSAL_LM_SAMPLE, "BaseModel": TF_BASE_MODEL_SAMPLE, } FLAX_SAMPLE_DOCSTRINGS = { "SequenceClassification": FLAX_SEQUENCE_CLASSIFICATION_SAMPLE, "QuestionAnswering": FLAX_QUESTION_ANSWERING_SAMPLE, "TokenClassification": FLAX_TOKEN_CLASSIFICATION_SAMPLE, "MultipleChoice": FLAX_MULTIPLE_CHOICE_SAMPLE, "MaskedLM": FLAX_MASKED_LM_SAMPLE, "BaseModel": FLAX_BASE_MODEL_SAMPLE, "LMHead": FLAX_CAUSAL_LM_SAMPLE, } def add_code_sample_docstrings( *docstr, processor_class=None, checkpoint=None, output_type=None, config_class=None, mask=None, model_cls=None, modality=None ): def docstring_decorator(fn): # model_class defaults to function's class if not specified otherwise model_class = fn.__qualname__.split(".")[0] if model_cls is None else model_cls if model_class[:2] == "TF": sample_docstrings = TF_SAMPLE_DOCSTRINGS elif model_class[:4] == "Flax": sample_docstrings = FLAX_SAMPLE_DOCSTRINGS else: sample_docstrings = PT_SAMPLE_DOCSTRINGS doc_kwargs = dict(model_class=model_class, processor_class=processor_class, checkpoint=checkpoint) if "SequenceClassification" in model_class and modality == "audio": code_sample = sample_docstrings["AudioClassification"] elif "SequenceClassification" in model_class: code_sample = sample_docstrings["SequenceClassification"] elif "QuestionAnswering" in model_class: code_sample = sample_docstrings["QuestionAnswering"] elif "TokenClassification" in model_class: code_sample = sample_docstrings["TokenClassification"] elif "MultipleChoice" in model_class: code_sample = sample_docstrings["MultipleChoice"] elif "MaskedLM" in model_class or model_class in ["FlaubertWithLMHeadModel", "XLMWithLMHeadModel"]: doc_kwargs["mask"] = "[MASK]" if mask is None else mask code_sample = sample_docstrings["MaskedLM"] elif "LMHead" in model_class or "CausalLM" in model_class: code_sample = sample_docstrings["LMHead"] elif "CTC" in model_class: code_sample = sample_docstrings["CTC"] elif "AudioFrameClassification" in model_class: code_sample = sample_docstrings["AudioFrameClassification"] elif "XVector" in model_class and modality == "audio": code_sample = sample_docstrings["AudioXVector"] elif "Model" in model_class and modality == "audio": code_sample = sample_docstrings["SpeechBaseModel"] elif "Model" in model_class or "Encoder" in model_class: code_sample = sample_docstrings["BaseModel"] else: raise ValueError(f"Docstring can't be built for model {model_class}") func_doc = (fn.__doc__ or "") + "".join(docstr) output_doc = "" if output_type is None else _prepare_output_docstrings(output_type, config_class) built_doc = code_sample.format(**doc_kwargs) fn.__doc__ = func_doc + output_doc + built_doc return fn return docstring_decorator
null
18,480
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def _get_indent(t): def _prepare_output_docstrings(output_type, config_class, min_indent=None): def replace_return_docstrings(output_type=None, config_class=None): def docstring_decorator(fn): func_doc = fn.__doc__ lines = func_doc.split("\n") i = 0 while i < len(lines) and re.search(r"^\s*Returns?:\s*$", lines[i]) is None: i += 1 if i < len(lines): indent = len(_get_indent(lines[i])) lines[i] = _prepare_output_docstrings(output_type, config_class, min_indent=indent) func_doc = "\n".join(lines) else: raise ValueError( f"The function {fn} should have an empty 'Return:' or 'Returns:' in its docstring as placeholder, " f"current docstring is:\n{func_doc}" ) fn.__doc__ = func_doc return fn return docstring_decorator
null
18,481
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging if ( os.path.isdir(old_default_cache_path) and not os.path.isdir(default_cache_path) and "PYTORCH_PRETRAINED_BERT_CACHE" not in os.environ and "PYTORCH_TRANSFORMERS_CACHE" not in os.environ and "TRANSFORMERS_CACHE" not in os.environ ): shutil.move(old_default_cache_path, default_cache_path) TRANSFORMERS_CACHE = os.getenv("TRANSFORMERS_CACHE", PYTORCH_TRANSFORMERS_CACHE) The provided code snippet includes necessary dependencies for implementing the `filename_to_url` function. Write a Python function `def filename_to_url(filename, cache_dir=None)` to solve the following problem: Return the url and etag (which may be `None`) stored for *filename*. Raise `EnvironmentError` if *filename* or its stored metadata do not exist. Here is the function: def filename_to_url(filename, cache_dir=None): """ Return the url and etag (which may be `None`) stored for *filename*. Raise `EnvironmentError` if *filename* or its stored metadata do not exist. """ if cache_dir is None: cache_dir = TRANSFORMERS_CACHE if isinstance(cache_dir, Path): cache_dir = str(cache_dir) cache_path = os.path.join(cache_dir, filename) if not os.path.exists(cache_path): raise EnvironmentError(f"file {cache_path} not found") meta_path = cache_path + ".json" if not os.path.exists(meta_path): raise EnvironmentError(f"file {meta_path} not found") with open(meta_path, encoding="utf-8") as meta_file: metadata = json.load(meta_file) url = metadata["url"] etag = metadata["etag"] return url, etag
Return the url and etag (which may be `None`) stored for *filename*. Raise `EnvironmentError` if *filename* or its stored metadata do not exist.
18,482
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging if ( os.path.isdir(old_default_cache_path) and not os.path.isdir(default_cache_path) and "PYTORCH_PRETRAINED_BERT_CACHE" not in os.environ and "PYTORCH_TRANSFORMERS_CACHE" not in os.environ and "TRANSFORMERS_CACHE" not in os.environ ): shutil.move(old_default_cache_path, default_cache_path) TRANSFORMERS_CACHE = os.getenv("TRANSFORMERS_CACHE", PYTORCH_TRANSFORMERS_CACHE) The provided code snippet includes necessary dependencies for implementing the `get_cached_models` function. Write a Python function `def get_cached_models(cache_dir: Union[str, Path] = None) -> List[Tuple]` to solve the following problem: Returns a list of tuples representing model binaries that are cached locally. Each tuple has shape `(model_url, etag, size_MB)`. Filenames in `cache_dir` are use to get the metadata for each model, only urls ending with *.bin* are added. Args: cache_dir (`Union[str, Path]`, *optional*): The cache directory to search for models within. Will default to the transformers cache if unset. Returns: List[Tuple]: List of tuples each with shape `(model_url, etag, size_MB)` Here is the function: def get_cached_models(cache_dir: Union[str, Path] = None) -> List[Tuple]: """ Returns a list of tuples representing model binaries that are cached locally. Each tuple has shape `(model_url, etag, size_MB)`. Filenames in `cache_dir` are use to get the metadata for each model, only urls ending with *.bin* are added. Args: cache_dir (`Union[str, Path]`, *optional*): The cache directory to search for models within. Will default to the transformers cache if unset. Returns: List[Tuple]: List of tuples each with shape `(model_url, etag, size_MB)` """ if cache_dir is None: cache_dir = TRANSFORMERS_CACHE elif isinstance(cache_dir, Path): cache_dir = str(cache_dir) cached_models = [] for file in os.listdir(cache_dir): if file.endswith(".json"): meta_path = os.path.join(cache_dir, file) with open(meta_path, encoding="utf-8") as meta_file: metadata = json.load(meta_file) url = metadata["url"] etag = metadata["etag"] if url.endswith(".bin"): size_MB = os.path.getsize(meta_path.strip(".json")) / 1e6 cached_models.append((url, etag, size_MB)) return cached_models
Returns a list of tuples representing model binaries that are cached locally. Each tuple has shape `(model_url, etag, size_MB)`. Filenames in `cache_dir` are use to get the metadata for each model, only urls ending with *.bin* are added. Args: cache_dir (`Union[str, Path]`, *optional*): The cache directory to search for models within. Will default to the transformers cache if unset. Returns: List[Tuple]: List of tuples each with shape `(model_url, etag, size_MB)`
18,483
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_torch_available(): return _torch_available def torch_required(func): # Chose a different decorator name than in tests so it's clear they are not the same. @wraps(func) def wrapper(*args, **kwargs): if is_torch_available(): return func(*args, **kwargs) else: raise ImportError(f"Method `{func.__name__}` requires PyTorch.") return wrapper
null
18,484
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_tf_available(): return _tf_available def tf_required(func): # Chose a different decorator name than in tests so it's clear they are not the same. @wraps(func) def wrapper(*args, **kwargs): if is_tf_available(): return func(*args, **kwargs) else: raise ImportError(f"Method `{func.__name__}` requires TF.") return wrapper
null
18,485
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def _is_numpy(x): return isinstance(x, np.ndarray)
null
18,486
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def _is_torch_device(x): import torch return isinstance(x, torch.device)
null
18,487
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_torch_available(): return _torch_available def is_tf_available(): return _tf_available def is_flax_available(): return _flax_available def _is_torch(x): import torch return isinstance(x, torch.Tensor) def _is_tensorflow(x): import tensorflow as tf return isinstance(x, tf.Tensor) def _is_jax(x): import jax.numpy as jnp # noqa: F811 return isinstance(x, jnp.ndarray) The provided code snippet includes necessary dependencies for implementing the `to_py_obj` function. Write a Python function `def to_py_obj(obj)` to solve the following problem: Convert a TensorFlow tensor, PyTorch tensor, Numpy array or python list to a python list. Here is the function: def to_py_obj(obj): """ Convert a TensorFlow tensor, PyTorch tensor, Numpy array or python list to a python list. """ if isinstance(obj, (dict, UserDict)): return {k: to_py_obj(v) for k, v in obj.items()} elif isinstance(obj, (list, tuple)): return [to_py_obj(o) for o in obj] elif is_tf_available() and _is_tensorflow(obj): return obj.numpy().tolist() elif is_torch_available() and _is_torch(obj): return obj.detach().cpu().tolist() elif is_flax_available() and _is_jax(obj): return np.asarray(obj).tolist() elif isinstance(obj, (np.ndarray, np.number)): # tolist also works on 0d np arrays return obj.tolist() else: return obj
Convert a TensorFlow tensor, PyTorch tensor, Numpy array or python list to a python list.
18,488
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def is_torch_available(): return _torch_available def is_tf_available(): return _tf_available def is_flax_available(): return _flax_available def _is_torch(x): import torch return isinstance(x, torch.Tensor) def _is_tensorflow(x): import tensorflow as tf return isinstance(x, tf.Tensor) def _is_jax(x): import jax.numpy as jnp # noqa: F811 return isinstance(x, jnp.ndarray) The provided code snippet includes necessary dependencies for implementing the `to_numpy` function. Write a Python function `def to_numpy(obj)` to solve the following problem: Convert a TensorFlow tensor, PyTorch tensor, Numpy array or python list to a Numpy array. Here is the function: def to_numpy(obj): """ Convert a TensorFlow tensor, PyTorch tensor, Numpy array or python list to a Numpy array. """ if isinstance(obj, (dict, UserDict)): return {k: to_numpy(v) for k, v in obj.items()} elif isinstance(obj, (list, tuple)): return np.array(obj) elif is_tf_available() and _is_tensorflow(obj): return obj.numpy() elif is_torch_available() and _is_torch(obj): return obj.detach().cpu().numpy() elif is_flax_available() and _is_jax(obj): return np.asarray(obj) else: return obj
Convert a TensorFlow tensor, PyTorch tensor, Numpy array or python list to a Numpy array.
18,489
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging if ( os.path.isdir(old_default_cache_path) and not os.path.isdir(default_cache_path) and "PYTORCH_PRETRAINED_BERT_CACHE" not in os.environ and "PYTORCH_TRANSFORMERS_CACHE" not in os.environ and "TRANSFORMERS_CACHE" not in os.environ ): shutil.move(old_default_cache_path, default_cache_path) The provided code snippet includes necessary dependencies for implementing the `is_local_clone` function. Write a Python function `def is_local_clone(repo_path, repo_url)` to solve the following problem: Checks if the folder in `repo_path` is a local clone of `repo_url`. Here is the function: def is_local_clone(repo_path, repo_url): """ Checks if the folder in `repo_path` is a local clone of `repo_url`. """ # First double-check that `repo_path` is a git repo if not os.path.exists(os.path.join(repo_path, ".git")): return False test_git = subprocess.run("git branch".split(), cwd=repo_path) if test_git.returncode != 0: return False # Then look at its remotes remotes = subprocess.run( "git remote -v".split(), stderr=subprocess.PIPE, stdout=subprocess.PIPE, check=True, encoding="utf-8", cwd=repo_path, ).stdout return repo_url in remotes.split()
Checks if the folder in `repo_path` is a local clone of `repo_url`.
18,490
import copy import fnmatch import functools import importlib.util import io import json import os import re import shutil import subprocess import sys import tarfile import tempfile import types from collections import OrderedDict, UserDict from contextlib import ExitStack, contextmanager from dataclasses import fields from enum import Enum from functools import partial, wraps from hashlib import sha256 from itertools import chain from pathlib import Path from types import ModuleType from typing import Any, BinaryIO, ContextManager, Dict, List, Optional, Tuple, Union from urllib.parse import urlparse from uuid import uuid4 from zipfile import ZipFile, is_zipfile import numpy as np from packaging import version from tqdm.auto import tqdm import requests from filelock import FileLock from .utils.versions import importlib_metadata from .utils import logging def get_full_repo_name(model_id: str, organization: Optional[str] = None, token: Optional[str] = None): pass
null
18,491
import math from typing import Callable, Iterable, Optional, Tuple, Union import torch from torch import nn from torch.optim import Optimizer from torch.optim.lr_scheduler import LambdaLR from .trainer_utils import SchedulerType from .utils import logging from .utils.versions import require_version The provided code snippet includes necessary dependencies for implementing the `get_constant_schedule` function. Write a Python function `def get_constant_schedule(optimizer: Optimizer, last_epoch: int = -1)` to solve the following problem: Create a schedule with a constant learning rate, using the learning rate set in optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. Here is the function: def get_constant_schedule(optimizer: Optimizer, last_epoch: int = -1): """ Create a schedule with a constant learning rate, using the learning rate set in optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. """ return LambdaLR(optimizer, lambda _: 1, last_epoch=last_epoch)
Create a schedule with a constant learning rate, using the learning rate set in optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule.
18,492
import math from typing import Callable, Iterable, Optional, Tuple, Union import torch from torch import nn from torch.optim import Optimizer from torch.optim.lr_scheduler import LambdaLR from .trainer_utils import SchedulerType from .utils import logging from .utils.versions import require_version The provided code snippet includes necessary dependencies for implementing the `get_constant_schedule_with_warmup` function. Write a Python function `def get_constant_schedule_with_warmup(optimizer: Optimizer, num_warmup_steps: int, last_epoch: int = -1)` to solve the following problem: Create a schedule with a constant learning rate preceded by a warmup period during which the learning rate increases linearly between 0 and the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. Here is the function: def get_constant_schedule_with_warmup(optimizer: Optimizer, num_warmup_steps: int, last_epoch: int = -1): """ Create a schedule with a constant learning rate preceded by a warmup period during which the learning rate increases linearly between 0 and the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. """ def lr_lambda(current_step: int): if current_step < num_warmup_steps: return float(current_step) / float(max(1.0, num_warmup_steps)) return 1.0 return LambdaLR(optimizer, lr_lambda, last_epoch=last_epoch)
Create a schedule with a constant learning rate preceded by a warmup period during which the learning rate increases linearly between 0 and the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule.
18,493
import math from typing import Callable, Iterable, Optional, Tuple, Union import torch from torch import nn from torch.optim import Optimizer from torch.optim.lr_scheduler import LambdaLR from .trainer_utils import SchedulerType from .utils import logging from .utils.versions import require_version The provided code snippet includes necessary dependencies for implementing the `get_linear_schedule_with_warmup` function. Write a Python function `def get_linear_schedule_with_warmup(optimizer, num_warmup_steps, num_training_steps, last_epoch=-1)` to solve the following problem: Create a schedule with a learning rate that decreases linearly from the initial lr set in the optimizer to 0, after a warmup period during which it increases linearly from 0 to the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. num_training_steps (`int`): The total number of training steps. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. Here is the function: def get_linear_schedule_with_warmup(optimizer, num_warmup_steps, num_training_steps, last_epoch=-1): """ Create a schedule with a learning rate that decreases linearly from the initial lr set in the optimizer to 0, after a warmup period during which it increases linearly from 0 to the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. num_training_steps (`int`): The total number of training steps. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. """ def lr_lambda(current_step: int): if current_step < num_warmup_steps: return float(current_step) / float(max(1, num_warmup_steps)) return max( 0.0, float(num_training_steps - current_step) / float(max(1, num_training_steps - num_warmup_steps)) ) return LambdaLR(optimizer, lr_lambda, last_epoch)
Create a schedule with a learning rate that decreases linearly from the initial lr set in the optimizer to 0, after a warmup period during which it increases linearly from 0 to the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. num_training_steps (`int`): The total number of training steps. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule.
18,494
import math from typing import Callable, Iterable, Optional, Tuple, Union import torch from torch import nn from torch.optim import Optimizer from torch.optim.lr_scheduler import LambdaLR from .trainer_utils import SchedulerType from .utils import logging from .utils.versions import require_version The provided code snippet includes necessary dependencies for implementing the `get_cosine_schedule_with_warmup` function. Write a Python function `def get_cosine_schedule_with_warmup( optimizer: Optimizer, num_warmup_steps: int, num_training_steps: int, num_cycles: float = 0.5, last_epoch: int = -1 )` to solve the following problem: Create a schedule with a learning rate that decreases following the values of the cosine function between the initial lr set in the optimizer to 0, after a warmup period during which it increases linearly between 0 and the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. num_training_steps (`int`): The total number of training steps. num_cycles (`float`, *optional*, defaults to 0.5): The number of waves in the cosine schedule (the defaults is to just decrease from the max value to 0 following a half-cosine). last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. Here is the function: def get_cosine_schedule_with_warmup( optimizer: Optimizer, num_warmup_steps: int, num_training_steps: int, num_cycles: float = 0.5, last_epoch: int = -1 ): """ Create a schedule with a learning rate that decreases following the values of the cosine function between the initial lr set in the optimizer to 0, after a warmup period during which it increases linearly between 0 and the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. num_training_steps (`int`): The total number of training steps. num_cycles (`float`, *optional*, defaults to 0.5): The number of waves in the cosine schedule (the defaults is to just decrease from the max value to 0 following a half-cosine). last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. """ def lr_lambda(current_step): if current_step < num_warmup_steps: return float(current_step) / float(max(1, num_warmup_steps)) progress = float(current_step - num_warmup_steps) / float(max(1, num_training_steps - num_warmup_steps)) return max(0.0, 0.5 * (1.0 + math.cos(math.pi * float(num_cycles) * 2.0 * progress))) return LambdaLR(optimizer, lr_lambda, last_epoch)
Create a schedule with a learning rate that decreases following the values of the cosine function between the initial lr set in the optimizer to 0, after a warmup period during which it increases linearly between 0 and the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. num_training_steps (`int`): The total number of training steps. num_cycles (`float`, *optional*, defaults to 0.5): The number of waves in the cosine schedule (the defaults is to just decrease from the max value to 0 following a half-cosine). last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule.
18,495
import math from typing import Callable, Iterable, Optional, Tuple, Union import torch from torch import nn from torch.optim import Optimizer from torch.optim.lr_scheduler import LambdaLR from .trainer_utils import SchedulerType from .utils import logging from .utils.versions import require_version The provided code snippet includes necessary dependencies for implementing the `get_cosine_with_hard_restarts_schedule_with_warmup` function. Write a Python function `def get_cosine_with_hard_restarts_schedule_with_warmup( optimizer: Optimizer, num_warmup_steps: int, num_training_steps: int, num_cycles: int = 1, last_epoch: int = -1 )` to solve the following problem: Create a schedule with a learning rate that decreases following the values of the cosine function between the initial lr set in the optimizer to 0, with several hard restarts, after a warmup period during which it increases linearly between 0 and the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. num_training_steps (`int`): The total number of training steps. num_cycles (`int`, *optional*, defaults to 1): The number of hard restarts to use. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. Here is the function: def get_cosine_with_hard_restarts_schedule_with_warmup( optimizer: Optimizer, num_warmup_steps: int, num_training_steps: int, num_cycles: int = 1, last_epoch: int = -1 ): """ Create a schedule with a learning rate that decreases following the values of the cosine function between the initial lr set in the optimizer to 0, with several hard restarts, after a warmup period during which it increases linearly between 0 and the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. num_training_steps (`int`): The total number of training steps. num_cycles (`int`, *optional*, defaults to 1): The number of hard restarts to use. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. """ def lr_lambda(current_step): if current_step < num_warmup_steps: return float(current_step) / float(max(1, num_warmup_steps)) progress = float(current_step - num_warmup_steps) / float(max(1, num_training_steps - num_warmup_steps)) if progress >= 1.0: return 0.0 return max(0.0, 0.5 * (1.0 + math.cos(math.pi * ((float(num_cycles) * progress) % 1.0)))) return LambdaLR(optimizer, lr_lambda, last_epoch)
Create a schedule with a learning rate that decreases following the values of the cosine function between the initial lr set in the optimizer to 0, with several hard restarts, after a warmup period during which it increases linearly between 0 and the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. num_training_steps (`int`): The total number of training steps. num_cycles (`int`, *optional*, defaults to 1): The number of hard restarts to use. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule.
18,496
import math from typing import Callable, Iterable, Optional, Tuple, Union import torch from torch import nn from torch.optim import Optimizer from torch.optim.lr_scheduler import LambdaLR from .trainer_utils import SchedulerType from .utils import logging from .utils.versions import require_version The provided code snippet includes necessary dependencies for implementing the `get_polynomial_decay_schedule_with_warmup` function. Write a Python function `def get_polynomial_decay_schedule_with_warmup( optimizer, num_warmup_steps, num_training_steps, lr_end=1e-7, power=1.0, last_epoch=-1 )` to solve the following problem: Create a schedule with a learning rate that decreases as a polynomial decay from the initial lr set in the optimizer to end lr defined by *lr_end*, after a warmup period during which it increases linearly from 0 to the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. num_training_steps (`int`): The total number of training steps. lr_end (`float`, *optional*, defaults to 1e-7): The end LR. power (`float`, *optional*, defaults to 1.0): Power factor. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Note: *power* defaults to 1.0 as in the fairseq implementation, which in turn is based on the original BERT implementation at https://github.com/google-research/bert/blob/f39e881b169b9d53bea03d2d341b31707a6c052b/optimization.py#L37 Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. Here is the function: def get_polynomial_decay_schedule_with_warmup( optimizer, num_warmup_steps, num_training_steps, lr_end=1e-7, power=1.0, last_epoch=-1 ): """ Create a schedule with a learning rate that decreases as a polynomial decay from the initial lr set in the optimizer to end lr defined by *lr_end*, after a warmup period during which it increases linearly from 0 to the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. num_training_steps (`int`): The total number of training steps. lr_end (`float`, *optional*, defaults to 1e-7): The end LR. power (`float`, *optional*, defaults to 1.0): Power factor. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Note: *power* defaults to 1.0 as in the fairseq implementation, which in turn is based on the original BERT implementation at https://github.com/google-research/bert/blob/f39e881b169b9d53bea03d2d341b31707a6c052b/optimization.py#L37 Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. """ lr_init = optimizer.defaults["lr"] if not (lr_init > lr_end): raise ValueError(f"lr_end ({lr_end}) must be be smaller than initial lr ({lr_init})") def lr_lambda(current_step: int): if current_step < num_warmup_steps: return float(current_step) / float(max(1, num_warmup_steps)) elif current_step > num_training_steps: return lr_end / lr_init # as LambdaLR multiplies by lr_init else: lr_range = lr_init - lr_end decay_steps = num_training_steps - num_warmup_steps pct_remaining = 1 - (current_step - num_warmup_steps) / decay_steps decay = lr_range * pct_remaining ** power + lr_end return decay / lr_init # as LambdaLR multiplies by lr_init return LambdaLR(optimizer, lr_lambda, last_epoch)
Create a schedule with a learning rate that decreases as a polynomial decay from the initial lr set in the optimizer to end lr defined by *lr_end*, after a warmup period during which it increases linearly from 0 to the initial lr set in the optimizer. Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. num_warmup_steps (`int`): The number of steps for the warmup phase. num_training_steps (`int`): The total number of training steps. lr_end (`float`, *optional*, defaults to 1e-7): The end LR. power (`float`, *optional*, defaults to 1.0): Power factor. last_epoch (`int`, *optional*, defaults to -1): The index of the last epoch when resuming training. Note: *power* defaults to 1.0 as in the fairseq implementation, which in turn is based on the original BERT implementation at https://github.com/google-research/bert/blob/f39e881b169b9d53bea03d2d341b31707a6c052b/optimization.py#L37 Return: `torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule.
18,497
import math from typing import Callable, Iterable, Optional, Tuple, Union import torch from torch import nn from torch.optim import Optimizer from torch.optim.lr_scheduler import LambdaLR from .trainer_utils import SchedulerType from .utils import logging from .utils.versions import require_version TYPE_TO_SCHEDULER_FUNCTION = { SchedulerType.LINEAR: get_linear_schedule_with_warmup, SchedulerType.COSINE: get_cosine_schedule_with_warmup, SchedulerType.COSINE_WITH_RESTARTS: get_cosine_with_hard_restarts_schedule_with_warmup, SchedulerType.POLYNOMIAL: get_polynomial_decay_schedule_with_warmup, SchedulerType.CONSTANT: get_constant_schedule, SchedulerType.CONSTANT_WITH_WARMUP: get_constant_schedule_with_warmup, } class SchedulerType(ExplicitEnum): LINEAR = "linear" COSINE = "cosine" COSINE_WITH_RESTARTS = "cosine_with_restarts" POLYNOMIAL = "polynomial" CONSTANT = "constant" CONSTANT_WITH_WARMUP = "constant_with_warmup" The provided code snippet includes necessary dependencies for implementing the `get_scheduler` function. Write a Python function `def get_scheduler( name: Union[str, SchedulerType], optimizer: Optimizer, num_warmup_steps: Optional[int] = None, num_training_steps: Optional[int] = None, )` to solve the following problem: Unified API to get any scheduler from its name. Args: name (`str` or `SchedulerType`): The name of the scheduler to use. optimizer (`torch.optim.Optimizer`): The optimizer that will be used during training. num_warmup_steps (`int`, *optional*): The number of warmup steps to do. This is not required by all schedulers (hence the argument being optional), the function will raise an error if it's unset and the scheduler type requires it. num_training_steps (`int``, *optional*): The number of training steps to do. This is not required by all schedulers (hence the argument being optional), the function will raise an error if it's unset and the scheduler type requires it. Here is the function: def get_scheduler( name: Union[str, SchedulerType], optimizer: Optimizer, num_warmup_steps: Optional[int] = None, num_training_steps: Optional[int] = None, ): """ Unified API to get any scheduler from its name. Args: name (`str` or `SchedulerType`): The name of the scheduler to use. optimizer (`torch.optim.Optimizer`): The optimizer that will be used during training. num_warmup_steps (`int`, *optional*): The number of warmup steps to do. This is not required by all schedulers (hence the argument being optional), the function will raise an error if it's unset and the scheduler type requires it. num_training_steps (`int``, *optional*): The number of training steps to do. This is not required by all schedulers (hence the argument being optional), the function will raise an error if it's unset and the scheduler type requires it. """ name = SchedulerType(name) schedule_func = TYPE_TO_SCHEDULER_FUNCTION[name] if name == SchedulerType.CONSTANT: return schedule_func(optimizer) # All other schedulers require `num_warmup_steps` if num_warmup_steps is None: raise ValueError(f"{name} requires `num_warmup_steps`, please provide that argument.") if name == SchedulerType.CONSTANT_WITH_WARMUP: return schedule_func(optimizer, num_warmup_steps=num_warmup_steps) # All other schedulers require `num_training_steps` if num_training_steps is None: raise ValueError(f"{name} requires `num_training_steps`, please provide that argument.") return schedule_func(optimizer, num_warmup_steps=num_warmup_steps, num_training_steps=num_training_steps)
Unified API to get any scheduler from its name. Args: name (`str` or `SchedulerType`): The name of the scheduler to use. optimizer (`torch.optim.Optimizer`): The optimizer that will be used during training. num_warmup_steps (`int`, *optional*): The number of warmup steps to do. This is not required by all schedulers (hence the argument being optional), the function will raise an error if it's unset and the scheduler type requires it. num_training_steps (`int``, *optional*): The number of training steps to do. This is not required by all schedulers (hence the argument being optional), the function will raise an error if it's unset and the scheduler type requires it.
18,498
import math from typing import Callable, Iterable, Optional, Tuple, Union import torch from torch import nn from torch.optim import Optimizer from torch.optim.lr_scheduler import LambdaLR from .trainer_utils import SchedulerType from .utils import logging from .utils.versions import require_version class AdafactorSchedule(LambdaLR): """ Since [`~optimization.Adafactor`] performs its own scheduling, if the training loop relies on a scheduler (e.g., for logging), this class creates a proxy object that retrieves the current lr values from the optimizer. It returns `initial_lr` during startup and the actual `lr` during stepping. """ def __init__(self, optimizer, initial_lr=0.0): def lr_lambda(_): return initial_lr for group in optimizer.param_groups: group["initial_lr"] = initial_lr super().__init__(optimizer, lr_lambda) for group in optimizer.param_groups: del group["initial_lr"] def get_lr(self): opt = self.optimizer lrs = [ opt._get_lr(group, opt.state[group["params"][0]]) for group in opt.param_groups if group["params"][0].grad is not None ] if len(lrs) == 0: lrs = self.base_lrs # if called before stepping return lrs The provided code snippet includes necessary dependencies for implementing the `get_adafactor_schedule` function. Write a Python function `def get_adafactor_schedule(optimizer, initial_lr=0.0)` to solve the following problem: Get a proxy schedule for [`~optimization.Adafactor`] Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. initial_lr (`float`, *optional*, defaults to 0.0): Initial lr Return: [`~optimization.Adafactor`] proxy schedule object. Here is the function: def get_adafactor_schedule(optimizer, initial_lr=0.0): """ Get a proxy schedule for [`~optimization.Adafactor`] Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. initial_lr (`float`, *optional*, defaults to 0.0): Initial lr Return: [`~optimization.Adafactor`] proxy schedule object. """ return AdafactorSchedule(optimizer, initial_lr)
Get a proxy schedule for [`~optimization.Adafactor`] Args: optimizer ([`~torch.optim.Optimizer`]): The optimizer for which to schedule the learning rate. initial_lr (`float`, *optional*, defaults to 0.0): Initial lr Return: [`~optimization.Adafactor`] proxy schedule object.
18,499
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging try: from torch.optim.lr_scheduler import SAVE_STATE_WARNING except ImportError: SAVE_STATE_WARNING = "" def torch_pad_and_concatenate(tensor1, tensor2, padding_index=-100): """Concatenates `tensor1` and `tensor2` on first axis, applying padding on the second if necessary.""" if len(tensor1.shape) == 1 or tensor1.shape[1] == tensor2.shape[1]: return torch.cat((tensor1, tensor2), dim=0) # Let's figure out the new shape new_shape = (tensor1.shape[0] + tensor2.shape[0], max(tensor1.shape[1], tensor2.shape[1])) + tensor1.shape[2:] # Now let's fill the result tensor result = tensor1.new_full(new_shape, padding_index) result[: tensor1.shape[0], : tensor1.shape[1]] = tensor1 result[tensor1.shape[0] :, : tensor2.shape[1]] = tensor2 return result def numpy_pad_and_concatenate(array1, array2, padding_index=-100): """Concatenates `array1` and `array2` on first axis, applying padding on the second if necessary.""" if len(array1.shape) == 1 or array1.shape[1] == array2.shape[1]: return np.concatenate((array1, array2), axis=0) # Let's figure out the new shape new_shape = (array1.shape[0] + array2.shape[0], max(array1.shape[1], array2.shape[1])) + array1.shape[2:] # Now let's fill the result tensor result = np.full_like(array1, padding_index, shape=new_shape) result[: array1.shape[0], : array1.shape[1]] = array1 result[array1.shape[0] :, : array2.shape[1]] = array2 return result The provided code snippet includes necessary dependencies for implementing the `nested_concat` function. Write a Python function `def nested_concat(tensors, new_tensors, padding_index=-100)` to solve the following problem: Concat the `new_tensors` to `tensors` on the first dim and pad them on the second if needed. Works for tensors or nested list/tuples of tensors. Here is the function: def nested_concat(tensors, new_tensors, padding_index=-100): """ Concat the `new_tensors` to `tensors` on the first dim and pad them on the second if needed. Works for tensors or nested list/tuples of tensors. """ assert type(tensors) == type( new_tensors ), f"Expected `tensors` and `new_tensors` to have the same type but found {type(tensors)} and {type(new_tensors)}." if isinstance(tensors, (list, tuple)): return type(tensors)(nested_concat(t, n, padding_index=padding_index) for t, n in zip(tensors, new_tensors)) elif isinstance(tensors, torch.Tensor): return torch_pad_and_concatenate(tensors, new_tensors, padding_index=padding_index) elif isinstance(tensors, np.ndarray): return numpy_pad_and_concatenate(tensors, new_tensors, padding_index=padding_index) else: raise TypeError(f"Unsupported type for concatenation: got {type(tensors)}")
Concat the `new_tensors` to `tensors` on the first dim and pad them on the second if needed. Works for tensors or nested list/tuples of tensors.
18,500
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging try: from torch.optim.lr_scheduler import SAVE_STATE_WARNING except ImportError: SAVE_STATE_WARNING = "" class BatchEncoding(UserDict): """ Holds the output of the [`~tokenization_utils_base.PreTrainedTokenizerBase.encode_plus`] and [`~tokenization_utils_base.PreTrainedTokenizerBase.batch_encode`] methods (tokens, attention_masks, etc). This class is derived from a python dictionary and can be used as a dictionary. In addition, this class exposes utility methods to map from word/character space to token space. Args: data (`dict`): Dictionary of lists/arrays/tensors returned by the encode/batch_encode methods ('input_ids', 'attention_mask', etc.). encoding (`tokenizers.Encoding` or `Sequence[tokenizers.Encoding]`, *optional*): If the tokenizer is a fast tokenizer which outputs additional information like mapping from word/character space to token space the `tokenizers.Encoding` instance or list of instance (for batches) hold this information. tensor_type (`Union[None, str, TensorType]`, *optional*): You can give a tensor_type here to convert the lists of integers in PyTorch/TensorFlow/Numpy Tensors at initialization. prepend_batch_axis (`bool`, *optional*, defaults to `False`): Whether or not to add a batch axis when converting to tensors (see `tensor_type` above). n_sequences (`Optional[int]`, *optional*): You can give a tensor_type here to convert the lists of integers in PyTorch/TensorFlow/Numpy Tensors at initialization. """ def __init__( self, data: Optional[Dict[str, Any]] = None, encoding: Optional[Union[EncodingFast, Sequence[EncodingFast]]] = None, tensor_type: Union[None, str, TensorType] = None, prepend_batch_axis: bool = False, n_sequences: Optional[int] = None, ): super().__init__(data) if isinstance(encoding, EncodingFast): encoding = [encoding] self._encodings = encoding if n_sequences is None and encoding is not None and len(encoding): n_sequences = encoding[0].n_sequences self._n_sequences = n_sequences self.convert_to_tensors(tensor_type=tensor_type, prepend_batch_axis=prepend_batch_axis) def n_sequences(self) -> Optional[int]: """ `Optional[int]`: The number of sequences used to generate each sample from the batch encoded in this [`BatchEncoding`]. Currently can be one of `None` (unknown), `1` (a single sentence) or `2` (a pair of sentences) """ return self._n_sequences def is_fast(self) -> bool: """ `bool`: Indicate whether this [`BatchEncoding`] was generated from the result of a [`PreTrainedTokenizerFast`] or not. """ return self._encodings is not None def __getitem__(self, item: Union[int, str]) -> Union[Any, EncodingFast]: """ If the key is a string, returns the value of the dict associated to `key` ('input_ids', 'attention_mask', etc.). If the key is an integer, get the `tokenizers.Encoding` for batch item with index `key`. """ if isinstance(item, str): return self.data[item] elif self._encodings is not None: return self._encodings[item] else: raise KeyError( "Indexing with integers (to access backend Encoding for a given batch index) " "is not available when using Python based tokenizers" ) def __getattr__(self, item: str): try: return self.data[item] except KeyError: raise AttributeError def __getstate__(self): return {"data": self.data, "encodings": self._encodings} def __setstate__(self, state): if "data" in state: self.data = state["data"] if "encodings" in state: self._encodings = state["encodings"] def keys(self): return self.data.keys() def values(self): return self.data.values() def items(self): return self.data.items() # After this point: # Extended properties and methods only available for fast (Rust-based) tokenizers # provided by HuggingFace tokenizers library. def encodings(self) -> Optional[List[EncodingFast]]: """ `Optional[List[tokenizers.Encoding]]`: The list all encodings from the tokenization process. Returns `None` if the input was tokenized through Python (i.e., not a fast) tokenizer. """ return self._encodings def tokens(self, batch_index: int = 0) -> List[str]: """ Return the list of tokens (sub-parts of the input strings after word/subword splitting and before conversion to integer indices) at a given batch index (only works for the output of a fast tokenizer). Args: batch_index (`int`, *optional*, defaults to 0): The index to access in the batch. Returns: `List[str]`: The list of tokens at that index. """ if not self._encodings: raise ValueError("tokens() is not available when using Python-based tokenizers") return self._encodings[batch_index].tokens def sequence_ids(self, batch_index: int = 0) -> List[Optional[int]]: """ Return a list mapping the tokens to the id of their original sentences: - `None` for special tokens added around or between sequences, - `0` for tokens corresponding to words in the first sequence, - `1` for tokens corresponding to words in the second sequence when a pair of sequences was jointly encoded. Args: batch_index (`int`, *optional*, defaults to 0): The index to access in the batch. Returns: `List[Optional[int]]`: A list indicating the sequence id corresponding to each token. Special tokens added by the tokenizer are mapped to `None` and other tokens are mapped to the index of their corresponding sequence. """ if not self._encodings: raise ValueError("sequence_ids() is not available when using Python-based tokenizers") return self._encodings[batch_index].sequence_ids def words(self, batch_index: int = 0) -> List[Optional[int]]: """ Return a list mapping the tokens to their actual word in the initial sentence for a fast tokenizer. Args: batch_index (`int`, *optional*, defaults to 0): The index to access in the batch. Returns: `List[Optional[int]]`: A list indicating the word corresponding to each token. Special tokens added by the tokenizer are mapped to `None` and other tokens are mapped to the index of their corresponding word (several tokens will be mapped to the same word index if they are parts of that word). """ if not self._encodings: raise ValueError("words() is not available when using Python-based tokenizers") warnings.warn( "`BatchEncoding.words()` property is deprecated and should be replaced with the identical, " "but more self-explanatory `BatchEncoding.word_ids()` property.", FutureWarning, ) return self.word_ids(batch_index) def word_ids(self, batch_index: int = 0) -> List[Optional[int]]: """ Return a list mapping the tokens to their actual word in the initial sentence for a fast tokenizer. Args: batch_index (`int`, *optional*, defaults to 0): The index to access in the batch. Returns: `List[Optional[int]]`: A list indicating the word corresponding to each token. Special tokens added by the tokenizer are mapped to `None` and other tokens are mapped to the index of their corresponding word (several tokens will be mapped to the same word index if they are parts of that word). """ if not self._encodings: raise ValueError("word_ids() is not available when using Python-based tokenizers") return self._encodings[batch_index].word_ids def token_to_sequence(self, batch_or_token_index: int, token_index: Optional[int] = None) -> int: """ Get the index of the sequence represented by the given token. In the general use case, this method returns `0` for a single sequence or the first sequence of a pair, and `1` for the second sequence of a pair Can be called as: - `self.token_to_sequence(token_index)` if batch size is 1 - `self.token_to_sequence(batch_index, token_index)` if batch size is greater than 1 This method is particularly suited when the input sequences are provided as pre-tokenized sequences (i.e., words are defined by the user). In this case it allows to easily associate encoded tokens with provided tokenized words. Args: batch_or_token_index (`int`): Index of the sequence in the batch. If the batch only comprises one sequence, this can be the index of the token in the sequence. token_index (`int`, *optional*): If a batch index is provided in *batch_or_token_index*, this can be the index of the token in the sequence. Returns: `int`: Index of the word in the input sequence. """ if not self._encodings: raise ValueError("token_to_sequence() is not available when using Python based tokenizers") if token_index is not None: batch_index = batch_or_token_index else: batch_index = 0 token_index = batch_or_token_index if batch_index < 0: batch_index = self._batch_size + batch_index if token_index < 0: token_index = self._seq_len + token_index return self._encodings[batch_index].token_to_sequence(token_index) def token_to_word(self, batch_or_token_index: int, token_index: Optional[int] = None) -> int: """ Get the index of the word corresponding (i.e. comprising) to an encoded token in a sequence of the batch. Can be called as: - `self.token_to_word(token_index)` if batch size is 1 - `self.token_to_word(batch_index, token_index)` if batch size is greater than 1 This method is particularly suited when the input sequences are provided as pre-tokenized sequences (i.e., words are defined by the user). In this case it allows to easily associate encoded tokens with provided tokenized words. Args: batch_or_token_index (`int`): Index of the sequence in the batch. If the batch only comprise one sequence, this can be the index of the token in the sequence. token_index (`int`, *optional*): If a batch index is provided in *batch_or_token_index*, this can be the index of the token in the sequence. Returns: `int`: Index of the word in the input sequence. """ if not self._encodings: raise ValueError("token_to_word() is not available when using Python based tokenizers") if token_index is not None: batch_index = batch_or_token_index else: batch_index = 0 token_index = batch_or_token_index if batch_index < 0: batch_index = self._batch_size + batch_index if token_index < 0: token_index = self._seq_len + token_index return self._encodings[batch_index].token_to_word(token_index) def word_to_tokens( self, batch_or_word_index: int, word_index: Optional[int] = None, sequence_index: int = 0 ) -> Optional[TokenSpan]: """ Get the encoded token span corresponding to a word in a sequence of the batch. Token spans are returned as a [`~tokenization_utils_base.TokenSpan`] with: - **start** -- Index of the first token. - **end** -- Index of the token following the last token. Can be called as: - `self.word_to_tokens(word_index, sequence_index: int = 0)` if batch size is 1 - `self.word_to_tokens(batch_index, word_index, sequence_index: int = 0)` if batch size is greater or equal to 1 This method is particularly suited when the input sequences are provided as pre-tokenized sequences (i.e. words are defined by the user). In this case it allows to easily associate encoded tokens with provided tokenized words. Args: batch_or_word_index (`int`): Index of the sequence in the batch. If the batch only comprises one sequence, this can be the index of the word in the sequence. word_index (`int`, *optional*): If a batch index is provided in *batch_or_token_index*, this can be the index of the word in the sequence. sequence_index (`int`, *optional*, defaults to 0): If pair of sequences are encoded in the batch this can be used to specify which sequence in the pair (0 or 1) the provided word index belongs to. Returns: Optional [`~tokenization_utils_base.TokenSpan`] Span of tokens in the encoded sequence. Returns `None` if no tokens correspond to the word. """ if not self._encodings: raise ValueError("word_to_tokens() is not available when using Python based tokenizers") if word_index is not None: batch_index = batch_or_word_index else: batch_index = 0 word_index = batch_or_word_index if batch_index < 0: batch_index = self._batch_size + batch_index if word_index < 0: word_index = self._seq_len + word_index span = self._encodings[batch_index].word_to_tokens(word_index, sequence_index) return TokenSpan(*span) if span is not None else None def token_to_chars(self, batch_or_token_index: int, token_index: Optional[int] = None) -> CharSpan: """ Get the character span corresponding to an encoded token in a sequence of the batch. Character spans are returned as a [`~tokenization_utils_base.CharSpan`] with: - **start** -- Index of the first character in the original string associated to the token. - **end** -- Index of the character following the last character in the original string associated to the token. Can be called as: - `self.token_to_chars(token_index)` if batch size is 1 - `self.token_to_chars(batch_index, token_index)` if batch size is greater or equal to 1 Args: batch_or_token_index (`int`): Index of the sequence in the batch. If the batch only comprise one sequence, this can be the index of the token in the sequence. token_index (`int`, *optional*): If a batch index is provided in *batch_or_token_index*, this can be the index of the token or tokens in the sequence. Returns: [`~tokenization_utils_base.CharSpan`]: Span of characters in the original string. """ if not self._encodings: raise ValueError("token_to_chars() is not available when using Python based tokenizers") if token_index is not None: batch_index = batch_or_token_index else: batch_index = 0 token_index = batch_or_token_index return CharSpan(*(self._encodings[batch_index].token_to_chars(token_index))) def char_to_token( self, batch_or_char_index: int, char_index: Optional[int] = None, sequence_index: int = 0 ) -> int: """ Get the index of the token in the encoded output comprising a character in the original string for a sequence of the batch. Can be called as: - `self.char_to_token(char_index)` if batch size is 1 - `self.char_to_token(batch_index, char_index)` if batch size is greater or equal to 1 This method is particularly suited when the input sequences are provided as pre-tokenized sequences (i.e. words are defined by the user). In this case it allows to easily associate encoded tokens with provided tokenized words. Args: batch_or_char_index (`int`): Index of the sequence in the batch. If the batch only comprise one sequence, this can be the index of the word in the sequence char_index (`int`, *optional*): If a batch index is provided in *batch_or_token_index*, this can be the index of the word in the sequence. sequence_index (`int`, *optional*, defaults to 0): If pair of sequences are encoded in the batch this can be used to specify which sequence in the pair (0 or 1) the provided character index belongs to. Returns: `int`: Index of the token. """ if not self._encodings: raise ValueError("char_to_token() is not available when using Python based tokenizers") if char_index is not None: batch_index = batch_or_char_index else: batch_index = 0 char_index = batch_or_char_index return self._encodings[batch_index].char_to_token(char_index, sequence_index) def word_to_chars( self, batch_or_word_index: int, word_index: Optional[int] = None, sequence_index: int = 0 ) -> CharSpan: """ Get the character span in the original string corresponding to given word in a sequence of the batch. Character spans are returned as a CharSpan NamedTuple with: - start: index of the first character in the original string - end: index of the character following the last character in the original string Can be called as: - `self.word_to_chars(word_index)` if batch size is 1 - `self.word_to_chars(batch_index, word_index)` if batch size is greater or equal to 1 Args: batch_or_word_index (`int`): Index of the sequence in the batch. If the batch only comprise one sequence, this can be the index of the word in the sequence word_index (`int`, *optional*): If a batch index is provided in *batch_or_token_index*, this can be the index of the word in the sequence. sequence_index (`int`, *optional*, defaults to 0): If pair of sequences are encoded in the batch this can be used to specify which sequence in the pair (0 or 1) the provided word index belongs to. Returns: `CharSpan` or `List[CharSpan]`: Span(s) of the associated character or characters in the string. CharSpan are NamedTuple with: - start: index of the first character associated to the token in the original string - end: index of the character following the last character associated to the token in the original string """ if not self._encodings: raise ValueError("word_to_chars() is not available when using Python based tokenizers") if word_index is not None: batch_index = batch_or_word_index else: batch_index = 0 word_index = batch_or_word_index return CharSpan(*(self._encodings[batch_index].word_to_chars(word_index, sequence_index))) def char_to_word(self, batch_or_char_index: int, char_index: Optional[int] = None, sequence_index: int = 0) -> int: """ Get the word in the original string corresponding to a character in the original string of a sequence of the batch. Can be called as: - `self.char_to_word(char_index)` if batch size is 1 - `self.char_to_word(batch_index, char_index)` if batch size is greater than 1 This method is particularly suited when the input sequences are provided as pre-tokenized sequences (i.e. words are defined by the user). In this case it allows to easily associate encoded tokens with provided tokenized words. Args: batch_or_char_index (`int`): Index of the sequence in the batch. If the batch only comprise one sequence, this can be the index of the character in the original string. char_index (`int`, *optional*): If a batch index is provided in *batch_or_token_index*, this can be the index of the character in the original string. sequence_index (`int`, *optional*, defaults to 0): If pair of sequences are encoded in the batch this can be used to specify which sequence in the pair (0 or 1) the provided character index belongs to. Returns: `int` or `List[int]`: Index or indices of the associated encoded token(s). """ if not self._encodings: raise ValueError("char_to_word() is not available when using Python based tokenizers") if char_index is not None: batch_index = batch_or_char_index else: batch_index = 0 char_index = batch_or_char_index return self._encodings[batch_index].char_to_word(char_index, sequence_index) def convert_to_tensors( self, tensor_type: Optional[Union[str, TensorType]] = None, prepend_batch_axis: bool = False ): """ Convert the inner content to tensors. Args: tensor_type (`str` or [`~file_utils.TensorType`], *optional*): The type of tensors to use. If `str`, should be one of the values of the enum [`~file_utils.TensorType`]. If `None`, no modification is done. prepend_batch_axis (`int`, *optional*, defaults to `False`): Whether or not to add the batch dimension during the conversion. """ if tensor_type is None: return self # Convert to TensorType if not isinstance(tensor_type, TensorType): tensor_type = TensorType(tensor_type) # Get a function reference for the correct framework if tensor_type == TensorType.TENSORFLOW: if not is_tf_available(): raise ImportError( "Unable to convert output to TensorFlow tensors format, TensorFlow is not installed." ) import tensorflow as tf as_tensor = tf.constant is_tensor = tf.is_tensor elif tensor_type == TensorType.PYTORCH: if not is_torch_available(): raise ImportError("Unable to convert output to PyTorch tensors format, PyTorch is not installed.") import torch as_tensor = torch.tensor is_tensor = torch.is_tensor elif tensor_type == TensorType.JAX: if not is_flax_available(): raise ImportError("Unable to convert output to JAX tensors format, JAX is not installed.") import jax.numpy as jnp # noqa: F811 as_tensor = jnp.array is_tensor = _is_jax else: as_tensor = np.asarray is_tensor = _is_numpy # (mfuntowicz: This code is unreachable) # else: # raise ImportError( # f"Unable to convert output to tensors format {tensor_type}" # ) # Do the tensor conversion in batch for key, value in self.items(): try: if prepend_batch_axis: value = [value] if not is_tensor(value): tensor = as_tensor(value) # Removing this for now in favor of controlling the shape with `prepend_batch_axis` # # at-least2d # if tensor.ndim > 2: # tensor = tensor.squeeze(0) # elif tensor.ndim < 2: # tensor = tensor[None, :] self[key] = tensor except: # noqa E722 if key == "overflowing_tokens": raise ValueError( "Unable to create tensor returning overflowing tokens of different lengths. " "Please see if a fast version of this tokenizer is available to have this feature available." ) raise ValueError( "Unable to create tensor, you should probably activate truncation and/or padding " "with 'padding=True' 'truncation=True' to have batched tensors with the same length." ) return self def to(self, device: Union[str, "torch.device"]) -> "BatchEncoding": """ Send all values to device by calling `v.to(device)` (PyTorch only). Args: device (`str` or `torch.device`): The device to put the tensors on. Returns: [`BatchEncoding`]: The same instance after modification. """ # This check catches things like APEX blindly calling "to" on all inputs to a module # Otherwise it passes the casts down and casts the LongTensor containing the token idxs # into a HalfTensor if isinstance(device, str) or _is_torch_device(device) or isinstance(device, int): self.data = {k: v.to(device=device) for k, v in self.data.items()} else: logger.warning(f"Attempting to cast a BatchEncoding to type {str(device)}. This is not supported.") return self The provided code snippet includes necessary dependencies for implementing the `find_batch_size` function. Write a Python function `def find_batch_size(tensors)` to solve the following problem: Find the first dimension of a tensor in a nested list/tuple/dict of tensors. Here is the function: def find_batch_size(tensors): """ Find the first dimension of a tensor in a nested list/tuple/dict of tensors. """ if isinstance(tensors, (list, tuple)): for t in tensors: result = find_batch_size(t) if result is not None: return result elif isinstance(tensors, (dict, BatchEncoding)): for key, value in tensors.items(): result = find_batch_size(value) if result is not None: return result elif isinstance(tensors, torch.Tensor): return tensors.shape[0] if len(tensors.shape) >= 1 else None elif isinstance(tensors, np.ndarray): return tensors.shape[0] if len(tensors.shape) >= 1 else None
Find the first dimension of a tensor in a nested list/tuple/dict of tensors.
18,501
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging try: from torch.optim.lr_scheduler import SAVE_STATE_WARNING except ImportError: SAVE_STATE_WARNING = "" The provided code snippet includes necessary dependencies for implementing the `nested_numpify` function. Write a Python function `def nested_numpify(tensors)` to solve the following problem: Numpify `tensors` (even if it's a nested list/tuple of tensors). Here is the function: def nested_numpify(tensors): "Numpify `tensors` (even if it's a nested list/tuple of tensors)." if isinstance(tensors, (list, tuple)): return type(tensors)(nested_numpify(t) for t in tensors) t = tensors.cpu() if t.dtype == torch.bfloat16: # As of Numpy 1.21.4, NumPy does not support bfloat16 (see # https://github.com/numpy/numpy/blob/a47ecdea856986cd60eabbd53265c2ca5916ad5d/doc/source/user/basics.types.rst ). # Until Numpy adds bfloat16, we must convert float32. t = t.to(torch.float32) return t.numpy()
Numpify `tensors` (even if it's a nested list/tuple of tensors).
18,502
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging The provided code snippet includes necessary dependencies for implementing the `nested_detach` function. Write a Python function `def nested_detach(tensors)` to solve the following problem: Detach `tensors` (even if it's a nested list/tuple of tensors). Here is the function: def nested_detach(tensors): "Detach `tensors` (even if it's a nested list/tuple of tensors)." if isinstance(tensors, (list, tuple)): return type(tensors)(nested_detach(t) for t in tensors) return tensors.detach()
Detach `tensors` (even if it's a nested list/tuple of tensors).
18,503
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging if is_torch_tpu_available(): import torch_xla.core.xla_model as xm try: from torch.optim.lr_scheduler import SAVE_STATE_WARNING except ImportError: SAVE_STATE_WARNING = "" def is_torch_tpu_available(): if not _torch_available: return False # This test is probably enough, but just in case, we unpack a bit. if importlib.util.find_spec("torch_xla") is None: return False if importlib.util.find_spec("torch_xla.core") is None: return False return importlib.util.find_spec("torch_xla.core.xla_model") is not None def nested_xla_mesh_reduce(tensors, name): if is_torch_tpu_available(): import torch_xla.core.xla_model as xm if isinstance(tensors, (list, tuple)): return type(tensors)(nested_xla_mesh_reduce(t, f"{name}_{i}") for i, t in enumerate(tensors)) if tensors.ndim == 0: tensors = tensors[None] return xm.mesh_reduce(name, tensors, torch.cat) else: raise ImportError("Torch xla must be installed to use `nested_xla_mesh_reduce`")
null
18,504
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging try: from torch.optim.lr_scheduler import SAVE_STATE_WARNING except ImportError: SAVE_STATE_WARNING = "" def distributed_concat(tensor: Any, num_total_examples: Optional[int] = None) -> Any: try: if isinstance(tensor, (tuple, list)): return type(tensor)(distributed_concat(t, num_total_examples) for t in tensor) output_tensors = [tensor.clone() for _ in range(dist.get_world_size())] output_tensors = [t if len(t.shape) > 0 else t[None] for t in output_tensors] dist.all_gather(output_tensors, tensor) concat = torch.cat(output_tensors, dim=0) # truncate the dummy elements added by SequentialDistributedSampler if num_total_examples is not None: concat = concat[:num_total_examples] return concat except AssertionError: raise AssertionError("Not currently using distributed training")
null
18,505
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging try: from torch.optim.lr_scheduler import SAVE_STATE_WARNING except ImportError: SAVE_STATE_WARNING = "" def distributed_broadcast_scalars( scalars: List[Union[int, float]], num_total_examples: Optional[int] = None, device: Optional[torch.device] = torch.device("cuda"), ) -> torch.Tensor: try: tensorized_scalar = torch.tensor(scalars).to(device) output_tensors = [tensorized_scalar.clone() for _ in range(dist.get_world_size())] dist.all_gather(output_tensors, tensorized_scalar) concat = torch.cat(output_tensors, dim=0) # truncate the dummy elements added by SequentialDistributedSampler if num_total_examples is not None: concat = concat[:num_total_examples] return concat except AssertionError: raise AssertionError("Not currently using distributed training")
null
18,506
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging def reissue_pt_warnings(caught_warnings): # Reissue warnings that are not the SAVE_STATE_WARNING if len(caught_warnings) > 1: for w in caught_warnings: if w.category != UserWarning or w.message != SAVE_STATE_WARNING: warnings.warn(w.message, w.category)
null
18,507
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging The provided code snippet includes necessary dependencies for implementing the `torch_distributed_zero_first` function. Write a Python function `def torch_distributed_zero_first(local_rank: int)` to solve the following problem: Decorator to make all processes in distributed training wait for each local_master to do something. Args: local_rank (`int`): The rank of the local process. Here is the function: def torch_distributed_zero_first(local_rank: int): """ Decorator to make all processes in distributed training wait for each local_master to do something. Args: local_rank (`int`): The rank of the local process. """ if local_rank not in [-1, 0]: dist.barrier() yield if local_rank == 0: dist.barrier()
Decorator to make all processes in distributed training wait for each local_master to do something. Args: local_rank (`int`): The rank of the local process.
18,508
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging try: from torch.optim.lr_scheduler import SAVE_STATE_WARNING except ImportError: SAVE_STATE_WARNING = "" def get_tpu_sampler(dataset: torch.utils.data.Dataset, batch_size: int): if xm.xrt_world_size() <= 1: return RandomSampler(dataset) return DistributedSampler(dataset, num_replicas=xm.xrt_world_size(), rank=xm.get_ordinal())
null
18,509
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging The provided code snippet includes necessary dependencies for implementing the `nested_new_like` function. Write a Python function `def nested_new_like(arrays, num_samples, padding_index=-100)` to solve the following problem: Create the same nested structure as `arrays` with a first dimension always at `num_samples`. Here is the function: def nested_new_like(arrays, num_samples, padding_index=-100): """Create the same nested structure as `arrays` with a first dimension always at `num_samples`.""" if isinstance(arrays, (list, tuple)): return type(arrays)(nested_new_like(x, num_samples) for x in arrays) return np.full_like(arrays, padding_index, shape=(num_samples, *arrays.shape[1:]))
Create the same nested structure as `arrays` with a first dimension always at `num_samples`.
18,510
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging The provided code snippet includes necessary dependencies for implementing the `expand_like` function. Write a Python function `def expand_like(arrays, new_seq_length, padding_index=-100)` to solve the following problem: Expand the `arrays` so that the second dimension grows to `new_seq_length`. Uses `padding_index` for padding. Here is the function: def expand_like(arrays, new_seq_length, padding_index=-100): """Expand the `arrays` so that the second dimension grows to `new_seq_length`. Uses `padding_index` for padding.""" result = np.full_like(arrays, padding_index, shape=(arrays.shape[0], new_seq_length) + arrays.shape[2:]) result[:, : arrays.shape[1]] = arrays return result
Expand the `arrays` so that the second dimension grows to `new_seq_length`. Uses `padding_index` for padding.
18,511
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging The provided code snippet includes necessary dependencies for implementing the `nested_truncate` function. Write a Python function `def nested_truncate(tensors, limit)` to solve the following problem: Truncate `tensors` at `limit` (even if it's a nested list/tuple of tensors). Here is the function: def nested_truncate(tensors, limit): "Truncate `tensors` at `limit` (even if it's a nested list/tuple of tensors)." if isinstance(tensors, (list, tuple)): return type(tensors)(nested_truncate(t, limit) for t in tensors) return tensors[:limit]
Truncate `tensors` at `limit` (even if it's a nested list/tuple of tensors).
18,512
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging try: from torch.optim.lr_scheduler import SAVE_STATE_WARNING except ImportError: SAVE_STATE_WARNING = "" The provided code snippet includes necessary dependencies for implementing the `get_length_grouped_indices` function. Write a Python function `def get_length_grouped_indices(lengths, batch_size, mega_batch_mult=None, generator=None)` to solve the following problem: Return a list of indices so that each slice of `batch_size` consecutive indices correspond to elements of similar lengths. To do this, the indices are: - randomly permuted - grouped in mega-batches of size `mega_batch_mult * batch_size` - sorted by length in each mega-batch The result is the concatenation of all mega-batches, with the batch of `batch_size` containing the element of maximum length placed first, so that an OOM happens sooner rather than later. Here is the function: def get_length_grouped_indices(lengths, batch_size, mega_batch_mult=None, generator=None): """ Return a list of indices so that each slice of `batch_size` consecutive indices correspond to elements of similar lengths. To do this, the indices are: - randomly permuted - grouped in mega-batches of size `mega_batch_mult * batch_size` - sorted by length in each mega-batch The result is the concatenation of all mega-batches, with the batch of `batch_size` containing the element of maximum length placed first, so that an OOM happens sooner rather than later. """ # Default for mega_batch_mult: 50 or the number to get 4 megabatches, whichever is smaller. if mega_batch_mult is None: mega_batch_mult = min(len(lengths) // (batch_size * 4), 50) # Just in case, for tiny datasets if mega_batch_mult == 0: mega_batch_mult = 1 # We need to use torch for the random part as a distributed sampler will set the random seed for torch. indices = torch.randperm(len(lengths), generator=generator) megabatch_size = mega_batch_mult * batch_size megabatches = [indices[i : i + megabatch_size].tolist() for i in range(0, len(lengths), megabatch_size)] megabatches = [list(sorted(megabatch, key=lambda i: lengths[i], reverse=True)) for megabatch in megabatches] # The rest is to get the biggest batch first. # Since each megabatch is sorted by descending length, the longest element is the first megabatch_maximums = [lengths[megabatch[0]] for megabatch in megabatches] max_idx = torch.argmax(torch.tensor(megabatch_maximums)).item() # Switch to put the longest element in first position megabatches[0][0], megabatches[max_idx][0] = megabatches[max_idx][0], megabatches[0][0] return [i for megabatch in megabatches for i in megabatch]
Return a list of indices so that each slice of `batch_size` consecutive indices correspond to elements of similar lengths. To do this, the indices are: - randomly permuted - grouped in mega-batches of size `mega_batch_mult * batch_size` - sorted by length in each mega-batch The result is the concatenation of all mega-batches, with the batch of `batch_size` containing the element of maximum length placed first, so that an OOM happens sooner rather than later.
18,513
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging try: from torch.optim.lr_scheduler import SAVE_STATE_WARNING except ImportError: SAVE_STATE_WARNING = "" logger = logging.get_logger(__name__) def _get_learning_rate(self): if self.deepspeed: # with deepspeed's fp16 and dynamic loss scale enabled the optimizer/scheduler steps may # not run for the first few dozen steps while loss scale is too large, and thus during # that time `get_last_lr` will fail if called during that warm up stage, so work around it: try: last_lr = self.lr_scheduler.get_last_lr()[0] except AssertionError as e: if "need to call step" in str(e): logger.warning("tried to get lr value before scheduler/optimizer started stepping, returning lr=0") last_lr = 0 else: raise else: last_lr = ( # backward compatibility for pytorch schedulers self.lr_scheduler.get_last_lr()[0] if version.parse(torch.__version__) >= version.parse("1.4") else self.lr_scheduler.get_lr()[0] ) return last_lr
null
18,514
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging def metrics_format(self, metrics: Dict[str, float]) -> Dict[str, float]: """ Reformat Trainer metrics values to a human-readable format Args: metrics (`Dict[str, float]`): The metrics returned from train/evaluate/predict Returns: metrics (`Dict[str, float]`): The reformatted metrics """ metrics_copy = metrics.copy() for k, v in metrics_copy.items(): if "_mem_" in k: metrics_copy[k] = f"{ v >> 20 }MB" elif "_runtime" in k: metrics_copy[k] = _secs2timedelta(v) elif k == "total_flos": metrics_copy[k] = f"{ int(v) >> 30 }GF" elif type(metrics_copy[k]) == float: metrics_copy[k] = round(v, 4) return metrics_copy The provided code snippet includes necessary dependencies for implementing the `log_metrics` function. Write a Python function `def log_metrics(self, split, metrics)` to solve the following problem: Log metrics in a specially formatted way Under distributed environment this is done only for a process with rank 0. Args: split (`str`): Mode/split name: one of `train`, `eval`, `test` metrics (`Dict[str, float]`): The metrics returned from train/evaluate/predictmetrics: metrics dict Notes on memory reports: In order to get memory usage report you need to install `psutil`. You can do that with `pip install psutil`. Now when this method is run, you will see a report that will include: : ```python init_mem_cpu_alloc_delta = 1301MB init_mem_cpu_peaked_delta = 154MB init_mem_gpu_alloc_delta = 230MB init_mem_gpu_peaked_delta = 0MB train_mem_cpu_alloc_delta = 1345MB train_mem_cpu_peaked_delta = 0MB train_mem_gpu_alloc_delta = 693MB train_mem_gpu_peaked_delta = 7MB ``` **Understanding the reports:** - the first segment, e.g., `train__`, tells you which stage the metrics are for. Reports starting with `init_` will be added to the first stage that gets run. So that if only evaluation is run, the memory usage for the `__init__` will be reported along with the `eval_` metrics. - the third segment, is either `cpu` or `gpu`, tells you whether it's the general RAM or the gpu0 memory metric. - `*_alloc_delta` - is the difference in the used/allocated memory counter between the end and the start of the stage - it can be negative if a function released more memory than it allocated. - `*_peaked_delta` - is any extra memory that was consumed and then freed - relative to the current allocated memory counter - it is never negative. When you look at the metrics of any stage you add up `alloc_delta` + `peaked_delta` and you know how much memory was needed to complete that stage. The reporting happens only for process of rank 0 and gpu 0 (if there is a gpu). Typically this is enough since the main process does the bulk of work, but it could be not quite so if model parallel is used and then other GPUs may use a different amount of gpu memory. This is also not the same under DataParallel where gpu0 may require much more memory than the rest since it stores the gradient and optimizer states for all participating GPUS. Perhaps in the future these reports will evolve to measure those too. The CPU RAM metric measures RSS (Resident Set Size) includes both the memory which is unique to the process and the memory shared with other processes. It is important to note that it does not include swapped out memory, so the reports could be imprecise. The CPU peak memory is measured using a sampling thread. Due to python's GIL it may miss some of the peak memory if that thread didn't get a chance to run when the highest memory was used. Therefore this report can be less than reality. Using `tracemalloc` would have reported the exact peak memory, but it doesn't report memory allocations outside of python. So if some C++ CUDA extension allocated its own memory it won't be reported. And therefore it was dropped in favor of the memory sampling approach, which reads the current process memory usage. The GPU allocated and peak memory reporting is done with `torch.cuda.memory_allocated()` and `torch.cuda.max_memory_allocated()`. This metric reports only "deltas" for pytorch-specific allocations, as `torch.cuda` memory management system doesn't track any memory allocated outside of pytorch. For example, the very first cuda call typically loads CUDA kernels, which may take from 0.5 to 2GB of GPU memory. Note that this tracker doesn't account for memory allocations outside of [`Trainer`]'s `__init__`, `train`, `evaluate` and `predict` calls. Because `evaluation` calls may happen during `train`, we can't handle nested invocations because `torch.cuda.max_memory_allocated` is a single counter, so if it gets reset by a nested eval call, `train`'s tracker will report incorrect info. If this [pytorch issue](https://github.com/pytorch/pytorch/issues/16266) gets resolved it will be possible to change this class to be re-entrant. Until then we will only track the outer level of `train`, `evaluate` and `predict` methods. Which means that if `eval` is called during `train`, it's the latter that will account for its memory usage and that of the former. This also means that if any other tool that is used along the [`Trainer`] calls `torch.cuda.reset_peak_memory_stats`, the gpu peak memory stats could be invalid. And the [`Trainer`] will disrupt the normal behavior of any such tools that rely on calling `torch.cuda.reset_peak_memory_stats` themselves. For best performance you may want to consider turning the memory profiling off for production runs. Here is the function: def log_metrics(self, split, metrics): """ Log metrics in a specially formatted way Under distributed environment this is done only for a process with rank 0. Args: split (`str`): Mode/split name: one of `train`, `eval`, `test` metrics (`Dict[str, float]`): The metrics returned from train/evaluate/predictmetrics: metrics dict Notes on memory reports: In order to get memory usage report you need to install `psutil`. You can do that with `pip install psutil`. Now when this method is run, you will see a report that will include: : ```python init_mem_cpu_alloc_delta = 1301MB init_mem_cpu_peaked_delta = 154MB init_mem_gpu_alloc_delta = 230MB init_mem_gpu_peaked_delta = 0MB train_mem_cpu_alloc_delta = 1345MB train_mem_cpu_peaked_delta = 0MB train_mem_gpu_alloc_delta = 693MB train_mem_gpu_peaked_delta = 7MB ``` **Understanding the reports:** - the first segment, e.g., `train__`, tells you which stage the metrics are for. Reports starting with `init_` will be added to the first stage that gets run. So that if only evaluation is run, the memory usage for the `__init__` will be reported along with the `eval_` metrics. - the third segment, is either `cpu` or `gpu`, tells you whether it's the general RAM or the gpu0 memory metric. - `*_alloc_delta` - is the difference in the used/allocated memory counter between the end and the start of the stage - it can be negative if a function released more memory than it allocated. - `*_peaked_delta` - is any extra memory that was consumed and then freed - relative to the current allocated memory counter - it is never negative. When you look at the metrics of any stage you add up `alloc_delta` + `peaked_delta` and you know how much memory was needed to complete that stage. The reporting happens only for process of rank 0 and gpu 0 (if there is a gpu). Typically this is enough since the main process does the bulk of work, but it could be not quite so if model parallel is used and then other GPUs may use a different amount of gpu memory. This is also not the same under DataParallel where gpu0 may require much more memory than the rest since it stores the gradient and optimizer states for all participating GPUS. Perhaps in the future these reports will evolve to measure those too. The CPU RAM metric measures RSS (Resident Set Size) includes both the memory which is unique to the process and the memory shared with other processes. It is important to note that it does not include swapped out memory, so the reports could be imprecise. The CPU peak memory is measured using a sampling thread. Due to python's GIL it may miss some of the peak memory if that thread didn't get a chance to run when the highest memory was used. Therefore this report can be less than reality. Using `tracemalloc` would have reported the exact peak memory, but it doesn't report memory allocations outside of python. So if some C++ CUDA extension allocated its own memory it won't be reported. And therefore it was dropped in favor of the memory sampling approach, which reads the current process memory usage. The GPU allocated and peak memory reporting is done with `torch.cuda.memory_allocated()` and `torch.cuda.max_memory_allocated()`. This metric reports only "deltas" for pytorch-specific allocations, as `torch.cuda` memory management system doesn't track any memory allocated outside of pytorch. For example, the very first cuda call typically loads CUDA kernels, which may take from 0.5 to 2GB of GPU memory. Note that this tracker doesn't account for memory allocations outside of [`Trainer`]'s `__init__`, `train`, `evaluate` and `predict` calls. Because `evaluation` calls may happen during `train`, we can't handle nested invocations because `torch.cuda.max_memory_allocated` is a single counter, so if it gets reset by a nested eval call, `train`'s tracker will report incorrect info. If this [pytorch issue](https://github.com/pytorch/pytorch/issues/16266) gets resolved it will be possible to change this class to be re-entrant. Until then we will only track the outer level of `train`, `evaluate` and `predict` methods. Which means that if `eval` is called during `train`, it's the latter that will account for its memory usage and that of the former. This also means that if any other tool that is used along the [`Trainer`] calls `torch.cuda.reset_peak_memory_stats`, the gpu peak memory stats could be invalid. And the [`Trainer`] will disrupt the normal behavior of any such tools that rely on calling `torch.cuda.reset_peak_memory_stats` themselves. For best performance you may want to consider turning the memory profiling off for production runs. """ if not self.is_world_process_zero(): return print(f"***** {split} metrics *****") metrics_formatted = self.metrics_format(metrics) k_width = max(len(str(x)) for x in metrics_formatted.keys()) v_width = max(len(str(x)) for x in metrics_formatted.values()) for key in sorted(metrics_formatted.keys()): print(f" {key: <{k_width}} = {metrics_formatted[key]:>{v_width}}")
Log metrics in a specially formatted way Under distributed environment this is done only for a process with rank 0. Args: split (`str`): Mode/split name: one of `train`, `eval`, `test` metrics (`Dict[str, float]`): The metrics returned from train/evaluate/predictmetrics: metrics dict Notes on memory reports: In order to get memory usage report you need to install `psutil`. You can do that with `pip install psutil`. Now when this method is run, you will see a report that will include: : ```python init_mem_cpu_alloc_delta = 1301MB init_mem_cpu_peaked_delta = 154MB init_mem_gpu_alloc_delta = 230MB init_mem_gpu_peaked_delta = 0MB train_mem_cpu_alloc_delta = 1345MB train_mem_cpu_peaked_delta = 0MB train_mem_gpu_alloc_delta = 693MB train_mem_gpu_peaked_delta = 7MB ``` **Understanding the reports:** - the first segment, e.g., `train__`, tells you which stage the metrics are for. Reports starting with `init_` will be added to the first stage that gets run. So that if only evaluation is run, the memory usage for the `__init__` will be reported along with the `eval_` metrics. - the third segment, is either `cpu` or `gpu`, tells you whether it's the general RAM or the gpu0 memory metric. - `*_alloc_delta` - is the difference in the used/allocated memory counter between the end and the start of the stage - it can be negative if a function released more memory than it allocated. - `*_peaked_delta` - is any extra memory that was consumed and then freed - relative to the current allocated memory counter - it is never negative. When you look at the metrics of any stage you add up `alloc_delta` + `peaked_delta` and you know how much memory was needed to complete that stage. The reporting happens only for process of rank 0 and gpu 0 (if there is a gpu). Typically this is enough since the main process does the bulk of work, but it could be not quite so if model parallel is used and then other GPUs may use a different amount of gpu memory. This is also not the same under DataParallel where gpu0 may require much more memory than the rest since it stores the gradient and optimizer states for all participating GPUS. Perhaps in the future these reports will evolve to measure those too. The CPU RAM metric measures RSS (Resident Set Size) includes both the memory which is unique to the process and the memory shared with other processes. It is important to note that it does not include swapped out memory, so the reports could be imprecise. The CPU peak memory is measured using a sampling thread. Due to python's GIL it may miss some of the peak memory if that thread didn't get a chance to run when the highest memory was used. Therefore this report can be less than reality. Using `tracemalloc` would have reported the exact peak memory, but it doesn't report memory allocations outside of python. So if some C++ CUDA extension allocated its own memory it won't be reported. And therefore it was dropped in favor of the memory sampling approach, which reads the current process memory usage. The GPU allocated and peak memory reporting is done with `torch.cuda.memory_allocated()` and `torch.cuda.max_memory_allocated()`. This metric reports only "deltas" for pytorch-specific allocations, as `torch.cuda` memory management system doesn't track any memory allocated outside of pytorch. For example, the very first cuda call typically loads CUDA kernels, which may take from 0.5 to 2GB of GPU memory. Note that this tracker doesn't account for memory allocations outside of [`Trainer`]'s `__init__`, `train`, `evaluate` and `predict` calls. Because `evaluation` calls may happen during `train`, we can't handle nested invocations because `torch.cuda.max_memory_allocated` is a single counter, so if it gets reset by a nested eval call, `train`'s tracker will report incorrect info. If this [pytorch issue](https://github.com/pytorch/pytorch/issues/16266) gets resolved it will be possible to change this class to be re-entrant. Until then we will only track the outer level of `train`, `evaluate` and `predict` methods. Which means that if `eval` is called during `train`, it's the latter that will account for its memory usage and that of the former. This also means that if any other tool that is used along the [`Trainer`] calls `torch.cuda.reset_peak_memory_stats`, the gpu peak memory stats could be invalid. And the [`Trainer`] will disrupt the normal behavior of any such tools that rely on calling `torch.cuda.reset_peak_memory_stats` themselves. For best performance you may want to consider turning the memory profiling off for production runs.
18,515
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging The provided code snippet includes necessary dependencies for implementing the `save_metrics` function. Write a Python function `def save_metrics(self, split, metrics, combined=True)` to solve the following problem: Save metrics into a json file for that split, e.g. `train_results.json`. Under distributed environment this is done only for a process with rank 0. Args: split (`str`): Mode/split name: one of `train`, `eval`, `test`, `all` metrics (`Dict[str, float]`): The metrics returned from train/evaluate/predict combined (`bool`, *optional*, defaults to `True`): Creates combined metrics by updating `all_results.json` with metrics of this call To understand the metrics please read the docstring of [`~Trainer.log_metrics`]. The only difference is that raw unformatted numbers are saved in the current method. Here is the function: def save_metrics(self, split, metrics, combined=True): """ Save metrics into a json file for that split, e.g. `train_results.json`. Under distributed environment this is done only for a process with rank 0. Args: split (`str`): Mode/split name: one of `train`, `eval`, `test`, `all` metrics (`Dict[str, float]`): The metrics returned from train/evaluate/predict combined (`bool`, *optional*, defaults to `True`): Creates combined metrics by updating `all_results.json` with metrics of this call To understand the metrics please read the docstring of [`~Trainer.log_metrics`]. The only difference is that raw unformatted numbers are saved in the current method. """ if not self.is_world_process_zero(): return path = os.path.join(self.args.output_dir, f"{split}_results.json") with open(path, "w") as f: json.dump(metrics, f, indent=4, sort_keys=True) if combined: path = os.path.join(self.args.output_dir, "all_results.json") if os.path.exists(path): with open(path, "r") as f: all_metrics = json.load(f) else: all_metrics = {} all_metrics.update(metrics) with open(path, "w") as f: json.dump(all_metrics, f, indent=4, sort_keys=True)
Save metrics into a json file for that split, e.g. `train_results.json`. Under distributed environment this is done only for a process with rank 0. Args: split (`str`): Mode/split name: one of `train`, `eval`, `test`, `all` metrics (`Dict[str, float]`): The metrics returned from train/evaluate/predict combined (`bool`, *optional*, defaults to `True`): Creates combined metrics by updating `all_results.json` with metrics of this call To understand the metrics please read the docstring of [`~Trainer.log_metrics`]. The only difference is that raw unformatted numbers are saved in the current method.
18,516
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging The provided code snippet includes necessary dependencies for implementing the `save_state` function. Write a Python function `def save_state(self)` to solve the following problem: Saves the Trainer state, since Trainer.save_model saves only the tokenizer with the model Under distributed environment this is done only for a process with rank 0. Here is the function: def save_state(self): """ Saves the Trainer state, since Trainer.save_model saves only the tokenizer with the model Under distributed environment this is done only for a process with rank 0. """ if not self.is_world_process_zero(): return path = os.path.join(self.args.output_dir, "trainer_state.json") self.state.save_to_json(path)
Saves the Trainer state, since Trainer.save_model saves only the tokenizer with the model Under distributed environment this is done only for a process with rank 0.
18,517
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging The provided code snippet includes necessary dependencies for implementing the `get_parameter_names` function. Write a Python function `def get_parameter_names(model, forbidden_layer_types)` to solve the following problem: Returns the names of the model parameters that are not inside a forbidden layer. Here is the function: def get_parameter_names(model, forbidden_layer_types): """ Returns the names of the model parameters that are not inside a forbidden layer. """ result = [] for name, child in model.named_children(): result += [ f"{name}.{n}" for n in get_parameter_names(child, forbidden_layer_types) if not isinstance(child, tuple(forbidden_layer_types)) ] # Add model specific parameters (defined with nn.Parameter) since they are not in any child. result += list(model._parameters.keys()) return result
Returns the names of the model parameters that are not inside a forbidden layer.
18,518
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging try: from torch.optim.lr_scheduler import SAVE_STATE_WARNING except ImportError: SAVE_STATE_WARNING = "" def smp_forward_backward(model, inputs, gradient_accumulation_steps=1, scaler=None): with torch.cuda.amp.autocast(enabled=(scaler is not None)): outputs = model(**inputs) loss = outputs["loss"] if isinstance(outputs, dict) else outputs[0] loss /= gradient_accumulation_steps if scaler is not None: loss = scaler.scale(loss).squeeze() model.backward(loss) return loss
null
18,519
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging def smp_forward_only(model, inputs): return model(**inputs)
null
18,520
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging try: from torch.optim.lr_scheduler import SAVE_STATE_WARNING except ImportError: SAVE_STATE_WARNING = "" def smp_gather(tensor): if isinstance(tensor, (list, tuple)): return type(tensor)(smp_gather(t) for t in tensor) elif isinstance(tensor, dict): return type(tensor)({k: smp_gather(v) for k, v in tensor.items()}) elif not isinstance(tensor, torch.Tensor): raise TypeError( f"Can't gather the values of type {type(tensor)}, only of nested list/tuple/dicts of tensors." ) all_tensors = smp.allgather(tensor, smp.CommGroup.DP_GROUP) all_tensors = [t if len(t.shape) > 0 else t[None] for t in all_tensors] return torch.cat([t.cpu() for t in all_tensors], dim=0)
null
18,521
import datetime import json import math import os import sys import warnings from contextlib import contextmanager from dataclasses import dataclass from logging import StreamHandler from typing import Any, Dict, Iterator, List, Optional, Union import numpy as np import torch from packaging import version from torch import nn from torch.utils.data import Dataset, IterableDataset, RandomSampler, Sampler from torch.utils.data.distributed import DistributedSampler from .file_utils import ( is_sagemaker_dp_enabled, is_sagemaker_mp_enabled, is_torch_tpu_available, is_training_run_on_sagemaker, ) from .tokenization_utils_base import BatchEncoding from .utils import logging def smp_nested_concat(tensor): if isinstance(tensor, (list, tuple)): return type(tensor)(smp_nested_concat(t) for t in tensor) elif isinstance(tensor, dict): return type(tensor)({k: smp_nested_concat(v) for k, v in tensor.items()}) # It doesn't seem possible to check here if `tensor` is a StepOutput because StepOutput lives in `smp.step` # which is also the name of the decorator so Python is confused. return tensor.concat().detach().cpu()
null
18,522
import inspect import math from abc import ABC from typing import Callable, Iterable, List, Optional import numpy as np import torch from .file_utils import add_start_docstrings from .utils.logging import get_logger def _get_ngrams(ngram_size: int, prev_input_ids: torch.Tensor, num_hypos: int): generated_ngrams = [{} for _ in range(num_hypos)] for idx in range(num_hypos): gen_tokens = prev_input_ids[idx].tolist() generated_ngram = generated_ngrams[idx] for ngram in zip(*[gen_tokens[i:] for i in range(ngram_size)]): prev_ngram_tuple = tuple(ngram[:-1]) generated_ngram[prev_ngram_tuple] = generated_ngram.get(prev_ngram_tuple, []) + [ngram[-1]] return generated_ngrams def _get_generated_ngrams(banned_ngrams, prev_input_ids, ngram_size, cur_len): # Before decoding the next token, prevent decoding of ngrams that have already appeared start_idx = cur_len + 1 - ngram_size ngram_idx = tuple(prev_input_ids[start_idx:cur_len].tolist()) return banned_ngrams.get(ngram_idx, []) The provided code snippet includes necessary dependencies for implementing the `_calc_banned_ngram_tokens` function. Write a Python function `def _calc_banned_ngram_tokens( ngram_size: int, prev_input_ids: torch.Tensor, num_hypos: int, cur_len: int ) -> List[Iterable[int]]` to solve the following problem: Copied from fairseq for no_repeat_ngram in beam_search Here is the function: def _calc_banned_ngram_tokens( ngram_size: int, prev_input_ids: torch.Tensor, num_hypos: int, cur_len: int ) -> List[Iterable[int]]: """Copied from fairseq for no_repeat_ngram in beam_search""" if cur_len + 1 < ngram_size: # return no banned tokens if we haven't generated no_repeat_ngram_size tokens yet return [[] for _ in range(num_hypos)] generated_ngrams = _get_ngrams(ngram_size, prev_input_ids, num_hypos) banned_tokens = [ _get_generated_ngrams(generated_ngrams[hypo_idx], prev_input_ids[hypo_idx], ngram_size, cur_len) for hypo_idx in range(num_hypos) ] return banned_tokens
Copied from fairseq for no_repeat_ngram in beam_search
18,523
import importlib.util import io import json import weakref from copy import deepcopy from functools import partialmethod from .dependency_versions_check import dep_version_check from .file_utils import is_torch_available from .utils import logging def is_deepspeed_available(): return importlib.util.find_spec("deepspeed") is not None
null
18,524
import importlib.util import io import json import weakref from copy import deepcopy from functools import partialmethod from .dependency_versions_check import dep_version_check from .file_utils import is_torch_available from .utils import logging _hf_deepspeed_config_weak_ref = None def set_hf_deepspeed_config(hf_deepspeed_config_obj): # this is a special weakref global object to allow us to get to Deepspeed config from APIs # that don't have an easy way to get to the Deepspeed config outside of the Trainer domain. global _hf_deepspeed_config_weak_ref # will go away automatically when HfDeepSpeedConfig is destroyed (when TrainingArguments is destroyed) _hf_deepspeed_config_weak_ref = weakref.ref(hf_deepspeed_config_obj)
null
18,525
import importlib.util import io import json import weakref from copy import deepcopy from functools import partialmethod from .dependency_versions_check import dep_version_check from .file_utils import is_torch_available from .utils import logging _hf_deepspeed_config_weak_ref = None def is_deepspeed_zero3_enabled(): if _hf_deepspeed_config_weak_ref is not None and _hf_deepspeed_config_weak_ref() is not None: return _hf_deepspeed_config_weak_ref().is_zero3() else: return False
null
18,526
import importlib.util import io import json import weakref from copy import deepcopy from functools import partialmethod from .dependency_versions_check import dep_version_check from .file_utils import is_torch_available from .utils import logging _hf_deepspeed_config_weak_ref = None def deepspeed_config(): if _hf_deepspeed_config_weak_ref is not None and _hf_deepspeed_config_weak_ref() is not None: return _hf_deepspeed_config_weak_ref().config else: return None
null
18,527
import importlib.util import io import json import weakref from copy import deepcopy from functools import partialmethod from .dependency_versions_check import dep_version_check from .file_utils import is_torch_available from .utils import logging The provided code snippet includes necessary dependencies for implementing the `deepspeed_reinit` function. Write a Python function `def deepspeed_reinit(trainer)` to solve the following problem: this is a temp hack based on: https://github.com/microsoft/DeepSpeed/issues/1394#issuecomment-937405374 until Deepspeed fixes a bug where it can't resume from a checkpoint after it did some stepping https://github.com/microsoft/DeepSpeed/issues/1612 Here is the function: def deepspeed_reinit(trainer): """ this is a temp hack based on: https://github.com/microsoft/DeepSpeed/issues/1394#issuecomment-937405374 until Deepspeed fixes a bug where it can't resume from a checkpoint after it did some stepping https://github.com/microsoft/DeepSpeed/issues/1612 """ import deepspeed deepspeed_engine, optimizer, _, lr_scheduler = deepspeed.initialize(**trainer.deepspeed_initialize_kwargs) return deepspeed_engine, optimizer, lr_scheduler
this is a temp hack based on: https://github.com/microsoft/DeepSpeed/issues/1394#issuecomment-937405374 until Deepspeed fixes a bug where it can't resume from a checkpoint after it did some stepping https://github.com/microsoft/DeepSpeed/issues/1612