tesseract icon indicating copy to clipboard operation
tesseract copied to clipboard

Use Intel VNNI for int dot product

Open amitdo opened this issue 2 years ago • 17 comments

There are two variants:

  • AVX512_VNNI (Tiger Lake, Rocket Lake) - 512bit/256bit/128bit
  • AVX_VNNI - (upcoming Alder Lake) - 256bit/128bit

VNNI replaces 3 simd instructions with one instruction.

It seems that we can use it inside MultiplyGroup().

https://software.intel.com/content/www/us/en/develop/articles/intel-advanced-vector-extensions-512-intel-avx-512-new-vector-neural-network-instruction.html

amitdo avatar Aug 03 '21 13:08 amitdo

That requires access to test machines which support those instructions. Up to now I don't have any server with AVX512.

stweil avatar Sep 05 '21 08:09 stweil

AVX_VNNI - (upcoming Alder Lake) - 256bit/128bit

This will be available soon. Maybe we could ask users to help testing this feature a few months from the launch.

amitdo avatar Sep 05 '21 08:09 amitdo

@stweil, can I add AVX_VNNI (256 bit) detection as a first step?

amitdo avatar Aug 02 '22 10:08 amitdo

Sure, but who has such hardware to test it?

stweil avatar Aug 03 '22 11:08 stweil

There are tens of millions of people with Intel's Alder Lake, some of them are Tesseract users. We can ask in the forum to test the detection (and later the intdotproductvnni). Hopefully, we will find at least one person that has this CPU and is willing to help.

amitdo avatar Aug 03 '22 23:08 amitdo

@amitdo, I just noticed that the notebook which I used for AVX512F also has AVX512VNNI. :-) Do you already have code for the detection? If not, I can add it ~(just have to find the right documentation which bit in cpuid is used)~. Wikipedia has the required documentation.

stweil avatar Aug 05 '22 17:08 stweil

Go ahead!

Could you do the AVX_VNNI detection too? You are more familiar with the detection code than me.

Since most of our files already have:

(C) Copyright <year>, Google Inc.

I think we can look at other Google projects with the same license as ours, and use parts of the code if we need it.

https://github.com/google/cpu_features/issues/263

https://github.com/tensorflow/tensorflow/blob/18d203b1ef84b1e2d4de9eb249194ab1386bdd7b/tensorflow/core/platform/cpu_info.cc

amitdo avatar Aug 06 '22 08:08 amitdo

Detection is now implemented by commit 0daf18c2028e5217cd996522816c3dd2ec1a4198.

stweil avatar Aug 06 '22 10:08 stweil

I see that you check that avx/avx2 is supported by the OS. Do you also check somewhere that avx512 is supported by the OS?

amitdo avatar Aug 06 '22 11:08 amitdo

No, currently only the hardware capabilities are checked for avx512. Up to now nobody complained, so maybe AVX512F was only used on operating systems which support it. I'll add a check for OS support. Thank you for the hint!

stweil avatar Aug 06 '22 12:08 stweil

I will try to implement intsimdmatrixavx512vnni.cpp.

amitdo avatar Aug 06 '22 16:08 amitdo

Great. Maybe you can use https://github.com/stweil/tesseract/tree/avx512-vnni (which adds the framework, but simply copied the existing AVX2 code) as a starting point.

stweil avatar Aug 06 '22 16:08 stweil

Yes, thank you. Please open a draft PR with that code. I'll push the needed changes to your PR.

amitdo avatar Aug 06 '22 16:08 amitdo

See PR #3894.

stweil avatar Aug 06 '22 17:08 stweil

Stefan,

There are two ways to implement intsimdmatrixavx512vnni.cpp:

  1. The 'right and complete way', which is also the 'complex way': a) First convert intsimdmatrixavx2.cpp to intsimdmatrixavx512.cpp. b) Then convert intsimdmatrixavx512.cpp to intsimdmatrixavx512vnni.cpp.
  2. The 'simple way', which is incomplete but still expected to work fine and to be much faster than intsimdmatrixavx2.cpp:
    AVX512-VNNI supports 256-bit vector operations, not just 512-bit vector operations. Since AVX2 uses 256-bit vectors, I believe only a few changes are needed to convert intsimdmatrixavx2.cpp to intsimdmatrixavx512vnni.cpp which will use 256-bits vectors instead of 512-bit vectors.

I want to implement the second way in PR #3894. We can still implement the first way later.

What do you think about my suggestion?

amitdo avatar Aug 07 '22 19:08 amitdo

intsimdmatrixavx512vnni.cpp?

stweil avatar Aug 08 '22 05:08 stweil

intsimdmatrixavx512vnni.cpp?

Fixed :-)

amitdo avatar Aug 08 '22 06:08 amitdo