Repository: pytorch
Issue: Compilation time for packing function is quite long
Labels: triaged,oncall: pt2,module: startup-tracing-compile

I've written a function to pack a 2D int4 tensor into a `int32` tensor. I want use `torch.compile` to speed up it, but the compilation process takes over 200 seconds. 

It's not a bug, but I wonder if you have any suggestions to speed up the compilation process.

If I already know the tensor's shape or shape range, is there any way to let `compile` know it?


### compile logs

```bash
I0704 23:06:28.505241 139809694049280 torch/_dynamo/utils.py:320] TorchDynamo compilation metrics:
I0704 23:06:28.505241 139809694049280 torch/_dynamo/utils.py:320] Function                                Runtimes (s)
I0704 23:06:28.505241 139809694049280 torch/_dynamo/utils.py:320] ------------------------------------  --------------
I0704 23:06:28.505241 139809694049280 torch/_dynamo/utils.py:320] _compile.<locals>.compile_inner             253.381
I0704 23:06:28.505241 139809694049280 torch/_dynamo/utils.py:320] OutputGraph.call_user_compiler              240.947
I0704 23:06:28.505241 139809694049280 torch/_dynamo/utils.py:320] create_aot_dispatcher_function              239.832
I0704 23:06:28.505241 139809694049280 torch/_dynamo/utils.py:320] compile_fx.<locals>.fw_compiler_base        197.729
I0704 23:06:28.505241 139809694049280 torch/_dynamo/utils.py:320] compile_fx_inner                            195.232
I0704 23:06:28.505241 139809694049280 torch/_dynamo/utils.py:320] GraphLowering.run                            59.4836
I0704 23:06:28.505241 139809694049280 torch/_dynamo/utils.py:320] GraphLowering.compile_to_module             118.977
I0704 23:06:28.505241 139809694049280 torch/_dynamo/utils.py:320] Scheduler.__init__                           62.0883
I0704 23:06:28.505241 139809694049280 torch/_dynamo/utils.py:320] Scheduler.codegen                            56.6688
I0704 23:06:28.505241 139809694049280 torch/_dynamo/utils.py:320] compile_file                                  3.1052
I0704 23:06:28.505241 139809694049280 torch/_dynamo/utils.py:320] WrapperCodeGen.generate                       0.1219

```

### Minified repro

 TORCH_COMPILE_DEBUG=1 python pack_tensor.py

```python
import timeit
import torch


def pack_2d_intx_tensor_to_int32_tensor(
    raw_tensor: torch.Tensor, n_pack: int, bits: int, compression_dtype=torch.int32
) -> torch.Tensor:
    target_len = (raw_tensor.shape[1] + n_pack - 1) // n_pack
    raw_tensor = raw_tensor.to(compression_dtype)
    packed_tensor = torch.zeros(raw_tensor.shape[0], target_len, dtype=compression_dtype)
    mask = torch.tensor(2**bits - 1, dtype=compression_dtype)
    for j in range(packed_tensor.shape[1]):
        start = n_pack * j
        end = n_pack * (j + 1)
        tmp = raw_tensor[:, start:end]
        tmp &= mask
        for e in range(tmp.shape[1]):
            tmp[:, e] = tmp[:, e] << (bits * e)
            packed_tensor[:, j] |= tmp[:, e]
    return packed_tensor


fn1_opt = torch.compile(pack_2d_intx_tensor_to_int32_tensor)


out_features = 1024
in_features = 1024
bits = 4
n_pack = 32 // 4
compression_dtype = torch.int32
raw_tensor = torch.randint(0, 15, (out_features, in_features), dtype=torch.int8)
iters = 20


time_res = timeit.timeit(lambda: fn1_opt(raw_tensor, n_pack, bits, compression_dtype), number=iters)
print(f"time_res: {time_res}")

```

### Versions

```bash

$ p torch/utils/collect_env.py
Collecting environment information...
PyTorch version: 2.3.0+cpu
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: CentOS Stream release 8 (x86_64)
GCC version: (GCC) 8.5.0 20210514 (Red Hat 8.5.0-22)
Clang version: 17.0.6 (Red Hat 17.0.6-1.module_el8+767+9fa966b8)
CMake version: version 3.26.5
Libc version: glibc-2.28

Python version: 3.8.16 (default, Jun 12 2023, 18:09:05)  [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-4.18.0-383.el8.x86_64-x86_64-with-glibc2.17
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
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              48
On-line CPU(s) list: 0-47
Thread(s) per core:  1
Core(s) per socket:  24
Socket(s):           2
NUMA node(s):        2
Vendor ID:           GenuineIntel
CPU family:          6
Model:               85
Model name:          Intel(R) Xeon(R) Gold 6252 CPU @ 2.10GHz
Stepping:            7
CPU MHz:             3127.938
CPU max MHz:         3700.0000
CPU min MHz:         1000.0000
BogoMIPS:            4200.00
L1d cache:           32K
L1i cache:           32K
L2 cache:            1024K
L3 cache:            36608K
NUMA node0 CPU(s):   0-23
NUMA node1 CPU(s):   24-47
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 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 ssbd mba ibrs ibpb stibp ibrs_enhanced 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 hwp hwp_act_window hwp_epp hwp_pkg_req pku ospke avx512_vnni md_clear flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] flake8==7.0.0
[pip3] flake8-import-conventions==0.1.0
[pip3] intel-extension-for-pytorch==2.3.0
[pip3] model-explorer-onnx==0.2.6
[pip3] mypy==1.10.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.23.5
[pip3] onnx==1.16.1
[pip3] onnx-graphsurgeon==0.3.25
[pip3] onnxruntime==1.17.3
[pip3] onnxruntime-extensions==0.8.0
[pip3] onnxscript==0.1.0.dev20240627
[pip3] pytorch-lightning==2.1.2
[pip3] pytorch-quantization==2.1.2
[pip3] pytorch-triton-rocm==2.0.2
[pip3] tf2onnx==1.14.0
[pip3] torch==2.3.0+cpu
[pip3] torchaudio==2.2.0.dev20240326
[pip3] torchmetrics==1.2.0
[pip3] torchprofile==0.0.4
[pip3] torchvision==0.18.0+cpu
[conda] blas                      1.0                         mkl    defaults
[conda] cpuonly                   2.0                           0    pytorch-nightly
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] intel-extension-for-pytorch 2.3.0                    pypi_0    pypi
[conda] libjpeg-turbo             2.0.0                h9bf148f_0    pytorch
[conda] mkl                       2023.1.0         h213fc3f_46344    defaults
[conda] mkl-service               2.4.0            py38h5eee18b_1    defaults
[conda] mkl_fft                   1.3.8            py38h5eee18b_0    defaults
[conda] mkl_random                1.2.4            py38hdb19cb5_0    defaults
[conda] mxnet-mkl                 1.6.0                    pypi_0    pypi
[conda] numpy                     1.23.5                   pypi_0    pypi
[conda] numpy-base                1.24.3           py38h060ed82_1    defaults
[conda] pytorch-lightning         2.1.2                    pypi_0    pypi
[conda] pytorch-mutex             1.0                         cpu    pytorch
[conda] pytorch-quantization      2.1.2                    pypi_0    pypi
[conda] pytorch-triton-rocm       2.0.2                    pypi_0    pypi
[conda] torch                     2.3.0+cpu                pypi_0    pypi
[conda] torchaudio                2.2.2+cpu                pypi_0    pypi
[conda] torchmetrics              1.2.0                    pypi_0    pypi
[conda] torchprofile              0.0.4                    pypi_0    pypi
[conda] torchvision               0.18.0+cpu               pypi_0    pypi
```

cc @ezyang @anijain2305 @chauhang @penguinwu @oulgen @jamesjwu @aorenste @laithsakka