Skip to content

Commit

Permalink
Add RISC-V V support (tesseract-ocr#4346)
Browse files Browse the repository at this point in the history
Convert riscv-v-spec-1.0.pdf into 111 PNG images,
then perform OCR on each one in sequence,
and measure the testing time on banana_f3:

old:        31m16.267s
new:        16m51.155s

Co-authored-by: sunyuechi <[email protected]>
Co-authored-by: Stefan Weil <[email protected]>
  • Loading branch information
sunyuechi and stweil committed Nov 8, 2024
1 parent d7c0a05 commit a33df79
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ libtesseract_la_LIBADD += libtesseract_neon.la
noinst_LTLIBRARIES += libtesseract_neon.la
endif

if HAVE_RVV
libtesseract_rvv_la_CXXFLAGS = $(RVV_CXXFLAGS)
libtesseract_rvv_la_CXXFLAGS += -O3
libtesseract_rvv_la_CXXFLAGS += -I$(top_srcdir)/src/ccutil
libtesseract_rvv_la_SOURCES = src/arch/intsimdmatrixrvv.cpp
libtesseract_la_LIBADD += libtesseract_rvv.la
noinst_LTLIBRARIES += libtesseract_rvv.la
endif

libtesseract_la_SOURCES += src/arch/intsimdmatrix.cpp
libtesseract_la_SOURCES += src/arch/simddetect.cpp

Expand Down
21 changes: 21 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ AM_CONDITIONAL([HAVE_AVX512F], false)
AM_CONDITIONAL([HAVE_FMA], false)
AM_CONDITIONAL([HAVE_SSE4_1], false)
AM_CONDITIONAL([HAVE_NEON], false)
AM_CONDITIONAL([HAVE_RVV], false)

case "${host_cpu}" in

Expand Down Expand Up @@ -188,6 +189,16 @@ case "${host_cpu}" in

;;

riscv*)

AX_CHECK_COMPILE_FLAG([-march=rv64gcv], [rvv=true], [rvv=false], [$WERROR])
AM_CONDITIONAL([HAVE_RVV], [$rvv])
if $rvv; then
AC_DEFINE([HAVE_RVV], [1], [Enable RVV instructions])
check_for_rvv=1
fi
;;

*)

AC_MSG_WARN([No compiler options for $host_cpu])
Expand All @@ -207,6 +218,16 @@ if test x$check_for_neon = x1; then
fi
fi
# additional checks for RVV targets
if test x$check_for_rvv = x1; then
AC_MSG_NOTICE([checking how to detect RVV availability])
AC_CHECK_FUNCS([getauxval])
if test $ac_cv_func_getauxval = no; then
AC_MSG_WARN([RVV is available, but we don't know how to check for it. Will not be able to use RVV.])
fi
fi

AX_CHECK_COMPILE_FLAG([-fopenmp-simd], [openmp_simd=true], [openmp_simd=false], [$WERROR])
AM_CONDITIONAL([OPENMP_SIMD], $openmp_simd)

Expand Down
2 changes: 2 additions & 0 deletions src/arch/intsimdmatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ struct TESS_API IntSimdMatrix {
static const IntSimdMatrix *intSimdMatrix;
// Only available with NEON.
static const IntSimdMatrix intSimdMatrixNEON;
// Only available with RVV.
static const IntSimdMatrix intSimdMatrixRVV;
// Only available with AVX2 / AVX / FMA / SSE.
static const IntSimdMatrix intSimdMatrixAVX2;
static const IntSimdMatrix intSimdMatrixSSE;
Expand Down
88 changes: 88 additions & 0 deletions src/arch/intsimdmatrixrvv.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
///////////////////////////////////////////////////////////////////////
// File: intsimdmatrixrvv.cpp
// Description: matrix-vector product for 8-bit data on rvv.
// Author: sunyuechi
//
// Copyright (c) 2024 Institute of Software Chinese Academy of Sciences (ISCAS).
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///////////////////////////////////////////////////////////////////////

#ifdef HAVE_CONFIG_H
# include "config_auto.h" // for HAVE_RVV, ...
#endif

#if HAVE_RVV
# include "intsimdmatrix.h"
# include "tesstypes.h"

namespace tesseract {

static int DotProduct(const int8_t *u, const int8_t *v, int num) {
int total = 0;

asm __volatile__ (
" .option arch, +v \n\t"
" vsetvli t0,zero,e32,m8,ta,ma \n\t"
" vmv.v.i v0,0 \n\t"
"1: \n\t"
" vsetvli t0,%[num],e8,m2,ta,ma \n\t"
" vle8.v v16,0(%[u]) \n\t"
" vle8.v v24,0(%[v]) \n\t"
" sub %[num],%[num],t0 \n\t"
" vwmul.vv v8,v24,v16 \n\t"
" add %[u],%[u],t0 \n\t"
" add %[v],%[v],t0 \n\t"
" vsetvli zero,zero,e16,m4,tu,ma \n\t"
" vwadd.wv v0,v0,v8 \n\t"
" bnez %[num],1b \n\t"
" vsetvli t0,zero,e32,m8,ta,ma \n\t"
" vmv.s.x v8,zero \n\t"
" vredsum.vs v0,v0,v8 \n\t"
" vmv.x.s %[total],v0 \n\t"
: [u] "+r" (u),
[v] "+r" (v),
[num] "+r" (num),
[total] "+r" (total)
:
: "cc", "memory"
);

return total;
}

static void matrixDotVector(int dim1, int dim2, const int8_t *wi, const TFloat *scales,
const int8_t *u, TFloat *v) {
int num_out = dim1;
int num_in = dim2 - 1;
for (int i = 0; i < num_out; ++i) {
const int8_t *wi_start = wi + i * dim2;
int total = DotProduct(wi_start, u, num_in);
// Add in the bias and apply scaling.
v[i] = (total + wi_start[num_in] * INT8_MAX) * scales[i];
}
}

const IntSimdMatrix IntSimdMatrix::intSimdMatrixRVV = {
// Function.
matrixDotVector,
// Number of 32 bit outputs held in each register.
1,
// Maximum number of registers that we will use to hold outputs.
1,
// Number of 8 bit inputs in the inputs register.
1,
// Number of inputs in each weight group.
1
};

} // namespace tesseract.

#endif /* HAVE_RVV */
20 changes: 20 additions & 0 deletions src/arch/simddetect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@
# endif
#endif

#if defined(HAVE_RVV)
# if defined(HAVE_GETAUXVAL)
# include <sys/auxv.h>
# define HWCAP_RV(letter) (1ul << ((letter) - 'A'))
# endif
#endif

namespace tesseract {

// Computes and returns the dot product of the two n-vectors u and v.
Expand All @@ -89,6 +96,8 @@ bool SIMDDetect::neon_available_ = true;
#elif defined(HAVE_NEON)
// If true, then Neon has been detected.
bool SIMDDetect::neon_available_;
#elif defined(HAVE_RVV)
bool SIMDDetect::rvv_available_;
#else
// If true, then AVX has been detected.
bool SIMDDetect::avx_available_;
Expand Down Expand Up @@ -229,6 +238,13 @@ SIMDDetect::SIMDDetect() {
elf_aux_info(AT_HWCAP, &hwcap, sizeof hwcap);
neon_available_ = hwcap & HWCAP_NEON;
# endif
#endif

#if defined(HAVE_RVV)
# if defined(HAVE_GETAUXVAL)
const unsigned long hwcap = getauxval(AT_HWCAP);
rvv_available_ = hwcap & HWCAP_RV('V');
# endif
#endif

// Select code for calculation of dot product based on autodetection.
Expand Down Expand Up @@ -258,6 +274,10 @@ SIMDDetect::SIMDDetect() {
} else if (neon_available_) {
// NEON detected.
SetDotProduct(DotProductNEON, &IntSimdMatrix::intSimdMatrixNEON);
#endif
#if defined(HAVE_RVV)
} else if (rvv_available_) {
SetDotProduct(DotProductGeneric, &IntSimdMatrix::intSimdMatrixRVV);
#endif
}

Expand Down
6 changes: 6 additions & 0 deletions src/arch/simddetect.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class SIMDDetect {
static inline bool IsNEONAvailable() {
return detector.neon_available_;
}
// Returns true if RVV is available on this system.
static inline bool IsRVVAvailable() {
return detector.rvv_available_;
}

// Update settings after config variable was set.
static TESS_API void Update();
Expand All @@ -86,6 +90,8 @@ class SIMDDetect {
static TESS_API bool sse_available_;
// If true, then NEON has been detected.
static TESS_API bool neon_available_;
// If true, then RVV has been detected.
static TESS_API bool rvv_available_;
};

} // namespace tesseract
Expand Down
3 changes: 3 additions & 0 deletions src/tesseract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ static void PrintVersionInfo() {
#if defined(HAVE_NEON) || defined(__aarch64__)
if (tesseract::SIMDDetect::IsNEONAvailable())
printf(" Found NEON\n");
#elif defined(HAVE_RVV)
if (tesseract::SIMDDetect::IsRVVAvailable())
printf(" Found RVV\n");
#else
if (tesseract::SIMDDetect::IsAVX512BWAvailable()) {
printf(" Found AVX512BW\n");
Expand Down

0 comments on commit a33df79

Please sign in to comment.