From 221032bddd1ace430f37282c421d7e3330dd927d Mon Sep 17 00:00:00 2001 From: moto <855818+mthrok@users.noreply.github.com> Date: Tue, 12 May 2020 16:03:31 +0000 Subject: [PATCH] Fix import path When building torchtext with buck, the path to _torchtext.so has to be normalized. --- torchtext/__init__.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/torchtext/__init__.py b/torchtext/__init__.py index ca04a6bbd5..b73f2cc6ce 100644 --- a/torchtext/__init__.py +++ b/torchtext/__init__.py @@ -14,9 +14,23 @@ def _init_extension(): + import os + import importlib import torch - torch.ops.load_library('torchtext/_torchtext.so') - torch.classes.load_library('torchtext/_torchtext.so') + + # load the custom_op_library and register the custom ops + lib_dir = os.path.dirname(__file__) + loader_details = ( + importlib.machinery.ExtensionFileLoader, + importlib.machinery.EXTENSION_SUFFIXES + ) + + extfinder = importlib.machinery.FileFinder(lib_dir, loader_details) + ext_specs = extfinder.find_spec("_torchtext") + if ext_specs is None: + raise ImportError + torch.ops.load_library(ext_specs.origin) + torch.classes.load_library(ext_specs.origin) _init_extension()