Skip to content

Commit badccb8

Browse files
authored
issue/410 Feature: Add infinicore python package
1 parent 1f50740 commit badccb8

File tree

13 files changed

+300
-2
lines changed

13 files changed

+300
-2
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
xmake-version: latest
3232

3333
- name: Build & Install
34-
run: python scripts/install.py --omp=y
34+
run: python scripts/install.py --omp=y -y
3535

3636
- name: install python packages
3737
run: |

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global-include *

include/infinicore.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef __INFINICORE_API_HPP__
2+
#define __INFINICORE_API_HPP__
3+
4+
#include "infinicore/tensor.hpp"
5+
6+
#endif

include/infinicore/device.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef __INFINICORE_DEVICE_API_HPP__
2+
#define __INFINICORE_DEVICE_API_HPP__
3+
4+
#include <cstdint>
5+
#include <string>
6+
7+
namespace infinicore {
8+
9+
class Device {
10+
public:
11+
using Index = std::size_t;
12+
13+
enum class Type {
14+
cpu,
15+
cuda,
16+
meta,
17+
};
18+
19+
Device(const Type &type, const Index &index = 0);
20+
21+
const Type &get_type() const;
22+
23+
const Index &get_index() const;
24+
25+
std::string to_string() const;
26+
27+
static std::string to_string(const Type &type);
28+
29+
private:
30+
Type type_;
31+
32+
Index index_;
33+
};
34+
35+
} // namespace infinicore
36+
37+
#endif

include/infinicore/dtype.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef __INFINICORE_DTYPE_API_HPP__
2+
#define __INFINICORE_DTYPE_API_HPP__
3+
4+
#include <infinicore.h>
5+
6+
namespace infinicore {
7+
8+
enum class DataType {
9+
bfloat16 = INFINI_DTYPE_BF16,
10+
float16 = INFINI_DTYPE_F16,
11+
float32 = INFINI_DTYPE_F32,
12+
float64 = INFINI_DTYPE_F64,
13+
int32 = INFINI_DTYPE_I32,
14+
int64 = INFINI_DTYPE_I64,
15+
uint8 = INFINI_DTYPE_U8,
16+
};
17+
18+
std::string to_string(const DataType &dtype);
19+
20+
} // namespace infinicore
21+
22+
#endif

include/infinicore/tensor.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#ifndef __INFINICORE_TENSOR_API_HPP__
2+
#define __INFINICORE_TENSOR_API_HPP__
3+
4+
#include <vector>
5+
6+
#include "device.hpp"
7+
#include "dtype.hpp"
8+
9+
namespace infinicore {
10+
11+
class Tensor {
12+
public:
13+
using Size = std::size_t;
14+
15+
using Stride = std::ptrdiff_t;
16+
17+
using Shape = std::vector<Size>;
18+
19+
using Strides = std::vector<Stride>;
20+
21+
Tensor(const Shape &shape, const DataType &dtype, const Device &device);
22+
23+
const Shape &get_shape() const;
24+
25+
const DataType &get_dtype() const;
26+
27+
const Device &get_device() const;
28+
29+
private:
30+
Shape shape_;
31+
32+
DataType dtype_;
33+
34+
Device device_;
35+
};
36+
37+
} // namespace infinicore
38+
39+
#endif

pyproject.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[build-system]
2+
requires = ["setuptools"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "InfiniCore"
7+
version = "0.1.0"
8+
description = "InfiniCore 是一个跨平台统一编程工具集,为不同芯片平台的功能(包括计算、运行时、通信等)提供统一 C 语言接口。"
9+
readme = "README.md"
10+
dependencies = []
11+
requires-python = ">=3.10"
12+
classifiers = [
13+
"Programming Language :: Python :: 3",
14+
"License :: OSI Approved :: MIT License",
15+
"Operating System :: OS Independent",
16+
]
17+
18+
[project.urls]
19+
Homepage = "https://github.com/InfiniTensor/InfiniCore"
20+
Issues = "https://github.com/InfiniTensor/InfiniCore/issues"
21+
22+
[tool.ruff]
23+
src = [".", "src"]
24+
25+
[tool.ruff.lint]
26+
select = ["E4", "E7", "E9", "F", "I"]

setup.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import glob
2+
import os
3+
import subprocess
4+
from pathlib import Path
5+
6+
from setuptools import setup
7+
from setuptools.command.build_py import build_py
8+
9+
INSTALLATION_DIR = os.getenv("INFINI_ROOT", str(Path.home() / ".infini"))
10+
11+
LIB_DIR = os.path.join(INSTALLATION_DIR, "lib")
12+
13+
PACKAGE_NAME = "infinicore"
14+
15+
PACKAGE_DIR = os.path.join(INSTALLATION_DIR, PACKAGE_NAME)
16+
17+
18+
class BuildPy(build_py):
19+
def run(self):
20+
subprocess.run(["xmake", "build", "-y"])
21+
subprocess.run(["xmake", "install"])
22+
built_lib = glob.glob(os.path.join(LIB_DIR, f"{PACKAGE_NAME}.*"))[0]
23+
os.makedirs(PACKAGE_DIR, exist_ok=True)
24+
self.copy_file(built_lib, PACKAGE_DIR)
25+
26+
27+
setup(
28+
cmdclass={"build_py": BuildPy},
29+
package_dir={"": "."},
30+
)

src/infinicore/device.cc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <infinicore.hpp>
2+
3+
namespace infinicore {
4+
5+
Device::Device(const Type &type, const Index &index) : type_{type}, index_{index} {}
6+
7+
const Device::Type &Device::get_type() const {
8+
return type_;
9+
}
10+
11+
const Device::Index &Device::get_index() const {
12+
return index_;
13+
}
14+
15+
std::string Device::to_string() const {
16+
return to_string(type_) + ":" + std::to_string(index_);
17+
}
18+
19+
std::string Device::to_string(const Type &type) {
20+
switch (type) {
21+
case Type::cpu:
22+
return "cpu";
23+
case Type::cuda:
24+
return "cuda";
25+
case Type::meta:
26+
return "meta";
27+
}
28+
29+
// TODO: Add error handling.
30+
return "";
31+
}
32+
33+
} // namespace infinicore

src/infinicore/dtype.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <infinicore.hpp>
2+
3+
namespace infinicore {
4+
5+
std::string to_string(const DataType &dtype) {
6+
std::string str{"infinicore."};
7+
8+
switch (dtype) {
9+
case DataType::bfloat16:
10+
str += "bfloat16";
11+
break;
12+
case DataType::float16:
13+
str += "float16";
14+
break;
15+
case DataType::float32:
16+
str += "float32";
17+
break;
18+
case DataType::float64:
19+
str += "float64";
20+
break;
21+
case DataType::int32:
22+
str += "int32";
23+
break;
24+
case DataType::int64:
25+
str += "int64";
26+
break;
27+
case DataType::uint8:
28+
str += "uint8";
29+
break;
30+
}
31+
32+
return str;
33+
}
34+
35+
} // namespace infinicore

0 commit comments

Comments
 (0)