Repository: pytorch
Issue: Misleading `log_abs_det_jacobian` shape for `StackTransform`?
Labels: module: distributions,triaged,bug

### 🐛 Describe the bug

```python
import torch
import torch.distributions.transforms as T

stack_transform = T.StackTransform(
    [
        T.AffineTransform(loc=0.0, scale=1.0),
        T.AffineTransform(loc=0.0, scale=1.0),
    ],
    dim=-1,
)

print(stack_transform.transforms[0].event_dim)  # 0, operates on scalars
print(stack_transform.event_dim)  # 1, operates on vectors

batch_size = 5
event_size = 2
y = torch.zeros(batch_size, event_size)
x = stack_transform.inv(y)  # shape (5, 2)

log_abs_det_jacobian = stack_transform.log_abs_det_jacobian(x, y)

# This is the results that feels misleading to me:
print(log_abs_det_jacobian.shape)  # (5, 2)
# But since `stack_transform`'s event dim is 1, shouldn't the 2 event dimensions
# be considered as independent, and the shape of `log_abs_det_jacobian` be (5,)?

# Afaik, this is not solvable using an `IndependentTransform`
independent_transform = T.IndependentTransform(
    stack_transform, reinterpreted_batch_ndims=1
)
ind_log_abs_det_jacobian = independent_transform.log_abs_det_jacobian(x, y)

# The `log_abs_det_jacobian` shape is, afaik, correct:
print(ind_log_abs_det_jacobian.shape)  # (5,)

# But:
print(
    independent_transform.event_dim
)  # 2, operates on a tensor, which is not what one would want?
```

### Versions

Collecting environment information...
/home/mind/lrouilla/dragostore/conda_drago5/envs/pyro/lib/python3.11/site-packages/torch/cuda/__init__.py:138: UserWarning: CUDA initialization: The NVIDIA driver on your system is too old (found version 9010). Please update your GPU driver by downloading and installing a new version from the URL: http://www.nvidia.com/Download/index.aspx Alternatively, go to: https://pytorch.org to install a PyTorch version that has been compiled with your version of the CUDA driver. (Triggered internally at ../c10/cuda/CUDAFunctions.cpp:108.)
  return torch._C._cuda_getDeviceCount() > 0
PyTorch version: 2.1.0+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.6 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: Could not collect
CMake version: version 3.10.2
Libc version: glibc-2.27

Python version: 3.11.4 (main, Jul  5 2023, 13:45:01) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-4.15.0-213-generic-x86_64-with-glibc2.27
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture :                          x86_64
Mode(s) opératoire(s) des processeurs : 32-bit, 64-bit
Boutisme :                              Little Endian
Processeur(s) :                         72
Liste de processeur(s) en ligne :       0-71
Thread(s) par cœur :                    2
Cœur(s) par socket :                    18
Socket(s) :                             2
Nœud(s) NUMA :                          2
Identifiant constructeur :              GenuineIntel
Famille de processeur :                 6
Modèle :                                85
Nom de modèle :                         Intel(R) Xeon(R) Gold 6140 CPU @ 2.30GHz
Révision :                              4
Vitesse du processeur en MHz :          3000.000
BogoMIPS :                              4600.00
Virtualisation :                        VT-x
Cache L1d :                             32K
Cache L1i :                             32K
Cache L2 :                              1024K
Cache L3 :                              25344K
Nœud NUMA 0 de processeur(s) :          0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70
Nœud NUMA 1 de processeur(s) :          1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71
Drapaux :                               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 pti intel_ppin ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm 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 md_clear flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] flake8==6.1.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.2
[pip3] torch==2.1.0
[pip3] torchaudio==2.0.2
[pip3] torchvision==0.15.2
[pip3] triton==2.1.0
[conda] blas                      1.0                         mkl  
[conda] cudatoolkit               11.8.0              h4ba93d1_12    conda-forge
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] mkl                       2023.1.0         h6d00ec8_46342  
[conda] mkl-service               2.4.0           py311h5eee18b_1  
[conda] mkl_fft                   1.3.6           py311ha02d727_1  
[conda] mkl_random                1.2.2           py311ha02d727_1  
[conda] numpy                     1.26.2                   pypi_0    pypi
[conda] pytorch-cuda              11.8                 h7e8668a_5    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torch                     2.1.0                    pypi_0    pypi
[conda] torchaudio                2.0.2               py311_cu118    pytorch
[conda] torchvision               0.15.2              py311_cu118    pytorch
[conda] triton                    2.1.0                    pypi_0    pypi


cc @fritzo @neerajprad @alicanb @nikitaved