mirror of
https://github.com/pytorch/pytorch.git
synced 2025-11-06 09:17:11 +08:00
Summary: Followup to [the serialized test framework](https://github.com/pytorch/pytorch/pull/10594) Round 1 for refactoring tests, starting alphabetically. I added some functionality, so I wanted to send out some of these initial changes sooner. I'm skipping all tests that don't explicitly call assertReferenceChecks. Some tests directly call np.allclose, and others are simply TestCase (rather than HypothesisTestCase). 1. Start alphabetically producing serialized outputs for test functions, annotating those we want to include with `serialized_test_util.given`. So far I've only added one test per operator, but this already does seem to add quite a few tests. 2. Add functionality to allow us to generate outputs using pytest by adding pytest argument options. This allows us to skip adding a `__main__` function to quite a few tests. 3. Catch any exceptions generating the gradient operator and skip serializing/reading it, since certain operators don't have gradients. 4. Add functionality to better handle jagged array inputs, which numpy doesn't handle very well. We simply explicitly do the conversion to dtype=object. 5. Make only one file per test function, rather than 4, to reduce the number of files in the github repo. I also noticed that there is some hypothesis handling that makes `serialized_test_util.given` not compatible with adding more hypothesis decorators on top. For example, there are tests that do ``` settings(...) given(...) def test_my_stuff(...) ``` But there is a hypothesis handler that explicitly checks that `given` is called below `settings`, so we cannot refactor this to `serialized_test_util.given`. I've just avoided decorating these kinds of tests for now, I hope that's alright. Pull Request resolved: https://github.com/pytorch/pytorch/pull/11350 Reviewed By: houseroad Differential Revision: D9693857 Pulled By: ajyu fbshipit-source-id: a9b4279afbe51c90cf2025c5ac6b2db2111f4af7
66 lines
1.6 KiB
Python
66 lines
1.6 KiB
Python
from __future__ import absolute_import
|
|
from __future__ import division
|
|
from __future__ import print_function
|
|
from __future__ import unicode_literals
|
|
|
|
from caffe2.python import core
|
|
import caffe2.python.hypothesis_test_util as hu
|
|
import caffe2.python.serialized_test.serialized_test_util as serial
|
|
|
|
from hypothesis import given
|
|
import hypothesis.strategies as st
|
|
import numpy as np
|
|
import unittest
|
|
|
|
|
|
class TestMean(serial.SerializedTestCase):
|
|
@serial.given(
|
|
k=st.integers(1, 5),
|
|
n=st.integers(1, 10),
|
|
m=st.integers(1, 10),
|
|
in_place=st.booleans(),
|
|
seed=st.integers(0, 2**32 - 1),
|
|
**hu.gcs
|
|
)
|
|
def test_mean(self, k, n, m, in_place, seed, gc, dc):
|
|
np.random.seed(seed)
|
|
input_names = []
|
|
input_vars = []
|
|
|
|
for i in range(k):
|
|
X_name = 'X' + str(i)
|
|
input_names.append(X_name)
|
|
var = np.random.randn(n, m).astype(np.float32)
|
|
input_vars.append(var)
|
|
|
|
def mean_ref(*args):
|
|
return [np.mean(args, axis=0)]
|
|
|
|
op = core.CreateOperator(
|
|
"Mean",
|
|
input_names,
|
|
['Y' if not in_place else 'X0'],
|
|
)
|
|
|
|
self.assertReferenceChecks(
|
|
device_option=gc,
|
|
op=op,
|
|
inputs=input_vars,
|
|
reference=mean_ref,
|
|
)
|
|
|
|
self.assertGradientChecks(
|
|
device_option=gc,
|
|
op=op,
|
|
inputs=input_vars,
|
|
outputs_to_check=0,
|
|
outputs_with_grads=[0],
|
|
)
|
|
|
|
self.assertDeviceChecks(dc, op, input_vars, [0])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|
|
|