[Bug][Relax][ONNX] BitShift raises AttributeError: 'str' object has no attribute 'decode' when direction attribute is omitted

オープン 初心者向け
#20,950 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@Chaitanya140904 がすでに取り組んでいます。

2026年11月10日 から。

  • #20963 @Chaitanya140904 による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
88/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python

調査の方向性

BitShift._impl_v11 を python/tvm/relax/frontend/onnx/onnx_frontend.py で読み、そのデフォルト処理を issue で言及されている他の文字列属性と比較してください。提供されたモデルで不具合を再現し、その後 direction を省略した BitShift ノードのインポートが成功し、左シフトに対応づけられることを確認してください。

索引モデルが issue の本文から書いたものです。

説明

needs-triage type: bug
Expected behavior

According to the ONNX specification for BitShift (opset 11+), when the direction attribute is omitted, it should default to "LEFT" (or b"LEFT"), converting the node to a relax.op.left_shift operation without crashing.

Actual behavior

When importing an ONNX model with a BitShift node that does not have an explicit direction attribute, from_onnx raises an AttributeError:

Error converting operator BitShift, with inputs: [x, y]
Traceback (most recent call last):
  File "repro.py", line 15, in <module>
    mod = from_onnx(model)
  File "python/tvm/relax/frontend/onnx/onnx_frontend.py", line 6864, in from_onnx
    return g.from_onnx(graph, opset)
  File "python/tvm/relax/frontend/onnx/onnx_frontend.py", line 6406, in from_onnx
    self._construct_nodes(graph)
  File "python/tvm/relax/frontend/onnx/onnx_frontend.py", line 6606, in _construct_nodes
    op = self._convert_operator(op_name, inputs, attr, self.opset)
  File "python/tvm/relax/frontend/onnx/onnx_frontend.py", line 6703, in _convert_operator
    sym = op_function(self.bb, inputs, attrs, [self._nodes, self._params])
  File "python/tvm/relax/frontend/onnx/onnx_frontend.py", line 917, in _impl_v11
    direction = attr.get("direction", "LEFT").decode("ascii")
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'decode'. Did you mean: 'encode'?
Environment
  • TVM Version: 0.28.dev0 (commit cbb1e12fe on main)
  • ONNX Version: 1.23.2
  • Python Version: 3.12.2
  • Operating System: Linux x86_64
Steps to reproduce
import onnx
from onnx import helper, TensorProto
from tvm.relax.frontend.onnx import from_onnx

# BitShift node with omitted direction attribute
node = helper.make_node("BitShift", inputs=["x", "y"], outputs=["z"])
graph = helper.make_graph(
    [node],
    "bitshift_graph",
    [
        helper.make_tensor_value_info("x", TensorProto.UINT32, [2, 2]),
        helper.make_tensor_value_info("y", TensorProto.UINT32, [2, 2]),
    ],
    [helper.make_tensor_value_info("z", TensorProto.UINT32, [2, 2])],
)
model = helper.make_model(graph, opset_imports=[helper.make_opsetid("", 11)])
mod = from_onnx(model)
Root Cause Analysis

In python/tvm/relax/frontend/onnx/onnx_frontend.py:

@classmethod
def _impl_v11(cls, bb, inputs, attr, params):
    direction = attr.get("direction", "LEFT").decode("ascii")

In ONNX protobuf, string attributes are delivered as bytes. Throughout onnx_frontend.py (e.g., lines 2084, 3344, 3888, 4440, 5537), default values for string attributes are always passed as byte literals (e.g. attr.get("mode", b"constant").decode("utf-8")).

In BitShift, however, the fallback value was supplied as a Python string "LEFT". When direction is not in attr, attr.get("direction", "LEFT") returns "LEFT", and calling "LEFT".decode("ascii") fails with AttributeError because strings in Python 3 cannot be decoded.

Proposed Fix

Change the fallback default from "LEFT" to b"LEFT":

diff --git a/python/tvm/relax/frontend/onnx/onnx_frontend.py b/python/tvm/relax/frontend/onnx/onnx_frontend.py
--- a/python/tvm/relax/frontend/onnx/onnx_frontend.py
+++ b/python/tvm/relax/frontend/onnx/onnx_frontend.py
@@ -914,7 +914,7 @@ class BitShift(BitwiseBase):
 
     @classmethod
     def _impl_v11(cls, bb, inputs, attr, params):
-        direction = attr.get("direction", "LEFT").decode("ascii")
+        direction = attr.get("direction", b"LEFT").decode("ascii")
         if direction == "LEFT":
             cls.numpy_op = _np.left_shift
             cls.relax_op = relax.op.left_shift
Triage
  • needs-triage
  • relax:frontend
主要言語
Python
スター
13.9k
フォーク
4k
平均マージ
8時間 29分
マージ済み PR(30日)
103

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

apache/tvm のほかの issue

apache/tvm の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。