Skip to content

Commit

Permalink
fix issue in upstream cpuinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
cocoa-xu committed Mar 17, 2024
1 parent c91899a commit 5b94d9e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 17 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/linux-precompile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@ jobs:
target_abi: "gnueabihf"
cc: "/usr/local/bin/nerves_toolchain_armv6_nerves_linux_gnueabihf-linux_x86_64-13.2.0/bin/armv6-nerves-linux-gnueabihf-gcc"
cxx: "/usr/local/bin/nerves_toolchain_armv6_nerves_linux_gnueabihf-linux_x86_64-13.2.0/bin/armv6-nerves-linux-gnueabihf-g++"
# - pair:
# otp: "25.x"
# arch_name: riscv64
# cmake_toolchain_file: cc_toolchain/riscv64-nerves-linux-gnu.cmake
# c_compiler: riscv64-nerves-linux-gnu-gcc
# cpp_compiler: riscv64-nerves-linux-gnu-g++
# libedgetpu_library: "riscv64"
# target_arch: "riscv64"
# target_os: "linux"
# target_abi: "gnu"
# cc: "/usr/local/bin/nerves_toolchain_riscv64_nerves_linux_gnu-linux_x86_64-13.2.0/bin/riscv64-nerves-linux-gnu-gcc"
# cxx: "/usr/local/bin/nerves_toolchain_riscv64_nerves_linux_gnu-linux_x86_64-13.2.0/bin/riscv64-nerves-linux-gnu-g++"
- pair:
otp: "25.x"
arch_name: riscv64
cmake_toolchain_file: cc_toolchain/riscv64-nerves-linux-gnu.cmake
c_compiler: riscv64-nerves-linux-gnu-gcc
cpp_compiler: riscv64-nerves-linux-gnu-g++
libedgetpu_library: "riscv64"
target_arch: "riscv64"
target_os: "linux"
target_abi: "gnu"
cc: "/usr/local/bin/nerves_toolchain_riscv64_nerves_linux_gnu-linux_x86_64-13.2.0/bin/riscv64-nerves-linux-gnu-gcc"
cxx: "/usr/local/bin/nerves_toolchain_riscv64_nerves_linux_gnu-linux_x86_64-13.2.0/bin/riscv64-nerves-linux-gnu-g++"

steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ $(NATIVE_BINDINGS_SO): $(UNICODE_DATA) unarchive_source_code install_libedgetpu_
git clone --depth 1 https://github.com/google/glog.git 3rd_party/glog ; \
fi && \
mkdir -p $(CMAKE_BINDINGS_BUILD_DIR) && \
python3 "$(shell pwd)/patches/apply_patch.py" "$(TFLITE_ROOT_DIR)" "$(TFLITE_VER)" && \
python3 "$(shell pwd)/patches/apply_patch.py" "$(TENSORFLOW_ROOT_DIR)" "$(TFLITE_VER)" && \
cd "$(CMAKE_BINDINGS_BUILD_DIR)" && \
cmake -D C_SRC="$(C_SRC)" \
-D PRIV_DIR="$(PRIV_DIR)" \
Expand Down
67 changes: 63 additions & 4 deletions patches/apply_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ def patch_fix_RoundToNearest(tf_version: str, tf_src_root: str):
print(f"warning: skip applying `patch_fix_RoundToNearest` to tf version `{tf_version}`")
return

# kernels/internal/optimized/neon_tensor_utils.cc
neon_tensor_utils_cc = Path(tf_src_root) / 'kernels' / 'internal' / 'optimized' / 'neon_tensor_utils.cc'
# tensorflow/lite/kernels/internal/optimized/neon_tensor_utils.cc
tf_lite_src_root = Path(tf_src_root) / 'tensorflow' / 'lite'
neon_tensor_utils_cc = Path(tf_lite_src_root) / 'kernels' / 'internal' / 'optimized' / 'neon_tensor_utils.cc'
fixed = StringIO()
patched_1 = False
skip_next_line = False
Expand Down Expand Up @@ -43,7 +44,8 @@ def patch_compiling_telemetry_cc(tf_version: str, tf_src_root: str):
print(f"warning: skip applying `patch_compiling_telemetry_cc` to tf version `{tf_version}`")
return

cmakelists_txt = Path(tf_src_root) / 'CMakeLists.txt'
tf_lite_src_root = Path(tf_src_root) / 'tensorflow' / 'lite'
cmakelists_txt = Path(tf_lite_src_root) / 'CMakeLists.txt'
fixed = StringIO()
patched_1 = False
with open(cmakelists_txt, 'r') as source:
Expand All @@ -62,7 +64,64 @@ def patch_compiling_telemetry_cc(tf_version: str, tf_src_root: str):
dst.write(fixed.getvalue())


patches = [patch_fix_RoundToNearest, patch_compiling_telemetry_cc]
def patch_cpuinfo_riscv64_sys_hwprobe(tf_version: str, tf_src_root: str):
if tf_version not in ['2.16.0', '2.16.1']:
print(f"warning: skip applying `patch_cpuinfo_riscv64_sys_hwprobe` to tf version `{tf_version}`")
return

# tensorflow/lite/tools/cmake/modules/cpuinfo.cmake
# tensorflow/workspace2.bzl
tf_lite_src_root = Path(tf_src_root) / 'tensorflow' / 'lite'
cpuinfo_cmake = Path(tf_lite_src_root) / 'tools' / 'cmake' / 'modules' / 'cpuinfo.cmake'
fixed = StringIO()
patched_1 = False
lines_to_skip = 0
with open(cpuinfo_cmake, 'r') as source:
for line in source:
line_strip = line.strip()
if not patched_1 and line_strip == '# Sync with tensorflow/third_party/cpuinfo/workspace.bzl':
fixed.write(f" {line_strip} # fixed\n")
fixed.write(" GIT_TAG 6543fec09b2f04ac4a666882998b534afc9c1349\n")
patched_1 = True
lines_to_skip = 1
else:
if lines_to_skip > 0:
lines_to_skip -= 1
continue
fixed.write(line)

if patched_1:
with open(cpuinfo_cmake, 'w') as dst:
dst.truncate(0)
dst.write(fixed.getvalue())

workspace2_bzl = Path(tf_src_root) / 'tensorflow' / 'workspace2.bzl'
fixed = StringIO()
patched_2 = False
with open(workspace2_bzl, 'r') as source:
for line in source:
line_strip = line.strip()
if not patched_2 and line_strip == 'name = "cpuinfo",':
fixed.write(' name = "cpuinfo", # fixed\n')
fixed.write(' strip_prefix = "cpuinfo-6543fec09b2f04ac4a666882998b534afc9c1349",\n')
fixed.write(' sha256 = "17180581df58b811ef93cfafd074598966a185f48e5a574e8947ca51419f7ca6",\n')
fixed.write(' urls = tf_mirror_urls("https://github.com/pytorch/cpuinfo/archive/6543fec09b2f04ac4a666882998b534afc9c1349.zip"),\n')
patched_2 = True
lines_to_skip = 3
else:
if lines_to_skip > 0:
lines_to_skip -= 1
continue
fixed.write(line)

if patched_2:
with open(workspace2_bzl, 'w') as dst:
dst.truncate(0)
dst.write(fixed.getvalue())



patches = [patch_fix_RoundToNearest, patch_compiling_telemetry_cc, patch_cpuinfo_riscv64_sys_hwprobe]

if __name__ == '__main__':
tf_version = None
Expand Down

0 comments on commit 5b94d9e

Please sign in to comment.