Skip to content

Commit 25ceee7

Browse files
authored
Simplify extension initialization (#1649)
1 parent ac1c9d5 commit 25ceee7

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

torchaudio/extension/extension.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
import warnings
2-
import importlib
32

43
import torch
54
from torchaudio._internal import module_utils as _mod_utils
65

76

87
def _init_extension():
9-
ext = 'torchaudio._torchaudio'
10-
if _mod_utils.is_module_available(ext):
11-
_init_script_module(ext)
12-
import torchaudio._torchaudio # noqa
8+
if _mod_utils.is_module_available('torchaudio._torchaudio'):
9+
# Note this import has two purposes
10+
# 1. to extract the path of the extension module so that
11+
# we can initialize the script module with the path.
12+
# 2. so that torchaudio._torchaudio is accessible in other modules.
13+
# Look at sox_io_backend which uses `torchaudio._torchaudio.XXX`,
14+
# assuming that the module `_torchaudio` is accessible.
15+
import torchaudio._torchaudio
16+
_init_script_module(torchaudio._torchaudio.__file__)
1317
else:
1418
warnings.warn('torchaudio C++ extension is not available.')
1519

1620

17-
def _init_script_module(module):
18-
path = importlib.util.find_spec(module).origin
21+
def _init_script_module(path):
1922
torch.classes.load_library(path)
2023
torch.ops.load_library(path)

0 commit comments

Comments
 (0)