rhasspy / piper

A fast, local neural text to speech system
https://rhasspy.github.io/piper-samples/
MIT License
4.57k stars 315 forks source link

Running on M1 mac #27

Open fivestones opened 1 year ago

fivestones commented 1 year ago

Is there any chance this would run on Apple silicon M1 or M2? I Have an M1. I first tried the raspberry pi binary but it didn't run. Then I tried compiling from source, but for some reason I get the error that it espeak-ng is not available. I have espeak-ng installed and can use it from the command line, but when trying to compile piper it don't find it for some reason. I installed espeak-ng through mac ports

Has anyone done this? Do you have any pointers for me?

This runs fine for me on the raspberry pi, but I was hoping to get it running and have faster tts on the apple silicon.

Thanks!

synesthesiam commented 1 year ago

Have you tried installing pkgconfig with Mac Ports as well? The CMake build script uses it to locate espeak-ng. An alternative would be to hard-code the include/lib path into CMakeLists.txt.

Does anyone have experience building CMake apps in Docker for Mac?

fivestones commented 1 year ago

I was able to get it to build on my M1 MacBook Pro (...with a bunch of pasting the make errors into chatgpt, following its instructions, rinsing and repeating with new errors!)

Here's what I did in the end: git clone https://github.com/rhasspy/piper.git cd piper sudo port install espeak-ng to install espeak-ng sudo port install pkgconfig to install pkgconfig brew install pkg-config to install pkg-config--just was trying to get it working but I'm guessing this wasn't necessary which espeak-ng which showed /opt/local/bin/espeak-ng export PKG_CONFIG_PATH="/opt/local/lib/pkgconfig:$PKG_CONFIG_PATH" to add the espeak-ng directory to pkgconfig's path brew install onnxruntime brew list onnxruntime which showed

/opt/homebrew/Cellar/onnxruntime/1.14.1/include/onnxruntime/ (80 files)
/opt/homebrew/Cellar/onnxruntime/1.14.1/lib/libonnxruntime.1.14.1.dylib
/opt/homebrew/Cellar/onnxruntime/1.14.1/lib/pkgconfig/libonnxruntime.pc
/opt/homebrew/Cellar/onnxruntime/1.14.1/lib/libonnxruntime.dylib

export CMAKE_PREFIX_PATH="/opt/homebrew/Cellar/onnxruntime/1.14.1:$CMAKE_PREFIX_PATH" --probably unnecessary since afterwards I was still getting the same error export CMAKE_INCLUDE_PATH="/opt/homebrew/Cellar/onnxruntime/1.14.1/include/onnxruntime:$CMAKE_INCLUDE_PATH" --probably also unnecessary export CMAKE_PREFIX_PATH="/opt/homebrew/Cellar/onnxruntime/1.14.1:$CMAKE_PREFIX_PATH" export CMAKE_PREFIX_PATH=/opt/homebrew/Cellar/onnxruntime/1.14.1/\ :/opt/homebrew/Cellar/onnxruntime/1.14.1:/opt/homebrew/Cellar/onnxruntime/1.14.1: export CMAKE_PREFIX_PATH="/opt/homebrew/Cellar/onnxruntime/1.14.1/include/onnxruntime/core/session:$CMAKE_PREFIX_PATH" --all of those last three lines didn't seem to fix anything so they are probably unnecessary Then:

cd src/cpp
nano CMakeLists.txt

Changed the line set(ONNXRUNTIME_ROOTDIR "/usr/local/include/onnxruntime") to set(ONNXRUNTIME_ROOTDIR "/opt/homebrew/Cellar/onnxruntime/1.14.1/") Added the following to the end of the CMakeLists.txt file:

include_directories(/opt/homebrew/Cellar/onnxruntime/1.14.1/include/onnxruntime)
include_directories(/opt/homebrew/Cellar/onnxruntime/1.14.1/include/onnxruntime/core/session)
link_directories(/opt/homebrew/Cellar/onnxruntime/1.14.1/lib)

Then nano synthesize.hpp Changed line 122 from Ort::OrtRelease(outputTensors[i].release()); to Ort::detail::OrtRelease(outputTensors[i].release()); and changed line 126 from Ort::OrtRelease(inputTensors[i].release()); to Ort::detail::OrtRelease(inputTensors[i].release()); then again nano CMakeLists.txt and changed line 33 from -static-libgcc -static-libstdc++ to -static-libstdc++ Then when running make I still got a few warnings (below)

mkdir -p build
cd build && cmake ../src/cpp -DCMAKE_BUILD_TYPE=Release && make
-- Configuring done (0.1s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/david/development/piper/build
[ 50%] Building CXX object CMakeFiles/piper.dir/main.cpp.o
clang: warning: -Wl,-rpath,RIGIN: 'linker' input unused [-Wunused-command-line-argument]
In file included from /Users/david/development/piper/src/cpp/main.cpp:15:
In file included from /Users/david/development/piper/src/cpp/piper.hpp:13:
/Users/david/development/piper/src/cpp/model.hpp:48:8: warning: unused variable 'loadDuration' [-Wunused-variable]
  auto loadDuration = chrono::duration<double>(endTime - startTime);
       ^
1 warning generated.
[100%] Linking CXX executable piper
clang: warning: argument unused during compilation: '-static-libstdc++' [-Wunused-command-line-argument]
[100%] Built target piper

...but it built! And it worked! The binary is in the build directory.

It's about 28 times faster than running on my RPi 4 with 4 GB ram

I'm not sure if any of the files could be updated in the repository so they will just work when compiling for M1, but anyway, that's what worked for me.

pachacamac commented 1 year ago

@fivestones thank you for the step by step guide! I followed it and still get a

piper/src/cpp/phonemize.hpp:69:9: error: use of undeclared identifier 'espeak_TextToPhonemes2'
        espeak_TextToPhonemes2((const void **)&inputTextPointer,

error. Did you encounter this too? Seems like your guide is also a bit outdated by now since the steps you describe for synthesize.hpp are already there by default.

shen2009fei commented 1 year ago

@fivestones thank you for the step by step guide! I followed it and still get a

piper/src/cpp/phonemize.hpp:69:9: error: use of undeclared identifier 'espeak_TextToPhonemes2'
        espeak_TextToPhonemes2((const void **)&inputTextPointer,

error. Did you encounter this too? Seems like your guide is also a bit outdated by now since the steps you describe for synthesize.hpp are already there by default.

I got the same error. Did you solve this problem?

fivestones commented 1 year ago

@pachacamac @shen2009fei I haven't tried anything else with this but honestly I just told chatgpt what I was trying to do, and each time I got an error I pasted the error in the chatgpt window and asked it what I should do. That got me 95% of the way there. A few things I figured out on my own but maybe try doing the same for the error you guys are getting? I didn't get that error.

rogeriochaves commented 1 year ago

thanks @fivestones! I followed your instructions, could compile successfully! But when I tried to run I got this:

> ./build/piper --model models/voice-en-us-amy-low/en-us-amy-low.onnx "hi"
libc++abi: terminating due to uncaught exception of type std::__1::__fs::filesystem::filesystem_error: filesystem error: in canonical: No such file or directory ["/proc/self/exe"] [""]

I asked GPT-4 to help me out, and it produced the code that fixes it. On the file src/cpp/main.cpp, before this part:

#else
  auto exePath = filesystem::canonical("/proc/self/exe");
#endif

add a condition for the mac:

#elif __APPLE__
  auto exePath = []() {
    char path[PATH_MAX];
    uint32_t size = sizeof(path);
    if (_NSGetExecutablePath(path, &size) == 0)
      return filesystem::canonical(path);
    else
      throw std::runtime_error("_NSGetExecutablePath failed");
  }();

Then make again and success :D

rudolfolah commented 11 months ago

Tried out the instructions here, I brew install for quite a few things and added the onnxruntime-related stuff to CMakeLists.txt. Unfortunately ran into this issue:

rudolfo@ESP_6DF8AC piper % make
mkdir -p build
cd build && cmake ../src/cpp -DCMAKE_BUILD_TYPE=Release && make
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/rudolfo/Workspace/piper/build
[ 33%] Building CXX object CMakeFiles/piper.dir/main.cpp.o
clang: warning: -Wl,-rpath,RIGIN: 'linker' input unused [-Wunused-command-line-argument]
In file included from /Users/rudolfo/Workspace/piper/src/cpp/main.cpp:27:
/Users/rudolfo/Workspace/piper/src/cpp/piper.hpp:11:10: fatal error: 'phoneme_ids.hpp' file not found
#include <phoneme_ids.hpp>
         ^~~~~~~~~~~~~~~~~
1 error generated.
make[3]: *** [CMakeFiles/piper.dir/main.cpp.o] Error 1
make[2]: *** [CMakeFiles/piper.dir/all] Error 2
make[1]: *** [all] Error 2
make: *** [piper] Error 2
synesthesiam commented 11 months ago

The build now depends on piper-phonemize, which will need to be built on the Mac as well. With that, you can see how the build processed in Docker.

Is it possible to build for Mac inside docker buildx?

rudolfolah commented 11 months ago

@synesthesiam ahh I see! I was able to get the docker build working, worked splendidly, just opened a PR to add instructions for that: https://github.com/rhasspy/piper/pull/109

manumao commented 9 months ago

will this work on a mac w/ Intel i5?

althink commented 8 months ago

I was able to build/modify it to run by myself on M1 Pro before I found this ticket. The issue I have is with training. Looks like there is a bug in pytorch < 2.0 on MPS devices:

File "/Users/krzysiek/Projects/ai/piper/src/python/piper_train/vits/attentions.py", line 299, in _get_relative_embeddings padded_relative_embeddings = F.pad( IndexError: Dimension out of range (expected to be in range of [-3, 2], but got 3)

They fixed it in some places but still can happen in 1.13.1 https://github.com/pytorch/pytorch/issues/89624

Is there any chance piper_train will be ported to torch/pytorch-lightning 2.0?

The most I was able to get was rewriting args handling and supporting multiple optimizers (it changed in 2.0 though I had no idea what I was doing ;) )

The last issue I got was: https://github.com/pytorch/pytorch/issues/78044