Skip to content

Add simulate_rir_ism method for simulating RIR with Image Source Method #2644

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

Closed
wants to merge 6 commits into from

Conversation

nateanl
Copy link
Member

@nateanl nateanl commented Aug 23, 2022

No description provided.

@nateanl nateanl marked this pull request as ready for review August 29, 2022 17:09
@nateanl nateanl requested a review from a team August 29, 2022 17:09
@nateanl
Copy link
Member Author

nateanl commented Aug 29, 2022

@nateanl
Copy link
Member Author

nateanl commented Aug 29, 2022

cc @fakufaku

@skipIfNoModule("pyroomacoustics")
@parameterized.expand([(2, 1), (3, 4)])
def test_simulate_rir_ism_single_band(self, D, channel):
"""Test simulate_rir_ism when absorption coefficients are identical for all walls."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

I do not see the test logic checks the identity described in the docstring.
It is checking against pyroomacoustic, but the test logic does not check identity for walls.

Can you elaborate?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. if e_absorption is just a floating-point value, then it means it is identical to all walls. That's why the room in pyroonacoustics is constructed with materials=pra.Material(e_absorption),.
The difference of this test and the one below is if e_absorption is a 2D Tensor, the multi-band logic in https://github.com/pytorch/audio/pull/2644/files#diff-bfbe08000c5dc05901af3bad9503693a99068426351748925a322990daed0cb3R217-R222 is applied. We need a separate test for that.

const int64_t ir_length = irs.size(3);
torch::Tensor rirs =
torch::zeros({num_band, num_mic, rir_length}, irs.dtype());
rirs.requires_grad_(true);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this work with torch.inference_mode()?

Copy link
Collaborator

Choose a reason for hiding this comment

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

actually, what does it accomplish?
build_rir_impl manipulates the data of rirs by pointer, so autograd cannot track it.
In this case, the modification of requires_grad=True should be done by client code.

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried to remove this line and run autograd_test, the test fails but was working with this line.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What happens if you move require_grad to after the call to build_rir_impl ?

Copy link
Member Author

Choose a reason for hiding this comment

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

It also passes the autograd test.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That suggests that the call to rirs.requires_grad_(true) can be outside of this function. Perhaps can you set it in test code?

torch::Tensor& filters) {
int64_t n = centers.size(0);
torch::Tensor new_bands = torch::zeros({n, 2}, centers.dtype());
new_bands.requires_grad_(true);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar to above, I double the effectiveness of requires_grad_ here.

return img_loc, att


def _hann(x: torch.Tensor, T: int):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add comment on why we cannot use torch.hann_window?

Copy link
Member Author

Choose a reason for hiding this comment

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

sue. The reason is the original formula of image source method truncate the function values, any points outside [-T//2. T//2] is 0.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, torch.hann_window can only sample the window function at integer points, which is suitable to create the window functions for STFT and such. In the image source model, we need to sample the continuous window function at non-integer points.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@nateanl This is not addressed. We need an explanation of why this function is required.

return torch.special.sinc(n - delay) * _hann(n - delay, 2 * pad)


def simulate_rir_ism(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you decorate this function so that if RIR is not compiled, it will fail immediately with easy-to-understand message?

"torchaudio::build_rir(Tensor irs, Tensor delay_i, int rir_length) -> Tensor",
&torchaudio::rir::build_rir);
m.def(
"torchaudio::make_filter(Tensor centers, float sample_rate, int n_fft) -> Tensor",
Copy link
Collaborator

Choose a reason for hiding this comment

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

The function name is very generic to be in torchaudio namespace. Can you make it more descriptive / specific?

using namespace torch::indexing;

namespace torchaudio {
namespace rir {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this file does not have corresponding header file, can you add anonymous namespace so that it is not referable from other C++ source?

@@ -1,9 +1,14 @@
import numpy as np

try:
import pyroomacoustics as pra
Copy link
Collaborator

@mthrok mthrok Oct 4, 2022

Choose a reason for hiding this comment

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

Please follow

if _mod_utils.is_module_available("soundfile"):
import soundfile

@mthrok mthrok mentioned this pull request Nov 8, 2022
6 tasks
@NicolasHug NicolasHug mentioned this pull request Nov 14, 2022
6 tasks
@nateanl
Copy link
Member Author

nateanl commented Nov 22, 2022

After discussing with @fakufaku and @NicolasHug, the gradients of converting a float tensor to int tensor and using it as indices for another tensor may require an analytic formula. Before getting the formula with proof, I will disable the autograd test and focus on the numerical correctness in the forward method.

@nateanl
Copy link
Member Author

nateanl commented Nov 30, 2022

will address the issue in a new PR #2880

@nateanl nateanl closed this Nov 30, 2022
facebook-github-bot pushed a commit that referenced this pull request Feb 14, 2023
Summary:
replicate of #2644

Pull Request resolved: #2880

Reviewed By: mthrok

Differential Revision: D41633911

Pulled By: nateanl

fbshipit-source-id: 73cf145d75c389e996aafe96571ab86dc21f86e5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants