pytorch type_error ai_generated true

RuntimeError: TorchScript type mismatch: Expected Optional[Tensor] but got Tensor for argument 'mask'

ID: pytorch/script-optional-type-error

Also available as: JSON · Markdown · 中文
90%Fix Rate
82%Confidence
1Evidence
2023-11-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
torch>=1.8 active
torch<=2.5.1 active

Root Cause

A TorchScript function expects an optional tensor argument, but a non-optional tensor is passed, causing a type mismatch during compilation or execution.

generic

中文

TorchScript 函数期望一个可选的张量参数,但传入了一个非可选张量,导致编译或执行期间类型不匹配。

Official Documentation

https://pytorch.org/docs/stable/jit.html#torch.jit.script

Workarounds

  1. 95% success Update the function signature to accept Optional[Tensor] and handle None inside. For example: def forward(self, x: torch.Tensor, mask: Optional[torch.Tensor] = None) -> torch.Tensor: if mask is not None: ...
    Update the function signature to accept Optional[Tensor] and handle None inside. For example: def forward(self, x: torch.Tensor, mask: Optional[torch.Tensor] = None) -> torch.Tensor: if mask is not None: ...
  2. 85% success Wrap the argument in a conditional that returns None if not provided: mask = mask if mask is not None else torch.empty(0) but ensure the function handles empty tensors.
    Wrap the argument in a conditional that returns None if not provided: mask = mask if mask is not None else torch.empty(0) but ensure the function handles empty tensors.

中文步骤

  1. Update the function signature to accept Optional[Tensor] and handle None inside. For example: def forward(self, x: torch.Tensor, mask: Optional[torch.Tensor] = None) -> torch.Tensor: if mask is not None: ...
  2. Wrap the argument in a conditional that returns None if not provided: mask = mask if mask is not None else torch.empty(0) but ensure the function handles empty tensors.

Dead Ends

Common approaches that don't work:

  1. Adding type hints to all variables without changing the function signature 50% fail

    Type hints alone do not change the runtime type; the function signature must explicitly declare the argument as Optional[Tensor].

  2. Using torch.jit.ignore decorator on the entire function 60% fail

    This disables TorchScript compilation entirely, which may degrade performance or break model deployment.