Skip to content

Commit

Permalink
update project structure and add interned strings and add ci
Browse files Browse the repository at this point in the history
  • Loading branch information
CanftIn committed Jul 3, 2024
1 parent c6aea9a commit 1488a79
Show file tree
Hide file tree
Showing 10 changed files with 368 additions and 6 deletions.
38 changes: 38 additions & 0 deletions .github/workflow/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: ci

on:
push:
branches:
- master

pull_request:
branches:
- master

jobs:
cmake_build:
name: cmake_build
runs-on: ubuntu-latest

steps:
- name: setup
run: |
sudo apt-get update
sudo apt-get install build-essential cmake g++ clang-15 libgtest-dev libgmock-dev make
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Generate makefile using CMake
run: |
mkdir build
cd build
cmake .. -DCMAKE_CXX_COMPILER=/usr/bin/clang++-15
- name: Run make
run: |
cd build
make -j$(nproc)
- name: Run unittests
run: |
cd build
ctest -j$(nproc)
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ include(GNUInstallDirs)
set(MY_AI_TRAINING_ROOT ${PROJECT_SOURCE_DIR})

if(MY_AI_TRAINING_BUILD_TESTS)
include(CTest)
enable_testing()
find_package(GTest)
if(NOT GTest_FOUND)
list(APPEND CMAKE_MODULE_PATH ${ONNX_ROOT}/cmake/external)
Expand All @@ -34,7 +36,7 @@ endif()

include_directories(src)

file(GLOB SRCS src/*.cc)
file(GLOB SRCS src/ir/*.cc)

add_library(my_ai_training_lib ${SRCS})

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# my_ai_training
# my_ai_training

练习ai相关代码的仓库,目前在过onnx代码中。
4 changes: 2 additions & 2 deletions src/assertions.cc → src/ir/assertions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
// ATTENTION: The code in this file is highly EXPERIMENTAL.
// Adventurous users should note that the APIs will probably change.

#include "assertions.h"
#include "ir/assertions.h"

#include <array>
#include <cstdarg>
#include <cstdio>

#include "common.h"
#include "ir/common.h"

namespace my_ai_training::ir {

Expand Down
File renamed without changes.
File renamed without changes.
78 changes: 78 additions & 0 deletions src/ir/interned_strings.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) ONNX Project Contributors

/*
* SPDX-License-Identifier: Apache-2.0
*/

// ATTENTION: The code in this file is highly EXPERIMENTAL.
// Adventurous users should note that the APIs will probably change.

#include "ir/interned_strings.h"

#include <stdint.h>

#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>

#include "ir/assertions.h"

namespace my_ai_training::ir {

struct InternedStrings {
InternedStrings()
: next_sym(kLastSymbol){
#define REGISTER_SYMBOL(s) \
string_to_sym_[#s] = k##s; \
sym_to_string_[k##s] = #s;
FORALL_BUILTIN_SYMBOLS(REGISTER_SYMBOL)
#undef REGISTER_SYMBOL
} uint32_t symbol(const std::string& s) {
std::lock_guard<std::mutex> guard(mutex_);
auto it = string_to_sym_.find(s);
if (it != string_to_sym_.end()) return it->second;
uint32_t k = next_sym++;
string_to_sym_[s] = k;
sym_to_string_[k] = s;
return k;
}
const char* string(Symbol sym) {
// Builtin Symbols are also in the maps, but
// we can bypass the need to acquire a lock
// to read the map for Builtins because we already
// know their string value
switch (sym) {
#define DEFINE_CASE(s) \
case k##s: \
return #s;
FORALL_BUILTIN_SYMBOLS(DEFINE_CASE)
#undef DEFINE_CASE
default:
return customString(sym);
}
}

private:
const char* customString(Symbol sym) {
std::lock_guard<std::mutex> guard(mutex_);
auto it = sym_to_string_.find(sym);
ONNX_ASSERT(it != sym_to_string_.end());
return it->second.c_str();
}
std::unordered_map<std::string, uint32_t> string_to_sym_;
std::unordered_map<uint32_t, std::string> sym_to_string_;
uint32_t next_sym;
std::mutex mutex_;
};

static InternedStrings& globalStrings() {
static InternedStrings s;
return s;
}

const char* Symbol::toString() const { return globalStrings().string(*this); }

Symbol::Symbol(const std::string& s) : value(globalStrings().symbol(s)) {}

} // namespace my_ai_training::ir
Loading

0 comments on commit 1488a79

Please sign in to comment.