|
| 1 | +# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, |
| 10 | +# software distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Any, List, Type, Union |
| 16 | + |
| 17 | +import numpy as np |
| 18 | +from pydantic import BaseModel, Field |
| 19 | + |
| 20 | +from deepsparse.pipeline import Pipeline |
| 21 | +from deepsparse.utils import model_to_path |
| 22 | +from open_clip.tokenizer import tokenize |
| 23 | + |
| 24 | + |
| 25 | +__all__ = ["CLIPTextInput", "CLIPTextOutput", "CLIPTextPipeline"] |
| 26 | + |
| 27 | + |
| 28 | +class CLIPTextInput(BaseModel): |
| 29 | + """ |
| 30 | + Input for the CLIP Text Branch |
| 31 | + """ |
| 32 | + |
| 33 | + text: Union[str, List[str]] = Field(description="List of text to process") |
| 34 | + |
| 35 | + |
| 36 | +class CLIPTextOutput(BaseModel): |
| 37 | + """ |
| 38 | + Output for the CLIP Text Branch |
| 39 | + """ |
| 40 | + |
| 41 | + text_embeddings: List[Any] = Field( |
| 42 | + description="Text embeddings for the single text or list of embeddings for " |
| 43 | + "multiple." |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +@Pipeline.register(task="clip_text", default_model_path=None) |
| 48 | +class CLIPTextPipeline(Pipeline): |
| 49 | + def __init__(self, **kwargs): |
| 50 | + super().__init__(**kwargs) |
| 51 | + |
| 52 | + self.tokenizer = tokenize |
| 53 | + |
| 54 | + @property |
| 55 | + def input_schema(self) -> Type[CLIPTextInput]: |
| 56 | + """ |
| 57 | + :return: pydantic model class that inputs to this pipeline must comply to |
| 58 | + """ |
| 59 | + return CLIPTextInput |
| 60 | + |
| 61 | + @property |
| 62 | + def output_schema(self) -> Type[CLIPTextOutput]: |
| 63 | + """ |
| 64 | + :return: pydantic model class that inputs to this pipeline must comply to |
| 65 | + """ |
| 66 | + return CLIPTextOutput |
| 67 | + |
| 68 | + def setup_onnx_file_path(self): |
| 69 | + """ |
| 70 | + Performs any setup to unwrap and process the given `model_path` and other |
| 71 | + class properties into an inference ready onnx file to be compiled by the |
| 72 | + engine of the pipeline |
| 73 | +
|
| 74 | + :return: file path to the ONNX file for the engine to compile |
| 75 | + """ |
| 76 | + return model_to_path(self.model_path) |
| 77 | + |
| 78 | + def process_inputs(self, inputs: CLIPTextInput) -> List[np.ndarray]: |
| 79 | + """ |
| 80 | + Preprocess inputs for CLIP's Trext Branch to comply with the DeepSparse Engine |
| 81 | +
|
| 82 | + :param inputs: CLITextInput |
| 83 | + :return: list of preprocessed numpy arrays |
| 84 | + """ |
| 85 | + if isinstance(inputs.text, str): |
| 86 | + inputs.text = [inputs.text] |
| 87 | + |
| 88 | + tokens = self.tokenizer(inputs.text) |
| 89 | + tokens = [np.array(t).astype(np.int32) for t in tokens] |
| 90 | + tokens = np.stack(tokens, axis=0) |
| 91 | + return [tokens] |
| 92 | + |
| 93 | + def process_engine_outputs( |
| 94 | + self, engine_outputs: List[np.array], **kwargs |
| 95 | + ) -> CLIPTextOutput: |
| 96 | + """ |
| 97 | + :param engine_outputs: list of numpy arrays that are the output of the engine |
| 98 | + forward pass |
| 99 | + :return: outputs of engine post-processed into an object in the `output_schema` |
| 100 | + format of this pipeline |
| 101 | + """ |
| 102 | + return self.output_schema(text_embeddings=engine_outputs) |
0 commit comments