Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The "traced_cell.code" output is wrong and misleading!! #1449

Closed
git-hub-tig opened this issue Mar 27, 2021 · 2 comments · Fixed by #2428
Closed

The "traced_cell.code" output is wrong and misleading!! #1449

git-hub-tig opened this issue Mar 27, 2021 · 2 comments · Fixed by #2428
Assignees
Labels
docathon-h1-2023 A label for the docathon in H1 2023 medium torchscript Issues relating to TorchScript tutorials

Comments

@git-hub-tig
Copy link

git-hub-tig commented Mar 27, 2021

In this code:

The print(traced_cell.code) output in docs https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html is:

def forward(self,
    input: Tensor,
    h: Tensor) -> Tuple[Tensor, Tensor]:
  _0 = torch.add((self.linear).forward(input, ), h, alpha=1)
  _1 = torch.tanh(_0)
  return (_1, _1)

But I think the right print(traced_cell.code) output should be like:

def forward(self,  
    input: Tensor,  
    h: Tensor) -> Tuple[Tensor, Tensor]:  
  _0 = self.dg  
  _1 = (self.linear).forward(input, )  
  _2 = (_0).forward(_1, )  
  _3 = torch.tanh(torch.add(_1, h, alpha=1))  
  return (_3, _3)  

The problem of generation of misleading output maybe related to some complex things!
If any guy know the real answer, please help this!!

Continue:
I find that the output seems random, the below img is the my result of two attempts, the output is different!!!
image

I also test the below code:

import torch

class MyDecisionGate(torch.nn.Module):
    def forward(self, x):
        if x.sum() > 0:
            return x
        else:
            return -x

class MyCell(torch.nn.Module):
    def __init__(self, dg):
        super(MyCell, self).__init__()
        self.dg = dg
        self.linear = torch.nn.Linear(4, 4)

    def forward(self, x, h):
        new_h = torch.tanh(self.dg(self.linear(x)) + h)
        return new_h, new_h

my_cell = MyCell(MyDecisionGate())
x, h = torch.ones(5, 4)*-1, torch.ones(5, 4)
# x, h = torch.rand(5, 4), torch.rand(5, 4)

print("########################################################")
traced_cell = torch.jit.trace(my_cell, (x, h))
print(traced_cell.code)
print("########################################################")
x2, h2 = torch.ones(5, 4), torch.ones(5, 4)
print("########################################################")
traced2_cell = torch.jit.trace(my_cell, (x2, h2))
print(traced2_cell.code)
print("########################################################")

The output also can be random:
image

cc @eellison @suo @gmagogsfm @jamesr66a

@holly1238 holly1238 added the torchscript Issues relating to TorchScript tutorials label Jul 27, 2021
@svekars svekars added torchscript Issues relating to TorchScript tutorials and removed torchscript Issues relating to TorchScript tutorials labels Mar 8, 2023
@svekars svekars added medium docathon-h1-2023 A label for the docathon in H1 2023 labels May 31, 2023
@arunppsg
Copy link
Contributor

arunppsg commented Jun 1, 2023

If I am not wrong, the result is random because of the if-else condition in the forward method. Quoting from the tutorial,

Tracing does exactly what we said it would: run the code, record the operations that happen and construct a ScriptModule that does exactly that. Unfortunately, things like control flow are erased.

When the control flows through the else part, it prints one trace and the when the control flows through the if part, it prints another trace. Since x is random, we can't know beforehand how the control will flow in that particular trace.

@bjhargrave
Copy link
Contributor

/assigntome

bjhargrave added a commit to bjhargrave/pytorch-tutorials that referenced this issue Jun 5, 2023
We also fix the code to use the scripted_cell just created.

Fixes pytorch#1449

Signed-off-by: BJ Hargrave <hargrave@us.ibm.com>
svekars pushed a commit that referenced this issue Jun 8, 2023
We also fix the code to use the scripted_cell just created.
Fixes #1449
Signed-off-by: BJ Hargrave <hargrave@us.ibm.com>
svekars pushed a commit that referenced this issue Jun 9, 2023
We also fix the code to use the scripted_cell just created.
Fixes #1449
Signed-off-by: BJ Hargrave <hargrave@us.ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docathon-h1-2023 A label for the docathon in H1 2023 medium torchscript Issues relating to TorchScript tutorials
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants