Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize RelationClassifier by adding the option to filter long sentences and truncate context #3593

Open
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

alanakbik
Copy link
Collaborator

The RelationClassifier separately considers each pair of entities in a Sentence for relation classification. If a sentence is long and contains many entities, this leads to a very large number of forward passes.

This PR introduces two new parameters to RelationClassifier to optimize this behavior:

  • max_allowed_tokens_between_entities allows users to specify the maximum allowed distance between two entities in order to be considered a relation candidate. The idea is that entities that lie far apart in a text are unlikely to be in relation. All entity pairs with too many tokens in between are filtered.
  • max_surrounding_context_length allows users to specify how much additional context (text around the two entities) is used to make the relation classification. Setting this to a low value makes classification more computationally efficient, since smaller encoded sentences are used in the forward pass.

Further, the PR introduces a new sanity check to ensure that one entity in a pair does not contain the other.

To enable this, the _encode_sentence function is changed to possibly return None if one of the criteria is not met. The _encode_sentence_for_inference and _encode_sentence_for_training methods are accordingly changed to check for None values before the yield.

@alanakbik alanakbik requested a review from dobbersc January 2, 2025 21:43
Comment on lines 259 to 260
max_allowed_tokens_between_entities: int = 20,
max_surrounding_context_length: int = 10,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if would make sense to also allow for None values to disable the measures. ̀This could then also be used as default value for _init_model_with_state_dict to not change the behaviour of existing models.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

Comment on lines 771 to 772
max_allowed_tokens_between_entities=state.get("max_allowed_tokens_between_entities", 25),
max_surrounding_context_length=state.get("max_surrounding_context_length", 50),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default parameters for backwards compatibility are different to the ones in the ̀ init` method. Are these a better fit?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

backwards compatibility is tricky, since older models will have no limitations on max allowed tokens or surrounding context. It's probably best to set really high numbers here (e.g., even higher).

Comment on lines 438 to 439
head_idx = -10000
tail_idx = 10000
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For safety and as a sanity check, we could also initialize these values as None and have an assertion after the for token in original_sentence loop that these variables should not be None.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had that before, but that gave me mypy errors. But I agree that this is better.

@dobbersc
Copy link
Collaborator

I've incorporated the suggestions and also added a test case for the new parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants