Skip to content

Commit 4e332b0

Browse files
committed
excuter:并行处理cpu,cuda
1 parent 280fad9 commit 4e332b0

File tree

22 files changed

+345
-396
lines changed

22 files changed

+345
-396
lines changed

doc/excuter/op-mem-ompsimd/list.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
| Operation | Author | Func Def | Math Formula | IR Instruction |
66
|-----------|--------|------------|--------------|----------------|
7-
| concat | none | concat(listtensor<any> tensors, var<int32> axis)->(tensor<any> Tresult) | Tresult = concat([T1, T2...], axis=3) | concat(listtensor<any> tensors, var<int32> axis)->(tensor<any> Tresult) |
87
| print | none | print(tensor<any> tensor1, var<string> format)->() | print(T1) | print(tensor<any> tensor1, var<string> format)->() |
98
| print | none | print(tensor<any> tensor1)->() | print(T1) | print(tensor<any> tensor1)->() |
109
| newtensor | none | newtensor(vector<int32> shape)->(tensor<any> tensor1) | T1 = zeros(shape) | newtensor(vector<int32> shape)->(tensor<any> tensor1) |

excuter/cpp-common/src/deepx/tensorfunc/init.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ namespace deepx::tensorfunc
3333
template <typename Author, typename T>
3434
struct uniformDispatcher
3535
{
36-
static void uniform(Tensor<T> &tensor, const T low , const T high ) = delete;
36+
static void uniform(Tensor<T> &tensor, const T low , const T high , const unsigned int seed) = delete;
3737
};
3838

3939
template <typename Author, typename T>
40-
void uniform(Tensor<T> &tensor, const T low , const T high = T(1))
40+
void uniform(Tensor<T> &tensor, const T low = T(0), const T high = T(1), const unsigned int seed = 0)
4141
{
42-
uniformDispatcher<Author, T>::uniform(tensor, low, high);
42+
uniformDispatcher<Author, T>::uniform(tensor, low, high, seed);
4343
}
4444
}
4545

excuter/cpp-common/src/deepx/tf/tf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace deepx::tf
1919
size_t arrow_pos = body.find("->");
2020
if (arrow_pos == string::npos)
2121
{
22-
throw runtime_error("Invalid IR format: missing arrow");
22+
throw runtime_error("Invalid IR format: missing arrow");
2323
}
2424

2525
// 获取输入和输出部分的原始字符串
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include <iostream>
2+
#include <memory>
3+
#include <vector>
4+
#include <string>
5+
#include <sstream>
6+
7+
#include "deepx/tf/tffactory.hpp"
8+
#include "deepx/dtype.hpp"
9+
10+
namespace deepx::tf
11+
{
12+
using namespace std;
13+
14+
shared_ptr<TF> TfFactory::get_tf(const TF &other) const
15+
{
16+
// 检查操作名是否存在
17+
auto family_it = tf_families.find(other.name);
18+
if (family_it == tf_families.end())
19+
{
20+
cerr << "<op> " << other.name << " not found" << endl;
21+
return nullptr;
22+
}
23+
24+
// 检查作者是否存在
25+
auto author_it = family_it->second->tf_authors.find(other.author);
26+
if (author_it == family_it->second->tf_authors.end())
27+
{
28+
cerr << "<op> " << other.name << " author:" << other.author << " not found" << endl;
29+
return nullptr;
30+
}
31+
32+
// 提取参数和返回值类型
33+
vector<TypeDef> arg_types;
34+
for (const auto &arg : other.args)
35+
{
36+
arg_types.push_back(arg.dtype);
37+
}
38+
39+
vector<TypeDef> return_types;
40+
for (const auto &ret : other.returns)
41+
{
42+
return_types.push_back(ret.dtype);
43+
}
44+
45+
// 尝试找到匹配的实现
46+
auto tf = author_it->second->get_matching_tf(arg_types, return_types);
47+
if (!tf)
48+
{
49+
cerr << "<op> " << other.name << " " << other.to_string(false, false) << " not found" << endl;
50+
cerr << "supported dtypes: " << endl;
51+
// 遍历所有已注册的实现
52+
for (const auto &registered_tf : author_it->second->tfs)
53+
{
54+
cerr << "(";
55+
for (size_t i = 0; i < registered_tf->args.size(); i++)
56+
{
57+
if (i > 0)
58+
cerr << ", ";
59+
cerr << dtype_str(registered_tf->args[i].dtype);
60+
}
61+
cerr << ")->(";
62+
for (size_t i = 0; i < registered_tf->returns.size(); i++)
63+
{
64+
if (i > 0)
65+
cerr << ", ";
66+
cerr << dtype_str(registered_tf->returns[i].dtype);
67+
}
68+
cerr << ")" << endl;
69+
}
70+
return nullptr;
71+
}
72+
return tf;
73+
}
74+
string TfFactory::print_markdown() const
75+
{
76+
std::stringstream ss;
77+
ss << "## excuter/op-mem-ompsimd 支持算子列表 \n\n";
78+
ss << "本页面由 `excuter/op-mem-ompsimd/src/deepx/tf/tffactory.hpp` 生成,请勿手动修改 \n\n";
79+
ss << "| Operation | Author | Func Def | Math Formula | IR Instruction |\n";
80+
ss << "|-----------|--------|------------|--------------|----------------|\n";
81+
82+
// 输出每个操作及其信息
83+
for (const auto &[name, tf_family] : tf_families)
84+
{
85+
for (const auto &[author, tf_author] : tf_family->tf_authors)
86+
{
87+
for (const auto &tf : tf_author->tfs)
88+
{
89+
ss << "| " << name << " | ";
90+
ss << (author.empty() ? " none " : author) << " | ";
91+
ss << tf->to_string(false, true) << " | ";
92+
ss << tf->math_formula() << " | ";
93+
ss << tf->to_string(false, true) << " |\n";
94+
}
95+
}
96+
}
97+
return ss.str();
98+
}
99+
}

excuter/op-mem-ompsimd/src/deepx/tf/tffactory.hpp renamed to excuter/cpp-common/src/deepx/tf/tffactory.hpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
11
#ifndef DEEPX_TF_TFFACTORY_HPP
22
#define DEEPX_TF_TFFACTORY_HPP
33

4-
#include <unordered_map>
5-
#include <string>
6-
#include <memory>
7-
#include <vector>
8-
#include <algorithm>
9-
104
#include "deepx/tf/tf.hpp"
115

126
namespace deepx::tf
137
{
8+
149
struct TypeSignature
1510
{
1611
vector<TypeDef> args;
1712
vector<TypeDef> returns;
1813

19-
bool is_compatible(const TypeSignature& other) const {
20-
return is_compatible_types(args, other.args) &&
14+
bool is_compatible(const TypeSignature &other) const
15+
{
16+
return is_compatible_types(args, other.args) &&
2117
is_compatible_types(returns, other.returns);
2218
}
2319

2420
private:
25-
static bool is_compatible_types(const vector<TypeDef>& a, const vector<TypeDef>& b) {
26-
if (a.size() != b.size()) return false;
27-
for (size_t i = 0; i < a.size(); i++) {
28-
if ((static_cast<uint8_t>(a[i].parts.category) &
29-
static_cast<uint8_t>(b[i].parts.category)) == 0) {
21+
static bool is_compatible_types(const vector<TypeDef> &a, const vector<TypeDef> &b)
22+
{
23+
if (a.size() != b.size())
24+
return false;
25+
for (size_t i = 0; i < a.size(); i++)
26+
{
27+
if ((static_cast<uint8_t>(a[i].parts.category) &
28+
static_cast<uint8_t>(b[i].parts.category)) == 0)
29+
{
3030
return false;
3131
}
32-
if (a[i].parts.precision != Precision::Any &&
33-
b[i].parts.precision != Precision::Any &&
34-
a[i].parts.precision != b[i].parts.precision) {
32+
if (a[i].parts.precision != Precision::Any &&
33+
b[i].parts.precision != Precision::Any &&
34+
a[i].parts.precision != b[i].parts.precision)
35+
{
3536
return false;
3637
}
3738
}
@@ -44,23 +45,28 @@ namespace deepx::tf
4445
vector<std::shared_ptr<TF>> tfs;
4546

4647
// 获取匹配的TF实现
47-
std::shared_ptr<TF> get_matching_tf(const vector<TypeDef>& arg_types,
48-
const vector<TypeDef>& return_types) const {
48+
std::shared_ptr<TF> get_matching_tf(const vector<TypeDef> &arg_types,
49+
const vector<TypeDef> &return_types) const
50+
{
4951
TypeSignature target{arg_types, return_types};
50-
51-
for (const auto& tf : tfs) {
52+
53+
for (const auto &tf : tfs)
54+
{
5255
vector<TypeDef> tf_arg_types;
53-
for (const auto& arg : tf->args) {
56+
for (const auto &arg : tf->args)
57+
{
5458
tf_arg_types.push_back(arg.dtype);
5559
}
56-
60+
5761
vector<TypeDef> tf_return_types;
58-
for (const auto& ret : tf->returns) {
62+
for (const auto &ret : tf->returns)
63+
{
5964
tf_return_types.push_back(ret.dtype);
6065
}
61-
66+
6267
TypeSignature current{tf_arg_types, tf_return_types};
63-
if (target.is_compatible(current)) {
68+
if (target.is_compatible(current))
69+
{
6470
return tf;
6571
}
6672
}
@@ -101,9 +107,6 @@ namespace deepx::tf
101107
// 输出为markdown表格格式
102108
string print_markdown() const;
103109
};
104-
105-
int register_all(TfFactory &tfactory);
106110
}
107-
108111

109112
#endif

excuter/op-mem-cuda/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ add_executable(${PROJECT_NAME} ${CLIENT_SOURCES})
5858
target_link_libraries(${PROJECT_NAME}
5959
PRIVATE
6060
deepx
61+
CUDA::cudart
6162
)
6263
# 测试
6364
add_subdirectory(test/tensorfunc)

excuter/op-mem-cuda/src/client/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <deepx/tensorfunc/init.hpp>
66
#include "deepx/tf/tf.hpp"
77
#include "deepx/tf/tffactory.hpp"
8+
#include "client/tfs.hpp"
89
#include "deepx/mem/mem.hpp"
910
#include "client/udpserver.hpp"
1011

@@ -23,7 +24,7 @@ int main()
2324
Mem mem;
2425
std::mutex memmutex;
2526

26-
client::udpserver server(8080);
27+
client::udpserver server(9090);
2728
deepx::tf::TfFactory tf_factory;
2829
register_all(tf_factory);
2930

excuter/op-mem-cuda/src/deepx/tf/tffactory.cpp renamed to excuter/op-mem-cuda/src/client/tfs.cpp

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7,80 +7,7 @@
77

88
namespace deepx::tf
99
{
10-
shared_ptr<TF> TfFactory::get_tf(const TF &other) const
11-
{
12-
// 检查操作名是否存在
13-
auto family_it = tf_families.find(other.name);
14-
if (family_it == tf_families.end())
15-
{
16-
cerr << "<op> " << other.name << " not found" << endl;
17-
return nullptr;
18-
}
19-
20-
// 检查作者是否存在
21-
auto author_it = family_it->second->tf_authors.find(other.author);
22-
if (author_it == family_it->second->tf_authors.end())
23-
{
24-
cerr << "<op> " << other.name << " author:" << other.author << " not found" << endl;
25-
return nullptr;
26-
}
27-
28-
// 提取参数和返回值类型
29-
vector<TypeDef> arg_types;
30-
for (const auto& arg : other.args) {
31-
arg_types.push_back(arg.dtype);
32-
}
33-
34-
vector<TypeDef> return_types;
35-
for (const auto& ret : other.returns) {
36-
return_types.push_back(ret.dtype);
37-
}
38-
39-
// 尝试找到匹配的实现
40-
auto tf = author_it->second->get_matching_tf(arg_types, return_types);
41-
if (!tf) {
42-
cerr << "<op> " << other.name << " " << other.to_string(false, false) << " not found" << endl;
43-
cerr << "supported dtypes: " << endl;
44-
// 遍历所有已注册的实现
45-
for (const auto& registered_tf : author_it->second->tfs) {
46-
cerr << "(";
47-
for (size_t i = 0; i < registered_tf->args.size(); i++) {
48-
if (i > 0) cerr << ", ";
49-
cerr << dtype_str(registered_tf->args[i].dtype);
50-
}
51-
cerr << ")->(";
52-
for (size_t i = 0; i < registered_tf->returns.size(); i++) {
53-
if (i > 0) cerr << ", ";
54-
cerr << dtype_str(registered_tf->returns[i].dtype);
55-
}
56-
cerr << ")" << endl;
57-
}
58-
return nullptr;
59-
}
60-
return tf;
61-
}
62-
string TfFactory::print_markdown() const
63-
{
64-
std::stringstream ss;
65-
ss << "## excuter/op-mem-cuda 支持算子列表 \n\n";
66-
ss << "本页面由 `excuter/op-mem-cuda/src/deepx/tf/tffactory.hpp` 生成,请勿手动修改 \n\n";
67-
ss << "| Operation | Author | Func Def | Math Formula | IR Instruction |\n";
68-
ss << "|-----------|--------|------------|--------------|----------------|\n";
69-
70-
// 输出每个操作及其信息
71-
for (const auto& [name, tf_family] : tf_families) {
72-
for (const auto& [author, tf_author] : tf_family->tf_authors) {
73-
for (const auto& tf : tf_author->tfs) {
74-
ss << "| " << name << " | ";
75-
ss << (author.empty() ? " none " : author) << " | ";
76-
ss << tf->to_string(false, true) << " | ";
77-
ss << tf->math_formula() << " | ";
78-
ss << tf->to_string(false, true) << " |\n";
79-
}
80-
}
81-
}
82-
return ss.str();
83-
}
10+
8411
// tensor
8512
void register_lifecycle(TfFactory &tffactory)
8613
{
@@ -107,6 +34,7 @@ namespace deepx::tf
10734
void register_util(TfFactory &opfactory)
10835
{
10936
opfactory.add_tf(std::make_shared<Print>());
37+
opfactory.add_tf(std::make_shared<Print>(1));
11038
}
11139

11240
// // elementwise
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef CLIENT_TFS_HPP
2+
#define CLIENT_TFS_HPP
3+
4+
#include "deepx/tf/tffactory.hpp"
5+
namespace deepx::tf{
6+
7+
int register_all(TfFactory &tfactory);
8+
}
9+
10+
#endif

0 commit comments

Comments
 (0)