Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/transformers/integrations/tpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import torch
from torch.utils.data import DataLoader

from ..utils import WEIGHTS_NAME, PushToHubMixin, is_torch_xla_available, logging
from ..utils import WEIGHTS_NAME, is_torch_xla_available, logging
from ..utils.hub import PushToHubMixin


logger = logging.get_logger(__name__)
Expand Down
3 changes: 2 additions & 1 deletion src/transformers/safetensors_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import httpx
from huggingface_hub import Discussion, HfApi, get_repo_discussions

from .utils import cached_file, http_user_agent, logging
from .utils import logging
from .utils.hub import cached_file, http_user_agent


logger = logging.get_logger(__name__)
Expand Down
4 changes: 1 addition & 3 deletions src/transformers/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from functools import lru_cache

from huggingface_hub.errors import EntryNotFoundError, RepositoryNotFoundError, RevisionNotFoundError
from packaging import version

from .. import __version__
Expand Down Expand Up @@ -81,11 +82,8 @@
LEGACY_PROCESSOR_CHAT_TEMPLATE_FILE,
S3_BUCKET_PREFIX,
TRANSFORMERS_DYNAMIC_MODULE_NAME,
EntryNotFoundError,
PushInProgress,
PushToHubMixin,
RepositoryNotFoundError,
RevisionNotFoundError,
cached_file,
define_sagemaker_information,
extract_commit_hash,
Expand Down
33 changes: 20 additions & 13 deletions src/transformers/utils/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from __future__ import annotations

import importlib
import inspect
import json
import os
Expand All @@ -29,11 +30,9 @@
from contextlib import AbstractContextManager, ExitStack, nullcontext
from dataclasses import fields, is_dataclass
from enum import Enum
from functools import partial, wraps
from functools import lru_cache, partial, wraps
from typing import TYPE_CHECKING, Any, TypedDict

import numpy as np

from ..utils import logging
from .import_utils import is_mlx_available, is_torch_available, is_torch_fx_proxy

Expand All @@ -53,6 +52,11 @@
_registered_model_output_types: set[type[Any]] = set()


@lru_cache
def _get_numpy():
return importlib.import_module("numpy")


def _register_model_output_pytree_node(output_type: type[ModelOutput]) -> None:
if not _is_torch_available:
return
Expand Down Expand Up @@ -152,7 +156,7 @@ def is_numpy_array(x) -> bool:
"""
Tests if `x` is a numpy array or not.
"""
return isinstance(x, np.ndarray)
return isinstance(x, _get_numpy().ndarray)


def is_torch_tensor(x) -> bool:
Expand Down Expand Up @@ -200,11 +204,12 @@ def _is_tensor_or_array_like(value):
"""
Check if a value is array-like (includes ragged arrays)
"""
numpy = _get_numpy()
if is_numpy_array(value):
return True
if is_torch_tensor(value):
return True
if isinstance(value, (int, float, bool, np.number)):
if isinstance(value, (int, float, bool, numpy.number)):
return True

if isinstance(value, (list, tuple)):
Expand Down Expand Up @@ -298,13 +303,14 @@ def to_py_obj(obj):
"""
Convert a PyTorch tensor, Numpy array or python list to a python list.
"""
numpy = _get_numpy()
if isinstance(obj, (int, float)):
return obj
elif isinstance(obj, (dict, UserDict)):
return {k: to_py_obj(v) for k, v in obj.items()}
elif isinstance(obj, (list, tuple)):
# Only convert directly if all elements are numeric scalars
if all(isinstance(x, (int, float, np.number)) for x in obj):
if all(isinstance(x, (int, float, numpy.number)) for x in obj):
return list(obj)

# Otherwise recurse element-wise
Expand All @@ -322,7 +328,7 @@ def to_py_obj(obj):
return framework_to_py_obj[framework](obj)

# tolist also works on 0d np arrays
if isinstance(obj, np.number):
if isinstance(obj, numpy.number):
return obj.tolist()
else:
return obj
Expand All @@ -332,6 +338,7 @@ def to_numpy(obj):
"""
Convert a PyTorch tensor, Numpy array or python list to a Numpy array.
"""
numpy = _get_numpy()

framework_to_numpy = {
"pt": lambda obj: obj.detach().cpu().numpy(),
Expand All @@ -341,7 +348,7 @@ def to_numpy(obj):
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)
return numpy.array(obj)

# This gives us a smart order to test the frameworks with the corresponding tests.
framework_to_test_func = _get_frameworks_and_test_func(obj)
Expand Down Expand Up @@ -618,7 +625,7 @@ def transpose(array, axes=None):
Framework-agnostic version of transpose operation.
"""
if is_numpy_array(array):
return np.transpose(array, axes=axes)
return _get_numpy().transpose(array, axes=axes)
elif is_torch_tensor(array):
return array.T if axes is None else array.permute(*axes)
else:
Expand All @@ -630,7 +637,7 @@ def reshape(array, newshape):
Framework-agnostic version of reshape operation.
"""
if is_numpy_array(array):
return np.reshape(array, newshape)
return _get_numpy().reshape(array, newshape)
elif is_torch_tensor(array):
return array.reshape(*newshape)
else:
Expand All @@ -642,7 +649,7 @@ def squeeze(array, axis=None):
Framework-agnostic version of squeeze operation.
"""
if is_numpy_array(array):
return np.squeeze(array, axis=axis)
return _get_numpy().squeeze(array, axis=axis)
elif is_torch_tensor(array):
return array.squeeze() if axis is None else array.squeeze(dim=axis)
else:
Expand All @@ -654,7 +661,7 @@ def expand_dims(array, axis):
Framework-agnostic version of expand_dims operation.
"""
if is_numpy_array(array):
return np.expand_dims(array, axis)
return _get_numpy().expand_dims(array, axis)
elif is_torch_tensor(array):
return array.unsqueeze(dim=axis)
else:
Expand All @@ -666,7 +673,7 @@ def tensor_size(array):
Framework-agnostic version of size operation.
"""
if is_numpy_array(array):
return np.size(array)
return _get_numpy().size(array)
elif is_torch_tensor(array):
return array.numel()
else:
Expand Down
Loading
Loading