I am trying to export the Transformer Model to Torch Script. In creating a module list from MultiHeadAttention Layer, the following error is generated. Accompanying code is also attached.
File "/anaconda3/lib/python3.7/site-packages/torch/jit/frontend.py", line 505, in build_Compare
raise NotSupportedError(err_range, "unsupported comparison operator: " + op.__name__)
torch.jit.frontend.NotSupportedError: unsupported comparison operator: In
kv_same = key.data_ptr() == value.data_ptr()
tgt_len, bsz, embed_dim = query.size()
assert embed_dim == self.embed_dim
assert list(query.size()) == [tgt_len, bsz, embed_dim]
assert key.size() == value.size()
if incremental_state is not None:
saved_state = self._get_input_buffer(incremental_state)
if 'prev_key' in saved_state:
~~~~~~~~~~~~~ <--- HERE
CODE:
__constants__ = ['attentions', 'causal', 'layers_module']
def __init__(self, <parameters>)
att_modules = []
for _ in range(num_layers):
att_modules.append(nn.MultiheadAttention(embed_dim, num_heads, dropout=dropout))
self.attentions = nn.ModuleList(att_modules)
If I create an empty ModuleList and then append the MultiHeadAttention Layer it gives no error in no script mode, but ModuleList has to be Const in Script Mode, that route is blocked as well. Error is generated in constructor itself as confirmed while debugging, not in forward method.