Repository: pytorch
Issue: Vec256<int64_t> does not handle LONG_MAX on minimum
Labels: module: cpu,triaged,module: NaNs and Infs,module: vectorization,module: reductions

## 🐛 Bug

I found this bug while working on https://github.com/pytorch/pytorch/pull/43092

When I try to implement `torch.amin`, I found that if I use `upper_bound<scalar_t>()` as the initial value of the reduction, I will end up getting a wrong negative result `-9223372036854775808` for `torch.long` tensor.

More experiments shows that
- `torch.int` tensor does not have such issue
- replacing the initial value with `999999`, or `upper_bound<scalar_t>() - 1000` could resolve this issue
- replacing the initial value with `upper_bound<scalar_t>() - 100` does not resolve this issue

## To Reproduce

Write a new reduction op

```C++
// aten/src/ATen/native/ReduceOps.cpp
Tensor &amin_out(Tensor& result, const Tensor& self, IntArrayRef dim, bool keepdim) {
  TORCH_CHECK(self.scalar_type() == result.scalar_type(), "Illegal dtype for self, and out:", self.scalar_type(), result.scalar_type());
  ScalarType dtype = get_dtype(result, self, {}, true);
  auto iter = make_reduction("min_values", result, self, dim, keepdim, dtype);
  TORCH_CHECK(iter.numel() > 0, "min_values on a tensor with no elements is not defined.");
  min_values_stub(iter.device_type(), iter);
  return result;
}
```

with cpu implementation
```C++
// aten/src/ATen/native/cpu/ReduceOpsKernel.cpp
static void min_values_kernel_impl(TensorIterator& iter) {
  AT_DISPATCH_ALL_TYPES_AND2(kHalf, kBool, iter.dtype(), "min_values_cpu", [&iter] {
    binary_kernel_reduce_vec(
      iter,
      [](scalar_t a, scalar_t b) -> scalar_t { return min_impl(a, b); },
      [](Vec256<scalar_t> a, Vec256<scalar_t> b) { return minimum(a, b); },
      upper_bound<scalar_t>());
  });
}

// .....

REGISTER_DISPATCH(min_values_stub, &min_values_kernel_impl);
```

and invoke this operator

```python
>>> torch.randint(1000, (5,5)).amin()
tensor(-9223372036854775808)
```

## Expected behavior

The reduction should return some positive number

## Environment

The `collect_env.py` does not show much useful information. I believe useful information should be about cpu:

```
gaoxiang@sunnyvale ~/misc master $ lscpu                                                                                                                                                                                                                                        
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   43 bits physical, 48 bits virtual
CPU(s):                          64
On-line CPU(s) list:             0-63
Thread(s) per core:              2
Core(s) per socket:              32
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       AuthenticAMD
CPU family:                      23
Model:                           49
Model name:                      AMD Ryzen Threadripper 3970X 32-Core Processor
Stepping:                        0
Frequency boost:                 enabled
CPU MHz:                         2807.857
CPU max MHz:                     3700.0000
CPU min MHz:                     2200.0000
BogoMIPS:                        7447.13
Virtualization:                  AMD-V
L1d cache:                       1 MiB
L1i cache:                       1 MiB
L2 cache:                        16 MiB
L3 cache:                        128 MiB
NUMA node0 CPU(s):               0-63
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Full AMD retpoline, IBPB conditional, STIBP conditional, RSB filling
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf pni pclmulqdq monitor sss
                                 e3 fma cx16 sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_
                                 l3 hw_pstate sme ssbd mba sev ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr rdpru 
                                 wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif umip rdpid overflow_recov succor smca

```

and cmake:

```
-- 
-- ******** Summary ********
--   CMake version         : 3.18.1
--   CMake command         : /usr/bin/cmake
--   System                : Linux
--   C++ compiler          : /usr/bin/g++-9
--   C++ compiler version  : 9.3.0
--   CXX flags             :  -Wno-deprecated -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -fopenmp -Wnon-virtual-dtor
--   Build type            : RelWithDebInfo
--   Compile definitions   : TH_BLAS_MKL;ONNX_ML=1;ONNXIFI_ENABLE_EXT=1
--   CMAKE_PREFIX_PATH     : /usr/lib/python3.8/site-packages;/opt/cuda
--   CMAKE_INSTALL_PREFIX  : /home/gaoxiang/pytorch-amax/torch
--   CMAKE_MODULE_PATH     : /home/gaoxiang/pytorch-amax/cmake/Modules;/home/gaoxiang/pytorch-amax/cmake/public/../Modules_CUDA_fix
-- 
--   ONNX version          : 1.7.0
--   ONNX NAMESPACE        : onnx_torch
--   ONNX_BUILD_TESTS      : OFF
--   ONNX_BUILD_BENCHMARKS : OFF
--   ONNX_USE_LITE_PROTO   : OFF
--   ONNXIFI_DUMMY_BACKEND : OFF
--   ONNXIFI_ENABLE_EXT    : OFF
-- 
--   Protobuf compiler     : 
--   Protobuf includes     : 
--   Protobuf libraries    : 
--   BUILD_ONNX_PYTHON     : OFF
-- 
-- ******** Summary ********
--   CMake version         : 3.18.1
--   CMake command         : /usr/bin/cmake
--   System                : Linux
--   C++ compiler          : /usr/bin/g++-9
--   C++ compiler version  : 9.3.0
--   CXX flags             :  -Wno-deprecated -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -fopenmp -Wnon-virtual-dtor
--   Build type            : RelWithDebInfo
--   Compile definitions   : TH_BLAS_MKL;ONNX_ML=1;ONNXIFI_ENABLE_EXT=1
--   CMAKE_PREFIX_PATH     : /usr/lib/python3.8/site-packages;/opt/cuda
--   CMAKE_INSTALL_PREFIX  : /home/gaoxiang/pytorch-amax/torch
--   CMAKE_MODULE_PATH     : /home/gaoxiang/pytorch-amax/cmake/Modules;/home/gaoxiang/pytorch-amax/cmake/public/../Modules_CUDA_fix
-- 
--   ONNX version          : 1.4.1
--   ONNX NAMESPACE        : onnx_torch
--   ONNX_BUILD_TESTS      : OFF
--   ONNX_BUILD_BENCHMARKS : OFF
--   ONNX_USE_LITE_PROTO   : OFF
--   ONNXIFI_DUMMY_BACKEND : OFF
-- 
--   Protobuf compiler     : 
--   Protobuf includes     : 
--   Protobuf libraries    : 
--   BUILD_ONNX_PYTHON     : OFF
```

## Additional context

cc: @ngimel @mruberry 


cc @VitalyFedyunin @heitorschueroff @ezyang @gchanan @zou3519 @bdhirsh