Skip to content

Commit

Permalink
[wasm] Update xnnpack (#7507)
Browse files Browse the repository at this point in the history
* wip

* Add xnn_caches

* Upgrade xnnpack

* exp

* Update xnnpack deps

* Fix xnn cache

* TEST

* Cleanup

* Cleanup

* Cleanup

* Update xnnpack

* Add flag to avoid unused function

* Add comment

* Add config to turn xnnpack logs off

* Add sha256 for emsdk

* Update xnnpack and toolchain, and disable xnn caches

* Fix lint

* Remove unused include
  • Loading branch information
chunnienc authored May 20, 2023
1 parent 80a8091 commit c66f302
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 52 deletions.
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ build --cxxopt="-std=c++17"
build --cxxopt="-fno-rtti"
build --cxxopt="-fno-exceptions"
build --cxxopt="-fomit-frame-pointer"
# Avoid unused function error `xnn_vlog` when building XNNPACK with `-c opt`.
build:xnnpack-opt --copt="-DXNN_LOG_TO_STDIO=0"
build:xnnpack-opt --define=xnn_log_level=none

# The following --define=EXECUTOR=remote will be able to be removed
# once https://github.com/bazelbuild/bazel/issues/7254 is fixed
Expand Down
66 changes: 20 additions & 46 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ http_archive(
name = "emsdk",
# TODO: Remove repo_mapping when emsdk updates to rules_nodejs 5
repo_mapping = {"@nodejs": "@nodejs_host"},
sha256 = "b8270749b99d8d14922d1831b93781a5560fba6f7bce65cd477fc1b6aa262535",
strip_prefix = "emsdk-3.1.28/bazel",
urls = ["https://github.com/emscripten-core/emsdk/archive/refs/tags/3.1.28.tar.gz"],
sha256 = "bbea764c57af830e761f1fb8600d42dc303aa63ffd43647694eda5b8b757b469",
strip_prefix = "emsdk-3.1.35/bazel",
urls = ["https://github.com/emscripten-core/emsdk/archive/refs/tags/3.1.35.tar.gz"],
)

load("@emsdk//:deps.bzl", emsdk_deps = "deps")
Expand All @@ -124,9 +124,9 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
# xnnpack used for fast vectorized wasm operations
git_repository(
name = "xnnpack",
commit = "5e8033a72a8d0f1c2b1f06e29137cc697c6b661d",
commit = "fa94f297e58c1e139ca64f78786df3744f557819",
remote = "https://github.com/google/XNNPACK.git",
shallow_since = "1643627844 -0800",
shallow_since = "1683912990 -0700",
)

# The libraries below are transitive dependencies of XNNPACK that we need to
Expand All @@ -136,10 +136,10 @@ git_repository(
http_archive(
name = "FP16",
build_file = "@xnnpack//third_party:FP16.BUILD",
sha256 = "0d56bb92f649ec294dbccb13e04865e3c82933b6f6735d1d7145de45da700156",
strip_prefix = "FP16-3c54eacb74f6f5e39077300c5564156c424d77ba",
sha256 = "e66e65515fa09927b348d3d584c68be4215cfe664100d01c9dbc7655a5716d70",
strip_prefix = "FP16-0a92994d729ff76a58f692d3028ca1b64b145d91",
urls = [
"https://github.com/Maratyszcza/FP16/archive/3c54eacb74f6f5e39077300c5564156c424d77ba.zip",
"https://github.com/Maratyszcza/FP16/archive/0a92994d729ff76a58f692d3028ca1b64b145d91.zip",
],
)

Expand All @@ -156,61 +156,35 @@ http_archive(
# pthreadpool library, used for parallelization
http_archive(
name = "pthreadpool",
sha256 = "8461f6540ae9f777ce20d1c0d1d249e5e61c438744fb390c0c6f91940aa69ea3",
strip_prefix = "pthreadpool-545ebe9f225aec6dca49109516fac02e973a3de2",
sha256 = "e6370550a1abf1503daf3c2c196e0a1c2b253440c39e1a57740ff49af2d8bedf",
strip_prefix = "pthreadpool-43edadc654d6283b4b6e45ba09a853181ae8e850",
urls = [
"https://github.com/Maratyszcza/pthreadpool/archive/545ebe9f225aec6dca49109516fac02e973a3de2.zip",
],
)

# clog library, used for logging
http_archive(
name = "clog",
build_file = "@xnnpack//third_party:clog.BUILD",
sha256 = "3f2dc1970f397a0e59db72f9fca6ff144b216895c1d606f6c94a507c1e53a025",
strip_prefix = "cpuinfo-d5e37adf1406cf899d7d9ec1d317c47506ccb970",
urls = [
"https://github.com/pytorch/cpuinfo/archive/d5e37adf1406cf899d7d9ec1d317c47506ccb970.tar.gz",
"https://github.com/Maratyszcza/pthreadpool/archive/43edadc654d6283b4b6e45ba09a853181ae8e850.zip",
],
)

# cpuinfo library, used for detecting processor characteristics
http_archive(
name = "cpuinfo",
build_file = "@xnnpack//third_party:cpuinfo.BUILD",
patches = ["@xnnpack//third_party:cpuinfo.patch"],
sha256 = "a7f9a188148a1660149878f737f42783e72f33a4f842f3e362fee2c981613e53",
strip_prefix = "cpuinfo-ed8b86a253800bafdb7b25c5c399f91bff9cb1f3",
sha256 = "ba668f9f8ea5b4890309b7db1ed2e152aaaf98af6f9a8a63dbe1b75c04e52cb9",
strip_prefix = "cpuinfo-3dc310302210c1891ffcfb12ae67b11a3ad3a150",
urls = [
"https://github.com/pytorch/cpuinfo/archive/ed8b86a253800bafdb7b25c5c399f91bff9cb1f3.zip",
"https://github.com/pytorch/cpuinfo/archive/3dc310302210c1891ffcfb12ae67b11a3ad3a150.zip",
],
)

# psimd library, used for fallback 128-bit SIMD micro-kernels
# Google Test framework, used by most unit-tests.
http_archive(
name = "psimd",
build_file = "@xnnpack//third_party:psimd.BUILD",
sha256 = "dc615342bcbe51ca885323e51b68b90ed9bb9fa7df0f4419dbfa0297d5e837b7",
strip_prefix = "psimd-072586a71b55b7f8c584153d223e95687148a900",
urls = [
"https://github.com/Maratyszcza/psimd/archive/072586a71b55b7f8c584153d223e95687148a900.zip",
],
)

git_repository(
name = "com_google_googletest",
commit = "cd17fa2abda2a2e4111cdabd62a87aea16835014",
remote = "https://github.com/google/googletest.git",
shallow_since = "1570558426 -0400",
sha256 = "5cb522f1427558c6df572d6d0e1bf0fd076428633d080e88ad5312be0b6a8859",
strip_prefix = "googletest-e23cdb78e9fef1f69a9ef917f447add5638daf2a",
urls = ["https://github.com/google/googletest/archive/e23cdb78e9fef1f69a9ef917f447add5638daf2a.zip"],
)

http_archive(
name = "rules_cc",
sha256 = "90d5a66950b492cbf86201cdc49c4b59796a85a4eb9fd63c07afe5f7132ea623",
strip_prefix = "rules_cc-8346df34b6593b051403b8e429db15c7f4ead937",
urls = [
"https://github.com/bazelbuild/rules_cc/archive/8346df34b6593b051403b8e429db15c7f4ead937.zip",
],
strip_prefix = "rules_cc-main",
urls = ["https://github.com/bazelbuild/rules_cc/archive/main.zip"],
)

http_archive(
Expand Down
4 changes: 2 additions & 2 deletions tfjs-backend-wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"jsdelivr": "dist/tf-backend-wasm.min.js",
"miniprogram": "dist/miniprogram",
"scripts": {
"build": "bazel build -c opt :tfjs-backend-wasm_pkg",
"build": "bazel build -c opt --config=xnnpack-opt :tfjs-backend-wasm_pkg",
"build-dbg": "bazel build -c dbg :tfjs-backend-wasm_pkg",
"publish-npm": "bazel run -c opt :tfjs-backend-wasm_pkg.publish",
"publish-npm": "bazel run -c opt --config=xnnpack-opt :tfjs-backend-wasm_pkg.publish",
"clean": "bazel clean --expunge",
"cpplint": "./scripts/cpplint.js",
"test": "bazel test -c dbg :tests --test_output=all",
Expand Down
1 change: 1 addition & 0 deletions tfjs-backend-wasm/src/cc/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const size_t num_tensors();
extern size_t xnn_operator_count;

extern pthreadpool *threadpool;

} // namespace backend

namespace wasm {
Expand Down
3 changes: 2 additions & 1 deletion tfjs-backend-wasm/src/cc/batch_mat_mul_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ void xnn_matmul(const size_t a_id, const size_t* a_shape_ptr,
const uint32_t flags = XNN_FLAG_TRANSPOSE_WEIGHTS;
xnn_status status = xnn_create_fully_connected_nc_f32(
input_channels, output_channels, input_stride, output_stride, b_buf,
bias_buf, output_min, output_max, flags, &fully_connected_op);
bias_buf, output_min, output_max, flags,
/*code_cache=*/nullptr, /*weights_cache=*/nullptr, &fully_connected_op);
if (status != xnn_status_success) {
tfjs::util::warn(
"XNN status for xnn_create_fully_connected_nc_f32 is not successful. "
Expand Down
2 changes: 1 addition & 1 deletion tfjs-backend-wasm/src/cc/conv2d_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void conv2d(const size_t x_id, const size_t batch_size,
stride_height, stride_width, dilation_height, dilation_width, groups,
group_input_channels, group_output_channels, input_pixel_stride,
output_pixel_stride, filter_xnn, bias_buf, output_min, output_max,
flags, &conv2d_op);
flags, /*code_cache=*/nullptr, /*weights_cache=*/nullptr, &conv2d_op);
if (status != xnn_status_success) {
util::warn(
"XNN status for xnn_create_convolution2d_nhwc_f32 is not successful. "
Expand Down
5 changes: 3 additions & 2 deletions tfjs-backend-wasm/src/cc/prelu_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ void prelu(const float* x_buf, const size_t x_size, const size_t weights_id,
const size_t strides = channels;

const uint32_t flags = 0;
xnn_status status = xnn_create_prelu_nc_f32(channels, strides, strides,
weights_buf, flags, &prelu_op);
xnn_status status = xnn_create_prelu_nc_f32(
channels, strides, strides, weights_buf, flags,
/*code_cache=*/nullptr, /*weights_cache=*/nullptr, &prelu_op);
if (status != xnn_status_success) {
util::warn(
"XNN status for xnn_create_prelu_nc_f32 is not successful. Got "
Expand Down

0 comments on commit c66f302

Please sign in to comment.