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

Overdrive cpp extension #1299

Merged
merged 7 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions torchaudio/csrc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(
sox/effects_chain.cpp
sox/types.cpp
lfilter.cpp
overdrive.cpp
)

if(BUILD_TRANSDUCER)
Expand Down
49 changes: 49 additions & 0 deletions torchaudio/csrc/overdrive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <torch/script.h>

namespace {

template <typename scalar_t>
void overdrive_cpu_kernel(
at::TensorAccessor<scalar_t, 2> waveform_accessor,
at::TensorAccessor<scalar_t, 2> temp_accessor,
at::TensorAccessor<scalar_t, 1> last_in_accessor,
at::TensorAccessor<scalar_t, 1> last_out_accessor,
at::TensorAccessor<scalar_t, 2> output_waveform_accessor) {
int64_t n_frames = waveform_accessor.size(1);
int64_t n_channels = waveform_accessor.size(0);

for (int64_t i_channel = 0; i_channel < n_channels; ++i_channel) {
for (int64_t i_frame = 0; i_frame < n_frames; ++i_frame) {
last_out_accessor[i_channel] = temp_accessor[i_channel][i_frame] -
last_in_accessor[i_channel] + 0.995 * last_out_accessor[i_channel];
last_in_accessor[i_channel] = temp_accessor[i_channel][i_frame];
output_waveform_accessor[i_channel][i_frame] =
waveform_accessor[i_channel][i_frame] * 0.5 +
last_out_accessor[i_channel] * 0.75;
}
}
}

void overdrive_core_loop_cpu(
at::Tensor& waveform,
at::Tensor& temp,
at::Tensor& last_in,
at::Tensor& last_out,
at::Tensor& output_waveform) {
AT_DISPATCH_FLOATING_TYPES(waveform.scalar_type(), "overdrive_cpu", ([&] {
overdrive_cpu_kernel<scalar_t>(
waveform.accessor<scalar_t, 2>(),
temp.accessor<scalar_t, 2>(),
last_in.accessor<scalar_t, 1>(),
last_out.accessor<scalar_t, 1>(),
output_waveform.accessor<scalar_t, 2>());
}));
}

} // namespace

// Note: We want to avoid using "catch-all" kernel.
// The following registration should be replaced with CPU specific registration.
TORCH_LIBRARY_FRAGMENT(torchaudio, m) {
m.def("torchaudio::_overdrive_core_loop", &overdrive_core_loop_cpu);
}
35 changes: 30 additions & 5 deletions torchaudio/functional/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,26 @@ def lowpass_biquad(
return biquad(waveform, b0, b1, b2, a0, a1, a2)


def _overdrive_core_loop_generic(
waveform: Tensor,
temp: Tensor,
last_in: Tensor,
last_out: Tensor,
output_waveform: Tensor
):
for i in range(waveform.shape[-1]):
last_out = temp[:, i] - last_in + 0.995 * last_out
last_in = temp[:, i]
output_waveform[:, i] = waveform[:, i] * 0.5 + last_out * 0.75


try:
_overdrive_core_loop_cpu = torch.ops.torchaudio._overdrive_core_loop
except RuntimeError as err:
assert str(err) == 'No such operator torchaudio::_overdrive_core_loop'
_overdrive_core_loop_cpu = _overdrive_core_loop_generic


def overdrive(waveform: Tensor, gain: float = 20, colour: float = 20) -> Tensor:
r"""Apply a overdrive effect to the audio. Similar to SoX implementation.
This effect applies a non linear distortion to the audio signal.
Expand Down Expand Up @@ -981,11 +1001,16 @@ def overdrive(waveform: Tensor, gain: float = 20, colour: float = 20) -> Tensor:

output_waveform = torch.zeros_like(waveform, dtype=dtype, device=device)

# TODO: Implement a torch CPP extension
for i in range(waveform.shape[-1]):
last_out = temp[:, i] - last_in + 0.995 * last_out
last_in = temp[:, i]
output_waveform[:, i] = waveform[:, i] * 0.5 + last_out * 0.75
# # TODO: Implement a torch CPP extension
# for i in range(waveform.shape[-1]):
# last_out = temp[:, i] - last_in + 0.995 * last_out
# last_in = temp[:, i]
# output_waveform[:, i] = waveform[:, i] * 0.5 + last_out * 0.75
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you can get rid of these comments now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mthrok sure will remove them.
Also I will try to implement the parallel_for as mentioned by @cpuhrsch in the old PR comments ( #580 ).

Copy link
Collaborator

Choose a reason for hiding this comment

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

@bhargavkathivarapu sounds good. Could you try measure the performance improvement?
Something like the following and change the shape of tensor to variety of shapes.
If numactl is missing, you can install it with sudo apt-get install -y numactl on Ubuntu.

OMP_NUM_THREADS=1 numactl --membind 0 --cpubind 0 python -m timeit -n 100 -r 5 -s """
import torch;
import torchaudio
x = torch.zeros(32, 100, dtype=torch.float)
""" """
torchaudio.functional.overdrive(x)
"""


if device == torch.device('cpu'):
_overdrive_core_loop_cpu(waveform, temp, last_in, last_out, output_waveform)
else:
_overdrive_core_loop_generic(waveform, temp, last_in, last_out, output_waveform)

return output_waveform.clamp(min=-1, max=1).view(actual_shape)

Expand Down