pytorch/test
Vaidotas Simkus e154b36685 Standardized clamp kernels to Numpy-like implementation (#43288)
Summary:
**BC-breaking note**

For ease of exposition let a_min be the value of the "min" argument to clamp, and a_max be the value of the "max" argument to clamp.

This PR changes the behavior of torch.clamp to always compute min(max(a, a_min), a_max). torch.clamp currently computes this in its vectorized CPU specializations:

78b95b6204/aten/src/ATen/cpu/vec256/vec256_double.h (L304)

but in other places it clamps differently:

78b95b6204/aten/src/ATen/cpu/vec256/vec256_base.h (L624)

78b95b6204/aten/src/ATen/native/cuda/UnaryOpsKernel.cu (L160)

These implementations are the same when a_min < a_max, but divergent when a_min > a_max. This divergence is easily triggered:

```
t = torch.arange(200).to(torch.float)
torch.clamp(t, 4, 2)[0]
: tensor(2.)

torch.clamp(t.cuda(), 4, 2)[0]
: tensor(4., device='cuda:0')

torch.clamp(torch.tensor(0), 4, 2)
: tensor(4)
```

This PR makes the behavior consistent with NumPy's clip. C++'s std::clamp's behavior is undefined when a_min > a_max, but Clang's std::clamp will return 10 in this case (although the program, per the above comment, is in error). Python has no standard clamp implementation.

**PR Summary**

Fixes discrepancy between AVX, CUDA, and base vector implementation for clamp, such that all implementations are consistent and use min(max_vec, max(min_vec, x) formula, thus making it equivalent to numpy.clip in all implementations.

The same fix as in https://github.com/pytorch/pytorch/issues/32587 but isolated to the kernel change only, so that the internal team can benchmark.

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

Reviewed By: colesbury

Differential Revision: D24079453

Pulled By: mruberry

fbshipit-source-id: 67f30d2f2c86bbd3e87080b32f00e8fb131a53f7
2020-10-06 13:42:08 -07:00
..
backward_compatibility remove beta defaulting in smooth_l1_loss_backward. added to the bc whitelist (#45588) 2020-10-02 07:53:04 -07:00
bottleneck_test
cpp Remove lock from GraphTask::set_exception_without_signal. (#45867) 2020-10-05 20:02:29 -07:00
cpp_api_parity Support tuple inputs in NN Module test (#44853) 2020-09-28 22:05:05 -07:00
cpp_extensions Add barrier() at the end of init_process_group and new_group. (#45181) 2020-09-25 15:46:59 -07:00
custom_backend [JIT] Modify to_backend API so that it accepts wrapped modules (#43612) 2020-09-28 17:17:01 -07:00
custom_operator
distributed Rocm skip test cases (#45782) 2020-10-05 15:12:25 -07:00
error_messages
expect
fx [FX] Make output a non-special Node (#45599) 2020-10-02 17:08:17 -07:00
jit [jit] allow slicing multiple dimensions with indicies (#45239) 2020-10-05 15:03:54 -07:00
mobile
namespace_b torch.package - a way to package models and code (#45015) 2020-09-22 21:21:21 -07:00
onnx Revert D23398534: [pytorch][PR] [ONNX] Improve error handling for adaptive_pool 2020-10-05 15:16:59 -07:00
optim
package_a torch.package - a way to package models and code (#45015) 2020-09-22 21:21:21 -07:00
quantization [quant][graphmode][fx][eagermode] Add leaky relu support in quantization workflows (#45712) 2020-10-06 12:16:04 -07:00
scripts
test_img
type_hint_tests
HowToWriteTestsUsingFileCheck.md
module_a.py torch.package - a way to package models and code (#45015) 2020-09-22 21:21:21 -07:00
print_test_stats.py add circle ci job name dimension to report test stats (#45457) 2020-09-28 20:51:58 -07:00
run_test.py reorganizing tests so that test1 and test2 are balanced in timing (#45778) 2020-10-06 07:57:08 -07:00
simulate_nccl_errors.py
test_autograd.py Avoid NaN values in torch.cdist backward for p<1 (#45720) 2020-10-05 16:19:29 -07:00
test_bundled_images.py
test_bundled_inputs.py
test_complex.py
test_cpp_api_parity.py Always use NewModuleTest instead of ModuleTest. (#44745) 2020-09-17 07:36:39 -07:00
test_cpp_extensions_aot.py
test_cpp_extensions_jit.py
test_cuda.py Enable NamedTuple data type to work with DDP (#44220) 2020-10-02 13:33:08 -07:00
test_cuda_primary_ctx.py
test_dataloader.py Add ShuffleDataset with buffer (#45290) 2020-09-30 07:58:15 -07:00
test_determination.py
test_dispatch.py Align casing in test_dispatch with dispatch keys. (#44933) 2020-09-22 10:50:08 -07:00
test_distributions.py
test_expecttest.py
test_foreach.py Use smaller N to speed up TestForeach (#45785) 2020-10-06 13:29:04 -07:00
test_function_schema.py Stricter backward compatibility check (#45773) 2020-10-06 01:28:48 -07:00
test_functional_autograd_benchmark.py
test_futures.py
test_fx.py [FX][WIP] Mutable Graph APIs (#45227) 2020-10-05 17:07:08 -07:00
test_indexing.py
test_jit.py Change type inferred from empty annotation (#45360) 2020-10-05 15:16:56 -07:00
test_jit_cuda_fuser.py remove skip annotations since we already disabled the tests wholesale (#45698) 2020-10-01 17:47:48 -07:00
test_jit_cuda_fuser_legacy.py Rename jobs and cli switches for testing GraphExecutor configurations to something a little bit more sensical. (#45715) 2020-10-05 11:43:28 -07:00
test_jit_cuda_fuser_profiling.py Rename jobs and cli switches for testing GraphExecutor configurations to something a little bit more sensical. (#45715) 2020-10-05 11:43:28 -07:00
test_jit_disabled.py
test_jit_fuser.py Adjust TF32 tests (#44240) 2020-09-24 10:25:58 -07:00
test_jit_fuser_legacy.py Rename jobs and cli switches for testing GraphExecutor configurations to something a little bit more sensical. (#45715) 2020-10-05 11:43:28 -07:00
test_jit_fuser_te.py Build shape expressions and remove outputs that are only used by aten::sizes (#45080) 2020-09-28 10:45:56 -07:00
test_jit_legacy.py Rename jobs and cli switches for testing GraphExecutor configurations to something a little bit more sensical. (#45715) 2020-10-05 11:43:28 -07:00
test_jit_profiling.py Rename jobs and cli switches for testing GraphExecutor configurations to something a little bit more sensical. (#45715) 2020-10-05 11:43:28 -07:00
test_jit_py3.py [JIT] Enable @unused syntax for ignoring properties (#45261) 2020-09-29 10:24:25 -07:00
test_jit_simple.py Rename jobs and cli switches for testing GraphExecutor configurations to something a little bit more sensical. (#45715) 2020-10-05 11:43:28 -07:00
test_jit_string.py
test_linalg.py Test torch.svd using complex float and double numbers (take 2) (#45795) 2020-10-03 11:33:28 -07:00
test_logging.py
test_mkldnn.py
test_mobile_optimizer.py [pytorch] Replace "blacklist" in test/test_mobile_optimizer.py (#45512) 2020-09-30 10:43:59 -07:00
test_multiprocessing.py
test_multiprocessing_spawn.py
test_namedtensor.py
test_namedtuple_return_api.py
test_native_functions.py Revert D23753711: [pytorch][PR] Add foreach APIs for binary ops with ScalarList 2020-09-24 11:55:49 -07:00
test_nn.py ReplicationPad accepts 0-dim batch size. (#39137) 2020-10-06 11:54:32 -07:00
test_numba_integration.py
test_op_aliases.py Migrate addr from the TH to Aten (CPU) (#44364) 2020-09-25 01:18:09 -07:00
test_openmp.py
test_ops.py Revert D24027761: Update backward definition for more operators and reenable tests in test_ops.py 2020-10-02 18:52:57 -07:00
test_optim.py Update test_multi_tensor_optimizers test (#45510) 2020-09-30 08:59:18 -07:00
test_overrides.py Complex gradcheck logic (#43208) 2020-09-20 22:05:04 -07:00
test_package.py [package] Add dependency viz (#45214) 2020-09-28 15:38:41 -07:00
test_profiler.py Source code level attribution in profiler (#43898) 2020-09-30 00:57:35 -07:00
test_pruning_op.py
test_quantization.py [quant][graphmode][fx][eagermode] Add leaky relu support in quantization workflows (#45712) 2020-10-06 12:16:04 -07:00
test_serialization.py
test_show_pickle.py
test_sparse.py Sparse softmax support (CUDA) (#42307) 2020-09-24 00:07:30 -07:00
test_spectral_ops.py torch.fft helper functions (#44877) 2020-10-05 22:04:52 -07:00
test_static_runtime.py [static runtime] Split out graph preparation from runtime (#44131) 2020-09-28 13:01:23 -07:00
test_tensor_creation_ops.py Rocm skip test cases (#45782) 2020-10-05 15:12:25 -07:00
test_tensorboard.py
test_tensorexpr.py Reduce size of test_unsqueeze to resolve consistent timeout issue (#45877) 2020-10-06 10:33:20 -07:00
test_throughput_benchmark.py
test_torch.py Standardized clamp kernels to Numpy-like implementation (#43288) 2020-10-06 13:42:08 -07:00
test_type_hints.py
test_type_info.py
test_type_promotion.py [fix] type promotion atan2 (#43466) 2020-09-23 22:23:05 -07:00
test_unary_ufuncs.py [numpy] Add torch.nan_to_num (#44592) 2020-10-05 01:38:56 -07:00
test_utils.py Re-land: Add callgrind collection to Timer #44717 (#45586) 2020-09-30 17:43:06 -07:00
test_vmap.py
test_vulkan.py
test_xnnpack_integration.py Disabling XNNPACK integration test in tsan mode (#45850) 2020-10-06 10:49:58 -07:00