# Stream.synchronize() is slow (python API)

**URL:** <https://forums.developer.nvidia.com/t/stream-synchronize-is-slow-python-api/183169>\
**Category:** TensorRT\
**Created:** [July 9, 2021, 3:24am UTC](https://forums.developer.nvidia.com/t/stream-synchronize-is-slow-python-api/183169 "2021-07-09T03:24:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![751180903](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@751180903](https://forums.developer.nvidia.com/u/751180903)\
**Post date:** [July 9, 2021, 3:24am UTC](https://forums.developer.nvidia.com/t/stream-synchronize-is-slow-python-api/183169/1 "2021-07-09T03:24:58Z")

</div>

## Description

During inference, stream.synchronize() is very slow. Is there any approach to get rid of it ?

## Environment

**TensorRT Version** : 8.0.0.3  
**GPU Type** : T4  
**Nvidia Driver Version** : 450  
**CUDA Version** : 11.0  
**CUDNN Version** : 8.2.0  
**Operating System + Version** : CENTOS7  
**Python Version (if applicable)**: 3.7.19  
**TensorFlow Version (if applicable)**:  
**PyTorch Version (if applicable)**:  
**Baremetal or Container (if container which image + tag)**:

## Relevant Files

Please attach or include links to any models, data, files, or scripts necessary to reproduce your issue. (Github repo, Google Drive, Dropbox, etc.)

## Steps To Reproduce

```auto
def do_inference(context, bindings, inputs, outputs, stream, batch_size):
    # Transfer data from CPU to the GPU.
    [cuda.memcpy_htod_async(inp.device, inp.host, stream) for inp in inputs]
    # Run inference.
    context.execute_async(batch_size=batch_size, bindings=bindings, stream_handle=stream.handle)
    # Transfer predictions back from the GPU.
    [cuda.memcpy_dtoh_async(out.host, out.device, stream) for out in outputs]
    # Synchronize the stream
    stream.synchronize()
    # Return only the host outputs.
    return [out.host for out in outputs]

```

Data transfer between cpu & gpu and execution speed is both okay.  
stream.synchronize() almost takes 90% percent of time in this function.

Please include:

- Exact steps/commands to build your repro
- Exact steps/commands to run your repro
- Full traceback of errors encountered

---

<div class="post-metadata">

**Author:** ![NVES](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/nves/32/14043_2.png) [@NVES](https://forums.developer.nvidia.com/u/NVES)\
**Post date:** [July 9, 2021, 3:37am UTC](https://forums.developer.nvidia.com/t/stream-synchronize-is-slow-python-api/183169/2 "2021-07-09T03:37:55Z")

</div>

Hi,  
Request you to share the model, script, profiler and performance output if not shared already so that we can help you better.  
Alternatively, you can try running your model with trtexec command.  
[https://github.com/NVIDIA/TensorRT/tree/master/samples/opensource/trtexec](https://github.com/NVIDIA/TensorRT/tree/master/samples/opensource/trtexec)

While measuring the model performance, make sure you consider the latency and throughput of the network inference, excluding the data pre and post-processing overhead.  
Please refer below link for more details:  
[https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-722/best-practices/index.html#measure-performance](https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-722/best-practices/index.html#measure-performance)

Thanks!

---

<div class="post-metadata">

**Author:** ![751180903](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@751180903](https://forums.developer.nvidia.com/u/751180903)\
**Post date:** [July 9, 2021, 3:49am UTC](https://forums.developer.nvidia.com/t/stream-synchronize-is-slow-python-api/183169/4 "2021-07-09T03:49:34Z")

</div>

@NVES Hi,

Please find below my trt engine.  
[test.trt](https://forums.developer.nvidia.com/uploads/short-url/g4X0LQV7CaZtZj8imFUSZc4HATN.trt) (20.7 MB)

It is a dynamic-input model. Input shapes are ranging from (1,3,100,100) to (500, 3, 410, 224) and output shape is (bs, 2).

When i tested a (450, 3, 394, 224) input on my machine, the synchronize step takes ~0.66s while the execution step only takes 0.001s.

Thanks.

---

<div class="post-metadata">

**Author:** ![spolisetty](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/spolisetty/32/14047_2.png) [@spolisetty](https://forums.developer.nvidia.com/u/spolisetty)\
**Post date:** [July 9, 2021, 5:05am UTC](https://forums.developer.nvidia.com/t/stream-synchronize-is-slow-python-api/183169/5 "2021-07-09T05:05:05Z")

</div>

Hi @751180903,

This looks like CUDA stream related. We recommend you to please post your concern on CUDA forum to get better help.

> **[CUDA Programming and Performance](https://forums.developer.nvidia.com/c/accelerated-computing/cuda/cuda-programming-and-performance/7)**
>
> General discussion area for algorithms, optimizations, and approaches to GPU Computing with CUDA C, C++, Thrust, Fortran, Python (pyCUDA), etc.

Thank you.

---

<div class="post-metadata">

**Author:** ![ycy1164656](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@ycy1164656](https://forums.developer.nvidia.com/u/ycy1164656)\
**Post date:** [August 19, 2021, 9:49am UTC](https://forums.developer.nvidia.com/t/stream-synchronize-is-slow-python-api/183169/6 "2021-08-19T09:49:42Z")

</div>

but it’s in your tensorrt demo code, do not make cuda-part your scapegoat…could you explain why this line code is here

---

<div class="post-metadata">

**Author:** ![SunilJB](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/suniljb/32/14043_2.png) [@SunilJB](https://forums.developer.nvidia.com/u/SunilJB)\
**Post date:** [August 24, 2021, 1:26pm UTC](https://forums.developer.nvidia.com/t/stream-synchronize-is-slow-python-api/183169/7 "2021-08-24T13:26:41Z")

</div>

Hi @ycy1164656  
By specifying a stream, the CUDA API calls become asynchronous, meaning that the call may return before the command has been completed. Memory transfer instructions and kernel invocation can use CUDA stream.  
`stream.synchronize()` synchronize all streams on a CUDA device.  
Sample codes are generalized for multiple inputs/outputs just for your reference. You can modify the code to make it all synchronous based on your use case.

Thanks
