[Bug][Relax][ONNX] BitShift raises AttributeError: 'str' object has no attribute 'decode' when direction attribute is omitted
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 88/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- python
調査の方向性
BitShift._impl_v11 を python/tvm/relax/frontend/onnx/onnx_frontend.py で読み、そのデフォルト処理を issue で言及されている他の文字列属性と比較してください。提供されたモデルで不具合を再現し、その後 direction を省略した BitShift ノードのインポートが成功し、左シフトに対応づけられることを確認してください。
索引モデルが issue の本文から書いたものです。
説明
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
cbb1e12feonmain) - 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 を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
apache/tvm のほかの issue
-
needs-triage type: bug
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
メンテナーはふだん 1 日以内に返信
-
[Bug] [Relax][ONNX] Resize align_corners divides by zero for a unit output extent対応中かも @GoyalIshaan が 3 日前に担当しました。 オープンneeds-triage type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
needs-triage type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
メンテナーはふだん 1 日以内に返信
-
[Bug] [Relax][ONNX] OneHot does not convert floating-point depth to int64対応中かも @tandede が 8 日前に担当しました。 オープンneeds-triage type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
needs-triage type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
databricks/databricks-sql-python#1034 ·
メンテナーはふだん 1 日以内に返信
-
premises-missing
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
WingedGuardian/GENesis-AGI#4432 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
huggingface/skills#301 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
intuitem/ciso-assistant-community#5275 ·
メンテナーはふだん 1 日以内に返信