Skip to content

Commit

Permalink
Revert "[inductor] Load python modules using importlib (#126454)"
Browse files Browse the repository at this point in the history
This reverts commit faa26df.

Reverted #126454 on behalf of https://github.com/DanilBaibak due to Break internal build ([comment](#126454 (comment)))
  • Loading branch information
pytorchmergebot committed May 20, 2024
1 parent cf35a59 commit 5ad2f10
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions torch/_inductor/runtime/compile_tasks.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import annotations

import functools
import importlib
import os
import sys
import warnings
from types import ModuleType
from typing import Any, Callable


Expand All @@ -31,20 +31,19 @@ def _reload_python_module_in_subproc(key, path):


def _reload_python_module(key, path):
spec = importlib.util.spec_from_file_location(f"{__name__}.{key}", path)
if spec is None or spec.loader is None:
raise RuntimeError(f"Failed to import {path}: path not found")
module = importlib.util.module_from_spec(spec)
module.key = key # type: ignore[attr-defined]
try:
spec.loader.exec_module(module)
except Exception as e:
raise RuntimeError(
f"Failed to import {path}\n{type(e).__name__}: {e}"
) from None

sys.modules[module.__name__] = module
return module
with open(path) as f:
try:
code = compile(f.read(), path, "exec")
except Exception as e:
raise RuntimeError(
f"Failed to import {path}\n{type(e).__name__}: {e}"
) from None
mod = ModuleType(f"{__name__}.{key}")
mod.__file__ = path
mod.key = key # type: ignore[attr-defined]
exec(code, mod.__dict__, mod.__dict__)
sys.modules[mod.__name__] = mod
return mod


@functools.lru_cache(None)
Expand Down

0 comments on commit 5ad2f10

Please sign in to comment.