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

Improve AllenNLPProcessor's performance #7

Merged
merged 1 commit into from
Apr 5, 2021
Merged
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
35 changes: 19 additions & 16 deletions forte_wrapper/allennlp/allennlp_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ def initialize(self, resources: Resources, configs: Config):
"that the entries of the same type as produced by "
"this processor will be overwritten if found.")
if configs.allow_parallel_entries:
logger.warning('Both `overwrite_entries` (whether to overwrite '
'the entries of the same type as produced by '
logger.warning('Both `overwrite_entries` (whether to overwrite'
' the entries of the same type as produced by '
'this processor) and '
'`allow_parallel_entries` (whether to allow '
'similar new entries when they already exist) '
'are True, all existing conflicting entries '
'will be deleted.')
else:
if not configs.allow_parallel_entries:
logger.warning('Both `overwrite_entries` (whether to overwrite '
'the entries of the same type as produced by '
logger.warning('Both `overwrite_entries` (whether to overwrite'
' the entries of the same type as produced by '
'this processor) and '
'`allow_parallel_entries` (whether to allow '
'similar new entries when they already exist) '
Expand Down Expand Up @@ -133,19 +133,22 @@ def default_configs(cls):
def _process(self, input_pack: DataPack):
# handle existing entries
self._process_existing_entries(input_pack)

for sentence in input_pack.get(Sentence):
result: Dict[str, List[str]] = {}
for key in self.predictor:
predicted_result = self.predictor[key].predict( # type: ignore
sentence=sentence.text)
sentences = [_ for _ in input_pack.get(Sentence)]
inputs = [{"sentence": s.text} for s in sentences]
results = {k: p.predict_batch_json(inputs)
for k, p in self.predictor.items()}
for i in range(len(sentences)):
result = {}
for key in self.predictor.keys():
if key == 'srl':
predicted_result = parse_allennlp_srl_results(
predicted_result['verbs'])
result.update(predicted_result)
result.update(
parse_allennlp_srl_results(results[key][i]["verbs"])
)
else:
result.update(results[key][i])
if "tokenize" in self.configs.processors:
# creating new tokens and dependencies
tokens = self._create_tokens(input_pack, sentence, result)
tokens = self._create_tokens(input_pack, sentences[i], result)
if "depparse" in self.configs.processors:
self._create_dependencies(input_pack, tokens, result)
if 'srl' in self.configs.processors:
Expand All @@ -159,8 +162,8 @@ def _process_existing_entries(self, input_pack):
if not self.configs.overwrite_entries:
if not self.configs.allow_parallel_entries:
raise ProcessorConfigError(
"Found existing entries, either `overwrite_entries` or "
"`allow_parallel_entries` should be True")
"Found existing entries, either `overwrite_entries` or"
" `allow_parallel_entries` should be True")
else:
# delete existing tokens and dependencies
for entry_type in (Token, Dependency):
Expand Down