-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathutils.hpp
More file actions
63 lines (56 loc) · 4.33 KB
/
utils.hpp
File metadata and controls
63 lines (56 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma once
#include "../utils/infini_status_string.h"
#include <spdlog/cfg/env.h>
#include <spdlog/spdlog.h>
#include <stdexcept>
inline struct SpdlogInitializer {
SpdlogInitializer() {
if (!std::getenv("INFINICORE_LOG_LEVEL")) {
spdlog::set_level(spdlog::level::info);
} else {
spdlog::cfg::load_env_levels("INFINICORE_LOG_LEVEL");
}
// Set pattern for logging
// Using SPDLOG_* macros enables source location support (%s and %#)
// Format: [timestamp] [level] [file:line] message
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] [%s:%#] %v");
}
} spdlog_initializer;
#define STRINGIZE_(x) #x
#define STRINGIZE(x) STRINGIZE_(x)
#define INFINICORE_CHECK_ERROR(call) \
do { \
SPDLOG_DEBUG("Entering `" #call "` at `" __FILE__ ":" STRINGIZE(__LINE__) "`."); \
infiniStatus_t ret = (call); \
SPDLOG_DEBUG("Exiting `" #call "` at `" __FILE__ ":" STRINGIZE(__LINE__) "`."); \
if (ret != INFINI_STATUS_SUCCESS) { \
throw std::runtime_error("`" #call "` failed with error: " + std::string(infini_status_string(ret)) \
+ " from " + std::string(__func__) \
+ " at " + std::string(__FILE__) \
+ ":" + std::to_string(__LINE__) + "."); \
} \
} while (false)
#define INFINICORE_ASSERT_TENSORS_SAME_DEVICE(FIRST___, ...) \
do { \
const auto &first_device___ = (FIRST___)->device(); \
for (const auto &tensor___ : {__VA_ARGS__}) { \
if (first_device___ != (tensor___)->device()) { \
throw std::runtime_error("Tensor devices mismatch " \
+ first_device___.toString() + " vs " \
+ (tensor___)->device().toString() \
+ " from " + std::string(__func__) \
+ " at " + std::string(__FILE__) \
+ ":" + std::to_string(__LINE__) + "."); \
} \
} \
} while (0)
#define INFINICORE_ASSERT(CONDITION__) \
do { \
if (!(CONDITION__)) { \
SPDLOG_ERROR( \
"Assertion `{}` failed from {} at {}:{}", \
#CONDITION__, __func__, __FILE__, __LINE__); \
throw std::runtime_error( \
std::string("Assertion `") + #CONDITION__ + "` failed from " + __func__ + " at " + __FILE__ + ":" + std::to_string(__LINE__)); \
} \
} while (0)