Skip to content

TextSimilarityTask support onnx #5841

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

Merged
merged 2 commits into from
May 10, 2023
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
23 changes: 17 additions & 6 deletions paddlenlp/taskflow/text_similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class TextSimilarityTask(Task):
def __init__(self, task, model, batch_size=1, max_length=384, **kwargs):
super().__init__(task=task, model=model, **kwargs)
self._static_mode = True
self._check_predictor_type()
if not self.from_hf_hub:
self._check_task_files()
if self._static_mode:
Expand Down Expand Up @@ -273,12 +274,22 @@ def _run_model(self, inputs):
if "rocketqa" in self.model_name or "ernie-search" in self.model_name:
with static_mode_guard():
for batch in inputs["data_loader"]:
input_ids, segment_ids = self._batchify_fn(batch)
self.input_handles[0].copy_from_cpu(input_ids)
self.input_handles[1].copy_from_cpu(segment_ids)
self.predictor.run()
scores = self.output_handle[0].copy_to_cpu().tolist()
results.extend(scores)

if self._predictor_type == "paddle-inference":
input_ids, segment_ids = self._batchify_fn(batch)
self.input_handles[0].copy_from_cpu(input_ids)
self.input_handles[1].copy_from_cpu(segment_ids)
self.predictor.run()
scores = self.output_handle[0].copy_to_cpu().tolist()
results.extend(scores)
else:
# onnx mode
input_dict = {}
input_ids, segment_ids = self._batchify_fn(batch)
input_dict["input_ids"] = input_ids
input_dict["token_type_ids"] = segment_ids
scores = self.predictor.run(None, input_dict)[0].tolist()
results.extend(scores)
else:
with static_mode_guard():
for batch in inputs["data_loader"]:
Expand Down