Skip to content

Commit

Permalink
Merge pull request #40 from InfiniTensor/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
YdrMaster authored Nov 29, 2023
2 parents 0c1be80 + c397252 commit c162ed4
Show file tree
Hide file tree
Showing 367 changed files with 3,718 additions and 2,859 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
.vscode/
.cache/
/build/

*.egg-info/
__pycache__/
*.so
*.log
*.onnx
*.pb
*.bin
*.npy
*.meta

/scripts/*.py
!/scripts/format.py
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
url = git@github.com:MengRao/fmtlog.git
[submodule "3rd-party/result"]
path = 3rd-party/result
url = git@github.com:oktal/result.git
url = git@github.com:willowell/result.git
[submodule "3rd-party/abseil-cpp"]
path = 3rd-party/abseil-cpp
url = git@github.com:abseil/abseil-cpp.git
Expand Down
2 changes: 1 addition & 1 deletion 3rd-party/abseil-cpp
2 changes: 1 addition & 1 deletion 3rd-party/result
Submodule result updated 1 files
+6 −6 result.h
31 changes: 21 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(refactor_graph VERSION 0.0.0 LANGUAGES CXX)
message(STATUS "Project " ${PROJECT_NAME} " version " ${PROJECT_VERSION})

option(ABSL_PROPAGATE_CXX_STD "Abseil need this option" ON)
option(USE_CUDA "Build with cuda" OFF)
option(USE_CUDA "Support Nvidia GPU" OFF)
option(USE_KUNLUN "Support Baidu Kunlunxin" OFF)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

if(USE_CUDA)
add_compile_definitions(USE_CUDA)
Expand All @@ -24,9 +27,21 @@ if(USE_CUDA)
message(STATUS "CMAKE_CUDA_ARCHITECTURES set to " ${CMAKE_CUDA_ARCHITECTURES})
endif()

# add_compile_options(-march=native) # this will cause error in some machine
if(USE_KUNLUN)
add_compile_definitions(USE_KUNLUN)
if (DEFINED KUNLUN_HOME)
set(KUNLUN_HOME ${KUNLUN_HOME} CACHE STRING "KUNLUN_HOME directory for Kunlun development")
elseif(DEFINED ENV{KUNLUN_HOME})
set(KUNLUN_HOME $ENV{KUNLUN_HOME} CACHE STRING "KUNLUN_HOME directory for Kunlun development")
else()
message(FATAL_ERROR "KUNLUN_HOME is not defined from cmake or env")
endif()
message(STATUS "KUNLUN_HOME: ${KUNLUN_HOME}")
endif()

add_compile_options(-march=native) # this will cause error in some machine
add_compile_options(-mtune=native)
add_compile_options(-fPIC)
add_compile_options(-Wall)

add_subdirectory(3rd-party/backward-cpp)

Expand All @@ -39,20 +54,16 @@ add_subdirectory(3rd-party/googletest)
include_directories(3rd-party/fmtlog)
add_definitions(-D FMTLOG_HEADER_ONLY)

# disable warning in 3rd-party/abseil-cpp
# see <https://github.com/abseil/abseil-cpp/pull/1287>
add_compile_options(-w)
include_directories(3rd-party/abseil-cpp)
add_subdirectory(3rd-party/abseil-cpp)
add_compile_options(-Wall)

include_directories(3rd-party/result)

enable_testing()

add_subdirectory(src/00common)
add_subdirectory(src/01graph_topo)
add_subdirectory(src/02mem_manager)
add_subdirectory(src/02hardware)
add_subdirectory(src/03runtime)
add_subdirectory(src/04kernel)
add_subdirectory(src/05computation)
Expand Down
11 changes: 5 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
.PHONY : build install-python reconfig clean clean-log format test

TYPE ?= debug
TYPE ?= Debug
CUDA ?= OFF
KUNLUN ?= OFF

FORMAT_ORIGIN ?=
CMAKE_EXTRA =
# CMAKE_EXTRA += -DCMAKE_CXX_COMPILER=

build:
mkdir -p build
cmake -DCMAKE_BUILD_TYPE=$(TYPE) -DUSE_CUDA=$(CUDA) -Bbuild
cmake -Bbuild -DCMAKE_BUILD_TYPE=$(TYPE) -DUSE_CUDA=$(CUDA) -DUSE_KUNLUN=$(KUNLUN) $(CMAKE_EXTRA)
make -j -C build

install-python: build
Expand All @@ -27,6 +29,3 @@ clean-log:

test:
make test -j -Cbuild

format:
@python3 scripts/format.py $(FORMAT_ORIGIN)
50 changes: 46 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

- [安装](#安装)
- [使用前端](#使用前端)
- [调试功能](#调试功能)
- [项目结构](#项目结构)
- [构建系统](#构建系统)
- [子项目简介](#子项目简介)
Expand All @@ -25,17 +26,18 @@
import sys
import numpy as np
from onnx import load
from refactor_graph.onnx import make_compiler
from refactor_graph.onnx import make_compiler, find_device
from onnxruntime import InferenceSession

model = load(sys.argv[1]) # ------------------------------------ 加载模型
input = np.random.random((10, 3, 224, 224)).astype(np.float32) # 加载测试样本

compiler = make_compiler(model) # ------------------------------ 模型导入到编译器
compiler.substitute("N", 10) # --------------------------------- 代换输入中的变量
find_device("nvidia", 0) # ------------------------------------- 初始化指定加速硬件
executor = compiler.compile("cuda", "default", []) # ----------- 编译模型(选择平台、分配器和优化选项)
executor.set_input(0, input) # --------------------------------- 设置输入
executor.prepare() # ------------------------------------------- 准备推理(分配输出空间)
executor.dispatch(find_device("nvidia", 1), "default") # ------- 执行器可以随时调度到另一个硬件
executor.run() # ----------------------------------------------- 推理

session = InferenceSession(model.SerializeToString()) # -------- 与 onnxruntime 对比结果以验证推理
Expand All @@ -59,6 +61,46 @@ executor = compiler.compile("cuda", "default", []) # -------- 编译模型
# 下同
```

### 调试功能

项目现已依托前端提供多种调试功能。

1. 列出算子信息

```python
executor.dbg()
```

2. 保存运行中间结果

```python
executor.trace("path_to_store_files", "data_file_format")
```

调用这个方法将启动一次模型推理,并在每个算子推理完成后将算子的所有输入输出张量保存到 `path_to_store_files` 参数指示的目录中。
如果目录不存在,将创建此目录。每个张量保存到一个文件,已存在的同名文件将被删除。
同时,为每个算子创建一个元信息文本文件,命名为 `node<N>.meta`,其内容具有下述格式:

```plaintext
<NodeName>\t<N>
<input/output>\t<K>\t<EdgeName>\t[FileName]
```

- `NodeName`: 节点的名字;
- `N`: 节点序号;
- `input/output`: 张量是节点的输入/输出;
- `K`: 输入/输出的序号;
- `EdgeName`: 张量的名字;
- `FileName`: (optional) 数据文件名。如果张量无效,不会保存数据文件,则文件名为空;

3. 逐算子计时

```python
executor.bench(<sync>)
```

对每次推理计时。`sync` 是一个指示是否在每次推理后插入同步的布尔参数,若设置为 `False`,则计时可能是推理异步启动的时间。

## 项目结构

### 构建系统
Expand Down Expand Up @@ -122,8 +164,8 @@ executor = compiler.compile("cuda", "default", []) # -------- 编译模型
- [fmtlog v2.2.1](https://github.com/MengRao/fmtlog/releases/tag/v2.2.1)
- [googletest v1.14.0](https://github.com/google/googletest/releases/tag/v1.14.0)
- [backward-cpp v1.6](https://github.com/bombela/backward-cpp/releases/tag/v1.6)
- [result master](https://github.com/oktal/result)
- [abseil-cpp 20230802.0](https://github.com/abseil/abseil-cpp/releases/tag/20230802.0)
- [result master](https://github.com/willowell/result)
- [abseil-cpp 20230802.1](https://github.com/abseil/abseil-cpp/releases/tag/20230802.1)

## 技术要点

Expand Down
14 changes: 13 additions & 1 deletion scripts/format.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sys
import sys, os
from pathlib import Path
from subprocess import run

Expand Down Expand Up @@ -48,3 +48,15 @@ def format_file(file):
assert files[0][:2] == "a/"
assert files[1][:2] == "b/"
format_file(files[1][2:])


def format_everything(dir):
if os.path.isdir(dir):
for root, _, files in os.walk(dir):
for file in files:
format_file(os.path.join(root, file))
else:
format_file(dir)


format_everything("src")
5 changes: 3 additions & 2 deletions src/00common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 3.10)
project(common)
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(common VERSION 0.0.0 LANGUAGES CXX)
message(STATUS "Project " ${PROJECT_NAME} " version " ${PROJECT_VERSION})

file(GLOB_RECURSE COMMON_SRC src/*.cc)
add_library(common STATIC ${COMMON_SRC})
Expand Down
4 changes: 2 additions & 2 deletions src/00common/include/common/bf16_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ namespace refactor {
constexpr static uint16_t MASK_SIGN16 = 0b1'00000'00000'00000;

public:
constexpr bf16_t() noexcept : code(converter{0.0}.u16[1]) {}
constexpr bf16_t(uint16_t code) noexcept : code(code) {}
constexpr bf16_t(float value) noexcept : code(converter{value}.u16[1]) {}
constexpr bf16_t(float value) noexcept : bf16_t(converter{value}.u16[1]) {}
constexpr bf16_t() noexcept : bf16_t(0.f) {}
constexpr bf16_t(bf16_t const &) noexcept = default;
constexpr bf16_t(bf16_t &&) noexcept = default;

Expand Down
4 changes: 3 additions & 1 deletion src/00common/include/common/error_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ namespace refactor {
[&]() -> T { \
throw refactor::UnreachableError(ERROR_MSG(fmt::format("Unreachable: " #F, ##__VA_ARGS__))); \
}()
#define UNREACHABLE() UNREACHABLEX(void, "no message")
#define UNREACHABLE() \
UNREACHABLEX(void, "no message"); \
std::abort()

#ifndef DISABLE_ASSERT
#define ASSERT(CONDITION, F, ...) \
Expand Down
4 changes: 3 additions & 1 deletion src/00common/include/common/range.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ namespace refactor {
};

template<class t = size_t> range_t<t> range0_(t end) {
ASSERT(end >= 0, "end must be greater than 0");
if constexpr (std::is_signed_v<t>) {
ASSERT(end >= 0, "end must be greater than 0");
}
return {0, end};
}
template<class t = size_t> range_t<t> range(t begin, t end) {
Expand Down
6 changes: 2 additions & 4 deletions src/00common/include/common/rc.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#ifndef RC_HPP
#define RC_HPP

#include <cstddef>
#include <functional>
#include <utility>

namespace refactor {

Expand All @@ -20,7 +18,7 @@ namespace refactor {
T *_value;
struct Counter {
size_t strong, weak;
} *_counter;
} * _counter;

Rc(T *ptr, Counter *counter) noexcept
: _value(ptr), _counter(counter) { inc(); }
Expand All @@ -38,7 +36,7 @@ namespace refactor {
}

public:
explicit Rc(T *ptr) noexcept
explicit Rc(T *ptr)
: _value(ptr),
_counter(nullptr) {
if (!ptr) { return; }
Expand Down
2 changes: 2 additions & 0 deletions src/00common/include/common/slice.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef SLICE_H
#define SLICE_H

#include <cstddef>

namespace refactor {
template<class t>
struct slice_t {
Expand Down
13 changes: 6 additions & 7 deletions src/00common/src/data_type.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "common/data_type.h"
#include "common/error_handler.h"
#include <fmt/core.h>
#include <unordered_set>

namespace refactor {
Expand Down Expand Up @@ -66,35 +65,35 @@ namespace refactor {

bool DT::isIeee754() const noexcept {
static const std::unordered_set<Enum> set{DT::F32, DT::FP16, DT::F64};
return set.find(internal) != set.end();
return set.contains(internal);
}
bool DT::isFloat() const noexcept {
static const std::unordered_set<Enum> set{DT::F32, DT::FP16, DT::F64, DT::BF16};
return set.find(internal) != set.end();
return set.contains(internal);
}
bool DT::isSignedLarge() const noexcept {
static const std::unordered_set<Enum> set{
DT::F32, DT::FP16, DT::BF16, DT::F64, DT::I32, DT::I64};
return set.find(internal) != set.end();
return set.contains(internal);
}
bool DT::isSigned() const noexcept {
static const std::unordered_set<Enum> set{
DT::F32, DT::FP16, DT::BF16, DT::F64,
DT::I8, DT::I16, DT::I32, DT::I64};
return set.find(internal) != set.end();
return set.contains(internal);
}
bool DT::isNumberic() const noexcept {
static const std::unordered_set<Enum> set{
DT::F32, DT::U8, DT::I8, DT::U16, DT::I16,
DT::I32, DT::I64, DT::FP16, DT::F64,
DT::U32, DT::U64, DT::BF16};
return set.find(internal) != set.end();
return set.contains(internal);
}
bool DT::isCpuNumberic() const noexcept {
static const std::unordered_set<Enum> set{
DT::F32, DT::U8, DT::I8, DT::U16, DT::I16,
DT::I32, DT::I64, DT::F64, DT::U32, DT::U64};
return set.find(internal) != set.end();
return set.contains(internal);
}
bool DT::isBool() const noexcept {
return internal == DT::Bool;
Expand Down
5 changes: 3 additions & 2 deletions src/01graph_topo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
cmake_minimum_required(VERSION 3.10)
project(graph_topo)
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(graph_topo VERSION 0.0.0 LANGUAGES CXX)
message(STATUS "Project " ${PROJECT_NAME} " version " ${PROJECT_VERSION})

file(GLOB_RECURSE GRAPH_TOPO_SRC src/*.cc src/*.cpp)
add_library(graph_topo STATIC ${GRAPH_TOPO_SRC})
Expand Down
2 changes: 1 addition & 1 deletion src/01graph_topo/include/graph_topo.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
#include "graph_topo/builder.hpp"
#include "graph_topo/inplace_modifier.h"
#include "graph_topo/polymorph_graph.hpp"
#include "graph_topo/searcher.h"
#include "graph_topo/searcher.hh"

#endif// GRAPH_TOPO_GRAPH_TOPO_H
Loading

0 comments on commit c162ed4

Please sign in to comment.