Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: moving the operations to the GPU if GPU available #1522

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pyannote/audio/pipelines/speaker_diarization.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ def iter_waveform_and_mask():
for i, batch in enumerate(batches, 1):
waveforms, masks = zip(*filter(lambda b: b[0] is not None, batch))

if torch.cuda.is_available():
Copy link

@grazder grazder Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should move to GPU only if self.device == 'cuda'. This can lead to some troubles, for example if user have gpu available, but he wants to run it on cpu. Also it don't match with .to behaviour

waveforms = tuple([x.cuda() for x in waveforms])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that we can cast waveform somewhere before. Because casting to cuda for every element in list seems not efficient. May be we can cast original file or waveform to cuda before splitting it into batch? So we don't need to make multiple casts to cuda for every element in batch instead of 1 cast

masks = tuple([x.cuda() for x in masks])

waveform_batch = torch.vstack(waveforms)
# (batch_size, 1, num_samples) torch.Tensor

Expand Down
8 changes: 4 additions & 4 deletions pyannote/audio/pipelines/speaker_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ def dimension(self) -> int:
dummy_waveforms = torch.rand(1, 1, 16000)
features = self.compute_fbank(dummy_waveforms)
embeddings = self.session_.run(
output_names=["embs"], input_feed={"feats": features.numpy()}
output_names=["embs"], input_feed={"feats": features.cpu().numpy()}
)[0]
_, dimension = embeddings.shape
return dimension
Expand All @@ -504,7 +504,7 @@ def min_num_samples(self) -> int:
continue

embeddings = self.session_.run(
output_names=["embs"], input_feed={"feats": features.numpy()}
output_names=["embs"], input_feed={"feats": features.cpu().numpy()}
)[0]

if np.any(np.isnan(embeddings)):
Expand Down Expand Up @@ -583,7 +583,7 @@ def __call__(

if masks is None:
embeddings = self.session_.run(
output_names=["embs"], input_feed={"feats": features.numpy()}
output_names=["embs"], input_feed={"feats": features.cpu().numpy()}
)[0]

return embeddings
Expand All @@ -606,7 +606,7 @@ def __call__(

embeddings[f] = self.session_.run(
output_names=["embs"],
input_feed={"feats": masked_feature.numpy()[None]},
input_feed={"feats": masked_feature.cpu().numpy()[None]},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, you are actually sending the features back to CPU, before sending them to the inference. Therefore, I don't really understand why this should speed things up.

Can you please provide me with a bit more context?
Ideally, a Colab notebook illustrating the speed up that this brings?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't use the cpu() method, it will result in an error. You can refer to the official PyTorch documentation for this. What I've observed is that if we don't place the tensor on the GPU, both torch.vstack() and torch.fft.rfft() will consume a significant amount of CPU resources.

# Under the 'wavs_16k' directory, there are a total of 8 '.lst' files, each of which contains paths to audio files. 
# After running the following code, 8 PyAnnote processes are launched on a computing node equipped 
# with 8 V100 GPUs, with each process occupying one GPU.
for list in `ls wavs_16k/*.lst`
do
    sleep 1s
    echo $list
    srun  -N1 -n1 --gres=gpu:1 python scripts/pyannote_main.py ${list} > logs/`basename $list .lst`_${time}.log 2>&1 &
done

)[0][0]

return embeddings
Expand Down