Repository: pytorch
Issue: [histogram] expand: attempting to expand a dimension of length 6!
Labels: triaged,module: functionalization,oncall: pt2,module: pt2-dispatcher

### 🐛 Describe the bug

runtime error seen for out variant of histogram

### Error logs

```
Traceback (most recent call last):
  File "/tmp/hist.py", line 23, in <module>
    histogram("cpu")
  File "/tmp/hist.py", line 17, in histogram
    output = fn(x, w, bins, hist, bin_edges)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 489, in _fn
    return fn(*args, **kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 655, in catch_errors
    return callback(frame, cache_entry, hooks, frame_state)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 727, in _convert_frame
    result = inner_convert(frame, cache_entry, hooks, frame_state)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 383, in _convert_frame_assert
    compiled_product = _compile(
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 646, in _compile
    guarded_code = compile_inner(code, one_graph, hooks, transform)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/utils.py", line 244, in time_wrapper
    r = func(*args, **kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 562, in compile_inner
    out_code = transform_code_object(code, transform)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/bytecode_transformation.py", line 1033, in transform_code_object
    transformations(instructions, code_options)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 151, in _fn
    return fn(*args, **kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py", line 527, in transform
    tracer.run()
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 2128, in run
    super().run()
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 818, in run
    and self.step()
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 781, in step
    getattr(self, inst.opname)(inst)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py", line 2243, in RETURN_VALUE
    self.output.compile_subgraph(
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/output_graph.py", line 945, in compile_subgraph
    self.compile_and_call_fx_graph(tx, pass2.graph_output_vars(), root)
  File "/usr/lib/python3.10/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/output_graph.py", line 1087, in compile_and_call_fx_graph
    compiled_fn = self.call_user_compiler(gm)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/utils.py", line 244, in time_wrapper
    r = func(*args, **kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/output_graph.py", line 1159, in call_user_compiler
    raise BackendCompilerFailed(self.compiler_fn, e).with_traceback(
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/output_graph.py", line 1140, in call_user_compiler
    compiled_fn = compiler_fn(gm, self.example_inputs())
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/repro/after_dynamo.py", line 117, in debug_wrapper
    compiled_gm = compiler_fn(gm, example_inputs)
  File "/tmp/lib/python3.10/site-packages/torch/__init__.py", line 1662, in __call__
    return compile_fx(model_, inputs_, config_patches=self.config)
  File "/tmp/lib/python3.10/site-packages/torch/_inductor/compile_fx.py", line 1168, in compile_fx
    return aot_autograd(
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/backends/common.py", line 55, in compiler_fn
    cg = aot_module_simplified(gm, example_inputs, **kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 887, in aot_module_simplified
    compiled_fn = create_aot_dispatcher_function(
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/utils.py", line 244, in time_wrapper
    r = func(*args, **kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 600, in create_aot_dispatcher_function
    compiled_fn = compiler_fn(flat_fn, fake_flat_args, aot_config, fw_metadata=fw_metadata)
  File "/tmp/lib/python3.10/site-packages/torch/_functorch/_aot_autograd/runtime_wrappers.py", line 425, in aot_wrapper_dedupe
    return compiler_fn(flat_fn, leaf_flat_args, aot_config, fw_metadata=fw_metadata)
  File "/tmp/lib/python3.10/site-packages/torch/_functorch/_aot_autograd/runtime_wrappers.py", line 630, in aot_wrapper_synthetic_base
    return compiler_fn(flat_fn, flat_args, aot_config, fw_metadata=fw_metadata)
  File "/tmp/lib/python3.10/site-packages/torch/_functorch/_aot_autograd/jit_compile_runtime_wrappers.py", line 72, in aot_dispatch_base
    fw_module, updated_flat_args, maybe_subclass_meta = aot_dispatch_base_graph(  # type: ignore[misc]
  File "/tmp/lib/python3.10/site-packages/torch/_functorch/_aot_autograd/dispatch_and_compile_graph.py", line 77, in aot_dispatch_base_graph
    fw_module = _create_graph(
  File "/tmp/lib/python3.10/site-packages/torch/_functorch/_aot_autograd/dispatch_and_compile_graph.py", line 32, in _create_graph
    fx_g = make_fx(f, decomposition_table=aot_config.decompositions)(*args)
  File "/tmp/lib/python3.10/site-packages/torch/fx/experimental/proxy_tensor.py", line 871, in wrapped
    t = dispatch_trace(wrap_key(func, args, fx_tracer, pre_dispatch), tracer=fx_tracer, concrete_args=tuple(phs))
  File "/tmp/lib/python3.10/site-packages/torch/_compile.py", line 24, in inner
    return torch._dynamo.disable(fn, recursive)(*args, **kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 489, in _fn
    return fn(*args, **kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/external_utils.py", line 17, in inner
    return fn(*args, **kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/fx/experimental/proxy_tensor.py", line 483, in dispatch_trace
    graph = tracer.trace(root, concrete_args)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py", line 489, in _fn
    return fn(*args, **kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/_dynamo/external_utils.py", line 17, in inner
    return fn(*args, **kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/fx/_symbolic_trace.py", line 821, in trace
    (self.create_arg(fn(*args)),),
  File "/tmp/lib/python3.10/site-packages/torch/fx/experimental/proxy_tensor.py", line 519, in wrapped
    out = f(*tensors)
  File "<string>", line 1, in <lambda>
  File "/tmp/lib/python3.10/site-packages/torch/_functorch/_aot_autograd/traced_function_transforms.py", line 466, in fwd_helper
    return _functionalized_f_helper(*args)
  File "/tmp/lib/python3.10/site-packages/torch/_functorch/_aot_autograd/traced_function_transforms.py", line 456, in _functionalized_f_helper
    inpt_old.copy_(inpt_new)
  File "/tmp/lib/python3.10/site-packages/torch/utils/_stats.py", line 20, in wrapper
    return fn(*args, **kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/fx/experimental/proxy_tensor.py", line 596, in __torch_dispatch__
    return self.inner_torch_dispatch(func, types, args, kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/fx/experimental/proxy_tensor.py", line 631, in inner_torch_dispatch
    return proxy_call(self, func, self.pre_dispatch, args, kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/fx/experimental/proxy_tensor.py", line 376, in proxy_call
    out = func(*args, **kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/_ops.py", line 513, in __call__
    return self._op(*args, **(kwargs or {}))
  File "/tmp/lib/python3.10/site-packages/torch/utils/_stats.py", line 20, in wrapper
    return fn(*args, **kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/_subclasses/fake_tensor.py", line 1392, in __torch_dispatch__
    return self.dispatch(func, types, args, kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/_subclasses/fake_tensor.py", line 1649, in dispatch
    op_impl_out = op_impl(self, func, *args, **kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/_subclasses/fake_tensor.py", line 697, in multi_device_op_default
    return run_and_return_new_tensor_of_input_device(fake_mode, func, args, kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/_subclasses/fake_tensor.py", line 654, in run_and_return_new_tensor_of_input_device
    out = func(*args, **kwargs)
  File "/tmp/lib/python3.10/site-packages/torch/_ops.py", line 513, in __call__
    return self._op(*args, **(kwargs or {}))
  File "/tmp/lib/python3.10/site-packages/torch/_meta_registrations.py", line 376, in meta_copy_
    aten.expand_copy.default(intermediate, self.size())
  File "/tmp/lib/python3.10/site-packages/torch/_ops.py", line 513, in __call__
    return self._op(*args, **(kwargs or {}))
  File "/tmp/lib/python3.10/site-packages/torch/_refs/__init__.py", line 2870, in expand
    torch._check(
  File "/tmp/lib/python3.10/site-packages/torch/__init__.py", line 1081, in _check
    _check_with(RuntimeError, cond, message)
  File "/tmp/lib/python3.10/site-packages/torch/__init__.py", line 1064, in _check_with
    raise error_type(message_evaluated)
torch._dynamo.exc.BackendCompilerFailed: backend='inductor' raised:
RuntimeError: expand: attempting to expand a dimension of length 6!

Set TORCH_LOGS="+dynamo" and TORCHDYNAMO_VERBOSE=1 for more information


You can suppress this exception and fall back to eager by setting:
    import torch._dynamo
    torch._dynamo.config.suppress_errors = True

```

### Minified repro

```
import torch


def histogram(device):
    x = torch.rand([1, 2, 3, 4, 1]).to(device)
    w = torch.rand([1, 2, 3, 4, 1]).to(device)
    bins = 6
    hist = torch.empty(0).to(device)
    bin_edges = torch.empty(0).to(device)

    def fn(x, w, bins, hist, bin_edges):
        return torch.histogram(
            x, bins, weight=w, range=(0.0, 7.0), density=False, out=(hist, bin_edges)
        )

    fn = torch.compile(fn, backend="inductor")
    output = fn(x, w, bins, hist, bin_edges)

    print("hist: ", hist.cpu())
    print("bin_edges: ", bin_edges.cpu())


histogram("cpu")

```

### Versions

[pip3] numpy==1.26.4
[pip3] torch==2.2.1
[pip3] torchaudio==2.2.0
[pip3] torchdata==0.7.1
[pip3] torchmetrics==1.2.1
[pip3] torchtext==0.17.0
[pip3] torchvision==0.17.0

cc @ezyang @msaroufim @bdhirsh @anijain2305 @zou3519 @chauhang