Pytorch Tensorboard图形可视化错误(TracingCheckError)

问题描述 投票:0回答:1

我遇到以下代码错误。我想做的事情相当简单:我想使用 TensorBoard 可视化 PyTorch 模型。我完全不知道为什么会发生这个错误。

import torch
from torch import nn
class MyModel(nn.Module):
  def __init__(self):
    super().__init__()
    self.mul = nn.MultiheadAttention(128, 4, batch_first=True)

  def forward(self, x):
    y, _ = self.mul(x, x, x)
    return y


x = torch.randn(1, 240 * 3, 128)
my = MyModel()

from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("./tensorboard_otameshi")
writer.add_graph(my, x)
writer.close()

错误是这样的。

TracingCheckError                         Traceback (most recent call last)
<ipython-input-1-c4876e5ab470> in <cell line: 18>()
     16 from torch.utils.tensorboard import SummaryWriter
     17 writer = SummaryWriter("./tensorboard_otameshi")
---> 18 writer.add_graph(my, x)
     19 writer.close()

5 frames
/usr/local/lib/python3.10/dist-packages/torch/jit/_trace.py in _check_trace(check_inputs, func, traced_func, check_tolerance, strict, force_outplace, is_trace_module, _module_class, example_inputs_is_kwarg)
    560         diag_info = graph_diagnostic_info()
    561         if any(info is not None for info in diag_info):
--> 562             raise TracingCheckError(*diag_info)
    563 
    564 

TracingCheckError: Tracing failed sanity checks!
ERROR: Graphs differed across invocations!
    Graph diff:
          graph(%self.1 : __torch__.MyModel,
                %x : Tensor):
            %mul : __torch__.torch.nn.modules.activation.MultiheadAttention = prim::GetAttr[name="mul"](%self.1)
        +   %4 : bool = prim::Constant[value=1](), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/modules/activation.py:1160:0
        -   %4 : NoneType = prim::Constant(), scope: __module.mul
        ?    ^
        +   %5 : NoneType = prim::Constant(), scope: __module.mul
        ?    ^
        -   %5 : Tensor = prim::Constant[value={5.65685}](), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5331:0
        -   %6 : int = prim::Constant[value=-2](), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:4767:0
        -   %7 : int = prim::Constant[value=3](), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/_tensor.py:1189:0
        -   %8 : int = prim::Constant[value=-1](), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:4761:0
        -   %9 : str = prim::Constant[value="trunc"](), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5208:0
        -   %10 : Tensor = prim::Constant[value={4}](), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5208:0
        -   %11 : int = prim::Constant[value=2](), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5164:0
        -   %12 : int = prim::Constant[value=0](), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/modules/activation.py:1183:0
        ?    ^^                              ^                                                                                                    ^^
        +   %6 : int = prim::Constant[value=4](), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/modules/activation.py:1160:0
        ?    ^                              ^                                                                                                    ^^
        -   %13 : int = prim::Constant[value=1](), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/modules/activation.py:1183:0
        ?    ^^                                                                                                                                   ^^
        +   %7 : int = prim::Constant[value=128](), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/modules/activation.py:1160:0
        ?    ^                               ++                                                                                                    ^^
            %out_proj : __torch__.torch.nn.modules.linear.NonDynamicallyQuantizableLinear = prim::GetAttr[name="out_proj"](%mul)
            %bias : Tensor = prim::GetAttr[name="bias"](%out_proj)
            %out_proj.1 : __torch__.torch.nn.modules.linear.NonDynamicallyQuantizableLinear = prim::GetAttr[name="out_proj"](%mul)
            %weight : Tensor = prim::GetAttr[name="weight"](%out_proj.1)
            %in_proj_bias : Tensor = prim::GetAttr[name="in_proj_bias"](%mul)
            %in_proj_weight : Tensor = prim::GetAttr[name="in_proj_weight"](%mul)
        +   %14 : Tensor, %15 : Tensor = aten::_native_multi_head_attention(%x, %x, %x, %7, %6, %in_proj_weight, %in_proj_bias, %weight, %bias, %5, %4, %4, %5), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/modules/activation.py:1160:0
        -   %query : Tensor = aten::transpose(%x, %13, %12), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/modules/activation.py:1183:0
        -   %21 : int = aten::size(%query, %12), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5164:0
        -   %tgt_len : Tensor = prim::NumToTensor(%21), scope: __module.mul
        -   %23 : int = aten::size(%query, %13), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5164:0
        -   %bsz : Tensor = prim::NumToTensor(%23), scope: __module.mul
        -   %25 : int = aten::size(%query, %11), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5164:0
        -   %embed_dim : Tensor = prim::NumToTensor(%25), scope: __module.mul
        -   %head_dim : Tensor = aten::div(%embed_dim, %10, %9), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5208:0
        -   %28 : int = aten::Int(%head_dim), scope: __module.mul
        -   %29 : int = aten::Int(%head_dim), scope: __module.mul
        -   %30 : int = aten::Int(%head_dim), scope: __module.mul
        -   %31 : int = aten::size(%query, %8), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:4761:0
        -   %32 : Tensor = aten::linear(%query, %in_proj_weight, %in_proj_bias), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:4765:0
        -   %33 : int[] = prim::ListConstruct(%7, %31), scope: __module.mul
        -   %34 : Tensor = aten::unflatten(%32, %8, %33), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/_tensor.py:1189:0
        -   %35 : Tensor = aten::unsqueeze(%34, %12), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:4767:0
        -   %36 : Tensor = aten::transpose(%35, %12, %6), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:4767:0
        -   %37 : Tensor = aten::squeeze(%36, %6), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:4767:0
        -   %proj : Tensor = aten::contiguous(%37, %12), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:4767:0
        -   %q.1 : Tensor = aten::select(%proj, %12, %12), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:4768:0
        -   %k.1 : Tensor = aten::select(%proj, %12, %13), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:4768:0
        -   %v.1 : Tensor = aten::select(%proj, %12, %11), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:4768:0
        -   %42 : Tensor = aten::mul(%bsz, %10), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5277:0
        -   %43 : int = aten::Int(%42), scope: __module.mul
        -   %44 : int[] = prim::ListConstruct(%21, %43, %30), scope: __module.mul
        -   %45 : Tensor = aten::view(%q.1, %44), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5277:0
        -   %q : Tensor = aten::transpose(%45, %12, %13), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5277:0
        -   %47 : int = aten::size(%k.1, %12), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5279:0
        -   %48 : Tensor = aten::mul(%bsz, %10), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5279:0
        -   %49 : int = aten::Int(%48), scope: __module.mul
        -   %50 : int[] = prim::ListConstruct(%47, %49, %29), scope: __module.mul
        -   %51 : Tensor = aten::view(%k.1, %50), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5279:0
        -   %k : Tensor = aten::transpose(%51, %12, %13), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5279:0
        -   %53 : int = aten::size(%v.1, %12), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5288:0
        -   %54 : Tensor = aten::mul(%bsz, %10), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5288:0
        -   %55 : int = aten::Int(%54), scope: __module.mul
        -   %56 : int[] = prim::ListConstruct(%53, %55, %28), scope: __module.mul
        -   %57 : Tensor = aten::view(%v.1, %56), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5288:0
        -   %v : Tensor = aten::transpose(%57, %12, %13), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5288:0
        -   %q_scaled : Tensor = aten::div(%q, %5), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5331:0
        -   %60 : Tensor = aten::transpose(%k, %6, %8), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5338:0
        -   %input : Tensor = aten::bmm(%q_scaled, %60), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5338:0
        -   %attn_output_weights.1 : Tensor = aten::softmax(%input, %8, %4), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:1843:0
        -   %attn_output.1 : Tensor = aten::bmm(%attn_output_weights.1, %v), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5343:0
        -   %64 : Tensor = aten::transpose(%attn_output.1, %12, %13), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5345:0
        -   %65 : Tensor = aten::contiguous(%64, %12), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5345:0
        -   %66 : Tensor = aten::mul(%tgt_len, %bsz), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5345:0
        -   %67 : int = aten::Int(%66), scope: __module.mul
        -   %68 : int[] = prim::ListConstruct(%67, %25), scope: __module.mul
        -   %attn_output.3 : Tensor = aten::view(%65, %68), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5345:0
        -   %attn_output.5 : Tensor = aten::linear(%attn_output.3, %weight, %bias), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5346:0
        -   %71 : int = aten::size(%attn_output.5, %13), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5347:0
        -   %72 : int[] = prim::ListConstruct(%21, %23, %71), scope: __module.mul
        -   %attn_output : Tensor = aten::view(%attn_output.5, %72), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/functional.py:5347:0
        -   %74 : Tensor = aten::transpose(%attn_output, %13, %12), scope: __module.mul # /usr/local/lib/python3.10/dist-packages/torch/nn/modules/activation.py:1217:0
        -   return (%74)
        ?            ^
        +   return (%14)
        ?            ^
    First diverging operator:
    Node diff:
        - %mul : __torch__.torch.nn.modules.activation.MultiheadAttention = prim::GetAttr[name="mul"](%self.1)
        + %mul : __torch__.torch.nn.modules.activation.___torch_mangle_1.MultiheadAttention = prim::GetAttr[name="mul"](%self.1)
        ?                                              ++++++++++++++++++

我能做什么?

看起来像是jit的错误。但我对此一无所知。

pytorch tensorboard
1个回答
0
投票

有人能解决这个问题吗?我也遇到过类似的事情,但是不知道怎么解决?有没有大佬给我出个主意?

© www.soinside.com 2019 - 2024. All rights reserved.