File size: 14,412 Bytes
aa654a4 | 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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | # tensor_ops.py
"""
Provides a library of robust tensor operations for Tensorus.
This module defines a static class `TensorOps` containing methods for common
tensor manipulations, including arithmetic, linear algebra, reshaping, and
more advanced operations. It emphasizes shape checking and error handling.
Future Enhancements:
- Add more advanced operations (FFT, specific convolutions).
- Implement optional automatic broadcasting checks.
- Add support for sparse tensors.
- Optimize operations further (e.g., using custom kernels if needed).
"""
import torch
import logging
from typing import Tuple, Optional, List, Union
# Configure basic logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
class TensorOps:
"""
A static library class providing robust tensor operations.
All methods are static and operate on provided torch.Tensor objects.
"""
@staticmethod
def _check_tensor(*tensors: torch.Tensor) -> None:
"""Internal helper to check if inputs are PyTorch tensors."""
for i, t in enumerate(tensors):
if not isinstance(t, torch.Tensor):
raise TypeError(f"Input at index {i} is not a torch.Tensor, but {type(t)}.")
@staticmethod
def _check_shape(tensor: torch.Tensor, expected_shape: Tuple[Optional[int], ...], op_name: str) -> None:
"""Internal helper to check tensor shape against an expected shape with wildcards (None)."""
TensorOps._check_tensor(tensor)
actual_shape = tensor.shape
if len(actual_shape) != len(expected_shape):
raise ValueError(f"{op_name} expects a tensor with {len(expected_shape)} dimensions, but got {len(actual_shape)} dimensions (shape {actual_shape}). Expected pattern: {expected_shape}")
for i, (actual_dim, expected_dim) in enumerate(zip(actual_shape, expected_shape)):
if expected_dim is not None and actual_dim != expected_dim:
raise ValueError(f"{op_name} expects dimension {i} to be {expected_dim}, but got {actual_dim}. Actual shape: {actual_shape}. Expected pattern: {expected_shape}")
logging.debug(f"{op_name} shape check passed for tensor with shape {actual_shape}")
# --- Arithmetic Operations ---
@staticmethod
def add(t1: torch.Tensor, t2: Union[torch.Tensor, float, int]) -> torch.Tensor:
"""Element-wise addition with type checking."""
TensorOps._check_tensor(t1)
if isinstance(t2, torch.Tensor):
TensorOps._check_tensor(t2)
try:
return torch.add(t1, t2)
except RuntimeError as e:
logging.error(f"Error during addition: {e}. t1 shape: {t1.shape}, t2 type: {type(t2)}, t2 shape (if tensor): {t2.shape if isinstance(t2, torch.Tensor) else 'N/A'}")
raise e
@staticmethod
def subtract(t1: torch.Tensor, t2: Union[torch.Tensor, float, int]) -> torch.Tensor:
"""Element-wise subtraction with type checking."""
TensorOps._check_tensor(t1)
if isinstance(t2, torch.Tensor):
TensorOps._check_tensor(t2)
try:
return torch.subtract(t1, t2)
except RuntimeError as e:
logging.error(f"Error during subtraction: {e}. t1 shape: {t1.shape}, t2 type: {type(t2)}, t2 shape (if tensor): {t2.shape if isinstance(t2, torch.Tensor) else 'N/A'}")
raise e
@staticmethod
def multiply(t1: torch.Tensor, t2: Union[torch.Tensor, float, int]) -> torch.Tensor:
"""Element-wise multiplication with type checking."""
TensorOps._check_tensor(t1)
if isinstance(t2, torch.Tensor):
TensorOps._check_tensor(t2)
try:
return torch.multiply(t1, t2)
except RuntimeError as e:
logging.error(f"Error during multiplication: {e}. t1 shape: {t1.shape}, t2 type: {type(t2)}, t2 shape (if tensor): {t2.shape if isinstance(t2, torch.Tensor) else 'N/A'}")
raise e
@staticmethod
def divide(t1: torch.Tensor, t2: Union[torch.Tensor, float, int]) -> torch.Tensor:
"""Element-wise division with type checking and zero division check."""
TensorOps._check_tensor(t1)
if isinstance(t2, torch.Tensor):
TensorOps._check_tensor(t2)
if torch.any(t2 == 0):
logging.warning("Division by zero encountered in tensor division.")
# Depending on policy, could raise error or return inf/nan
# raise ValueError("Division by zero in tensor division.")
elif isinstance(t2, (int, float)):
if t2 == 0:
logging.error("Division by zero scalar.")
raise ValueError("Division by zero.")
else:
raise TypeError(f"Divisor must be a tensor or scalar, got {type(t2)}")
try:
return torch.divide(t1, t2)
except RuntimeError as e:
logging.error(f"Error during division: {e}. t1 shape: {t1.shape}, t2 type: {type(t2)}, t2 shape (if tensor): {t2.shape if isinstance(t2, torch.Tensor) else 'N/A'}")
raise e
# --- Matrix and Dot Operations ---
@staticmethod
def matmul(t1: torch.Tensor, t2: torch.Tensor) -> torch.Tensor:
"""Matrix multiplication (torch.matmul) with shape checks."""
TensorOps._check_tensor(t1, t2)
if t1.ndim < 1 or t2.ndim < 1:
raise ValueError(f"Matmul requires tensors with at least 1 dimension, got {t1.ndim} and {t2.ndim}")
# Basic check for standard 2D matrix multiplication
if t1.ndim == 2 and t2.ndim == 2:
if t1.shape[1] != t2.shape[0]:
raise ValueError(f"Matrix multiplication shape mismatch: t1 shape {t1.shape} (inner dim {t1.shape[1]}) and t2 shape {t2.shape} (inner dim {t2.shape[0]}) are incompatible.")
# Note: torch.matmul handles broadcasting and batch matmul, more complex checks could be added here.
try:
return torch.matmul(t1, t2)
except RuntimeError as e:
logging.error(f"Error during matmul: {e}. t1 shape: {t1.shape}, t2 shape: {t2.shape}")
raise e
@staticmethod
def dot(t1: torch.Tensor, t2: torch.Tensor) -> torch.Tensor:
"""Dot product (torch.dot) for 1D tensors."""
TensorOps._check_tensor(t1, t2)
TensorOps._check_shape(t1, (None,), "dot product input 1")
TensorOps._check_shape(t2, (None,), "dot product input 2")
if t1.shape[0] != t2.shape[0]:
raise ValueError(f"Dot product requires 1D tensors of the same size, got shapes {t1.shape} and {t2.shape}")
try:
return torch.dot(t1, t2)
except RuntimeError as e:
logging.error(f"Error during dot product: {e}. t1 shape: {t1.shape}, t2 shape: {t2.shape}")
raise e
# --- Reduction Operations ---
@staticmethod
def sum(tensor: torch.Tensor, dim: Optional[Union[int, Tuple[int, ...]]] = None, keepdim: bool = False) -> torch.Tensor:
"""Sum of tensor elements over given dimensions."""
TensorOps._check_tensor(tensor)
try:
return torch.sum(tensor, dim=dim, keepdim=keepdim)
except RuntimeError as e:
logging.error(f"Error during sum: {e}. tensor shape: {tensor.shape}, dim: {dim}")
raise e
@staticmethod
def mean(tensor: torch.Tensor, dim: Optional[Union[int, Tuple[int, ...]]] = None, keepdim: bool = False) -> torch.Tensor:
"""Mean of tensor elements over given dimensions."""
TensorOps._check_tensor(tensor)
try:
# Ensure float tensor for mean calculation
return torch.mean(tensor.float(), dim=dim, keepdim=keepdim)
except RuntimeError as e:
logging.error(f"Error during mean: {e}. tensor shape: {tensor.shape}, dim: {dim}")
raise e
@staticmethod
def min(tensor: torch.Tensor, dim: Optional[int] = None, keepdim: bool = False) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
"""Min of tensor elements over a given dimension."""
TensorOps._check_tensor(tensor)
try:
return torch.min(tensor, dim=dim, keepdim=keepdim)
except RuntimeError as e:
logging.error(f"Error during min: {e}. tensor shape: {tensor.shape}, dim: {dim}")
raise e
@staticmethod
def max(tensor: torch.Tensor, dim: Optional[int] = None, keepdim: bool = False) -> Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]:
"""Max of tensor elements over a given dimension."""
TensorOps._check_tensor(tensor)
try:
return torch.max(tensor, dim=dim, keepdim=keepdim)
except RuntimeError as e:
logging.error(f"Error during max: {e}. tensor shape: {tensor.shape}, dim: {dim}")
raise e
# --- Reshaping and Slicing ---
@staticmethod
def reshape(tensor: torch.Tensor, shape: Tuple[int, ...]) -> torch.Tensor:
"""Reshape tensor with validation."""
TensorOps._check_tensor(tensor)
try:
return torch.reshape(tensor, shape)
except RuntimeError as e:
logging.error(f"Error during reshape: {e}. Original shape: {tensor.shape}, target shape: {shape}")
raise ValueError(f"Cannot reshape tensor of shape {tensor.shape} to {shape}. {e}")
@staticmethod
def transpose(tensor: torch.Tensor, dim0: int, dim1: int) -> torch.Tensor:
"""Transpose tensor dimensions."""
TensorOps._check_tensor(tensor)
try:
return torch.transpose(tensor, dim0, dim1)
except Exception as e: # Catches index errors etc.
logging.error(f"Error during transpose: {e}. tensor shape: {tensor.shape}, dim0: {dim0}, dim1: {dim1}")
raise e
@staticmethod
def permute(tensor: torch.Tensor, dims: Tuple[int, ...]) -> torch.Tensor:
"""Permute tensor dimensions."""
TensorOps._check_tensor(tensor)
if len(dims) != tensor.ndim:
raise ValueError(f"Permute dims tuple length {len(dims)} must match tensor rank {tensor.ndim}")
if len(set(dims)) != len(dims) or not all(0 <= d < tensor.ndim for d in dims):
raise ValueError(f"Invalid permutation dims {dims} for tensor rank {tensor.ndim}")
try:
return tensor.permute(dims) # Use the method directly
except Exception as e:
logging.error(f"Error during permute: {e}. tensor shape: {tensor.shape}, dims: {dims}")
raise e
# --- Concatenation and Splitting ---
@staticmethod
def concatenate(tensors: List[torch.Tensor], dim: int = 0) -> torch.Tensor:
"""Concatenate tensors along a dimension with checks."""
if not tensors:
raise ValueError("Cannot concatenate an empty list of tensors.")
TensorOps._check_tensor(*tensors)
# Add checks for shape compatibility along non-concatenated dims if needed
try:
return torch.cat(tensors, dim=dim)
except RuntimeError as e:
shapes = [t.shape for t in tensors]
logging.error(f"Error during concatenation: {e}. Input shapes: {shapes}, dim: {dim}")
raise e
@staticmethod
def stack(tensors: List[torch.Tensor], dim: int = 0) -> torch.Tensor:
"""Stack tensors along a new dimension with checks."""
if not tensors:
raise ValueError("Cannot stack an empty list of tensors.")
TensorOps._check_tensor(*tensors)
# Add checks for shape equality if needed
try:
return torch.stack(tensors, dim=dim)
except RuntimeError as e:
shapes = [t.shape for t in tensors]
logging.error(f"Error during stack: {e}. Input shapes: {shapes}, dim: {dim}")
raise e
# --- Advanced Operations ---
@staticmethod
def einsum(equation: str, *tensors: torch.Tensor) -> torch.Tensor:
"""Einstein summation with type checking."""
TensorOps._check_tensor(*tensors)
try:
return torch.einsum(equation, *tensors)
except RuntimeError as e:
shapes = [t.shape for t in tensors]
logging.error(f"Error during einsum: {e}. Equation: '{equation}', Input shapes: {shapes}")
raise e
# Example Usage
if __name__ == "__main__":
t1 = torch.tensor([[1., 2.], [3., 4.]])
t2 = torch.tensor([[5., 6.], [7., 8.]])
t3 = torch.tensor([1., 2.])
t4 = torch.tensor([3., 4.])
print("--- Arithmetic ---")
print("Add:", TensorOps.add(t1, t2))
print("Subtract:", TensorOps.subtract(t1, 5.0))
print("Multiply:", TensorOps.multiply(t1, t2))
print("Divide:", TensorOps.divide(t1, 2.0))
try:
TensorOps.divide(t1, torch.tensor([[1., 0.], [1., 1.]]))
except ValueError as e:
print("Caught expected division by zero warning/error.") # Logging handles the warning
print("\n--- Matrix/Dot ---")
print("Matmul:", TensorOps.matmul(t1, t2))
print("Dot:", TensorOps.dot(t3, t4))
try:
TensorOps.matmul(t1, t3) # Incompatible shapes
except ValueError as e:
print(f"Caught expected matmul error: {e}")
print("\n--- Reduction ---")
print("Sum (all):", TensorOps.sum(t1))
print("Mean (dim 0):", TensorOps.mean(t1, dim=0))
print("Max (dim 1):", TensorOps.max(t1, dim=1)) # Returns (values, indices)
print("\n--- Reshaping ---")
print("Reshape:", TensorOps.reshape(t1, (4, 1)))
print("Transpose:", TensorOps.transpose(t1, 0, 1))
print("Permute:", TensorOps.permute(torch.rand(2,3,4), (1, 2, 0)).shape)
print("\n--- Concat/Stack ---")
print("Concatenate (dim 0):", TensorOps.concatenate([t1, t2], dim=0))
print("Stack (dim 0):", TensorOps.stack([t1, t1], dim=0)) # Stacks along new dim 0
print("\n--- Advanced ---")
print("Einsum (trace):", TensorOps.einsum('ii->', t1)) # Trace
print("Einsum (batch matmul):", TensorOps.einsum('bij,bjk->bik', torch.rand(5, 2, 3), torch.rand(5, 3, 4)).shape)
print("\n--- Error Handling Example ---")
try:
TensorOps.add(t1, "not a tensor")
except TypeError as e:
print(f"Caught expected type error: {e}")
try:
TensorOps._check_shape(t1, (None, 3), "Example Op") # Wrong dim size
except ValueError as e:
print(f"Caught expected shape error: {e}") |