From 0273be02327580a6bfd159d5967ec9812a0f386a Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Wed, 17 Aug 2022 22:32:58 +0300 Subject: [PATCH 1/4] Exclude unused libs under torch/lib. Reduce Debian package size by 700Mb - libtorch_cuda.so (1Gb) and libtorch_cpu.so (700Mb) are large shared libs that are available at package root and under torch/lib. - The top level imports are used, so they unused libs are removed from package - This reduces the single file package size from 1.6Gb to 920Mb --- Khoj.spec | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/Khoj.spec b/Khoj.spec index 3697f32d..7df8b79b 100644 --- a/Khoj.spec +++ b/Khoj.spec @@ -1,4 +1,6 @@ # -*- mode: python ; coding: utf-8 -*- +from os.path import join +from platform import system from PyInstaller.utils.hooks import copy_metadata datas = [('src/interface/web', 'src/interface/web')] @@ -29,6 +31,13 @@ a = Analysis( cipher=block_cipher, noarchive=False, ) + +# Filter out unused, duplicate shared libs under torch/lib +torch_lib_path = set([ + join('torch', 'lib', 'libtorch_cuda.so'), + join('torch', 'lib', 'libtorch_cpu.so')]) +a.datas = [entry for entry in a.datas if not entry[0] in torch_lib_path] + pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) exe = EXE( @@ -53,9 +62,11 @@ exe = EXE( entitlements_file=None, icon='src/interface/web/assets/icons/favicon.icns', ) -app = BUNDLE( - exe, - name='Khoj.app', - icon='src/interface/web/assets/icons/favicon.icns', - bundle_identifier=None, -) + +if system() == 'Darwin': + app = BUNDLE( + exe, + name='Khoj.app', + icon='src/interface/web/assets/icons/favicon.icns', + bundle_identifier=None, + ) From 7cf345a1386fc30d5ccb16e4229d84c5ca945185 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Wed, 17 Aug 2022 22:59:01 +0300 Subject: [PATCH 2/4] Exclude unused mac libs under torch/lib. Reduce Mac app size by 30Mb libtorch_cuda only seems to be imported in Linux. Which is why the size of the Mac, Windows apps are 700Mb smaller than the Debian app size. Guessing this is because libtorch_cuda only works on Linux machines? Anyway, removing libtorch_{cpu,python}.dylib under torch/lib from the Mac app reduces it's size from 190Mb to 160Mb. 15% reduction isn't too bad --- Khoj.spec | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Khoj.spec b/Khoj.spec index 7df8b79b..56d164e7 100644 --- a/Khoj.spec +++ b/Khoj.spec @@ -35,7 +35,11 @@ a = Analysis( # Filter out unused, duplicate shared libs under torch/lib torch_lib_path = set([ join('torch', 'lib', 'libtorch_cuda.so'), - join('torch', 'lib', 'libtorch_cpu.so')]) + join('torch', 'lib', 'libtorch_cuda.dylib'), + join('torch', 'lib', 'libtorch_cpu.so'), + join('torch', 'lib', 'libtorch_cpu.dylib'), + join('torch', 'lib', 'libtorch_python.so'), + join('torch', 'lib', 'libtorch_python.dylib')]) a.datas = [entry for entry in a.datas if not entry[0] in torch_lib_path] pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher) From 9ee02b0804cdf40ec4c50948c54415da182cac53 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Wed, 17 Aug 2022 23:04:26 +0300 Subject: [PATCH 3/4] Add --noconfirm in call to pyinstaller from Github release workflow Added just for safety, workflow works fine without it too --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0a672e37..3c22ecec 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -51,7 +51,7 @@ jobs: export PYTHONHASHSEED=42 export SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct - pyinstaller Khoj.spec + pyinstaller --noconfirm Khoj.spec if [ "$RUNNER_OS" == "Windows" ]; then mv dist/Khoj.exe dist/khoj_"$GITHUB_REF_NAME"_amd64.exe fi From d25ddb93f78dafb0be3c002b6f147184804253e9 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Wed, 17 Aug 2022 23:17:27 +0300 Subject: [PATCH 4/4] Fix missing closing bracket from SOURCE_DATE_EPOCH def in release.yml --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3c22ecec..88ef1adf 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -49,7 +49,7 @@ jobs: run: | # Setup Environment for Reproducible Builds export PYTHONHASHSEED=42 - export SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct + export SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct) pyinstaller --noconfirm Khoj.spec if [ "$RUNNER_OS" == "Windows" ]; then