minor fixes for torch.jit.script

This commit is contained in:
jinzr 2023-10-23 20:32:40 +08:00
parent 902dc2364a
commit eb67938c8c
3 changed files with 12 additions and 1 deletions

View File

@ -74,6 +74,10 @@ class Decoder(nn.Module):
groups=embedding_dim, groups=embedding_dim,
bias=False, bias=False,
) )
else:
# To avoid `RuntimeError: Module 'Decoder' has no attribute 'conv'`
# when inference with torch.jit.script and context_size == 1
self.conv = nn.Identity()
self.output_linear = nn.Linear(embedding_dim, vocab_size) self.output_linear = nn.Linear(embedding_dim, vocab_size)
def forward(self, y: torch.Tensor, need_pad: bool = True) -> torch.Tensor: def forward(self, y: torch.Tensor, need_pad: bool = True) -> torch.Tensor:

View File

@ -71,6 +71,10 @@ class Decoder(nn.Module):
groups=embedding_dim, groups=embedding_dim,
bias=False, bias=False,
) )
else:
# To avoid `RuntimeError: Module 'Decoder' has no attribute 'conv'`
# when inference with torch.jit.script and context_size == 1
self.conv = nn.Identity()
def forward(self, y: torch.Tensor, need_pad: bool = True) -> torch.Tensor: def forward(self, y: torch.Tensor, need_pad: bool = True) -> torch.Tensor:
""" """

View File

@ -17,7 +17,6 @@
import torch import torch
import torch.nn as nn import torch.nn as nn
import torch.nn.functional as F import torch.nn.functional as F
from scaling import Balancer from scaling import Balancer
@ -95,6 +94,10 @@ class Decoder(nn.Module):
max_abs=1.0, max_abs=1.0,
prob=0.05, prob=0.05,
) )
else:
# To avoid `RuntimeError: Module 'Decoder' has no attribute 'conv'`
# when inference with torch.jit.script and context_size == 1
self.conv = nn.Identity()
def forward(self, y: torch.Tensor, need_pad: bool = True) -> torch.Tensor: def forward(self, y: torch.Tensor, need_pad: bool = True) -> torch.Tensor:
""" """