Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add batched_run to Engine #1100

Merged
merged 12 commits into from
Jul 11, 2023
Merged

Add batched_run to Engine #1100

merged 12 commits into from
Jul 11, 2023

Conversation

mgoin
Copy link
Member

@mgoin mgoin commented Jun 29, 2023

Refactors Pipeline.split_engine_inputs and Pipeline.join_engine_outputs into standalone functions in utils/data.py so that Engine can also use them as batch_run (as an alternative to run)

>>> import deepsparse
>>> engine = deepsparse.Engine("zoo:cv/classification/mobilenet_v1-1.0/pytorch/sparseml/imagenet/base-none")
>>> import numpy as np
>>> inp = [np.zeros((4,3,224,224), dtype=np.float32)]
>>> inp[0].shape
(4, 3, 224, 224)

>>> engine.run(inp)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/nm/drive1/home/mgoin/code/deepsparse/src/deepsparse/engine.py", line 503, in run
    self._validate_inputs(inp)
  File "/nm/drive1/home/mgoin/code/deepsparse/src/deepsparse/engine.py", line 688, in _validate_inputs
    raise ValueError(
ValueError: array batch size of 4 must match the batch size the model was instantiated with 1

>>> engine.batched_run(inp)
2023-07-11 15:40:43 deepsparse.engine WARNING  Using batched_run with an Engine of batch_size=1 isn't recommended for optimal performance.
[array([[-1.346764  ,  0.7402935 , -0.6495765 , ..., -1.8527336 ,
        -0.29401016,  0.99657345],
       [-1.346764  ,  0.7402935 , -0.6495765 , ..., -1.8527336 ,
        -0.29401016,  0.99657345],
       [-1.346764  ,  0.7402935 , -0.6495765 , ..., -1.8527336 ,
        -0.29401016,  0.99657345],
       [-1.346764  ,  0.7402935 , -0.6495765 , ..., -1.8527336 ,
        -0.29401016,  0.99657345]], dtype=float32), array([[1.1318562e-04, 9.1240724e-04, 2.2728769e-04, ..., 6.8241949e-05,
        3.2433696e-04, 1.1789345e-03],
       [1.1318562e-04, 9.1240724e-04, 2.2728769e-04, ..., 6.8241949e-05,
        3.2433696e-04, 1.1789345e-03],
       [1.1318562e-04, 9.1240724e-04, 2.2728769e-04, ..., 6.8241949e-05,
        3.2433696e-04, 1.1789345e-03],
       [1.1318562e-04, 9.1240724e-04, 2.2728769e-04, ..., 6.8241949e-05,
        3.2433696e-04, 1.1789345e-03]], dtype=float32)]

Copy link
Contributor

@bfineran bfineran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI - we have some similar functionality in the pipeline (fails if input batch size isn't divisible by compiled batch size):

batches = self.split_engine_inputs(engine_inputs, self._batch_size)

could look into migrating

@mgoin
Copy link
Member Author

mgoin commented Jun 29, 2023

Great callout @bfineran , I refactored those functions with my padding functionality into utils/data.py so both Engine and Pipeline can use thme

@mgoin mgoin marked this pull request as ready for review June 29, 2023 20:45
@mgoin mgoin requested review from a team, shubhra, tdg5 and dsikka and removed request for a team June 29, 2023 20:45
bfineran
bfineran previously approved these changes Jul 11, 2023
Copy link
Contributor

@bfineran bfineran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment. LGTM- nice tests

src/deepsparse/utils/data.py Outdated Show resolved Hide resolved
@mgoin mgoin merged commit 3f9ff51 into main Jul 11, 2023
7 checks passed
@mgoin mgoin deleted the batched-engine branch July 11, 2023 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants