Repository: pytorch
Issue: [FX] Tensor constants are not lifted to attributes in direct Graph construction
Labels: triaged,module: fx

### 🐛 Describe the bug

```
import torch
import torch.fx

g = torch.fx.Graph()
x = g.placeholder('x')
added = g.call_function(torch.add, (x, torch.randn(5, 3)))
g.output(added)

gm = torch.fx.GraphModule(torch.nn.Module(), g)

print(gm.code)

gm(torch.randn(5, 3))
```

```
def forward(self, x):
    add = torch.add(x, tensor([[-1.9261e+00,  7.0327e-01,  8.8908e-02],
            [ 7.4264e-01, -7.2484e-01,  2.5882e+00],
            [-1.0321e+00, -3.5961e-01, -2.0262e-03],
            [-1.4555e-01, -2.7759e-01, -6.3642e-01],
            [ 1.5129e-01, -2.9932e+00, -1.5494e+00]]));  x = None
    return add
    
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/torch/fx/graph_module.py", line 608, in wrapped_call
    return super(type(self), self).__call__(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py", line 1102, in _call_impl
    return forward_call(*input, **kwargs)
  File "<eval_with_key>.0", line 5, in forward
    add = torch.add(x, tensor([[-1.9261e+00,  7.0327e-01,  8.8908e-02],
NameError: name 'tensor' is not defined

Call using an FX-traced Module, line 5 of the traced Module's generated forward function:
def forward(self, x):
    add = torch.add(x, tensor([[-1.9261e+00,  7.0327e-01,  8.8908e-02],

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <--- HERE
            [ 7.4264e-01, -7.2484e-01,  2.5882e+00],

            [-1.0321e+00, -3.5961e-01, -2.0262e-03],

---------------------------------------------------------------------------
```

We currently lift tensor constants into attributes in `Tracer.create_arg`, but this is never called when someone constructs a `GraphModule` from a manually-created `Graph`. We should figure out a way to make this behavior consistent, e.g. by pulling this logic from `Tracer` or reorganizing the code 

### Versions


Collecting environment information...
PyTorch version: 1.11.0a0+git708f7b1
Is debug build: False
CUDA used to build PyTorch: 11.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 18.04.5 LTS (x86_64)
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Clang version: 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
CMake version: version 3.19.6
Libc version: glibc-2.27

Python version: 3.9.5 (default, Jun  4 2021, 12:28:51)  [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.4.0-1051-aws-x86_64-with-glibc2.27
Is CUDA available: False
CUDA runtime version: 11.1.105
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Probably one of the following:
/usr/local/cuda-10.1/targets/x86_64-linux/lib/libcudnn.so.7.6.5
/usr/local/cuda-10.2/targets/x86_64-linux/lib/libcudnn.so.7.6.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.0.5
/usr/local/cuda-11.0/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.0.5
/usr/local/cuda-11.1/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.0.5
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] mypy==0.812
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.21.2
[pip3] pytorch-sphinx-theme==0.0.24
[pip3] torch==1.12.0a0+git19ef3eb
[pip3] torchtext==0.9.0a0+4de31fc
[pip3] torchvision==0.12.0a0+3e79d14
[conda] blas                      1.0                         mkl  
[conda] mkl                       2021.3.0           h06a4308_520  
[conda] mkl-include               2021.3.0           h06a4308_520  
[conda] mkl-service               2.4.0            py39h7f8727e_0  
[conda] mkl_fft                   1.3.1            py39hd3c417c_0  
[conda] mkl_random                1.2.2            py39h51133e4_0  
[conda] mypy                      0.812                    pypi_0    pypi
[conda] mypy-extensions           0.4.3                    pypi_0    pypi
[conda] mypy_extensions           0.4.3            py39h06a4308_1  
[conda] numpy                     1.21.2           py39h20f2e39_0  
[conda] numpy-base                1.21.2           py39h79a1101_0  
[conda] pytorch-sphinx-theme      0.0.24                    dev_0    <develop>
[conda] torch                     1.12.0a0+git19ef3eb           dev_0    <develop>
[conda] torchtext                 0.9.0a0+4de31fc          pypi_0    pypi
[conda] torchvision               0.12.0a0+3e79d14           dev_0    <develop>

cc @ezyang @SherlockNoMad