Repository: pytorch/pytorch

Issue:
`torch.compile` can not be used out of the box in nightly/2.1
### 🐛 Describe the bug

Consider following trivial sample:
```python
import torch

def foo(x: torch.Tensor) -> torch.Tensor:
   return torch.sin(x) + torch.cos(x)

if __name__=="__main__":
    x = torch.rand(3, 3, device="cuda")
    x_eager = foo(x)
    x_pt2 = torch.compile(foo)(x)
    print(torch.allclose(x_eager, x_pt2))
```

Following sample can be executed on vanilla `ubuntu:20.04` using 2.0.1, but fails with 2.1 release candidate, due to the spurious dependencies:
```shell
% python3 foo.py 
...
  File "/usr/local/lib/python3.8/dist-packages/torch/_dynamo/variables/base.py", line 96, in __call__
    obj = type.__call__(cls, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/torch/_dynamo/variables/constant.py", line 36, in __init__
    if isinstance(value, np.number):
torch._dynamo.exc.InternalTorchDynamoError: 'NoneType' object has no attribute 'number'

from user code:
   File "foo.py", line 4, in foo
    return torch.sin(x) + torch.cos(x)



Please note, that this is a regression, i.e. it used to work in 2.0
### Versions

2.1/nightly

cc @ezyang @msaroufim @wconstab @bdhirsh @anijain2305


Discussion:

Resolving patch:
diff --git a/torch/_dynamo/variables/constant.py b/torch/_dynamo/variables/constant.py
--- a/torch/_dynamo/variables/constant.py
+++ b/torch/_dynamo/variables/constant.py
@@ -33,7 +33,7 @@ def __init__(self, value, **kwargs):
             for disallowed_type, reason in _type_to_assert_reason.items():
                 assert not isinstance(value, disallowed_type), reason
 
-        if isinstance(value, np.number):
+        if np is not None and isinstance(value, np.number):
             self.value = value.item()
         else:
             self.value = value
diff --git a/torch/_inductor/codegen/cpp.py b/torch/_inductor/codegen/cpp.py
--- a/torch/_inductor/codegen/cpp.py
+++ b/torch/_inductor/codegen/cpp.py
@@ -9,7 +9,6 @@
 from copy import copy, deepcopy
 from typing import Dict, List
 
-import numpy
 import sympy
 
 import torch
@@ -2148,7 +2147,7 @@ def constant(val, dtype):
                     # VecKernel override dtype for constant
                     # Vectorization only support int32/fp32 now
                     # So if dtype = int64/fp64, we will cast it to int32/fp32 if possible
-                    i32_iinfo = numpy.iinfo(numpy.int32)
+                    i32_iinfo = torch.iinfo(torch.int32)
                     if (
                         dtype == torch.int64
                         and val <= i32_iinfo.max
@@ -2156,12 +2155,12 @@ def constant(val, dtype):
                     ):
                         opt_ctx.dtype = torch.int32
 
-                    f32_iinfo = numpy.finfo(numpy.float32)
+                    f32_iinfo = torch.finfo(torch.float32)
                     if dtype == torch.double:
                         if (
                             (val <= f32_iinfo.max and val >= f32_iinfo.min)
-                            or (val == numpy.inf)
-                            or (val == -numpy.inf)
+                            or (val == torch.inf)
+                            or (val == -torch.inf)
                         ):
                             opt_ctx.dtype = torch.float32
 
@@ -2206,7 +2205,7 @@ def can_use_int32():
 
                     vars_ranges = {k: ValueRanges(0, v - 1) for k, v in sizes.items()}
                     if not vars_ranges or len(vars_ranges) != len(free_symbols):
-                        i32_iinfo = numpy.iinfo(numpy.int32)
+                        i32_iinfo = torch.iinfo(torch.int32)
                         return (
                             expr.is_number
                             and expr <= i32_iinfo.max
diff --git a/torch/_inductor/fx_passes/split_cat.py b/torch/_inductor/fx_passes/split_cat.py
--- a/torch/_inductor/fx_passes/split_cat.py
+++ b/torch/_inductor/fx_passes/split_cat.py
@@ -3,8 +3,6 @@
 import operator
 from typing import Callable, List, Sequence, Tuple, Union
 
-import numpy
-
 import torch
 from torch._dynamo.utils import counters
 
@@ -454,8 +452,7 @@ def get_simplified_split_ranges(
                 for user_input in user_inputs
                 if isinstance(user_input, tuple)
             }
-
-        cumulative_sizes = [0] + list(numpy.cumsum(split_sections))
+        cumulative_sizes = [0] + torch.cumsum(torch.tensor(split_sections), 0).tolist()
         split_ranges = sorted(
             [(cumulative_sizes[r[0]], cumulative_sizes[r[1] + 1]) for r in ranges]
         )
@@ -578,7 +575,7 @@ def replace_split(
                     for i in range(len(split_ranges))
                 ]
         # Now assign the right getitem to the right input
-        cumulative_sizes = [0] + list(numpy.cumsum(split_sections))
+        cumulative_sizes = [0] + torch.cumsum(torch.tensor(split_sections), 0).tolist()
         new_user_inputs_list = []
         for user_inputs in user_inputs_list:
             new_user_inputs = []


Test patch: