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

dispatch special cases for x ** 1 and x ** 2 #266

Merged
merged 1 commit into from
Jul 29, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/mygrad/tensor_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

from functools import wraps
from numbers import Number
from typing import Optional, Set, Type, Union

import numpy as np
Expand All @@ -19,6 +20,7 @@
Negative,
Positive,
Power,
Square,
Subtract,
)
from mygrad.operation_base import BroadcastableOp, Operation
Expand Down Expand Up @@ -672,6 +674,14 @@ def __rmatmul__(self, other):
return self._op(MatMul, other, self)

def __pow__(self, other):
if isinstance(other, Number) or (
isinstance(other, np.ndarray) and other.ndim == 0
):
if other == 1:
return self._op(Positive, self)
elif other == 2:
return self._op(Square, self)

return self._op(Power, self, other)

def __rpow__(self, other):
Expand Down
2 changes: 1 addition & 1 deletion tests/tensor_base/test_graph_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_op_tracks_graph():
h = z - f
assert h.creator.graph == {h.creator} | f.creator.graph

i = ((h + 3) ** 2) / 5
i = ((h + 3) ** 4) / 5
assert h.creator.graph < i.creator.graph
assert (
len(i.creator.graph - h.creator.graph) == 3
Expand Down
72 changes: 72 additions & 0 deletions tests/tensor_base/test_pow_special_cases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from functools import partial

import hypothesis.strategies as st
import numpy as np
import pytest
from hypothesis import given

import mygrad as mg
from mygrad.math.arithmetic.ops import Positive, Square

from ..wrappers.uber import backprop_test_factory, fwdprop_test_factory


def custom_pow(x, p, constant=False):
out = x ** p
if isinstance(out, mg.Tensor):
out._constant = constant
return out


def any_scalar(*args, p):
return st.sampled_from([int(p), float(p), np.array(p)])


@pytest.mark.parametrize("power, op", [(1, Positive), (2, Square)])
def test_pow_uses_special_case(power, op):
@given(exp=st.sampled_from([int(power), float(power), np.array(power)]))
def wrapped_func(exp):
out = mg.arange(2) ** exp
assert isinstance(out.creator, op)

wrapped_func()


@fwdprop_test_factory(
mygrad_func=custom_pow,
true_func=custom_pow,
num_arrays=1,
kwargs={"p": partial(any_scalar, p=1)},
)
def test_pow_1_fwd():
pass


@backprop_test_factory(
mygrad_func=custom_pow,
true_func=custom_pow,
num_arrays=1,
kwargs={"p": partial(any_scalar, p=1)},
)
def test_pow_1_bkwd():
pass


@fwdprop_test_factory(
mygrad_func=custom_pow,
true_func=custom_pow,
num_arrays=1,
kwargs={"p": partial(any_scalar, p=2)},
)
def test_pow_2_fwd():
pass


@backprop_test_factory(
mygrad_func=custom_pow,
true_func=custom_pow,
num_arrays=1,
kwargs={"p": partial(any_scalar, p=2)},
)
def test_pow_2_bkwd():
pass