diffsptk 4.1.0__tar.gz → 4.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. {diffsptk-4.1.0 → diffsptk-4.2.0}/.gitignore +1 -0
  2. {diffsptk-4.1.0 → diffsptk-4.2.0}/PKG-INFO +1 -1
  3. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/functional.py +2 -2
  4. diffsptk-4.2.0/diffsptk/kernels/__init__.py +0 -0
  5. diffsptk-4.2.0/diffsptk/kernels/common.py +172 -0
  6. diffsptk-4.2.0/diffsptk/kernels/zerodf.cu +262 -0
  7. diffsptk-4.2.0/diffsptk/kernels/zerodf.py +213 -0
  8. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/excite.py +5 -5
  9. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/mglsadf.py +8 -2
  10. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/zerodf.py +56 -11
  11. diffsptk-4.2.0/diffsptk/version.py +1 -0
  12. {diffsptk-4.1.0 → diffsptk-4.2.0}/pyproject.toml +1 -0
  13. diffsptk-4.1.0/diffsptk/version.py +0 -1
  14. {diffsptk-4.1.0 → diffsptk-4.2.0}/CITATION.cff +0 -0
  15. {diffsptk-4.1.0 → diffsptk-4.2.0}/LICENSE +0 -0
  16. {diffsptk-4.1.0 → diffsptk-4.2.0}/README.md +0 -0
  17. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/__init__.py +0 -0
  18. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/__init__.py +0 -0
  19. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/acorr.py +0 -0
  20. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/acr2csm.py +0 -0
  21. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/alaw.py +0 -0
  22. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/ap.py +0 -0
  23. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/b2mc.py +0 -0
  24. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/base.py +0 -0
  25. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/c2acr.py +0 -0
  26. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/c2mpir.py +0 -0
  27. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/c2ndps.py +0 -0
  28. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/cdist.py +0 -0
  29. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/chroma.py +0 -0
  30. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/cqt.py +0 -0
  31. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/csm2acr.py +0 -0
  32. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/dct.py +0 -0
  33. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/decimate.py +0 -0
  34. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/delay.py +0 -0
  35. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/delta.py +0 -0
  36. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/dequantize.py +0 -0
  37. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/df2.py +0 -0
  38. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/dfs.py +0 -0
  39. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/dht.py +0 -0
  40. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/drc.py +0 -0
  41. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/dst.py +0 -0
  42. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/dtw.py +0 -0
  43. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/entropy.py +0 -0
  44. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/f0eval.py +0 -0
  45. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/fbank.py +0 -0
  46. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/fftcep.py +0 -0
  47. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/fftr.py +0 -0
  48. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/flux.py +0 -0
  49. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/frame.py +0 -0
  50. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/freqt.py +0 -0
  51. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/freqt2.py +0 -0
  52. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/gammatone.py +0 -0
  53. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/gmm.py +0 -0
  54. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/gnorm.py +0 -0
  55. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/griffin.py +0 -0
  56. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/grpdelay.py +0 -0
  57. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/hilbert.py +0 -0
  58. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/histogram.py +0 -0
  59. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/ialaw.py +0 -0
  60. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/ica.py +0 -0
  61. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/icqt.py +0 -0
  62. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/idct.py +0 -0
  63. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/idht.py +0 -0
  64. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/idst.py +0 -0
  65. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/ifbank.py +0 -0
  66. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/ifftr.py +0 -0
  67. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/ifreqt2.py +0 -0
  68. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/igammatone.py +0 -0
  69. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/ignorm.py +0 -0
  70. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/iltcdf.py +0 -0
  71. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/imdct.py +0 -0
  72. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/imdst.py +0 -0
  73. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/imglsadf.py +0 -0
  74. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/imsvq.py +0 -0
  75. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/interpolate.py +0 -0
  76. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/ipnorm.py +0 -0
  77. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/ipqmf.py +0 -0
  78. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/is2par.py +0 -0
  79. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/istft.py +0 -0
  80. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/iulaw.py +0 -0
  81. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/ivq.py +0 -0
  82. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/lar2par.py +0 -0
  83. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/lbg.py +0 -0
  84. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/levdur.py +0 -0
  85. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/linear_intpl.py +0 -0
  86. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/lpc.py +0 -0
  87. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/lpc2lsp.py +0 -0
  88. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/lpc2par.py +0 -0
  89. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/lpccheck.py +0 -0
  90. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/lsp2lpc.py +0 -0
  91. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/lsp2sp.py +0 -0
  92. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/lspcheck.py +0 -0
  93. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/ltcdf.py +0 -0
  94. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/magic_intpl.py +0 -0
  95. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/mc2b.py +0 -0
  96. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/mcep.py +0 -0
  97. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/mcpf.py +0 -0
  98. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/mdct.py +0 -0
  99. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/mdst.py +0 -0
  100. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/medfilt.py +0 -0
  101. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/mfcc.py +0 -0
  102. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/mgc2mgc.py +0 -0
  103. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/mgc2sp.py +0 -0
  104. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/mgcep.py +0 -0
  105. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/mlpg.py +0 -0
  106. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/mlsacheck.py +0 -0
  107. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/mpir2c.py +0 -0
  108. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/msvq.py +0 -0
  109. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/ndps2c.py +0 -0
  110. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/nmf.py +0 -0
  111. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/norm0.py +0 -0
  112. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/oband.py +0 -0
  113. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/par2is.py +0 -0
  114. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/par2lar.py +0 -0
  115. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/par2lpc.py +0 -0
  116. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/pca.py +0 -0
  117. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/phase.py +0 -0
  118. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/pitch.py +0 -0
  119. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/pitch_spec.py +0 -0
  120. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/plp.py +0 -0
  121. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/pnorm.py +0 -0
  122. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/pol_root.py +0 -0
  123. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/poledf.py +0 -0
  124. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/pqmf.py +0 -0
  125. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/quantize.py +0 -0
  126. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/rlevdur.py +0 -0
  127. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/rmse.py +0 -0
  128. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/root_pol.py +0 -0
  129. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/smcep.py +0 -0
  130. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/snr.py +0 -0
  131. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/spec.py +0 -0
  132. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/stft.py +0 -0
  133. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/ulaw.py +0 -0
  134. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/unframe.py +0 -0
  135. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/vq.py +0 -0
  136. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/wht.py +0 -0
  137. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/window.py +0 -0
  138. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/world_synth.py +0 -0
  139. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/yingram.py +0 -0
  140. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/modules/zcross.py +0 -0
  141. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/signals.py +0 -0
  142. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/third_party/librosa/LICENSE +0 -0
  143. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/third_party/librosa/__init__.py +0 -0
  144. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/third_party/librosa/constantq.py +0 -0
  145. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/third_party/librosa/filters.py +0 -0
  146. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/third_party/librosa/typing.py +0 -0
  147. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/third_party/librosa/util.py +0 -0
  148. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/third_party/world/LICENSE +0 -0
  149. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/third_party/world/__init__.py +0 -0
  150. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/third_party/world/common.py +0 -0
  151. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/typing.py +0 -0
  152. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/utils/__init__.py +0 -0
  153. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/utils/private.py +0 -0
  154. {diffsptk-4.1.0 → diffsptk-4.2.0}/diffsptk/utils/public.py +0 -0
@@ -1,5 +1,6 @@
1
1
  # project
2
2
  .venv/
3
+ uv.lock
3
4
  dist/
4
5
 
5
6
  # codecov
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: diffsptk
3
- Version: 4.1.0
3
+ Version: 4.2.0
4
4
  Summary: Speech signal processing modules for machine learning
5
5
  Project-URL: Homepage, https://sp-tk.sourceforge.net/
6
6
  Project-URL: Documentation, https://sp-nitech.github.io/diffsptk/stable/
@@ -3146,7 +3146,7 @@ def unframe(
3146
3146
  out_length : int >= 1 or None
3147
3147
  The length of the original waveform, :math:`T`.
3148
3148
 
3149
- frame_peirod : int >= 1
3149
+ frame_period : int >= 1
3150
3150
  The frame period in samples, :math:`P`.
3151
3151
 
3152
3152
  center : bool
@@ -3334,7 +3334,7 @@ def zerodf(
3334
3334
  zeroth_index : int >= 0
3335
3335
  The index of the zeroth coefficient in the filter coefficients.
3336
3336
 
3337
- mode : ['direct', 'efficient']
3337
+ mode : ['direct', 'efficient', 'cuda']
3338
3338
  The implementation mode for time-varying convolution.
3339
3339
 
3340
3340
  Returns
File without changes
@@ -0,0 +1,172 @@
1
+ # ------------------------------------------------------------------------ #
2
+ # Copyright 2022 SPTK Working Group #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); #
5
+ # you may not use this file except in compliance with the License. #
6
+ # You may obtain a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ # ------------------------------------------------------------------------ #
16
+
17
+ import ctypes
18
+ import warnings
19
+ from importlib import resources
20
+ from typing import Any
21
+
22
+ import torch
23
+
24
+ _C_TYPES = {torch.float32: "float", torch.float64: "double"}
25
+ _KERNELS: dict[tuple, Any] = {}
26
+ _CONTEXTS: dict[int, ctypes.c_void_p] = {}
27
+ _disabled = False
28
+
29
+
30
+ class KernelUnavailableError(RuntimeError):
31
+ """Raised when a kernel cannot be compiled or loaded."""
32
+
33
+
34
+ def is_available() -> bool:
35
+ """Return True if CUDA kernels can be compiled at runtime."""
36
+ return (
37
+ not _disabled
38
+ and torch.cuda.is_available()
39
+ and not torch.version.hip # type: ignore[attr-defined]
40
+ and hasattr(torch.cuda, "_compile_kernel")
41
+ )
42
+
43
+
44
+ def is_supported(*tensors: torch.Tensor) -> bool:
45
+ """Return True if the given tensors can be processed by the CUDA kernels."""
46
+ if not is_available():
47
+ return False
48
+ t0 = tensors[0]
49
+ if t0.dtype not in _C_TYPES:
50
+ return False
51
+ return all(
52
+ t.is_cuda and t.device == t0.device and t.dtype == t0.dtype for t in tensors
53
+ )
54
+
55
+
56
+ def _check(result: int) -> None:
57
+ if result != 0:
58
+ raise RuntimeError(f"CUDA driver API call failed with error code {result}.")
59
+
60
+
61
+ def _driver() -> Any:
62
+ from torch.cuda import _utils
63
+
64
+ return _utils._get_gpu_runtime_library() # type: ignore[attr-defined]
65
+
66
+
67
+ def _ensure_context(device: torch.device) -> ctypes.c_void_p | None:
68
+ """Make the primary context of the device current on the calling thread.
69
+
70
+ The kernels are loaded and launched via the CUDA driver API, which requires a
71
+ current context. Threads that have not called the CUDA runtime API yet, e.g.,
72
+ the worker thread of the autograd engine, may not have it.
73
+
74
+ Returns the previously current context if it has been replaced, else None.
75
+ """
76
+ lib = _driver()
77
+ ctx = ctypes.c_void_p()
78
+ _check(lib.cuCtxGetCurrent(ctypes.byref(ctx)))
79
+ if ctx.value:
80
+ dev = ctypes.c_int()
81
+ _check(lib.cuCtxGetDevice(ctypes.byref(dev)))
82
+ if dev.value == device.index:
83
+ return None
84
+
85
+ if device.index not in _CONTEXTS:
86
+ dev = ctypes.c_int()
87
+ _check(lib.cuDeviceGet(ctypes.byref(dev), device.index))
88
+ primary = ctypes.c_void_p()
89
+ _check(lib.cuDevicePrimaryCtxRetain(ctypes.byref(primary), dev))
90
+ _CONTEXTS[device.index] = primary
91
+ _check(lib.cuCtxSetCurrent(_CONTEXTS[device.index]))
92
+ return ctx
93
+
94
+
95
+ def _restore_context(ctx: ctypes.c_void_p | None) -> None:
96
+ if ctx is not None:
97
+ _check(_driver().cuCtxSetCurrent(ctx))
98
+
99
+
100
+ def get_kernel(
101
+ source: str,
102
+ name: str,
103
+ dtype: torch.dtype,
104
+ device: torch.device,
105
+ defines: tuple[tuple[str, int], ...] = (),
106
+ ) -> Any:
107
+ """Compile (or fetch from the cache) a kernel defined in a .cu file.
108
+
109
+ If the compilation fails, the kernels are disabled for the rest of the process
110
+ and KernelUnavailableError is raised so that the caller can fall back to a
111
+ PyTorch implementation.
112
+ """
113
+ global _disabled
114
+ key = (source, name, dtype, device.index, defines)
115
+ if key not in _KERNELS:
116
+ code = resources.files("diffsptk.kernels").joinpath(source).read_text()
117
+ options = [f"-DSCALAR_T={_C_TYPES[dtype]}"]
118
+ options += [f"-D{k}={v}" for k, v in defines]
119
+ try:
120
+ with torch.cuda.device(device):
121
+ _KERNELS[key] = torch.cuda._compile_kernel( # type: ignore[attr-defined]
122
+ code, name, nvcc_options=options
123
+ )
124
+ except Exception as e:
125
+ _disabled = True
126
+ warnings.warn(
127
+ f"Failed to compile the CUDA kernel {name!r}; "
128
+ f"falling back to the PyTorch implementation: {e}",
129
+ stacklevel=2,
130
+ )
131
+ raise KernelUnavailableError(str(e)) from e
132
+ return _KERNELS[key]
133
+
134
+
135
+ def prepare(
136
+ source: str,
137
+ names: tuple[str, ...],
138
+ *,
139
+ dtype: torch.dtype,
140
+ device: torch.device,
141
+ defines: tuple[tuple[str, int], ...] = (),
142
+ ) -> None:
143
+ """Compile the kernels in advance to detect a compilation failure early."""
144
+ previous = _ensure_context(device)
145
+ try:
146
+ for name in names:
147
+ get_kernel(source, name, dtype, device, defines)
148
+ finally:
149
+ _restore_context(previous)
150
+
151
+
152
+ def launch(
153
+ source: str,
154
+ name: str,
155
+ n_threads: int,
156
+ args: list,
157
+ *,
158
+ dtype: torch.dtype,
159
+ device: torch.device,
160
+ defines: tuple[tuple[str, int], ...] = (),
161
+ block_size: int = 256,
162
+ ) -> None:
163
+ """Launch a 1-D kernel with at least n_threads threads."""
164
+ if n_threads <= 0:
165
+ return
166
+ n_blocks = (n_threads + block_size - 1) // block_size
167
+ previous = _ensure_context(device)
168
+ try:
169
+ kernel = get_kernel(source, name, dtype, device, defines)
170
+ kernel(grid=(n_blocks, 1, 1), block=(block_size, 1, 1), args=args)
171
+ finally:
172
+ _restore_context(previous)
@@ -0,0 +1,262 @@
1
+ // ------------------------------------------------------------------------ //
2
+ // Copyright 2022 SPTK Working Group //
3
+ // //
4
+ // Licensed under the Apache License, Version 2.0 (the "License"); //
5
+ // you may not use this file except in compliance with the License. //
6
+ // You may obtain a copy of the License at //
7
+ // //
8
+ // http://www.apache.org/licenses/LICENSE-2.0 //
9
+ // //
10
+ // Unless required by applicable law or agreed to in writing, software //
11
+ // distributed under the License is distributed on an "AS IS" BASIS, //
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
13
+ // See the License for the specific language governing permissions and //
14
+ // limitations under the License. //
15
+ // ------------------------------------------------------------------------ //
16
+
17
+ // Time-varying all-zero filter with linearly interpolated coefficients:
18
+ //
19
+ // y[t] = sum_{m=0}^{L-1} h_t[m] x[t + z - m],
20
+ // h_t = b[n] + r[j] (b[n+1] - b[n]), t = nP + j, b[N] := b[N-1],
21
+ //
22
+ // where x is zero outside [0, T) and z is the index of the zeroth coefficient.
23
+ // SCALAR_T is given as a compile-time definition.
24
+
25
+ // Loads x[u] with zero padding outside [0, T).
26
+ __device__ __forceinline__ SCALAR_T load_or_zero(const SCALAR_T* __restrict__ x,
27
+ int u, int T) {
28
+ return (0 <= u && u < T) ? x[u] : (SCALAR_T)0;
29
+ }
30
+
31
+ // Each thread computes BLOCKING consecutive outputs t0, ..., t0 + BLOCKING - 1
32
+ // of the same frame. The inputs x[t0 + r + z - m] are kept in a sliding window
33
+ // of registers, so that each pair of coefficients is used BLOCKING times.
34
+ //
35
+ // x: (B, T), b: (B, N, L), ramp: (P,), y: (B, T)
36
+ extern "C" __global__ void zerodf_forward(const SCALAR_T* __restrict__ x,
37
+ const SCALAR_T* __restrict__ b,
38
+ const SCALAR_T* __restrict__ ramp,
39
+ SCALAR_T* __restrict__ y, int B, int T,
40
+ int N, int L, int P, int z) {
41
+ const int G = (P + BLOCKING - 1) / BLOCKING;
42
+ const long long idx = (long long)blockIdx.x * blockDim.x + threadIdx.x;
43
+ if (idx >= (long long)B * N * G) return;
44
+ const long long bn = idx / G;
45
+ const int g = (int)(idx - bn * G);
46
+ const long long bi = bn / N;
47
+ const int n = (int)(bn - bi * N);
48
+ const int j0 = g * BLOCKING;
49
+ const int t0 = n * P + j0;
50
+
51
+ const SCALAR_T* xb = x + bi * T;
52
+ const SCALAR_T* b1 = b + bn * L;
53
+ const SCALAR_T* b2 = b + (bi * N + min(n + 1, N - 1)) * L;
54
+
55
+ // Restrict m so that t0 + r + z - m < T for r = 0 and >= 0 for the last r.
56
+ const int m_lo = max(0, t0 + z - (T - 1));
57
+ const int m_hi = min(L - 1, t0 + BLOCKING - 1 + z);
58
+
59
+ SCALAR_T w[BLOCKING], s1[BLOCKING], s2[BLOCKING];
60
+ #pragma unroll
61
+ for (int r = 0; r < BLOCKING; r++) {
62
+ w[r] = load_or_zero(xb, t0 + r + z - m_lo, T);
63
+ s1[r] = 0;
64
+ s2[r] = 0;
65
+ }
66
+ for (int m = m_lo; m <= m_hi; m++) {
67
+ const SCALAR_T c1 = b1[m];
68
+ const SCALAR_T c2 = b2[m];
69
+ #pragma unroll
70
+ for (int r = 0; r < BLOCKING; r++) {
71
+ s1[r] += c1 * w[r];
72
+ s2[r] += c2 * w[r];
73
+ }
74
+ #pragma unroll
75
+ for (int r = BLOCKING - 1; 0 < r; r--) w[r] = w[r - 1];
76
+ w[0] = load_or_zero(xb, t0 + z - m - 1, T);
77
+ }
78
+
79
+ SCALAR_T* yb = y + bi * T;
80
+ #pragma unroll
81
+ for (int r = 0; r < BLOCKING; r++) {
82
+ const int j = j0 + r;
83
+ if (j < P) yb[t0 + r] = s1[r] + ramp[j] * (s2[r] - s1[r]);
84
+ }
85
+ }
86
+
87
+ // Index of shared memory with a skew to avoid bank conflicts when threads access
88
+ // addresses separated by BLOCKING.
89
+ __device__ __forceinline__ int skew(int i) { return i + (i >> 5); }
90
+
91
+ // Gradient with respect to x:
92
+ //
93
+ // gx[s] = sum_{m=0}^{L-1} gy[t] h_t[m], t = s - z + m, 0 <= t < T.
94
+ //
95
+ // A block of BWD_X_THREADS threads computes S = BWD_X_THREADS * BLOCKING
96
+ // consecutive outputs, and each thread computes BLOCKING consecutive outputs
97
+ // s0, ..., s0 + BLOCKING - 1. The block visits t in chunks of at most BWD_X_CHUNK
98
+ // samples within a frame. For each chunk, the required coefficients of the two
99
+ // frames and the weighted gradients are staged in shared memory. Each thread keeps
100
+ // the coefficients b[t + z - s0 - r] in a sliding window of registers.
101
+ //
102
+ // gy: (B, T), b: (B, N, L), ramp: (P,), gx: (B, T)
103
+ #define BWD_X_SPAN (BWD_X_THREADS * BLOCKING)
104
+ #define BWD_X_WIDTH (BWD_X_CHUNK + BWD_X_SPAN - 1)
105
+
106
+ extern "C" __global__ void __launch_bounds__(BWD_X_THREADS)
107
+ zerodf_backward_x(const SCALAR_T* __restrict__ gy,
108
+ const SCALAR_T* __restrict__ b,
109
+ const SCALAR_T* __restrict__ ramp, SCALAR_T* __restrict__ gx,
110
+ int B, int T, int N, int L, int P, int z) {
111
+ __shared__ SCALAR_T b1s[BWD_X_WIDTH + (BWD_X_WIDTH >> 5) + 1];
112
+ __shared__ SCALAR_T b2s[BWD_X_WIDTH + (BWD_X_WIDTH >> 5) + 1];
113
+ __shared__ SCALAR_T v1s[BWD_X_CHUNK];
114
+ __shared__ SCALAR_T v2s[BWD_X_CHUNK];
115
+
116
+ const int n_tiles = (T + BWD_X_SPAN - 1) / BWD_X_SPAN;
117
+ const long long bi = blockIdx.x / n_tiles;
118
+ const int S0 = (int)(blockIdx.x - bi * n_tiles) * BWD_X_SPAN;
119
+ const int s0 = S0 + threadIdx.x * BLOCKING;
120
+
121
+ const SCALAR_T* gyb = gy + bi * T;
122
+ const SCALAR_T* bb = b + bi * N * L;
123
+
124
+ // Range of t that contributes to the block and to this thread.
125
+ const int bt_lo = max(0, S0 - z);
126
+ const int bt_hi = min(T - 1, S0 + BWD_X_SPAN - 1 - z + L - 1);
127
+ const int tt_lo = max(0, s0 - z);
128
+ const int tt_hi = min(T - 1, s0 + BLOCKING - 1 - z + L - 1);
129
+
130
+ SCALAR_T acc[BLOCKING];
131
+ #pragma unroll
132
+ for (int r = 0; r < BLOCKING; r++) acc[r] = 0;
133
+
134
+ for (int n = bt_lo / P; n * P <= bt_hi; n++) {
135
+ const SCALAR_T* b1 = bb + (long long)n * L;
136
+ const SCALAR_T* b2 = bb + (long long)min(n + 1, N - 1) * L;
137
+ const int f_hi = min(bt_hi, n * P + P - 1);
138
+ for (int ta = max(bt_lo, n * P); ta <= f_hi; ta += BWD_X_CHUNK) {
139
+ const int tb = min(f_hi, ta + BWD_X_CHUNK - 1);
140
+
141
+ // Stage the coefficients b[base + i] for i in [0, width).
142
+ const int base = ta + z - (S0 + BWD_X_SPAN - 1);
143
+ const int width = tb - ta + BWD_X_SPAN;
144
+ __syncthreads();
145
+ for (int i = threadIdx.x; i < width; i += BWD_X_THREADS) {
146
+ const int m = base + i;
147
+ const bool valid = 0 <= m && m < L;
148
+ b1s[skew(i)] = valid ? b1[m] : (SCALAR_T)0;
149
+ b2s[skew(i)] = valid ? b2[m] : (SCALAR_T)0;
150
+ }
151
+ for (int i = threadIdx.x; i <= tb - ta; i += BWD_X_THREADS) {
152
+ const int t = ta + i;
153
+ const SCALAR_T v = gyb[t];
154
+ const SCALAR_T v2 = ramp[t - n * P] * v;
155
+ v1s[i] = v - v2;
156
+ v2s[i] = v2;
157
+ }
158
+ __syncthreads();
159
+
160
+ const int lo = max(ta, tt_lo);
161
+ const int hi = min(tb, tt_hi);
162
+ if (lo <= hi) {
163
+ // Local index of b[t + z - s0] in the staged buffer.
164
+ const int k = lo + z - s0 - base;
165
+ SCALAR_T w1[BLOCKING], w2[BLOCKING];
166
+ #pragma unroll
167
+ for (int r = 0; r < BLOCKING; r++) {
168
+ w1[r] = b1s[skew(k - r)];
169
+ w2[r] = b2s[skew(k - r)];
170
+ }
171
+ for (int t = lo; t <= hi; t++) {
172
+ const SCALAR_T v1 = v1s[t - ta];
173
+ const SCALAR_T v2 = v2s[t - ta];
174
+ #pragma unroll
175
+ for (int r = 0; r < BLOCKING; r++) acc[r] += v1 * w1[r] + v2 * w2[r];
176
+ #pragma unroll
177
+ for (int r = BLOCKING - 1; 0 < r; r--) {
178
+ w1[r] = w1[r - 1];
179
+ w2[r] = w2[r - 1];
180
+ }
181
+ const int kn = k + (t - lo) + 1;
182
+ if (t < hi) {
183
+ w1[0] = b1s[skew(kn)];
184
+ w2[0] = b2s[skew(kn)];
185
+ }
186
+ }
187
+ }
188
+ }
189
+ }
190
+
191
+ SCALAR_T* gxb = gx + bi * T;
192
+ #pragma unroll
193
+ for (int r = 0; r < BLOCKING; r++) {
194
+ if (s0 + r < T) gxb[s0 + r] = acc[r];
195
+ }
196
+ }
197
+
198
+ // Accumulates sum_j (a + c ramp[j]) gy[f0 + j] x[f0 + j + d - r] into acc[r] for
199
+ // r = 0, ..., BLOCKING - 1 over the frame starting at f0. The inputs are kept in
200
+ // a sliding window of registers.
201
+ __device__ __forceinline__ void accumulate_frame_b(
202
+ const SCALAR_T* __restrict__ gy, const SCALAR_T* __restrict__ x,
203
+ const SCALAR_T* __restrict__ ramp, int f0, int d, int P, int T, SCALAR_T a,
204
+ SCALAR_T c, SCALAR_T* acc) {
205
+ // Restrict j so that f0 + j + d - r is in [0, T) for some r.
206
+ const int j_lo = max(0, -d - f0);
207
+ const int j_hi = min(P - 1, T - 1 - d - f0 + BLOCKING - 1);
208
+ if (j_hi < j_lo) return;
209
+
210
+ SCALAR_T w[BLOCKING];
211
+ #pragma unroll
212
+ for (int r = 0; r < BLOCKING; r++) w[r] = load_or_zero(x, f0 + j_lo + d - r, T);
213
+ for (int j = j_lo; j <= j_hi; j++) {
214
+ const SCALAR_T v = (a + c * ramp[j]) * gy[f0 + j];
215
+ #pragma unroll
216
+ for (int r = 0; r < BLOCKING; r++) acc[r] += v * w[r];
217
+ #pragma unroll
218
+ for (int r = BLOCKING - 1; 0 < r; r--) w[r] = w[r - 1];
219
+ w[0] = load_or_zero(x, f0 + j + 1 + d, T);
220
+ }
221
+ }
222
+
223
+ // Gradient with respect to b. Frame n receives the weight (1 - r) from its own
224
+ // samples and r from the samples of frame n - 1. The last frame also receives r
225
+ // from its own samples because b[N] := b[N-1]. Each thread computes BLOCKING
226
+ // consecutive coefficients m0, ..., m0 + BLOCKING - 1 of the same frame.
227
+ //
228
+ // gy: (B, T), x: (B, T), ramp: (P,), gb: (B, N, L)
229
+ extern "C" __global__ void zerodf_backward_b(const SCALAR_T* __restrict__ gy,
230
+ const SCALAR_T* __restrict__ x,
231
+ const SCALAR_T* __restrict__ ramp,
232
+ SCALAR_T* __restrict__ gb, int B,
233
+ int T, int N, int L, int P, int z) {
234
+ const int G = (L + BLOCKING - 1) / BLOCKING;
235
+ const long long idx = (long long)blockIdx.x * blockDim.x + threadIdx.x;
236
+ if (idx >= (long long)B * N * G) return;
237
+ const long long bn = idx / G;
238
+ const int g = (int)(idx - bn * G);
239
+ const long long bi = bn / N;
240
+ const int n = (int)(bn - bi * N);
241
+ const int m0 = g * BLOCKING;
242
+ const int d = z - m0;
243
+
244
+ const SCALAR_T* gyb = gy + bi * T;
245
+ const SCALAR_T* xb = x + bi * T;
246
+
247
+ SCALAR_T acc[BLOCKING];
248
+ #pragma unroll
249
+ for (int r = 0; r < BLOCKING; r++) acc[r] = 0;
250
+ if (n == N - 1) {
251
+ accumulate_frame_b(gyb, xb, ramp, n * P, d, P, T, 1, 0, acc);
252
+ } else {
253
+ accumulate_frame_b(gyb, xb, ramp, n * P, d, P, T, 1, -1, acc);
254
+ }
255
+ if (0 < n) accumulate_frame_b(gyb, xb, ramp, (n - 1) * P, d, P, T, 0, 1, acc);
256
+
257
+ SCALAR_T* gbb = gb + bn * L;
258
+ #pragma unroll
259
+ for (int r = 0; r < BLOCKING; r++) {
260
+ if (m0 + r < L) gbb[m0 + r] = acc[r];
261
+ }
262
+ }
@@ -0,0 +1,213 @@
1
+ # ------------------------------------------------------------------------ #
2
+ # Copyright 2022 SPTK Working Group #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); #
5
+ # you may not use this file except in compliance with the License. #
6
+ # You may obtain a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ # ------------------------------------------------------------------------ #
16
+
17
+ from typing import cast
18
+
19
+ import torch
20
+
21
+ from .common import launch, prepare
22
+
23
+ _SOURCE = "zerodf.cu"
24
+ _KERNEL_NAMES = ("zerodf_forward", "zerodf_backward_x", "zerodf_backward_b")
25
+
26
+ # Number of consecutive samples processed by a thread.
27
+ _BLOCKING = 8
28
+ # Number of threads in a block and number of samples staged at once in the
29
+ # backward kernel with respect to x.
30
+ _BWD_X_THREADS = 32
31
+ _BWD_X_CHUNK = 256
32
+ _DEFINES = (
33
+ ("BLOCKING", _BLOCKING),
34
+ ("BWD_X_THREADS", _BWD_X_THREADS),
35
+ ("BWD_X_CHUNK", _BWD_X_CHUNK),
36
+ )
37
+
38
+
39
+ def _n_groups(n: int) -> int:
40
+ return (n + _BLOCKING - 1) // _BLOCKING
41
+
42
+
43
+ def _n_tiles(n: int) -> int:
44
+ span = _BWD_X_THREADS * _BLOCKING
45
+ return (n + span - 1) // span
46
+
47
+
48
+ # The output y = F(x, b) is bilinear in x and b. Let T(g, x, b) = <g, F(x, b)>.
49
+ # Then F, the gradient with respect to x (Gx), and the gradient with respect to b
50
+ # (Gb) are the partial derivatives of the trilinear form T:
51
+ #
52
+ # F(x, b) = dT/dg, Gx(g, b) = dT/dx, Gb(g, x) = dT/db.
53
+ #
54
+ # Hence, the derivatives of each of the three operations are given by the other
55
+ # two, which makes the operations differentiable to any order.
56
+
57
+
58
+ def _forward(x, b, ramp, zeroth_index):
59
+ B, T = x.shape
60
+ N, L = b.shape[-2:]
61
+ P = ramp.numel()
62
+ y = torch.empty_like(x)
63
+ launch(
64
+ _SOURCE,
65
+ "zerodf_forward",
66
+ B * N * _n_groups(P),
67
+ [x, b, ramp, y, B, T, N, L, P, zeroth_index],
68
+ dtype=x.dtype,
69
+ device=x.device,
70
+ defines=_DEFINES,
71
+ )
72
+ return y
73
+
74
+
75
+ def _backward_x(g, b, ramp, zeroth_index):
76
+ B, T = g.shape
77
+ N, L = b.shape[-2:]
78
+ P = ramp.numel()
79
+ grad_x = torch.empty_like(g)
80
+ launch(
81
+ _SOURCE,
82
+ "zerodf_backward_x",
83
+ B * _n_tiles(T) * _BWD_X_THREADS,
84
+ [g, b, ramp, grad_x, B, T, N, L, P, zeroth_index],
85
+ dtype=g.dtype,
86
+ device=g.device,
87
+ defines=_DEFINES,
88
+ block_size=_BWD_X_THREADS,
89
+ )
90
+ return grad_x
91
+
92
+
93
+ def _backward_b(g, x, ramp, zeroth_index, L):
94
+ B, T = x.shape
95
+ P = ramp.numel()
96
+ N = T // P
97
+ grad_b = torch.empty(B, N, L, dtype=x.dtype, device=x.device)
98
+ launch(
99
+ _SOURCE,
100
+ "zerodf_backward_b",
101
+ B * N * _n_groups(L),
102
+ [g, x, ramp, grad_b, B, T, N, L, P, zeroth_index],
103
+ dtype=x.dtype,
104
+ device=x.device,
105
+ defines=_DEFINES,
106
+ )
107
+ return grad_b
108
+
109
+
110
+ class _Forward(torch.autograd.Function):
111
+ @staticmethod
112
+ def forward(ctx, x, b, ramp, zeroth_index):
113
+ ctx.save_for_backward(x, b, ramp)
114
+ ctx.zeroth_index = zeroth_index
115
+ return _forward(x, b, ramp, zeroth_index)
116
+
117
+ @staticmethod
118
+ def backward(ctx, *grad):
119
+ x, b, ramp = ctx.saved_tensors
120
+ z = ctx.zeroth_index
121
+ g = grad[0].contiguous()
122
+ grad_x = grad_b = None
123
+ if ctx.needs_input_grad[0]:
124
+ grad_x = _BackwardX.apply(g, b, ramp, z)
125
+ if ctx.needs_input_grad[1]:
126
+ grad_b = _BackwardB.apply(g, x, ramp, z, b.size(-1))
127
+ return grad_x, grad_b, None, None
128
+
129
+
130
+ class _BackwardX(torch.autograd.Function):
131
+ @staticmethod
132
+ def forward(ctx, g, b, ramp, zeroth_index):
133
+ ctx.save_for_backward(g, b, ramp)
134
+ ctx.zeroth_index = zeroth_index
135
+ return _backward_x(g, b, ramp, zeroth_index)
136
+
137
+ @staticmethod
138
+ def backward(ctx, *grad):
139
+ g, b, ramp = ctx.saved_tensors
140
+ z = ctx.zeroth_index
141
+ h = grad[0].contiguous()
142
+ grad_g = grad_b = None
143
+ if ctx.needs_input_grad[0]:
144
+ grad_g = _Forward.apply(h, b, ramp, z)
145
+ if ctx.needs_input_grad[1]:
146
+ grad_b = _BackwardB.apply(g, h, ramp, z, b.size(-1))
147
+ return grad_g, grad_b, None, None
148
+
149
+
150
+ class _BackwardB(torch.autograd.Function):
151
+ @staticmethod
152
+ def forward(ctx, g, x, ramp, zeroth_index, L):
153
+ ctx.save_for_backward(g, x, ramp)
154
+ ctx.zeroth_index = zeroth_index
155
+ return _backward_b(g, x, ramp, zeroth_index, L)
156
+
157
+ @staticmethod
158
+ def backward(ctx, *grad):
159
+ g, x, ramp = ctx.saved_tensors
160
+ z = ctx.zeroth_index
161
+ h = grad[0].contiguous()
162
+ grad_g = grad_x = None
163
+ if ctx.needs_input_grad[0]:
164
+ grad_g = _Forward.apply(x, h, ramp, z)
165
+ if ctx.needs_input_grad[1]:
166
+ grad_x = _BackwardX.apply(g, h, ramp, z)
167
+ return grad_g, grad_x, None, None, None
168
+
169
+
170
+ def zerodf(
171
+ x: torch.Tensor, b: torch.Tensor, ramp: torch.Tensor, zeroth_index: int
172
+ ) -> torch.Tensor:
173
+ """Apply a time-varying all-zero filter using the CUDA kernel.
174
+
175
+ Parameters
176
+ ----------
177
+ x : Tensor [shape=(..., T)]
178
+ The input signal.
179
+
180
+ b : Tensor [shape=(..., T/P, L)]
181
+ The filter coefficients.
182
+
183
+ ramp : Tensor [shape=(P,)]
184
+ The interpolation weights within a frame.
185
+
186
+ zeroth_index : int
187
+ The index of the zeroth coefficient.
188
+
189
+ Returns
190
+ -------
191
+ out : Tensor [shape=(..., T)]
192
+ The output signal.
193
+
194
+ Raises
195
+ ------
196
+ KernelUnavailableError
197
+ If the kernels cannot be compiled.
198
+
199
+ """
200
+ if x.shape[:-1] != b.shape[:-2]:
201
+ raise ValueError("The batch shapes of x and b must be the same.")
202
+
203
+ # Compile all kernels in advance so that a compilation failure is detected
204
+ # before the autograd graph is built.
205
+ prepare(_SOURCE, _KERNEL_NAMES, dtype=x.dtype, device=x.device, defines=_DEFINES)
206
+
207
+ y = _Forward.apply(
208
+ x.reshape(-1, x.size(-1)).contiguous(),
209
+ b.reshape(-1, b.size(-2), b.size(-1)).contiguous(),
210
+ ramp.to(dtype=x.dtype).contiguous().view(-1),
211
+ zeroth_index,
212
+ )
213
+ return cast(torch.Tensor, y).view(x.shape)