# How to set QNNPack parallelism?

**URL:** <https://discuss.pytorch.org/t/how-to-set-qnnpack-parallelism/97359>\
**Category:** Mobile\
**Created:** [September 24, 2020, 10:55am UTC](https://discuss.pytorch.org/t/how-to-set-qnnpack-parallelism/97359 "2020-09-24T10:55:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbalchev](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dbalchev](https://discuss.pytorch.org/u/dbalchev)\
**Post date:** [September 24, 2020, 10:55am UTC](https://discuss.pytorch.org/t/how-to-set-qnnpack-parallelism/97359/1 "2020-09-24T10:55:54Z")

</div>

For our usecase we need to do the job on one thread (preferable the main one). For all other workflows we just call `torch.set_num_threads(1)` and this gets the job done 🙂.  
But QNNPack (more specifically the quantized Convolution) doesn’t respect this setting, since it uses [`caffe2::pthreadpool_()`](https://github.com/pytorch/pytorch/blob/v1.6.0/aten/src/ATen/native/quantized/cpu/qconv.cpp#L606).  
In our tests we get good results by making that function return `null`.

Is there a way to set the `num_threads` of that pool without recompiling pytorch?  
If not, what’s the minimal acceptable refactoring to make that configurable or respect the `torch.set_num_threads`?

---

<div class="post-metadata">

**Author:** ![jerryzh168](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jerryzh168/32/15217_2.png) [@jerryzh168](https://discuss.pytorch.org/u/jerryzh168)\
**Post date:** [October 7, 2020, 12:19am UTC](https://discuss.pytorch.org/t/how-to-set-qnnpack-parallelism/97359/2 "2020-10-07T00:19:20Z")

</div>

This is related to mobile, can you add a ‘mobile’ tag?

---

<div class="post-metadata">

**Author:** ![dbalchev](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dbalchev](https://discuss.pytorch.org/u/dbalchev)\
**Post date:** [October 7, 2020, 7:45am UTC](https://discuss.pytorch.org/t/how-to-set-qnnpack-parallelism/97359/3 "2020-10-07T07:45:35Z")

</div>

Our use-case is not for mobile. We plan to use QNNPACK if the machine we’re running on doesn’t support AVX2.

Should I add the mobile tag despite that?

---

<div class="post-metadata">

**Author:** ![dbalchev](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dbalchev](https://discuss.pytorch.org/u/dbalchev)\
**Post date:** [October 19, 2020, 8:19am UTC](https://discuss.pytorch.org/t/how-to-set-qnnpack-parallelism/97359/4 "2020-10-19T08:19:58Z")

</div>

I’m not sure how to add the mobile tag?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [October 19, 2020, 8:39am UTC](https://discuss.pytorch.org/t/how-to-set-qnnpack-parallelism/97359/5 "2020-10-19T08:39:39Z")

</div>

Changed the category for you. 😉

---

<div class="post-metadata">

**Author:** ![dbalchev](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dbalchev](https://discuss.pytorch.org/u/dbalchev)\
**Post date:** [October 22, 2020, 8:27am UTC](https://discuss.pytorch.org/t/how-to-set-qnnpack-parallelism/97359/6 "2020-10-22T08:27:48Z")

</div>

@ptrblck thanks (:  
@jerryzh168 any updates on this?

---

<div class="post-metadata">

**Author:** ![kimishpatel](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kimishpatel/32/39320_2.png) [@kimishpatel](https://discuss.pytorch.org/u/kimishpatel)\
**Post date:** [October 26, 2020, 10:02pm UTC](https://discuss.pytorch.org/t/how-to-set-qnnpack-parallelism/97359/7 "2020-10-26T22:02:22Z")

</div>

@dbalchev, you can try `caffe2::pthreadpool()->set_thread_count(1)`.

---

<div class="post-metadata">

**Author:** ![dbalchev](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dbalchev](https://discuss.pytorch.org/u/dbalchev)\
**Post date:** [October 27, 2020, 8:36am UTC](https://discuss.pytorch.org/t/how-to-set-qnnpack-parallelism/97359/8 "2020-10-27T08:36:52Z")

</div>

@kimishpatel We’re using the python wheel for inference. I’ve tried recompiling pytorch, so that `torch.set_num_threads` calls `pthreadpool()->set_thread_count` as well ([PR with the rebased commit](https://github.com/pytorch/pytorch/pull/45566)). It works, but we have to ship a custom pytorch wheel. I didn’t find a way to do that through the python API or even with a C++ extension.

---

<div class="post-metadata">

**Author:** ![kimishpatel](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/kimishpatel/32/39320_2.png) [@kimishpatel](https://discuss.pytorch.org/u/kimishpatel)\
**Post date:** [November 2, 2020, 3:38pm UTC](https://discuss.pytorch.org/t/how-to-set-qnnpack-parallelism/97359/9 "2020-11-02T15:38:35Z")

</div>

> [@kimishpatel](#):
>
> caffe2::pthreadpool()-\>set\_thread\_count(1)

@dbalchev sorry for late response. For some reason I did not get any notification. Unfortunately at the moment we dont have python API for this. Possibly in next release, or we can thin of introducing it in master if that may work for you.

---

<div class="post-metadata">

**Author:** ![dbalchev](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/d/e9a140/32.png) [@dbalchev](https://discuss.pytorch.org/u/dbalchev)\
**Post date:** [November 2, 2020, 5:58pm UTC](https://discuss.pytorch.org/t/how-to-set-qnnpack-parallelism/97359/10 "2020-11-02T17:58:08Z")

</div>

@kimishpatel Thanks for the reply! In my PR I set the size of that pool in `torch.set_num_threads` and it’s approved (:
