Skip to content

Commit 3169840

Browse files
committed
Create dummymodule _C instead of using load_library(blahso)
1 parent 4be2205 commit 3169840

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

extension_cpp/__init__.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
11
import torch
22
from pathlib import Path
3-
4-
so_files = list(Path(__file__).parent.glob("_C*.so"))
5-
assert (
6-
len(so_files) == 1
7-
), f"Expected one _C*.so file, found {len(so_files)}"
8-
torch.ops.load_library(so_files[0])
9-
10-
from . import ops
3+
from . import _C, ops

extension_cpp/csrc/muladd.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1-
#include <torch/extension.h>
1+
#include <Python.h>
2+
#include <ATen/Operators.h>
3+
#include <torch/all.h>
4+
#include <torch/library.h>
25

36
#include <vector>
47

8+
extern "C" {
9+
/* Creates a dummy empty _C module that can be imported from Python.
10+
The import from Python will load the .so associated with this extension
11+
built from this file, so that all the TORCH_LIBRARY calls below are run.*/
12+
PyObject* PyInit__C(void)
13+
{
14+
static struct PyModuleDef module_def = {
15+
PyModuleDef_HEAD_INIT,
16+
"_C", /* name of module */
17+
NULL, /* module documentation, may be NULL */
18+
-1, /* size of per-interpreter state of the module,
19+
or -1 if the module keeps state in global variables. */
20+
NULL, /* methods */
21+
};
22+
return PyModule_Create(&module_def);
23+
}
24+
}
25+
526
namespace extension_cpp {
627

728
at::Tensor mymuladd_cpu(const at::Tensor& a, const at::Tensor& b, double c) {

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def get_extensions():
3838
"cxx": [
3939
"-O3" if not debug_mode else "-O0",
4040
"-fdiagnostics-color=always",
41+
"-DPy_LIMITED_API=0x03090000",
4142
],
4243
"nvcc": [
4344
"-O3" if not debug_mode else "-O0",

0 commit comments

Comments
 (0)