Skip to content

Commit

Permalink
[Pipeline Refactor] Migration (#1460)
Browse files Browse the repository at this point in the history
* update pathways to use new v2 pipeline

* fix image classification

* quality

* leftover fixes

* fix batch sizes:

* quality; use old tasks for server

* fix server tests

* fix captioning

* fix init for haystack

* fix import

* fix another import

* fix test

* fix init file

* fix test

* fix remaining base tests

* fix docstring

* quality

* update codegen alias to use the new registry and text generation pipeline
  • Loading branch information
dsikka committed Dec 11, 2023
1 parent e2f2305 commit 23096ef
Show file tree
Hide file tree
Showing 121 changed files with 2,573 additions and 2,410 deletions.
1 change: 0 additions & 1 deletion src/deepsparse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
from .engine import *
from .tasks import *
from .pipeline import *
from .base_pipeline import *
from .loggers import *
from .version import __version__, is_release
from .analytics import deepsparse_analytics as _analytics
Expand Down
2 changes: 1 addition & 1 deletion src/deepsparse/clip/captioning_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import torch
import torch.nn.functional as F
from deepsparse.clip import CLIPDecoderInput, CLIPTextInput, CLIPVisualInput
from deepsparse.pipeline import BasePipeline, Pipeline
from deepsparse.legacy.pipeline import BasePipeline, Pipeline


__all__ = ["CLIPCaptionInput", "CLIPCaptionOutput", "CLIPCaptionPipeline"]
Expand Down
2 changes: 1 addition & 1 deletion src/deepsparse/clip/decoder_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import numpy as np
from pydantic import BaseModel, Field

from deepsparse import Pipeline
from deepsparse.legacy import Pipeline
from deepsparse.utils import model_to_path


Expand Down
2 changes: 1 addition & 1 deletion src/deepsparse/clip/text_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import numpy as np
from pydantic import BaseModel, Field

from deepsparse.pipeline import Pipeline
from deepsparse.legacy.pipeline import Pipeline
from deepsparse.utils import model_to_path
from open_clip.tokenizer import tokenize

Expand Down
2 changes: 1 addition & 1 deletion src/deepsparse/clip/visual_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from torchvision.transforms import InterpolationMode

from deepsparse.clip.constants import CLIP_RGB_MEANS, CLIP_RGB_STDS
from deepsparse.pipeline import Pipeline
from deepsparse.legacy.pipeline import Pipeline
from deepsparse.pipelines.computer_vision import ComputerVisionSchema
from deepsparse.utils import model_to_path

Expand Down
2 changes: 1 addition & 1 deletion src/deepsparse/clip/zeroshot_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pydantic import BaseModel, Field

from deepsparse.clip import CLIPTextInput, CLIPVisualInput
from deepsparse.pipeline import BasePipeline, Pipeline
from deepsparse.legacy.pipeline import BasePipeline, Pipeline
from scipy.special import softmax


Expand Down
6 changes: 5 additions & 1 deletion src/deepsparse/evaluation/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@
)
from src.deepsparse.evaluation.results import Result, save_result
from src.deepsparse.evaluation.utils import args_to_dict, get_save_path
from src.deepsparse.pipeline import DEEPSPARSE_ENGINE, ORT_ENGINE, TORCHSCRIPT_ENGINE
from src.deepsparse.operators.engine_operator import (
DEEPSPARSE_ENGINE,
ORT_ENGINE,
TORCHSCRIPT_ENGINE,
)


_LOGGER = logging.getLogger(__name__)
Expand Down
6 changes: 5 additions & 1 deletion src/deepsparse/evaluation/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
)
from src.deepsparse.evaluation.registry import EvaluationRegistry
from src.deepsparse.evaluation.results import Result
from src.deepsparse.pipeline import DEEPSPARSE_ENGINE, ORT_ENGINE, TORCHSCRIPT_ENGINE
from src.deepsparse.operators.engine_operator import (
DEEPSPARSE_ENGINE,
ORT_ENGINE,
TORCHSCRIPT_ENGINE,
)


__all__ = ["evaluate"]
Expand Down
3 changes: 2 additions & 1 deletion src/deepsparse/evaluation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

from transformers import AutoModelForCausalLM

from deepsparse import DEEPSPARSE_ENGINE, ORT_ENGINE, Pipeline
from deepsparse import Pipeline
from deepsparse.operators.engine_operator import DEEPSPARSE_ENGINE, ORT_ENGINE


__all__ = ["text_generation_model_from_target", "get_save_path", "args_to_dict"]
Expand Down
7 changes: 5 additions & 2 deletions src/deepsparse/image_classification/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
"Please install deepsparse[image_classification] to use this pathway"
)


from .constants import *
from .pipelines import *
from .pipeline import *

# flake8: noqa
from .postprocess_operator import *
from .preprocess_operator import *
from .schemas import *


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,43 @@
# limitations under the License.

import logging
import warnings
from typing import Dict, Optional, Tuple, Union

from deepsparse.v2.image_classification.postprocess_operator import (
from deepsparse.image_classification.postprocess_operator import (
ImageClassificationPostProcess,
)
from deepsparse.v2.image_classification.preprocess_operator import (
from deepsparse.image_classification.preprocess_operator import (
ImageClassificationPreProcess,
)
from deepsparse.v2.operators.engine_operator import EngineOperator
from deepsparse.v2.pipeline import Pipeline
from deepsparse.v2.routers.router import LinearRouter
from deepsparse.v2.schedulers.scheduler import OperatorScheduler
from deepsparse.operators.engine_operator import EngineOperator
from deepsparse.operators.registry import OperatorRegistry
from deepsparse.pipeline import Pipeline
from deepsparse.routers.router import LinearRouter
from deepsparse.schedulers.scheduler import OperatorScheduler


_LOGGER = logging.getLogger(__name__)

__all__ = ["ImageClassificationPipeline"]


@OperatorRegistry.register(name="image_classification")
class ImageClassificationPipeline(Pipeline):
def __init__(
self,
model_path: str,
engine_kwargs: Optional[Dict] = None,
class_names: Union[None, str, Dict[str, str]] = None,
image_size: Optional[Tuple[int]] = None,
top_k: int = 1,
**engine_kwargs,
):

if not engine_kwargs:
engine_kwargs = {}
engine_kwargs["model_path"] = model_path
elif engine_kwargs.get("model_path") != model_path:
warnings.warn(f"Updating engine_kwargs to include {model_path}")
_LOGGER.warning(f"Updating engine_kwargs to include {model_path}")
engine_kwargs["model_path"] = model_path

engine = EngineOperator(**engine_kwargs)
preproces = ImageClassificationPreProcess(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import numpy
from pydantic import BaseModel, Field

from deepsparse.v2.operators import Operator
from deepsparse.operators import Operator


class ImageClassificationOutput(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
IMAGENET_RGB_MEANS,
IMAGENET_RGB_STDS,
)
from deepsparse.operators import Operator
from deepsparse.pipelines.computer_vision import ComputerVisionSchema
from deepsparse.v2.operators import Operator


class ImageClassificationInput(ComputerVisionSchema):
Expand Down
9 changes: 4 additions & 5 deletions src/deepsparse/image_classification/validation_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
on Imagenette [default: zoo:cv/classificati
on/resnet_v1-50/pytorch/sparseml/imagenette/
base-none]
--image-size, --image_size INTEGER
integer size to evaluate images at (will be
reshaped to square shape) [default: 224]
--batch-size, --batch_size INTEGER
Test batch size, must divide the dataset
evenly, else last batch will be dropped
[default: 1]
--image-size, --image_size INTEGER
integer size to evaluate images at (will be
reshaped to square shape) [default: 224]
--num-cores, --num_cores INTEGER
Number of CPU cores to run deepsparse with,
default is all available
Expand Down Expand Up @@ -213,11 +213,10 @@ def main(
pipeline = Pipeline.create(
task="image_classification",
model_path=model_path,
engine_type=engine,
batch_size=batch_size,
num_cores=num_cores,
engine_type=engine,
)
print(f"engine info: {pipeline.engine}")
correct = total = 0
progress_bar = tqdm(data_loader)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# flake8: noqa

# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,9 +11,9 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from .helpers import *
from .state import *
from .types import *

# flake8: noqa

from .data import * # isort:skip
from .base_pipeline import *
from .pipeline import *
from .tasks import *
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
from pydantic import BaseModel

from deepsparse import Context
from deepsparse.legacy.tasks import SupportedTasks, dynamic_import_task
from deepsparse.loggers.base_logger import BaseLogger
from deepsparse.loggers.build_logger import logger_from_config
from deepsparse.loggers.constants import validate_identifier
from deepsparse.tasks import SupportedTasks, dynamic_import_task


__all__ = [
Expand Down Expand Up @@ -166,7 +166,7 @@ def create(
implementation
:return: pipeline object initialized for the given task
"""
from deepsparse.pipeline import Bucketable, BucketingPipeline, Pipeline
from deepsparse.legacy.pipeline import Bucketable, BucketingPipeline, Pipeline

pipeline_constructor = BasePipeline._get_task_constructor(task)
model_path = kwargs.get("model_path", None)
Expand Down Expand Up @@ -278,7 +278,7 @@ def from_config(
logging. Default is None
:return: loaded Pipeline object from the config
"""
from deepsparse.pipeline import PipelineConfig
from deepsparse.legacy.pipeline import PipelineConfig

if isinstance(config, Path) or (
isinstance(config, str) and os.path.exists(config)
Expand Down Expand Up @@ -308,7 +308,7 @@ def to_config(self) -> "PipelineConfig": # noqa: F821
"""
:return: PipelineConfig that can be used to reload this object
"""
from deepsparse.pipeline import PipelineConfig
from deepsparse.legacy.pipeline import PipelineConfig

if not hasattr(self, "task"):
raise RuntimeError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@
# limitations under the License.

# flake8: noqa
from .postprocess_operator import *
from .preprocess_operator import *


from .pipeline import * # isort:skip
from .pipelines import *
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
ImageClassificationInput,
ImageClassificationOutput,
)
from deepsparse.pipeline import Pipeline
from deepsparse.legacy.pipeline import Pipeline
from deepsparse.utils import model_to_path


Expand Down
Loading

0 comments on commit 23096ef

Please sign in to comment.