1. Common errors
1.1 The variable changes from tensor type to int type
错误描述: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can’t record the data flow of Python values。
错误代码:
```python
x0 = torch.randn(1, 3, 640, 480)
h, w = x0.size()[-2:] #{tensor:()} tenrsor(640)
paddingBottom = int(np.ceil(h/64)*64-h) #{int} 0
paddingRight = int(np.ceil(w/64)*64-w) # {int} 32
x0 = nn.ReplicationPad2d((0, paddingRight, 0, paddingBottom))(x0) ##{tensor:(tenrsor(1),tenrsor(3),tenrsor(640),tenrsor(512))}
正确代码
x0 = torch.randn(1, 3, 640, 480)
h, w = x0.size()[-2:]
paddingBottom = np.ceil(h/64)*64-h # 修改第一处:去掉int,保持tensor类型
paddingRight = np.ceil(w/64)*64-w # 修改第二处:去掉int,保持tensor类型
x0 = nn.ReplicationPad2d((0, paddingRight.numel(), 0, paddingBottom.numel()))(x0) # 修改第三处:增加.numel()
Reference solution: torch.jit.trace Eliminate TracerWarning [1] as in the comment area, keep the variable as tensor type, and obtain the int value through .numel().
1.2 警告:floordiv is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the ‘trunc’ function NOT ‘floor’).
From module: einops/einops.py
inferred_length: int = length // known_product #报错代码
# 修改为:
inferred_length: int = torch.div(length, known_product, rounding_mode='floor') # 修改:引入 torch.div实现
Reference solutions: Document [2] and Document [3]
1.3 einops.Rearrange replaced by torch.transpose module
采用einops.Rearrange报错:
TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can’t record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
known: Set[str] = {axis for axis in composite_axis if axis_name2known_length[axis] != _unknown_axis_length}
from einops.layers.torch import Rearrange
trans_x = Rearrange('b h w c -> b c h w')(trans_x) #原始代码
----------------------修改为--------------------
trans_x = trans_x.transpose(3, 2).transpose(2, 1) #修改代码,运行速度上有轻微的可忽略不计的提升
from einops import rearrange
attn_mask0 = rearrange(attn_mask, 'w1 w2 p1 p2 p3 p4 -> 1 1 (w1 w2) (p1 p2) (p3 p4)') #原始代码
----------------------修改为--------------------
w1, w2, p1, p2, p3, p4 = attn_mask.size() #修改代码
attn_mask = attn_mask.reshape(1, 1, w1 * w2, p1 * p2, p3 * p4) #修改代码
references
[1] torch.jit.trace eliminates TracerWarning, https://blog.csdn.net/WANGWUSHAN/article/details/118052523
[2] Problems that occur when using python (seeking solutions) https://zhuanlan.zhihu.com /p/562922076
[3] UserWarning: floordiv is deprecated, and its behavior will change in a future version of pytorch. https://blog.csdn.net/weixin_43564920/article/details/127004030