Repository: pytorch
Issue: test_generate_tensor_from_list_of_numpy_primitive_type fails if run under pytest
Labels: triaged,module: dynamic shapes

### 🐛 Describe the bug

Sample failure:

```
__________________ StaticDefaultDynamicShapesFunctionTests.test_return_numpy_ndarray_dynamic_shapes_static_default ___________________
Traceback (most recent call last):
  File "/data/users/ezyang/d/pytorch/test/dynamo/test_functions.py", line 42, in test_fn
    return torch._dynamo.testing.standard_test(self, fn=fn, nargs=nargs)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/testing.py", line 225, in standard_test
    self.assertTrue(same(val1a, correct1))
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/utils.py", line 944, in same
    assert isinstance(res, torch.Tensor), f"type mismatch {type(ref)} {type(res)}"
AssertionError: type mismatch <class 'torch.Tensor'> <class 'numpy.ndarray'>
-------------------------------------------------------- Captured stdout call --------------------------------------------------------
stats [('calls_captured', 2), ('unique_graphs', 1)]
___________________________ DynamicShapesMiscTests.test_generate_tensor_from_list_of_numpy_primitive_type ____________________________
Traceback (most recent call last):
  File "/data/users/ezyang/d/pytorch/test/dynamo/test_misc.py", line 3548, in test_generate_tensor_from_list_of_numpy_primitive_type
    res = opt_fn()
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/eval_frame.py", line 295, in _fn
    return fn(*args, **kwargs)
  File "/data/users/ezyang/d/pytorch/test/dynamo/test_misc.py", line 3541, in fn
    x = np.array([1, 2, 3, 4, 5, 6], dtype=np.int64)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/eval_frame.py", line 448, in catch_errors
    return callback(frame, cache_size, hooks, frame_state)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/convert_frame.py", line 527, in _convert_frame
    result = inner_convert(frame, cache_size, hooks, frame_state)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/convert_frame.py", line 127, in _fn
    return fn(*args, **kwargs)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/convert_frame.py", line 360, in _convert_frame_assert
    return _compile(
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/utils.py", line 180, in time_wrapper
    r = func(*args, **kwargs)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/convert_frame.py", line 430, in _compile
    out_code = transform_code_object(code, transform)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/bytecode_transformation.py", line 1000, in transform_code_object
    transformations(instructions, code_options)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/convert_frame.py", line 415, in transform
    tracer.run()
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/symbolic_convert.py", line 2024, in run
    super().run()
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/symbolic_convert.py", line 707, in run
    and self.step()
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/symbolic_convert.py", line 667, in step
    getattr(self, inst.opname)(inst)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/symbolic_convert.py", line 389, in wrapper
    return inner_fn(self, inst)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/symbolic_convert.py", line 1099, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/symbolic_convert.py", line 558, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/variables/torch.py", line 607, in call_function
    tensor_variable = wrap_fx_proxy(
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/variables/builder.py", line 1063, in wrap_fx_proxy
    return wrap_fx_proxy_cls(
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/variables/builder.py", line 1098, in wrap_fx_proxy_cls
    example_value = get_fake_value(proxy.node, tx)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/utils.py", line 1298, in get_fake_value
    raise TorchRuntimeError(str(e)).with_traceback(e.__traceback__) from None
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/utils.py", line 1266, in get_fake_value
    return wrap_fake_exception(
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/utils.py", line 860, in wrap_fake_exception
    return fn()
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/utils.py", line 1267, in <lambda>
    lambda: run_node(tx.output, node, args, kwargs, nnmodule)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/utils.py", line 1332, in run_node
    raise RuntimeError(fn_str + str(e)).with_traceback(e.__traceback__) from e
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/utils.py", line 1319, in run_node
    return node.target(*args, **kwargs)
torch._dynamo.exc.TorchRuntimeError: Failed running call_function <class 'torch.LongTensor'>(*([FakeTensor(..., size=(), dtype=torch.int64), FakeTensor(..., size=(), dtype=torch.int64), FakeTensor(..., size=(), dtype=torch.int64)],), **{}):
The tensor has a non-zero number of elements, but its data is not allocated yet. Caffe2 uses a lazy allocation, so you will need to call mutable_data() or raw_mutable_data() to actually allocate memory.

from user code:
   File "/data/users/ezyang/d/pytorch/test/dynamo/test_misc.py", line 3543, in <resume in fn>
    z = torch.LongTensor(y)

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

-------------------------------------------------------- Captured stdout call --------------------------------------------------------
frames [('total', 2), ('ok', 1)]
unimplemented []
graph_break [('numpy.<built-in function array>()', 1)]
____________________ DynamicShapesMiscTests.test_generate_tensor_from_list_of_numpy_primitive_type_dynamic_shapes ____________________
Traceback (most recent call last):
  File "/data/users/ezyang/d/pytorch/test/dynamo/test_misc.py", line 3548, in test_generate_tensor_from_list_of_numpy_primitive_type
    res = opt_fn()
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/eval_frame.py", line 295, in _fn
    return fn(*args, **kwargs)
  File "/data/users/ezyang/d/pytorch/test/dynamo/test_misc.py", line 3541, in fn
    x = np.array([1, 2, 3, 4, 5, 6], dtype=np.int64)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/eval_frame.py", line 448, in catch_errors
    return callback(frame, cache_size, hooks, frame_state)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/convert_frame.py", line 527, in _convert_frame
    result = inner_convert(frame, cache_size, hooks, frame_state)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/convert_frame.py", line 127, in _fn
    return fn(*args, **kwargs)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/convert_frame.py", line 360, in _convert_frame_assert
    return _compile(
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/utils.py", line 180, in time_wrapper
    r = func(*args, **kwargs)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/convert_frame.py", line 430, in _compile
    out_code = transform_code_object(code, transform)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/bytecode_transformation.py", line 1000, in transform_code_object
    transformations(instructions, code_options)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/convert_frame.py", line 415, in transform
    tracer.run()
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/symbolic_convert.py", line 2024, in run
    super().run()
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/symbolic_convert.py", line 707, in run
    and self.step()
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/symbolic_convert.py", line 667, in step
    getattr(self, inst.opname)(inst)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/symbolic_convert.py", line 389, in wrapper
    return inner_fn(self, inst)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/symbolic_convert.py", line 1099, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/symbolic_convert.py", line 558, in call_function
    self.push(fn.call_function(self, args, kwargs))
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/variables/torch.py", line 607, in call_function
    tensor_variable = wrap_fx_proxy(
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/variables/builder.py", line 1063, in wrap_fx_proxy
    return wrap_fx_proxy_cls(
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/variables/builder.py", line 1098, in wrap_fx_proxy_cls
    example_value = get_fake_value(proxy.node, tx)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/utils.py", line 1298, in get_fake_value
    raise TorchRuntimeError(str(e)).with_traceback(e.__traceback__) from None
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/utils.py", line 1266, in get_fake_value
    return wrap_fake_exception(
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/utils.py", line 860, in wrap_fake_exception
    return fn()
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/utils.py", line 1267, in <lambda>
    lambda: run_node(tx.output, node, args, kwargs, nnmodule)
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/utils.py", line 1332, in run_node
    raise RuntimeError(fn_str + str(e)).with_traceback(e.__traceback__) from e
  File "/data/users/ezyang/d/pytorch/torch/_dynamo/utils.py", line 1319, in run_node
    return node.target(*args, **kwargs)
torch._dynamo.exc.TorchRuntimeError: Failed running call_function <class 'torch.LongTensor'>(*([FakeTensor(..., size=(), dtype=torch.int64), FakeTensor(..., size=(), dtype=torch.int64), FakeTensor(..., size=(), dtype=torch.int64)],), **{}):
The tensor has a non-zero number of elements, but its data is not allocated yet. Caffe2 uses a lazy allocation, so you will need to call mutable_data() or raw_mutable_data() to actually allocate memory.

from user code:
   File "/data/users/ezyang/d/pytorch/test/dynamo/test_misc.py", line 3543, in <resume in fn>
    z = torch.LongTensor(y)

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

-------------------------------------------------------- Captured stdout call --------------------------------------------------------
frames [('total', 2), ('ok', 1)]
unimplemented []
graph_break [('numpy.<built-in function array>()', 1)]
```

It doesn't fail if I run it under python directly

### Versions

main