Repository: pytorch
Issue: inconsistency in ```torch.nn.functional.log_softmax``` on CPU and GPU
Labels: module: numerical-stability,triaged,module: bfloat16

### 🐛 Describe the bug

Testing consistency of ```torch::nn::functional::log_softmax``` between CPU and GPU using bfloat16 data.
```python #
#include <iostream>
#include <torch/torch.h>

int main() {

   torch::Tensor input = torch::tensor(
    {
        {
            {
                {{-1.6641, -0.9219}, {2.5156, -0.4648}},
                {{1.0469, 1.4531}, {0.0908, 0.1514}}
            },
            {
                {{-1.2422, 1.2656}, {-1.0859, -0.0801}},
                {{2.3750, -0.4238}, {-0.9023, -0.4570}}
            }
        },
        {
            {
                {{-1.3125, 0.1099}, {-0.2129, -0.6641}},
                {{-0.3867, -0.2617}, {1.1641, 0.4043}}
            },
            {
                {{-0.5352, -0.4785}, {0.2061, 0.2734}},
                {{-1.3672, 0.0544}, {-1.3203, -1.5469}}
            }
        }
    },
    torch::kBFloat16);

    auto options = 
      torch::nn::functional::LogSoftmaxFuncOptions(-1)
        .dtype(c10::nullopt);

    auto result_cpu = torch::nn::functional::log_softmax(input, options);

    torch::Tensor input_cuda = input.cuda();

    auto result_gpu = torch::nn::functional::log_softmax(input_cuda, options);

    std::cout << "initialized tensor (CPU):\n" << input << std::endl;

    std::cout << "CPU result: \n" << result_cpu << std::endl;
    std::cout << "GPU result: \n" << result_gpu << std::endl;
    
    bool inconsistent = !torch::allclose(result_cpu, result_gpu.cpu(), 1e-03, 1e-02);
    std::cout << "inconsistency with atol=1e-02 and rtol=1e-03: " << std::boolalpha << inconsistent << std::endl;

    return 0;
}
```
outputs (e.g take a look at matrices (1,1,2), (1,2,2), and (2,2,1)—they show notable differences in the log_softmax output.):
```
initialized tensor (CPU):
(1,1,1,.,.) = 
 -1.6641 -0.9219
  2.5156 -0.4648

(2,1,1,.,.) = 
 -1.3125  0.1099
 -0.2129 -0.6641

(1,2,1,.,.) = 
 -1.2422  1.2656
 -1.0859 -0.0801

(2,2,1,.,.) = 
 -0.5352 -0.4785
  0.2061  0.2734

(1,1,2,.,.) = 
  1.0469  1.4531
  0.0908  0.1514

(2,1,2,.,.) = 
 -0.3867 -0.2617
  1.1641  0.4043

(1,2,2,.,.) = 
  2.3750 -0.4238
 -0.9023 -0.4570

(2,2,2,.,.) = 
 -1.3672  0.0544
 -1.3203 -1.5469
[ CPUBFloat16Type{2,2,2,2,2} ]

CPU result: 
(1,1,1,.,.) = 
 -1.1328 -0.3906
 -0.0459 -3.0312

(2,1,1,.,.) = 
 -1.6406 -0.2168
 -0.4941 -0.9453

(1,2,1,.,.) = 
 -2.5781 -0.0752
 -1.3203 -0.3125

(2,2,1,.,.) = 
 -0.7188 -0.6641
 -0.7266 -0.6602

(1,1,2,.,.) = 
 -0.9141 -0.5078
 -0.7188 -0.6602

(2,1,2,.,.) = 
 -0.7578 -0.6328
 -0.3848 -1.1406

(1,2,2,.,.) = 
 0.01 *
 -6.0547 -285.9375
  -93.7500 -49.4141

(2,2,2,.,.) = 
 -1.6406 -0.2168
 -0.5859 -0.8125
[ CPUBFloat16Type{2,2,2,2,2} ]

GPU result: 
(1,1,1,.,.) = 
 -1.1328 -0.3887
 -0.0496 -3.0312

(2,1,1,.,.) = 
 -1.6406 -0.2158
 -0.4922 -0.9453

(1,2,1,.,.) = 
 -2.5938 -0.0781
 -1.3203 -0.3125

(2,2,1,.,.) = 
 -0.7227 -0.6641
 -0.7266 -0.6602

(1,1,2,.,.) = 
 -0.9180 -0.5117
 -0.7227 -0.6641

(2,1,2,.,.) = 
 -0.7578 -0.6328
 -0.3828 -1.1406

(1,2,2,.,.) = 
 0.01 *
 -5.9082 -285.9375
  -94.1406 -49.4141

(2,2,2,.,.) = 
 -1.6406 -0.2158
 -0.5859 -0.8125
[ CUDABFloat16Type{2,2,2,2,2} ]

inconsistency with atol=1e-02 and rtol=1e-03: true
```

### Versions

OS: Ubuntu 22.04.4 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: 16.0.4 (https://github.com/llvm/llvm-project ae42196bc493ffe877a7e3dff8be32035dea4d07)
CMake version: version 3.22.1
Libc version: glibc-2.35

Python version: 3.8.5 (default, Sep 4 2020, 07:30:14) [GCC 7.3.0] (64-bit runtime)
Python platform: Linux-5.15.0-105-generic-x86_64-with-glibc2.10
Is CUDA available: N/A
CUDA runtime version: 12.1.66
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration:
GPU 0: NVIDIA GeForce RTX 3090
GPU 1: NVIDIA GeForce RTX 3090
GPU 2: NVIDIA GeForce RTX 3090
GPU 3: NVIDIA GeForce RTX 3090

Nvidia driver version: 550.78
cuDNN version: Probably one of the following:
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn.so.8.9.2
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.9.2
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.9.2
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.9.2
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.9.2
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.9.2
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.9.2
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: N/A

CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 46 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 96
On-line CPU(s) list: 0-95
Vendor ID: GenuineIntel
Model name: Intel(R) Xeon(R) Gold 6248R CPU @ 3.00GHz
CPU family: 6
Model: 85
Thread(s) per core: 2
Core(s) per socket: 24
Socket(s): 2
Stepping: 7
CPU max MHz: 4000.0000
CPU min MHz: 1200.0000
BogoMIPS: 6000.00
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku ospke avx512_vnni md_clear flush_l1d arch_capabilities
Virtualization: VT-x
L1d cache: 1.5 MiB (48 instances)
L1i cache: 1.5 MiB (48 instances)
L2 cache: 48 MiB (48 instances)
L3 cache: 71.5 MiB (2 instances)
NUMA node(s): 2
NUMA node0 CPU(s): 0-23,48-71
NUMA node1 CPU(s): 24-47,72-95
Vulnerability Gather data sampling: Mitigation; Microcode
Vulnerability Itlb multihit: KVM: Mitigation: VMX disabled
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed: Mitigation; Enhanced IBRS
Vulnerability Spec rstack overflow: 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; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Mitigation; TSX disabled

Versions of relevant libraries:
[pip3] flake8==3.8.4
[pip3] numpy==1.19.2
[pip3] numpydoc==1.1.0
[pip3] torch==2.2.0a0+git9fa3350
[conda] blas 1.0 mkl
[conda] mkl 2020.2 256
[conda] mkl-service 2.3.0 py38he904b0f_0
[conda] mkl_fft 1.2.0 py38h23d657b_0
[conda] mkl_random 1.1.1 py38h0573a6f_0
[conda] numpy 1.19.2 py38h54aff64_0
[conda] numpy-base 1.19.2 py38hfa32c7d_0
[conda] numpydoc 1.1.0 pyhd3eb1b0_1
[conda] torch 2.2.0a0+git9fa3350 dev_0