Making Numpy depedency in Local Tensor optional to fix broken Torchao CI (#165938)

In recent change LocalTensor introduced dependency on Numpy and has broken Torchao CI.
This dependency cna be made optional and required only when Local Tensor is used.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/165938
Approved by: https://github.com/atalman
This commit is contained in:
Dzmitry Huba 2025-10-20 15:00:23 -07:00 committed by PyTorch MergeBot
parent 4a6cf0a93e
commit 0b1c462979

View File

@ -51,7 +51,14 @@ from collections.abc import Sequence
from types import TracebackType
from typing import Any, Callable, Generator, Optional, Union
import numpy as np
try:
import numpy as np
HAS_NUMPY = True
except ModuleNotFoundError:
HAS_NUMPY = False
np = None # type: ignore[assignment]
import torch
from torch import Size, SymBool, SymInt, Tensor
@ -524,8 +531,13 @@ class LocalTensor(torch.Tensor):
with LocalTensorMode(local_tensor._ranks):
return func(*args, **kwargs)
def numpy(self, *, force: bool = False) -> np.ndarray:
return self.reconcile().numpy(force=force)
def numpy(
self, *, force: bool = False
) -> np.ndarray: # pyrefly: ignore # missing-attribute
if HAS_NUMPY:
return self.reconcile().numpy(force=force)
else:
raise RuntimeError("Numpy is not available")
def __lt__(
self, other: torch.Tensor | bool | complex | float | int