Repository: pytorch
Issue: numpy's division compiled but returning Different results
Labels: triaged,module: numpy,oncall: pt2

### 🐛 Describe the bug

I want to compile a model with torch.compile
One of the snippets I've used is
```python
            print(type(self.upsample_scale), self.upsample_scale, "self.upsample_scale")
            print(1/self.upsample_scale, type(1/self.upsample_scale), "1/self.upsample_scale")
            rad_values = torch.nn.functional.interpolate(rad_values.transpose(1, 2), 
                                                         scale_factor=1/self.upsample_scale, 
                                                         mode="linear").transpose(1, 2)
```
And this part is actually raising the issue.
Is it an issue with the compilation of numpy's division. As I see type changes before and after compilation.
Or is it an issue with interpolate function, not getting proper arguments.

I further tried a simple code
```python
def foo(x):
    return 1/x

x = np.float64(12)
print(foo(x))
print(type(foo(x)))

# Output
# 0.08333333333333333
# <class 'numpy.float64'>
```

and 
```python
@torch.compile
def foo(x):
    return 1/x

x = np.float64(12)
print(foo(x))
print(type(foo(x)))

# Output
# 0.08333333333333333
# <class 'numpy.ndarray'>
```
Is it an issue with numpy's division compilation or is the issue of passing this value in interpolate should cause issue or not.

### Error logs

Before compile

<class 'numpy.int64'> 300 self.upsample_scale
0.0033333333333333335 <class 'numpy.float64'> 1/self.upsample_scale
And everything works

After Compile same code returns

<class 'numpy.int64'> 300 self.upsample_scale
0.0033333334 <class 'numpy.ndarray'> 1/self.upsample_scale
```txt
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[11], line 33
     31 for text in texts:
     32     start_time = time.time()
---> 33     out = infer(text, inp_audio)
     34     print(f"Time taken: {time.time() - start_time}")
     35     ipd.display(ipd.Audio(out, rate=24000))

Cell In[11], line 18, in infer(text, audio)
     15 wave_tensor = torch.from_numpy(wave).float().to(device)
     16 encoded = encoder(wave_tensor)
---> 18 outs = model(tokens, input_lengths, encoded).cpu().numpy()[..., :-50]
     19 return outs

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:1736, in Module._wrapped_call_impl(self, *args, **kwargs)
   1734     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   1735 else:
-> 1736     return self._call_impl(*args, **kwargs)

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:1747, in Module._call_impl(self, *args, **kwargs)
   1742 # If we don't have any hooks, we want to skip the rest of the logic in
   1743 # this function, and just call forward.
   1744 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
   1745         or _global_backward_pre_hooks or _global_backward_hooks
   1746         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1747     return forward_call(*args, **kwargs)
   1749 result = None
   1750 called_always_called_hooks = set()

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/.venv/lib/python3.12/site-packages/torch/_dynamo/eval_frame.py:465, in _TorchDynamoContext.__call__.<locals>._fn(*args, **kwargs)
    460 saved_dynamic_layer_stack_depth = (
    461     torch._C._functorch.get_dynamic_layer_stack_depth()
    462 )
    464 try:
--> 465     return fn(*args, **kwargs)
    466 finally:
    467     # Restore the dynamic layer stack depth if necessary.
    468     torch._C._functorch.pop_dynamic_layer_stack_and_undo_to_depth(
    469         saved_dynamic_layer_stack_depth
    470     )

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:1736, in Module._wrapped_call_impl(self, *args, **kwargs)
   1734     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   1735 else:
-> 1736     return self._call_impl(*args, **kwargs)

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:1747, in Module._call_impl(self, *args, **kwargs)
   1742 # If we don't have any hooks, we want to skip the rest of the logic in
   1743 # this function, and just call forward.
   1744 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
   1745         or _global_backward_pre_hooks or _global_backward_hooks
   1746         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1747     return forward_call(*args, **kwargs)
   1749 result = None
   1750 called_always_called_hooks = set()

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/StyleTTS2/Models/StyleTTS2Catalan/models.py:3042, in StyleTTS2Catalan.forward(self, tokens, input_lengths, ref_s, alpha, beta, diffusion_steps, embedding_scale, device)
   3039     self.text_aligner = ASRCNN(**args.asr_config)
   3040     self.pitch_extractor = JDCNet(num_class=1, seq_len=192)
-> 3042 def forward(self, tokens, input_lengths, ref_s, alpha=0.3, beta=0.7, 
   3043             diffusion_steps=5, embedding_scale=1, device='cuda'):
   3044     # Prepare text mask
   3045     text_mask = length_to_mask(input_lengths).to(device)
   3047     # Text encoding

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/StyleTTS2/Models/StyleTTS2Catalan/models.py:3045, in torch_dynamo_resume_in_forward_at_3045(___stack0, self, tokens, input_lengths, ref_s, alpha, beta, diffusion_steps, embedding_scale, device)
   3042 def forward(self, tokens, input_lengths, ref_s, alpha=0.3, beta=0.7, 
   3043             diffusion_steps=5, embedding_scale=1, device='cuda'):
   3044     # Prepare text mask
-> 3045     text_mask = length_to_mask(input_lengths).to(device)
   3047     # Text encoding
   3048     t_en = self.text_encoder(tokens, input_lengths, text_mask)

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/StyleTTS2/Models/StyleTTS2Catalan/models.py:3048, in torch_dynamo_resume_in_forward_at_3048(___stack0, self, tokens, input_lengths, ref_s, alpha, beta, diffusion_steps, embedding_scale, device, text_mask)
   3045 text_mask = length_to_mask(input_lengths).to(device)
   3047 # Text encoding
-> 3048 t_en = self.text_encoder(tokens, input_lengths, text_mask)
   3050 # Duration prediction with BERT and subsequent processing
   3051 bert_dur = self.bert(tokens, attention_mask=(~text_mask).int())

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/StyleTTS2/Models/StyleTTS2Catalan/models.py:3055, in torch_dynamo_resume_in_forward_at_3055(___stack0, self, input_lengths, ref_s, alpha, beta, device, text_mask, t_en, d_en)
   3052 d_en = self.bert_encoder(bert_dur).transpose(-1, -2)
   3054 # Sample from the latent space using diffusion
-> 3055 s_pred = self.sampler(
   3056     noise=torch.randn((1, 256)).unsqueeze(1).to(device),
   3057     embedding=bert_dur,
   3058     embedding_scale=embedding_scale,
   3059     features=ref_s,
   3060     num_steps=diffusion_steps
   3061 ).squeeze(1)
   3063 # Split latent variables into `s` and `ref`
   3064 s = s_pred[:, 128:]

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/StyleTTS2/Models/StyleTTS2Catalan/models.py:3104, in torch_dynamo_resume_in_forward_at_3078(___stack0, self, input_lengths, device, t_en, s, ref)
   3101 asr = asr_new
   3103 # Decoder output
-> 3104 out = self.decoder(asr, F0_pred, N_pred, ref.squeeze().unsqueeze(0))
   3106 return out.squeeze()

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:1736, in Module._wrapped_call_impl(self, *args, **kwargs)
   1734     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   1735 else:
-> 1736     return self._call_impl(*args, **kwargs)

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:1747, in Module._call_impl(self, *args, **kwargs)
   1742 # If we don't have any hooks, we want to skip the rest of the logic in
   1743 # this function, and just call forward.
   1744 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
   1745         or _global_backward_pre_hooks or _global_backward_hooks
   1746         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1747     return forward_call(*args, **kwargs)
   1749 result = None
   1750 called_always_called_hooks = set()

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/StyleTTS2/Models/StyleTTS2Catalan/models.py:2826, in Decoder.forward(self, asr, F0_curve, N, s)
   2818     self.asr_res = nn.Sequential(
   2819         weight_norm(nn.Conv1d(512, 64, kernel_size=1)),
   2820     )
   2823     self.generator = Generator(style_dim, resblock_kernel_sizes, upsample_rates, upsample_initial_channel, resblock_dilation_sizes, upsample_kernel_sizes)
-> 2826 def forward(self, asr, F0_curve, N, s):
   2827     if self.training:
   2828         downlist = [0, 3, 7]

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:1736, in Module._wrapped_call_impl(self, *args, **kwargs)
   1734     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   1735 else:
-> 1736     return self._call_impl(*args, **kwargs)

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:1747, in Module._call_impl(self, *args, **kwargs)
   1742 # If we don't have any hooks, we want to skip the rest of the logic in
   1743 # this function, and just call forward.
   1744 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
   1745         or _global_backward_pre_hooks or _global_backward_hooks
   1746         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1747     return forward_call(*args, **kwargs)
   1749 result = None
   1750 called_always_called_hooks = set()

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/StyleTTS2/Models/StyleTTS2Catalan/models.py:2759, in Generator.forward(self, x, s, f0)
   2756     self.ups.apply(init_weights)
   2757     self.conv_post.apply(init_weights)
-> 2759 def forward(self, x, s, f0):
   2761     f0 = self.f0_upsamp(f0[:, None]).transpose(1, 2)  # bs,n,t
   2763     har_source, noi_source, uv = self.m_source(f0)

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:1736, in Module._wrapped_call_impl(self, *args, **kwargs)
   1734     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   1735 else:
-> 1736     return self._call_impl(*args, **kwargs)

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:1747, in Module._call_impl(self, *args, **kwargs)
   1742 # If we don't have any hooks, we want to skip the rest of the logic in
   1743 # this function, and just call forward.
   1744 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
   1745         or _global_backward_pre_hooks or _global_backward_hooks
   1746         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1747     return forward_call(*args, **kwargs)
   1749 result = None
   1750 called_always_called_hooks = set()

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/StyleTTS2/Models/StyleTTS2Catalan/models.py:2695, in SourceModuleHnNSF.forward(self, x)
   2692     self.l_linear = torch.nn.Linear(harmonic_num + 1, 1)
   2693     self.l_tanh = torch.nn.Tanh()
-> 2695 def forward(self, x):
   2696     """
   2697     Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
   2698     F0_sampled (batchsize, length, 1)
   2699     Sine_source (batchsize, length, 1)
   2700     noise_source (batchsize, length 1)
   2701     """
   2702     # source for harmonic branch

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:1736, in Module._wrapped_call_impl(self, *args, **kwargs)
   1734     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   1735 else:
-> 1736     return self._call_impl(*args, **kwargs)

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/.venv/lib/python3.12/site-packages/torch/nn/modules/module.py:1747, in Module._call_impl(self, *args, **kwargs)
   1742 # If we don't have any hooks, we want to skip the rest of the logic in
   1743 # this function, and just call forward.
   1744 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
   1745         or _global_backward_pre_hooks or _global_backward_hooks
   1746         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1747     return forward_call(*args, **kwargs)
   1749 result = None
   1750 called_always_called_hooks = set()

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/StyleTTS2/Models/StyleTTS2Catalan/models.py:2632, in SineGen.forward(self, f0)
   2629         sines = torch.cos(i_phase * 2 * np.pi)
   2630     return sines
-> 2632 def forward(self, f0):
   2633     """ sine_tensor, uv = forward(f0)
   2634     input F0: tensor(batchsize=1, length, dim=1)
   2635               f0 for unvoiced steps should be 0
   2636     output sine_tensor: tensor(batchsize=1, length, dim)
   2637     output uv: tensor(batchsize=1, length, 1)
   2638     """
   2639     f0_buf = torch.zeros(f0.shape[0], f0.shape[1], self.dim,
   2640                          device=f0.device)

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/StyleTTS2/Models/StyleTTS2Catalan/models.py:2557, in SineGen._f02sine(self, f0_values)
   2554     uv = (f0 > self.voiced_threshold).type(torch.float32)
   2555     return uv
-> 2557 def _f02sine(self, f0_values):
   2558     """ f0_values: (batchsize, length, dim)
   2559         where dim indicates fundamental tone and overtones
   2560     """
   2561     # convert to F0 in rad. The interger part n can be ignored
   2562     # because 2 * np.pi * n doesn't affect phase

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/StyleTTS2/Models/StyleTTS2Catalan/models.py:2585, in torch_dynamo_resume_in__f02sine_at_2585(___stack0, self, rad_values)
   2571         # instantanouse phase sine[t] = sin(2*pi \sum_i=1 ^{t} rad)
   2572         if not self.flag_for_pulse:
   2573 #             # for normal case
   2574 
   (...)
   2583 
   2584 #             phase = torch.cumsum(rad_values, dim=1) * 2 * np.pi
-> 2585             print(type(self.upsample_scale), self.upsample_scale, "self.upsample_scale")
   2586             print(1/self.upsample_scale, type(1/self.upsample_scale), "1/self.upsample_scale")
   2587             rad_values = torch.nn.functional.interpolate(rad_values.transpose(1, 2), 
   2588                                                          scale_factor=1/self.upsample_scale, 
   2589                                                          mode="linear").transpose(1, 2)

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/StyleTTS2/Models/StyleTTS2Catalan/models.py:2586, in torch_dynamo_resume_in__f02sine_at_2586(___stack0, self, rad_values)
   2572         if not self.flag_for_pulse:
   2573 #             # for normal case
   2574 
   (...)
   2583 
   2584 #             phase = torch.cumsum(rad_values, dim=1) * 2 * np.pi
   2585             print(type(self.upsample_scale), self.upsample_scale, "self.upsample_scale")
-> 2586             print(1/self.upsample_scale, type(1/self.upsample_scale), "1/self.upsample_scale")
   2587             rad_values = torch.nn.functional.interpolate(rad_values.transpose(1, 2), 
   2588                                                          scale_factor=1/self.upsample_scale, 
   2589                                                          mode="linear").transpose(1, 2)
   2590             print(rad_values.shape)

File ~/Desktop/dev/aiml/audio/StyleTTS2-SageMaker/.venv/lib/python3.12/site-packages/torch/nn/functional.py:4559, in interpolate(input, size, scale_factor, mode, align_corners, recompute_scale_factor, antialias)
   4557 if input.dim() == 3 and mode == "linear":
   4558     assert align_corners is not None
-> 4559     return torch._C._nn.upsample_linear1d(
   4560         input, output_size, align_corners, scale_factors
   4561     )
   4562 if input.dim() == 4 and mode == "bilinear":
   4563     assert align_corners is not None

TypeError: upsample_linear1d() received an invalid combination of arguments - got (Tensor, NoneType, bool, list), but expected one of:
 * (Tensor input, tuple of ints output_size, bool align_corners, tuple of floats scale_factors)
      didn't match because some of the arguments have invalid types: (Tensor, !NoneType!, bool, !list of [numpy.ndarray]!)
 * (Tensor input, tuple of ints output_size, bool align_corners, float scales = None, *, Tensor out = None)
 ```
 

### Versions

PyTorch version: 2.5.1+cu124
Is debug build: False
CUDA used to build PyTorch: 12.4
ROCM used to build PyTorch: N/A

OS: Ubuntu 24.04.1 LTS (x86_64)
GCC version: (Ubuntu 13.2.0-23ubuntu4) 13.2.0
Clang version: 18.1.3 (1ubuntu1)
CMake version: version 3.28.3
Libc version: glibc-2.39

Python version: 3.12.3 (main, Nov  6 2024, 18:32:19) [GCC 13.2.0] (64-bit runtime)
Python platform: Linux-6.8.0-49-generic-x86_64-with-glibc2.39
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3050 Laptop GPU
Nvidia driver version: 550.120
cuDNN version: Could not collect
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
Address sizes:                        48 bits physical, 48 bits virtual
Byte Order:                           Little Endian
CPU(s):                               16
On-line CPU(s) list:                  0-15
Vendor ID:                            AuthenticAMD
Model name:                           AMD Ryzen 7 6800H with Radeon Graphics
CPU family:                           25
Model:                                68
Thread(s) per core:                   2
Core(s) per socket:                   8
Socket(s):                            1
Stepping:                             1
CPU(s) scaling MHz:                   53%
CPU max MHz:                          4785.0000
CPU min MHz:                          400.0000
BogoMIPS:                             6388.18
Flags:                                fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local user_shstk clzero irperf xsaveerptr rdpru wbnoinvd cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif v_spec_ctrl umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor smca fsrm debug_swap
Virtualization:                       AMD-V
L1d cache:                            256 KiB (8 instances)
L1i cache:                            256 KiB (8 instances)
L2 cache:                             4 MiB (8 instances)
L3 cache:                             16 MiB (1 instance)
NUMA node(s):                         1
NUMA node0 CPU(s):                    0-15
Vulnerability Gather data sampling:   Not affected
Vulnerability Itlb multihit:          Not affected
Vulnerability L1tf:                   Not affected
Vulnerability Mds:                    Not affected
Vulnerability Meltdown:               Not affected
Vulnerability Mmio stale data:        Not affected
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed:               Not affected
Vulnerability Spec rstack overflow:   Vulnerable: Safe RET, no microcode
Vulnerability Spec store bypass:      Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:             Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:             Mitigation; Retpolines; IBPB conditional; IBRS_FW; STIBP always-on; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                  Not affected
Vulnerability Tsx async abort:        Not affected

Versions of relevant libraries:
[pip3] numpy==2.0.2
[pip3] nvidia-cublas-cu12==12.4.5.8
[pip3] nvidia-cuda-cupti-cu12==12.4.127
[pip3] nvidia-cuda-nvrtc-cu12==12.4.127
[pip3] nvidia-cuda-runtime-cu12==12.4.127
[pip3] nvidia-cudnn-cu12==9.1.0.70
[pip3] nvidia-cufft-cu12==11.2.1.3
[pip3] nvidia-curand-cu12==10.3.5.147
[pip3] nvidia-cusolver-cu12==11.6.1.9
[pip3] nvidia-cusparse-cu12==12.3.1.170
[pip3] nvidia-nccl-cu12==2.21.5
[pip3] nvidia-nvjitlink-cu12==12.4.127
[pip3] nvidia-nvtx-cu12==12.4.127
[pip3] torch==2.5.1
[pip3] torch-model-archiver==0.12.0
[pip3] torchaudio==2.5.1
[pip3] torchserve==0.12.0
[pip3] triton==3.1.0
[conda] Could not collect

cc @mruberry @rgommers @chauhang @penguinwu