我找不到任何有关 jaxtyping 入门的说明或教程。我尝试了最简单的程序,但无法解析。我使用的是Python 3.11。我在 GitHub jaxtyping 项目上没有看到任何有关上限的内容(下限是 Python 3.9),而且看起来它正在积极维护(上次提交是 8 小时前)。 我错过了什么步骤?
jaxtyping==0.2.36
numpy==2.1.3
torch==2.5.1
typeguard==4.4.1
(似乎出于某种原因需要 numpy,即使我没有使用它)
from typeguard import typechecked
from jaxtyping import Float
from torch import Tensor
@typechecked
def matmul(a: Float[Tensor, "m n"], b: Float[Tensor, "n p"]) -> Float[Tensor, "m p"]:
"""
Matrix multiplication of two 2D arrays.
"""
raise NotImplementedError("This function is not implemented yet.")
(venv) dspyz@dspyz-desktop:~/helloworld$ python matmul.py
Traceback (most recent call last):
File "/home/dspyz/helloworld/matmul.py", line 6, in <module>
@typechecked
^^^^^^^^^^^
File "/home/dspyz/helloworld/venv/lib/python3.11/site-packages/typeguard/_decorators.py", line 221, in typechecked
retval = instrument(target)
^^^^^^^^^^^^^^^^^^
File "/home/dspyz/helloworld/venv/lib/python3.11/site-packages/typeguard/_decorators.py", line 72, in instrument
instrumentor.visit(module_ast)
File "/usr/lib/python3.11/ast.py", line 418, in visit
return visitor(node)
^^^^^^^^^^^^^
File "/home/dspyz/helloworld/venv/lib/python3.11/site-packages/typeguard/_transformer.py", line 598, in visit_Module
self.generic_visit(node)
File "/home/dspyz/helloworld/venv/lib/python3.11/site-packages/typeguard/_transformer.py", line 498, in generic_visit
node = super().generic_visit(node)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/ast.py", line 494, in generic_visit
value = self.visit(value)
^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/ast.py", line 418, in visit
return visitor(node)
^^^^^^^^^^^^^
File "/home/dspyz/helloworld/venv/lib/python3.11/site-packages/typeguard/_transformer.py", line 672, in visit_FunctionDef
with self._use_memo(node):
File "/usr/lib/python3.11/contextlib.py", line 137, in __enter__
return next(self.gen)
^^^^^^^^^^^^^^
File "/home/dspyz/helloworld/venv/lib/python3.11/site-packages/typeguard/_transformer.py", line 556, in _use_memo
new_memo.return_annotation = self._convert_annotation(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/dspyz/helloworld/venv/lib/python3.11/site-packages/typeguard/_transformer.py", line 582, in _convert_annotation
new_annotation = cast(expr, AnnotationTransformer(self).visit(annotation))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/dspyz/helloworld/venv/lib/python3.11/site-packages/typeguard/_transformer.py", line 355, in visit
new_node = super().visit(node)
^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/ast.py", line 418, in visit
return visitor(node)
^^^^^^^^^^^^^
File "/home/dspyz/helloworld/venv/lib/python3.11/site-packages/typeguard/_transformer.py", line 421, in visit_Subscript
[self.visit(item) for item in node.slice.elts],
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/dspyz/helloworld/venv/lib/python3.11/site-packages/typeguard/_transformer.py", line 421, in <listcomp>
[self.visit(item) for item in node.slice.elts],
^^^^^^^^^^^^^^^^
File "/home/dspyz/helloworld/venv/lib/python3.11/site-packages/typeguard/_transformer.py", line 355, in visit
new_node = super().visit(node)
^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/ast.py", line 418, in visit
return visitor(node)
^^^^^^^^^^^^^
File "/home/dspyz/helloworld/venv/lib/python3.11/site-packages/typeguard/_transformer.py", line 474, in visit_Constant
expression = ast.parse(node.value, mode="eval")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/ast.py", line 50, in parse
return compile(source, filename, mode, flags,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<unknown>", line 1
m p
^
SyntaxError: invalid syntax
您遇到了此处报告的问题:https://github.com/patrick-kidger/jaxtyping/issues/80
您可以通过安装 typeguard 3.0 版来解决这个问题,但考虑到这个错误已经存在了多久而没有任何真正的修复,我怀疑最好的结论是
jaxtyping
不再与 typeguard
兼容。