Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyO3: Always broadcast +,-, / ,* #1101

Merged
Merged
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
Always broadcast magic methods
  • Loading branch information
LLukas22 committed Oct 15, 2023
commit 8cc03421de715cca950f6618cac956532d52039a
8 changes: 4 additions & 4 deletions candle-pyo3/src/lib.rs
Original file line number Diff line number Diff line change
@@ -536,7 +536,7 @@ impl PyTensor {
/// &RETURNS&: Tensor
fn __add__(&self, rhs: &PyAny) -> PyResult<Self> {
let tensor = if let Ok(rhs) = rhs.extract::<Self>() {
(&self.0 + &rhs.0).map_err(wrap_err)?
self.0.broadcast_add(&rhs.0).map_err(wrap_err)?
} else if let Ok(rhs) = rhs.extract::<f64>() {
(&self.0 + rhs).map_err(wrap_err)?
} else {
@@ -553,7 +553,7 @@ impl PyTensor {
/// &RETURNS&: Tensor
fn __mul__(&self, rhs: &PyAny) -> PyResult<Self> {
let tensor = if let Ok(rhs) = rhs.extract::<Self>() {
(&self.0 * &rhs.0).map_err(wrap_err)?
self.0.broadcast_mul(&rhs.0).map_err(wrap_err)?
} else if let Ok(rhs) = rhs.extract::<f64>() {
(&self.0 * rhs).map_err(wrap_err)?
} else {
@@ -570,7 +570,7 @@ impl PyTensor {
/// &RETURNS&: Tensor
fn __sub__(&self, rhs: &PyAny) -> PyResult<Self> {
let tensor = if let Ok(rhs) = rhs.extract::<Self>() {
(&self.0 - &rhs.0).map_err(wrap_err)?
self.0.broadcast_sub(&rhs.0).map_err(wrap_err)?
} else if let Ok(rhs) = rhs.extract::<f64>() {
(&self.0 - rhs).map_err(wrap_err)?
} else {
@@ -583,7 +583,7 @@ impl PyTensor {
/// &RETURNS&: Tensor
fn __truediv__(&self, rhs: &PyAny) -> PyResult<Self> {
let tensor = if let Ok(rhs) = rhs.extract::<Self>() {
(&self.0 / &rhs.0).map_err(wrap_err)?
self.0.broadcast_div(&rhs.0).map_err(wrap_err)?
} else if let Ok(rhs) = rhs.extract::<f64>() {
(&self.0 / rhs).map_err(wrap_err)?
} else {
73 changes: 73 additions & 0 deletions candle-pyo3/tests/native/test_tensor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import candle
from candle import Tensor
import pytest


def test_tensor_can_be_constructed():
@@ -72,3 +73,75 @@ def test_tensor_can_be_scliced_3d():
assert t[:, 0, 0].values() == [1, 9]
assert t[..., 0].values() == [[1, 5], [9, 13]]
assert t[..., 0:2].values() == [[[1, 2], [5, 6]], [[9, 10], [13, 14]]]


def test_tensor_can_be_added():
t = Tensor(42.0)
result = t + t
assert result.values() == 84.0
result = t + 2.0
assert result.values() == 44.0
a = candle.rand((3, 1, 4))
b = candle.rand((2, 1))
c_native = a.broadcast_add(b)
c = a + b
assert c.shape == (3, 2, 4)
assert c.values() == c_native.values()
with pytest.raises(ValueError):
d = candle.rand((3, 4, 5))
e = candle.rand((4, 6))
f = d + e


def test_tensor_can_be_subtracted():
t = Tensor(42.0)
result = t - t
assert result.values() == 0
result = t - 2.0
assert result.values() == 40.0
a = candle.rand((3, 1, 4))
b = candle.rand((2, 1))
c_native = a.broadcast_sub(b)
c = a - b
assert c.shape == (3, 2, 4)
assert c.values() == c_native.values()
with pytest.raises(ValueError):
d = candle.rand((3, 4, 5))
e = candle.rand((4, 6))
f = d - e


def test_tensor_can_be_multiplied():
t = Tensor(42.0)
result = t * t
assert result.values() == 1764.0
result = t * 2.0
assert result.values() == 84.0
a = candle.rand((3, 1, 4))
b = candle.rand((2, 1))
c_native = a.broadcast_mul(b)
c = a * b
assert c.shape == (3, 2, 4)
assert c.values() == c_native.values()
with pytest.raises(ValueError):
d = candle.rand((3, 4, 5))
e = candle.rand((4, 6))
f = d * e


def test_tensor_can_be_divided():
t = Tensor(42.0)
result = t / t
assert result.values() == 1.0
result = t / 2.0
assert result.values() == 21.0
a = candle.rand((3, 1, 4))
b = candle.rand((2, 1))
c_native = a.broadcast_div(b)
c = a / b
assert c.shape == (3, 2, 4)
assert c.values() == c_native.values()
with pytest.raises(ValueError):
d = candle.rand((3, 4, 5))
e = candle.rand((4, 6))
f = d / e