Merged
mock torch when compile pypto kernel #9115
poorwillAdd on Jul 24
mock torch when compile pypto kernel #9115
Merged
Merged fromwangbing110/ops-transformermasterintocann/ops-transformermaster
Total: 2 files changed+15-3
| @@ -24,6 +24,14 @@ import os | |||
| 24 | import shutil | 24 | import shutil |
| 25 | import sys | 25 | import sys |
| 26 | from pathlib import Path | 26 | from pathlib import Path |
| 27 | +from unittest.mock import MagicMock | ||
| 28 | + | ||
| 29 | + | ||
| 30 | +def _install_build_only_torch_stub(): | ||
| 31 | + """Stub runtime-only dependencies that binary codegen does not use.""" | ||
| 32 | + sys.modules["torch"] = MagicMock(name="torch") | ||
| 33 | + sys.modules["torch_npu"] = MagicMock(name="torch_npu") | ||
| 34 | + | ||
| 27 | 35 | ||
| 28 | # soc (ASCEND_COMPUTE_UNIT, lowercased) -> pypto arch token used by PYPTO_JIT_ARCH. | 36 | # soc (ASCEND_COMPUTE_UNIT, lowercased) -> pypto arch token used by PYPTO_JIT_ARCH. |
| 29 | _ARCH_MAP = { | 37 | _ARCH_MAP = { |
| @@ -36,6 +44,7 @@ _ARCH_MAP = { | |||
| 36 | def _load_kernels(py_file: Path): | 44 | def _load_kernels(py_file: Path): |
| 37 | """Import the kernel module and return all @pl.jit kernel objects defined in it.""" | 45 | """Import the kernel module and return all @pl.jit kernel objects defined in it.""" |
| 38 | sys.path.insert(0, str(py_file.parent)) | 46 | sys.path.insert(0, str(py_file.parent)) |
| 47 | + _install_build_only_torch_stub() | ||
| 39 | 48 | ||
| 40 | import pypto_pro.runtime.opc.pypto_compile as pto_compile | 49 | import pypto_pro.runtime.opc.pypto_compile as pto_compile |
| 41 | from pypto_pro.runtime.jit import _TileJitKernel | 50 | from pypto_pro.runtime.jit import _TileJitKernel |
| @@ -342,9 +342,12 @@ PYPTO_COMPILE_OP_API = """ | |||
| 342 | 342 | ||
| 343 | # Extra import appended to the wrapper head for pypto ops (kept out of the default head so non-pypto | 343 | # Extra import appended to the wrapper head for pypto ops (kept out of the default head so non-pypto |
| 344 | # builds / environments without pypto_pro are unaffected). | 344 | # builds / environments without pypto_pro are unaffected). |
| 345 | -PYPTO_IMPORT_HEADER = ( | 345 | +PYPTO_IMPORT_HEADER = """ |
| 346 | - "from pypto_pro.runtime.opc.pypto_compile import pypto_compile_op\n" | 346 | +from unittest.mock import MagicMock as _PyptoMagicMock |
| 347 | -) | 347 | +sys.modules["torch"] = _PyptoMagicMock(name="torch") |
| 348 | +sys.modules["torch_npu"] = _PyptoMagicMock(name="torch_npu") | ||
| 349 | +from pypto_pro.runtime.opc.pypto_compile import pypto_compile_op | ||
| 350 | +""" | ||
| 348 | 351 | ||
| 349 | SUP_API = """ | 352 | SUP_API = """ |
| 350 | def {}({}{}): | 353 | def {}({}{}): |