forked from deepjavalibrary/djl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tokenizers] Support import zero-shot-classification to model zoo
- Loading branch information
Showing
4 changed files
with
69 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
extensions/tokenizers/src/main/python/djl_converter/zero_shot_classification_converter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env python | ||
# | ||
# Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file | ||
# except in compliance with the License. A copy of the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" | ||
# BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the License for | ||
# the specific language governing permissions and limitations under the License. | ||
import math | ||
|
||
from djl_converter.huggingface_converter import HuggingfaceConverter | ||
|
||
|
||
class ZeroShotClassificationConverter(HuggingfaceConverter): | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.task = "zero-shot-classification" | ||
self.application = "nlp/zero_shot_classification" | ||
self.translator = "ai.djl.huggingface.translator.ZeroShotClassificationTranslatorFactory" | ||
self.inputs = "one day I will see the world" | ||
self.labels = ['travel'] | ||
|
||
def encode_inputs(self, tokenizer): | ||
return tokenizer(self.inputs, | ||
f"This example is {self.labels[0]}.", | ||
return_tensors='pt') | ||
|
||
def verify_jit_output(self, hf_pipeline, encoding, out): | ||
logits = out['logits'] | ||
entail_contradiction_logits = logits[:, [0, 2]] | ||
probs = entail_contradiction_logits.softmax(dim=1) | ||
score = probs[:, 1].item() | ||
|
||
pipeline_output = hf_pipeline(self.inputs, self.labels) | ||
expected = pipeline_output["scores"][0] | ||
|
||
if math.isclose(expected, score, abs_tol=1e-3): | ||
return True, None | ||
|
||
return False, f"Unexpected inference result" | ||
|
||
def get_extra_arguments(self, hf_pipeline, model_id: str, | ||
temp_dir: str) -> dict: | ||
return { | ||
"padding": "true", | ||
"truncation": "only_first", | ||
} |