Skip to content

Commit

Permalink
Explicitly set max sequence length for the roberta encoder, fix outpu…
Browse files Browse the repository at this point in the history
…t shape computation, and add unit test. (#2861)
  • Loading branch information
justinxzhao authored Dec 20, 2022
1 parent f651017 commit 8caf81b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion ludwig/encoders/text_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,7 @@ def __init__(
)
transformer = RobertaModel(config)
self.transformer = FreezeModule(transformer, frozen=not trainable)
self.max_sequence_length = max_sequence_length
self.reduce_output = reduce_output
if not self.reduce_output == "cls_pooled":
self.reduce_sequence = SequenceReducer(reduce_mode=reduce_output)
Expand Down Expand Up @@ -930,7 +931,7 @@ def input_shape(self) -> torch.Size:
@property
def output_shape(self) -> torch.Size:
if self.reduce_output is None:
return torch.Size([self.max_sequence_length, self.transformer.module.config.hidden_size])
return torch.Size([self.max_sequence_length - 2, self.transformer.module.config.hidden_size])
return torch.Size([self.transformer.module.config.hidden_size])

@property
Expand Down
2 changes: 1 addition & 1 deletion tests/ludwig/encoders/test_text_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_gpt_encoder(use_pretrained: bool, reduce_output: str, max_sequence_leng


@pytest.mark.parametrize("use_pretrained", [False])
@pytest.mark.parametrize("reduce_output", ["cls_pooled", "sum"])
@pytest.mark.parametrize("reduce_output", ["cls_pooled", "sum", None])
@pytest.mark.parametrize("max_sequence_length", [20])
def test_roberta_encoder(use_pretrained: bool, reduce_output: str, max_sequence_length: int):
roberta_encoder = text_encoders.RoBERTaEncoder(
Expand Down

0 comments on commit 8caf81b

Please sign in to comment.