from caffe2.python import core, workspace
from hypothesis import given, assume, settings
import caffe2.python.hypothesis_test_util as hu
import hypothesis.strategies as st
import numpy as np
import unittest
class TestElementwiseOps(hu.HypothesisTestCase):
@given(X=hu.tensor(dtype=np.float32), **hu.gcs)
@settings(deadline=10000)
def test_abs(self, X, gc, dc):
op = core.CreateOperator(
"Abs",
["X"],
["Y"],
)
def abs_ref(X):
return [np.absolute(X)]
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=[X],
reference=abs_ref,
ensure_outputs_are_inferred=True,
)
self.assertDeviceChecks(dc, op, [X], [0])
self.assertGradientChecks(gc, op, [X], 0, [0], ensure_outputs_are_inferred=True)
@given(X=hu.tensor(dtype=np.float32), inplace=st.booleans(), **hu.gcs)
@settings(deadline=10000)
def test_exp(self, X, inplace, gc, dc):
op = core.CreateOperator(
"Exp",
["X"],
["X"] if inplace else ["Y"],
)
def exp_ref(X):
return [np.exp(X)]
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=[X],
reference=exp_ref,
ensure_outputs_are_inferred=True,
)
self.assertDeviceChecks(dc, op, [X], [0])
self.assertGradientChecks(gc, op, [X], 0, [0], ensure_outputs_are_inferred=True)
@given(n=st.integers(0, 6), m=st.integers(4, 6),
seed=st.integers(0, 1000), **hu.gcs)
@settings(deadline=1000)
def test_log(self, n, m, gc, dc, seed):
np.random.seed(seed)
X = np.random.rand(n, m).astype(np.float32) + 1.0
def log_op(X):
return [np.log(X)]
op = core.CreateOperator(
"Log",
["X"],
["Z"]
)
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=[X],
reference=log_op,
ensure_outputs_are_inferred=True,
)
self.assertGradientChecks(
gc, op, [X], 0, [0], stepsize=1e-4, threshold=1e-2,
ensure_outputs_are_inferred=True)
@given(n=st.integers(0, 10), m=st.integers(4, 6),
d=st.integers(2, 3), seed=st.integers(0, 1000), **hu.gcs)
@settings(deadline=10000)
def test_powt(self, n, m, d, gc, dc, seed):
np.random.seed(seed)
X = np.random.rand(n, m, d).astype(np.float32) + 1.0
Y = np.random.rand(n, m, d).astype(np.float32) + 2.0
def powt_op(X, Y):
return [np.power(X, Y)]
#two gradients Y*X^(Y-1) and X^Y * ln(X)
def powt_grad(g_out, outputs, fwd_inputs):
[X, Y] = fwd_inputs
Z = outputs[0]
return ([Y * np.power(X, Y - 1), Z * np.log(X)] * g_out)
op = core.CreateOperator(
"Pow",
["X", "Y"],
["Z"]
)
self.assertReferenceChecks(device_option=gc,
op=op,
inputs=[X, Y],
reference=powt_op,
output_to_grad="Z",
grad_reference=powt_grad,
ensure_outputs_are_inferred=True)
@given(n=st.integers(0, 6), m=st.integers(4, 6),
seed=st.integers(0, 1000), **hu.gcs)
@settings(deadline=10000)
def test_sqr(self, n, m, gc, dc, seed):
np.random.seed(seed)
X = np.random.rand(n, m).astype(np.float32)
def sqr_op(X):
return [np.square(X)]
op = core.CreateOperator(
"Sqr",
["X"],
["Z"]
)
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=[X],
reference=sqr_op,
ensure_outputs_are_inferred=True,
)
self.assertGradientChecks(
gc, op, [X], 0, [0], stepsize=1e-4, threshold=1e-2,
ensure_outputs_are_inferred=True)
@given(
X=hu.tensor(
elements=hu.floats(min_value=0.1, max_value=10),
# allow empty tensor
min_value=0),
inplace=st.booleans(),
**hu.gcs
)
@settings(deadline=10000)
def test_sqrt(self, X, inplace, gc, dc):
def sqrt_op(X):
return [np.sqrt(X)]
op = core.CreateOperator(
"Sqrt",
["X"],
["X"] if inplace else ["Y"]
)
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=[X],
reference=sqrt_op,
ensure_outputs_are_inferred=True,
)
self.assertDeviceChecks(dc, op, [X], [0])
# stepsize need to be smaller than the possible minimum X, so the
# sqrt is well defined
self.assertGradientChecks(
gc, op, [X], 0, [0], stepsize=1e-2, ensure_outputs_are_inferred=True)
@given(X=hu.tensor(dtype=np.float32), inplace=st.booleans(), **hu.gcs)
@settings(deadline=10000)
def test_softsign(self, X, inplace, gc, dc):
op = core.CreateOperator(
"Softsign",
["X"],
["X"] if inplace else ["Y"],
)
def softsign_ref(X):
return [X / (1.0 + np.absolute(X))]
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=[X],
reference=softsign_ref,
ensure_outputs_are_inferred=True,
)
self.assertDeviceChecks(dc, op, [X], [0])
if not inplace:
self.assertGradientChecks(
gc, op, [X], 0, [0],
ensure_outputs_are_inferred=True,
)
@given(X=hu.tensor(elements=hu.floats(min_value=0.1, max_value=10.0), dtype=np.float32),
inplace=st.booleans(), **hu.gcs)
@settings(deadline=10000)
def test_rsqrt(self, X, inplace, gc, dc):
op = core.CreateOperator(
"Rsqrt",
["X"],
["X"] if inplace else ["Y"],
)
def rsqrt_ref(X):
return [1.0 / np.sqrt(X)]
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=[X],
reference=rsqrt_ref,
ensure_outputs_are_inferred=True,
)
self.assertDeviceChecks(dc, op, [X], [0])
self.assertGradientChecks(
gc, op, [X], 0, [0], stepsize=5e-3,
ensure_outputs_are_inferred=True,
)
@given(X=hu.tensor(dtype=np.float32), **hu.gcs)
@settings(deadline=10000)
def test_cube(self, X, gc, dc):
op = core.CreateOperator(
"Cube",
["X"],
["Y"],
)
def cube_ref(X):
return [np.power(X, 3)]
def cube_grad_ref(g_out, outputs, fwd_inputs):
dY = g_out
[X] = fwd_inputs
return [dY * np.square(X) * 3]
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=[X],
reference=cube_ref,
output_to_grad="Y",
grad_reference=cube_grad_ref,
ensure_outputs_are_inferred=True,
)
self.assertDeviceChecks(dc, op, [X], [0])
@given(X=hu.tensor(dtype=np.float32), in_place=st.booleans(), **hu.gcs)
@settings(deadline=10000)
def test_cbrt(self, X, in_place, gc, dc):
op = core.CreateOperator(
"Cbrt",
["X"],
["X"] if in_place else ["Y"],
)
def cbrt_ref(X):
return [np.cbrt(X)]
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=[X],
reference=cbrt_ref,
ensure_outputs_are_inferred=True,
)
@given(X=hu.tensor(elements=hu.floats(min_value=1.0, max_value=10.0), dtype=np.float32),
in_place=st.booleans(), **hu.gcs)
@settings(deadline=10000)
def test_cbrt_grad(self, X, in_place, gc, dc):
op = core.CreateOperator(
"Cbrt",
["X"],
["X"] if in_place else ["Y"],
)
self.assertGradientChecks(
gc, op, [X], 0, [0],
ensure_outputs_are_inferred=True,
)
self.assertGradientChecks(
gc, op, [-X], 0, [0],
ensure_outputs_are_inferred=True,
)
@given(n=st.integers(0, 6), m=st.integers(4, 6),
seed=st.integers(0, 1000), **hu.gcs)
@settings(deadline=10000)
def test_swish(self, n, m, gc, dc, seed):
np.random.seed(seed)
X = np.random.rand(n, m).astype(np.float32)
def swish(X):
return [np.divide(X, (1. + np.exp(-X)))]
op = core.CreateOperator(
"Swish",
["X"],
["Z"]
)
self.assertReferenceChecks(
device_option=gc,
op=op,
inputs=[X],
reference=swish,
ensure_outputs_are_inferred=True,
)
self.assertGradientChecks(
gc, op, [X], 0, [0], stepsize=1e-4, threshold=1e-2,
ensure_outputs_are_inferred=True)
@given(n=st.integers(0, 6), m=st.integers(4, 6),
seed=st.integers(0, 1000), **hu.gcs)
@settings(deadline=1000)
def test_swish_gradient_inplace(self, n, m, gc, dc, seed):
np.random.seed(seed)
def swish(X):
return [np.divide(X, (1. + np.exp(-X)))]
def swish_gradient(X, Y, dY):
return [dY * (Y + np.divide(1. - Y, 1. + np.exp(-X)))]
X = np.random.rand(n, m).astype(np.float32)
Y = swish(X)[0]
dY = np.random.rand(n, m).astype(np.float32)
op = core.CreateOperator(
"SwishGradient",
["X", "Y", "grad"],
"grad"
Loading ...