1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
# mypy: allow-untyped-defs
from functools import partial
from typing import Optional, Tuple, Union
import torch
import torch._prims as prims
import torch._prims_common as utils
import torch._refs as refs
import torch._refs.linalg as linalg
from torch import Tensor
from torch._prims_common import (
check_fp_or_complex,
check_is_matrix,
Dim,
DimsType,
ELEMENTWISE_TYPE_PROMOTION_KIND,
IntLike,
TensorLikeType,
)
from torch._prims_common.wrappers import (
_maybe_convert_to_dtype,
elementwise_type_promotion_wrapper,
out_wrapper,
)
__all__ = [
"diagonal",
"matrix_norm",
"norm",
"svd",
"svdvals",
"vector_norm",
"vecdot",
"cross",
]
def _check_norm_dtype(dtype: Optional[torch.dtype], x_dtype: torch.dtype, fn_name: str):
"""
Checks related to the dtype kwarg in `linalg.*norm` functions
"""
if dtype is not None:
torch._check(
utils.is_float_dtype(dtype) or utils.is_complex_dtype(dtype),
lambda: f"{fn_name}: dtype should be floating point or complex. Got {dtype}",
)
torch._check(
utils.is_complex_dtype(dtype) == utils.is_complex_dtype(x_dtype),
lambda: "{fn_name}: dtype should be {d} for {d} inputs. Got {dtype}".format(
fn_name=fn_name,
d="complex" if utils.is_complex_dtype(x_dtype) else "real",
dtype=dtype,
),
)
torch._check(
utils.get_higher_dtype(dtype, x_dtype) == dtype,
lambda: f"{fn_name}: the dtype of the input ({x_dtype}) should be convertible "
"without narrowing to the specified dtype ({dtype})",
)
import operator
# Utilities should come BEFORE this import
from torch._decomp import register_decomposition
from torch._decomp.decompositions import pw_cast_for_opmath
@register_decomposition(torch._ops.ops.aten.linalg_cross)
@out_wrapper()
@pw_cast_for_opmath
def cross(a: Tensor, b: Tensor, dim: int = -1):
torch._check(
a.ndim == b.ndim,
lambda: "linalg.cross: inputs must have the same number of dimensions.",
)
torch._check(
a.size(dim) == 3 and b.size(dim) == 3,
lambda: f"linalg.cross: inputs dim {dim} must have length 3, got {a.size(dim)} and {b.size(dim)}",
)
a, b = torch.broadcast_tensors(a, b)
dim = utils.canonicalize_dim(a.ndim, dim)
idx = torch.arange(3, device=a.device)
return a.index_select(dim, (idx + 1) % 3) * b.index_select(
dim, (idx + 2) % 3
) - a.index_select(dim, (idx + 2) % 3) * b.index_select(dim, (idx + 1) % 3)
def diagonal(
input: TensorLikeType,
*,
offset: int = 0,
dim1: int = -2,
dim2: int = -1,
) -> TensorLikeType:
return torch.diagonal(input, offset=offset, dim1=dim1, dim2=dim2)
@register_decomposition(torch._ops.ops.aten.linalg_vector_norm)
@out_wrapper(exact_dtype=True)
def vector_norm(
x: TensorLikeType,
ord: Union[float, int] = 2,
dim: Optional[DimsType] = None,
keepdim: bool = False,
*,
dtype: Optional[torch.dtype] = None,
) -> Tensor:
from torch.fx.experimental.symbolic_shapes import guard_size_oblivious
# Checks
check_fp_or_complex(x.dtype, "linalg.vector_norm")
if isinstance(dim, Dim):
dim = [dim] # type: ignore[assignment]
if guard_size_oblivious(x.numel() == 0) and (ord < 0.0 or ord == float("inf")):
torch._check(
dim is not None and len(dim) != 0,
lambda: f"linalg.vector_norm cannot compute the {ord} norm on an empty tensor "
"because the operation does not have an identity",
)
shape = x.shape
assert dim is not None # mypy does not seem to be able to see through check?
for d in dim:
torch._check(
shape[d] != 0,
lambda: f"linalg.vector_norm cannot compute the {ord} norm on the "
f"dimension {d} because this dimension is empty and the "
"operation does not have an identity",
)
_check_norm_dtype(dtype, x.dtype, "linalg.vector_norm")
computation_dtype, result_dtype = utils.reduction_dtypes(
x, utils.REDUCTION_OUTPUT_TYPE_KIND.COMPLEX_TO_FLOAT, dtype
)
to_result_dtype = partial(_maybe_convert_to_dtype, dtype=result_dtype)
# Implementation
if ord == 0.0:
return torch.sum(torch.ne(x, 0.0), dim=dim, keepdim=keepdim, dtype=result_dtype)
elif ord == float("inf"):
return to_result_dtype(torch.amax(torch.abs(x), dim=dim, keepdim=keepdim)) # type: ignore[return-value,arg-type]
elif ord == float("-inf"):
return to_result_dtype(torch.amin(torch.abs(x), dim=dim, keepdim=keepdim)) # type: ignore[return-value,arg-type]
else:
# From here on the computation dtype is important as the reduction is non-trivial
x = _maybe_convert_to_dtype(x, computation_dtype) # type: ignore[assignment]
reduce_sum = partial(torch.sum, dim=dim, keepdim=keepdim)
is_ord_even = ord % 2 == 0 if isinstance(ord, IntLike) else ord % 2.0 == 0.0
if not (is_ord_even and utils.is_float_dtype(x.dtype)):
x = torch.abs(x)
return to_result_dtype(torch.pow(reduce_sum(torch.pow(x, ord)), 1.0 / ord)) # type: ignore[return-value]
def _backshift_permutation(dim0, dim1, ndim):
# Auxiliary function for matrix_norm
# Computes the permutation that moves the two given dimensions to the back
ret = [i for i in range(ndim) if i != dim0 and i != dim1]
ret.extend((dim0, dim1))
return ret
def _inverse_permutation(perm):
# Given a permutation, returns its inverse. It's equivalent to argsort on an array
return [i for i, j in sorted(enumerate(perm), key=operator.itemgetter(1))]
# CompositeImplicitAutograd
@out_wrapper(exact_dtype=True)
def matrix_norm(
A: TensorLikeType,
ord: Union[float, str] = "fro",
dim: DimsType = (-2, -1),
keepdim: bool = False,
*,
dtype: Optional[torch.dtype] = None,
) -> TensorLikeType:
# shape
check_is_matrix(A, "linalg.matrix_norm")
# dim
dim = utils.canonicalize_dims(A.ndim, dim)
if isinstance(dim, Dim):
dim = (dim,) # type: ignore[assignment]
torch._check(
len(dim) == 2, lambda: "linalg.matrix_norm: dim must be a 2-tuple. Got {dim}"
)
torch._check(
dim[0] != dim[1],
lambda: "linalg.matrix_norm: dims must be different. Got ({dim[0]}, {dim[1]})",
)
# dtype arg
_check_norm_dtype(dtype, A.dtype, "linalg.matrix_norm")
if isinstance(ord, str):
# ord
torch._check(
ord in ("fro", "nuc"),
lambda: "linalg.matrix_norm: Order {ord} not supported.",
)
# dtype
check_fp_or_complex(
A.dtype, "linalg.matrix_norm", allow_low_precision_dtypes=ord != "nuc"
)
if ord == "fro":
return vector_norm(A, 2, dim, keepdim, dtype=dtype)
else: # ord == "nuc"
if dtype is not None:
A = _maybe_convert_to_dtype(A, dtype) # type: ignore[assignment]
perm = _backshift_permutation(dim[0], dim[1], A.ndim)
result = torch.sum(svdvals(prims.transpose(A, perm)), -1, keepdim)
if keepdim:
inv_perm = _inverse_permutation(perm)
result = prims.transpose(torch.unsqueeze(result, -1), inv_perm)
return result
else:
# ord
abs_ord = abs(ord)
torch._check(
abs_ord in (2, 1, float("inf")),
lambda: "linalg.matrix_norm: Order {ord} not supported.",
)
# dtype
check_fp_or_complex(
A.dtype, "linalg.matrix_norm", allow_low_precision_dtypes=ord != 2
)
max_min = partial(torch.amax if ord > 0.0 else torch.amin, keepdim=keepdim)
if abs_ord == 2.0:
if dtype is not None:
A = _maybe_convert_to_dtype(A, dtype) # type: ignore[assignment]
perm = _backshift_permutation(dim[0], dim[1], A.ndim)
result = max_min(svdvals(prims.transpose(A, perm)), dim=-1)
if keepdim:
inv_perm = _inverse_permutation(perm)
result = prims.transpose(torch.unsqueeze(result, -1), inv_perm)
return result
else: # 1, -1, inf, -inf
dim0, dim1 = dim
if abs_ord == float("inf"):
dim0, dim1 = dim1, dim0
if not keepdim and (dim0 < dim1):
dim1 -= 1
return max_min(
vector_norm(A, 1.0, dim=dim0, keepdim=keepdim, dtype=dtype), dim1
)
# CompositeImplicitAutograd
@out_wrapper(exact_dtype=True)
def norm(
A: TensorLikeType,
ord: Optional[Union[float, str]] = None,
dim: Optional[DimsType] = None,
keepdim: bool = False,
*,
dtype: Optional[torch.dtype] = None,
) -> TensorLikeType:
if dim is not None:
if isinstance(dim, Dim):
dim = (dim,) # type: ignore[assignment]
torch._check(
len(dim) in (1, 2),
lambda: "linalg.norm: If dim is specified, it must be of length 1 or 2. Got {dim}",
)
elif ord is not None:
torch._check(
A.ndim in (1, 2),
lambda: "linalg.norm: If dim is not specified but ord is, the input must be 1D or 2D. Got {A.ndim}D",
)
if ord is not None and (
(dim is not None and len(dim) == 2) or (dim is None and A.ndim == 2)
):
if dim is None:
dim = (0, 1)
return matrix_norm(A, ord, dim, keepdim, dtype=dtype)
else:
if ord is None:
ord = 2.0
return vector_norm(A, ord, dim, keepdim, dtype=dtype) # type: ignore[arg-type]
# CompositeImplicitAutograd
@out_wrapper("U", "S", "Vh", exact_dtype=True)
def svd(A: TensorLikeType, full_matrices: bool = True) -> Tuple[Tensor, Tensor, Tensor]:
return prims.svd(A, full_matrices=full_matrices)
# CompositeImplicitAutograd
@out_wrapper(exact_dtype=True)
def svdvals(A: TensorLikeType) -> Tensor:
return svd(A, full_matrices=False)[1]
# CompositeImplicitAutograd
@out_wrapper()
@elementwise_type_promotion_wrapper(
type_promoting_args=("x", "y"),
type_promotion_kind=ELEMENTWISE_TYPE_PROMOTION_KIND.DEFAULT,
)
def vecdot(x: Tensor, y: Tensor, dim: int = -1) -> Tensor:
check_fp_or_complex(x.dtype, "linalg.vecdot")
return (x.conj() * y).sum(dim=dim)
|