neuralmagic / deepsparse

Sparsity-aware deep learning inference runtime for CPUs
https://neuralmagic.com/deepsparse/
Other
2.97k stars 171 forks source link

[V2 Pipeline] Middleware manager #1471

Closed horheynm closed 8 months ago

horheynm commented 8 months ago

Description

Adds ability to run middlewares for Pipeline and Operator callables

Problem

No existing way to just get the timings of each callable, add a generic manager for now, TimerManager in a separate pr

Solution

Create middleware manager and add a chain of callables to call middlewares before calling the callables

Design

Very similar to Starlette (Fastapi) middleware. Before calling the callables for Pipeline or Operator, add a "gadget" where the gadget logic is called before and after the callable.

One note is that Pipeline and Operator do not rely on the middleware. It doesnt know it exists. It is just a helper.

Usage

Check tests,

middleware = [MiddlewareSpec(ReducerMiddleware)]
middleware_manager = MiddlewareManager(middleware)
Pipeline(...,middleware_manager=middleware_manager)

Testing

Checks the functionality of middleware and middleware managers work as expected

bfineran commented 8 months ago

also - as discussed we need to cut down on the number of times we instantiate the actual middleware callable classes