TinyLlama.cpp 1.0
A lightweight C++ implementation of the TinyLlama language model
Loading...
Searching...
No Matches
Public Types | Static Public Member Functions | Static Private Member Functions | Static Private Attributes | List of all members
Logger Class Reference

Static logging class for application-wide logging. More...

#include <logger.h>

Collaboration diagram for Logger:
Collaboration graph

Public Types

enum class  Level : int {
  DEBUG = 0 , INFO = 1 , WARNING = 2 , ERROR = 3 ,
  CRITICAL = 4 , OFF = 5
}
 

Static Public Member Functions

static void set_level (Level new_level)
 
static Level get_level ()
 
static void set_logfile (const std::string &filename)
 
static void enable_console (bool enabled)
 
static void debug (const std::string &message)
 
static void info (const std::string &message)
 
static void warning (const std::string &message)
 
static void error (const std::string &message)
 
static void critical (const std::string &message)
 
static void fatal (const std::string &message)
 
static std::string ptrToString (const void *ptr)
 
static std::string uint16ToHex (uint16_t val)
 
template<typename T >
static std::string to_hex (T val)
 
static void log_vector_stats (const std::string &name, const std::vector< float > &v, int n_show=5)
 
static void log_vector_stats_int8 (const std::string &name, const std::vector< int8_t > &v, int n_show=5)
 

Static Private Member Functions

static void log_internal (Level level, const std::string &message)
 
static std::string level_to_string (Level level)
 
static void ensure_logfile_open_and_truncated ()
 

Static Private Attributes

static Level current_level_ = Logger::Level::INFO
 
static std::ofstream log_file_stream_
 
static std::string log_file_path_ = "debugging.log"
 
static bool console_enabled_ = false
 
static bool log_file_truncated_ = false
 

Detailed Description

Static logging class for application-wide logging.

Provides methods for logging messages at different severity levels (info, error, warning, debug, fatal) and utilities for logging vector statistics. All methods are static and can be called from anywhere in the application.

Definition at line 51 of file logger.h.

Member Enumeration Documentation

◆ Level

enum class Logger::Level : int
strong
Enumerator
DEBUG 
INFO 
WARNING 
ERROR 
CRITICAL 
OFF 

Definition at line 55 of file logger.h.

55 : int {
56 DEBUG = 0,
57 INFO = 1,
58 WARNING = 2,
59 ERROR = 3,
60 CRITICAL = 4,
61 OFF = 5
62 };

Member Function Documentation

◆ critical()

void Logger::critical ( const std::string &  message)
static

Definition at line 147 of file logger.cpp.

147 {
149}
static void log_internal(Level level, const std::string &message)
Definition logger.cpp:101

References CRITICAL, and log_internal().

◆ debug()

void Logger::debug ( const std::string &  message)
static

◆ enable_console()

void Logger::enable_console ( bool  enabled)
static

Definition at line 54 of file logger.cpp.

54 {
55 std::lock_guard<std::mutex> lock(logger_mutex);
56 console_enabled_ = enabled;
57}
static bool console_enabled_
Definition logger.h:94
std::mutex logger_mutex
Definition logger.cpp:32

References console_enabled_, and logger_mutex.

◆ ensure_logfile_open_and_truncated()

void Logger::ensure_logfile_open_and_truncated ( )
staticprivate

Definition at line 71 of file logger.cpp.

71 {
72 if (!log_file_stream_.is_open() || !log_file_truncated_) {
73 if (log_file_stream_.is_open()) {
74 log_file_stream_.close();
75 }
77 log_file_stream_.open(log_file_path_, std::ios::out | std::ios::trunc);
79 } else {
80 log_file_stream_.open(log_file_path_, std::ios::out | std::ios::app);
81 }
82
83 if (!log_file_stream_.is_open()) {
84 // Fallback to cerr if file cannot be opened
85 std::cerr << "[LOGGER_ERROR] Failed to open log file: " << log_file_path_ << std::endl;
86 }
87 } else if (log_file_stream_.is_open() && log_file_stream_.tellp() == 0 && !log_file_truncated_) {
88 // File was opened by another instance/call but not truncated by this instance yet.
89 // This case is tricky with static loggers if not managed carefully.
90 // For simplicity, we assume first successful open truncates.
91 // Re-opening in trunc mode if it's empty and we haven't truncated.
92 log_file_stream_.close();
93 log_file_stream_.open(log_file_path_, std::ios::out | std::ios::trunc);
95 if (!log_file_stream_.is_open()) {
96 std::cerr << "[LOGGER_ERROR] Failed to re-open/truncate log file: " << log_file_path_ << std::endl;
97 }
98 }
99}
static bool log_file_truncated_
Definition logger.h:95
static std::string log_file_path_
Definition logger.h:93
static std::ofstream log_file_stream_
Definition logger.h:92

References log_file_path_, log_file_stream_, and log_file_truncated_.

Referenced by log_internal(), and set_logfile().

◆ error()

void Logger::error ( const std::string &  message)
static

Definition at line 143 of file logger.cpp.

143 {
144 log_internal(Level::ERROR, message);
145}

References ERROR, and log_internal().

Referenced by Tokenizer::add_bigram_to_queue_refactored(), apply_rope_batch_cpu(), apply_rope_vector(), tinyllama::argmax(), argmax(), attention_batch_cpu(), attention_batch_cpu_sequence_aware(), tinyllama::TinyLlamaSession::batch_generation_parallel(), tinyllama::TinyLlamaSession::batch_prefill_parallel(), Tokenizer::bpe_tokenize_to_ids(), dequantize_vector_q4k_to_f32(), dequantize_vector_q6k_to_f32(), dequantize_vector_q8_0_to_f32(), Tokenizer::encode(), TinyLlamaModel::forward(), CPUBatchProcessor::forward_cpu_batch(), TinyLlamaModel::forward_cpu_batch_generation(), TinyLlamaModel::forward_cpu_logits_batch(), tinyllama::TinyLlamaSession::generate(), tinyllama::TinyLlamaSession::generate_batch(), KVCache::initialize(), TinyLlamaModel::initialize_gpu_and_rope(), TinyLlamaModel::initialize_rope_freqs(), TinyLlamaModel::initialize_weights(), SafeTensorsLoader::load_all_tensors_parallel(), load_gguf_meta(), SafeTensorsLoader::load_model_config_from_json(), TinyLlamaModel::lookup_embedding(), main(), matmul_f32_f32_batch_cpu(), matmul_q4k_f32_batch_cpu(), matmul_q6k_f32_batch_cpu(), matmul_q8_0_f32_batch_cpu(), matvec_bf16_f32_vector_cpu(), matvec_f32_f32_vector_cpu(), rmsnorm_batch_cpu(), rmsnorm_vector_cpu(), TinyLlamaModel::TinyLlamaModel(), Tokenizer::Tokenizer(), Tokenizer::Tokenizer(), update_kv_cache_batch_cpu(), update_kv_cache_batch_cpu_sequence_aware(), weighted_sum_probs_v(), write_vector_to_file(), Shard::~Shard(), and TinyLlamaModel::~TinyLlamaModel().

◆ fatal()

void Logger::fatal ( const std::string &  message)
static

Definition at line 151 of file logger.cpp.

151 {
152 log_internal(Level::CRITICAL, "[FATAL] " + message); // Log as critical
153 if (log_file_stream_.is_open()) {
154 log_file_stream_.close();
155 }
156 std::cerr << "[FATAL] " << message << std::endl; // Ensure it goes to cerr
157 std::exit(EXIT_FAILURE);
158}

References CRITICAL, log_file_stream_, and log_internal().

Referenced by TinyLlamaModel::initialize_weights().

◆ get_level()

Logger::Level Logger::get_level ( )
static

Definition at line 39 of file logger.cpp.

39 {
40 std::lock_guard<std::mutex> lock(logger_mutex);
41 return current_level_;
42}
static Level current_level_
Definition logger.h:91

References current_level_, and logger_mutex.

◆ info()

void Logger::info ( const std::string &  message)
static

Definition at line 135 of file logger.cpp.

135 {
136 log_internal(Level::INFO, message);
137}

References INFO, and log_internal().

Referenced by Tokenizer::apply_chat_template(), attention_batch_cpu(), tinyllama::TinyLlamaSession::batch_generation_parallel(), tinyllama::TinyLlamaSession::batch_prefill_parallel(), TinyLlamaModel::clear_layer_dequantized_weights(), TinyLlamaModel::ensure_bf16_concatenated_weights_loaded(), TinyLlamaModel::ensure_f32_concatenated_weights_loaded(), TinyLlamaModel::ensure_q_proj_dequantized(), TinyLlamaModel::forward(), TinyLlamaModel::forward_cpu_batch_generation(), TinyLlamaModel::forward_cpu_logits_batch(), TinyLlamaModel::free_bf16_concatenated_weights(), tinyllama::TinyLlamaSession::generate(), tinyllama::TinyLlamaSession::generate_batch(), generate_bpe_merges_from_vocab_scores(), KVCache::initialize(), TinyLlamaModel::initialize_gpu_and_rope(), TinyLlamaModel::initialize_rope_freqs(), TinyLlamaModel::initialize_weights(), SafeTensorsLoader::load_all_tensors_parallel(), Tokenizer::load_bpe_merges_from_json(), SafeTensorsLoader::load_from_directory(), load_gguf_meta(), SafeTensorsLoader::load_model_config_from_json(), SafeTensorsLoader::load_single_file(), Tokenizer::load_vocab_from_json(), log_raw_float_pointer(), log_vec_stats(), log_vector_summary(), tinyllama::log_vector_summary_detailed(), log_vector_summary_detailed(), log_vector_summary_with_tail(), TinyLlamaModel::lookup_embedding(), main(), matvec_q8k_f32_vector_cpu(), parse_model_config(), parse_model_config_from_gguf(), SafeTensorsLoader::SafeTensorsLoader(), Shard::Shard(), TinyLlamaModel::smart_gemm_batch_cuda(), TinyLlamaModel::TinyLlamaModel(), TinyLlamaModel::TinyLlamaModel(), TinyLlamaModel::TinyLlamaModel(), tinyllama::TinyLlamaSession::TinyLlamaSession(), Tokenizer::Tokenizer(), Tokenizer::Tokenizer(), update_kv_cache_batch_cpu(), write_vector_to_file(), SafeTensorsLoader::~SafeTensorsLoader(), TinyLlamaModel::~TinyLlamaModel(), and tinyllama::TinyLlamaSession::~TinyLlamaSession().

◆ level_to_string()

std::string Logger::level_to_string ( Level  level)
staticprivate

Definition at line 59 of file logger.cpp.

59 {
60 switch (level) {
61 case Level::DEBUG: return "DEBUG";
62 case Level::INFO: return "INFO";
63 case Level::WARNING: return "WARNING";
64 case Level::ERROR: return "ERROR";
65 case Level::CRITICAL: return "CRITICAL";
66 case Level::OFF: return "OFF"; // Should not happen if check is done before
67 default: return "UNKNOWN";
68 }
69}

References CRITICAL, DEBUG, ERROR, INFO, OFF, and WARNING.

Referenced by log_internal().

◆ log_internal()

void Logger::log_internal ( Level  level,
const std::string &  message 
)
staticprivate

Definition at line 101 of file logger.cpp.

101 {
102 std::lock_guard<std::mutex> lock(logger_mutex);
103 if (level < current_level_ || level == Level::OFF) {
104 return;
105 }
106
108
109 auto now = std::chrono::system_clock::now();
110 auto now_c = std::chrono::system_clock::to_time_t(now);
111
112 std::ostringstream log_line_stream;
113 log_line_stream << std::put_time(std::localtime(&now_c), "%Y-%m-%d %H:%M:%S")
114 << " [" << level_to_string(level) << "] " << message;
115 std::string log_line = log_line_stream.str();
116
117 if (console_enabled_) {
118 if (level == Level::ERROR || level == Level::CRITICAL || level == Level::WARNING) {
119 std::cerr << log_line << std::endl;
120 } else {
121 std::cout << log_line << std::endl;
122 }
123 }
124
125 if (log_file_stream_.is_open()) {
126 log_file_stream_ << log_line << std::endl;
127 log_file_stream_.flush(); // Ensure it's written immediately
128 }
129}
static void ensure_logfile_open_and_truncated()
Definition logger.cpp:71
static std::string level_to_string(Level level)
Definition logger.cpp:59

References console_enabled_, CRITICAL, current_level_, ensure_logfile_open_and_truncated(), ERROR, level_to_string(), log_file_stream_, logger_mutex, OFF, and WARNING.

Referenced by critical(), debug(), error(), fatal(), info(), log_vector_stats(), log_vector_stats_int8(), and warning().

◆ log_vector_stats()

void Logger::log_vector_stats ( const std::string &  name,
const std::vector< float > &  v,
int  n_show = 5 
)
static

Definition at line 160 of file logger.cpp.

161 {
162 if (v.empty()) {
163 log_internal(Level::INFO, name + ": (empty vector)");
164 return;
165 }
166 float min_val = v[0];
167 float max_val = v[0];
168 double sum_val = 0.0;
169 bool all_finite_vals = true;
170
171 for (float val : v) {
172 if (std::isnan(val) || std::isinf(val)) {
173 all_finite_vals = false;
174 }
175 if (val < min_val) min_val = val;
176 if (val > max_val) max_val = val;
177 sum_val += static_cast<double>(val);
178 }
179 float mean_val = static_cast<float>(sum_val / v.size());
180
181 std::ostringstream oss;
182 oss << name << ": size=" << v.size()
183 << ", min=" << min_val
184 << ", max=" << max_val
185 << ", mean=" << mean_val
186 << ", all_finite=" << (all_finite_vals ? "yes" : "no")
187 << ", first_vals=[";
188 for (int i = 0; i < std::min((int)v.size(), n_show); ++i) {
189 oss << v[i] << (i == std::min((int)v.size(), n_show) - 1 ? "" : ", ");
190 }
191 oss << "]";
192 log_internal(Level::INFO, oss.str());
193}

References INFO, and log_internal().

◆ log_vector_stats_int8()

void Logger::log_vector_stats_int8 ( const std::string &  name,
const std::vector< int8_t > &  v,
int  n_show = 5 
)
static

Definition at line 195 of file logger.cpp.

195 {
196 if (v.empty()) {
197 log_internal(Level::INFO, name + ": (empty int8_t vector)");
198 return;
199 }
200 int8_t min_val = v[0];
201 int8_t max_val = v[0];
202 long long sum_val = 0; // Use long long for sum to avoid overflow with many int8_t values
203
204 for (int8_t val : v) {
205 if (val < min_val) min_val = val;
206 if (val > max_val) max_val = val;
207 sum_val += static_cast<long long>(val);
208 }
209 // Cast sum to double for mean calculation to preserve precision before casting to float
210 float mean_val = static_cast<float>(static_cast<double>(sum_val) / v.size());
211
212 std::ostringstream oss;
213 oss << name << ": size=" << v.size()
214 << ", min=" << static_cast<int>(min_val) // Cast to int for printing
215 << ", max=" << static_cast<int>(max_val) // Cast to int for printing
216 << ", mean=" << mean_val
217 << ", first_vals=[";
218 for (int i = 0; i < std::min((int)v.size(), n_show); ++i) {
219 oss << static_cast<int>(v[i]) << (i == std::min((int)v.size(), n_show) - 1 ? "" : ", ");
220 }
221 oss << "]";
222 log_internal(Level::INFO, oss.str());
223}

References INFO, and log_internal().

◆ ptrToString()

std::string Logger::ptrToString ( const void *  ptr)
static

Definition at line 225 of file logger.cpp.

225 {
226 std::ostringstream oss;
227 oss << ptr;
228 return oss.str();
229}

Referenced by TinyLlamaModel::initialize_gpu_and_rope().

◆ set_level()

void Logger::set_level ( Level  new_level)
static

Definition at line 34 of file logger.cpp.

34 {
35 std::lock_guard<std::mutex> lock(logger_mutex);
36 current_level_ = new_level;
37}

References current_level_, and logger_mutex.

◆ set_logfile()

void Logger::set_logfile ( const std::string &  filename)
static

Definition at line 44 of file logger.cpp.

44 {
45 std::lock_guard<std::mutex> lock(logger_mutex);
46 if (log_file_stream_.is_open()) {
47 log_file_stream_.close();
48 }
49 log_file_path_ = filename;
50 log_file_truncated_ = false; // Force truncation for new file
51 ensure_logfile_open_and_truncated(); // Open new file
52}

References ensure_logfile_open_and_truncated(), log_file_path_, log_file_stream_, log_file_truncated_, and logger_mutex.

◆ to_hex()

template<typename T >
std::string Logger::to_hex ( val)
static

Definition at line 233 of file logger.cpp.

233 {
234 std::ostringstream oss;
235 oss << "0x" << std::hex << static_cast<unsigned long long>(val); // Cast to larger type if necessary
236 return oss.str();
237}

Referenced by uint16ToHex().

◆ uint16ToHex()

std::string Logger::uint16ToHex ( uint16_t  val)
static

Definition at line 242 of file logger.cpp.

242 {
243 return to_hex(val); // Now calls the templated version
244}
static std::string to_hex(T val)
Definition logger.cpp:233

References to_hex().

◆ warning()

void Logger::warning ( const std::string &  message)
static

Definition at line 139 of file logger.cpp.

139 {
141}

References log_internal(), and WARNING.

Referenced by Tokenizer::apply_chat_template(), apply_rope_batch_cpu(), apply_rope_vector(), attention_batch_cpu(), tinyllama::TinyLlamaSession::batch_generation_parallel(), Tokenizer::bpe_tokenize(), Tokenizer::bpe_tokenize_from_scores(), Tokenizer::bpe_tokenize_to_ids(), TinyLlamaModel::clear_layer_dequantized_weights(), Tokenizer::decode_sentencepiece(), dequantize_vector_q4k_to_f32(), dequantize_vector_q6k_to_f32(), dequantize_vector_q8_0_to_f32(), Tokenizer::encode(), CPUBatchProcessor::forward_cpu_batch(), tinyllama::TinyLlamaSession::generate(), tinyllama::TinyLlamaSession::generate_batch(), generate_bpe_merges_from_vocab_scores(), Tokenizer::ids_to_tokens(), KVCache::initialize(), KVCache::initialize_batch(), TinyLlamaModel::initialize_gpu_and_rope(), TinyLlamaModel::initialize_rope_freqs(), Tokenizer::load_bpe_merges_from_json(), SafeTensorsLoader::load_from_directory(), load_gguf_meta(), SafeTensorsLoader::load_model_config_from_json(), Tokenizer::load_sentencepiece_model(), Tokenizer::load_vocab_from_json(), main(), parse_model_config(), parse_model_config_from_gguf(), SafeTensorsLoader::parse_shard_metadata(), SafeTensorsLoader::SafeTensorsLoader(), TinyLlamaModel::smart_gemm_batch_cuda(), TinyLlamaModel::TinyLlamaModel(), TinyLlamaModel::TinyLlamaModel(), tinyllama::TinyLlamaSession::TinyLlamaSession(), Tokenizer::Tokenizer(), and Tokenizer::Tokenizer().

Member Data Documentation

◆ console_enabled_

bool Logger::console_enabled_ = false
staticprivate

Definition at line 94 of file logger.h.

Referenced by enable_console(), and log_internal().

◆ current_level_

Logger::Level Logger::current_level_ = Logger::Level::INFO
staticprivate

Definition at line 91 of file logger.h.

Referenced by get_level(), log_internal(), and set_level().

◆ log_file_path_

std::string Logger::log_file_path_ = "debugging.log"
staticprivate

Definition at line 93 of file logger.h.

Referenced by ensure_logfile_open_and_truncated(), and set_logfile().

◆ log_file_stream_

std::ofstream Logger::log_file_stream_
staticprivate

Definition at line 92 of file logger.h.

Referenced by ensure_logfile_open_and_truncated(), fatal(), log_internal(), and set_logfile().

◆ log_file_truncated_

bool Logger::log_file_truncated_ = false
staticprivate

Definition at line 95 of file logger.h.

Referenced by ensure_logfile_open_and_truncated(), and set_logfile().


The documentation for this class was generated from the following files: