Fft operations on Mobile

Kablys: <p>I am trying to run audio classification model on Android device, but I am getting error:<br>
<code>RuntimeError: fft: ATen not compiled with MKL support</code>, it’s caused by <code>MelSpectrogram</code> transformation.<br>
I found few related issues on GitHub:</p>
<ul>
<li><a href="https://github.com/pytorch/audio/issues/408" class="inline-onebox" rel="noopener nofollow ugc">torchaudio mobile? · Issue #408 · pytorch/audio · GitHub</a></li>
<li><a href="https://github.com/pytorch/pytorch/issues/41592" class="inline-onebox" rel="noopener nofollow ugc">Add SpectralOps CPU implementation for ARM/PowerPC processors (where MKL is not available) · Issue #41592 · pytorch/pytorch · GitHub</a></li>
</ul>
<p>But neither of them had clear solution. What is proper way to fix this problem, preferably with minimal changes to model.</p>

guangy10: <p>Thanks for reaching out. Wondering if there is any detailed log your could share? <a class="mention" href="/u/iseeyuan">@iseeyuan</a> <a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a> could further help you from there.</p>

guangy10: <p><a class="mention" href="/u/cccclai">@cccclai</a> may be able to help as well.</p>

iseeyuan: <p><a class="mention" href="/u/kablys">@Kablys</a> Thanks for raising this up! I don’t think we have a solution on this yet. I’ll follow up in Issue <span class="hashtag">#41592</span> and see if there is any update there. We are also looking for contributions on implementing those ops in mobile.</p>

Kablys: <p>Simplest PyTorch code to recreate issue:</p>
<pre><code class="lang-python">import torch

def only_stft():
    return torch.stft(torch.tensor([1.0, 1.0, 1.0]), 4)

stft = torch.jit.script(only_stft)
stft.save("stft.pt")
</code></pre>
<p>Then I added this Java snippet inside of HelloWorldApp example code: <a href="https://github.com/pytorch/android-demo-app/tree/master/HelloWorldApp" rel="noopener nofollow ugc">android-demo-app/HelloWorldApp at master · pytorch/android-demo-app · GitHub</a></p>
<pre><code class="lang-java">    try {
      Module mod = Module.load(assetFilePath(this, "stft.pt"));
      IValue result = mod.forward();
    } catch (IOException e) {
      finish();
    }
</code></pre>
<p>The error I get (replaced few personal parts with <code>...</code>):</p>
<pre><code class="lang-auto">Install successfully finished in 4 s 38 ms.
$ adb shell am start -n "org.pytorch.helloworld/org.pytorch.helloworld.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 23935 on device '...'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/orch.helloworld: type=1400 audit(0.0:322903): avc: denied { read } for name="u:object_r:vendor_default_prop:s0" dev="tmpfs" ino=11093 scontext=u:r:untrusted_app_27:s0:c10,c257,c512,c768 tcontext=u:object_r:vendor_default_prop:s0 tclass=file permissive=0
E/libc: Access denied finding property "ro.hardware.chipname"
W/native: [W SpectralOps.cpp:639] Warning: stft will soon require the return_complex parameter be given for real inputs, and will further require that return_complex=True in a future PyTorch release. (function operator())
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: org.pytorch.helloworld, PID: 23935
    java.lang.RuntimeException: Unable to start activity ComponentInfo{org.pytorch.helloworld/org.pytorch.helloworld.MainActivity}: java.lang.RuntimeException: The following operation failed in the TorchScript interpreter.
    Traceback of TorchScript, serialized code (most recent call last):
      File "code/__torch__.py", line 7, in forward
      def forward(self: __torch__.PlaceholderModule) -&gt; Tensor:
        _0 = torch.tensor([1., 1., 1.], dtype=None, device=None, requires_grad=False)
        _1 = __torch__.torch.functional.stft(_0, 4, None, None, None, True, "reflect", False, None, None, )
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
        return _1
      File "code/__torch__/torch/functional.py", line 21, in stft
      else:
        input0 = input
      _2 = torch.stft(input0, n_fft, hop_length, win_length, window, normalized, onesided, return_complex)
           ~~~~~~~~~~ &lt;--- HERE
      return _2
    
    Traceback of TorchScript, original code (most recent call last):
      File "C:\Users\...\Anaconda3\envs\...\lib\site-packages\torch\functional.py", line 515, in stft
            input = F.pad(input.view(extended_shape), (pad, pad), pad_mode)
            input = input.view(input.shape[-signal_dim:])
        return _VF.stft(input, n_fft, hop_length, win_length, window,  # type: ignore
               ~~~~~~~~ &lt;--- HERE
                        normalized, onesided, return_complex)
    RuntimeError: fft: ATen not compiled with MKL support
    
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3534)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3689)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2239)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:227)
        at android.app.ActivityThread.main(ActivityThread.java:7822)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1026)
     Caused by: java.lang.RuntimeException: The following operation failed in the TorchScript interpreter.
    Traceback of TorchScript, serialized code (most recent call last):
      File "code/__torch__.py", line 7, in forward
      def forward(self: __torch__.PlaceholderModule) -&gt; Tensor:
        _0 = torch.tensor([1., 1., 1.], dtype=None, device=None, requires_grad=False)
        _1 = __torch__.torch.functional.stft(_0, 4, None, None, None, True, "reflect", False, None, None, )
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ &lt;--- HERE
        return _1
      File "code/__torch__/torch/functional.py", line 21, in stft
      else:
        input0 = input
      _2 = torch.stft(input0, n_fft, hop_length, win_length, window, normalized, onesided, return_complex)
           ~~~~~~~~~~ &lt;--- HERE
      return _2
    
    Traceback of TorchScript, original code (most recent call last):
      File "C:\Users\...\Anaconda3\envs\...\lib\site-packages\torch\functional.py", line 515, in stft
            input = F.pad(input.view(extended_shape), (pad, pad), pad_mode)
            input = input.view(input.shape[-signal_dim:])
        return _VF.stft(input, n_fft, hop_length, win_length, window,  # type: ignore
               ~~~~~~~~ &lt;--- HERE
                        normalized, onesided, return_complex)
    RuntimeError: fft: ATen not compiled with MKL support
    
        at org.pytorch.NativePeer.forward(Native Method)
        at org.pytorch.Module.forward(Module.java:49)
        at org.pytorch.helloworld.MainActivity.onCreate(MainActivity.java:37)
        at android.app.Activity.performCreate(Activity.java:7963)
        at android.app.Activity.performCreate(Activity.java:7952)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3505)
        	... 11 more
I/Process: Sending signal. PID: 23935 SIG: 9
</code></pre>

guangy10: <p>Thanks! That would be helpful for us to look into the issue. We will follow up with you once we get a solution.</p>

yousanke: <p>Hi, so we are hitting the same issue trying to run fft on Android devices. Will this feature be implemented in the near future? It will be appreciated if we have this on PyTorch mobile because it is not easy to find a workaround for fft. Thanks!</p>

eigen: <p>Bump, same issue here.</p>