facebookresearch / audiocraft

Audiocraft is a library for audio processing and generation with deep learning. It features the state-of-the-art EnCodec audio compressor / tokenizer, along with MusicGen, a simple and controllable music generation LM with textual and melodic conditioning.
MIT License
20.71k stars 2.11k forks source link

Install: "No module named torch"? ....But I do have torch #105

Closed drscotthawley closed 1 year ago

drscotthawley commented 1 year ago

I have torch 2.0.1 installed. In the same environment that I'm running pip in.

$ pip list | grep torch
torch             2.0.1

But running pip install -U audiocraft ends up producing the error...

        File "<string>", line 23, in <module>
      ModuleNotFoundError: No module named 'torch'
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

Seems to be part of installing xformers-0.0.20.

Collecting xformers (from audiocraft)
  Using cached xformers-0.0.20.tar.gz (7.6 MB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error
...

Some person in the xformers GitHub issues suggested that "xformers doesn't run on Mac". But there's nothing in the documentation that says that.

Could the README for audiocraft clarify whether us Mac users are doomed?

Vargol commented 1 year ago

Alternatively could the project provide an alternative to xformers usage where xformers is not strictly required

Vargol commented 1 year ago

xformers_patch.txt

@drscotthawley Here's a patch to hack xformers out. It only had minimal testing as I've only got a 8mb M1 so the medley model was still running after an hour and I can't get torch audio working to test the melody conditioned and extension stuff but it has generated text conditioned tunes with the small model The main downside is it takes out memory efficient cross-attention, I tried leaving it using the torch version instead of xformers but that produced rubbish for me.

drscotthawley commented 1 year ago

@Vargol OMG! Wonderful! Excellent. That works.

I'm also finding that textwrap is undefined as a separate package when I try to run pip, (in requirements.txt), but is automatically included in Python (3.9.17), so commenting out that line of requirements.txt and applying your patch worked!

Closing.

phdhorse41 commented 1 year ago

xformers_patch.txt

@drscotthawley Here's a patch to hack xformers out. It only had minimal testing as I've only got a 8mb M1 so the medley model was still running after an hour and I can't get torch audio working to test the melody conditioned and extension stuff but it has generated text conditioned tunes with the small model The main downside is it takes out memory efficient cross-attention, I tried leaving it using the torch version instead of xformers but that produced rubbish for me.

Hi, I use Windows and I have this problem as well... Where should I put this .txt file into?

Vargol commented 1 year ago

it's a patch, but it may be very out of date I can see a lot of changes to the code since so its probably broken.

if to cloned the repo you can use git patch to apply it. Patches are basically diff output if you can read that you can apply the changed manually.

I can see in the code reference to torch memory efficient attention so they might have reworked the code enough that chunks of the patch are irrelevant.

Vargol commented 1 year ago

@phdhorse41

I revised what needs to be done. main is broken ATM so you need to checkout tag 0.0.2 or use the code in release 0.0.2

in file requirements.txt remove the xformers line and then setup audiocraft as per the docs .

in the file audiocraft/modules/transformer.py

comment out or delete the xformers import

#from xformers import ops

around line 177 hard code self.memory_efficient to be false

so
self.memory_efficient = memory_efficient becomes self.memory_efficient = False

around line 189 remove or comment out the memory_efficient check

#        if memory_efficient:
#            _verify_xformers_memory_efficient_compat()

Around line 372 there's a call to unbind change the model from ops to torch

q, k, v = ops.unbind(packed, dim=2) to q, k, v = torch.unbind(packed, dim=2)

And that should work, but use more memory. I tried keeping the torch efficient attention working but it produced garbage :-( I suspect there still some xformers specific stuff in there somewhere, but it needs someone that knows what the code is supposed to be doing to sort that out.

FinnS928 commented 1 year ago

Hi, I'm having a similar issue on M1 Mac. I think everything necessary is installed, but it doesn't work and says the same thing as in this threads original problem. Do you know how I can fix this? Thank you!

Vargol commented 1 year ago

Download tag 0.0.2 https://github.com/facebookresearch/audiocraft/releases/tag/v0.0.2 edit requirements.txt to remove the xformers line install from root directory (where requirements.txt is) pip install -e . Edit the files as stated in https://github.com/facebookresearch/audiocraft/issues/105#issuecomment-1665423023 and it should work

xts-bit commented 1 year ago

@Vargol Please help I'm on MacOS and getting that same error i already have torch but still that error is there any updated fix for this issues?

Vargol commented 1 year ago

I've not looked at this recently I'm afraid, there does seem to be some attempt to make it work without xformers but xformers is still in requirements.txt.

So you'll still need to download the code take xformers out of requirements.txt and run pip install -e . from the main directory where requirements.txt to install it in your venv and see if it works.

xts-bit commented 1 year ago

@Vargol Which code? My code is there in a folder named musicGen in which i have a file named main.py. What i need to do?

xts-bit commented 1 year ago

Any GitHub repo where i can get basic example to play with audiocraft

Vargol commented 1 year ago

Start Terminal Go to a directory to want to keep every thing in Terminal

git clone https://github.com/facebookresearch/audiocraft.git
cd audiocraft
python -m venv venv
. venv/bin/activate

Edit the requirements.txt to remove the xformers line

Now install audiocraft in the venv

pip install -e .

edit the file audiocraft/modules/transformer.py line 23 comment out the line from xformers import ops

line 178 self.memory_efficient = memory_efficient becomes self.memory_efficient = False

line 190 remove or comment out the memory_efficient check

#        if memory_efficient:
#            _verify_xformers_memory_efficient_compat()

line 373 q, k, v = ops.unbind(packed, dim=2) to q, k, v = torch.unbind(packed, dim=2)

So exactly as described in comment #150 except the lines have moved down by 1.

Vargol commented 1 year ago

There's examples in the README.md

https://github.com/facebookresearch/audiocraft/blob/main/docs/MUSICGEN.md

Basic example

import torchaudio
from audiocraft.models import MusicGen
from audiocraft.data.audio import audio_write

model = MusicGen.get_pretrained('facebook/musicgen-small')
model.set_generation_params(duration=8)  # generate 8 seconds.
descriptions = ['happy rock', 'energetic EDM', 'sad jazz']
wav = model.generate(descriptions)  # generates 3 samples.

for idx, one_wav in enumerate(wav):
    # Will save under {idx}.wav, with loudness normalization at -14 db LUFS.
    audio_write(f'{idx}', one_wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True)
xts-bit commented 1 year ago

@Vargol Thank you,I followed the steps now what to do i get this error when i run my file from audiocraft.data.audio import audio_write ModuleNotFoundError: No module named 'audiocraft.data'

Code

import streamlit as st
from audiocraft.data.audio import audio_write
from audiocraft.models import MusicGen

def music_gen(theme: str, duration: int):
    model = MusicGen.get_pretrained("small")
    model.set_generation_params(duration=duration)  # durationの設定で、musicの長さを設定

    # リストに生成するmusicのテキストを入力する
    # 同じ単語でも同じmusicを生成しない。
    descriptions = [theme]

    wav = model.generate(descriptions)

    for idx, one_wav in enumerate(wav):
        # Will save under {idx}.wav, with loudness normalization at -14 db LUFS.
        audio_write(f"{idx}", one_wav.cpu(), model.sample_rate, strategy="loudness")

def main():
    st.title("音楽生成アプリ")
    # テキストボックス
    with st.form(key="input_form"):
        # テーマ入力
        theme = st.text_input("テーマを入力してください。", "RPGの戦闘用BGM")
        # 長さ入力
        duration = st.number_input("音楽の長さを入力してください。(単位:ms)", 1, 24, 3)
        # 送信ボタン
        submit_button = st.form_submit_button(label="実行")

    if submit_button:
        music_gen(theme, duration)
        st.audio("0.wav")

if __name__ == "__main__":
    main()
Vargol commented 1 year ago

Are you running it from main directory from the git repo ? Is the venv still active, I think it nor being active is the the most likely

xts-bit commented 1 year ago

@Vargol My code is there in a folder named musicGen in which I have a file named main.py and a folder name audiocraft. I'm doing python3 main.py in the terminal in musicGen directory

Vargol commented 1 year ago

In my instruction I told you to do the following....

git clone https://github.com/facebookresearch/audiocraft.git cd audiocraft python -m venv venv . venv/bin/activate

That last to lines create a virtual environment where all the things didn't for audiocraft including the audiocraft python modules are installed, the last line 'switching it on' It needs to be switched on for audiocraft from python scripts. Make sure it is switched on and then run your script from the same terminal session.

xts-bit commented 1 year ago

@Vargol I followed these steps and can see a venv folder in my audiocraft folder, But can you please tell me where should i put my main.py file and run my code?

Vargol commented 1 year ago

It the venv is currently active if should work from anywhere as long as its in the same terminal session that the venv was activated, if not you need to activate the venv in that session by running . venv/bin/activate

xts-bit commented 1 year ago

Thanks, it runs the code but it error "ModuleNotFoundError: No module named 'soundfile'"

I'm just trying to generate music using Audiocraft and generate of audio file of that music so i can listen,

Can you please help me correct my code to make it work like that?

It also says

 Warning: to view this Streamlit app on a browser, run it with the following
  command:

    streamlit run main.py [ARGUMENTS]
Vargol commented 1 year ago

I'm sorry I don't know Streamlit I guess its a browser based GUI thing.

It you just want to test audiocraft, run the example code in the README.md or comment above, it'll generate three files wav files. 0.wav. 1.wav and 2.wav

xts-bit commented 1 year ago

@Vargol Thanks it works :)

xts-bit commented 1 year ago

@Vargol I get this error "ERROR: Package 'audiocraft' requires a different Python: 3.7.10 not in '>=3.8.0'" When i deploy the code on render.com my build command is "python3 -m venv venv && . venv/bin/activate && pip install -e ."

Any idea how can i fix?

Vargol commented 1 year ago

https://render.com/docs/python-version

xts-bit commented 1 year ago

I tried this but it says bash: gunicorn: command not found but i have pip install -e . in my build command and have gunicorn in my requirements.txt

my start command gunicorn app:app

Vargol commented 1 year ago

gunicorn isn't required for audiocraft as far as I can see, must be a render.com issue, try pip install gunicorn

xts-bit commented 1 year ago

Render keeps giving me the error

 File "/opt/render/project/src/main.py", line 1, in <module>
 from flask import Flask, request, jsonify
 ModuleNotFoundError: No module named 'flask'

my build command is python -m venv venv && . venv/bin/activate && pip install -e . && pip install flask && pip install firebase-rest-api

Is this issue of No module named torch is known to the creator of audiocraft? Will it be fixed in old version of python as said in the readme Python 3.9, PyTorch 2.0.0.