tianrengao / SqueezeWave

Other
254 stars 50 forks source link

Running in CPU #6

Open alokprasad opened 4 years ago

alokprasad commented 4 years ago

Looks like Apex library needs CUDA, when i without cuda , getting below error. any idea.

args.sampling_rate, args.is_fp16, args.denoiser_strength)

File "inference.py", line 48, in main squeezewave, _ = amp.initialize(squeezewave, [], opt_level="O3") File "/usr/local/lib/python3.6/dist-packages/apex-0.1-py3.6.egg/apex/amp/frontend.py", line 358, in initialize return _initialize(models, optimizers, _amp_state.opt_properties, num_losses, cast_model_outputs) File "/usr/local/lib/python3.6/dist-packages/apex-0.1-py3.6.egg/apex/amp/_initialize.py", line 171, in _initialize check_params_fp32(models) File "/usr/local/lib/python3.6/dist-packages/apex-0.1-py3.6.egg/apex/amp/_initialize.py", line 93, in check_params_fp32 name, param.type())) File "/usr/local/lib/python3.6/dist-packages/apex-0.1-py3.6.egg/apex/amp/_amp_state.py", line 32, in warn_or_err raise RuntimeError(msg) RuntimeError: Found param WN.0.in_layers.0.0.weight with type torch.FloatTensor, expected torch.cuda.FloatTensor. When using amp.initialize, you need to provide a model with parameters located on a CUDA device before passing it no matter what optimization level you chose. Use model.to('cuda') to use the default device.

alokprasad commented 4 years ago

I removed all CUDA specific code from glow.py and inference.py , it runs now but the wave file generated is full of NULL , so no speech in it. Any idea what could be the issue?

diff --git a/denoiser.py b/denoiser.py
index 8f9ff57..2da8f3a 100644
--- a/denoiser.py
+++ b/denoiser.py
@@ -11,7 +11,7 @@ class Denoiser(torch.nn.Module):
         super(Denoiser, self).__init__()
         self.stft = STFT(filter_length=filter_length,
                          hop_length=int(filter_length/n_overlap),
-                         win_length=win_length).cuda()
+                         win_length=win_length)
         if mode == 'zeros':
             mel_input = torch.zeros(
                 (1, 80, 88),
@@ -32,7 +32,7 @@ class Denoiser(torch.nn.Module):
         self.register_buffer('bias_spec', bias_spec[:, :, 0][:, :, None])

     def forward(self, audio, strength=0.1):
-        audio_spec, audio_angles = self.stft.transform(audio.cuda().float())
+        audio_spec, audio_angles = self.stft.transform(audio.float())
         audio_spec_denoised = audio_spec - self.bias_spec * strength
         audio_spec_denoised = torch.clamp(audio_spec_denoised, 0.0)
         audio_denoised = self.stft.inverse(audio_spec_denoised, audio_angles)
diff --git a/glow.py b/glow.py
index f692103..6690272 100644
--- a/glow.py
+++ b/glow.py
@@ -103,7 +103,7 @@ class Invertible1x1Conv(torch.nn.Module):
                 # Reverse computation
                 W_inverse = W.float().inverse()
                 W_inverse = Variable(W_inverse[..., None])
-                if z.type() == 'torch.cuda.HalfTensor':
+                if z.type() == 'torch.HalfTensor':
                     W_inverse = W_inverse.half()
                 self.W_inverse = W_inverse
             z = F.conv1d(z, self.W_inverse, bias=None, stride=1, padding=0)
@@ -148,8 +148,8 @@ class WN(torch.nn.Module):
             # depthwise separable convolution
             depthwise = torch.nn.Conv1d(n_channels, n_channels, 3,
                 dilation=dilation, padding=padding,
-                groups=n_channels).cuda()
-            pointwise = torch.nn.Conv1d(n_channels, 2*n_channels, 1).cuda()
+                groups=n_channels)
+            pointwise = torch.nn.Conv1d(n_channels, 2*n_channels, 1)
             bn = torch.nn.BatchNorm1d(n_channels) 
             self.in_layers.append(torch.nn.Sequential(bn, depthwise, pointwise))
             # res_skip_layer
@@ -245,12 +245,12 @@ class SqueezeWave(torch.nn.Module):
     def infer(self, spect, sigma=1.0):
         spect_size = spect.size()
         l = spect.size(2)*(256 // self.n_audio_channel)
-        if spect.type() == 'torch.cuda.HalfTensor':
-            audio = torch.cuda.HalfTensor(spect.size(0),
+        if spect.type() == 'torch.HalfTensor':
+            audio = torch.HalfTensor(spect.size(0),
                                           self.n_remaining_channels,
                                           l).normal_()
         else:
-            audio = torch.cuda.FloatTensor(spect.size(0),
+            audio = torch.FloatTensor(spect.size(0),
                                            self.n_remaining_channels,
                                            l).normal_()

@@ -268,10 +268,10 @@ class SqueezeWave(torch.nn.Module):
             audio = self.convinv[k](audio, reverse=True)

             if k % self.n_early_every == 0 and k > 0:
-                if spect.type() == 'torch.cuda.HalfTensor':
-                    z = torch.cuda.HalfTensor(spect.size(0), self.n_early_size, l).normal_()
+                if spect.type() == 'torch.HalfTensor':
+                    z = torch.HalfTensor(spect.size(0), self.n_early_size, l).normal_()
                 else:
-                    z = torch.cuda.FloatTensor(spect.size(0), self.n_early_size, l).normal_()
+                    z = torch.FloatTensor(spect.size(0), self.n_early_size, l).normal_()
                 audio = torch.cat((sigma*z, audio),1)

         audio = audio.permute(0,2,1).contiguous().view(audio.size(0), -1).data
diff --git a/inference.py b/inference.py
index 568e6ce..f31c013 100644
--- a/inference.py
+++ b/inference.py
@@ -32,27 +32,31 @@ from scipy.io.wavfile import write
 import torch
 from mel2samp import files_to_list, MAX_WAV_VALUE
 from denoiser import Denoiser
-
+import time

 def main(mel_files, squeezewave_path, sigma, output_dir, sampling_rate, is_fp16,
          denoiser_strength):
     mel_files = files_to_list(mel_files)
-    squeezewave = torch.load(squeezewave_path)['model']
+
+    #device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
+    device = torch.device('cpu')   
+    squeezewave = torch.load(squeezewave_path,map_location=device) ['model']
     squeezewave = squeezewave.remove_weightnorm(squeezewave)
-    squeezewave.cuda().eval()
+    squeezewave.eval()
     if is_fp16:
         from apex import amp
-        squeezewave, _ = amp.initialize(squeezewave, [], opt_level="O3")
+        squeezewave, _ = amp.initialize(squeezewave, [])

     if denoiser_strength > 0:
-        denoiser = Denoiser(squeezewave).cuda()
-
+        denoiser = Denoiser(squeezewave)
+    start = time.time()
     for i, file_path in enumerate(mel_files):
         file_name = os.path.splitext(os.path.basename(file_path))[0]
         mel = torch.load(file_path)
-        mel = torch.autograd.Variable(mel.cuda())
+        mel = torch.autograd.Variable(mel)
         mel = torch.unsqueeze(mel, 0)
         mel = mel.half() if is_fp16 else mel
+       
         with torch.no_grad():
             audio = squeezewave.infer(mel, sigma=sigma).float()
             if denoiser_strength > 0:
@@ -65,6 +69,9 @@ def main(mel_files, squeezewave_path, sigma, output_dir, sampling_rate, is_fp16,
             output_dir, "{}_synthesis.wav".format(file_name))
         write(audio_path, sampling_rate, audio)
         print(audio_path)
+    end = time.time()
+    print("Squeezewave vocoder time")
+    print(end-start)

 if __name__ == "__main__":
alokprasad commented 4 years ago

ok i was able to solve it , diff is here https://github.com/alokprasad/binaries/blob/master/squeezewave.diff I will request a pull request for the same .

BohanZhai commented 4 years ago

Thank you so much for your attention to our work, and you are right, if you want to run it on CPU, you need to delete all .cuda(), and change all Cuda tensors to normal tensors.

varungujjar commented 4 years ago

@alokprasad I made the changes as per the https://github.com/alokprasad/binaries/blob/master/squeezewave.diff

removed all references to cuda ..however am still unable to run the model using this command

python inference.py -f <(ls mel_spectrograms/*.pt) -w L64_large_pretrain -o . --is_fp16 -s 0.6

File "inference.py", line 92, in args.sampling_rate, args.is_fp16, args.denoiserstrength) File "inference.py", line 46, in main squeezewave, = amp.initialize(squeezewave,[],opt_level="O3") File "/Volumes/Work/dev/SqueezeWave/env/lib/python3.7/site-packages/apex-0.1-py3.7.egg/apex/amp/frontend.py", line 358, in initialize return _initialize(models, optimizers, _amp_state.opt_properties, num_losses, cast_model_outputs) File "/Volumes/Work/dev/SqueezeWave/env/lib/python3.7/site-packages/apex-0.1-py3.7.egg/apex/amp/_initialize.py", line 171, in _initialize check_params_fp32(models) File "/Volumes/Work/dev/SqueezeWave/env/lib/python3.7/site-packages/apex-0.1-py3.7.egg/apex/amp/_initialize.py", line 93, in check_params_fp32 name, param.type())) File "/Volumes/Work/dev/SqueezeWave/env/lib/python3.7/site-packages/apex-0.1-py3.7.egg/apex/amp/_amp_state.py", line 32, in warn_or_err raise RuntimeError(msg) RuntimeError: Found param WN.0.in_layers.0.0.weight with type torch.FloatTensor, expected torch.cuda.FloatTensor. When using amp.initialize, you need to provide a model with parameters located on a CUDA device before passing it no matter what optimization level you chose. Use model.to('cuda') to use the default device.

How did u solve it ? did i miss anything ?

alokprasad commented 4 years ago

@varungujjar you can use this repo , have put all the changes there https://github.com/alokprasad/fastspeech_squeezewave

varungujjar commented 4 years ago

@alokprasad great ... also just refered to your page i'll be trying this on a RPI4 4gb and get back to you with the timing .. :)