prepare removal of deprecated functionality in torch.testing (#87969)
_Redo of #86586 with all BC breaking changes granularly placed into separate commits._
---
Per title. Deprecation happened on Feb 25, 2022 in c6f1bbc0ac33be0c8ad9956e3fc15e78ddb6cb95, which made it into the 1.12 release. Since it is now 245 days later and the next release will be 1.14, the removals later in the stack comply with the [BC policy](https://github.com/pytorch/pytorch/wiki/PyTorch's-Python-Frontend-Backward-and-Forward-Compatibility-Policy#minimizing-the-disruption-of-bc-breaking-changes).
Pull Request resolved: https://github.com/pytorch/pytorch/pull/87969
Approved by: https://github.com/mruberry
diff --git a/test/distributed/algorithms/ddp_comm_hooks/test_ddp_hooks.py b/test/distributed/algorithms/ddp_comm_hooks/test_ddp_hooks.py
index ead934e..d3ea932 100644
--- a/test/distributed/algorithms/ddp_comm_hooks/test_ddp_hooks.py
+++ b/test/distributed/algorithms/ddp_comm_hooks/test_ddp_hooks.py
@@ -124,7 +124,7 @@
# Register hook case, get the hook grads.
hook_grads = self._get_grads(process_group, DDPCommHookType.ALLREDUCE)
- torch.testing.assert_allclose(hook_grads, reference_grads, rtol=1e-5, atol=0)
+ torch.testing.assert_close(hook_grads, reference_grads, rtol=1e-5, atol=0)
@requires_nccl()
@skip_if_lt_x_gpu(2)
@@ -141,7 +141,7 @@
# Register hook case, get the hook grads.
hook_grads = self._get_grads(process_group, DDPCommHookType.FP16_COMPRESS)
- torch.testing.assert_allclose(hook_grads, reference_grads, rtol=1e-5, atol=1e-4)
+ torch.testing.assert_close(hook_grads, reference_grads, rtol=1e-5, atol=1e-4)
@requires_nccl()
@skip_if_lt_x_gpu(2)
@@ -158,7 +158,7 @@
# Register hook case, get the hook grads.
hook_grads = self._get_grads(process_group, DDPCommHookType.QUANTIZE_PER_TENSOR)
- torch.testing.assert_allclose(hook_grads, reference_grads, rtol=1e-5, atol=1e-4)
+ torch.testing.assert_close(hook_grads, reference_grads, rtol=1e-5, atol=1e-4)
@requires_nccl()
@skip_if_lt_x_gpu(2)
@@ -177,7 +177,7 @@
process_group, DDPCommHookType.QUANTIZE_PER_CHANNEL
)
- torch.testing.assert_allclose(hook_grads, reference_grads, rtol=1e-5, atol=1e-4)
+ torch.testing.assert_close(hook_grads, reference_grads, rtol=1e-5, atol=1e-4)
@requires_nccl()
@@ -198,7 +198,7 @@
hook_grads.div_(self.world_size)
dist.all_reduce(hook_grads, group=process_group)
- torch.testing.assert_allclose(hook_grads, reference_grads, rtol=1e-5, atol=0)
+ torch.testing.assert_close(hook_grads, reference_grads, rtol=1e-5, atol=0)
@requires_nccl()
@skip_if_lt_x_gpu(2)
diff --git a/test/distributed/fsdp/test_fsdp_misc.py b/test/distributed/fsdp/test_fsdp_misc.py
index 234af4f..79ed6da 100644
--- a/test/distributed/fsdp/test_fsdp_misc.py
+++ b/test/distributed/fsdp/test_fsdp_misc.py
@@ -110,7 +110,7 @@
def _check_equal(local, fsdp):
with FSDP.summon_full_params(fsdp):
for p1, p2 in zip(fsdp.parameters(), local.parameters()):
- torch.testing.assert_allclose(p1, p2)
+ torch.testing.assert_close(p1, p2)
for sharding_strategy in [
ShardingStrategy.FULL_SHARD,
diff --git a/test/distributions/test_distributions.py b/test/distributions/test_distributions.py
index b6201d4..1270185 100644
--- a/test/distributions/test_distributions.py
+++ b/test/distributions/test_distributions.py
@@ -1421,7 +1421,7 @@
# theoretical results.
dist = Poisson(rate_zero)
dist.log_prob(torch.ones_like(rate_zero)).backward()
- torch.testing.assert_allclose(rate_zero.grad, torch.inf)
+ self.assertEqual(rate_zero.grad, torch.inf)
@unittest.skipIf(not TEST_NUMPY, "Numpy not found")
def test_poisson_sample(self):
diff --git a/test/functorch/functorch_additional_op_db.py b/test/functorch/functorch_additional_op_db.py
index b090121..6343e74 100644
--- a/test/functorch/functorch_additional_op_db.py
+++ b/test/functorch/functorch_additional_op_db.py
@@ -4,8 +4,7 @@
import torch
-from torch.testing import \
- (floating_types, floating_types_and, all_types_and_complex_and)
+from torch.testing._internal.common_dtype import floating_types, floating_types_and, all_types_and_complex_and
from torch.testing._internal.common_utils import make_tensor
from torch.testing._internal.common_methods_invocations import OpInfo, SampleInput, DecorateInfo
diff --git a/test/fx/test_fx_param_shape_control_flow.py b/test/fx/test_fx_param_shape_control_flow.py
index e9af35d..04db468 100644
--- a/test/fx/test_fx_param_shape_control_flow.py
+++ b/test/fx/test_fx_param_shape_control_flow.py
@@ -91,26 +91,26 @@
performs both mm and relu ops in cascade
"""
x = torch.randn(10, 5)
- torch.testing.assert_allclose(mm_only_mod(x), torch.mm(x, mm_only_mod.get_mul_matrix()))
+ torch.testing.assert_close(mm_only_mod(x), torch.mm(x, mm_only_mod.get_mul_matrix()))
tracer = torch.fx.Tracer(param_shapes_constant=True)
traced_graph = tracer.trace(mm_only_mod)
# verify the graph module calculates the same result
graph_mod_mm = torch.fx.GraphModule(mm_only_mod, traced_graph)
- torch.testing.assert_allclose(graph_mod_mm(x), torch.mm(x, mm_only_mod.get_mul_matrix()))
+ torch.testing.assert_close(graph_mod_mm(x), torch.mm(x, mm_only_mod.get_mul_matrix()))
# Make a new module with different parameter shape to go down the different
# code path
x = torch.randn(10, 15)
- torch.testing.assert_allclose(relu_mod(x), torch.relu(torch.mm(x, relu_mod.get_mul_matrix())))
+ torch.testing.assert_close(relu_mod(x), torch.relu(torch.mm(x, relu_mod.get_mul_matrix())))
tracer2 = torch.fx.Tracer(param_shapes_constant=True)
traced_graph2 = tracer2.trace(relu_mod)
# verify the graph module calculates the same result
graph_mod_relu = torch.fx.GraphModule(relu_mod, traced_graph2)
- torch.testing.assert_allclose(graph_mod_relu(x), torch.relu(torch.mm(x, relu_mod.get_mul_matrix())))
+ torch.testing.assert_close(graph_mod_relu(x), torch.relu(torch.mm(x, relu_mod.get_mul_matrix())))
graph1_node_targets = [n.target for n in traced_graph.nodes]
diff --git a/test/jit/test_misc.py b/test/jit/test_misc.py
index 98ec783..8a5d4ea 100644
--- a/test/jit/test_misc.py
+++ b/test/jit/test_misc.py
@@ -379,4 +379,4 @@
expected = foo(x, 0)
scripted = torch.jit.script(foo)
actual = scripted(x, 0)
- torch.testing.assert_allclose(expected, actual)
+ torch.testing.assert_close(expected, actual)
diff --git a/test/mobile/test_lite_script_type.py b/test/mobile/test_lite_script_type.py
index 9a778fb..8769a4b 100644
--- a/test/mobile/test_lite_script_type.py
+++ b/test/mobile/test_lite_script_type.py
@@ -28,7 +28,7 @@
buffer.seek(0)
mobile_module = _load_for_lite_interpreter(buffer) # Error here
mobile_module_result = mobile_module(sample_input).a
- torch.testing.assert_allclose(
+ torch.testing.assert_close(
script_module_result,
mobile_module_result
)
@@ -91,7 +91,7 @@
buffer_mobile.seek(0)
mobile_module = _load_for_lite_interpreter(buffer_mobile)
mobile_module_result = mobile_module(sample_input)
- torch.testing.assert_allclose(
+ torch.testing.assert_close(
script_module_result,
mobile_module_result
)
@@ -117,7 +117,7 @@
buffer_mobile.seek(0)
mobile_module = _load_for_lite_interpreter(buffer_mobile)
mobile_module_result = mobile_module(sample_input)
- torch.testing.assert_allclose(
+ torch.testing.assert_close(
script_module_result,
mobile_module_result
)
@@ -136,7 +136,7 @@
buffer_mobile.seek(0)
mobile_module = _load_for_lite_interpreter(buffer_mobile)
mobile_module_result = mobile_module(sample_input)
- torch.testing.assert_allclose(
+ torch.testing.assert_close(
script_module_result,
mobile_module_result
)
@@ -166,7 +166,7 @@
buffer_mobile.seek(0)
mobile_module = _load_for_lite_interpreter(buffer_mobile)
mobile_module_result = mobile_module(sample_input)
- torch.testing.assert_allclose(
+ torch.testing.assert_close(
script_module_result.baz.di,
mobile_module_result.baz.di
)
diff --git a/test/quantization/core/test_quantized_tensor.py b/test/quantization/core/test_quantized_tensor.py
index 35d3ba3..a204350 100644
--- a/test/quantization/core/test_quantized_tensor.py
+++ b/test/quantization/core/test_quantized_tensor.py
@@ -1461,7 +1461,7 @@
X = torch.randn(5 , 10)
quantized_X = X.to(torch.bfloat16)
dedequantized_X = quantized_X.to(torch.float32)
- torch.testing.assert_allclose(X, dedequantized_X, rtol=1e-4, atol=5e-3)
+ torch.testing.assert_close(X, dedequantized_X, rtol=1e-4, atol=5e-3)
def test_decomposed_quantize(self):
# register the ops
diff --git a/test/quantization/core/test_workflow_module.py b/test/quantization/core/test_workflow_module.py
index 7194872..6ac8bed 100644
--- a/test/quantization/core/test_workflow_module.py
+++ b/test/quantization/core/test_workflow_module.py
@@ -1011,11 +1011,11 @@
)
# Compare params with reference
- torch.testing.assert_allclose(out, out_ref)
- torch.testing.assert_allclose(
+ torch.testing.assert_close(out, out_ref)
+ torch.testing.assert_close(
running_min_op, mod.activation_post_process.min_val
)
- torch.testing.assert_allclose(
+ torch.testing.assert_close(
running_max_op, mod.activation_post_process.max_val
)
@@ -1066,11 +1066,11 @@
)
# Compare params with reference
- torch.testing.assert_allclose(out, out_ref)
- torch.testing.assert_allclose(
+ torch.testing.assert_close(out, out_ref)
+ torch.testing.assert_close(
running_min_op, mod.activation_post_process.min_val
)
- torch.testing.assert_allclose(
+ torch.testing.assert_close(
running_max_op, mod.activation_post_process.max_val
)
@@ -1095,12 +1095,12 @@
x = torch.randn(5, 5, device=device)
out = mod(x)
out_ref = mod_ref(x)
- torch.testing.assert_allclose(out, out_ref)
- torch.testing.assert_allclose(
+ torch.testing.assert_close(out, out_ref)
+ torch.testing.assert_close(
mod_ref.activation_post_process.min_val,
mod.activation_post_process.min_val,
)
- torch.testing.assert_allclose(
+ torch.testing.assert_close(
mod_ref.activation_post_process.max_val,
mod.activation_post_process.max_val,
)
@@ -1151,20 +1151,20 @@
False,
)
# Compare params with reference
- torch.testing.assert_allclose(out, out_ref)
+ torch.testing.assert_close(out, out_ref)
if mod.observer_enabled[0]:
- torch.testing.assert_allclose(
+ torch.testing.assert_close(
running_min_op, mod.activation_post_process.min_val
)
- torch.testing.assert_allclose(
+ torch.testing.assert_close(
running_max_op, mod.activation_post_process.max_val
)
if mod.fake_quant_enabled:
- torch.testing.assert_allclose(scale, mod.scale)
- torch.testing.assert_allclose(zero_point, mod.zero_point)
+ torch.testing.assert_close(scale, mod.scale)
+ torch.testing.assert_close(zero_point, mod.zero_point)
- torch.testing.assert_allclose(mod.state_dict()['activation_post_process.min_val'], running_min_op)
- torch.testing.assert_allclose(mod.state_dict()['activation_post_process.max_val'], running_max_op)
+ torch.testing.assert_close(mod.state_dict()['activation_post_process.min_val'], running_min_op)
+ torch.testing.assert_close(mod.state_dict()['activation_post_process.max_val'], running_max_op)
def test_fused_mod_reduce_range(self):
obs = FusedMovingAvgObsFakeQuantize(quant_min=0, quant_max=255, dtype=torch.quint8, reduce_range=True)
diff --git a/test/quantization/core/test_workflow_ops.py b/test/quantization/core/test_workflow_ops.py
index b459b58..a0687d8 100644
--- a/test/quantization/core/test_workflow_ops.py
+++ b/test/quantization/core/test_workflow_ops.py
@@ -1083,7 +1083,7 @@
self.assertEqual(in_running_min_ref, in_running_min_op)
self.assertEqual(in_running_max_ref, in_running_max_op)
- torch.testing.assert_allclose(out, x_in)
+ torch.testing.assert_close(out, x_in)
# Test empty input works
x = torch.empty(0, 5, device=device)
@@ -1176,7 +1176,7 @@
x_in = x
self.assertEqual(in_running_min_ref, in_running_min_op)
self.assertEqual(in_running_max_ref, in_running_max_op)
- torch.testing.assert_allclose(out, x_in)
+ torch.testing.assert_close(out, x_in)
@given(device=st.sampled_from(['cpu', 'cuda'] if torch.cuda.is_available() else ['cpu']),)
@settings(deadline=None)
@@ -1218,7 +1218,7 @@
False,
)
# verify the output matches
- torch.testing.assert_allclose(out, x_fake_quant)
+ torch.testing.assert_close(out, x_fake_quant)
# verify the gradient matches expectation of fake_quant op
dout = torch.rand_like(x, dtype=torch.float).to(device)
@@ -1264,7 +1264,7 @@
False,
)
# verify the output matches
- torch.testing.assert_allclose(out, x)
+ torch.testing.assert_close(out, x)
# verify the gradient matches expectation of fake_quant op
dout = torch.rand_like(x, dtype=torch.float).to(device)
diff --git a/test/quantization/fx/test_quantize_fx.py b/test/quantization/fx/test_quantize_fx.py
index 04109ce..2bc1ed4 100644
--- a/test/quantization/fx/test_quantize_fx.py
+++ b/test/quantization/fx/test_quantize_fx.py
@@ -8157,7 +8157,7 @@
inp = torch.randn(5, 5, device=device, requires_grad=True)
out_ref = prepared_ref(inp)
out = prepared(inp)
- torch.testing.assert_allclose(out, out_ref)
+ torch.testing.assert_close(out, out_ref)
# try backward pass
labels = torch.randn(5, 5, device=device)
@@ -8165,7 +8165,7 @@
grad = torch.autograd.grad(loss, [inp])
loss_ref = (out_ref - labels).sum()
grad_ref = torch.autograd.grad(loss_ref, [inp])
- torch.testing.assert_allclose(grad[0], grad_ref[0])
+ torch.testing.assert_close(grad[0], grad_ref[0])
if 'fbgemm' in torch.backends.quantized.supported_engines:
# During the lowering step in convert, fold_weight calls quantized::linear_prepack
@@ -8178,7 +8178,7 @@
out = converted(inp)
out_ref = converted_ref(inp)
- torch.testing.assert_allclose(out, out_ref)
+ torch.testing.assert_close(out, out_ref)
if __name__ == '__main__':
raise RuntimeError("This test file is not meant to be run directly, use:\n\n"
"\tpython test/test_quantization.py TESTNAME\n\n"
diff --git a/test/test_fx.py b/test/test_fx.py
index eac58fb..9a46a50 100644
--- a/test/test_fx.py
+++ b/test/test_fx.py
@@ -234,7 +234,7 @@
new_instance.__init__(gm3, gm3.graph)
x = torch.randn(5, 3)
- torch.testing.assert_allclose(new_instance(x), torch.relu(x))
+ torch.testing.assert_close(new_instance(x), torch.relu(x))
def test_custom_import(self):
graph = torch.fx.Graph()
@@ -809,7 +809,7 @@
traced = torch.fx.symbolic_trace(ec)
x = torch.randn(bs, d_hid)
- torch.testing.assert_allclose(ec(x), traced(x))
+ torch.testing.assert_close(ec(x), traced(x))
def test_node_tagging(self):
@@ -1126,7 +1126,7 @@
traced = torch.fx.symbolic_trace(foo)
x = (torch.randn(5, 3),)
- torch.testing.assert_allclose(traced(x), x[0])
+ torch.testing.assert_close(traced(x), x[0])
bio = io.BytesIO()
@@ -1136,7 +1136,7 @@
loaded = torch.load(bio)
- torch.testing.assert_allclose(loaded(x), x[0])
+ torch.testing.assert_close(loaded(x), x[0])
def test_torch_fx_len(self):
class FXLenTest(torch.nn.Module):
@@ -1806,7 +1806,7 @@
interp = Interpreter(gm)
x = torch.randn(5, 3)
out = interp.run(x)
- torch.testing.assert_allclose(out, x + 3.14159)
+ torch.testing.assert_close(out, x + 3.14159)
def test_interpreter_not_enough_args(self):
class Model(torch.nn.Module):
@@ -2315,8 +2315,8 @@
traced1.recompile()
x = torch.randn(15, 15)
- torch.testing.assert_allclose(traced1(x), torch.relu(x))
- torch.testing.assert_allclose(copied(x), torch.neg(x))
+ torch.testing.assert_close(traced1(x), torch.relu(x))
+ torch.testing.assert_close(copied(x), torch.neg(x))
def test_direct_param_use(self):
class TransposeTest(torch.nn.Module):
@@ -2699,7 +2699,7 @@
replica = gm._replicate_for_data_parallel()
out_replica = replica(x)
- torch.testing.assert_allclose(out_replica, out)
+ torch.testing.assert_close(out_replica, out)
def test_ast_rewriter_rewrites_assert(self):
class M(torch.nn.Module):
@@ -3045,7 +3045,7 @@
traced_graph = MyCustomTracer().trace(model)
gm2 = torch.fx.GraphModule(model, traced_graph)
gm2.delete_all_unused_submodules()
- torch.testing.assert_allclose(gm2(inputs), model(inputs))
+ torch.testing.assert_close(gm2(inputs), model(inputs))
def test_fx_stateless(self):
class MockModule(torch.nn.Module):
diff --git a/test/test_fx_experimental.py b/test/test_fx_experimental.py
index ae7a225..a8fc077 100644
--- a/test/test_fx_experimental.py
+++ b/test/test_fx_experimental.py
@@ -782,7 +782,7 @@
x = torch.randn(5, 3)
foo = torch.randn(5, 3)
- torch.testing.assert_allclose(split(x, foo=foo), traced(x, foo=foo))
+ torch.testing.assert_close(split(x, foo=foo), traced(x, foo=foo))
@skipIfNoTorchVision
def test_subgraph_trivial_resnet(self):
@@ -814,7 +814,7 @@
split = split_module(traced, mtt, lambda node: 0)
x = torch.randn(50, 512)
- torch.testing.assert_allclose(split(x), traced(x))
+ torch.testing.assert_close(split(x), traced(x))
def test_normalize_binary_operators(self):
ops_to_test = {
diff --git a/test/test_jit_autocast.py b/test/test_jit_autocast.py
index 93674bb..d311eb6 100644
--- a/test/test_jit_autocast.py
+++ b/test/test_jit_autocast.py
@@ -797,7 +797,7 @@
y = traced_model(x.clone())
with torch.cpu.amp.autocast(), torch.no_grad():
y2 = model(x.clone())
- torch.testing.assert_allclose(y.double(), y2.double(), rtol=1e-03, atol=1e-03)
+ torch.testing.assert_close(y.double(), y2.double(), rtol=1e-03, atol=1e-03)
for i in range(self.models.__len__()):
test_nchw_autocast_jit_trace_model(self.models[i], self.inputs[i])
@@ -812,7 +812,7 @@
y = traced_model(x.clone().to(memory_format=torch.channels_last))
with torch.cpu.amp.autocast(), torch.no_grad():
y2 = model(x.clone().to(memory_format=torch.channels_last))
- torch.testing.assert_allclose(y.double(), y2.double(), rtol=1e-03, atol=1e-03)
+ torch.testing.assert_close(y.double(), y2.double(), rtol=1e-03, atol=1e-03)
for i in range(self.models.__len__()):
if self.inputs[i].size().__len__() == 5:
# NHWC 3D case not support yet
diff --git a/test/test_jit_fuser_te.py b/test/test_jit_fuser_te.py
index e1c820f..1958529 100644
--- a/test/test_jit_fuser_te.py
+++ b/test/test_jit_fuser_te.py
@@ -2202,7 +2202,9 @@
def test(fn, args):
trace = torch.jit.trace(fn, args)
self.assertAllFused(trace.graph_for(*args))
- torch.testing.assert_allclose(fn(*args), trace(*args))
+ # TODO: Are `NaN`'s actually ok here or did this pass silently before, because `equal_nan=True` was the
+ # default?
+ torch.testing.assert_close(fn(*args), trace(*args), equal_nan=True)
def bn(i, x):
return torch.batch_norm(i, x, x, x, x, False, 0.1, 1e-4, False).relu()
diff --git a/test/test_module_init.py b/test/test_module_init.py
index dc05a95..98dcb3e 100644
--- a/test/test_module_init.py
+++ b/test/test_module_init.py
@@ -4,7 +4,7 @@
import torch
from unittest import mock
from unittest.mock import MagicMock, patch
-from torch.testing import floating_types
+from torch.testing._internal.common_dtype import floating_types
from torch.testing._internal.common_device_type import instantiate_device_type_tests, dtypes
from torch.testing._internal.common_quantization import skipIfNoFBGEMM
from torch.testing._internal.common_utils import TestCase, run_tests
diff --git a/test/test_mps.py b/test/test_mps.py
index c89a9d2..03410e2 100644
--- a/test/test_mps.py
+++ b/test/test_mps.py
@@ -65,7 +65,7 @@
return np.maximum(np_features, np.zeros(np_features.shape)).astype(np_features.dtype)
def testNpRelu(self):
- torch.testing.assert_allclose(
+ torch.testing.assert_close(
np.array([[0., 0.7, 0.0, 0.3, 0.0], [0.1, 0.0, 0.5, 0.0, 0.9]]),
self._npRelu(
np.array([[-0.9, 0.7, -0.5, 0.3, -0.1], [0.1, -0.3, 0.5, -0.7,
@@ -79,7 +79,7 @@
py_relu = torch.nn.ReLU(inplace=False)(py_tensor)
py_relu_cpu = py_relu.to("cpu")
- torch.testing.assert_allclose(np_relu, py_relu_cpu)
+ self.assertEqual(np_relu, py_relu_cpu)
def _testReluInPlace(self, np_features, device):
np_relu = self._npRelu(np_features)
@@ -89,9 +89,9 @@
py_relu = torch.nn.ReLU(inplace=True)(py_tensor)
py_relu_cpu = py_relu.to("cpu")
- torch.testing.assert_allclose(np_relu, py_relu_cpu)
+ self.assertEqual(np_relu, py_relu_cpu)
# Inplace Relu modifies the initial input and it should match the output of Relu
- torch.testing.assert_allclose(np_relu, py_tensor.to("cpu"))
+ self.assertEqual(np_relu, py_tensor.to("cpu"))
def testNumbersCPU(self):
for t in [np.int32]:
@@ -156,7 +156,7 @@
return np.maximum(np_features, negative_slope * np_features).astype(np_features.dtype)
def testNpLeakyRelu(self):
- torch.testing.assert_allclose(
+ torch.testing.assert_close(
np.array([[-0.09, 0.7, -0.05, 0.3, -0.01],
[0.1, -0.03, 0.5, -0.07, 0.9]]),
self._npLeakyRelu(
@@ -171,14 +171,14 @@
cpu_leaky_relu = relu_op(cpu_x)
mps_leaky_relu = relu_op(mps_x)
- torch.testing.assert_allclose(cpu_leaky_relu, mps_leaky_relu.to('cpu'))
+ torch.testing.assert_close(cpu_leaky_relu, mps_leaky_relu.to('cpu'))
# test backward pass
cpu_grad = torch.ones_like(cpu_leaky_relu)
mps_grad = cpu_grad.to('mps')
cpu_leaky_relu.backward(gradient=cpu_grad)
mps_leaky_relu.backward(gradient=mps_grad)
- torch.testing.assert_allclose(cpu_x.grad, mps_x.grad.to('cpu'))
+ torch.testing.assert_close(cpu_x.grad, mps_x.grad.to('cpu'))
def testNumbersCPU(self):
for t in [np.float32]:
@@ -257,14 +257,14 @@
cpu_leaky_relu = relu_op(cpu_x)
mps_leaky_relu = relu_op(mps_x)
- torch.testing.assert_allclose(cpu_leaky_relu, mps_leaky_relu.to('cpu'))
+ torch.testing.assert_close(cpu_leaky_relu, mps_leaky_relu.to('cpu'))
# test backward pass
cpu_grad = torch.ones_like(cpu_leaky_relu)
mps_grad = cpu_grad.to('mps')
cpu_leaky_relu.backward(gradient=cpu_grad)
mps_leaky_relu.backward(gradient=mps_grad)
- torch.testing.assert_allclose(cpu_x.grad, mps_x.grad.to('cpu'))
+ torch.testing.assert_close(cpu_x.grad, mps_x.grad.to('cpu'))
def testNumbersGPU(self):
for t in [np.float32]:
@@ -293,14 +293,14 @@
B = torch.ones(5, 6).to("mps")
C = torch.ones(6, 5).to("mps")
D = torch.mm(B, C).cpu()
- torch.testing.assert_allclose(D, torch.full((5, 5), 6.0))
+ torch.testing.assert_close(D, torch.full((5, 5), 6.0))
def test_addmm(self):
A = torch.ones(5, 5).to("mps")
B = torch.ones(5, 6).to("mps")
C = torch.ones(6, 5).to("mps")
D = torch.addmm(A, B, C).to("cpu")
- torch.testing.assert_allclose(D, torch.full((5, 5), 7.0))
+ torch.testing.assert_close(D, torch.full((5, 5), 7.0))
def test_bmm(self):
batch1_cpu = torch.randn(10, 3, 4)
@@ -355,7 +355,7 @@
def test_local_scalar_dense_mps(self):
x_cpu = torch.randn(1)
y_mps = x_cpu.to("mps")
- torch.testing.assert_allclose(x_cpu.item(), y_mps.item())
+ torch.testing.assert_close(x_cpu.item(), y_mps.item())
def test_linear_1d_weight(self):
device = 'cpu'
diff --git a/test/test_nn.py b/test/test_nn.py
index cab9db7..13036ef 100644
--- a/test/test_nn.py
+++ b/test/test_nn.py
@@ -4907,7 +4907,7 @@
n, k = k, n
Id = torch.eye(k, dtype=X.dtype, device=X.device).expand(*(X.size()[:-2]), k, k)
eps = 10 * n * torch.finfo(X.dtype).eps
- torch.testing.assert_allclose(X.mH @ X, Id, atol=eps, rtol=0.)
+ torch.testing.assert_close(X.mH @ X, Id, atol=eps, rtol=0.)
def assert_weight_allclose_Q(weight, W):
@@ -4920,7 +4920,7 @@
Q *= R.diagonal(dim1=-2, dim2=-1).sgn().unsqueeze(-2)
if wide_matrix:
Q = Q.mT
- torch.testing.assert_allclose(Q, weight, atol=1e-5, rtol=0.)
+ torch.testing.assert_close(Q, weight, atol=1e-5, rtol=0.)
for shape, dtype, use_linear in product(((4, 4), (5, 3), (3, 5)), # square/ tall / wide
@@ -4979,7 +4979,7 @@
w_new = w_new.mT
if can_initialize:
m.weight = w_new
- torch.testing.assert_allclose(w_new, m.weight, atol=1e-5, rtol=0.)
+ torch.testing.assert_close(w_new, m.weight, atol=1e-5, rtol=0.)
else:
msg = "assign to the matrix exponential or the Cayley parametrization"
with self.assertRaisesRegex(NotImplementedError, msg):
diff --git a/test/test_optim.py b/test/test_optim.py
index a55a74d..e611f75 100644
--- a/test/test_optim.py
+++ b/test/test_optim.py
@@ -1650,7 +1650,7 @@
new_scheduler = CosineAnnealingLR(
self.opt, T_max=T_max, eta_min=eta_min, last_epoch=0)
new_lrs = new_scheduler._last_lr
- torch.testing.assert_allclose(original_lrs, new_lrs, rtol=1e-4, atol=1e-5)
+ torch.testing.assert_close(original_lrs, new_lrs, rtol=1e-4, atol=1e-5)
def test_reduce_lr_on_plateau1(self):
epochs = 10
diff --git a/test/test_torch.py b/test/test_torch.py
index f84d8af..b507f68 100644
--- a/test/test_torch.py
+++ b/test/test_torch.py
@@ -37,7 +37,7 @@
skipCUDAMemoryLeakCheckIf, BytesIOContext,
skipIfRocm, skipIfNoSciPy, TemporaryFileName, TemporaryDirectoryName,
wrapDeterministicFlagAPITest, DeterministicGuard, CudaSyncGuard,
- skipIfNotRegistered, bytes_to_scalar, parametrize, skipIfMps)
+ skipIfNotRegistered, bytes_to_scalar, parametrize, skipIfMps, noncontiguous_like)
from multiprocessing.reduction import ForkingPickler
from torch.testing._internal.common_device_type import (
expectedFailureMeta,
@@ -2959,10 +2959,9 @@
dest = make_tensor(size, device=device, dtype=dtype, noncontiguous=dest_noncontig)
src_size = size[:dim] + (num_src,) + size[dim + 1:]
src = make_tensor(src_size, device=device, dtype=dtype, noncontiguous=src_noncontig)
- idx = torch.randint(num_dest, (num_src,), dtype=idx_dtype, device=device)
- if index_noncontig:
- # noncontiguous_like fails with RuntimeError: XLA tensors do not have storage
- idx = torch.testing.make_non_contiguous(idx)
+ idx = torch.testing.make_tensor(
+ num_src, low=0, high=num_dest, dtype=idx_dtype, device=device, noncontiguous=index_noncontig
+ )
expected = dest.clone()
dest.index_reduce_(dim, idx, src, reduce, include_self=include_self)
# fill rows in idx with reduction inits if include_self=False
@@ -5588,10 +5587,10 @@
dest = make_tensor(dest.shape, device=device, dtype=dest.dtype, noncontiguous=True)
src = torch.randn(num_copy, *other_sizes, device=device)
if not src_contig:
- src = torch.testing.make_non_contiguous(src)
+ src = noncontiguous_like(src)
idx = torch.randperm(num_dest, dtype=dtype, device=device).narrow(0, 0, num_copy)
if not index_contig:
- idx = torch.testing.make_non_contiguous(idx)
+ idx = noncontiguous_like(idx)
# index_add_ without alpha argument
dest2 = dest.clone()
dest.index_add_(0, idx, src)