|
| 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 | + |
| 16 | +from typing import List |
| 17 | + |
| 18 | +from pydantic import BaseModel, Field, PrivateAttr |
| 19 | + |
| 20 | +from deepsparse.v2.operators import Operator |
| 21 | +from deepsparse.v2.routers import Router |
| 22 | +from deepsparse.v2.schedulers import OperatorScheduler, SchedulerGroup |
| 23 | + |
| 24 | + |
| 25 | +__all__ = ["Pipeline"] |
| 26 | + |
| 27 | + |
| 28 | +class Pipeline(BaseModel): |
| 29 | + """ |
| 30 | + Pipeline accepts a series of operators, schedulers, and a router which define |
| 31 | + an end to end ML transformation. |
| 32 | +
|
| 33 | + Calling a pipeline runs these transformations |
| 34 | + """ |
| 35 | + |
| 36 | + stages: List[Operator] = Field( |
| 37 | + required=True, |
| 38 | + description="In-order list of operators that make up this pipeline", |
| 39 | + ) |
| 40 | + router: Router = Field( |
| 41 | + default_factor=Router, |
| 42 | + description="Router object to determine order and run the stages. " |
| 43 | + "Defaults to the base Router object", |
| 44 | + ) |
| 45 | + schedulers: List[OperatorScheduler] = Field( |
| 46 | + default_factor=lambda: [OperatorScheduler()], |
| 47 | + description="List of schedulers to run operators in order of priority", |
| 48 | + ) |
| 49 | + |
| 50 | + _scheduler_group: SchedulerGroup = PrivateAttr() |
| 51 | + |
| 52 | + class Config: |
| 53 | + arbitrary_types_allowed = True |
| 54 | + |
| 55 | + def __init__(self, *args, **kwargs): |
| 56 | + super().__init__(*args, **kwargs) |
| 57 | + |
| 58 | + self.validate() |
| 59 | + |
| 60 | + # SchedulerGroup handles running all schedulers in order of priority |
| 61 | + self._scheduler_group = SchedulerGroup(self.schedulers) |
| 62 | + |
| 63 | + def __call__(self, *args, return_context: bool = False, **kwargs): |
| 64 | + """ |
| 65 | + :param return_context: if True, retrns tuple of the pipelien output |
| 66 | + and entire context. Default False |
| 67 | + :return: output of the pipeline stages ran with the router for the given input |
| 68 | + """ |
| 69 | + if len(args) > 1: |
| 70 | + raise ValueError( |
| 71 | + "Only 1 in-line argument may be supplied to Pipeline which " |
| 72 | + f"must be a Schema, found: {len(args)}" |
| 73 | + ) |
| 74 | + if args and kwargs: |
| 75 | + raise ValueError( |
| 76 | + "Pipeline can only run either a single in-line argument schema or a " |
| 77 | + f"series of kwargs, found {len(args)} args and {len(kwargs)} kwargs" |
| 78 | + ) |
| 79 | + |
| 80 | + pipeline_input = args[0] or kwargs |
| 81 | + pipeline_output, context = self.router.run( |
| 82 | + inp=pipeline_input, |
| 83 | + operators=self.stages, |
| 84 | + scheduler=self._scheduler_group, |
| 85 | + ) |
| 86 | + |
| 87 | + if return_context: |
| 88 | + return pipeline_output, context |
| 89 | + |
| 90 | + return pipeline_output |
| 91 | + |
| 92 | + def validate(self): |
| 93 | + router_validation = self.router.validate(self.stages) |
| 94 | + |
| 95 | + if router_validation is False: |
| 96 | + # default error message |
| 97 | + stage_types = [type(stage) for stage in self.stages] |
| 98 | + raise ValueError( |
| 99 | + f"Invalid Router: {type(self.router)} for stages: {stage_types}" |
| 100 | + ) |
| 101 | + elif isinstance(router_validation, str): |
| 102 | + raise ValueError(f"Invalid Router for stages: {router_validation}") |
0 commit comments