XapaJIaMnu / translateLocally

Fast and secure translation on your local machine, powered by marian and Bergamot.
MIT License
501 stars 28 forks source link

Compiling for Arm64 on Debian #174

Open boroli opened 1 week ago

boroli commented 1 week ago

I wanted to use translateLocally on my PinePhone, which is running mobian and has a Cortex-a53 CPU. I couldn't compile translateLocally directly on my phone, instead I set up a qemu VM that way:

wget -c https://cloud.debian.org/images/cloud/trixie/daily/latest/debian-13-nocloud-arm64-daily.qcow2
qemu-img resize debian-13-nocloud-arm64-daily.qcow2 +10G

The nocloud image has the default user root without password.

Boot into the Qemu VM:

qemu-system-aarch64 -m 8G -cpu cortex-a53 -smp 4 -machine virt -nographic -vga none -bios /usr/share/qemu-efi-aarch64/QEMU_EFI.fd -drive if=none,file=debian-13-nocloud-arm64-daily.qcow2,id=hd0 -device virtio-blk-device,drive=hd0 -device virtio-net-device,netdev=net0 -netdev user,hostfwd=tcp:127.0.0.1:2222-:22,id=net0

Make sure you select -cpu cortex-a53. If you boot with -cpu cortex-a72 translateLocally will compile, but will use instruction sets, which are not available on the native cortex-a53 CPU on the PinePhone. You can check and compare the instructions by:

mobian@mobian:~$ for t in param target; do cmd="gcc -Q -O2 --help=$t"; diff -U0 <(LANG=C $cmd) <(LANG=C $cmd -march=native); done
--- /dev/fd/63  2024-09-25 13:43:43.498193423 +0200
+++ /dev/fd/62  2024-09-25 13:43:43.506193512 +0200
@@ -3 +3 @@
-  -march=                              armv8-a
+  -march=                              armv8-a+crypto+crc

and on a Cortex-A72:

for t in param target; do cmd="gcc -Q -O2 --help=$t"; diff -U0 <(LANG=C $cmd) <(LANG=C $cmd -march=native); done
--- /dev/fd/63  2024-09-24 20:43:02.773358011 +0000
+++ /dev/fd/62  2024-09-24 20:43:02.809357468 +0000
@@ -3 +3 @@
-  -march=                              armv8-a
+  -march=                              armv8-a+crypto+crc+lse+rcpc+rdma+fp16fml+rng+sb+ssbs+sve2-sm4+sve2-aes+sve2-sha3+sve2-bitperm+i8mm+bf16+flagm+pauth

You can check the emulated CPU with lscpu.

If you use the Qemu VM -cpu max, then translateLocally won't compile and will fail with the following error:

/tmp/ccySen0g.s: Assembler messages:
/tmp/ccySen0g.s:14442: Error: selected processor does not support `eor3 v28.16b,v19.16b,v22.16b,v25.16b'
/tmp/ccySen0g.s:14456: Error: selected processor does not support `eor3 v5.16b,v19.16b,v1.16b,v4.16b'
/tmp/ccySen0g.s:14469: Error: selected processor does not support `eor3 v30.16b,v19.16b,v24.16b,v27.16b'
/tmp/ccySen0g.s:14482: Error: selected processor does not support `eor3 v5.16b,v19.16b,v2.16b,v6.16b'
/tmp/ccySen0g.s:14495: Error: selected processor does not support `eor3 v3.16b,v19.16b,v26.16b,v29.16b'
/tmp/ccySen0g.s:14508: Error: selected processor does not support `eor3 v5.16b,v19.16b,v20.16b,v6.16b'
make[2]: *** [3rd_party/bergamot-translator/3rd_party/marian-dev/src/3rd_party/sentencepiece/src/CMakeFiles/sentencepiece_train-static.dir/build.make:132: 3rd_party/bergamot-translator/3rd_party/marian-dev/src/3rd_party/sentencepiece/src/CMakeFiles/sentencepiece_train-static.dir/trainer_interface.cc.o] Error 1

At the first boot of the VM, you need to extend the file system, before you can install all dependencies:

apt update
apt install cloud-guest-utils
growpart /dev/vda 1
reboot

Then install all dependencies:

apt install libxkbcommon-x11-dev libvulkan-dev libgl1-mesa-dev qt6-base-dev qt6-base-dev-tools qt6-tools-dev qt6-tools-dev-tools qt6-l10n-tools qt6-translations-l10n libqt6svg6-dev libarchive-dev libpcre2-dev git cmake g++ libruy-dev

The package libpcre++-dev is no longer available, but apparently also not required anymore. Since Debian Trixie and Ubuntu 24.04 the package libruy-dev is already available as deb package, but I didn't figure out how to link against the system package, instead of building its own 3rd-party package of libruy. Maybe with the system package, the Ruy hack around the inclusion of header files can be omitted.

The building translateLocally is straightforward:

git clone --recurse-submodules https://github.com/XapaJIaMnu/translateLocally.git
cd translateLocally
mkdir build
cd  build
cmake ..
cmake/fix_ruy_build.sh .. .
make -j 4

Copy the resulting binary, as well as logo and desktop file onto the PinePhone, and into the right places, assuming ~/bin/ is in your $PATH directory:

mv translateLocally ~/bin/
mv dist/translateLocally.desktop ~/.local/share/applications/
mv logo/translateLocally_logo.* ~/.icons/

And install missing dependencies on the PinePhone:

sudo apt install libqt6network6 libqt6svg6 libqt6network6 libqt6widgets6 qt6-wayland

This process also works on Bookworm, should you still run bookworm on your PinePhone.

And with that offline translations (in case you don't have mobile reception) is also working natively on the PinePhone now:

Screenshot_20240925-221822