Skip to content

Commit

Permalink
Python logging & changed Python class names (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
chhwang authored Sep 5, 2024
1 parent eb300e6 commit d8bbaeb
Show file tree
Hide file tree
Showing 41 changed files with 804 additions and 476 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ build/
*.pyc
*.pyo
*.pyd
.pytest_cache/

# Git
**/.git
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- name: Build
run: |
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Debug -DARK_BUILD_PYTHON=ON -DARK_BYPASS_GPU_CHECK=ON -DARK_USE_CUDA=ON -DARK_BUILD_TESTS=OFF ..
cmake -DCMAKE_BUILD_TYPE=Debug -DARK_BYPASS_GPU_CHECK=ON -DARK_USE_CUDA=ON -DARK_BUILD_TESTS=OFF ..
make build ark_py
- name: Perform CodeQL Analysis
Expand Down Expand Up @@ -95,7 +95,7 @@ jobs:
- name: Build
run: |
mkdir build && cd build
CXX=/opt/rocm/bin/hipcc cmake -DCMAKE_BUILD_TYPE=Debug -DARK_BUILD_PYTHON=ON -DARK_BYPASS_GPU_CHECK=ON -DARK_USE_ROCM=ON -DARK_BUILD_TESTS=OFF ..
CXX=/opt/rocm/bin/hipcc cmake -DCMAKE_BUILD_TYPE=Debug -DARK_BYPASS_GPU_CHECK=ON -DARK_USE_ROCM=ON -DARK_BUILD_TESTS=OFF ..
make -j build ark_py
- name: Perform CodeQL Analysis
Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/ut-cuda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
- name: Build
run: |
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Debug -DARK_BUILD_PYTHON=ON ..
cmake -DCMAKE_BUILD_TYPE=Debug ..
make -j ut ark_py
- name: Run C++ UT
Expand All @@ -71,7 +71,11 @@ jobs:
- name: Run Python UT
run: |
cd build
ARK_ROOT=$PWD pytest --cov=../python/ark --cov-report lcov:py_coverage.info --verbose ../python/unittest/test.py
PYTHONPATH=$PWD/python ARK_ROOT=$PWD python3 -m pytest \
--cov=python/ark \
--cov-report lcov:py_coverage.info \
--verbose \
../python/unittest/test.py
- name: Report Coverage
env:
Expand Down
2 changes: 0 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"cmake.environment": {
"ARK_ROOT": "${workspaceFolder}/build",
"ARK_IGNORE_BINARY_CACHE": "1",
"ARK_DISABLE_GRAPH_OPT": "0",
"ARK_IPC_LISTEN_PORT_BASE": "42000",
// "ARK_LOG_LEVEL": "DEBUG"
},
"cmake.ctestArgs": [
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ option(ARK_USE_CUDA "Use NVIDIA/CUDA." OFF)
option(ARK_USE_ROCM "Use AMD/ROCm." OFF)
option(ARK_BYPASS_GPU_CHECK "Bypass GPU check." OFF)
option(ARK_BUILD_TESTS "Build unit tests." ON)
option(ARK_BUILD_PYTHON "Build Python module." ON)

if(ARK_BYPASS_GPU_CHECK)
if(ARK_USE_CUDA)
Expand Down
15 changes: 15 additions & 0 deletions ark/api/log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#include "ark/log.hpp"

#include "logging.hpp"

namespace ark {

void log(LogLevel level, const std::string &file, int line,
const std::string &msg) {
_log(level, file, line, msg);
}

} // namespace ark
18 changes: 18 additions & 0 deletions ark/include/ark/log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#ifndef ARK_LOG_HPP
#define ARK_LOG_HPP

#include <string>

namespace ark {

typedef enum { DEBUG, INFO, WARN, ERROR } LogLevel;

void log(LogLevel level, const std::string &file, int line,
const std::string &msg);

} // namespace ark

#endif // ARK_LOG_HPP
36 changes: 18 additions & 18 deletions ark/logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
#include <string>

#include "ark/error.hpp"
#include "ark/log.hpp"

namespace ark {

typedef enum { DEBUG, INFO, WARN, ERROR } LogLevel;

class Logging {
public:
Logging(const std::string &lv);
Expand Down Expand Up @@ -46,43 +45,44 @@ void _log_helper(std::stringstream &ss, T value, Args... args) {
_log_helper(ss, args...);
}

template <LogLevel Level, bool AppendNewLine, typename T, typename... Args>
inline std::string _log_msg(const std::string &file, int line, T value,
template <typename T, typename... Args>
inline std::string _log_msg(LogLevel level, bool append_newline,
const std::string &file, int line, T value,
Args... args) {
std::stringstream ss;
_log_header(ss, Level, file, line);
_log_header(ss, level, file, line);
_log_helper(ss, value, args...);
if constexpr (AppendNewLine) ss << std::endl;
if (append_newline) ss << std::endl;
return ss.str();
}

template <LogLevel Level, typename T, typename... Args>
inline void _log(const std::string &file, int line, T value, Args... args) {
if (Level >= get_logging().get_level()) {
std::clog << _log_msg<Level, true>(file, line, value, args...);
template <typename T, typename... Args>
inline void _log(LogLevel level, const std::string &file, int line, T value,
Args... args) {
if (level >= get_logging().get_level()) {
std::clog << _log_msg(level, true, file, line, value, args...);
}
if constexpr (Level == ERROR) {
if (level == ERROR) {
throw std::runtime_error("ARK runtime error");
}
}

template <typename Exception, typename T, typename... Args>
inline void _err(const std::string &file, int line, T value, Args... args) {
throw Exception(_log_msg<ERROR, false>(file, line, value, args...));
throw Exception(_log_msg(ERROR, false, file, line, value, args...));
}

// Logging.
#define LOG(level, ...) \
do { \
ark::_log<level>(__FILE__, __LINE__, __VA_ARGS__); \
ark::_log(level, __FILE__, __LINE__, __VA_ARGS__); \
break; \
} while (0)

#define ERR(exception, ...) \
do { \
std::string exc_str = " (" #exception ")"; \
ark::_err<exception>(__FILE__, __LINE__, __VA_ARGS__, exc_str); \
break; \
#define ERR(exception, ...) \
do { \
ark::_err<exception>(__FILE__, __LINE__, __VA_ARGS__); \
break; \
} while (0)

#define CHECK(cond) \
Expand Down
2 changes: 1 addition & 1 deletion ark/ops/ops_test_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ float reduction_abs_error_bound(float max_abs, int reduction_length) {
// If the reduction length is too large, the error will be dominated by
// the rounding error of the reduction itself.
if (reduction_length > (1 << (NumFracBits + 1))) {
UNITTEST_FEXIT("reduction length is too large");
UNITTEST_FAIL("reduction length is too large");
}
float max_diff =
reduction_length * 2 * max_abs * 1.0f / (1 << (NumFracBits + 1));
Expand Down
6 changes: 3 additions & 3 deletions ark/unittest/unittest_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// Grep SIGALRM and exit.
static void sigalrm_timeout_handler(int) {
signal(SIGALRM, SIG_IGN);
UNITTEST_FEXIT("timeout");
UNITTEST_FAIL("timeout");
}

namespace ark {
Expand Down Expand Up @@ -64,7 +64,7 @@ void wait_all_threads() {
int spawn_process(std::function<State()> func) {
pid_t pid = fork();
if (pid < 0) {
UNITTEST_UEXIT("fork() failed");
UNITTEST_UNEXPECTED("fork() failed");
} else if (pid == 0) {
State ret = func();
std::exit(ret);
Expand All @@ -82,7 +82,7 @@ void wait_all_processes() {
do {
pid = wait(&status);
if (pid == -1) {
UNITTEST_UEXIT("wait() failed");
UNITTEST_UNEXPECTED("wait() failed");
}
} while (!WIFEXITED(status));
status = WEXITSTATUS(status);
Expand Down
Loading

0 comments on commit d8bbaeb

Please sign in to comment.