Skip to content
This repository was archived by the owner on Nov 7, 2024. It is now read-only.
Merged
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
14 changes: 14 additions & 0 deletions tensornetwork/backends/abstract_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,3 +1030,17 @@ def cholesky(self,
Tuple[Tensor, Tensor]:
raise NotImplementedError(
f"Backend {self.name} has not implemented cholesky.")

def eps(self, dtype: Type[np.number]) -> float:
"""
Return machine epsilon for given `dtype`

Args:
dtype: A dtype.

Returns:
float: Machine epsilon.
"""

raise NotImplementedError(
f"Backend {self.name} has not implemented eps.")
12 changes: 12 additions & 0 deletions tensornetwork/backends/jax/jax_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,3 +888,15 @@ def power(self, a: Tensor, b: Union[Tensor, float]) -> Tensor:
b: The tensor that contains the exponent or a single scalar.
"""
return jnp.power(a, b)

def eps(self, dtype: Type[np.number]) -> float:
"""
Return machine epsilon for given `dtype`

Args:
dtype: A dtype.

Returns:
float: Machine epsilon.
"""
return jnp.finfo(dtype).eps
6 changes: 5 additions & 1 deletion tensornetwork/backends/jax/jax_backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1254,4 +1254,8 @@ def test_power(dtype):
actual = backend.power(base_tensor, power)
expected = jax.numpy.power(base_tensor, power)
np.testing.assert_allclose(expected, actual)


@pytest.mark.parametrize("dtype", np_dtypes)
def test_eps(dtype):
backend = jax_backend.JaxBackend()
assert backend.eps(dtype) == np.finfo(dtype).eps
24 changes: 18 additions & 6 deletions tensornetwork/backends/numpy/numpy_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,24 +762,36 @@ def deserialize_tensor(self, s: str) -> Tensor:

def power(self, a: Tensor, b: Union[Tensor, float]) -> Tensor:
"""
Returns the exponentiation of tensor a raised to b.
If b is a tensor, then the exponentiation is element-wise
Returns the exponentiation of tensor a raised to b.
If b is a tensor, then the exponentiation is element-wise
between the two tensors, with a as the base and b as the power.
Note that a and b must be broadcastable to the same shape if
Note that a and b must be broadcastable to the same shape if
b is a tensor.
If b is a scalar, then the exponentiation is each value in a
raised to the power of b.

Args:
a: The tensor containing the bases.
b: The tensor containing the powers; or a single scalar as the power.

Returns:
The tensor that is each element of a raised to the
The tensor that is each element of a raised to the
power of b. Note that the shape of the returned tensor
is that produced by the broadcast of a and b.
"""
return np.power(a, b)

def item(self, tensor):
return tensor.item()

def eps(self, dtype: Type[np.number]) -> float:
"""
Return machine epsilon for given `dtype`

Args:
dtype: A dtype.

Returns:
float: Machine epsilon.
"""
return np.finfo(dtype).eps
5 changes: 5 additions & 0 deletions tensornetwork/backends/numpy/numpy_backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,3 +969,8 @@ def test_item(dtype):
backend = numpy_backend.NumPyBackend()
tensor = backend.randn((1,), dtype=dtype, seed=10)
assert tensor.item() == backend.item(tensor)

@pytest.mark.parametrize("dtype", np_dtypes)
def test_eps(dtype):
backend = numpy_backend.NumPyBackend()
assert backend.eps(dtype) == np.finfo(dtype).eps
14 changes: 13 additions & 1 deletion tensornetwork/backends/pytorch/pytorch_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,18 @@ def sign(self, tensor: Tensor) -> Tensor:
tensor: The input tensor.
"""
return torchlib.sign(tensor)

def item(self, tensor):
return tensor.item()

def eps(self, dtype: Type[np.number]) -> float:
"""
Return machine epsilon for given `dtype`

Args:
dtype: A dtype.

Returns:
float: Machine epsilon.
"""
return torchlib.finfo(dtype).eps
5 changes: 5 additions & 0 deletions tensornetwork/backends/pytorch/pytorch_backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,3 +670,8 @@ def test_item(dtype):
backend = pytorch_backend.PyTorchBackend()
tensor = backend.randn((1,), dtype=dtype, seed=10)
assert backend.item(tensor) == tensor.item()

@pytest.mark.parametrize("dtype", torch_randn_dtypes)
def test_eps(dtype):
backend = pytorch_backend.PyTorchBackend()
assert backend.eps(dtype) == torch.finfo(dtype).eps
12 changes: 12 additions & 0 deletions tensornetwork/backends/symmetric/symmetric_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,3 +689,15 @@ def matmul(self, tensor1: Tensor, tensor2: Tensor):
if (tensor1.ndim != 2) or (tensor2.ndim != 2):
raise ValueError("inputs to `matmul` have to be matrices")
return tensor1 @ tensor2

def eps(self, dtype: Type[numpy.number]) -> float:
"""
Return machine epsilon for given `dtype`

Args:
dtype: A dtype.

Returns:
float: Machine epsilon.
"""
return numpy.finfo(dtype).eps
5 changes: 5 additions & 0 deletions tensornetwork/backends/symmetric/symmetric_backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1715,3 +1715,8 @@ def test_matmul_raises():
B = BlockSparseTensor.random(indices=inds2, dtype=dtype)
with pytest.raises(ValueError, match="inputs to"):
_ = backend.matmul(A, B)

@pytest.mark.parametrize("dtype", np_dtypes)
def test_eps(dtype):
backend = symmetric_backend.SymmetricBackend()
assert backend.eps(dtype) == np.finfo(dtype).eps
12 changes: 12 additions & 0 deletions tensornetwork/backends/tensorflow/tensorflow_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,15 @@ def power(self, a: Tensor, b: Union[Tensor, float]) -> Tensor:
is that produced by the broadcast of a and b.
"""
return tf.math.pow(a, b)

def eps(self, dtype: Type[np.number]) -> float:
"""
Return machine epsilon for given `dtype`

Args:
dtype: A dtype.

Returns:
float: Machine epsilon.
"""
return tf.experimental.numpy.finfo(dtype).eps
5 changes: 5 additions & 0 deletions tensornetwork/backends/tensorflow/tensorflow_backend_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,8 @@ def test_power(dtype):
actual = backend.power(base_tensor, power)
expected = tf.math.pow(base_tensor, power)
np.testing.assert_allclose(expected, actual)

@pytest.mark.parametrize("dtype", tf_dtypes)
def test_eps(dtype):
backend = tensorflow_backend.TensorFlowBackend()
assert backend.eps(dtype) == tf.experimental.numpy.finfo(dtype).eps