Skip to content

Commit 5b02f42

Browse files
committed
移除了 infinicore::py
1 parent 817d151 commit 5b02f42

32 files changed

Lines changed: 513 additions & 628 deletions

python/infinicore/__init__.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from infinicore.device import device
2+
from infinicore.dtype import (
3+
bfloat16,
4+
bool,
5+
cdouble,
6+
cfloat,
7+
chalf,
8+
complex32,
9+
complex64,
10+
complex128,
11+
double,
12+
float,
13+
float16,
14+
float32,
15+
float64,
16+
half,
17+
int,
18+
int8,
19+
int16,
20+
int32,
21+
int64,
22+
long,
23+
short,
24+
uint8,
25+
)
26+
from infinicore.ops.matmul import matmul
27+
from infinicore.tensor import empty, from_blob, ones, zeros
28+
29+
__all__ = [
30+
"device",
31+
"bfloat16",
32+
"bool",
33+
"cdouble",
34+
"cfloat",
35+
"chalf",
36+
"complex32",
37+
"complex64",
38+
"complex128",
39+
"double",
40+
"float",
41+
"float16",
42+
"float32",
43+
"float64",
44+
"half",
45+
"int",
46+
"int8",
47+
"int16",
48+
"int32",
49+
"int64",
50+
"long",
51+
"short",
52+
"uint8",
53+
"matmul",
54+
"empty",
55+
"from_blob",
56+
"ones",
57+
"zeros",
58+
]

python/infinicore/device.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import _infinicore
2+
3+
4+
class device:
5+
def __init__(self, type=None, index=None):
6+
if type is None:
7+
type = "cpu"
8+
9+
if isinstance(type, device):
10+
self.type = type.type
11+
self.index = type.index
12+
13+
return
14+
15+
if ":" in type:
16+
if index is not None:
17+
raise ValueError(
18+
'`index` should not be provided when `type` contains `":"`.'
19+
)
20+
21+
type, index = type.split(":")
22+
index = int(index)
23+
24+
self.type = type
25+
26+
self.index = index
27+
28+
_type, _index = device._to_infinicore_device(type, index if index else 0)
29+
30+
self._underlying = _infinicore.Device(_type, _index)
31+
32+
def __repr__(self):
33+
return f"device(type='{self.type}'{f', index={self.index}' if self.index is not None else ''})"
34+
35+
def __str__(self):
36+
return f"{self.type}{f':{self.index}' if self.index is not None else ''}"
37+
38+
@staticmethod
39+
def _to_infinicore_device(type, index):
40+
all_device_types = tuple(_infinicore.Device.Type.__members__.values())[:-1]
41+
all_device_count = tuple(
42+
_infinicore.get_device_count(device) for device in all_device_types
43+
)
44+
45+
torch_devices = {
46+
torch_type: {
47+
infinicore_type: 0
48+
for infinicore_type in all_device_types
49+
if _TORCH_DEVICE_MAP[infinicore_type] == torch_type
50+
}
51+
for torch_type in _TORCH_DEVICE_MAP.values()
52+
}
53+
54+
for i, count in enumerate(all_device_count):
55+
infinicore_device_type = _infinicore.Device.Type(i)
56+
torch_devices[_TORCH_DEVICE_MAP[infinicore_device_type]][
57+
infinicore_device_type
58+
] += count
59+
60+
for infinicore_device_type, infinicore_device_count in torch_devices[
61+
type
62+
].items():
63+
for i in range(infinicore_device_count):
64+
if index == 0:
65+
return infinicore_device_type, i
66+
67+
index -= 1
68+
69+
70+
_TORCH_DEVICE_MAP = {
71+
_infinicore.Device.Type.CPU: "cpu",
72+
_infinicore.Device.Type.NVIDIA: "cuda",
73+
_infinicore.Device.Type.CAMBRICON: "mlu",
74+
_infinicore.Device.Type.ASCEND: "npu",
75+
_infinicore.Device.Type.METAX: "cuda",
76+
_infinicore.Device.Type.MOORE: "musa",
77+
_infinicore.Device.Type.ILUVATAR: "cuda",
78+
_infinicore.Device.Type.KUNLUN: "cuda",
79+
_infinicore.Device.Type.SUGON: "cuda",
80+
}

python/infinicore/dtype.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import _infinicore
2+
3+
4+
class dtype:
5+
def __init__(self, data_type):
6+
"""An internal method. Please do not use this directly."""
7+
8+
self._underlying = data_type
9+
10+
def __repr__(self):
11+
repr_map = {
12+
_infinicore.DataType.BYTE: "uint8",
13+
_infinicore.DataType.BOOL: "bool",
14+
_infinicore.DataType.I8: "int8",
15+
_infinicore.DataType.I16: "int16",
16+
_infinicore.DataType.I32: "int32",
17+
_infinicore.DataType.I64: "int64",
18+
_infinicore.DataType.U8: "uint8",
19+
_infinicore.DataType.U16: "uint16",
20+
_infinicore.DataType.U32: "uint32",
21+
_infinicore.DataType.U64: "uint64",
22+
_infinicore.DataType.F8: "float8",
23+
_infinicore.DataType.F16: "float16",
24+
_infinicore.DataType.F32: "float32",
25+
_infinicore.DataType.F64: "float64",
26+
_infinicore.DataType.C16: "complex16",
27+
_infinicore.DataType.C32: "complex32",
28+
_infinicore.DataType.C64: "complex64",
29+
_infinicore.DataType.C128: "complex128",
30+
_infinicore.DataType.BF16: "bfloat16",
31+
}
32+
33+
return f"infinicore.{repr_map[self._underlying]}"
34+
35+
36+
float32 = dtype(_infinicore.DataType.F32)
37+
float = float32
38+
float64 = dtype(_infinicore.DataType.F64)
39+
double = float64
40+
complex32 = dtype(_infinicore.DataType.C32)
41+
chalf = complex32
42+
complex64 = dtype(_infinicore.DataType.C64)
43+
cfloat = complex64
44+
complex128 = dtype(_infinicore.DataType.C128)
45+
cdouble = complex128
46+
float16 = dtype(_infinicore.DataType.F16)
47+
half = float16
48+
bfloat16 = dtype(_infinicore.DataType.BF16)
49+
uint8 = dtype(_infinicore.DataType.U8)
50+
int8 = dtype(_infinicore.DataType.I8)
51+
int16 = dtype(_infinicore.DataType.I16)
52+
short = int16
53+
int32 = dtype(_infinicore.DataType.I32)
54+
int = int32
55+
int64 = dtype(_infinicore.DataType.I64)
56+
long = int64
57+
bool = dtype(_infinicore.DataType.BOOL)

python/infinicore/ops/__init__.py

Whitespace-only changes.

python/infinicore/ops/matmul.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import _infinicore
2+
3+
from infinicore.tensor import Tensor
4+
5+
6+
def matmul(input, other, *, out=None):
7+
if out is None:
8+
return Tensor(_infinicore.matmul(input._underlying, other._underlying))
9+
10+
_infinicore.matmul_(out._underlying, input._underlying, other._underlying)

python/infinicore/tensor.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import ctypes
2+
3+
import _infinicore
4+
5+
6+
class Tensor:
7+
def __init__(self, tensor):
8+
"""An internal method. Please do not use this directly."""
9+
10+
self._underlying = tensor
11+
12+
@property
13+
def shape(self):
14+
return self._underlying.shape
15+
16+
@property
17+
def dtype(self):
18+
return self._underlying.dtype
19+
20+
@property
21+
def device(self):
22+
return self._underlying.device
23+
24+
@property
25+
def ndim(self):
26+
return self._underlying.device
27+
28+
def data_ptr(self):
29+
return self._underlying.data_ptr
30+
31+
def size(self, dim=None):
32+
if dim is None:
33+
return self.shape
34+
35+
return self.shape[dim]
36+
37+
def stride(self, dim=None):
38+
if dim is None:
39+
return self._underlying.strides
40+
41+
return self._underlying.strides[dim]
42+
43+
def numel(self):
44+
return self._underlying.numel()
45+
46+
def is_contiguous(self):
47+
return self._underlying.is_contiguous()
48+
49+
def is_is_pinned(self):
50+
return self._underlying.is_is_pinned()
51+
52+
def copy_(self, src):
53+
return Tensor(self._underlying.copy_(src._underlying))
54+
55+
def to(self, *args, **kwargs):
56+
return Tensor(
57+
self._underlying.to(*tuple(arg._underlying for arg in args), **kwargs)
58+
)
59+
60+
def as_strided(self, size, stride):
61+
Tensor(self._underlying.as_strided(size, stride))
62+
63+
def contiguous(self):
64+
return Tensor(self._underlying.contiguous())
65+
66+
def permute(self, dims):
67+
return Tensor(self._underlying.permute(dims))
68+
69+
def view(self, shape):
70+
return Tensor(self._underlying.view(shape))
71+
72+
73+
def empty(size, *, dtype=None, device=None, pin_memory=False):
74+
return Tensor(
75+
_infinicore.empty(size, dtype._underlying, device._underlying, pin_memory)
76+
)
77+
78+
79+
def zeros(size, *, dtype=None, device=None, pin_memory=False):
80+
return Tensor(
81+
_infinicore.zeros(size, dtype._underlying, device._underlying, pin_memory)
82+
)
83+
84+
85+
def ones(size, *, dtype=None, device=None, pin_memory=False):
86+
return Tensor(
87+
_infinicore.ones(size, dtype._underlying, device._underlying, pin_memory)
88+
)
89+
90+
91+
def from_blob(data_ptr, size, *, dtype=None, device=None):
92+
return Tensor(
93+
_infinicore.from_blob(data_ptr, size, dtype._underlying, device._underlying)
94+
)

setup.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import glob
21
import os
2+
import shutil
33
import subprocess
44
from pathlib import Path
55

@@ -10,9 +10,9 @@
1010

1111
LIB_DIR = os.path.join(INSTALLATION_DIR, "lib")
1212

13-
PACKAGE_NAME = "infinicore"
13+
PYTHON_PACKAGE_DIR = os.path.join("python", "infinicore")
1414

15-
PACKAGE_DIR = os.path.join(INSTALLATION_DIR, PACKAGE_NAME)
15+
LIB_PACKAGE_DIR = os.path.join(LIB_DIR, "infinicore")
1616

1717

1818
class BuildPy(build_py):
@@ -21,12 +21,11 @@ def run(self):
2121
subprocess.run(["xmake", "install"])
2222
subprocess.run(["xmake", "build", "-y", "infinicore"])
2323
subprocess.run(["xmake", "install", "infinicore"])
24-
built_lib = glob.glob(os.path.join(LIB_DIR, f"{PACKAGE_NAME}.*"))[0]
25-
os.makedirs(PACKAGE_DIR, exist_ok=True)
26-
self.copy_file(built_lib, PACKAGE_DIR)
2724

25+
if os.path.exists(LIB_PACKAGE_DIR):
26+
shutil.rmtree(LIB_PACKAGE_DIR)
2827

29-
setup(
30-
cmdclass={"build_py": BuildPy},
31-
package_dir={"": "."},
32-
)
28+
shutil.copytree(PYTHON_PACKAGE_DIR, LIB_PACKAGE_DIR)
29+
30+
31+
setup(cmdclass={"build_py": BuildPy})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <pybind11/pybind11.h>
4+
5+
#include "infinicore.hpp"
6+
7+
namespace py = pybind11;
8+
9+
namespace infinicore::context {
10+
11+
inline void bind(py::module &m) {
12+
m.def("get_device", &getDevice);
13+
m.def("get_device_count", &getDeviceCount);
14+
}
15+
16+
} // namespace infinicore::context

src/infinicore/pybind11/device.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
#include <pybind11/pybind11.h>
4+
5+
#include "infinicore.hpp"
6+
7+
namespace py = pybind11;
8+
9+
namespace infinicore::device {
10+
11+
inline void bind(py::module &m) {
12+
py::class_<Device> device(m, "Device");
13+
14+
py::enum_<Device::Type>(device, "Type")
15+
.value("CPU", Device::Type::CPU)
16+
.value("NVIDIA", Device::Type::NVIDIA)
17+
.value("CAMBRICON", Device::Type::CAMBRICON)
18+
.value("ASCEND", Device::Type::ASCEND)
19+
.value("METAX", Device::Type::METAX)
20+
.value("MOORE", Device::Type::MOORE)
21+
.value("ILUVATAR", Device::Type::ILUVATAR)
22+
.value("KUNLUN", Device::Type::KUNLUN)
23+
.value("SUGON", Device::Type::SUGON)
24+
.value("COUNT", Device::Type::COUNT);
25+
26+
device
27+
.def(py::init<const Device::Type &, const Device::Index &>(),
28+
py::arg("type") = Device::Type::CPU, py::arg("index") = 0)
29+
.def_property_readonly("type", &Device::getType)
30+
.def_property_readonly("index", &Device::getIndex)
31+
.def("__str__", static_cast<std::string (Device::*)() const>(&Device::toString));
32+
}
33+
34+
} // namespace infinicore::device

0 commit comments

Comments
 (0)