|
15 | 15 | import os
|
16 | 16 | from typing import Any, Dict, List, Optional, Tuple, Union
|
17 | 17 |
|
18 |
| - |
19 |
| -try: |
20 |
| - from transformers import AutoModelForCausalLM, PreTrainedModel |
21 |
| - |
22 |
| - transformers_error = None |
23 |
| -except ImportError as import_error: |
24 |
| - transformers_error = import_error |
25 |
| - |
26 |
| - |
27 | 18 | from deepsparse import Pipeline
|
28 |
| -from deepsparse.operators.engine_operator import DEEPSPARSE_ENGINE, ORT_ENGINE |
29 | 19 |
|
30 | 20 |
|
31 | 21 | __all__ = [
|
32 |
| - "create_model_from_target", |
| 22 | + "create_pipeline", |
33 | 23 | "get_save_path",
|
34 | 24 | "args_to_dict",
|
35 | 25 | "resolve_integration",
|
@@ -57,36 +47,36 @@ def potentially_check_dependency_import(integration_name: str) -> bool:
|
57 | 47 |
|
58 | 48 |
|
59 | 49 | def resolve_integration(
|
60 |
| - model: Union[Pipeline, "PreTrainedModel"], datasets: Union[str, List[str]] |
| 50 | + pipeline: Pipeline, datasets: Union[str, List[str]] |
61 | 51 | ) -> Union[str, None]:
|
62 | 52 | """
|
63 |
| - Given a model and dataset, infer the name of the evaluation integration |
| 53 | + Given a pipeline and dataset, infer the name of the evaluation integration |
64 | 54 | to use. If unable to infer a name, return None.
|
65 | 55 |
|
66 | 56 | Currently:
|
67 | 57 | if the model is a generative language model,
|
68 | 58 | default to 'lm-evaluation-harness' otherwise return None
|
69 | 59 |
|
70 |
| - :param model: The model to infer the integration for |
| 60 | + :param pipeline: The pipeline to infer the integration for |
71 | 61 | :param datasets: The datasets to infer the integration for
|
72 | 62 | :return: The name of the integration to use or None if unable to infer
|
73 | 63 | """
|
74 |
| - if if_generative_language_model(model): |
| 64 | + if if_generative_language_model(pipeline): |
75 | 65 | return LM_EVALUATION_HARNESS
|
76 | 66 | return None
|
77 | 67 |
|
78 | 68 |
|
79 |
| -def if_generative_language_model(model: Any) -> bool: |
| 69 | +def if_generative_language_model(pipeline: Pipeline) -> bool: |
80 | 70 | """
|
81 | 71 | Checks if the model is a generative language model.
|
82 | 72 | """
|
83 |
| - _check_transformers_dependency() |
84 |
| - if isinstance(model, Pipeline): |
85 |
| - return model.__class__.__name__ == "TextGenerationPipeline" |
86 |
| - elif isinstance(model, PreTrainedModel): |
87 |
| - return "CausalLM" in model.__class__.__name__ |
88 |
| - else: |
89 |
| - return False |
| 73 | + pipeline_name = pipeline.__class__.__name__ |
| 74 | + if pipeline_name == "TextGenerationPipeline" or ( |
| 75 | + pipeline_name == "TextGenerationPipelineNoKVCache" |
| 76 | + ): |
| 77 | + return True |
| 78 | + |
| 79 | + return False |
90 | 80 |
|
91 | 81 |
|
92 | 82 | def args_to_dict(args: Tuple[Any, ...]) -> Dict[str, Any]:
|
@@ -134,43 +124,30 @@ def get_save_path(
|
134 | 124 | return os.path.join(base_path, file_name)
|
135 | 125 |
|
136 | 126 |
|
137 |
| -def create_model_from_target( |
138 |
| - target: str, |
| 127 | +def create_pipeline( |
| 128 | + model_path: str, |
139 | 129 | engine_type: Optional[str] = None,
|
140 | 130 | **kwargs,
|
141 |
| -) -> Union[Pipeline, "AutoModelForCausalLM"]: |
| 131 | +) -> Pipeline: |
142 | 132 | """
|
143 |
| - Create a model or a pipeline from a target path. |
| 133 | + Create a pipeline for evaluation |
144 | 134 |
|
145 |
| - Note: This function is currently limited to: |
146 |
| - - creating pipelines of type 'text-generation' |
147 |
| - - creating dense huggingface models of type 'AutoModelForCausalLM' |
148 |
| - This function will be expanded in the future to support more |
149 |
| - model types and frameworks. |
| 135 | + Note: This function is currently primarily |
| 136 | + focused on creating pipelines of type 'text-generation' |
| 137 | + This function will be expanded in the future to support |
| 138 | + more tasks and models |
150 | 139 |
|
151 |
| - :param target: The target path to initialize the |
| 140 | + :param model_path: The target path to initialize the |
152 | 141 | text generation model from. This can be a local
|
153 | 142 | or remote path to the model or a sparsezoo stub
|
154 | 143 | :param engine_type: The engine type to initialize the model with.
|
155 |
| - :return: The initialized model |
| 144 | + :return: The initialized pipeline |
156 | 145 | """
|
157 |
| - _check_transformers_dependency() |
158 |
| - |
159 |
| - if engine_type in [DEEPSPARSE_ENGINE, ORT_ENGINE]: |
160 |
| - return Pipeline.create( |
161 |
| - task="text-generation", |
162 |
| - model_path=target, |
163 |
| - sequence_length=kwargs.pop("sequence_length", 2048), |
164 |
| - engine_type=engine_type, |
165 |
| - batch_size=kwargs.pop("batch_size", 1), |
166 |
| - **kwargs, |
167 |
| - ) |
168 |
| - else: |
169 |
| - return AutoModelForCausalLM.from_pretrained(target, **kwargs) |
170 |
| - |
171 |
| - |
172 |
| -def _check_transformers_dependency(): |
173 |
| - if transformers_error: |
174 |
| - raise ImportError( |
175 |
| - "transformers is needed to use this module" |
176 |
| - ) from transformers_error |
| 146 | + return Pipeline.create( |
| 147 | + task=kwargs.pop("task", "text-generation"), |
| 148 | + model_path=model_path, |
| 149 | + sequence_length=kwargs.pop("sequence_length", 2048), |
| 150 | + engine_type=engine_type, |
| 151 | + batch_size=kwargs.pop("batch_size", 1), |
| 152 | + **kwargs, |
| 153 | + ) |
0 commit comments