# How to run fp32 model in fp16 mode?

**URL:** <https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963>\
**Category:** Questions\
**Created:** [November 17, 2022, 3:43am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963 "2022-11-17T03:43:45Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![A\_newer](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@A\_newer](https://discuss.tvm.apache.org/u/A_newer)\
**Post date:** [November 17, 2022, 3:43am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/1 "2022-11-17T03:43:46Z")

</div>

I want to read a local fp32 .tflite model and run it in fp16 mode. How to achieve that?

---

<div class="post-metadata">

**Author:** ![elvin-n](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/elvin-n/32/2682_2.png) [@elvin-n](https://discuss.tvm.apache.org/u/elvin-n)\
**Post date:** [November 17, 2022, 5:44am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/2 "2022-11-17T05:44:28Z")

</div>

you need to use [ToMixedPrecision](https://tvm.apache.org/docs/reference/api/python/relay/transform.html?#tvm.relay.transform.ToMixedPrecision) transformation before compilation of the model

For example:

```auto
        with tvm.transform.PassContext(
            config={"relay.ToMixedPrecision.keep_orig_output_dtype": True}
        ):
            mod = ToMixedPrecision("float16")(mod)

```

And target hardware definitely should support fp16 operations, That is a case of GPU and latest ARM cpu, for example, but not x86 processors yet

And one more note - the algorithm of transformation is based on three types of operations - ops are olways converted, ops which can be converted if neighbor op are converted and ops which never will be converted. The list of ops can be seen [here](https://github.com/apache/tvm/blob/main/python/tvm/relay/transform/mixed_precision.py) I am mentioned just you would not be surprised why some ops might not be converted.

You can modify the list but you will have to verify accuracy of the network additionally. That is a requirement in any case, but for enumerated lists of ops accuracy drop is usually negligible.

---

<div class="post-metadata">

**Author:** ![A\_newer](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@A\_newer](https://discuss.tvm.apache.org/u/A_newer)\
**Post date:** [November 17, 2022, 6:42am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/3 "2022-11-17T06:42:26Z")

</div>

Thanks for suggestion. I have tried this function but the benchmark result shows that it was getting worse.

---

<div class="post-metadata">

**Author:** ![elvin-n](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/elvin-n/32/2682_2.png) [@elvin-n](https://discuss.tvm.apache.org/u/elvin-n)\
**Post date:** [November 17, 2022, 6:43am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/4 "2022-11-17T06:43:52Z")

</div>

What is target hardware?

Have you tuned network anyhow for fp32 and fp16?

---

<div class="post-metadata">

**Author:** ![A\_newer](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@A\_newer](https://discuss.tvm.apache.org/u/A_newer)\
**Post date:** [November 17, 2022, 6:58am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/5 "2022-11-17T06:58:45Z")

</div>

I tried on Dimensity 9000, both for it’s cpu and gpu, which is mali.  
I have tuned a network in fp32, and it can’t be transfer to fp16 in this way.

---

<div class="post-metadata">

**Author:** ![elvin-n](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/elvin-n/32/2682_2.png) [@elvin-n](https://discuss.tvm.apache.org/u/elvin-n)\
**Post date:** [November 17, 2022, 8:29am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/6 "2022-11-17T08:29:13Z")

</div>

Yes, you have to re-tune model been converted into fp16. Old tuning statistics will be ignored. And only after this we can compare performance and can claim about problems.

---

<div class="post-metadata">

**Author:** ![A\_newer](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@A\_newer](https://discuss.tvm.apache.org/u/A_newer)\
**Post date:** [November 17, 2022, 8:42am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/7 "2022-11-17T08:42:34Z")

</div>

Thanks, I will take a try on tuning fp16.  
And before that, I want to run a fp32 network in both fp32 and fp16 without tuning and make a comparison.

---

<div class="post-metadata">

**Author:** ![elvin-n](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/elvin-n/32/2682_2.png) [@elvin-n](https://discuss.tvm.apache.org/u/elvin-n)\
**Post date:** [November 17, 2022, 9:19am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/8 "2022-11-17T09:19:41Z")

</div>

Yes, non tuned vs non tunes is also proper comparison. On the same time the default configuration of kernels is a result of schedule author’s analysis that happened on certain device that most likely will not fit to another model even with the same architecture. I.e. it should be compared but I would not do much conclusions based on these results.

And I recommend to tune rather more than less trials especially in case of AutoTVM when we cannot estimate the tuning efficiency. In case of AutoScheduler you always can take a look into total\_latency.tsv and see if performance continue improve or you get to some flat state.

---

<div class="post-metadata">

**Author:** ![A\_newer](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@A\_newer](https://discuss.tvm.apache.org/u/A_newer)\
**Post date:** [November 25, 2022, 7:27am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/9 "2022-11-25T07:27:33Z")

</div>

I followed your instruction to tune a mobilenet on gpu, which make a big progress in benchmark. Thanks a lot.  
But I can’t restore such a result on mobile cpu. After I add ToMixedPrecision for my model, it gets slower compared with fp32 during and after tuning process.

---

<div class="post-metadata">

**Author:** ![elvin-n](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/elvin-n/32/2682_2.png) [@elvin-n](https://discuss.tvm.apache.org/u/elvin-n)\
**Post date:** [November 25, 2022, 7:37am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/10 "2022-11-25T07:37:53Z")

</div>

What target do you use for compilation and tuning?

---

<div class="post-metadata">

**Author:** ![A\_newer](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@A\_newer](https://discuss.tvm.apache.org/u/A_newer)\
**Post date:** [November 25, 2022, 7:48am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/11 "2022-11-25T07:48:49Z")

</div>

llvm -mtriple=aarch64-linux-gnu -mattr=+neon

---

<div class="post-metadata">

**Author:** ![elvin-n](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/elvin-n/32/2682_2.png) [@elvin-n](https://discuss.tvm.apache.org/u/elvin-n)\
**Post date:** [November 25, 2022, 7:52am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/12 "2022-11-25T07:52:59Z")

</div>

Augh, I have never verified what happens on ARM cpu for fp16. I was mostly concentrated on graphics. Will try to verify

---

<div class="post-metadata">

**Author:** ![elenkalda-arm](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/e/ee59a6/32.png) [@elenkalda-arm](https://discuss.tvm.apache.org/u/elenkalda-arm)\
**Post date:** [November 25, 2022, 9:52am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/13 "2022-11-25T09:52:44Z")

</div>

Adding `+fullfp16` might help (but also might not). There was a study done on FP16 on the CPUs and it seemed like some networks saw improved performance, some became slower (the slowness was due to ToMixedPrecision introducing extra casts, you can also try running FoldConstant after ToMixedPrecision to get rid of some of them). Also, I think some of the slowness was due to some schedules not being particularly vectorization friendly… That was all without any tuning though. cc @ashutosh-arm who knows more 🙂

---

<div class="post-metadata">

**Author:** ![A\_newer](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@A\_newer](https://discuss.tvm.apache.org/u/A_newer)\
**Post date:** [November 28, 2022, 1:51am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/14 "2022-11-28T01:51:46Z")

</div>

Sorry, what’s fullfp16 and where to add? Thanks.

---

<div class="post-metadata">

**Author:** ![A\_newer](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@A\_newer](https://discuss.tvm.apache.org/u/A_newer)\
**Post date:** [November 28, 2022, 2:18am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/15 "2022-11-28T02:18:26Z")

</div>

Thanks a lot. Now I have another error when applying MixPrecision for mali gpu.  
Error log is like:

* * *

File “/home/tvm/tvm/python/tvm/auto\_scheduler/measure.py”, line 1150,  
in \_rpc\_run func.entry\_func(\*loc\_args)  
File “/home/tvm/tvm/python/tvm/\_ffi/\_ctypes/packed\_func.py”, line 237, in **call** rai  
…  
execution of TVM. For more information, please see: [Handle TVM Errors — tvm 0.11.dev0 documentation](https://tvm.apache.org/docs/errors.html) ---------------------------------------------------------------  
Check failed: ret == 0 (-1 vs. 0) :  
TVMError: Cannot handle float16 as device function argument , all\_cost:2.11, Tstamp:1669419374.61)

* * *

Besides that, the layer named vm\_mod\_fused\_nn\_contrib\_conv2d\_winograd\_without\_weight\_transform\_add\_nn\_relu\_1 has no performance static. In the schedule table, it’s like

* * *

| 4 | vm\_mod\_fused\_nn\_contrib\_conv2d\_winograd\_without\_weight\_transform\_add\_nn\_relu\_1 | - | - | 5120 |  
| 5 | vm\_mod\_fused\_nn\_contrib\_conv2d\_winograd\_without\_weight\_transform\_add\_nn\_relu\_2 | - | - | 8640 |  
| 6 | vm\_mod\_fused\_nn\_conv2d\_add\_add | 0.876 | 213.75 | 64 |

* * *

As you can see, the 6th layer conv2d has it’s Laytency and Speed data, but not for 4th and 5th layer.

---

<div class="post-metadata">

**Author:** ![elenkalda-arm](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/e/ee59a6/32.png) [@elenkalda-arm](https://discuss.tvm.apache.org/u/elenkalda-arm)\
**Post date:** [November 28, 2022, 9:46am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/17 "2022-11-28T09:46:30Z")

</div>

Sorry - it should be added to the `-mattr`. It enables the FP16 support on the processor (in case it wasn’t already enabled).

---

<div class="post-metadata">

**Author:** ![A\_newer](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@A\_newer](https://discuss.tvm.apache.org/u/A_newer)\
**Post date:** [November 28, 2022, 10:06am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/18 "2022-11-28T10:06:01Z")

</div>

Thanks, I will try and reply soon.

---

<div class="post-metadata">

**Author:** ![A\_newer](https://discuss.tvm.apache.org/letter_avatar_proxy/v4/letter/a/ecccb3/32.png) [@A\_newer](https://discuss.tvm.apache.org/u/A_newer)\
**Post date:** [December 2, 2022, 9:21am UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/19 "2022-12-02T09:21:08Z")

</div>

I try fullfp16+MixPrecision on resnet50. But it gets slower on neon cpu after tuning.

---

<div class="post-metadata">

**Author:** ![ashutosh-arm](https://discuss.tvm.apache.org/user_avatar/discuss.tvm.apache.org/ashutosh-arm/32/2808_2.png) [@ashutosh-arm](https://discuss.tvm.apache.org/u/ashutosh-arm)\
**Post date:** [December 21, 2022, 5:55pm UTC](https://discuss.tvm.apache.org/t/how-to-run-fp32-model-in-fp16-mode/13963/20 "2022-12-21T17:55:14Z")

</div>

Just for reference adding link to the mattr in use in a sample test: [https://github.com/apache/tvm/blob/36d89a28fb984caa83082b034c46180a82dcd1ea/tests/python/integration/test\_arm\_aprofile.py#L28](https://github.com/apache/tvm/blob/36d89a28fb984caa83082b034c46180a82dcd1ea/tests/python/integration/test_arm_aprofile.py#L28)
