Seed changed when debugging acos in cuda

mszhanyi: <p>Generally,the seed is 1234 in testing. When I was debugging the test code  (test_ops.py),  I find in Linux (Ubuntu 16:04, Geforce 1080), when the op is acos and device is cuda:0, the seed is changed.<br>
The function of make_tensor, which in testing\common_utils.py result is.<br>
input:tensor([-0.7754+0.9690j, -0.2091-0.4136j,  0.9258+0.6882j, -0.8382-0.4225j,<br>
0.8441-0.1108j, -0.5019+0.6919j, -0.1726+0.5296j,  0.3494-0.6687j,<br>
-0.2975+0.4149j, -0.3018-0.2714j, -0.8510+0.9209j,  0.8011-0.6919j,<br>
-0.9471-0.7434j, -0.2749+0.2165j, -0.9148-0.3147j,  0.4287-0.4181j,<br>
-0.7237-0.8627j, -0.9948+0.8622j,  0.9395+0.6238j, -0.0843-0.6137j],<br>
device=‘cuda:0’, dtype=torch.complex128, requires_grad=True)<br>
If the seed is 1234, the result is.<br>
tensor([ 0.7069-0.7754j, -0.3517-0.2091j,  0.8470+0.9258j,  0.4945-0.8382j,<br>
0.2541+0.8441j,  0.4346-0.5019j,  0.6110-0.1726j, -0.7419+0.3494j,<br>
0.2255-0.2975j,  0.7646-0.3018j, -0.9519-0.8510j,  0.5869+0.8011j,<br>
-0.5018-0.9471j,  0.0435-0.2749j, -0.8904-0.9148j, -0.8258+0.4287j,<br>
0.2637-0.7237j,  0.7252-0.9948j, -0.5960+0.9395j, -0.8582-0.0843j],<br>
device=‘cuda:0’, dtype=torch.complex128, requires_grad=True)</p>
<p>I debugged in Linux CPU, windows CPU and GPU. The input tensors are all generated by seed of 1234 in the same condition.</p>
<p>So far, I don’t find when the seed is changed and bad impacts of this change.</p>

albanD: <p>Hi,</p>
<p>Could this be related to this issue: <a href="https://github.com/pytorch/pytorch/issues/42952">https://github.com/pytorch/pytorch/issues/42952</a> ?</p>

mszhanyi: <p>Not exactly. I found it when I was debugging a similar issue that you mentioned.<br>
The test still passed even the input value is not expected due to a unknown seed.<br>
I think it’s reasonable for the test result but I just doubt maybe there’s another potential issue in creating the input. So I raised it in forum not in issues.<br>
The details to reproduce  is:</p>
<ol>
<li>line 92 in test_ops.py<br>
<span class="mention">@dtypes</span>(torch.double, torch.cdouble)<br>
<span class="mention">@ops</span>(op_db)<br>
def test_fn_grad(self, device, dtype, op):<br>
if op.name == “acos” and device == “cuda:0”:<br>
self._grad_test_helper(device, dtype, op, op.get_op())</li>
<li>set breakpoint on self._grad_test_helper(device, dtype, op, op.get_op())</li>
<li>step in and step in …<br>
at the line 1464 of common_utils.py<br>
the value is not generated by the seed of 1234.<br>
I don’t know what’s the seed because there’s no way to get the seed from the random value.</li>
</ol>

albanD: <p>Is it because other things generated random Tensors before hitting this line?</p>
<blockquote>
<p>at the line 1464 of common_utils.py</p>
</blockquote>
<p>Can you link to the code on github at a particular commit (press y when on the page with the file you want then select the line) because I think this line number is outdated?</p>

mszhanyi: <p>Thank you for taking care of it.<br>
we’ll find the real and imag are not expected.<br>
<a href="https://github.com/pytorch/pytorch/blob/1c15452703be4e918a2989316f9a1e2fec7a825d/torch/testing/_internal/common_utils.py#L1464" rel="nofollow noopener">get the rand complex</a></p>

albanD: <p>A seed of 1234 on CPU and GPU will generate different values, this is expected as we use different random number generators on these two devices.<br>
If you generate both on CPU though, you will get the same result, as long as you don’t draw any other random number between the time where you set the seed and sample. For example, windows might not run all the tests that run on linux. And so by the time you get to this test, you might have drawn a different number of samples from the random number generator, giving you different values.</p>

mszhanyi: <p>I know CPU and GPU’s random are different.<br>
The issue is in Linux not Windows.</p>
<p>we could add one line to force manual_seed as 1234 and comment it later.<br>
we’ll find the real and imag value are different if the device is cuda:0<br>
<span class="hashtag">#torch</span>. manual_seed(1234)<br>
real = torch.rand(size, device=device, dtype=float_dtype) * span + low<br>
imag = torch.rand(size, device=device, dtype=float_dtype) * span + low</p>
<p>So, is it caused by samples generated by other tests?</p>

albanD: <blockquote>
<p>So, is it caused by samples generated by other tests?</p>
</blockquote>
<p>Or more likely other inputs generated for this test?</p>
<p>Even after re-reading your message above I feel like I’m missing something as I don’t see what is the exact problem is? Could you rephrase what is unexpected here please?</p>

mszhanyi: <p>Thank your reply. I just curious why the test input value isn’t computed by the seed of 1234 in this condition (acos, cuda:0, linux).</p>
<p>In other conditions, I can get the same value as <a href="https://github.com/pytorch/pytorch/blob/1c15452703be4e918a2989316f9a1e2fec7a825d/test/test_ops.py#L69" rel="nofollow noopener">Pytorch source code</a> with the below snippet.<br>
Anyway, it’s not a big issue so far.</p>
<p>import torch<br>
span = 1.99998<br>
low = -0.99999<br>
size = (20,)<br>
float_dtype = torch.float64<br>
torch.manual_seed(1234)<br>
device = torch.device(“cuda:0”) <span class="hashtag">#change</span> cpu or other<br>
real = torch.rand(size, device=device, dtype=float_dtype) * span + low<br>
imag = torch.rand(size, device=device, dtype=float_dtype) * span + low<br>
c = torch.complex(real, imag)<br>
c.requires_grad = True<br>
print(f"c = {c}")</p>

albanD: <p>Ho,</p>
<p>Maybe because acos samples the input at a different time <a href="https://github.com/pytorch/pytorch/blob/36ec8f8fb8d0356db7cb67230500f70833a2b2ba/torch/testing/_internal/common_methods_invocations.py#L682">here</a> because it needs to clamp it and so cannot just specify the size?</p>