Skip to content

Commit 9a05446

Browse files
PanZezhong1725voltjiawooway777
authored
issue/461 InfiniCore 推理运行时
Co-authored-by: Jiacheng Huang <huangjiacheng0709@outlook.com> Co-authored-by: wooway777 <wooway777@gmail.com>
1 parent 37411f6 commit 9a05446

72 files changed

Lines changed: 3418 additions & 158 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ jobs:
1919

2020
- name: checkout code
2121
uses: actions/checkout@v4
22+
with:
23+
submodules: recursive
24+
fetch-depth: 0
2225

2326
- name: Check Format
2427
run: |

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ build/
1010

1111
# Python
1212
__pycache__/
13+
*.egg-info/
1314

1415
# Log
1516
*.log
@@ -22,3 +23,8 @@ cache/
2223

2324
#GGUF
2425
*.gguf
26+
27+
# Compressed
28+
*.gz
29+
*.zip
30+
*.tar

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "third_party/spdlog"]
2+
path = third_party/spdlog
3+
url = https://github.com/gabime/spdlog.git

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ API 定义以及使用方式详见 [`InfiniCore文档`](https://github.com/Infin
2828

2929
## 配置和使用
3030

31+
### 子模块
32+
33+
由于仓库中含有子模块,所以在克隆时请添加 `--recursive``--recurse-submodules`,如:
34+
35+
```shell
36+
git clone --recursive https://github.com/InfiniTensor/InfiniCore.git
37+
```
38+
39+
或者在普通克隆后进行更新:
40+
41+
```shell
42+
git submodule update --init --recursive
43+
```
44+
3145
### 一键安装
3246

3347
`script/` 目录中提供了 `install.py` 安装脚本。使用方式如下:

include/infinicore.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#ifndef __INFINICORE_API_HPP__
2-
#define __INFINICORE_API_HPP__
1+
#pragma once
32

3+
#include "infinicore/ops.hpp"
44
#include "infinicore/tensor.hpp"
5-
6-
#endif
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#pragma once
2+
3+
#include <cstddef>
4+
#include <functional>
5+
#include <iostream>
6+
#include <list>
7+
#include <optional>
8+
#include <stdexcept>
9+
#include <unordered_map>
10+
11+
namespace infinicore::common {
12+
template <typename Key, typename Value>
13+
class LRUCache {
14+
public:
15+
using KeyValuePair = std::pair<Key, Value>;
16+
using ListIt = typename std::list<KeyValuePair>::iterator;
17+
using Destructor = std::function<void(Value &)>;
18+
19+
explicit LRUCache(size_t capacity = 100, Destructor destructor = nullptr)
20+
: capacity_(capacity), destructor_(destructor) {
21+
if (capacity == 0) {
22+
capacity_ = UINT64_MAX; // effectively unbounded
23+
}
24+
}
25+
26+
~LRUCache() {
27+
cleanup();
28+
}
29+
30+
bool contains(const Key &key) const {
31+
return map_.find(key) != map_.end();
32+
}
33+
34+
void put(const Key &key, const Value &value) {
35+
auto it = map_.find(key);
36+
if (it != map_.end()) {
37+
if (destructor_) {
38+
destructor_(it->second->second);
39+
}
40+
it->second->second = value;
41+
touch(it);
42+
} else {
43+
// insert new
44+
if (list_.size() >= capacity_) {
45+
evictLRU();
46+
}
47+
list_.emplace_front(key, value);
48+
map_[key] = list_.begin();
49+
}
50+
}
51+
52+
std::optional<Value> get(const Key &key) {
53+
auto it = map_.find(key);
54+
if (it == map_.end()) {
55+
return std::nullopt;
56+
}
57+
touch(it);
58+
return it->second->second;
59+
}
60+
61+
std::optional<Value> get(const Key &key) const {
62+
auto it = map_.find(key);
63+
if (it == map_.end()) {
64+
return std::nullopt;
65+
}
66+
// Note: can't touch in const context
67+
return it->second->second;
68+
}
69+
70+
void setDestructor(Destructor destructor) {
71+
destructor_ = destructor;
72+
}
73+
74+
void setCapacity(size_t capacity) {
75+
capacity_ = capacity;
76+
while (list_.size() > capacity_) {
77+
evictLRU();
78+
}
79+
}
80+
81+
void clear() {
82+
if (destructor_) {
83+
for (auto &item : list_) {
84+
safeDestruct(item.second);
85+
}
86+
}
87+
list_.clear();
88+
map_.clear();
89+
}
90+
91+
const std::list<KeyValuePair> &getAllItems() const {
92+
return list_;
93+
}
94+
95+
protected:
96+
std::list<KeyValuePair> list_; // front = most recent, back = least
97+
98+
private:
99+
void touch(typename std::unordered_map<Key, ListIt>::iterator it) {
100+
// move this key to front (most recent)
101+
list_.splice(list_.begin(), list_, it->second);
102+
it->second = list_.begin();
103+
}
104+
105+
void safeDestruct(Value &value) {
106+
if (!destructor_) {
107+
return;
108+
}
109+
110+
try {
111+
destructor_(value);
112+
} catch (const std::exception &e) {
113+
// Built-in default error handling
114+
std::cerr << "Cache destructor error (type: " << typeid(Value).name()
115+
<< "): " << e.what() << std::endl;
116+
}
117+
}
118+
119+
void evictLRU() {
120+
if (!list_.empty()) {
121+
auto &kv = list_.back();
122+
safeDestruct(kv.second);
123+
map_.erase(kv.first);
124+
list_.pop_back();
125+
}
126+
}
127+
128+
void cleanup() {
129+
clear();
130+
}
131+
132+
size_t capacity_;
133+
std::unordered_map<Key, ListIt> map_;
134+
Destructor destructor_;
135+
};
136+
137+
} // namespace infinicore::common

include/infinicore/common/hash.hpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#pragma once
2+
3+
#include "../tensor.hpp"
4+
5+
#include <type_traits>
6+
7+
namespace infinicore {
8+
9+
// Base hash_combine for arithmetic types
10+
template <typename T>
11+
std::enable_if_t<std::is_arithmetic_v<T>, void>
12+
hash_combine(size_t &seed, const T &value) {
13+
seed ^= std::hash<T>{}(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
14+
}
15+
16+
// Specialization for Tensor
17+
inline void hash_combine(size_t &seed, Tensor tensor) {
18+
hash_combine(seed, static_cast<size_t>(tensor->dtype()));
19+
for (Size shape : tensor->shape()) {
20+
hash_combine(seed, shape);
21+
}
22+
for (Stride stride : tensor->strides()) {
23+
hash_combine(seed, static_cast<size_t>(stride));
24+
}
25+
}
26+
27+
// Specialization for std::string
28+
inline void hash_combine(size_t &seed, const std::string &str) {
29+
hash_combine(seed, std::hash<std::string>{}(str));
30+
}
31+
32+
// Specialization for const char*
33+
inline void hash_combine(size_t &seed, const char *str) {
34+
hash_combine(seed, std::string(str));
35+
}
36+
37+
// Variadic template for multiple arguments
38+
template <typename First, typename... Rest>
39+
void hash_combine(size_t &seed, const First &first, const Rest &...rest) {
40+
hash_combine(seed, first);
41+
hash_combine(seed, rest...);
42+
}
43+
44+
// Base case for variadic template
45+
inline void hash_combine(size_t &seed) {
46+
// Base case - do nothing
47+
}
48+
49+
// Convenience function to hash multiple values
50+
template <typename... Types>
51+
size_t hash_combine(const Types &...values) {
52+
size_t seed = 0;
53+
hash_combine(seed, values...);
54+
return seed;
55+
}
56+
57+
} // namespace infinicore
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include "../device.hpp"
4+
#include "../memory.hpp"
5+
6+
#include <infiniop.h>
7+
#include <infinirt.h>
8+
9+
#include <memory>
10+
11+
namespace infinicore {
12+
13+
namespace context {
14+
void setDevice(Device device);
15+
Device getDevice();
16+
size_t getDeviceCount(Device::Type type);
17+
18+
infinirtStream_t getStream();
19+
infiniopHandle_t getInfiniopHandle();
20+
21+
void syncStream();
22+
void syncDevice();
23+
24+
std::shared_ptr<Memory> allocateMemory(size_t size);
25+
std::shared_ptr<Memory> allocateHostMemory(size_t size);
26+
std::shared_ptr<Memory> allocatePinnedHostMemory(size_t size);
27+
28+
void memcpyH2D(void *dst, const void *src, size_t size);
29+
void memcpyD2H(void *dst, const void *src, size_t size);
30+
void memcpyD2D(void *dst, const void *src, size_t size);
31+
void memcpyH2H(void *dst, const void *src, size_t size);
32+
33+
} // namespace context
34+
35+
} // namespace infinicore

include/infinicore/device.hpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
1-
#ifndef __INFINICORE_DEVICE_API_HPP__
2-
#define __INFINICORE_DEVICE_API_HPP__
1+
#pragma once
32

43
#include <cstdint>
54
#include <string>
65

6+
#include "infinicore.h"
7+
78
namespace infinicore {
89

910
class Device {
1011
public:
1112
using Index = std::size_t;
1213

1314
enum class Type {
14-
cpu,
15-
cuda,
16-
meta,
15+
CPU = INFINI_DEVICE_CPU,
16+
NVIDIA = INFINI_DEVICE_NVIDIA,
17+
CAMBRICON = INFINI_DEVICE_CAMBRICON,
18+
ASCEND = INFINI_DEVICE_ASCEND,
19+
METAX = INFINI_DEVICE_METAX,
20+
MOORE = INFINI_DEVICE_MOORE,
21+
ILUVATAR = INFINI_DEVICE_ILUVATAR,
22+
KUNLUN = INFINI_DEVICE_KUNLUN,
23+
SUGON = INFINI_DEVICE_SUGON,
24+
COUNT = INFINI_DEVICE_TYPE_COUNT,
1725
};
1826

19-
Device(const Type &type, const Index &index = 0);
27+
Device(const Type &type = Type::CPU, const Index &index = 0);
28+
29+
const Type &getType() const;
2030

21-
const Type &get_type() const;
31+
const Index &getIndex() const;
2232

23-
const Index &get_index() const;
33+
std::string toString() const;
2434

25-
std::string to_string() const;
35+
static std::string toString(const Type &type);
2636

27-
static std::string to_string(const Type &type);
37+
bool operator==(const Device &other) const;
38+
39+
bool operator!=(const Device &other) const;
2840

2941
private:
3042
Type type_;
@@ -33,5 +45,3 @@ class Device {
3345
};
3446

3547
} // namespace infinicore
36-
37-
#endif

include/infinicore/dtype.hpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
1-
#ifndef __INFINICORE_DTYPE_API_HPP__
2-
#define __INFINICORE_DTYPE_API_HPP__
1+
#pragma once
32

43
#include <infinicore.h>
4+
#include <string>
55

66
namespace infinicore {
77

88
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,
9+
BYTE = INFINI_DTYPE_BYTE,
10+
BOOL = INFINI_DTYPE_BOOL,
11+
I8 = INFINI_DTYPE_I8,
12+
I16 = INFINI_DTYPE_I16,
13+
I32 = INFINI_DTYPE_I32,
14+
I64 = INFINI_DTYPE_I64,
15+
U8 = INFINI_DTYPE_U8,
16+
U16 = INFINI_DTYPE_U16,
17+
U32 = INFINI_DTYPE_U32,
18+
U64 = INFINI_DTYPE_U64,
19+
F8 = INFINI_DTYPE_F8,
20+
F16 = INFINI_DTYPE_F16,
21+
F32 = INFINI_DTYPE_F32,
22+
F64 = INFINI_DTYPE_F64,
23+
C16 = INFINI_DTYPE_C16,
24+
C32 = INFINI_DTYPE_C32,
25+
C64 = INFINI_DTYPE_C64,
26+
C128 = INFINI_DTYPE_C128,
27+
BF16 = INFINI_DTYPE_BF16,
1628
};
1729

18-
std::string to_string(const DataType &dtype);
30+
std::string toString(const DataType &dtype);
31+
size_t dsize(const DataType &dtype);
1932

2033
} // namespace infinicore
21-
22-
#endif

0 commit comments

Comments
 (0)