tensorflow: update XNNPACK and install headers / libs

This CL updates the XNNPACK dependency of Tensorflow to commit
79cd5f9e18ad0925ac9a050b00ea5a36230072db, which includes fixes to
its quantized implementation.

This CL also installs the XNNPACK headers and libs when built with the
'xnnpack' USE flag.

BUG=b:163339463
TEST=cq passes

Change-Id: I30fa8c30950f59d8b1e5d006b3f94e2895a2b7db
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/overlays/chromiumos-overlay/+/2790824
Tested-by: Jim Pollock <jmpollock@chromium.org>
Auto-Submit: Jim Pollock <jmpollock@chromium.org>
Reviewed-by: Stuart Langley <slangley@chromium.org>
diff --git a/sci-libs/tensorflow/Manifest b/sci-libs/tensorflow/Manifest
index b67ba1f..9bdb7b0 100644
--- a/sci-libs/tensorflow/Manifest
+++ b/sci-libs/tensorflow/Manifest
@@ -2,7 +2,7 @@
 DIST FP16-4dfe081cf6bcd15db339cf2680b9281b8451eeb3.zip 91462 BLAKE2B dc40d6129c009fc28b40d58b81b89e96a2deeeda3ea19c680b66073f02f955cc5dfc379d7843ec8aeed9fedeae6a70594b45752014526deee5c6226ec4b952e9 SHA512 15ae434977209a938739691eb91150556bac75a4a5449a9b4f135424f42beabd67ede4cf12c3a5594dea5028cc4aadaf989d0b42c2bb6f741db55ebd34717e24
 DIST FXdiv-63058eff77e11aa15bf531df5dd34395ec3017c8.zip 16646 BLAKE2B 8f6ca6feee16cb57d9b4504e7615703c2c7ea86fa8ea3f814668478cc13b8cb3cb1429200359be5521ca11f77e1c3d575b2222d123d3ac03586faab88a131ac2 SHA512 6b4830a56137e226d51ba8166fe1a31c51256f1666cc0a683abfb154fbe313d415ce15e362756865a1dd91510cf581c619604b29e8a5288c328c85da57d53308
 DIST OouraFFT-v1.0.tar.gz 110531 BLAKE2B de0e280407e21118e2bc6ca93a7caf3c4a49d0a13eda018b1497f98851af73dda4cef56460dba310336c7ed958e34feef4784ca2575a13287dec2c1ac9a5af6d SHA512 89c6e8fd57abf26351b3efb792008a1bbe62d404a4225dcae8aa666b3782a421be071bdc9760ebb0c95b5336ee5ea517d2fa43ab915045f7cf6fd76e73578079
-DIST XNNPACK-fb8d1f1b2bb2e32c141564528a39748c4631b453.zip 8335569 BLAKE2B dc93b1af0660051afcf8d77c9239621fddf94a2427083b0b091ffc501cf9bd317e1cd421cbdafb71baefe219afb76ed138e31e977a1ea0c036674d01191af9e5 SHA512 f4fbd425bc9fcc9cec33a1506423978a7e02544b2c3d9102e1dafd02c26ac6e4c5eaea7192417cd063e29b2d76e758cca1c37f12a7cd95343500bbfb5bc2a622
+DIST XNNPACK-79cd5f9e18ad0925ac9a050b00ea5a36230072db.zip 10378085 BLAKE2B b163263e7ff23848e442b3086677da46135ca1ee20ca792ee30c2127d4246e5d31ca0bceb7997e573f21803cc0620fc0c6e368305557c9520ef134597c70f958 SHA512 422ddfa0ed53806a652cf221d43674f0f6af25e8f209e29c0ef9fcaaa6c30eca65853af6d3d8bbf07f8a6213ea7aa55dbb003c11adacf96650e668cafee3b7f4
 DIST abseil-cpp-6f9d96a1f41439ac172ee2ef7ccd8edf0e5d068c.tar.gz 1785034 BLAKE2B 83cee194b416fff3d6ec3f4c02b4210a60ebfcf0316524fe9deb94372bfc34f33075fa59e2e71d8bb17c32f61a6e324072651c2bbf382d3d4a7540657b8a33a8 SHA512 f64fee62863f2103c1991136fd3bc2b71cd28c7ff62138ac991b5a7f81780a05e0e2bdd6a119d02e1d70dd54f989f584093957efaec94f26c9d6c3f4ee37f8ae
 DIST abseil-cpp-daf381e8535a1f1f1b8a75966a74e7cca63dee89.tar.gz 1275955 BLAKE2B eb6e0f9d55458a4ae6e8b86be89add9b0cc44d1ff5a11f6fa1ec93e2f46885c8ff87b8c1e674e1ba5ac13556ed303963864057f0e258fce41fa753d891846996 SHA512 56a827296e2948b90922766369f2040e04abd03320375240ad32fcc2925cdd1ffea312edcef1009a0c8abd79f521a61e433b7651dd4c2721d2b1ad13f48256de
 DIST backports.weakref-1.0rc1.tar.gz 7761 BLAKE2B 4cb2554a4b71bb1f8c41a790268511e4b382effc7e0328f74346d086de539a177111de9e2dabac19a44b2a1cdbf59a7425e30eee4caa4bfe66b0ca97f1f460a9 SHA512 f37e9eb0d9060d2e1588a941f623460bd4477d7e180b38b5cd46d9c1db1ee094ae63e4f5eeeb422823bf3d067f46c4124cb124a9e4ddb795bc0bfbc4802ab826
diff --git a/sci-libs/tensorflow/files/tensorflow-2.5.0-0005-xnnpack-update.patch b/sci-libs/tensorflow/files/tensorflow-2.5.0-0005-xnnpack-update.patch
new file mode 100644
index 0000000..6a644f4
--- /dev/null
+++ b/sci-libs/tensorflow/files/tensorflow-2.5.0-0005-xnnpack-update.patch
@@ -0,0 +1,33 @@
+diff --git a/tensorflow/lite/delegates/xnnpack/BUILD b/tensorflow/lite/delegates/xnnpack/BUILD
+index 8eb31175..9eafefc6 100644
+--- a/tensorflow/lite/delegates/xnnpack/BUILD
++++ b/tensorflow/lite/delegates/xnnpack/BUILD
+@@ -32,7 +32,7 @@ cc_library(
+         "//tensorflow/lite/schema:schema_fbs",
+         "//tensorflow/lite/tools/optimize/sparsity:format_converter",
+         "@FP16",
+-        "@XNNPACK//:xnnpack_f32",
++        "@XNNPACK//:xnnpack_for_tflite",
+     ],
+ )
+ 
+diff --git a/tensorflow/workspace2.bzl b/tensorflow/workspace2.bzl
+index efc50709..28a0e935 100644
+--- a/tensorflow/workspace2.bzl
++++ b/tensorflow/workspace2.bzl
+@@ -126,11 +126,11 @@ def _tf_repositories():
+     # and update the sha256 with the result.
+     tf_http_archive(
+         name = "XNNPACK",
+-        sha256 = "95b778a920a1a79efdb11bf68dda9b4fd16779a1a0210438582e750f9bfb6351",
+-        strip_prefix = "XNNPACK-fb8d1f1b2bb2e32c141564528a39748c4631b453",
++        sha256 = "dbb28e7ed4c09b6d09d9fcc194081560039994e60bfa753ba7f408f57605672e",
++        strip_prefix = "XNNPACK-79cd5f9e18ad0925ac9a050b00ea5a36230072db",
+         urls = [
+-            "https://storage.googleapis.com/mirror.tensorflow.org/github.com/google/XNNPACK/archive/fb8d1f1b2bb2e32c141564528a39748c4631b453.zip",
+-            "https://github.com/google/XNNPACK/archive/fb8d1f1b2bb2e32c141564528a39748c4631b453.zip",
++            "https://storage.googleapis.com/mirror.tensorflow.org/github.com/google/XNNPACK/archive/79cd5f9e18ad0925ac9a050b00ea5a36230072db.zip",
++            "https://github.com/google/XNNPACK/archive/79cd5f9e18ad0925ac9a050b00ea5a36230072db.zip",
+         ],
+     )
+ 
diff --git a/sci-libs/tensorflow/tensorflow-2.5.0-r1.ebuild b/sci-libs/tensorflow/tensorflow-2.5.0-r2.ebuild
similarity index 100%
rename from sci-libs/tensorflow/tensorflow-2.5.0-r1.ebuild
rename to sci-libs/tensorflow/tensorflow-2.5.0-r2.ebuild
diff --git a/sci-libs/tensorflow/tensorflow-2.5.0.ebuild b/sci-libs/tensorflow/tensorflow-2.5.0.ebuild
index 6c49572..7bd2fb5 100644
--- a/sci-libs/tensorflow/tensorflow-2.5.0.ebuild
+++ b/sci-libs/tensorflow/tensorflow-2.5.0.ebuild
@@ -20,7 +20,7 @@
 SLOT="0"
 KEYWORDS="*"
 # ChromeOS uses 'minimal' to compile only TensorFlow Lite, compilation without 'minimal' is not supported.
-IUSE="cuda mpi +python xla minimal label_image benchmark_model"
+IUSE="cuda mpi +python xla minimal label_image benchmark_model xnnpack"
 
 # distfiles that bazel uses for the workspace, will be copied to basel-distdir
 bazel_external_uris="
@@ -37,7 +37,7 @@
 	https://github.com/google/farmhash/archive/816a4ae622e964763ca0862d9dbd19324a1eaf45.tar.gz -> farmhash-816a4ae622e964763ca0862d9dbd19324a1eaf45.tar.gz
 	https://github.com/google/gemmlowp/archive/fda83bdc38b118cc6b56753bd540caa49e570745.zip -> gemmlowp-fda83bdc38b118cc6b56753bd540caa49e570745.zip
 	https://github.com/google/ruy/archive/54774a7a2cf85963777289193629d4bd42de4a59.zip -> ruy-54774a7a2cf85963777289193629d4bd42de4a59.zip
-	https://github.com/google/XNNPACK/archive/fb8d1f1b2bb2e32c141564528a39748c4631b453.zip -> XNNPACK-fb8d1f1b2bb2e32c141564528a39748c4631b453.zip
+	https://github.com/google/XNNPACK/archive/79cd5f9e18ad0925ac9a050b00ea5a36230072db.zip -> XNNPACK-79cd5f9e18ad0925ac9a050b00ea5a36230072db.zip
 	https://github.com/intel/ARM_NEON_2_x86_SSE/archive/1200fe90bb174a6224a525ee60148671a786a71f.tar.gz -> ARM_NEON_2_x86_SSE-1200fe90bb174a6224a525ee60148671a786a71f.tar.gz	https://github.com/googleapis/googleapis/archive/541b1ded4abadcc38e8178680b0677f65594ea6f.zip -> googleapis-541b1ded4abadcc38e8178680b0677f65594ea6f.zip
 	https://github.com/petewarden/OouraFFT/archive/v1.0.tar.gz -> OouraFFT-v1.0.tar.gz
 	https://github.com/pytorch/cpuinfo/archive/5916273f79a21551890fd3d56fc5375a78d1598d.zip -> pytorch-cpuinfo-5916273f79a21551890fd3d56fc5375a78d1598d.zip
@@ -149,6 +149,7 @@
 	"${FILESDIR}/tensorflow-2.5.0-0002-ashmem-create.patch"
 	"${FILESDIR}/tensorflow-2.5.0-0003-nnapi-delegates.patch"
 	"${FILESDIR}/tensorflow-2.5.0-0004-cpuinfo-arm-fix.patch"
+	"${FILESDIR}/tensorflow-2.5.0-0005-xnnpack-update.patch"
 )
 
 S="${WORKDIR}/${MY_P}"
@@ -467,6 +468,25 @@
 			einfo "Install benchmark_model tool"
 			dobin bazel-bin/tensorflow/lite/tools/benchmark/benchmark_model
 		fi
+
+		if use xnnpack; then
+			einfo "Installing XNNPACK headers and libs"
+			local bindir="../tensorflow-${PV}-bazel-base/execroot/org_tensorflow/bazel-out/$(get-cpu-str "${CHOST}")-opt/bin/external/"
+			insinto /usr/include/${PN}/xnnpack/
+			doins "../tensorflow-${PV}-bazel-base/external/XNNPACK/include/xnnpack.h"
+			doins "../tensorflow-${PV}-bazel-base/external/pthreadpool/include/pthreadpool.h"
+			dolib.a "${bindir}/clog/libclog.a"
+			dolib.a "${bindir}/cpuinfo/libcpuinfo_impl.pic.a"
+			dolib.a "${bindir}/pthreadpool/libpthreadpool.a"
+			# The lib names vary wildly between amd64 and arm, so
+			# easier just to scan for them rather than explicitly
+			# listing them and switching on ${ARCH}.
+			find "${bindir}/XNNPACK/" -name "*.a" |
+			while read -r i; do
+				dolib.a "${i}"
+			done
+		fi
+
 	fi
 
 	einstalldocs