pytorch/caffe2/python/operator_test/scale_op_test.py
Bugra Akyildiz 27c7158166 Remove __future__ imports for legacy Python2 supports (#45033)
Summary:
There is a module called `2to3` which you can target for future specifically to remove these, the directory of `caffe2` has the most redundant imports:

```2to3 -f future -w caffe2```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/45033

Reviewed By: seemethere

Differential Revision: D23808648

Pulled By: bugra

fbshipit-source-id: 38971900f0fe43ab44a9168e57f2307580d36a38
2020-09-23 17:57:02 -07:00

64 lines
2.1 KiB
Python

from caffe2.python import core, workspace
import caffe2.python.hypothesis_test_util as hu
import caffe2.python.serialized_test.serialized_test_util as serial
import hypothesis.strategies as st
import numpy as np
class TestScaleOps(serial.SerializedTestCase):
@serial.given(dim=st.sampled_from([[1, 386, 1], [386, 1, 1],
[1, 256, 1], [256, 1, 1],
[1024, 256, 1], [1, 1024, 1],
[1, 1, 1]]),
scale=st.floats(0.0, 10.0),
num_tensors=st.integers(1, 10),
**hu.gcs)
def test_scale_ops(self, dim, scale, num_tensors, gc, dc):
in_tensors = []
in_tensor_ps = []
out_tensors = []
out_ref_tensors = []
# initialize tensors
for i in range(num_tensors):
tensor = "X_{}".format(i)
X = np.random.rand(*dim).astype(np.float32) - 0.5
in_tensors.append(tensor)
in_tensor_ps.append(X)
out_tensor = "O_{}".format(i)
out_tensors.append(out_tensor)
workspace.FeedBlob(tensor, X, device_option=gc)
# run ScaleBlobs operator
scale_blobs_op = core.CreateOperator(
"ScaleBlobs",
in_tensors,
out_tensors,
scale=scale,
)
scale_blobs_op.device_option.CopyFrom(gc)
workspace.RunOperatorOnce(scale_blobs_op)
# run Scale op for each tensor and compare with ScaleBlobs
for i in range(num_tensors):
tensor = "X_{}".format(i)
out_ref_tensor = "O_ref_{}".format(i)
scale_op = core.CreateOperator(
"Scale",
[tensor],
[out_ref_tensor],
scale=scale,
)
scale_op.device_option.CopyFrom(gc)
workspace.RunOperatorOnce(scale_op)
o_ref = workspace.FetchBlob(out_ref_tensor)
o = workspace.FetchBlob(out_tensors[i])
np.testing.assert_allclose(o, o_ref)
if __name__ == '__main__':
unittest.main()