Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doctr/models/recognition/master/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ def decode(self, encoded: torch.Tensor) -> torch.Tensor:
output = self.decoder(ys, encoded, source_mask, target_mask)
# update ys with the next token and ignore the first token (SOS)
ys[:, i + 1] = self.linear(output[:, i]).argmax(-1)
if (ys == self.vocab_size).any(dim=-1).all(): # every sequence has emitted EOS
# Stop decoding if all sequences have reached the EOS token
# NOTE: `break` isn't correctly translated to Onnx so we don't break here if we want to export
if not self.exportable and (ys == self.vocab_size).any(dim=-1).all():
break

# Shape (N, max_length, vocab_size + 1)
Expand Down
25 changes: 25 additions & 0 deletions tests/pytorch/test_models_recognition_pt.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,31 @@ def test_models_onnx_export(arch_name, input_shape):
pytest.skip(f"Output of {arch_name}:\nMax element-wise difference: {np.max(np.abs(pt_logits - ort_outs[0]))}")


@pytest.mark.parametrize("arch_name, head", [["master", "linear"], ["parseq", "head"]])
@pytest.mark.parametrize("exportable", [False, True])
Comment thread
felixdittrich92 marked this conversation as resolved.
Resolved
def test_autoregressive_decoding_early_stop(arch_name, head, exportable):
# Decoding stops once every sequence has emitted EOS, except when the model is exportable: tracing would
# freeze the number of decoding steps to the one the dummy input needed
model = recognition.__dict__[arch_name](pretrained=False, pretrained_backbone=False, exportable=exportable).eval()
input_tensor = torch.rand((2, 3, 32, 128))

def _decoder_calls(eos_bias: float) -> int:
calls = []
with torch.no_grad():
getattr(model, head).bias[model.vocab_size] = eos_bias
hook = model.decoder.register_forward_hook(lambda *_: calls.append(1))
try:
with torch.no_grad():
model(input_tensor)
finally:
hook.remove()
return len(calls)

# EOS emitted at the first step vs. never emitted
early, full = _decoder_calls(1e4), _decoder_calls(-1e4)
assert early == full if exportable else early < full


@pytest.mark.parametrize(
"arch_name",
[
Expand Down
Loading