From 8f4d69e12b178778cca37b4b7f666f5ec0515515 Mon Sep 17 00:00:00 2001 From: LittleMouse Date: Fri, 16 Jan 2026 18:55:10 +0800 Subject: [PATCH 01/22] [update] update ax630c model --- .../main_asr/mode_sense-voice-small-10s.json | 31 +++++++++++++++++++ .../{mode_kws.json => mode_kws-ax630c.json} | 2 +- projects/llm_framework/tools/llm_pack.py | 2 +- 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 projects/llm_framework/main_asr/mode_sense-voice-small-10s.json rename projects/llm_framework/main_kws/{mode_kws.json => mode_kws-ax630c.json} (97%) diff --git a/projects/llm_framework/main_asr/mode_sense-voice-small-10s.json b/projects/llm_framework/main_asr/mode_sense-voice-small-10s.json new file mode 100644 index 0000000..69b0aa2 --- /dev/null +++ b/projects/llm_framework/main_asr/mode_sense-voice-small-10s.json @@ -0,0 +1,31 @@ +{ + "mode": "sense-voice-small-10s", + "type": "asr", + "homepage": "https://huggingface.co/M5Stack/SenseVoiceSmall-axmodel", + "compile_flage": "pulsar2 build --input model-10-seconds.onnx --config config_sensevoice_main_u16.json --output_dir sensevoice-axmodel --output_name model-10-seconds.axmodel --target_hardware AX620E --compiler.check 0", + "pulsar_version": "5.0-patch1-fd447d0d", + "capabilities": [ + "Chinese", + "English", + "Cantonese", + "Japanese", + "Korean" + ], + "input_type": [ + "sys.pcm", + "sys.cap.0_0" + ], + "output_type": [ + "asr.utf-8" + ], + "mode_param": { + "model_config.sense_voice.model": "model.axmodel", + "model_config.sense_voice.use_itn": true, + "model_config.tokens": "tokens.txt", + "silero_vad.model": "silero_vad.ort", + "model_config.provider": "axera", + "silence_timeout": 2000, + "awake_delay": 50 + }, + "mode_param_bak": {} +} \ No newline at end of file diff --git a/projects/llm_framework/main_kws/mode_kws.json b/projects/llm_framework/main_kws/mode_kws-ax630c.json similarity index 97% rename from projects/llm_framework/main_kws/mode_kws.json rename to projects/llm_framework/main_kws/mode_kws-ax630c.json index 14f0d4b..0c4542b 100644 --- a/projects/llm_framework/main_kws/mode_kws.json +++ b/projects/llm_framework/main_kws/mode_kws-ax630c.json @@ -1,5 +1,5 @@ { - "mode": "kws", + "mode": "kws-ax630c", "type": "kws", "homepage": "", "capabilities": [ diff --git a/projects/llm_framework/tools/llm_pack.py b/projects/llm_framework/tools/llm_pack.py index 050e61e..91c6a1f 100755 --- a/projects/llm_framework/tools/llm_pack.py +++ b/projects/llm_framework/tools/llm_pack.py @@ -401,7 +401,7 @@ def create_bin_deb(package_name, version, src_folder, revision = 'm5stack1', dep 'llm-model-sherpa-onnx-streaming-zipformer-small-bilingual-zh-en-2023-02-16-ax650':[create_data_deb,'llm-model-sherpa-onnx-streaming-zipformer-small-bilingual-zh-en-2023-02-16-ax650', '0.4', src_folder, revision], # KWS model ## AX630C - 'llm-model-kws':[create_data_deb,'llm-model-kws', '0.4', src_folder, revision], + 'llm-model-kws-ax630c':[create_data_deb,'llm-model-kws-ax630c', '0.4', src_folder, revision], ## AX650 'llm-model-kws-ax650':[create_data_deb,'llm-model-kws-ax650', '0.4', src_folder, revision], # ASR model From d3ad6eb91bb502df098e522dd4614345fe16cd15 Mon Sep 17 00:00:00 2001 From: dianjixz <18637716021@163.com> Date: Tue, 20 Jan 2026 09:13:45 +0800 Subject: [PATCH 02/22] [update] all deb bin add llm-sys depends --- projects/llm_framework/tools/llm_pack.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/llm_framework/tools/llm_pack.py b/projects/llm_framework/tools/llm_pack.py index 91c6a1f..ddc825b 100755 --- a/projects/llm_framework/tools/llm_pack.py +++ b/projects/llm_framework/tools/llm_pack.py @@ -241,7 +241,7 @@ def create_data_deb(package_name, version, src_folder, revision='m5stack1', depe shutil.rmtree(deb_folder) return package_name + " creat success!" -def create_bin_deb(package_name, version, src_folder, revision = 'm5stack1', depends = 'lib-llm (>= 1.8)'): +def create_bin_deb(package_name, version, src_folder, revision = 'm5stack1', depends = 'lib-llm (>= 1.8), llm-sys'): bin_files = glob.glob(os.path.join(src_folder, package_name.replace("-", "_") + "-*")) version_info = "0" if bin_files: @@ -373,7 +373,7 @@ def create_bin_deb(package_name, version, src_folder, revision = 'm5stack1', dep Tasks = { # Unit 'lib-llm':[create_lib_deb,'lib-llm', '1.9', src_folder, revision], - 'llm-sys':[create_bin_deb,'llm-sys', '1.6', src_folder, revision], + 'llm-sys':[create_bin_deb,'llm-sys', '1.6', src_folder, revision, 'lib-llm (>= 1.8)'], 'llm-audio':[create_bin_deb,'llm-audio', '1.8', src_folder, revision], 'llm-kws':[create_bin_deb,'llm-kws', '1.10', src_folder, revision], 'llm-asr':[create_bin_deb,'llm-asr', '1.8', src_folder, revision], From c89412ef6f07261f3d0cd00901ae689aa812777e Mon Sep 17 00:00:00 2001 From: LittleMouse Date: Thu, 22 Jan 2026 18:25:20 +0800 Subject: [PATCH 03/22] [update] update llm-asr llm-audio llm-kws llm-melotts & add AI_Pyramid_Demo file --- projects/llm_framework/main_asr/SConstruct | 2 +- projects/llm_framework/main_asr/src/main.cpp | 185 ++++------ projects/llm_framework/main_audio/SConstruct | 2 +- .../llm_framework/main_audio/src/alsa_audio.c | 322 ++++++++++++------ .../llm_framework/main_audio/src/main.cpp | 76 ++++- projects/llm_framework/main_kws/SConstruct | 2 +- projects/llm_framework/main_kws/src/main.cpp | 30 +- .../llm_framework/main_melotts/SConstruct | 2 +- .../llm_framework/main_melotts/src/main.cpp | 48 +-- projects/llm_framework/tools/llm_pack.py | 8 +- .../tools/test_tools/AI_Pyramid_Demo.py | 235 +++++++++++++ 11 files changed, 634 insertions(+), 278 deletions(-) create mode 100644 projects/llm_framework/tools/test_tools/AI_Pyramid_Demo.py diff --git a/projects/llm_framework/main_asr/SConstruct b/projects/llm_framework/main_asr/SConstruct index 1098aab..e498541 100644 --- a/projects/llm_framework/main_asr/SConstruct +++ b/projects/llm_framework/main_asr/SConstruct @@ -40,7 +40,7 @@ LDFLAGS += [ STATIC_FILES += Glob('mode_*.json') -env['COMPONENTS'].append({'target':'llm_asr-1.8', +env['COMPONENTS'].append({'target':'llm_asr-1.9', 'SRCS':SRCS, 'INCLUDE':INCLUDE, 'PRIVATE_INCLUDE':PRIVATE_INCLUDE, diff --git a/projects/llm_framework/main_asr/src/main.cpp b/projects/llm_framework/main_asr/src/main.cpp index 024de85..6e1044b 100644 --- a/projects/llm_framework/main_asr/src/main.cpp +++ b/projects/llm_framework/main_asr/src/main.cpp @@ -116,6 +116,7 @@ class llm_task { int delay_audio_frame_ = 10; float silence_ms_accum_ = 0.0f; float silence_timeout = 1000.0f; + std::string pending_text_; buffer_t *pcmdata; std::function pause; @@ -554,146 +555,98 @@ class llm_task { void sys_pcm_on_data_onnx(const std::string &raw) { - if (delay_audio_frame_ == 0) { - buffer_write_char(pcmdata, raw.data(), raw.length()); - buffer_position_set(pcmdata, 0); - - std::vector floatSamples; - int16_t audio_val; - while (buffer_read_i16(pcmdata, &audio_val, 1)) { - float normalizedSample = static_cast(audio_val) / INT16_MAX; - floatSamples.push_back(normalizedSample); - } + if (raw.size() >= sizeof(int16_t)) { + const int16_t *pcm16 = reinterpret_cast(raw.data()); + size_t n16 = raw.size() / sizeof(int16_t); + PushPreRollPcm(pcm16, n16); + } - buffer_resize(pcmdata, 0); - int32_t window_size = vad_config_.silero_vad.window_size; - int32_t i = 0; - std::string final_text; + static int count = 0; + if (count < delay_audio_frame_) { + buffer_write_char(pcmdata, raw.data(), raw.length()); + count++; + return; + } - while (i < floatSamples.size()) { - if (i + window_size <= floatSamples.size()) { - vad_->AcceptWaveform(floatSamples.data() + i, window_size); - } else { - vad_->Flush(); - } - i += window_size; + buffer_write_char(pcmdata, raw.data(), raw.length()); + buffer_position_set(pcmdata, 0); - while (!vad_->Empty()) { - const auto &segment = vad_->Front(); - float duration = segment.samples.size() / 16000.f; - float start_time = segment.start / 16000.f; - float end_time = start_time + duration; + std::vector floatSamples; + floatSamples.reserve((delay_audio_frame_ + 1) * kFrameSamples); - if (duration < 0.1f) { - vad_->Pop(); - continue; - } + int16_t audio_val; + while (buffer_read_i16(pcmdata, &audio_val, 1)) { + floatSamples.push_back(static_cast(audio_val) / 32768.0f); + } - if (!offline_stream_) offline_stream_ = onnx_recognizer_->CreateStream(); + buffer_resize(pcmdata, 0); + count = 0; - offline_stream_->AcceptWaveform(onnx_asr_config_.feat_config.sampling_rate, segment.samples.data(), - segment.samples.size()); + vad_->AcceptWaveform(floatSamples.data(), floatSamples.size()); + bool detected = vad_->IsSpeechDetected(); + bool speech_start = (!prev_vad_detected_ && detected); + prev_vad_detected_ = detected; - onnx_recognizer_->DecodeStream(offline_stream_.get()); - const auto &result = offline_stream_->GetResult(); + while (!vad_->Empty()) { + const auto &segment = vad_->Front(); - final_text += result.text; + if (!offline_stream_) { + offline_stream_ = onnx_recognizer_->CreateStream(); + } - vad_->Pop(); - offline_stream_.reset(); + if (speech_start && !pre_roll_pcm_.empty()) { + std::vector pre; + pre.reserve(pre_roll_pcm_.size()); + for (int16_t s : pre_roll_pcm_) { + pre.push_back(static_cast(s) / 32768.0f); } - } - if (out_callback_) { - out_callback_(final_text, true); - } - } else { - if (raw.size() >= sizeof(int16_t)) { - const int16_t *pcm16 = reinterpret_cast(raw.data()); - size_t n16 = raw.size() / sizeof(int16_t); - PushPreRollPcm(pcm16, n16); - } + std::vector merged; + merged.reserve(pre.size() + segment.samples.size()); + merged.insert(merged.end(), pre.begin(), pre.end()); + merged.insert(merged.end(), segment.samples.begin(), segment.samples.end()); - static int count = 0; - if (count < delay_audio_frame_) { - buffer_write_char(pcmdata, raw.data(), raw.length()); - count++; - return; + offline_stream_->AcceptWaveform(kSampleRate, merged.data(), merged.size()); + pre_roll_pcm_.clear(); + speech_start = false; + } else { + offline_stream_->AcceptWaveform(kSampleRate, segment.samples.data(), segment.samples.size()); } - buffer_write_char(pcmdata, raw.data(), raw.length()); - buffer_position_set(pcmdata, 0); - - std::vector floatSamples; - floatSamples.reserve((delay_audio_frame_ + 1) * kFrameSamples); + onnx_recognizer_->DecodeStream(offline_stream_.get()); + const auto &result = offline_stream_->GetResult(); - int16_t audio_val; - while (buffer_read_i16(pcmdata, &audio_val, 1)) { - floatSamples.push_back(static_cast(audio_val) / 32768.0f); + if (!result.text.empty() && out_callback_) { + out_callback_(result.text, false); } - buffer_resize(pcmdata, 0); - count = 0; - - vad_->AcceptWaveform(floatSamples.data(), floatSamples.size()); - - bool detected = vad_->IsSpeechDetected(); - bool speech_start = (!prev_vad_detected_ && detected); - prev_vad_detected_ = detected; - - while (!vad_->Empty()) { - const auto &segment = vad_->Front(); - - if (!offline_stream_) { - offline_stream_ = onnx_recognizer_->CreateStream(); - } - - if (speech_start && !pre_roll_pcm_.empty()) { - std::vector pre; - pre.reserve(pre_roll_pcm_.size()); - for (int16_t s : pre_roll_pcm_) { - pre.push_back(static_cast(s) / 32768.0f); - } - - std::vector merged; - merged.reserve(pre.size() + segment.samples.size()); - merged.insert(merged.end(), pre.begin(), pre.end()); - merged.insert(merged.end(), segment.samples.begin(), segment.samples.end()); - - offline_stream_->AcceptWaveform(kSampleRate, merged.data(), merged.size()); - - pre_roll_pcm_.clear(); - speech_start = false; - } else { - offline_stream_->AcceptWaveform(kSampleRate, segment.samples.data(), segment.samples.size()); - } - - onnx_recognizer_->DecodeStream(offline_stream_.get()); - - const auto &result = offline_stream_->GetResult(); - if (!result.text.empty() && out_callback_) { - out_callback_(result.text, true); - } + if (!result.text.empty()) { + if (!pending_text_.empty()) pending_text_ += " "; + pending_text_ += result.text; + } - vad_->Pop(); + vad_->Pop(); + offline_stream_.reset(); + } - offline_stream_.reset(); + { + float chunk_ms = (delay_audio_frame_ + 1) * 10.0f; + if (detected) { + silence_ms_accum_ = 0.0f; + } else { + silence_ms_accum_ += chunk_ms; } - { - float chunk_ms = (delay_audio_frame_ + 1) * 10.0f; - if (detected) { - silence_ms_accum_ = 0.0f; - } else { - silence_ms_accum_ += chunk_ms; + if (silence_ms_accum_ >= silence_timeout) { + if (!pending_text_.empty() && out_callback_) { + out_callback_(pending_text_, true); } + pending_text_.clear(); - if (silence_ms_accum_ >= silence_timeout) { - if (ensleep_) { - if (pause) pause(); - } - silence_ms_accum_ = 0.0f; + if (ensleep_) { + if (pause) pause(); } + silence_ms_accum_ = 0.0f; } } } diff --git a/projects/llm_framework/main_audio/SConstruct b/projects/llm_framework/main_audio/SConstruct index 3e1c8d6..1f82d1f 100644 --- a/projects/llm_framework/main_audio/SConstruct +++ b/projects/llm_framework/main_audio/SConstruct @@ -32,7 +32,7 @@ if 'CONFIG_AX_620E_MSP_ENABLED' in os.environ: STATIC_FILES += [AFile('audio.json'), AFile('audio_kit.json'), AFile('audio_pyramid.json')] STATIC_FILES += Glob('mode_*.json') -env['COMPONENTS'].append({'target':'llm_audio-1.8', +env['COMPONENTS'].append({'target':'llm_audio-1.9', 'SRCS':SRCS, 'INCLUDE':INCLUDE, 'PRIVATE_INCLUDE':PRIVATE_INCLUDE, diff --git a/projects/llm_framework/main_audio/src/alsa_audio.c b/projects/llm_framework/main_audio/src/alsa_audio.c index fa86a5e..7902500 100644 --- a/projects/llm_framework/main_audio/src/alsa_audio.c +++ b/projects/llm_framework/main_audio/src/alsa_audio.c @@ -1,30 +1,82 @@ #include "alsa_audio.h" -#include "samplerate.h" #include +#include +#include +#include +#include +#include #include #include -#include #include -static struct pcm *g_play_pcm = NULL; -static int gplayLoopExit = 1; -static int gcapLoopExit = 1; +static atomic_int gplayLoopExit = 2; +static atomic_int gcapLoopExit = 2; + +static struct pcm *g_play_pcm = NULL; +static struct pcm *g_cap_pcm = NULL; +static pthread_mutex_t g_play_mtx = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t g_cap_mtx = PTHREAD_MUTEX_INITIALIZER; + AlsaConfig cap_config; AlsaConfig play_config; +#ifndef HAVE_PCM_STOP +#define HAVE_PCM_STOP 1 +#endif + +static void set_play_pcm(struct pcm *pcm) +{ + pthread_mutex_lock(&g_play_mtx); + g_play_pcm = pcm; + pthread_mutex_unlock(&g_play_mtx); +} + +static struct pcm *get_play_pcm(void) +{ + pthread_mutex_lock(&g_play_mtx); + struct pcm *pcm = g_play_pcm; + pthread_mutex_unlock(&g_play_mtx); + return pcm; +} + +static void clear_play_pcm_if_match(struct pcm *pcm) +{ + pthread_mutex_lock(&g_play_mtx); + if (g_play_pcm == pcm) g_play_pcm = NULL; + pthread_mutex_unlock(&g_play_mtx); +} + +static void set_cap_pcm(struct pcm *pcm) +{ + pthread_mutex_lock(&g_cap_mtx); + g_cap_pcm = pcm; + pthread_mutex_unlock(&g_cap_mtx); +} + +static struct pcm *get_cap_pcm(void) +{ + pthread_mutex_lock(&g_cap_mtx); + struct pcm *pcm = g_cap_pcm; + pthread_mutex_unlock(&g_cap_mtx); + return pcm; +} + +static void clear_cap_pcm_if_match(struct pcm *pcm) +{ + pthread_mutex_lock(&g_cap_mtx); + if (g_cap_pcm == pcm) g_cap_pcm = NULL; + pthread_mutex_unlock(&g_cap_mtx); +} + void alsa_cap_start(unsigned int card, unsigned int device, float Volume, int channel, int rate, int bit, AUDIOCallback callback) { - gcapLoopExit = 0; - struct pcm_config config; - unsigned int pcm_open_flags; - struct pcm *pcm; - char *buffer; - unsigned int size; - unsigned int frames_read; - unsigned int total_frames_read; - unsigned int bytes_per_frame; + (void)Volume; + (void)bit; + atomic_store(&gcapLoopExit, 0); + + struct pcm_config config; memset(&config, 0, sizeof(config)); config.channels = channel; config.rate = 48000; @@ -35,94 +87,110 @@ void alsa_cap_start(unsigned int card, unsigned int device, float Volume, int ch config.stop_threshold = 0; config.silence_threshold = 0; - pcm_open_flags = PCM_IN; - if (1) pcm_open_flags |= PCM_MMAP; - - pcm = pcm_open(card, device, pcm_open_flags, &config); + unsigned int pcm_open_flags = PCM_IN | PCM_MMAP; + struct pcm *pcm = pcm_open(card, device, pcm_open_flags, &config); if (!pcm || !pcm_is_ready(pcm)) { - fprintf(stderr, "Unable to open PCM device (%s)\n", pcm_get_error(pcm)); + fprintf(stderr, "Unable to open PCM capture device (%s)\n", pcm ? pcm_get_error(pcm) : "invalid pcm"); + if (pcm) pcm_close(pcm); + atomic_store(&gcapLoopExit, 2); return; } - size = pcm_frames_to_bytes(pcm, pcm_get_buffer_size(pcm)); - buffer = malloc(size); + set_cap_pcm(pcm); + + const int in_channels = channel; + const int in_frames = (int)pcm_get_buffer_size(pcm); + const int in_bytes = (int)pcm_frames_to_bytes(pcm, in_frames); + + uint8_t *buffer = (uint8_t *)malloc((size_t)in_bytes); if (!buffer) { - fprintf(stderr, "Unable to allocate %u bytes\n", size); + fprintf(stderr, "Unable to allocate %d bytes\n", in_bytes); + clear_cap_pcm_if_match(pcm); pcm_close(pcm); + atomic_store(&gcapLoopExit, 2); return; } - if (1) { - printf("Capturing sample: %u ch, %u hz, %u bit\n", channel, rate, pcm_format_to_bits(PCM_FORMAT_S16_LE)); + int16_t *ch0 = (int16_t *)malloc((size_t)in_frames * sizeof(int16_t)); + if (!ch0) { + fprintf(stderr, "Unable to allocate ch0 buffer\n"); + free(buffer); + clear_cap_pcm_if_match(pcm); + pcm_close(pcm); + atomic_store(&gcapLoopExit, 2); + return; } - bytes_per_frame = pcm_frames_to_bytes(pcm, 1); - total_frames_read = 0; - SRC_STATE *src_state = NULL; - float *in_float = NULL, *out_float = NULL; + float *in_float = NULL; + float *out_float = NULL; + int16_t *out_short = NULL; + int out_frames_cap = 0; - int in_frames = pcm_get_buffer_size(pcm); - int out_frames = (int)((float)in_frames * ((float)rate / 48000.0f) + 1); + if (rate != 48000) { + int err = 0; + src_state = src_new(SRC_SINC_FASTEST, 1, &err); + if (!src_state) { + fprintf(stderr, "src_new failed: %s\n", src_strerror(err)); + free(ch0); + free(buffer); + clear_cap_pcm_if_match(pcm); + pcm_close(pcm); + atomic_store(&gcapLoopExit, 2); + return; + } - int src_channels = 1; + out_frames_cap = (int)((double)in_frames * ((double)rate / 48000.0) + 64); + in_float = (float *)malloc((size_t)in_frames * sizeof(float)); + out_float = (float *)malloc((size_t)out_frames_cap * sizeof(float)); + out_short = (int16_t *)malloc((size_t)out_frames_cap * sizeof(int16_t)); - if (rate != 48000) { - src_state = src_new(SRC_SINC_FASTEST, src_channels, NULL); - in_float = malloc(in_frames * src_channels * sizeof(float)); - out_float = malloc(out_frames * src_channels * sizeof(float)); - if (!src_state || !in_float || !out_float) { + if (!in_float || !out_float || !out_short) { fprintf(stderr, "Unable to allocate resample buffers\n"); - free(buffer); if (in_float) free(in_float); if (out_float) free(out_float); + if (out_short) free(out_short); if (src_state) src_delete(src_state); + free(ch0); + free(buffer); + clear_cap_pcm_if_match(pcm); pcm_close(pcm); + atomic_store(&gcapLoopExit, 2); return; } } - while (!gcapLoopExit) { - int ret = pcm_readi(pcm, buffer, in_frames); + fprintf(stdout, "Capturing sample: in=%dch @48000Hz -> out=%dHz, S16LE\n", channel, rate); + + while (atomic_load(&gcapLoopExit) == 0) { + int ret = pcm_readi(pcm, buffer, (unsigned int)in_frames); if (ret < 0) { + if (atomic_load(&gcapLoopExit) == 1) break; fprintf(stderr, "Error capturing samples - %d (%s)\n", errno, strerror(errno)); break; } - frames_read = ret; - total_frames_read += frames_read; - - if (rate == 48000) { - int in_channels = channel; // 比如 4 - int16_t *in = (int16_t *)buffer; - - int16_t *ch0 = malloc(frames_read * sizeof(int16_t)); - if (!ch0) { - fprintf(stderr, "Unable to allocate ch0 buffer\n"); - break; - } + const int frames_read = ret; + if (frames_read <= 0) continue; - for (unsigned int i = 0; i < frames_read; ++i) { - ch0[i] = in[i * in_channels + 0]; - } - - callback((const char *)ch0, frames_read * sizeof(int16_t)); - free(ch0); + const int16_t *in = (const int16_t *)buffer; + for (int i = 0; i < frames_read; ++i) { + ch0[i] = in[i * in_channels]; + } + if (rate == 48000) { + callback((const char *)ch0, frames_read * (int)sizeof(int16_t)); } else { - int in_channels = channel; // 比如 4 - short *in_short = (short *)buffer; - for (int i = 0; i < frames_read; ++i) { - short s = in_short[i * in_channels + 0]; - in_float[i] = s / 32768.0f; + in_float[i] = (float)ch0[i] / 32768.0f; } SRC_DATA src_data; + memset(&src_data, 0, sizeof(src_data)); src_data.data_in = in_float; src_data.input_frames = frames_read; src_data.data_out = out_float; - src_data.output_frames = out_frames; + src_data.output_frames = out_frames_cap; src_data.src_ratio = (double)rate / 48000.0; src_data.end_of_input = 0; @@ -131,46 +199,66 @@ void alsa_cap_start(unsigned int card, unsigned int device, float Volume, int ch fprintf(stderr, "SRC error: %s\n", src_strerror(error)); break; } - int out_samples = src_data.output_frames_gen; - short *out_short = malloc(out_samples * sizeof(short)); - if (!out_short) { - fprintf(stderr, "Unable to allocate out_short buffer\n"); - break; - } + + int out_samples = src_data.output_frames_gen; + if (out_samples > out_frames_cap) out_samples = out_frames_cap; + for (int i = 0; i < out_samples; ++i) { float sample = out_float[i]; if (sample > 1.0f) sample = 1.0f; if (sample < -1.0f) sample = -1.0f; - out_short[i] = (short)(sample * 32767.0f); + out_short[i] = (int16_t)(sample * 32767.0f); } - callback((const char *)out_short, out_samples * sizeof(short)); - free(out_short); + + callback((const char *)out_short, out_samples * (int)sizeof(int16_t)); } } - if (rate != 48000) { - free(in_float); - free(out_float); - src_delete(src_state); - } + if (src_state) src_delete(src_state); + if (in_float) free(in_float); + if (out_float) free(out_float); + if (out_short) free(out_short); + free(ch0); free(buffer); + clear_cap_pcm_if_match(pcm); pcm_close(pcm); + atomic_store(&gcapLoopExit, 2); } -void alsa_close_cap() +void alsa_close_cap(void) { - gcapLoopExit = 1; + atomic_store(&gcapLoopExit, 1); + + struct pcm *pcm = get_cap_pcm(); + if (pcm) { +#if HAVE_PCM_STOP + pcm_stop(pcm); +#else + pthread_mutex_lock(&g_cap_mtx); + if (g_cap_pcm) { + struct pcm *to_close = g_cap_pcm; + g_cap_pcm = NULL; + pthread_mutex_unlock(&g_cap_mtx); + pcm_close(to_close); + } else { + pthread_mutex_unlock(&g_cap_mtx); + } +#endif + } } -int alsa_cap_status() +int alsa_cap_status(void) { - return gcapLoopExit; + return atomic_load(&gcapLoopExit); } void alsa_play(unsigned int card, unsigned int device, float Volume, int channel, int rate, int bit, const void *data, int size) { - gplayLoopExit = 0; + (void)Volume; + (void)bit; + + atomic_store(&gplayLoopExit, 0); struct pcm_config config; memset(&config, 0, sizeof(config)); @@ -184,33 +272,71 @@ void alsa_play(unsigned int card, unsigned int device, float Volume, int channel config.start_threshold = config.period_size; unsigned int pcm_open_flags = PCM_OUT; - - struct pcm *pcm = pcm_open(card, device, pcm_open_flags, &config); + struct pcm *pcm = pcm_open(card, device, pcm_open_flags, &config); if (!pcm || !pcm_is_ready(pcm)) { fprintf(stderr, "Unable to open PCM playback device (%s)\n", pcm ? pcm_get_error(pcm) : "invalid pcm"); - if (pcm) { - pcm_close(pcm); - } - gplayLoopExit = 2; + if (pcm) pcm_close(pcm); + atomic_store(&gplayLoopExit, 2); return; } - int frames = pcm_bytes_to_frames(pcm, size); - int written_frames = pcm_writei(pcm, data, frames); - if (written_frames < 0) { - fprintf(stderr, "PCM playback error %s\n", pcm_get_error(pcm)); + set_play_pcm(pcm); + + const uint8_t *p = (const uint8_t *)data; + int remaining_bytes = size; + + while (remaining_bytes > 0) { + if (atomic_load(&gplayLoopExit) != 0) break; + + int remaining_frames = (int)pcm_bytes_to_frames(pcm, (unsigned int)remaining_bytes); + + int frames_to_write = remaining_frames; + if (frames_to_write > (int)config.period_size) frames_to_write = (int)config.period_size; + + int written_frames = pcm_writei(pcm, p, (unsigned int)frames_to_write); + + if (written_frames < 0) { + if (atomic_load(&gplayLoopExit) == 1) break; + fprintf(stderr, "PCM playback error: %s\n", pcm_get_error(pcm)); + break; + } + + if (written_frames == 0) break; + + int written_bytes = (int)pcm_frames_to_bytes(pcm, (unsigned int)written_frames); + + p += written_bytes; + remaining_bytes -= written_bytes; } - printf("Played %d frames\n", written_frames); + + clear_play_pcm_if_match(pcm); pcm_close(pcm); - gplayLoopExit = 2; + atomic_store(&gplayLoopExit, 2); } -void alsa_close_play() +void alsa_close_play(void) { - gplayLoopExit = 1; + atomic_store(&gplayLoopExit, 1); + + struct pcm *pcm = get_play_pcm(); + if (pcm) { +#if HAVE_PCM_STOP + pcm_stop(pcm); +#else + pthread_mutex_lock(&g_play_mtx); + if (g_play_pcm) { + struct pcm *to_close = g_play_pcm; + g_play_pcm = NULL; + pthread_mutex_unlock(&g_play_mtx); + pcm_close(to_close); + } else { + pthread_mutex_unlock(&g_play_mtx); + } +#endif + } } -int alsa_play_status() +int alsa_play_status(void) { - return gplayLoopExit; + return atomic_load(&gplayLoopExit); } \ No newline at end of file diff --git a/projects/llm_framework/main_audio/src/main.cpp b/projects/llm_framework/main_audio/src/main.cpp index 6a54c7b..5392413 100644 --- a/projects/llm_framework/main_audio/src/main.cpp +++ b/projects/llm_framework/main_audio/src/main.cpp @@ -18,7 +18,7 @@ int main_exit_flage = 0; static void __sigint(int iSigNo) { - SLOGW("llm_sys will be exit!"); + SLOGW("llm_audio will be exit!"); main_exit_flage = 1; } @@ -48,6 +48,15 @@ class llm_audio : public StackFlow { std::unique_ptr audio_cap_thread_; std::mutex ax_play_mtx; + struct PlayItem { + uint64_t gen; + std::shared_ptr data; + }; + + std::atomic play_gen_{0}; + std::atomic play_pending_{0}; + std::atomic_bool play_running_{false}; + static void on_cap_sample(const char *data, int size) { self->pub_ctx_->send_data((const char *)data, size); @@ -71,26 +80,38 @@ class llm_audio : public StackFlow { void hw_queue_play(const std::shared_ptr &arg) { - if (audio_clear_flage_) { + auto item = std::static_pointer_cast(arg); + const uint64_t cur_gen = play_gen_.load(std::memory_order_relaxed); + + if (item->gen != cur_gen) { return; } - std::shared_ptr originalPtr = std::static_pointer_cast(arg); - std::string audio_data(static_cast(originalPtr->data()), originalPtr->size()); + auto pending_guard = std::unique_ptr>( + (void *)1, [&](void *) { play_pending_.fetch_sub(1, std::memory_order_relaxed); }); + if (audio_clear_flage_.load(std::memory_order_relaxed)) { + return; + } + + std::string audio_data((const char *)item->data->data(), item->data->size()); std::string final_data = audio_data; if (play_config.channel == 2) { final_data = mono_to_stereo_s16le(audio_data); } - std::lock_guard guard(ax_play_mtx); + play_running_.store(true, std::memory_order_relaxed); + { + std::lock_guard guard(ax_play_mtx); #if defined(CONFIG_AX_620E_MSP_ENABLED) || defined(CONFIG_AX_620Q_MSP_ENABLED) - ax_play(play_config.card, play_config.device, play_config.volume, play_config.channel, play_config.rate, - play_config.bit, audio_data.c_str(), audio_data.length()); + ax_play(play_config.card, play_config.device, play_config.volume, play_config.channel, play_config.rate, + play_config.bit, audio_data.c_str(), audio_data.length()); #else - alsa_play(play_config.card, play_config.device, play_config.volume, play_config.channel, play_config.rate, - play_config.bit, final_data.c_str(), final_data.length()); + alsa_play(play_config.card, play_config.device, play_config.volume, play_config.channel, play_config.rate, + play_config.bit, final_data.c_str(), final_data.length()); #endif + } + play_running_.store(false, std::memory_order_relaxed); } void hw_play(const std::string &audio_data) @@ -103,7 +124,7 @@ class llm_audio : public StackFlow { std::lock_guard guard(ax_play_mtx); #if defined(CONFIG_AX_620E_MSP_ENABLED) || defined(CONFIG_AX_620Q_MSP_ENABLED) ax_play(play_config.card, play_config.device, play_config.volume, play_config.channel, play_config.rate, - play_config.bit, audio_data.c_str(), audio_data.length()); + play_config.bit, final_data.c_str(), final_data.length()); #else alsa_play(play_config.card, play_config.device, play_config.volume, play_config.channel, play_config.rate, play_config.bit, final_data.c_str(), final_data.length()); @@ -192,6 +213,8 @@ class llm_audio : public StackFlow { "cap_stop", std::bind(&llm_audio::cap_stop, this, std::placeholders::_1, std::placeholders::_2)); rpc_ctx_->register_rpc_action( "cap_stop_all", std::bind(&llm_audio::cap_stop_all, this, std::placeholders::_1, std::placeholders::_2)); + rpc_ctx_->register_rpc_action( + "queue_status", std::bind(&llm_audio::queue_status, this, std::placeholders::_1, std::placeholders::_2)); } int setup(const std::string &work_id, const std::string &object, const std::string &data) override { @@ -478,7 +501,13 @@ class llm_audio : public StackFlow { std::string enqueue_play(pzmq *_pzmq, const std::shared_ptr &rawdata) { audio_clear_flage_ = false; - event_queue_.enqueue(EVENT_QUEUE_PLAY, rawdata); + + auto item = std::make_shared(); + item->gen = play_gen_.load(std::memory_order_relaxed); + item->data = rawdata; + + play_pending_.fetch_add(1, std::memory_order_relaxed); + event_queue_.enqueue(EVENT_QUEUE_PLAY, item); return LLM_NONE; } @@ -514,7 +543,13 @@ class llm_audio : public StackFlow { std::string queue_play_stop(pzmq *_pzmq, const std::shared_ptr &rawdata) { - audio_clear_flage_ = true; + play_gen_.fetch_add(1, std::memory_order_relaxed); + audio_clear_flage_.store(true, std::memory_order_relaxed); +#if defined(CONFIG_AX_620E_MSP_ENABLED) || defined(CONFIG_AX_620Q_MSP_ENABLED) + ax_close_play(); +#else + alsa_close_play(); +#endif return LLM_NONE; } @@ -545,6 +580,23 @@ class llm_audio : public StackFlow { return LLM_NONE; } + std::string queue_status(pzmq *_pzmq, const std::shared_ptr &rawdata) + { + int pending = play_pending_.load(std::memory_order_relaxed); + bool running = play_running_.load(std::memory_order_relaxed); + +#if defined(CONFIG_AX_620E_MSP_ENABLED) || defined(CONFIG_AX_620Q_MSP_ENABLED) + int hw = ax_play_status(); +#else + int hw = alsa_play_status(); +#endif + + std::ostringstream os; + os << "{\"pending\":" << pending << ",\"running\":" << (running ? "true" : "false") << ",\"hw_status\":" << hw + << "}"; + return os.str(); + } + ~llm_audio() { _play_stop(); diff --git a/projects/llm_framework/main_kws/SConstruct b/projects/llm_framework/main_kws/SConstruct index 3ad5f92..0c46047 100644 --- a/projects/llm_framework/main_kws/SConstruct +++ b/projects/llm_framework/main_kws/SConstruct @@ -60,7 +60,7 @@ ignore['ignore'] = list(set(ignore['ignore'])) with open('../dist/fileignore', 'w') as f: json.dump(ignore, f, indent=4) -env['COMPONENTS'].append({'target':'llm_kws-1.10', +env['COMPONENTS'].append({'target':'llm_kws-1.11', 'SRCS':SRCS, 'INCLUDE':INCLUDE, 'PRIVATE_INCLUDE':PRIVATE_INCLUDE, diff --git a/projects/llm_framework/main_kws/src/main.cpp b/projects/llm_framework/main_kws/src/main.cpp index 4883478..49b4213 100644 --- a/projects/llm_framework/main_kws/src/main.cpp +++ b/projects/llm_framework/main_kws/src/main.cpp @@ -41,8 +41,8 @@ typedef std::function task_callback_ typedef struct mode_config_axera { int chunk_size = 32; - float threshold = 0.9f; - int min_continuous_frames = 5; + float threshold = 0.6f; + int min_continuous_frames = 2; int REFRACTORY_TIME_MS = 2000; int RESAMPLE_RATE = 16000; int FEAT_DIM = 80; @@ -83,6 +83,9 @@ class llm_task { long long frame_index_global_ = 0; int last_btn_204_state = -1; + std::atomic last_manual_trigger_ms_{0}; + static constexpr long long MANUAL_TRIGGER_INTERVAL_MS = 2000; + public: inline const std::string &model() const { @@ -107,6 +110,12 @@ class llm_task { friend class llm_kws; + static long long now_ms() + { + using namespace std::chrono; + return duration_cast(steady_clock::now().time_since_epoch()).count(); + } + bool parse_config(const nlohmann::json &config_body) { try { @@ -540,6 +549,15 @@ class llm_task { void trigger_wakeup() { + long long now = now_ms(); + long long last = last_manual_trigger_ms_.load(); + + if (now - last < MANUAL_TRIGGER_INTERVAL_MS) { + return; + } + + last_manual_trigger_ms_.store(now); + if (enwake_audio_ && (!wake_wav_file_.empty()) && play_awake_wav) { play_awake_wav(wake_wav_file_); } @@ -698,7 +716,15 @@ class llm_kws : public StackFlow { } } if (post != 0) { +#if !defined(CONFIG_AX_620E_MSP_ENABLED) && !defined(CONFIG_AX_620Q_MSP_ENABLED) + unit_call("audio", "cap_stop_all", "sys"); + std::this_thread::sleep_for(std::chrono::milliseconds(20)); +#endif unit_call("audio", "play_raw", std::string((char *)(wav_data.data() + post), size - post)); +#if !defined(CONFIG_AX_620E_MSP_ENABLED) && !defined(CONFIG_AX_620Q_MSP_ENABLED) + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + unit_call("audio", "cap", "None"); +#endif } } diff --git a/projects/llm_framework/main_melotts/SConstruct b/projects/llm_framework/main_melotts/SConstruct index e0dea06..e831768 100644 --- a/projects/llm_framework/main_melotts/SConstruct +++ b/projects/llm_framework/main_melotts/SConstruct @@ -32,7 +32,7 @@ REQUIREMENTS += ['onnxruntime'] STATIC_FILES += Glob('models/mode_*.json') -env['COMPONENTS'].append({'target':'llm_melotts-1.10', +env['COMPONENTS'].append({'target':'llm_melotts-1.11', 'SRCS':SRCS, 'INCLUDE':INCLUDE, 'PRIVATE_INCLUDE':PRIVATE_INCLUDE, diff --git a/projects/llm_framework/main_melotts/src/main.cpp b/projects/llm_framework/main_melotts/src/main.cpp index 4b6f351..6d0a2c0 100644 --- a/projects/llm_framework/main_melotts/src/main.cpp +++ b/projects/llm_framework/main_melotts/src/main.cpp @@ -88,7 +88,6 @@ class llm_task { task_callback_t out_callback_; bool enaudio_; int awake_delay_ = 1000; - bool cap_; std::string tts_string_stream_buff; bool parse_config(const nlohmann::json &config_body) @@ -257,48 +256,22 @@ class llm_task { { try { std::vector wav_pcm_data; -#if !defined(CONFIG_AX_620E_MSP_ENABLED) && !defined(CONFIG_AX_620Q_MSP_ENABLED) - std::string initial_status = unit_call("audio", "audio_status", "sys"); - if (!cap_ && initial_status.find("\"cap\":\"Running\"") != std::string::npos) { - unit_call("audio", "cap_stop_all", "sys"); - cap_ = true; - } -#endif if (msg_str.empty()) { if (out_callback_) { std::string output = wav_pcm_data.empty() ? std::string() : std::string((char *)wav_pcm_data.data(), wav_pcm_data.size() * sizeof(int16_t)); out_callback_(output, finish); -#if !defined(CONFIG_AX_620E_MSP_ENABLED) && !defined(CONFIG_AX_620Q_MSP_ENABLED) - int none_count = 0; - const int max_iterations = 100; - - for (int i = 0; i < max_iterations; ++i) { - std::string current_status = unit_call("audio", "audio_status", "sys"); - if (current_status.find("\"play\":\"None\"") != std::string::npos) { - none_count++; - } else { - none_count = 0; - } - if (none_count >= 5) { - break; - } - - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - } - - if (cap_) { - unit_call("audio", "cap", "sys"); - cap_ = false; - } -#endif } return false; } std::vector phones_bef, tones_bef; - lexicon_->convert(msg_str, phones_bef, tones_bef); + std::string clean = msg_str; + for (char &c : clean) { + if (c == '\r' || c == '\n' || c == '\t') c = ' '; + } + lexicon_->convert(clean, phones_bef, tones_bef); auto phones = intersperse(phones_bef, 0); auto tones = intersperse(tones_bef, 0); int phone_len = phones.size(); @@ -560,11 +533,7 @@ class llm_tts : public StackFlow { llm_channel->send(llm_task_obj->response_format_, base64_data, LLM_NO_ERROR); } if (llm_task_obj->response_format_.find("sys") != std::string::npos) { -#if defined(CONFIG_AX_620E_MSP_ENABLED) || defined(CONFIG_AX_620Q_MSP_ENABLED) unit_call("audio", "queue_play", data); -#else - unit_call("audio", "play_raw", data); -#endif } } @@ -634,9 +603,8 @@ class llm_tts : public StackFlow { error_body["message"] = "Model run failed."; llm_channel->send("None", "None", error_body, llm_channel->work_id_); } - } else { - llm_task_obj->TTS("", true); } + llm_task_obj->TTS("", true); } } @@ -655,10 +623,6 @@ class llm_tts : public StackFlow { if (llm_task_obj->response_format_.find("sys") != std::string::npos) { unit_call("audio", "queue_play_stop", data); } - std::this_thread::sleep_for(std::chrono::milliseconds(llm_task_obj->awake_delay_)); - if (llm_task_obj->response_format_.find("sys") != std::string::npos) { - unit_call("audio", "play_stop", data); - } llm_channel->subscriber_work_id( llm_task_obj->superior_id_, std::bind(&llm_tts::task_user_data, this, std::weak_ptr(llm_task_obj), diff --git a/projects/llm_framework/tools/llm_pack.py b/projects/llm_framework/tools/llm_pack.py index ddc825b..a4db415 100755 --- a/projects/llm_framework/tools/llm_pack.py +++ b/projects/llm_framework/tools/llm_pack.py @@ -374,12 +374,12 @@ def create_bin_deb(package_name, version, src_folder, revision = 'm5stack1', dep # Unit 'lib-llm':[create_lib_deb,'lib-llm', '1.9', src_folder, revision], 'llm-sys':[create_bin_deb,'llm-sys', '1.6', src_folder, revision, 'lib-llm (>= 1.8)'], - 'llm-audio':[create_bin_deb,'llm-audio', '1.8', src_folder, revision], - 'llm-kws':[create_bin_deb,'llm-kws', '1.10', src_folder, revision], - 'llm-asr':[create_bin_deb,'llm-asr', '1.8', src_folder, revision], + 'llm-audio':[create_bin_deb,'llm-audio', '1.9', src_folder, revision], + 'llm-kws':[create_bin_deb,'llm-kws', '1.11', src_folder, revision], + 'llm-asr':[create_bin_deb,'llm-asr', '1.9', src_folder, revision], 'llm-llm':[create_bin_deb,'llm-llm', '1.12', src_folder, revision], 'llm-tts':[create_bin_deb,'llm-tts', '1.6', src_folder, revision], - 'llm-melotts':[create_bin_deb,'llm-melotts', '1.10', src_folder, revision], + 'llm-melotts':[create_bin_deb,'llm-melotts', '1.11', src_folder, revision], 'llm-camera':[create_bin_deb,'llm-camera', '1.9', src_folder, revision, 'lib-llm'], 'llm-vlm':[create_bin_deb,'llm-vlm', '1.11', src_folder, revision], 'llm-yolo':[create_bin_deb,'llm-yolo', '1.9', src_folder, revision], diff --git a/projects/llm_framework/tools/test_tools/AI_Pyramid_Demo.py b/projects/llm_framework/tools/test_tools/AI_Pyramid_Demo.py new file mode 100644 index 0000000..68328a1 --- /dev/null +++ b/projects/llm_framework/tools/test_tools/AI_Pyramid_Demo.py @@ -0,0 +1,235 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD +# +# SPDX-License-Identifier: MIT +import argparse +import json +import socket +import uuid +from typing import Any, Dict, Optional + + +class TcpJsonClient: + def __init__(self, host="127.0.0.1", port=10001, timeout=60.0): + self.host = host + self.port = port + self.timeout = timeout + self.sock: Optional[socket.socket] = None + self._rx_buf = b"" + + def connect(self): + self.sock = socket.create_connection((self.host, self.port), timeout=self.timeout) + self.sock.settimeout(None) + + def close(self): + if self.sock: + try: + self.sock.close() + finally: + self.sock = None + + def send_json(self, payload: Dict[str, Any]): + assert self.sock is not None + data = (json.dumps(payload, ensure_ascii=False) + "\n").encode("utf-8") + self.sock.sendall(data) + + def recv_json(self) -> Dict[str, Any]: + assert self.sock is not None + while True: + if b"\n" in self._rx_buf: + line, self._rx_buf = self._rx_buf.split(b"\n", 1) + line = line.strip() + if not line: + continue + return json.loads(line.decode("utf-8", errors="replace")) + chunk = self.sock.recv(4096) + if not chunk: + raise ConnectionError("Server closed connection") + self._rx_buf += chunk + + def recv_json_stream(self): + while True: + yield self.recv_json() + + +def new_request_id() -> str: + return str(uuid.uuid4()) + + +def assert_ok(resp: Dict[str, Any]): + err = resp.get("error") or {} + code = err.get("code", 0) + msg = err.get("message", "") + if code != 0: + raise RuntimeError(f"Server error code={code}, message={msg}") + + +def make_setup_kws(kws_text="HI PYRAMID") -> Dict[str, Any]: + return { + "request_id": new_request_id(), + "work_id": "kws", + "action": "setup", + "object": "kws.setup", + "data": { + "model": "kws-ax650", + "response_format": "kws.bool", + "input": ["buttons_thread", "sys.pcm"], + "enoutput": True, + "kws": kws_text, + "enwake_audio": True, + }, + } + + +def make_setup_asr(kws_work_id: str) -> Dict[str, Any]: + return { + "request_id": new_request_id(), + "work_id": "asr", + "action": "setup", + "object": "asr.setup", + "data": { + "model": "sense-voice-small-10s-ax650", + "response_format": "asr.utf-8.stream", + "input": [kws_work_id, "sys.pcm"], + "enoutput": True, + }, + } + + +def make_setup_llm(kws_work_id: str, asr_work_id: str) -> Dict[str, Any]: + return { + "request_id": new_request_id(), + "work_id": "llm", + "action": "setup", + "object": "llm.setup", + "data": { + "model": "qwen2.5-0.5B-Int4-ax650", + "response_format": "llm.utf-8.stream", + "input": [kws_work_id, asr_work_id], + "enoutput": True, + "max_token_len": 256, + "prompt": "You are a knowledgeable assistant capable of answering various questions and providing information. Respond exclusively in English and do not use any other language.", + }, + } + + +def make_setup_melotts(kws_work_id: str, llm_work_id: str) -> Dict[str, Any]: + return { + "request_id": new_request_id(), + "work_id": "melotts", + "action": "setup", + "object": "melotts.setup", + "data": { + "model": "melotts-en-default-ax650", + "response_format": "sys.pcm", + "input": [kws_work_id, llm_work_id], + "enoutput": False, + }, + } + + +def make_exit(work_id: str) -> Dict[str, Any]: + return {"request_id": new_request_id(), "work_id": work_id, "action": "exit"} + + +def make_trigger(work_id: str) -> Dict[str, Any]: + return {"request_id": new_request_id(), "work_id": work_id, "action": "trigger"} + + +class App: + def __init__(self, host: str, port: int, kws_text: str): + self.client = TcpJsonClient(host, port) + self.kws_text = kws_text + self.workids: Dict[str, Optional[str]] = {"kws": None, "asr": None, "llm": None, "melotts": None} + self.created = [] + self._asr_text = "" + self._llm_text = "" + + def _add_created(self, wid: Optional[str]): + if wid and wid not in self.created: + self.created.append(wid) + + def setup_pipeline(self): + self.client.send_json(make_setup_kws(self.kws_text)) + resp = self.client.recv_json() + assert_ok(resp) + self.workids["kws"] = resp.get("work_id") + self._add_created(self.workids["kws"]) + + self.client.send_json(make_setup_asr(self.workids["kws"])) + resp = self.client.recv_json() + assert_ok(resp) + self.workids["asr"] = resp.get("work_id") + self._add_created(self.workids["asr"]) + + self.client.send_json(make_setup_llm(self.workids["kws"], self.workids["asr"])) + resp = self.client.recv_json() + assert_ok(resp) + self.workids["llm"] = resp.get("work_id") + self._add_created(self.workids["llm"]) + + self.client.send_json(make_setup_melotts(self.workids["kws"], self.workids["llm"])) + resp = self.client.recv_json() + assert_ok(resp) + self.workids["melotts"] = resp.get("work_id") + self._add_created(self.workids["melotts"]) + + def handle_message(self, msg: Dict[str, Any]): + assert_ok(msg) + obj = msg.get("object") + data = msg.get("data") + + if obj == "kws.bool" and data is True: + self._asr_text = "" + self._llm_text = "" + print("\n[kws.trigger]") + return + + if obj == "asr.utf-8.stream" and isinstance(data, dict): + delta = data.get("delta", "") + if delta: + self._asr_text += delta + print(delta, end="", flush=True) + if data.get("finish") is True: + print() + return + + if obj == "llm.utf-8.stream" and isinstance(data, dict): + delta = data.get("delta", "") + if delta: + self._llm_text += delta + print(delta, end="", flush=True) + if data.get("finish") is True: + print() + return + + def run(self): + self.client.connect() + try: + self.setup_pipeline() + for msg in self.client.recv_json_stream(): + self.handle_message(msg) + finally: + for wid in reversed(self.created): + try: + self.client.send_json(make_exit(wid)) + except Exception: + pass + self.client.close() + + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--host", default="127.0.0.1") + ap.add_argument("--port", type=int, default=10001) + ap.add_argument("--kws", default="HI PYRAMID") + args = ap.parse_args() + App(args.host, args.port, args.kws).run() + + +if __name__ == "__main__": + try: + main() + except KeyboardInterrupt: + pass \ No newline at end of file From f4e7b73b4c431f427c42d3473da0193eaf5df3ad Mon Sep 17 00:00:00 2001 From: LittleMouse Date: Fri, 23 Jan 2026 18:03:22 +0800 Subject: [PATCH 04/22] [fix] ASR output empty --- projects/llm_framework/main_asr/src/main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/projects/llm_framework/main_asr/src/main.cpp b/projects/llm_framework/main_asr/src/main.cpp index 6e1044b..c2265c0 100644 --- a/projects/llm_framework/main_asr/src/main.cpp +++ b/projects/llm_framework/main_asr/src/main.cpp @@ -543,7 +543,7 @@ class llm_task { bool is_endpoint = ncnn_recognizer_->IsEndpoint(ncnn_stream_.get()); if (is_endpoint) { ncnn_stream_->Finalize(); - if ((!lower_text.empty()) && out_callback_) { + if (out_callback_) { out_callback_(lower_text, true); } ncnn_stream_.reset(); @@ -638,7 +638,7 @@ class llm_task { } if (silence_ms_accum_ >= silence_timeout) { - if (!pending_text_.empty() && out_callback_) { + if (out_callback_) { out_callback_(pending_text_, true); } pending_text_.clear(); @@ -689,7 +689,7 @@ class llm_task { bool is_endpoint = onnx_online_recognizer_->IsEndpoint(online_stream.get()); if (is_endpoint) { - if ((!lower_text.empty()) && out_callback_) { + if (out_callback_) { out_callback_(lower_text, true); } online_stream.reset(); From f82b01e892b1223d89e613547944d5ac78922b9c Mon Sep 17 00:00:00 2001 From: LittleMouse Date: Fri, 23 Jan 2026 18:27:33 +0800 Subject: [PATCH 05/22] [update] llm-asr version --- projects/llm_framework/main_asr/SConstruct | 2 +- projects/llm_framework/main_asr/src/main.cpp | 5 ++++- projects/llm_framework/tools/llm_pack.py | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/projects/llm_framework/main_asr/SConstruct b/projects/llm_framework/main_asr/SConstruct index e498541..5428581 100644 --- a/projects/llm_framework/main_asr/SConstruct +++ b/projects/llm_framework/main_asr/SConstruct @@ -40,7 +40,7 @@ LDFLAGS += [ STATIC_FILES += Glob('mode_*.json') -env['COMPONENTS'].append({'target':'llm_asr-1.9', +env['COMPONENTS'].append({'target':'llm_asr-1.10', 'SRCS':SRCS, 'INCLUDE':INCLUDE, 'PRIVATE_INCLUDE':PRIVATE_INCLUDE, diff --git a/projects/llm_framework/main_asr/src/main.cpp b/projects/llm_framework/main_asr/src/main.cpp index c2265c0..ee762b9 100644 --- a/projects/llm_framework/main_asr/src/main.cpp +++ b/projects/llm_framework/main_asr/src/main.cpp @@ -772,7 +772,10 @@ class llm_asr : public StackFlow { const std::string *next_data = &data; if (finish) { if (!llm_task_obj->is_using_itn()) { - tmp_msg1 = data + "."; + if (tmp_msg1.empty()) + tmp_msg1 = data; + else + tmp_msg1 = data + "."; next_data = &tmp_msg1; } } diff --git a/projects/llm_framework/tools/llm_pack.py b/projects/llm_framework/tools/llm_pack.py index a4db415..e0c6a8e 100755 --- a/projects/llm_framework/tools/llm_pack.py +++ b/projects/llm_framework/tools/llm_pack.py @@ -376,7 +376,7 @@ def create_bin_deb(package_name, version, src_folder, revision = 'm5stack1', dep 'llm-sys':[create_bin_deb,'llm-sys', '1.6', src_folder, revision, 'lib-llm (>= 1.8)'], 'llm-audio':[create_bin_deb,'llm-audio', '1.9', src_folder, revision], 'llm-kws':[create_bin_deb,'llm-kws', '1.11', src_folder, revision], - 'llm-asr':[create_bin_deb,'llm-asr', '1.9', src_folder, revision], + 'llm-asr':[create_bin_deb,'llm-asr', '1.10', src_folder, revision], 'llm-llm':[create_bin_deb,'llm-llm', '1.12', src_folder, revision], 'llm-tts':[create_bin_deb,'llm-tts', '1.6', src_folder, revision], 'llm-melotts':[create_bin_deb,'llm-melotts', '1.11', src_folder, revision], From 1fac79e5bb796b91fc42d3fa5dd8d29963f6fc85 Mon Sep 17 00:00:00 2001 From: LittleMouse Date: Mon, 26 Jan 2026 18:41:30 +0800 Subject: [PATCH 06/22] [update] update llm-tts supported kokoro tts --- projects/llm_framework/SConstruct | 2 +- projects/llm_framework/main_tts/SConstruct | 10 +- .../main_tts/mode_kokoro-ax650.json | 33 ++ projects/llm_framework/main_tts/src/Kokoro.h | 68 ++++ projects/llm_framework/main_tts/src/main.cpp | 317 ++++++++++++++++-- projects/llm_framework/tools/llm_pack.py | 3 +- 6 files changed, 391 insertions(+), 42 deletions(-) create mode 100644 projects/llm_framework/main_tts/mode_kokoro-ax650.json create mode 100644 projects/llm_framework/main_tts/src/Kokoro.h diff --git a/projects/llm_framework/SConstruct b/projects/llm_framework/SConstruct index 731210d..98fe9c0 100644 --- a/projects/llm_framework/SConstruct +++ b/projects/llm_framework/SConstruct @@ -5,7 +5,7 @@ import shutil os.environ['SDK_PATH'] = os.path.normpath(str(Path(os.getcwd())/'..'/'..'/'SDK')) os.environ['EXT_COMPONENTS_PATH'] = os.path.normpath(str(Path(os.getcwd())/'..'/'..'/'ext_components')) -version = 'v0.1.3' +version = 'v0.1.5' static_lib = 'static_lib' update = False diff --git a/projects/llm_framework/main_tts/SConstruct b/projects/llm_framework/main_tts/SConstruct index a38e56f..4ec7405 100644 --- a/projects/llm_framework/main_tts/SConstruct +++ b/projects/llm_framework/main_tts/SConstruct @@ -8,7 +8,7 @@ with open(env['PROJECT_TOOL_S']) as f: SRCS = Glob('src/*.c*') INCLUDE = [ADir('include'), ADir('.')] PRIVATE_INCLUDE = [] -REQUIREMENTS = ['pthread', 'utilities', 'gomp', 'eventpp', 'StackFlow'] +REQUIREMENTS = ['pthread', 'utilities', 'ax_msp', 'gomp', 'eventpp', 'StackFlow'] STATIC_LIB = [] DYNAMIC_LIB = [] DEFINITIONS = [] @@ -20,14 +20,18 @@ STATIC_FILES = [] DEFINITIONS += ['-O3', '-fopenmp', '-std=c++17'] LDFLAGS+=['-Wl,-rpath=/opt/m5stack/lib', '-Wl,-rpath=/usr/local/m5stack/lib', '-Wl,-rpath=/usr/local/m5stack/lib/gcc-10.3', '-Wl,-rpath=/opt/lib', '-Wl,-rpath=/opt/usr/lib', '-Wl,-rpath=./'] LINK_SEARCH_PATH += [ADir('../static_lib')] -REQUIREMENTS += ['tts'] +REQUIREMENTS += ['ax_engine', 'ax_interpreter', 'ax_sys'] +REQUIREMENTS += ['samplerate'] +REQUIREMENTS += ['tts', 'kokoro', 'libkokoro_backend'] INCLUDE += [ADir('src/runner/eigen-3.4.0'), ADir('src/runner/src/tn/header'), ADir('src/runner/include'), ADir('src/runner/src/header')] +REQUIREMENTS += ['onnxruntime'] + STATIC_FILES += Glob('mode_*.json') -env['COMPONENTS'].append({'target':'llm_tts-1.6', +env['COMPONENTS'].append({'target':'llm_tts-1.7', 'SRCS':SRCS, 'INCLUDE':INCLUDE, 'PRIVATE_INCLUDE':PRIVATE_INCLUDE, diff --git a/projects/llm_framework/main_tts/mode_kokoro-ax650.json b/projects/llm_framework/main_tts/mode_kokoro-ax650.json new file mode 100644 index 0000000..034ea69 --- /dev/null +++ b/projects/llm_framework/main_tts/mode_kokoro-ax650.json @@ -0,0 +1,33 @@ +{ + "mode": "kokoro-ax650", + "type": "tts", + "homepage": "https://huggingface.co/AXERA-TECH/kokoro.axera", + "capabilities": [ + "tts", + "Chinese", + "English" + ], + "input_type": [ + "tts.utf-8" + ], + "output_type": [ + "tts.wav", + "sys.play.0_1" + ], + "mode_param": { + "axmodel_dir": "./models", + "lang": "z", + "voice_path": "./voices", + "voice_name": "zf_xiaoxiao", + "vocab_path": "./vocab.txt", + "espeak_data_path": "./espeak-ng-data", + "dict_dir": "./dict", + "spacker_speed": 1.0, + "max_len": 96, + "mode_rate": 24000, + "audio_rate": 48000, + "speed": 1.0, + "pause": 0.0, + "fade_out": 0.0 + } +} \ No newline at end of file diff --git a/projects/llm_framework/main_tts/src/Kokoro.h b/projects/llm_framework/main_tts/src/Kokoro.h new file mode 100644 index 0000000..4d05a8b --- /dev/null +++ b/projects/llm_framework/main_tts/src/Kokoro.h @@ -0,0 +1,68 @@ +/* + * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD + * + * SPDX-License-Identifier: MIT + */ +#pragma once + +#include +#include +#include + +namespace kokoro { +struct SentenceInfo { + std::string sentence; + std::vector input_ids; + std::string phonemes; + int content_len = 0; + bool is_long = false; + std::vector sub_results; + + SentenceInfo() = default; + SentenceInfo(const std::string &s, const std::vector &ids, const std::string &ph, int len) + : sentence(s), input_ids(ids), phonemes(ph), content_len(len), is_long(false) + { + } + + SentenceInfo(const std::string &s, const std::vector &subs) + : sentence(s), is_long(true), sub_results(subs) + { + } +}; + +struct MergedGroup { + bool is_long_split = false; + std::vector input_ids; + std::string phonemes; + std::vector sub_results; + + MergedGroup() = default; + MergedGroup(const std::vector &ids, const std::string &ph) : is_long_split(false), input_ids(ids), phonemes(ph) + { + } + + MergedGroup(const std::vector &subs) : is_long_split(true), sub_results(subs) + { + } +}; + +class Kokoro { +public: + Kokoro(); + ~Kokoro(); + Kokoro(const Kokoro &) = delete; + Kokoro &operator=(const Kokoro &) = delete; + + bool init(const std::string &model_path, int max_seq_len = 96, const std::string &lang_code = "z", + const std::string &voices_path = "./voices", const std::string &voice_name = "af_heart", + const std::string &vocab_path = "dict/vocab.txt", + const std::string &espeak_data_path = "./espeak-ng-data", const std::string &dict_dir = "./dict"); + + bool tts(const std::string &text, const std::string &voice_name, float speed, int sample_rate, float fade_out, + float pause_duration, std::vector &generated_audio); + +private: + struct Impl; + std::unique_ptr impl_; +}; +} // namespace kokoro \ No newline at end of file diff --git a/projects/llm_framework/main_tts/src/main.cpp b/projects/llm_framework/main_tts/src/main.cpp index 88ef4a8..42a5ad4 100644 --- a/projects/llm_framework/main_tts/src/main.cpp +++ b/projects/llm_framework/main_tts/src/main.cpp @@ -6,7 +6,9 @@ #include "StackFlow.h" #include "utils.h" #include "SynthesizerTrn.h" - +#include "Kokoro.h" +#include +#include #include #include #include @@ -14,6 +16,7 @@ #include #include #include +#include #include "../../../../SDK/components/utilities/include/sample_log.h" using namespace StackFlows; @@ -21,13 +24,31 @@ using namespace StackFlows; int main_exit_flage = 0; static void __sigint(int iSigNo) { - SLOGW("llm_sys will be exit!"); + SLOGW("llm_tts will be exit!"); main_exit_flage = 1; } static std::string base_model_path_; static std::string base_model_config_path_; +typedef struct { + std::string axmodel_dir; + std::string lang; + std::string voice_path; + std::string voice_name; + std::string vocab_path; + std::string espeak_data_path; + std::string dict_dir; + float spacker_speed = 1.0; + int max_len = 96; + int mode_rate = 44100; + int audio_rate = 16000; + float speed = 1.0f; + float pause = 0.0f; + float fade_out = 0.0f; + +} kokoro_config; + struct SynthesizerTrn_config { int spacker_role = 0; float spacker_speed = 1.0; @@ -42,29 +63,35 @@ typedef std::function task_callback_ else if (obj.contains(#key)) \ mode_config_.key = obj[#key]; +#define CONFIG_KOKORO_AUTO_SET(obj, key) \ + if (config_body.contains(#key)) \ + mode_kokoro_config_.key = config_body[#key]; \ + else if (obj.contains(#key)) \ + mode_kokoro_config_.key = obj[#key]; + class llm_task { private: float *dataW = NULL; int modelSize; public: + kokoro_config mode_kokoro_config_; std::unique_ptr synthesizer_; + std::unique_ptr kokoro_; + std::string model_type_; + std::string model_; SynthesizerTrn_config mode_config_; + std::string response_format_; std::vector inputs_; bool enoutput_; bool enstream_; std::atomic_bool superior_flage_; std::string superior_id_; - std::string model_; - std::string response_format_; + static int ax_init_flage_; task_callback_t out_callback_; + bool enaudio_; int awake_delay_ = 1000; - void set_output(task_callback_t out_callback) - { - out_callback_ = out_callback; - } - bool parse_config(const nlohmann::json &config_body) { try { @@ -72,6 +99,13 @@ class llm_task { response_format_ = config_body.at("response_format"); enoutput_ = config_body.at("enoutput"); + if (model_.rfind("single-speaker", 0) == 0) { + model_type_ = "summer_tts"; + } else { + model_type_ = "kokoro_tts"; + } + + if (config_body.contains("enaudio")) enaudio_ = config_body.at("enaudio"); if (config_body.contains("input")) { if (config_body["input"].is_string()) { inputs_.push_back(config_body["input"].get()); @@ -85,11 +119,11 @@ class llm_task { SLOGE("setup config_body error"); return true; } - enstream_ = (response_format_.find("stream") != std::string::npos); + enstream_ = response_format_.find("stream") == std::string::npos ? false : true; return false; } - int load_model(const nlohmann::json &config_body) + int load_summer_tts_model(const nlohmann::json &config_body) { if (parse_config(config_body)) { return -1; @@ -137,24 +171,203 @@ class llm_task { SLOGI("Available speakers in the model are %d", spkNum); } catch (...) { SLOGE("config false"); - return -3; + return -6; } return 0; } - bool TTS(const std::string &msg) + int load_kokoro_tts_model(const nlohmann::json &config_body) + { + nlohmann::json file_body; + std::list config_file_paths = + get_config_file_paths(base_model_path_, base_model_config_path_, model_); + try { + for (auto file_name : config_file_paths) { + std::ifstream config_file(file_name); + if (!config_file.is_open()) { + SLOGW("config file :%s miss", file_name.c_str()); + continue; + } + SLOGI("config file :%s read", file_name.c_str()); + config_file >> file_body; + config_file.close(); + break; + } + if (file_body.empty()) { + SLOGE("all config file miss"); + return -2; + } + std::string base_model = base_model_path_ + model_ + "/"; + SLOGI("base_model %s", base_model.c_str()); + CONFIG_KOKORO_AUTO_SET(file_body["mode_param"], axmodel_dir); + CONFIG_KOKORO_AUTO_SET(file_body["mode_param"], lang); + CONFIG_KOKORO_AUTO_SET(file_body["mode_param"], voice_path); + CONFIG_KOKORO_AUTO_SET(file_body["mode_param"], voice_name); + CONFIG_KOKORO_AUTO_SET(file_body["mode_param"], vocab_path); + CONFIG_KOKORO_AUTO_SET(file_body["mode_param"], espeak_data_path); + CONFIG_KOKORO_AUTO_SET(file_body["mode_param"], dict_dir); + CONFIG_KOKORO_AUTO_SET(file_body["mode_param"], spacker_speed); + CONFIG_KOKORO_AUTO_SET(file_body["mode_param"], max_len); + CONFIG_KOKORO_AUTO_SET(file_body["mode_param"], mode_rate); + CONFIG_KOKORO_AUTO_SET(file_body["mode_param"], audio_rate); + CONFIG_KOKORO_AUTO_SET(file_body["mode_param"], speed); + CONFIG_KOKORO_AUTO_SET(file_body["mode_param"], pause); + CONFIG_KOKORO_AUTO_SET(file_body["mode_param"], fade_out); + mode_kokoro_config_.axmodel_dir = base_model + mode_kokoro_config_.axmodel_dir; + mode_kokoro_config_.voice_path = base_model + mode_kokoro_config_.voice_path; + mode_kokoro_config_.vocab_path = base_model + mode_kokoro_config_.vocab_path; + mode_kokoro_config_.espeak_data_path = base_model + mode_kokoro_config_.espeak_data_path; + mode_kokoro_config_.dict_dir = base_model + mode_kokoro_config_.dict_dir; + kokoro_ = std::make_unique(); + if (!kokoro_->init(mode_kokoro_config_.axmodel_dir, mode_kokoro_config_.max_len, mode_kokoro_config_.lang, + mode_kokoro_config_.voice_path, mode_kokoro_config_.voice_name, + mode_kokoro_config_.vocab_path, mode_kokoro_config_.espeak_data_path, + mode_kokoro_config_.dict_dir)) { + SLOGE("encoder init failed!"); + return -4; + } + } catch (...) { + SLOGE("config false"); + return -6; + } + return 0; + } + + int load_model(const nlohmann::json &config_body) + { + if (parse_config(config_body)) { + return -1; + } + if (model_type_ == "summer_tts") { + SLOGI("load summer tts model"); + return load_summer_tts_model(config_body); + } else { + SLOGI("load kokoro tts model"); + return load_kokoro_tts_model(config_body); + } + } + + void set_output(task_callback_t out_callback) + { + out_callback_ = out_callback; + } + + void resample_audio(const float *input_buffer, int input_length, float *output_buffer, int *output_length, + double src_ratio) + { + int error = 0; + SRC_STATE *src_state = src_new(SRC_SINC_FASTEST, 1, &error); + if (!src_state) { + SLOGE("src_new failed: %s", src_strerror(error)); + *output_length = 0; + return; + } + + SRC_DATA src_data; + memset(&src_data, 0, sizeof(src_data)); + src_data.data_in = input_buffer; + src_data.input_frames = input_length; + src_data.data_out = output_buffer; + src_data.output_frames = static_cast(input_length * src_ratio) + 1; + src_data.src_ratio = src_ratio; + src_data.end_of_input = 1; + + error = src_process(src_state, &src_data); + if (error) { + SLOGE("src_process failed: %s", src_strerror(error)); + *output_length = 0; + src_delete(src_state); + return; + } + + *output_length = src_data.output_frames_gen; + src_delete(src_state); + } + + bool TTS(const std::string &msg, bool finish) { SLOGI("TTS msg:%s", msg.c_str()); - int32_t dataLen; - int16_t *rawData = synthesizer_->infer(msg, mode_config_.spacker_role, mode_config_.spacker_speed, dataLen); - if (!rawData) { - SLOGW("tts infer false!"); - return true; + if (msg.empty()) { + if (out_callback_) { + out_callback_(std::string(), finish); + } + return false; } - std::vector wavData(rawData, rawData + dataLen); - out_callback_(std::string((char *)rawData, dataLen * sizeof(int16_t)), true); - free(rawData); - return false; + + if (model_type_ == "summer_tts") { + int32_t dataLen = 0; + int16_t *rawData = synthesizer_->infer(msg, mode_config_.spacker_role, mode_config_.spacker_speed, dataLen); + + if (!rawData || dataLen <= 0) { + SLOGW("summer tts infer failed!"); + return true; + } + + if (out_callback_) { + out_callback_(std::string(reinterpret_cast(rawData), dataLen * sizeof(int16_t)), finish); + } + + free(rawData); + return false; + + } else if (model_type_ == "kokoro_tts") { + std::vector audio; + int src_rate = mode_kokoro_config_.mode_rate; + int dst_rate = mode_kokoro_config_.audio_rate; + + if (!kokoro_->tts(msg, mode_kokoro_config_.voice_name, mode_kokoro_config_.speed, src_rate, + mode_kokoro_config_.fade_out, mode_kokoro_config_.pause, audio)) { + SLOGE("kokoro tts run failed!"); + return true; + } + + if (audio.empty()) { + if (out_callback_) { + out_callback_(std::string(), finish); + } + SLOGE("kokoro tts audio empty!"); + return false; + } + + std::vector resampled_pcm; + const float *pcm_ptr = audio.data(); + int pcm_len = static_cast(audio.size()); + + if (src_rate != dst_rate) { + double ratio = static_cast(dst_rate) / static_cast(src_rate); + + int max_dst_len = static_cast(pcm_len * ratio) + 1; + resampled_pcm.resize(max_dst_len); + + int out_len = 0; + resample_audio(pcm_ptr, pcm_len, resampled_pcm.data(), &out_len, ratio); + resampled_pcm.resize(out_len); + pcm_ptr = resampled_pcm.data(); + pcm_len = out_len; + } + + std::vector wav_pcm_data; + wav_pcm_data.reserve(pcm_len); + for (int i = 0; i < pcm_len; ++i) { + float v = pcm_ptr[i]; + + if (std::abs(v) > 0.95f) { + v = v > 0 ? 0.95f : -0.95f; + } + + wav_pcm_data.push_back(static_cast(v * INT16_MAX)); + } + + if (out_callback_) { + out_callback_( + std::string(reinterpret_cast(wav_pcm_data.data()), wav_pcm_data.size() * sizeof(int16_t)), + finish); + } + + return false; + } + + return true; } bool delete_model() @@ -163,8 +376,38 @@ class llm_task { return true; } + void _ax_init() + { + if (!ax_init_flage_) { + int ret = AX_SYS_Init(); + if (0 != ret) { + fprintf(stderr, "AX_SYS_Init failed! ret = 0x%x\n", ret); + } + AX_ENGINE_NPU_ATTR_T npu_attr; + memset(&npu_attr, 0, sizeof(npu_attr)); + ret = AX_ENGINE_Init(&npu_attr); + if (0 != ret) { + fprintf(stderr, "Init ax-engine failed{0x%8x}.\n", ret); + } + } + ax_init_flage_++; + } + + void _ax_deinit() + { + if (ax_init_flage_ > 0) { + --ax_init_flage_; + if (!ax_init_flage_) { + AX_ENGINE_Deinit(); + AX_SYS_Deinit(); + } + } + } + llm_task(const std::string &workid) { + enaudio_ = true; + _ax_init(); } void start() @@ -178,9 +421,11 @@ class llm_task { ~llm_task() { stop(); + if (kokoro_) kokoro_.reset(); + _ax_deinit(); } }; - +int llm_task::ax_init_flage_ = 0; #undef CONFIG_AUTO_SET class llm_tts : public StackFlow { @@ -203,15 +448,17 @@ class llm_tts : public StackFlow { return; } std::string base64_data; - int len = encode_base64(data, base64_data); + if (!data.empty()) { + int len = encode_base64(data, base64_data); + } if (llm_channel->enstream_) { static int count = 0; nlohmann::json data_body; data_body["index"] = count++; - if (!finish) + if (!data.empty()) data_body["delta"] = base64_data; else - data_body["delta"] = std::string(""); + data_body["delta"] = ""; data_body["finish"] = finish; if (finish) count = 0; llm_channel->send(llm_task_obj->response_format_, data_body, LLM_NO_ERROR); @@ -265,10 +512,10 @@ class llm_tts : public StackFlow { int ret; std::string tmp_msg1; if (enstream) { - std::string finish = sample_json_str_get((*next_data), "finish"); - tmp_msg1 = sample_json_str_get((*next_data), "delta"); - finish_flage = (finish == "true") ? true : false; - next_data = &tmp_msg1; + std::string finish_str = sample_json_str_get((*next_data), "finish"); + finish_flage = (finish_str.find("true") != std::string::npos); + tmp_msg1 = sample_json_str_get((*next_data), "delta"); + next_data = &tmp_msg1; } std::string tmp_msg2; if (enbase64) { @@ -284,7 +531,7 @@ class llm_tts : public StackFlow { if (cutf8 == "," || cutf8 == "、" || cutf8 == "," || cutf8 == "。" || cutf8 == "." || cutf8 == "!" || cutf8 == "!" || cutf8 == "?" || cutf8 == "?" || cutf8 == ";" || cutf8 == ";") { faster_stream_buff += cutf8; - ret = llm_task_obj->TTS(faster_stream_buff); + ret = llm_task_obj->TTS(faster_stream_buff, false); faster_stream_buff.clear(); if (ret) { error_body["code"] = -11; @@ -298,7 +545,7 @@ class llm_tts : public StackFlow { if (finish_flage) { if (!faster_stream_buff.empty()) { faster_stream_buff.push_back('.'); - ret = llm_task_obj->TTS(faster_stream_buff); + ret = llm_task_obj->TTS(faster_stream_buff, true); faster_stream_buff.clear(); if (ret) { error_body["code"] = -11; @@ -306,6 +553,7 @@ class llm_tts : public StackFlow { llm_channel->send("None", "None", error_body, llm_channel->work_id_); } } + llm_task_obj->TTS("", true); } } @@ -323,10 +571,6 @@ class llm_tts : public StackFlow { if (llm_task_obj->response_format_.find("sys") != std::string::npos) { unit_call("audio", "queue_play_stop", data); } - std::this_thread::sleep_for(std::chrono::milliseconds(llm_task_obj->awake_delay_)); - if (llm_task_obj->response_format_.find("sys") != std::string::npos) { - unit_call("audio", "play_stop", data); - } llm_channel->subscriber_work_id( llm_task_obj->superior_id_, std::bind(&llm_tts::task_user_data, this, std::weak_ptr(llm_task_obj), @@ -458,7 +702,6 @@ class llm_tts : public StackFlow { for (auto it = llm_task_obj->inputs_.begin(); it != llm_task_obj->inputs_.end();) { if (*it == data) { it = llm_task_obj->inputs_.erase(it); - break; } else { ++it; } diff --git a/projects/llm_framework/tools/llm_pack.py b/projects/llm_framework/tools/llm_pack.py index e0c6a8e..9ac017e 100755 --- a/projects/llm_framework/tools/llm_pack.py +++ b/projects/llm_framework/tools/llm_pack.py @@ -378,7 +378,7 @@ def create_bin_deb(package_name, version, src_folder, revision = 'm5stack1', dep 'llm-kws':[create_bin_deb,'llm-kws', '1.11', src_folder, revision], 'llm-asr':[create_bin_deb,'llm-asr', '1.10', src_folder, revision], 'llm-llm':[create_bin_deb,'llm-llm', '1.12', src_folder, revision], - 'llm-tts':[create_bin_deb,'llm-tts', '1.6', src_folder, revision], + 'llm-tts':[create_bin_deb,'llm-tts', '1.7', src_folder, revision], 'llm-melotts':[create_bin_deb,'llm-melotts', '1.11', src_folder, revision], 'llm-camera':[create_bin_deb,'llm-camera', '1.9', src_folder, revision, 'lib-llm'], 'llm-vlm':[create_bin_deb,'llm-vlm', '1.11', src_folder, revision], @@ -412,6 +412,7 @@ def create_bin_deb(package_name, version, src_folder, revision = 'm5stack1', dep # TTS model 'llm-model-single-speaker-english-fast':[create_data_deb,'llm-model-single-speaker-english-fast', '0.3', src_folder, revision], 'llm-model-single-speaker-fast':[create_data_deb,'llm-model-single-speaker-fast', '0.3', src_folder, revision], + 'llm-model-kokoro-ax650':[create_data_deb,'llm-model-kokoro-ax650', '0.4', src_folder, revision], # VAD model 'llm-model-silero-vad':[create_data_deb,'llm-model-silero-vad', '0.4', src_folder, revision], # MeloTTS model From 865ca0e861a27a198f7c3bac6c2960c7c79e598f Mon Sep 17 00:00:00 2001 From: LittleMouse Date: Fri, 30 Jan 2026 12:20:56 +0800 Subject: [PATCH 07/22] [update] update llm-asr add new model --- projects/llm_framework/main_asr/src/main.cpp | 83 ++++++-- .../mode_qwen3-vl-2B-Int4-P1536-ax650.json | 54 +++++ .../tokenizer_qwen3-vl-2B-Int4-P1536-ax650.py | 195 ++++++++++++++++++ projects/llm_framework/tools/llm_pack.py | 1 + 4 files changed, 314 insertions(+), 19 deletions(-) create mode 100644 projects/llm_framework/main_vlm/models/mode_qwen3-vl-2B-Int4-P1536-ax650.json create mode 100644 projects/llm_framework/main_vlm/scripts/tokenizer_qwen3-vl-2B-Int4-P1536-ax650.py diff --git a/projects/llm_framework/main_asr/src/main.cpp b/projects/llm_framework/main_asr/src/main.cpp index ee762b9..e477759 100644 --- a/projects/llm_framework/main_asr/src/main.cpp +++ b/projects/llm_framework/main_asr/src/main.cpp @@ -555,6 +555,59 @@ class llm_task { void sys_pcm_on_data_onnx(const std::string &raw) { + if (delay_audio_frame_ == 0) { + if (raw.size() < sizeof(int16_t)) return; + + const int16_t *pcm16 = reinterpret_cast(raw.data()); + size_t n16 = raw.size() / sizeof(int16_t); + + std::vector samples; + samples.reserve(n16); + for (size_t i = 0; i < n16; ++i) { + samples.push_back(static_cast(pcm16[i]) / 32768.0f); + } + + int32_t window_size = vad_config_.silero_vad.window_size; + size_t i = 0; + + while (i < samples.size()) { + if (i + window_size <= samples.size()) { + vad_->AcceptWaveform(samples.data() + i, window_size); + } else { + vad_->Flush(); + } + i += window_size; + + while (!vad_->Empty()) { + const auto &segment = vad_->Front(); + float duration = segment.samples.size() / static_cast(kSampleRate); + if (duration < 0.1f) { + vad_->Pop(); + continue; + } + + auto s = onnx_recognizer_->CreateStream(); + s->AcceptWaveform(kSampleRate, segment.samples.data(), segment.samples.size()); + onnx_recognizer_->DecodeStream(s.get()); + const auto &result = s->GetResult(); + + if (!result.text.empty()) { + if (out_callback_) out_callback_(result.text, false); + + if (!pending_text_.empty()) pending_text_ += " "; + pending_text_ += result.text; + } + + vad_->Pop(); + } + } + + if (out_callback_) out_callback_(pending_text_, true); + pending_text_.clear(); + + return; + } + if (raw.size() >= sizeof(int16_t)) { const int16_t *pcm16 = reinterpret_cast(raw.data()); size_t n16 = raw.size() / sizeof(int16_t); @@ -578,7 +631,6 @@ class llm_task { while (buffer_read_i16(pcmdata, &audio_val, 1)) { floatSamples.push_back(static_cast(audio_val) / 32768.0f); } - buffer_resize(pcmdata, 0); count = 0; @@ -600,7 +652,6 @@ class llm_task { for (int16_t s : pre_roll_pcm_) { pre.push_back(static_cast(s) / 32768.0f); } - std::vector merged; merged.reserve(pre.size() + segment.samples.size()); merged.insert(merged.end(), pre.begin(), pre.end()); @@ -619,7 +670,6 @@ class llm_task { if (!result.text.empty() && out_callback_) { out_callback_(result.text, false); } - if (!result.text.empty()) { if (!pending_text_.empty()) pending_text_ += " "; pending_text_ += result.text; @@ -629,25 +679,20 @@ class llm_task { offline_stream_.reset(); } - { - float chunk_ms = (delay_audio_frame_ + 1) * 10.0f; - if (detected) { - silence_ms_accum_ = 0.0f; - } else { - silence_ms_accum_ += chunk_ms; - } + float chunk_ms = (delay_audio_frame_ + 1) * 10.0f; + if (detected) + silence_ms_accum_ = 0.0f; + else + silence_ms_accum_ += chunk_ms; - if (silence_ms_accum_ >= silence_timeout) { - if (out_callback_) { - out_callback_(pending_text_, true); - } - pending_text_.clear(); + if (silence_ms_accum_ >= silence_timeout) { + if (out_callback_) out_callback_(pending_text_, true); + pending_text_.clear(); - if (ensleep_) { - if (pause) pause(); - } - silence_ms_accum_ = 0.0f; + if (ensleep_) { + if (pause) pause(); } + silence_ms_accum_ = 0.0f; } } diff --git a/projects/llm_framework/main_vlm/models/mode_qwen3-vl-2B-Int4-P1536-ax650.json b/projects/llm_framework/main_vlm/models/mode_qwen3-vl-2B-Int4-P1536-ax650.json new file mode 100644 index 0000000..02ffa02 --- /dev/null +++ b/projects/llm_framework/main_vlm/models/mode_qwen3-vl-2B-Int4-P1536-ax650.json @@ -0,0 +1,54 @@ +{ + "mode": "qwen3-vl-2B-Int4-P1536-ax650", + "type": "vlm", + "homepage": "https://huggingface.co/AXERA-TECH/Qwen3-VL-2B-Instruct-GPTQ-Int4-P1536-CTX2047", + "capabilities": [ + "text_generation", + "chat" + ], + "input_type": [ + "vlm.chat_completion", + "vlm.chat_completion.stream" + ], + "output_type": [ + "vlm.utf-8", + "vlm.utf-8.stream" + ], + "mode_param": { + "tokenizer_type": 2, + "url_tokenizer_model": "http://localhost:8080", + "filename_tokens_embed": "model.embed_tokens.weight.bfloat16.bin", + "filename_post_axmodel": "qwen3_vl_text_post.axmodel", + "template_filename_axmodel": "qwen3_vl_text_p128_l%d_together.axmodel", + "filename_image_encoder_axmodel": "Qwen3-VL-2B-Instruct_vision.axmodel", + "enable_temperature": true, + "temperature": 0.7, + "enable_top_p_sampling": false, + "top_p": 0.9, + "enable_top_k_sampling": true, + "top_k": 40, + "enable_repetition_penalty": false, + "repetition_penalty": 1.1, + "penalty_window": 50, + "axmodel_num": 28, + "tokens_embed_num": 151936, + "tokens_embed_size": 2048, + "b_use_mmap_load_embed": true, + "b_video": true, + "vision_config.temporal_patch_size": 2, + "vision_config.tokens_per_second": 2, + "vision_config.spatial_merge_size": 2, + "vision_config.patch_size": 16, + "vision_config.height": 384, + "vision_config.width": 384, + "vision_config.fps": 1, + "image_token_id": 151655, + "video_token_id": 151656, + "vision_start_token_id": 151652, + "precompute_len": 0, + "cmm_size": 3582336, + "ext_scripts": [ + "tokenizer_qwen3-vl-2B-Int4-P1536-ax650.py" + ] + } +} \ No newline at end of file diff --git a/projects/llm_framework/main_vlm/scripts/tokenizer_qwen3-vl-2B-Int4-P1536-ax650.py b/projects/llm_framework/main_vlm/scripts/tokenizer_qwen3-vl-2B-Int4-P1536-ax650.py new file mode 100644 index 0000000..ed6f1aa --- /dev/null +++ b/projects/llm_framework/main_vlm/scripts/tokenizer_qwen3-vl-2B-Int4-P1536-ax650.py @@ -0,0 +1,195 @@ +from transformers import AutoTokenizer, PreTrainedTokenizerFast +from transformers.tokenization_utils_base import AddedToken +from http.server import HTTPServer, BaseHTTPRequestHandler +import json +import argparse + + +class Tokenizer_Http: + def __init__(self, model_id, system_content="You are a helpful assistant."): + self.tokenizer = AutoTokenizer.from_pretrained( + model_id, + trust_remote_code=True, + use_fast=False + ) + self.token_ids_cache = [] + self.system_content = system_content + + def encode(self, content): + text = [ + f'<|im_start|>system\n{self.system_content}<|im_end|>\n' + f'<|im_start|>user\n{content}<|im_end|>\n' + f'<|im_start|>assistant\n' + ] + input_ids = self.tokenizer(text) + return input_ids["input_ids"][0] + + def encode_vpm_image(self, content="Describe this image.", num_img=1, img_token_num=256): + imgs_token = ( + '<|vision_start|>' + + '<|image_pad|>' * img_token_num + + '<|vision_end|>' + ) + imgs_token *= num_img + text = ( + f'<|im_start|>system\n{self.system_content}<|im_end|>\n' + f'<|im_start|>user\n{imgs_token}{content}<|im_end|>\n' + f'<|im_start|>assistant\n' + ) + + output_kwargs = { + 'text_kwargs': {'padding': True, 'return_tensors': 'pt'}, + 'images_kwargs': {'return_tensors': 'pt'}, + 'audio_kwargs': {'padding': True, 'return_tensors': 'pt'}, + 'videos_kwargs': {'fps': 2.0, 'return_tensors': 'pt'}, + 'common_kwargs': {'return_tensors': 'pt'}, + } + + text_inputs = self.tokenizer(text, **output_kwargs["text_kwargs"]) + return text_inputs["input_ids"].tolist()[0] + + + def encode_vpm_video(self, content="Describe this image.", num_img=1, img_token_num=256): + imgs_token = ( + '<|vision_start|>' + + '<|video_pad|>' * img_token_num * num_img + + '<|vision_end|>' + ) + + text = ( + f'<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n' + f'<|im_start|>user\n{imgs_token}{content}<|im_end|>\n' + f'<|im_start|>assistant\n' + ) + + output_kwargs = { + 'text_kwargs': {'padding': True, 'return_tensors': 'pt'}, + 'images_kwargs': {'return_tensors': 'pt'}, + 'audio_kwargs': {'padding': True, 'return_tensors': 'pt'}, + 'videos_kwargs': {'fps': 2.0, 'return_tensors': 'pt'}, + 'common_kwargs': {'return_tensors': 'pt'}, + } + + text_inputs = self.tokenizer(text, **output_kwargs["text_kwargs"]) + return text_inputs["input_ids"].tolist()[0] + + def decode(self, token_ids): + self.token_ids_cache += token_ids + text = self.tokenizer.decode(self.token_ids_cache) + if "\ufffd" in text: + print("text 中包含非法字符") + return "" + else: + self.token_ids_cache.clear() + return text + + @property + def bos_id(self): + return self.tokenizer.bos_token_id + + @property + def eos_id(self): + return self.tokenizer.eos_token_id + + @property + def bos_token(self): + return self.tokenizer.bos_token + + @property + def eos_token(self): + return self.tokenizer.eos_token + + @property + def img_start_token(self): + return self.tokenizer.encode("<|vision_start|>")[0] + + @property + def img_context_token(self): + return self.tokenizer.encode("<|image_pad|>")[0] + + +class Request(BaseHTTPRequestHandler): + timeout = 5 + server_version = 'Apache' + + def do_GET(self): + print(self.path) + self.send_response(200) + self.send_header("type", "get") + self.end_headers() + + if self.path == '/bos_id': + bos_id = tokenizer.bos_id + msg = json.dumps({'bos_id': -1 if bos_id is None else bos_id}) + elif self.path == '/eos_id': + eos_id = tokenizer.eos_id + msg = json.dumps({'eos_id': -1 if eos_id is None else eos_id}) + elif self.path == '/img_start_token': + img_start_token = tokenizer.img_start_token + msg = json.dumps({'img_start_token': -1 if img_start_token is None else img_start_token}) + elif self.path == '/img_context_token': + img_context_token = tokenizer.img_context_token + msg = json.dumps({'img_context_token': -1 if img_context_token is None else img_context_token}) + else: + msg = 'error' + + print(msg) + msg = str(msg).encode() + self.wfile.write(msg) + + def do_POST(self): + data = self.rfile.read(int(self.headers['content-length'])) + req = json.loads(data.decode()) + + if self.path == "/encode": + prompt = req['text'] + b_img_prompt = req.get('img_prompt', False) + img_type = req.get('img_type', 'image') # 默认 image + + if b_img_prompt: + if img_type == 'image': + token_ids = tokenizer.encode_vpm_image( + prompt, + req.get("num_img", 1), + req.get("img_token_num", 256) + ) + elif img_type == 'video': + token_ids = tokenizer.encode_vpm_video( + prompt, + req.get("num_img", 1), + req.get("img_token_num", 256) + ) + else: + token_ids = tokenizer.encode(prompt) # fallback + else: + token_ids = tokenizer.encode(prompt) + + msg = json.dumps({'token_ids': -1 if token_ids is None else token_ids}) + + elif self.path == "/decode": + req = json.loads(data.decode()) + token_ids = req['token_ids'] + text = tokenizer.decode(token_ids) + msg = json.dumps({'text': "" if text is None else text}) + else: + msg = 'error' + + self.send_response(200) + self.end_headers() + self.wfile.write(str(msg).encode()) + + +if __name__ == "__main__": + args = argparse.ArgumentParser() + args.add_argument('--host', type=str, default='localhost') + args.add_argument('--port', type=int, default=8080) + args.add_argument('--model_id', type=str, default='tokenizer') + args.add_argument('--content', type=str, default='You are a helpful assistant.') + args = args.parse_args() + + tokenizer = Tokenizer_Http(args.model_id, system_content=args.content) + host = (args.host, args.port) + + print(f"http://{args.host}:{args.port}") + server = HTTPServer(host, Request) + server.serve_forever() \ No newline at end of file diff --git a/projects/llm_framework/tools/llm_pack.py b/projects/llm_framework/tools/llm_pack.py index 9ac017e..56cbd37 100755 --- a/projects/llm_framework/tools/llm_pack.py +++ b/projects/llm_framework/tools/llm_pack.py @@ -486,6 +486,7 @@ def create_bin_deb(package_name, version, src_folder, revision = 'm5stack1', dep 'llm-model-qwen2.5-3B-Int4-ax650':[create_data_deb,'llm-model-qwen2.5-3B-Int4-ax650', '0.4', src_folder, revision], 'llm-model-qwen2.5-7B-Int4-ax650':[create_data_deb,'llm-model-qwen2.5-7B-Int4-ax650', '0.4', src_folder, revision], 'llm-model-qwen3-vl-2B-Int4-ax650':[create_data_deb,'llm-model-qwen3-vl-2B-Int4-ax650', '0.5', src_folder, revision], + 'llm-model-qwen3-vl-2B-Int4-P1536-ax650':[create_data_deb,'llm-model-qwen3-vl-2B-Int4-P1536-ax650', '0.5', src_folder, revision], # Llama model 'llm-model-llama3.2-1B-prefill-ax630c':[create_data_deb,'llm-model-llama3.2-1B-prefill-ax630c', data_version, src_folder, revision], 'llm-model-llama3.2-1B-p256-ax630c':[create_data_deb,'llm-model-llama3.2-1B-p256-ax630c', '0.4', src_folder, revision], From d8f3ab4714d3fd82e9a45315eb1b640ead9230f3 Mon Sep 17 00:00:00 2001 From: dianjixz <18637716021@163.com> Date: Wed, 25 Feb 2026 18:10:12 +0800 Subject: [PATCH 08/22] [update] Update llm-kws to 1.12 - fix the button state update logic in main.cpp --- projects/llm_framework/main_kws/SConstruct | 2 +- projects/llm_framework/main_kws/src/main.cpp | 4 ++-- projects/llm_framework/tools/llm_pack.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/projects/llm_framework/main_kws/SConstruct b/projects/llm_framework/main_kws/SConstruct index 0c46047..df1e6e5 100644 --- a/projects/llm_framework/main_kws/SConstruct +++ b/projects/llm_framework/main_kws/SConstruct @@ -60,7 +60,7 @@ ignore['ignore'] = list(set(ignore['ignore'])) with open('../dist/fileignore', 'w') as f: json.dump(ignore, f, indent=4) -env['COMPONENTS'].append({'target':'llm_kws-1.11', +env['COMPONENTS'].append({'target':'llm_kws-1.12', 'SRCS':SRCS, 'INCLUDE':INCLUDE, 'PRIVATE_INCLUDE':PRIVATE_INCLUDE, diff --git a/projects/llm_framework/main_kws/src/main.cpp b/projects/llm_framework/main_kws/src/main.cpp index 49b4213..6f11061 100644 --- a/projects/llm_framework/main_kws/src/main.cpp +++ b/projects/llm_framework/main_kws/src/main.cpp @@ -850,9 +850,9 @@ class llm_kws : public StackFlow { std::string user_msg = sample_unescapeString(data); nlohmann::json btn_json = nlohmann::json::parse(user_msg); - if (btn_json.contains("code") && btn_json.contains("vale")) { + if (btn_json.contains("code") && (btn_json.contains("vale") || btn_json.contains("value"))) { int current_code = btn_json["code"]; - int current_vale = btn_json["vale"]; + int current_vale = btn_json.contains("vale") ? btn_json["vale"] : btn_json["value"]; if (current_vale == 204) { int last_code = llm_task_obj->get_btn_204_state(); diff --git a/projects/llm_framework/tools/llm_pack.py b/projects/llm_framework/tools/llm_pack.py index 56cbd37..67152d5 100755 --- a/projects/llm_framework/tools/llm_pack.py +++ b/projects/llm_framework/tools/llm_pack.py @@ -375,7 +375,7 @@ def create_bin_deb(package_name, version, src_folder, revision = 'm5stack1', dep 'lib-llm':[create_lib_deb,'lib-llm', '1.9', src_folder, revision], 'llm-sys':[create_bin_deb,'llm-sys', '1.6', src_folder, revision, 'lib-llm (>= 1.8)'], 'llm-audio':[create_bin_deb,'llm-audio', '1.9', src_folder, revision], - 'llm-kws':[create_bin_deb,'llm-kws', '1.11', src_folder, revision], + 'llm-kws':[create_bin_deb,'llm-kws', '1.12', src_folder, revision], 'llm-asr':[create_bin_deb,'llm-asr', '1.10', src_folder, revision], 'llm-llm':[create_bin_deb,'llm-llm', '1.12', src_folder, revision], 'llm-tts':[create_bin_deb,'llm-tts', '1.7', src_folder, revision], From 1ec30450d3c58016409edb5605c82b3f545e4619 Mon Sep 17 00:00:00 2001 From: LittleMouse Date: Wed, 18 Mar 2026 16:13:40 +0800 Subject: [PATCH 09/22] [update] add llm_llm2 --- ext_components/SimpleCV/Kconfig | 5 + ext_components/SimpleCV/SConstruct | 51 + ext_components/openai-api/Kconfig | 5 + ext_components/openai-api/SConstruct | 51 + ext_components/tokenizer/SConstruct | 3 +- projects/llm_framework/SConstruct | 2 +- projects/llm_framework/config_defaults.mk | 3 + projects/llm_framework/main_llm2/Kconfig | 0 projects/llm_framework/main_llm2/SConstruct | 64 + .../models/mode_Qwen3-0.6B-ax650.json | 34 + projects/llm_framework/main_llm2/src/main.cpp | 683 + .../main_llm2/src/runner/LLM.cpp | 825 + .../main_llm2/src/runner/LLM.hpp | 119 + .../main_llm2/src/runner/LLMEmbedSelector.hpp | 113 + .../main_llm2/src/runner/LLMPostprocess.hpp | 333 + .../main_llm2/src/runner/VLMType.hpp | 67 + .../ax_model_runner/ax_model_runner.hpp | 153 + .../ax_model_runner/ax_model_runner_ax650.cpp | 400 + .../ax_model_runner/ax_model_runner_ax650.hpp | 24 + .../include/magic_enum/magic_enum.hpp | 1564 + .../src/runner/utils/ax_cmm_utils.hpp | 61 + .../main_llm2/src/runner/utils/ax_cv.hpp | 96 + .../main_llm2/src/runner/utils/bfloat16.hpp | 71 + .../main_llm2/src/runner/utils/cqdm.cpp | 48 + .../main_llm2/src/runner/utils/cqdm.h | 28 + .../main_llm2/src/runner/utils/files.cpp | 45 + .../main_llm2/src/runner/utils/files.hpp | 9 + .../main_llm2/src/runner/utils/http_utils.hpp | 102 + .../src/runner/utils/image_processor.cpp | 234 + .../src/runner/utils/image_processor.hpp | 32 + .../main_llm2/src/runner/utils/json.hpp | 24766 ++++++++++++++++ .../src/runner/utils/memory_utils.cpp | 65 + .../src/runner/utils/memory_utils.hpp | 89 + .../main_llm2/src/runner/utils/mrope.cpp | 213 + .../main_llm2/src/runner/utils/mrope.hpp | 43 + .../main_llm2/src/runner/utils/sample_log.h | 65 + .../src/runner/utils/string_utility.hpp | 217 + .../main_llm2/src/runner/utils/timer.hpp | 57 + .../src/runner/vision/vision_module.cpp | 1208 + .../src/runner/vision/vision_module.hpp | 137 + projects/llm_framework/tools/llm_pack.py | 4 +- 41 files changed, 32084 insertions(+), 5 deletions(-) create mode 100644 ext_components/SimpleCV/Kconfig create mode 100644 ext_components/SimpleCV/SConstruct create mode 100644 ext_components/openai-api/Kconfig create mode 100644 ext_components/openai-api/SConstruct create mode 100644 projects/llm_framework/main_llm2/Kconfig create mode 100644 projects/llm_framework/main_llm2/SConstruct create mode 100644 projects/llm_framework/main_llm2/models/mode_Qwen3-0.6B-ax650.json create mode 100644 projects/llm_framework/main_llm2/src/main.cpp create mode 100644 projects/llm_framework/main_llm2/src/runner/LLM.cpp create mode 100644 projects/llm_framework/main_llm2/src/runner/LLM.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/LLMEmbedSelector.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/LLMPostprocess.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/VLMType.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/ax_model_runner/ax_model_runner.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/ax_model_runner/ax_model_runner_ax650.cpp create mode 100644 projects/llm_framework/main_llm2/src/runner/ax_model_runner/ax_model_runner_ax650.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/magic_enum/include/magic_enum/magic_enum.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/ax_cmm_utils.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/ax_cv.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/bfloat16.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/cqdm.cpp create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/cqdm.h create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/files.cpp create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/files.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/http_utils.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/image_processor.cpp create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/image_processor.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/json.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/memory_utils.cpp create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/memory_utils.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/mrope.cpp create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/mrope.hpp create mode 100755 projects/llm_framework/main_llm2/src/runner/utils/sample_log.h create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/string_utility.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/utils/timer.hpp create mode 100644 projects/llm_framework/main_llm2/src/runner/vision/vision_module.cpp create mode 100644 projects/llm_framework/main_llm2/src/runner/vision/vision_module.hpp diff --git a/ext_components/SimpleCV/Kconfig b/ext_components/SimpleCV/Kconfig new file mode 100644 index 0000000..adfcb6e --- /dev/null +++ b/ext_components/SimpleCV/Kconfig @@ -0,0 +1,5 @@ +menuconfig AX_SIMPLECV_ENABLED + bool "Enable SimpleCV support" + default n + help + enable SimpleCV support \ No newline at end of file diff --git a/ext_components/SimpleCV/SConstruct b/ext_components/SimpleCV/SConstruct new file mode 100644 index 0000000..2f50b0a --- /dev/null +++ b/ext_components/SimpleCV/SConstruct @@ -0,0 +1,51 @@ +# component2/SConscript +Import("env") +import os +from pathlib import Path + +with open(env["PROJECT_TOOL_S"]) as f: + exec(f.read()) + +_SDK_PATH = os.path.normpath( + os.environ.get("SDK_PATH", str(Path(os.getcwd()) / ".." / "..")) +) + +env["GIT_REPO_LISTS"]["SimpleCV"] = { + "url": "https://github.com/ZHEQIUSHUI/SimpleCV.git", + "commit": "7729953c7c75ffaea441f70e0a9eb62790f3eb2b", + "path": str(Path(_SDK_PATH) / "github_source" / "SimpleCV"), +} + +if "CONFIG_AX_SIMPLECV_ENABLED" in os.environ: + check_component("SimpleCV") + SRCS = [] + INCLUDE = [] + PRIVATE_INCLUDE = [] + REQUIREMENTS = [] + STATIC_LIB = [] + DYNAMIC_LIB = [] + DEFINITIONS = [] + DEFINITIONS_PRIVATE = [] + LDFLAGS = [] + LINK_SEARCH_PATH = [] + + INCLUDE += [ + os.path.join(env["GIT_REPO_LISTS"]["SimpleCV"]["path"], "include"), + ] + + env["COMPONENTS"].append( + { + "target": os.path.basename(env["component_dir"]), + "SRCS": SRCS, + "INCLUDE": INCLUDE, + "PRIVATE_INCLUDE": PRIVATE_INCLUDE, + "REQUIREMENTS": REQUIREMENTS, + "STATIC_LIB": STATIC_LIB, + "DYNAMIC_LIB": DYNAMIC_LIB, + "DEFINITIONS": DEFINITIONS, + "DEFINITIONS_PRIVATE": DEFINITIONS_PRIVATE, + "LDFLAGS": LDFLAGS, + "LINK_SEARCH_PATH": LINK_SEARCH_PATH, + "REGISTER": "static", + } + ) \ No newline at end of file diff --git a/ext_components/openai-api/Kconfig b/ext_components/openai-api/Kconfig new file mode 100644 index 0000000..bf1b401 --- /dev/null +++ b/ext_components/openai-api/Kconfig @@ -0,0 +1,5 @@ +menuconfig AX_OPENAI_API_ENABLED + bool "Enable openai-api support" + default n + help + enable openai-api support \ No newline at end of file diff --git a/ext_components/openai-api/SConstruct b/ext_components/openai-api/SConstruct new file mode 100644 index 0000000..e09a664 --- /dev/null +++ b/ext_components/openai-api/SConstruct @@ -0,0 +1,51 @@ +# component2/SConscript +Import("env") +import os +from pathlib import Path + +with open(env["PROJECT_TOOL_S"]) as f: + exec(f.read()) + +_SDK_PATH = os.path.normpath( + os.environ.get("SDK_PATH", str(Path(os.getcwd()) / ".." / "..")) +) + +env["GIT_REPO_LISTS"]["openai-api"] = { + "url": "https://github.com/ZHEQIUSHUI/openai-api.cpp.git", + "commit": "127f3e75054a733ca1b903ff1f8ec600b90cb6b0", + "path": str(Path(_SDK_PATH) / "github_source" / "openai-api.cpp"), +} + +if "CONFIG_AX_OPENAI_API_ENABLED" in os.environ: + check_component("openai-api") + SRCS = [] + INCLUDE = [] + PRIVATE_INCLUDE = [] + REQUIREMENTS = [] + STATIC_LIB = [] + DYNAMIC_LIB = [] + DEFINITIONS = [] + DEFINITIONS_PRIVATE = [] + LDFLAGS = [] + LINK_SEARCH_PATH = [] + + INCLUDE += [ + os.path.join(env["GIT_REPO_LISTS"]["openai-api"]["path"], "include"), + ] + + env["COMPONENTS"].append( + { + "target": os.path.basename(env["component_dir"]), + "SRCS": SRCS, + "INCLUDE": INCLUDE, + "PRIVATE_INCLUDE": PRIVATE_INCLUDE, + "REQUIREMENTS": REQUIREMENTS, + "STATIC_LIB": STATIC_LIB, + "DYNAMIC_LIB": DYNAMIC_LIB, + "DEFINITIONS": DEFINITIONS, + "DEFINITIONS_PRIVATE": DEFINITIONS_PRIVATE, + "LDFLAGS": LDFLAGS, + "LINK_SEARCH_PATH": LINK_SEARCH_PATH, + "REGISTER": "static", + } + ) \ No newline at end of file diff --git a/ext_components/tokenizer/SConstruct b/ext_components/tokenizer/SConstruct index e393f25..0788ac7 100644 --- a/ext_components/tokenizer/SConstruct +++ b/ext_components/tokenizer/SConstruct @@ -12,7 +12,7 @@ _SDK_PATH = os.path.normpath( env["GIT_REPO_LISTS"]["tokenizer"] = { "url": "https://github.com/ZHEQIUSHUI/tokenizer.git", - "commit": "83f41d4b5b9a135c167d44fcdf2a0c56ebacca6d", + "commit": "109a37cf70d1891e0dc41c39261dabb70d8a4fce", "path": str(Path(_SDK_PATH) / "github_source" / "tokenizer"), } @@ -32,7 +32,6 @@ if "CONFIG_AX_TOKENIZER_ENABLED" in os.environ: INCLUDE += [ os.path.join(env["GIT_REPO_LISTS"]["tokenizer"]["path"], "include"), ] - print("AX-TOKENIZER INCLUDE:", INCLUDE) env["COMPONENTS"].append( { diff --git a/projects/llm_framework/SConstruct b/projects/llm_framework/SConstruct index 98fe9c0..f0c4571 100644 --- a/projects/llm_framework/SConstruct +++ b/projects/llm_framework/SConstruct @@ -5,7 +5,7 @@ import shutil os.environ['SDK_PATH'] = os.path.normpath(str(Path(os.getcwd())/'..'/'..'/'SDK')) os.environ['EXT_COMPONENTS_PATH'] = os.path.normpath(str(Path(os.getcwd())/'..'/'..'/'ext_components')) -version = 'v0.1.5' +version = 'v0.1.6' static_lib = 'static_lib' update = False diff --git a/projects/llm_framework/config_defaults.mk b/projects/llm_framework/config_defaults.mk index 898c48a..eb263c9 100644 --- a/projects/llm_framework/config_defaults.mk +++ b/projects/llm_framework/config_defaults.mk @@ -20,7 +20,10 @@ CONFIG_LHV_WITH_EVPP=y CONFIG_STACKFLOW_ENABLED=y CONFIG_UTILITIES_BASE64_ENABLED=y CONFIG_SINGLE_HEADER_LIBS_ENABLED=y +CONFIG_AX_OPENAI_API_ENABLED=y CONFIG_AX_SAMPLES_ENABLED=y +CONFIG_AX_SIMPLECV_ENABLED=y +CONFIG_AX_TOKENIZER_ENABLED=y CONFIG_SIMDJSON_COMPENENT_ENABLED=y CONFIG_SAMPLE_COMMON_ENABLED=y CONFIG_MODBUS_ENABLED=y \ No newline at end of file diff --git a/projects/llm_framework/main_llm2/Kconfig b/projects/llm_framework/main_llm2/Kconfig new file mode 100644 index 0000000..e69de29 diff --git a/projects/llm_framework/main_llm2/SConstruct b/projects/llm_framework/main_llm2/SConstruct new file mode 100644 index 0000000..c74c0d6 --- /dev/null +++ b/projects/llm_framework/main_llm2/SConstruct @@ -0,0 +1,64 @@ +import os + +Import('env') +with open(env['PROJECT_TOOL_S']) as f: + exec(f.read()) + + +SRCS = append_srcs_dir(ADir('src')) +INCLUDE = [ADir('include'), ADir('.')] +PRIVATE_INCLUDE = [ADir('../include')] +REQUIREMENTS = ['pthread','atomic','rt', 'dl', 'm', 'ax_msp', 'utilities', 'eventpp', 'StackFlow', 'tokenizer', 'openai-api', 'SimpleCV'] +STATIC_LIB = [] +DYNAMIC_LIB = [] +DEFINITIONS = [] +DEFINITIONS_PRIVATE = [] +LDFLAGS = [] +LINK_SEARCH_PATH = [] +STATIC_FILES = [] + +DEFINITIONS += ['-O2'] +DEFINITIONS += ['-std=c++17'] +LDFLAGS+=['-Wl,-rpath=/opt/m5stack/lib', '-Wl,-rpath=/usr/local/m5stack/lib', '-Wl,-rpath=/usr/local/m5stack/lib/gcc-10.3', '-Wl,-rpath=/opt/lib', '-Wl,-rpath=/opt/usr/lib', '-Wl,-rpath=./'] +REQUIREMENTS += ['ax_engine', 'ax_interpreter', 'ax_sys', 'utilities'] +LINK_SEARCH_PATH += [ADir('../static_lib')] + + +INCLUDE += [ADir('src/runner'), ADir('src/runner/utils'), ADir('src/runner/ax_model_runner]'), ADir('src/runner/magic_enum/include')] +STATIC_LIB += [AFile('../static_lib/libopenai_api.a'), AFile('../static_lib/libsimplecv.a'), AFile('../static_lib/libtokenizer.a')] + +STATIC_FILES += Glob('models/mode_*.json') + +IGNORE_FILES = [] +IGNORE_FILES += ['llm'] + +import json +if not os.path.exists('../dist'): + os.makedirs('../dist') +ignore = {'ignore':[]} +try: + with open('../dist/fileignore', 'a+') as f: + f.seek(0) + ignore = json.load(f) +except: + pass +ignore['ignore'] += IGNORE_FILES +ignore['ignore'] = list(set(ignore['ignore'])) +with open('../dist/fileignore', 'w') as f: + json.dump(ignore, f, indent=4) + +env['COMPONENTS'].append({'target':'llm_llm2-1.12', + 'SRCS':SRCS, + 'INCLUDE':INCLUDE, + 'PRIVATE_INCLUDE':PRIVATE_INCLUDE, + 'REQUIREMENTS':REQUIREMENTS, + 'STATIC_LIB':STATIC_LIB, + 'DYNAMIC_LIB':DYNAMIC_LIB, + 'DEFINITIONS':DEFINITIONS, + 'DEFINITIONS_PRIVATE':DEFINITIONS_PRIVATE, + 'LDFLAGS':LDFLAGS, + 'LINK_SEARCH_PATH':LINK_SEARCH_PATH, + 'STATIC_FILES':STATIC_FILES, + 'REGISTER':'project' + }) + diff --git a/projects/llm_framework/main_llm2/models/mode_Qwen3-0.6B-ax650.json b/projects/llm_framework/main_llm2/models/mode_Qwen3-0.6B-ax650.json new file mode 100644 index 0000000..febcc58 --- /dev/null +++ b/projects/llm_framework/main_llm2/models/mode_Qwen3-0.6B-ax650.json @@ -0,0 +1,34 @@ +{ + "mode": "Qwen3-0.6B-ax650", + "type": "llm", + "homepage": "https://huggingface.co/AXERA-TECH/Qwen3-0.6B", + "capabilities": [ + "text_generation", + "chat" + ], + "input_type": [ + "llm.utf-8", + "llm.utf-8.stream", + "llm.chat_completion", + "llm.chat_completion.stream" + ], + "output_type": [ + "llm.utf-8", + "llm.utf-8.stream" + ], + "mode_param": { + "system_prompt": "you are a helpful assistant.", + "model_name": "Qwen3-0.6B-ax650", + "template_filename_axmodel": "qwen3_p128_l%d_together.axmodel", + "axmodel_num": 28, + "filename_post_axmodel": "qwen3_post.axmodel", + "post_config_path": "post_config.json", + "tokenizer_type": "Qwen3", + "url_tokenizer_model": "qwen3_tokenizer.txt", + "filename_tokens_embed": "model.embed_tokens.weight.bfloat16.bin", + "tokens_embed_num": 151936, + "tokens_embed_size": 1024, + "b_use_mmap_load_embed": true, + "b_use_mmap_load_layer": true + } +} \ No newline at end of file diff --git a/projects/llm_framework/main_llm2/src/main.cpp b/projects/llm_framework/main_llm2/src/main.cpp new file mode 100644 index 0000000..69807c9 --- /dev/null +++ b/projects/llm_framework/main_llm2/src/main.cpp @@ -0,0 +1,683 @@ +/* + * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD + * + * SPDX-License-Identifier: MIT + */ +#include "StackFlow.h" +#include "runner/LLM.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../../../../SDK/components/utilities/include/sample_log.h" +#include "thread_safe_list.h" +using namespace StackFlows; +#ifdef ENABLE_BACKWARD +#define BACKWARD_HAS_DW 1 +#include "backward.hpp" +#include "backward.h" +#endif + +#define MAX_TASK_NUM 2 + +int main_exit_flage = 0; +static void __sigint(int iSigNo) +{ + SLOGW("llm_llm2 will be exit!"); + main_exit_flage = 1; +} + +static std::string base_model_path_; +static std::string base_model_config_path_; + +typedef std::function task_callback_t; + +#define CONFIG_AUTO_SET(obj, key) \ + if (config_body.contains(#key)) \ + mode_config_.key = config_body[#key]; \ + else if (obj.contains(#key)) \ + mode_config_.key = obj[#key]; + +class llm_task { +private: + static std::atomic next_port_; + std::atomic_bool tokenizer_server_flage_; + unsigned int port_; + pid_t tokenizer_pid_ = -1; + +public: + enum inference_status { INFERENCE_NONE = 0, INFERENCE_RUNNING }; + LLMAttrType mode_config_; + std::unique_ptr lLaMa_; + std::string model_; + std::string response_format_; + std::vector inputs_; + std::string prompt_; + std::string last_reply; + std::vector prompt_data; + std::vector tokens_ids, tokens_diff; + std::vector> k_caches, v_caches; + std::string kvcache_path; + int precompute_len = 0; + std::vector _token_ids; + static int ax_init_flage_; + task_callback_t out_callback_; + bool enoutput_; + bool enstream_; + + std::unique_ptr inference_run_; + thread_safe::list async_list_; + + void set_output(task_callback_t out_callback) + { + out_callback_ = out_callback; + } + + bool parse_config(const nlohmann::json &config_body) + { + try { + model_ = config_body.at("model"); + response_format_ = config_body.at("response_format"); + enoutput_ = config_body.at("enoutput"); + prompt_ = config_body.at("prompt"); + + if (config_body.contains("input")) { + if (config_body["input"].is_string()) { + inputs_.push_back(config_body["input"].get()); + } else if (config_body["input"].is_array()) { + for (auto _in : config_body["input"]) { + inputs_.push_back(_in.get()); + } + } + } + } catch (...) { + SLOGE("setup config_body error"); + return true; + } + enstream_ = (response_format_.find("stream") != std::string::npos); + return false; + } + + int load_model(const nlohmann::json &config_body) + { + if (parse_config(config_body)) { + return -1; + } + nlohmann::json file_body; + std::list config_file_paths = + get_config_file_paths(base_model_path_, base_model_config_path_, model_); + try { + for (auto file_name : config_file_paths) { + std::ifstream config_file(file_name); + if (!config_file.is_open()) { + SLOGW("config file :%s miss", file_name.c_str()); + continue; + } + SLOGI("config file :%s read", file_name.c_str()); + config_file >> file_body; + config_file.close(); + break; + } + if (file_body.empty()) { + SLOGE("all config file miss"); + return -2; + } + std::string base_model = base_model_path_ + model_ + "/"; + SLOGI("base_model %s", base_model.c_str()); + + CONFIG_AUTO_SET(file_body["mode_param"], system_prompt); + + CONFIG_AUTO_SET(file_body["mode_param"], template_filename_axmodel); + CONFIG_AUTO_SET(file_body["mode_param"], axmodel_num); + CONFIG_AUTO_SET(file_body["mode_param"], filename_post_axmodel); + CONFIG_AUTO_SET(file_body["mode_param"], post_config_path); + + CONFIG_AUTO_SET(file_body["mode_param"], tokenizer_type); + CONFIG_AUTO_SET(file_body["mode_param"], url_tokenizer_model); + CONFIG_AUTO_SET(file_body["mode_param"], b_bos); + CONFIG_AUTO_SET(file_body["mode_param"], b_eos); + + CONFIG_AUTO_SET(file_body["mode_param"], filename_tokens_embed); + CONFIG_AUTO_SET(file_body["mode_param"], tokens_embed_num); + CONFIG_AUTO_SET(file_body["mode_param"], tokens_embed_size); + CONFIG_AUTO_SET(file_body["mode_param"], b_use_mmap_load_embed); + + CONFIG_AUTO_SET(file_body["mode_param"], filename_image_encoder_axmodel); + CONFIG_AUTO_SET(file_body["mode_param"], vision_cache_dir); + CONFIG_AUTO_SET(file_body["mode_param"], vision_width); + CONFIG_AUTO_SET(file_body["mode_param"], vision_height); + CONFIG_AUTO_SET(file_body["mode_param"], vision_patch_size); + CONFIG_AUTO_SET(file_body["mode_param"], vision_temporal_patch_size); + CONFIG_AUTO_SET(file_body["mode_param"], vision_spatial_merge_size); + CONFIG_AUTO_SET(file_body["mode_param"], vision_fps); + CONFIG_AUTO_SET(file_body["mode_param"], vision_tokens_per_second); + + CONFIG_AUTO_SET(file_body["mode_param"], prefill_token_num); + CONFIG_AUTO_SET(file_body["mode_param"], prefill_max_token_num); + CONFIG_AUTO_SET(file_body["mode_param"], prefill_grpid); + CONFIG_AUTO_SET(file_body["mode_param"], max_token_len); + CONFIG_AUTO_SET(file_body["mode_param"], kv_cache_num); + CONFIG_AUTO_SET(file_body["mode_param"], kv_cache_size); + CONFIG_AUTO_SET(file_body["mode_param"], b_use_mmap_load_layer); + + mode_config_.template_filename_axmodel = base_model + mode_config_.template_filename_axmodel; + mode_config_.filename_post_axmodel = base_model + mode_config_.filename_post_axmodel; + mode_config_.filename_tokens_embed = base_model + mode_config_.filename_tokens_embed; + mode_config_.url_tokenizer_model = base_model + mode_config_.url_tokenizer_model; + mode_config_.post_config_path = base_model + mode_config_.post_config_path; + mode_config_.filename_image_encoder_axmodel = base_model + mode_config_.filename_image_encoder_axmodel; + mode_config_.vision_cache_dir = base_model + mode_config_.vision_cache_dir; + mode_config_.runing_callback = [this](std::string str, float token_per_sec, void *reserve) { + if (this->out_callback_) { + this->out_callback_(str, false); + } + }; + lLaMa_ = std::make_unique(); + if (!lLaMa_->Init(mode_config_)) { + lLaMa_->Deinit(); + lLaMa_.reset(); + return -2; + } + + } catch (...) { + SLOGE("config false"); + return -3; + } + return 0; + } + + void run() + { + std::string par; + for (;;) { + { + par = async_list_.get(); + if (par.empty()) break; + inference(par); + } + } + } + + int inference_async(const std::string &msg) + { + if (msg.empty()) return -1; + if (async_list_.size() < 3) { + std::string par = msg; + async_list_.put(par); + } else { + SLOGE("inference list is full\n"); + } + return async_list_.size(); + } + + void inference(const std::string &msg) + { + try { + if (lLaMa_) { + std::vector history; + if (!mode_config_.system_prompt.empty()) { + history.push_back({SYSTEM, TEXT, mode_config_.system_prompt}); + } + + history.push_back({USER, TEXT, msg}); + history = lLaMa_->Run(history); + std::string out; + if (!history.empty() && history.back().role == ASSISTANT) { + out = history.back().data; + } + + if (out_callback_) out_callback_(out, true); + } + } catch (...) { + SLOGW("lLaMa_->Run have error!"); + } + } + + bool pause() + { + if (lLaMa_) lLaMa_->Stop(); + return true; + } + + bool delete_model() + { + if (tokenizer_pid_ != -1) { + kill(tokenizer_pid_, SIGTERM); + waitpid(tokenizer_pid_, nullptr, 0); + tokenizer_pid_ = -1; + } + if (lLaMa_) lLaMa_->Deinit(); + if (lLaMa_) lLaMa_.reset(); + return true; + } + + static unsigned int getNextPort() + { + unsigned int port = next_port_++; + if (port > 8089) { + next_port_ = 8080; + port = 8080; + } + return port; + } + + void _ax_init() + { + if (!ax_init_flage_) { + int ret = AX_SYS_Init(); + if (0 != ret) { + fprintf(stderr, "AX_SYS_Init failed! ret = 0x%x\n", ret); + } + AX_ENGINE_NPU_ATTR_T npu_attr; + memset(&npu_attr, 0, sizeof(npu_attr)); + ret = AX_ENGINE_Init(&npu_attr); + if (0 != ret) { + fprintf(stderr, "Init ax-engine failed{0x%8x}.\n", ret); + } + } + ax_init_flage_++; + } + + void _ax_deinit() + { + if (ax_init_flage_ > 0) { + --ax_init_flage_; + if (!ax_init_flage_) { + AX_ENGINE_Deinit(); + AX_SYS_Deinit(); + } + } + } + + llm_task(const std::string &workid) : tokenizer_server_flage_(false), port_(getNextPort()) + { + inference_run_ = std::make_unique(std::bind(&llm_task::run, this)); + _ax_init(); + } + + void start() + { + if (!inference_run_) { + inference_run_ = std::make_unique(std::bind(&llm_task::run, this)); + } + } + + void stop() + { + if (inference_run_) { + std::string par; + async_list_.put(par); + if (lLaMa_) lLaMa_->Stop(); + inference_run_->join(); + inference_run_.reset(); + } + } + + ~llm_task() + { + stop(); + if (tokenizer_pid_ != -1) { + kill(tokenizer_pid_, SIGTERM); + waitpid(tokenizer_pid_, nullptr, WNOHANG); + } + if (lLaMa_) { + lLaMa_->Deinit(); + } + _ax_deinit(); + } +}; + +std::atomic llm_task::next_port_{8080}; +int llm_task::ax_init_flage_ = 0; + +#undef CONFIG_AUTO_SET + +class llm_llm : public StackFlow { +private: + std::unordered_map> llm_task_; + +public: + llm_llm() : StackFlow("llm") + { + } + + void task_output(const std::weak_ptr llm_task_obj_weak, + const std::weak_ptr llm_channel_weak, const std::string &data, bool finish) + { + auto llm_task_obj = llm_task_obj_weak.lock(); + auto llm_channel = llm_channel_weak.lock(); + if (!(llm_task_obj && llm_channel)) { + return; + } + SLOGI("send:%s", data.c_str()); + if (llm_channel->enstream_) { + static int count = 0; + nlohmann::json data_body; + data_body["index"] = count++; + data_body["delta"] = data; + if (!finish) + data_body["delta"] = data; + else + data_body["delta"] = std::string(""); + data_body["finish"] = finish; + if (finish) count = 0; + SLOGI("send stream"); + llm_channel->send(llm_task_obj->response_format_, data_body, LLM_NO_ERROR); + } else if (finish) { + SLOGI("send utf-8"); + llm_channel->send(llm_task_obj->response_format_, data, LLM_NO_ERROR); + } + } + + void task_pause(const std::weak_ptr llm_task_obj_weak, + const std::weak_ptr llm_channel_weak) + { + auto llm_task_obj = llm_task_obj_weak.lock(); + auto llm_channel = llm_channel_weak.lock(); + if (!(llm_task_obj && llm_channel)) { + return; + } + if (llm_task_obj->lLaMa_) llm_task_obj->lLaMa_->Stop(); + } + + void pause(const std::string &work_id, const std::string &object, const std::string &data) override + { + SLOGI("llm_llm2::work:%s", data.c_str()); + + nlohmann::json error_body; + int work_id_num = sample_get_work_id_num(work_id); + if (llm_task_.find(work_id_num) == llm_task_.end()) { + error_body["code"] = -6; + error_body["message"] = "Unit Does Not Exist"; + send("None", "None", error_body, work_id); + return; + } + task_pause(llm_task_[work_id_num], get_channel(work_id_num)); + send("None", "None", LLM_NO_ERROR, work_id); + } + + void task_user_data(const std::weak_ptr llm_task_obj_weak, + const std::weak_ptr llm_channel_weak, const std::string &object, + const std::string &data) + { + nlohmann::json error_body; + auto llm_task_obj = llm_task_obj_weak.lock(); + auto llm_channel = llm_channel_weak.lock(); + if (!(llm_task_obj && llm_channel)) { + error_body["code"] = -11; + error_body["message"] = "Model run failed."; + send("None", "None", error_body, unit_name_); + return; + } + const std::string *next_data = &data; + int ret; + std::string tmp_msg1; + if (object.find("stream") != std::string::npos) { + static std::unordered_map stream_buff; + try { + if (decode_stream(data, tmp_msg1, stream_buff)) { + return; + }; + } catch (...) { + stream_buff.clear(); + error_body["code"] = -25; + error_body["message"] = "Stream data index error."; + send("None", "None", error_body, unit_name_); + return; + } + next_data = &tmp_msg1; + } + std::string tmp_msg2; + if (object.find("base64") != std::string::npos) { + ret = decode_base64((*next_data), tmp_msg2); + if (ret == -1) { + error_body["code"] = -23; + error_body["message"] = "Base64 decoding error."; + send("None", "None", error_body, unit_name_); + return; + } + next_data = &tmp_msg2; + } + llm_task_obj->inference_async(sample_unescapeString(*next_data)); + } + + void task_asr_data(const std::weak_ptr llm_task_obj_weak, + const std::weak_ptr llm_channel_weak, const std::string &object, + const std::string &data) + { + auto llm_task_obj = llm_task_obj_weak.lock(); + auto llm_channel = llm_channel_weak.lock(); + if (!(llm_task_obj && llm_channel)) { + return; + } + if (object.find("stream") != std::string::npos) { + if (sample_json_str_get(data, "finish") == "true") { + llm_task_obj->inference_async(sample_json_str_get(data, "delta")); + } + } else { + llm_task_obj->inference_async(data); + } + } + + void kws_awake(const std::weak_ptr llm_task_obj_weak, + const std::weak_ptr llm_channel_weak, const std::string &object, + const std::string &data) + { + auto llm_task_obj = llm_task_obj_weak.lock(); + auto llm_channel = llm_channel_weak.lock(); + if (!(llm_task_obj && llm_channel)) { + return; + } + if (llm_task_obj->lLaMa_) llm_task_obj->lLaMa_->Stop(); + } + + int setup(const std::string &work_id, const std::string &object, const std::string &data) override + { + nlohmann::json error_body; + if ((llm_task_channel_.size() - 1) == MAX_TASK_NUM) { + error_body["code"] = -21; + error_body["message"] = "task full"; + send("None", "None", error_body, "llm"); + return -1; + } + + int work_id_num = sample_get_work_id_num(work_id); + auto llm_channel = get_channel(work_id); + auto llm_task_obj = std::make_shared(work_id); + + nlohmann::json config_body; + try { + config_body = nlohmann::json::parse(data); + } catch (...) { + SLOGE("setup json format error."); + error_body["code"] = -2; + error_body["message"] = "json format error."; + send("None", "None", error_body, "kws"); + return -2; + } + int ret = llm_task_obj->load_model(config_body); + if (ret == 0) { + llm_channel->set_output(llm_task_obj->enoutput_); + llm_channel->set_stream(llm_task_obj->enstream_); + + llm_task_obj->set_output(std::bind(&llm_llm::task_output, this, std::weak_ptr(llm_task_obj), + std::weak_ptr(llm_channel), std::placeholders::_1, + std::placeholders::_2)); + + for (const auto input : llm_task_obj->inputs_) { + if (input.find("llm") != std::string::npos) { + llm_channel->subscriber_work_id( + "", std::bind(&llm_llm::task_user_data, this, std::weak_ptr(llm_task_obj), + std::weak_ptr(llm_channel), std::placeholders::_1, + std::placeholders::_2)); + } else if ((input.find("asr") != std::string::npos) || (input.find("whisper") != std::string::npos)) { + llm_channel->subscriber_work_id( + input, std::bind(&llm_llm::task_asr_data, this, std::weak_ptr(llm_task_obj), + std::weak_ptr(llm_channel), std::placeholders::_1, + std::placeholders::_2)); + } else if (input.find("kws") != std::string::npos) { + llm_channel->subscriber_work_id( + input, std::bind(&llm_llm::kws_awake, this, std::weak_ptr(llm_task_obj), + std::weak_ptr(llm_channel), std::placeholders::_1, + std::placeholders::_2)); + } + } + llm_task_[work_id_num] = llm_task_obj; + SLOGI("load_mode success"); + send("None", "None", LLM_NO_ERROR, work_id); + return 0; + } else { + SLOGE("load_mode Failed"); + error_body["code"] = -5; + error_body["message"] = "Model loading failed."; + send("None", "None", error_body, "llm"); + return -1; + } + } + + void link(const std::string &work_id, const std::string &object, const std::string &data) override + { + SLOGI("llm_llm2::link:%s", data.c_str()); + int ret = 1; + nlohmann::json error_body; + int work_id_num = sample_get_work_id_num(work_id); + if (llm_task_.find(work_id_num) == llm_task_.end()) { + error_body["code"] = -6; + error_body["message"] = "Unit Does Not Exist"; + send("None", "None", error_body, work_id); + return; + } + auto llm_channel = get_channel(work_id); + auto llm_task_obj = llm_task_[work_id_num]; + if (data.find("asr") != std::string::npos) { + ret = llm_channel->subscriber_work_id( + data, + std::bind(&llm_llm::task_asr_data, this, std::weak_ptr(llm_task_obj), + std::weak_ptr(llm_channel), std::placeholders::_1, std::placeholders::_2)); + llm_task_obj->inputs_.push_back(data); + } else if (data.find("kws") != std::string::npos) { + ret = llm_channel->subscriber_work_id( + data, + std::bind(&llm_llm::kws_awake, this, std::weak_ptr(llm_task_obj), + std::weak_ptr(llm_channel), std::placeholders::_1, std::placeholders::_2)); + llm_task_obj->inputs_.push_back(data); + } + if (ret) { + error_body["code"] = -20; + error_body["message"] = "link false"; + send("None", "None", error_body, work_id); + return; + } else { + send("None", "None", LLM_NO_ERROR, work_id); + } + } + + void unlink(const std::string &work_id, const std::string &object, const std::string &data) override + { + SLOGI("llm_llm2::unlink:%s", data.c_str()); + int ret = 0; + nlohmann::json error_body; + int work_id_num = sample_get_work_id_num(work_id); + if (llm_task_.find(work_id_num) == llm_task_.end()) { + error_body["code"] = -6; + error_body["message"] = "Unit Does Not Exist"; + send("None", "None", error_body, work_id); + return; + } + auto llm_channel = get_channel(work_id); + llm_channel->stop_subscriber_work_id(data); + auto llm_task_obj = llm_task_[work_id_num]; + for (auto it = llm_task_obj->inputs_.begin(); it != llm_task_obj->inputs_.end();) { + if (*it == data) { + it = llm_task_obj->inputs_.erase(it); + } else { + ++it; + } + } + send("None", "None", LLM_NO_ERROR, work_id); + } + + void taskinfo(const std::string &work_id, const std::string &object, const std::string &data) override + { + SLOGI("llm_llm2::taskinfo:%s", data.c_str()); + // int ret = 0; + nlohmann::json req_body; + int work_id_num = sample_get_work_id_num(work_id); + if (WORK_ID_NONE == work_id_num) { + std::vector task_list; + std::transform(llm_task_channel_.begin(), llm_task_channel_.end(), std::back_inserter(task_list), + [](const auto task_channel) { return task_channel.second->work_id_; }); + req_body = task_list; + send("llm.tasklist", req_body, LLM_NO_ERROR, work_id); + } else { + if (llm_task_.find(work_id_num) == llm_task_.end()) { + req_body["code"] = -6; + req_body["message"] = "Unit Does Not Exist"; + send("None", "None", req_body, work_id); + return; + } + auto llm_task_obj = llm_task_[work_id_num]; + req_body["model"] = llm_task_obj->model_; + req_body["response_format"] = llm_task_obj->response_format_; + req_body["enoutput"] = llm_task_obj->enoutput_; + req_body["inputs"] = llm_task_obj->inputs_; + send("llm.taskinfo", req_body, LLM_NO_ERROR, work_id); + } + } + + int exit(const std::string &work_id, const std::string &object, const std::string &data) override + { + SLOGI("llm_llm2::exit:%s", data.c_str()); + + nlohmann::json error_body; + int work_id_num = sample_get_work_id_num(work_id); + if (llm_task_.find(work_id_num) == llm_task_.end()) { + error_body["code"] = -6; + error_body["message"] = "Unit Does Not Exist"; + send("None", "None", error_body, work_id); + return -1; + } + llm_task_[work_id_num]->stop(); + auto llm_channel = get_channel(work_id_num); + llm_channel->stop_subscriber(""); + llm_task_.erase(work_id_num); + send("None", "None", LLM_NO_ERROR, work_id); + return 0; + } + + ~llm_llm() + { + while (1) { + auto iteam = llm_task_.begin(); + if (iteam == llm_task_.end()) { + break; + } + iteam->second->stop(); + get_channel(iteam->first)->stop_subscriber(""); + iteam->second.reset(); + llm_task_.erase(iteam->first); + } + } +}; + +int main(int argc, char *argv[]) +{ + signal(SIGTERM, __sigint); + signal(SIGINT, __sigint); + mkdir("/tmp/llm", 0777); + llm_llm llm; + while (!main_exit_flage) { + sleep(1); + } + llm.llm_firework_exit(); + return 0; +} \ No newline at end of file diff --git a/projects/llm_framework/main_llm2/src/runner/LLM.cpp b/projects/llm_framework/main_llm2/src/runner/LLM.cpp new file mode 100644 index 0000000..f5a785d --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/LLM.cpp @@ -0,0 +1,825 @@ +#include "LLM.hpp" +#include +#include +#include +#include +#include + +#include "bfloat16.hpp" +#include "LLMEmbedSelector.hpp" +#include "LLMPostprocess.hpp" +#include "UTF8Filter.hpp" +#include "cqdm.h" +#include "timer.hpp" +#include "utils/memory_utils.hpp" +#include "sample_log.h" + +#include "vision/vision_module.hpp" + +#ifdef USE_AXCL +#include "ax_model_runner/ax_model_runner_axcl.hpp" +#include "utils/axcl_manager.h" +using ax_runner_t = ax_runner_axcl; +#else +#include "ax_model_runner/ax_model_runner_ax650.hpp" +#include "ax_cmm_utils.hpp" +#include +using ax_runner_t = ax_runner_ax650; +#endif + +#define ALIGN_DOWN(x, a) ((x) & ~((a) - 1)) + +#ifdef USE_AXCL +static inline void llm_memset(void *phy, int val, size_t n, int devid) { axcl_Memset(phy, (uint8_t)val, n, devid); } +static inline void llm_h2d(void *phy_dst, const void *src, size_t n, int devid) { axcl_Memcpy(phy_dst, src, n, AXCL_MEMCPY_HOST_TO_DEVICE, devid); } +static inline void llm_d2h(void *dst, const void *phy_src, size_t n, int devid) { axcl_Memcpy(dst, phy_src, n, AXCL_MEMCPY_DEVICE_TO_HOST, devid); } +static inline void llm_d2d(void *phy_dst, const void *phy_src, size_t n, int devid) { axcl_Memcpy(phy_dst, phy_src, n, AXCL_MEMCPY_DEVICE_TO_DEVICE, devid); } +#define LLM_WADDR(t) ((void *)(t).phyAddr) +#define LLM_RADDR(t) ((const void *)(t).phyAddr) +#define LLM_DEVID(layer_obj) ((layer_obj).layer.get_devid()) +#else +static inline void llm_memset(void *vir, int val, size_t n, int /*devid*/) { memset(vir, val, n); } +static inline void llm_h2d(void *vir_dst, const void *src, size_t n, int /*devid*/) { memcpy(vir_dst, src, n); } +static inline void llm_d2h(void *dst, const void *vir_src, size_t n, int /*devid*/) { memcpy(dst, vir_src, n); } +static inline void llm_d2d(void *vir_dst, const void *vir_src, size_t n, int /*devid*/) { memcpy(vir_dst, vir_src, n); } +#define LLM_WADDR(t) ((t).pVirAddr) +#define LLM_RADDR(t) ((const void *)(t).pVirAddr) +#define LLM_DEVID(layer_obj) (0) +#endif + +struct LLM::Impl { + UTF8Filter utf8_filter; + std::shared_ptr tokenizer; + LLaMaEmbedSelector embed_selector; + + std::vector last_tokens_ids; + bool b_os_kvcache = false; + std::vector> k_caches, v_caches; + int precompute_len = 0; + + LLMAttrType _attr; + + std::unique_ptr vision; + vision::RunState vision_state; + bool has_vision_state = false; + + struct LLMLayer { + ax_runner_t layer; + std::string filename; + MMap layer_buffer; + std::vector layer_buffer_vec; + }; + + std::vector llama_layers; + ax_runner_t llama_post; + + int decode_grpid = 0; + bool b_stop = false; + LLMPostprocess postprocess; + + // ---- small helpers ---- + static int post_process(LLMPostprocess &postprocess, unsigned short *p, int n, std::vector &history, float *val = 0) + { + std::vector logits(n); + for (int i = 0; i < n; i++) + { + unsigned int proc = p[i] << 16; + logits[i] = *reinterpret_cast(&proc); + } + return postprocess.apply(logits, history); + } + + static inline void fill_indices(unsigned int *dst, int start, int count) + { for (int i = 0; i < count; ++i) dst[i] = (unsigned int)(start + i); } + + static inline void build_prefill_mask(std::vector &mask_tmp, + int kv_cache_num, + int token_rows, + int history_len, + int valid_rows) + { + bfloat16 bf16 = -65536.f; + std::fill(mask_tmp.begin(), mask_tmp.end(), bf16.data); + const int rows = std::max(0, std::min(token_rows, valid_rows)); + for (int r = 0; r < rows; ++r) { + auto row = mask_tmp.data() + r * (kv_cache_num + token_rows); + for (int j = 0; j < history_len; ++j) row[j] = 0; + int cur = kv_cache_num; for (int j = cur; j < cur + r + 1; ++j) row[j] = 0; + } + } + +#ifdef USE_AXCL + std::vector distributeModels(int cardCount, int modelCount) + { + std::vector assign(modelCount, 0); + if (cardCount <= 0 || modelCount <= 0) return assign; + int base = modelCount / cardCount; + int rem = modelCount % cardCount; + int idx = 0; + for (int c = 0; c < cardCount; c++) { + int cnt = base + (c < rem ? 1 : 0); + for (int i = 0; i < cnt; i++) assign[idx++] = c; + } + return assign; + } +#endif + + std::vector diff_token_ids(const std::vector &ids1, const std::vector &ids2, int &offset) + { + int min_len = (int)std::min(ids1.size(), ids2.size()); + offset = 0; + for (int i = 0; i < min_len; i++) { if (ids1[i] == ids2[i]) offset++; else break; } + if (offset >= (int)ids2.size()) return {}; + return std::vector(ids2.begin() + offset, ids2.end()); + } + + bool Init(LLMAttrType attr) + { + ALOGI("LLM init start"); + t_cqdm cqdm = create_cqdm(attr.axmodel_num + 3, 32); + this->_attr = attr; + + tokenizer = create_tokenizer(this->_attr.tokenizer_type); + if (!tokenizer) { ALOGE("create_tokenizer(%s) failed", this->_attr.tokenizer_type.c_str()); return false; } + if (!tokenizer->load(attr.url_tokenizer_model)) { ALOGE("tokenizer.init(%s) failed", attr.url_tokenizer_model.c_str()); return false; } + tokenizer->set_think_in_prompt(true); + update_cqdm(&cqdm, 0, "count", "tokenizer init ok"); + +#ifdef USE_AXCL + for (auto &devid : _attr.dev_ids) { if (axcl_Init(devid) != 0) { ALOGE("axcl_Init(%d) failed", devid); return false; } } + llama_layers.resize(attr.axmodel_num); + auto dev_assign = distributeModels((int)_attr.dev_ids.size(), attr.axmodel_num); + std::vector rets(attr.axmodel_num, 0); + std::atomic process_idx(1); +#pragma omp parallel for if (_attr.dev_ids.size() > 1) + for (int i = 0; i < attr.axmodel_num; i++) + { + char path[1024]; + sprintf(path, attr.template_filename_axmodel.c_str(), i); + llama_layers[i].filename = path; + int devid = _attr.dev_ids[dev_assign[i]]; + rets[i] = llama_layers[i].layer.init(llama_layers[i].filename.c_str(), devid); + int remain = axcl_GetCMMRemain(devid); + sprintf(path, "init %d axmodel ok,devid(%d) remain_cmm(%d MB)", i, devid, remain); + update_cqdm(&cqdm, process_idx++, "count", path); + } + for (int i = 0; i < attr.axmodel_num; i++) { if (rets[i] != 0) { ALOGE("init axmodel(%s) failed", llama_layers[i].filename.c_str()); return false; } } + { + int post_devid = llama_layers.back().layer.get_devid(); + int ret = llama_post.init(attr.filename_post_axmodel.c_str(), post_devid); + if (ret != 0) { ALOGE("init post axmodel(%s) failed", attr.filename_post_axmodel.c_str()); return false; } + char path[1024]; + sprintf(path, "init post axmodel ok,remain_cmm(%d MB)", axcl_GetCMMRemain(post_devid)); + update_cqdm(&cqdm, attr.axmodel_num + 1, "count", path); + } +#else + llama_layers.resize(attr.axmodel_num); + char axmodel_path[1024]; + for (int i = 0; i < attr.axmodel_num; i++) + { + sprintf(axmodel_path, attr.template_filename_axmodel.c_str(), i); + llama_layers[i].filename = axmodel_path; + int ret = llama_layers[i].layer.init(llama_layers[i].filename.c_str(), -1); + if (ret != 0) { ALOGE("init axmodel(%s) failed", llama_layers[i].filename.c_str()); return false; } + int remain_cmm = get_remaining_cmm_size(); + sprintf(axmodel_path, "init %d axmodel ok,remain_cmm(%d MB)", i, remain_cmm); + update_cqdm(&cqdm, i + 1, "count", axmodel_path); + } + { + int ret = llama_post.init(attr.filename_post_axmodel.c_str(), -1); + if (ret != 0) { ALOGE("init post axmodel(%s) failed", attr.filename_post_axmodel.c_str()); return false; } + int remain_cmm = get_remaining_cmm_size(); + sprintf(axmodel_path, "init post axmodel ok,remain_cmm(%d MB)", remain_cmm); + update_cqdm(&cqdm, attr.axmodel_num + 1, "count", axmodel_path); + } +#endif + printf("\n"); + { + _attr.max_token_len = llama_layers[0].layer.get_input("mask").nSize / sizeof(unsigned short) - 1; + ALOGI("max_token_len : %d", _attr.max_token_len); + _attr.kv_cache_size = llama_layers[0].layer.get_output("K_cache_out").nSize / sizeof(unsigned short); + _attr.kv_cache_num = llama_layers[0].layer.get_input("K_cache").nSize / _attr.kv_cache_size / sizeof(unsigned short); + ALOGI("kv_cache_size : %d, kv_cache_num: %d", _attr.kv_cache_size, _attr.kv_cache_num); + if (_attr.max_token_len > _attr.kv_cache_num) { ALOGE("max_token_len(%d) > kv_cache_num(%d)", _attr.max_token_len, _attr.kv_cache_num); return false; } + _attr.prefill_token_num = llama_layers[0].layer.get_input(1, "indices").vShape[1]; + ALOGI("prefill_token_num : %d", _attr.prefill_token_num); + for (size_t i = 0; i < llama_layers[0].layer.get_num_input_groups() - 1; i++) + { + int n = llama_layers[0].layer.get_input((int)i + 1, "K_cache").vShape[1]; + ALOGI("grp: %zu, prefill_max_kv_cache_num : %d", i + 1, n); + _attr.prefill_max_kv_cache_num_grp.push_back(n); + } + _attr.prefill_max_token_num = _attr.prefill_max_kv_cache_num_grp.back(); + _attr.prefill_grpid = (int)_attr.prefill_max_kv_cache_num_grp.size(); + ALOGI("prefill_max_token_num : %d", _attr.prefill_max_token_num); + } + + // embed file check → then init + { + auto &t_in0 = llama_layers[0].layer.get_input(decode_grpid, "input"); + int model_embed_sz = t_in0.nSize / (int)sizeof(unsigned short); + if (model_embed_sz != _attr.tokens_embed_size) + { + ALOGE("tokens_embed_size mismatch: config(%d) != model(%d). Please fix config or embed file.", _attr.tokens_embed_size, model_embed_sz); + return false; + } + if (!embed_selector.Init(_attr.filename_tokens_embed, _attr.tokens_embed_num, _attr.tokens_embed_size, _attr.b_use_mmap_load_embed)) + { + ALOGE("embed_selector.Init(%s, %d, %d) failed", _attr.filename_tokens_embed.c_str(), _attr.tokens_embed_num, _attr.tokens_embed_size); + return false; + } + update_cqdm(&cqdm, attr.axmodel_num + 2, "count", "embed_selector init ok"); + } + printf("\n"); + + // Optional VLM vision encoder (runtime controlled by attr.vlm_type). + has_vision_state = false; + if (_attr.vlm_type != VLMType::None) + { + vision.reset(new vision::VisionModule()); + std::string verr; + int vdevid = +#ifdef USE_AXCL + llama_layers[0].layer.get_devid(); +#else + -1; +#endif + + if (!vision->Init(_attr.vlm_type, + _attr.filename_image_encoder_axmodel, + _attr.vision_cache_dir, + _attr.tokens_embed_size, + vdevid, + tokenizer, + _attr.vision_width, + _attr.vision_height, + _attr.vision_temporal_patch_size, + _attr.vision_spatial_merge_size, + _attr.vision_patch_size, + _attr.vision_fps, + _attr.vision_tokens_per_second, + verr)) + { + ALOGE("vision.Init(vlm_type=%s/%d) failed: %s", + std::string(VLMTypeName(_attr.vlm_type)).c_str(), + (int)_attr.vlm_type, + verr.c_str()); + return false; + } + } + + if (!postprocess.load_config(attr.post_config_path)) { ALOGW("load postprocess config(%s) failed", attr.post_config_path.c_str()); } + ALOGI("LLM init ok"); + return true; + } + + void Deinit() + { + for (int i = 0; i < _attr.axmodel_num; i++) llama_layers[i].layer.deinit(); + llama_post.deinit(); + embed_selector.Deinit(); + if (vision) vision->Deinit(); +#ifdef USE_AXCL + for (auto &devid : _attr.dev_ids) axcl_Exit(devid); +#endif + } + + void Stop() { b_stop = true; } + + int GenerateKVCachePrefill(std::vector &_token_ids, + std::vector> &k_caches, + std::vector> &v_caches, + int &prefill_precompute_len) + { + bfloat16 bf16 = -65536.f; + int input_embed_num = (int)_token_ids.size(); + prefill_precompute_len = input_embed_num; + k_caches.resize(_attr.axmodel_num); + v_caches.resize(_attr.axmodel_num); + + int prefill_split_num = (int)ceil((double)input_embed_num / _attr.prefill_token_num); + int prefill_grpid = (int)_attr.prefill_max_kv_cache_num_grp.size(); + for (size_t i = 0; i < _attr.prefill_max_kv_cache_num_grp.size(); i++) { if (input_embed_num <= _attr.prefill_max_kv_cache_num_grp[i]) { prefill_grpid = (int)i + 1; break; } } + ALOGI("input token num : %d, prefill_split_num : %d prefill_grpid : %d", input_embed_num, prefill_split_num, prefill_grpid); + + for (int i = 0; i < _attr.axmodel_num; i++) + { + auto &lyr = llama_layers[i]; + int devid = LLM_DEVID(lyr); + llm_memset(LLM_WADDR(lyr.layer.get_input(prefill_grpid, "K_cache")), 0, lyr.layer.get_input(prefill_grpid, "K_cache").nSize, devid); + llm_memset(LLM_WADDR(lyr.layer.get_input(prefill_grpid, "V_cache")), 0, lyr.layer.get_input(prefill_grpid, "V_cache").nSize, devid); + } + + if (input_embed_num == 0) + { + for (int i = 0; i < _attr.axmodel_num; i++) { k_caches[i].resize(prefill_precompute_len * _attr.kv_cache_size); v_caches[i].resize(prefill_precompute_len * _attr.kv_cache_size); } + ALOGI("input token num is 0, skip"); + return 0; + } + + int kv_cache_num = _attr.prefill_max_kv_cache_num_grp[prefill_grpid - 1]; + std::vector test_embed(_token_ids.size() * _attr.tokens_embed_size); + for (size_t i = 0; i < _token_ids.size(); i++) embed_selector.getByIndex(_token_ids[i], test_embed.data() + i * _attr.tokens_embed_size); + + for (int p = 0; p < prefill_split_num; p++) + { + int input_num_token = (p == prefill_split_num - 1) ? input_embed_num - p * _attr.prefill_token_num : _attr.prefill_token_num; + std::vector mask_tmp(_attr.prefill_token_num * (kv_cache_num + _attr.prefill_token_num), bf16.data); + for (int i = 0; i < _attr.prefill_token_num; i++) if (i < input_num_token) + { + auto mask_ptr = mask_tmp.data() + i * (kv_cache_num + _attr.prefill_token_num); + for (int j = 0; j < p * _attr.prefill_token_num; j++) mask_ptr[j] = 0; + int cur_start = kv_cache_num; for (int j = cur_start; j < cur_start + i + 1; j++) mask_ptr[j] = 0; + } + std::vector embed_tmp(_attr.prefill_token_num * _attr.tokens_embed_size, 0); + size_t copy_tokens = (p == prefill_split_num - 1) ? (size_t)(input_embed_num - p * _attr.prefill_token_num) : (size_t)_attr.prefill_token_num; + memcpy(embed_tmp.data(), test_embed.data() + p * _attr.prefill_token_num * _attr.tokens_embed_size, copy_tokens * _attr.tokens_embed_size * sizeof(unsigned short)); + + for (int m = 0; m < _attr.axmodel_num; m++) + { + auto &lyr = llama_layers[m]; int devid = LLM_DEVID(lyr); + auto &t_idx = lyr.layer.get_input(prefill_grpid, "indices"); + unsigned int *idx_ptr = (unsigned int *)t_idx.pVirAddr; memset(idx_ptr, 0, t_idx.nSize); + int idx_i = 0; for (int i = 0; i < input_num_token; ++i) idx_ptr[idx_i++] = (unsigned int)(p * _attr.prefill_token_num + i); + llm_h2d(LLM_WADDR(t_idx), idx_ptr, t_idx.nSize, devid); + auto &t_mask = lyr.layer.get_input(prefill_grpid, "mask"); llm_h2d(LLM_WADDR(t_mask), mask_tmp.data(), mask_tmp.size() * sizeof(unsigned short), devid); + auto &t_in = lyr.layer.get_input(prefill_grpid, "input"); llm_h2d(LLM_WADDR(t_in), embed_tmp.data(), embed_tmp.size() * sizeof(unsigned short), devid); + lyr.layer.inference(prefill_grpid); + auto &out_k = lyr.layer.get_output(prefill_grpid, "K_cache_out"); + auto &out_v = lyr.layer.get_output(prefill_grpid, "V_cache_out"); + auto &pre_k = lyr.layer.get_input(prefill_grpid, "K_cache"); + auto &pre_v = lyr.layer.get_input(prefill_grpid, "V_cache"); + int kv_off = p * _attr.prefill_token_num * _attr.kv_cache_size; + size_t kv_sz = (size_t)_attr.prefill_token_num * _attr.kv_cache_size * sizeof(unsigned short); + llm_d2d((unsigned short *)LLM_WADDR(pre_k) + kv_off, LLM_RADDR(out_k), kv_sz, devid); + llm_d2d((unsigned short *)LLM_WADDR(pre_v) + kv_off, LLM_RADDR(out_v), kv_sz, devid); + auto &t_out = lyr.layer.get_output(prefill_grpid, "output"); llm_d2h(embed_tmp.data(), LLM_RADDR(t_out), embed_tmp.size() * sizeof(unsigned short), devid); + } + } + + for (int i = 0; i < _attr.axmodel_num; i++) + { + auto &lyr = llama_layers[i]; int devid = LLM_DEVID(lyr); + k_caches[i].resize(prefill_precompute_len * _attr.kv_cache_size); + v_caches[i].resize(prefill_precompute_len * _attr.kv_cache_size); + auto &t_k = lyr.layer.get_input(prefill_grpid, "K_cache"); + auto &t_v = lyr.layer.get_input(prefill_grpid, "V_cache"); + llm_d2h(k_caches[i].data(), LLM_RADDR(t_k), prefill_precompute_len * _attr.kv_cache_size * sizeof(unsigned short), devid); + llm_d2h(v_caches[i].data(), LLM_RADDR(t_v), prefill_precompute_len * _attr.kv_cache_size * sizeof(unsigned short), devid); + } + return 0; + } + + int GetKVCache(std::vector> &kv_k, std::vector> &kv_v, int &kv_precompute_len) + { + bfloat16 bf16 = -65536.f; + auto &t_mask = llama_layers[0].layer.get_input(decode_grpid, "mask"); + std::vector mask(t_mask.nSize / sizeof(unsigned short), bf16.data); + llm_d2h(mask.data(), LLM_RADDR(t_mask), t_mask.nSize, LLM_DEVID(llama_layers[0])); + kv_precompute_len = 0; for (size_t i = 0; i < mask.size(); i++) { if (mask[i] == bf16.data) { kv_precompute_len = (int)i + 1; break; } } + ALOGI("precompute_len:%d, remaining:%d", kv_precompute_len, _attr.prefill_max_kv_cache_num_grp.back() - kv_precompute_len); + if (b_os_kvcache) + { + kv_k.resize(_attr.axmodel_num); kv_v.resize(_attr.axmodel_num); + for (int i = 0; i < _attr.axmodel_num; i++) + { + auto &lyr = llama_layers[i]; int devid = LLM_DEVID(lyr); + kv_k[i].resize(kv_precompute_len * _attr.kv_cache_size); kv_v[i].resize(kv_precompute_len * _attr.kv_cache_size); + auto &t_k = lyr.layer.get_input(decode_grpid, "K_cache"); auto &t_v = lyr.layer.get_input(decode_grpid, "V_cache"); + llm_d2h(kv_k[i].data(), LLM_RADDR(t_k), kv_precompute_len * _attr.kv_cache_size * sizeof(unsigned short), devid); + llm_d2h(kv_v[i].data(), LLM_RADDR(t_v), kv_precompute_len * _attr.kv_cache_size * sizeof(unsigned short), devid); + } + } + _attr.prefill_max_token_num = _attr.prefill_max_kv_cache_num_grp.back(); + return 0; + } + + int SetKVCache(std::vector> &kv_k, + std::vector> &kv_v, + int _precompute_len, int input_num_token) + { + for (size_t i = 0; i < _attr.prefill_max_kv_cache_num_grp.size(); i++) + { + if (_precompute_len + input_num_token <= _attr.prefill_max_kv_cache_num_grp[i]) { _attr.prefill_grpid = (int)i + 1; break; } + } + int kv_cache_num = _attr.prefill_max_kv_cache_num_grp[_attr.prefill_grpid - 1]; + ALOGI("prefill_grpid:%d kv_cache_num:%d precompute_len:%d input_num_token:%d", _attr.prefill_grpid, kv_cache_num, _precompute_len, input_num_token); + _attr.prefill_max_token_num = ALIGN_DOWN(_attr.prefill_max_token_num - _precompute_len, _attr.prefill_token_num); + ALOGI("current prefill_max_token_num:%d", _attr.prefill_max_token_num); + if (_precompute_len == 0) { ALOGI("first run"); return 0; } + if (_precompute_len + input_num_token > kv_cache_num) { ALOGE("precompute_len(%d) + input_num_token(%d) > kv_cache_num(%d)", _precompute_len, input_num_token, kv_cache_num); return -1; } + if (input_num_token > _attr.prefill_max_token_num) { ALOGE("input_num_token(%d) > prefill_max_token_num(%d)", input_num_token, _attr.prefill_max_token_num); return -1; } + if (!b_os_kvcache) return 0; + if (kv_k.size() != kv_v.size() || (int)kv_k.size() != _attr.axmodel_num) { ALOGE("kv cache size mismatch"); return -1; } + for (int i = 0; i < _attr.axmodel_num; i++) + { + auto &lyr = llama_layers[i]; int devid = LLM_DEVID(lyr); + auto &dk = lyr.layer.get_input(decode_grpid, "K_cache"); auto &dv = lyr.layer.get_input(decode_grpid, "V_cache"); + llm_memset(LLM_WADDR(dk), 0, dk.nSize, devid); llm_memset(LLM_WADDR(dv), 0, dv.nSize, devid); + auto &pk = lyr.layer.get_input(_attr.prefill_grpid, "K_cache"); auto &pv = lyr.layer.get_input(_attr.prefill_grpid, "V_cache"); + llm_memset(LLM_WADDR(pk), 0, pk.nSize, devid); llm_memset(LLM_WADDR(pv), 0, pv.nSize, devid); + } + size_t kv_bytes = (size_t)_precompute_len * _attr.kv_cache_size * sizeof(unsigned short); + for (int m = 0; m < _attr.axmodel_num; m++) + { + auto &lyr = llama_layers[m]; int devid = LLM_DEVID(lyr); + auto &kc = kv_k[m]; auto &vc = kv_v[m]; + if ((int)kc.size() < _precompute_len * _attr.kv_cache_size || (int)vc.size() < _precompute_len * _attr.kv_cache_size) { ALOGE("kv_cache buffer too small for layer %d", m); return -1; } + auto &dk = lyr.layer.get_input(decode_grpid, "K_cache"); auto &dv = lyr.layer.get_input(decode_grpid, "V_cache"); + llm_h2d(LLM_WADDR(dk), kc.data(), kv_bytes, devid); llm_h2d(LLM_WADDR(dv), vc.data(), kv_bytes, devid); + auto &pk = lyr.layer.get_input(_attr.prefill_grpid, "K_cache"); auto &pv = lyr.layer.get_input(_attr.prefill_grpid, "V_cache"); + llm_h2d(LLM_WADDR(pk), kc.data(), kv_bytes, devid); llm_h2d(LLM_WADDR(pv), vc.data(), kv_bytes, devid); + } + return 0; + } + + void ResetKVCache() + { + last_tokens_ids.clear(); k_caches.clear(); v_caches.clear(); precompute_len = 0; + for (int i = 0; i < _attr.axmodel_num; i++) + { + auto &lyr = llama_layers[i]; int devid = LLM_DEVID(lyr); + auto &dk = lyr.layer.get_input(decode_grpid, "K_cache"); auto &dv = lyr.layer.get_input(decode_grpid, "V_cache"); + llm_memset(LLM_WADDR(dk), 0, dk.nSize, devid); llm_memset(LLM_WADDR(dv), 0, dv.nSize, devid); + } + } + + std::string Run(std::vector &test_embed, int output_max_token = -1) + { + b_stop = false; std::string final_out; + bfloat16 bf16 = -65536.f; + std::vector mask(_attr.kv_cache_num + 1, bf16.data); + std::vector embed(_attr.tokens_embed_size, 0); + int kv_cache_num = _attr.prefill_max_kv_cache_num_grp[_attr.prefill_grpid - 1]; + std::vector token_ids; + int input_embed_num = (int)(test_embed.size() / _attr.tokens_embed_size); + int prefill_split_num = (int)ceil((double)input_embed_num / _attr.prefill_token_num); + ALOGI("input token num : %d, prefill_split_num : %d", input_embed_num, prefill_split_num); + mask[_attr.kv_cache_num] = 0; for (int i = 0; i < precompute_len + input_embed_num; i++) mask[i] = 0; + timer t_cost, ttft_timer; ttft_timer.start(); + + // Prefill + // Pre-compute which prefill group id we will use per chunk, so we can propagate KV caches + // to the future groups (axcl-qwen3-vl behavior). + std::vector prefill_grp_list; + prefill_grp_list.resize(prefill_split_num, 1); + int max_prefill_gid = 1; + for (int p = 0; p < prefill_split_num; ++p) { + const int history_len = precompute_len + p * _attr.prefill_token_num; + int g = 1; + for (size_t gi = 0; gi < _attr.prefill_max_kv_cache_num_grp.size(); ++gi) { + if (history_len <= _attr.prefill_max_kv_cache_num_grp[gi]) { g = (int)gi + 1; break; } + } + prefill_grp_list[p] = g; + if (g > max_prefill_gid) max_prefill_gid = g; + } + + for (int p = 0; p < prefill_split_num; p++) + { + if (b_stop) break; + int input_num_token = (p == prefill_split_num - 1) ? input_embed_num - p * _attr.prefill_token_num : _attr.prefill_token_num; + const int history_len = precompute_len + p * _attr.prefill_token_num; + + // Pick the smallest prefill group that can cover `history_len` cached tokens. + // Qwen-VL models are sensitive to group/kv_cache_num mismatch. + const int prefill_grpid = prefill_grp_list[p]; + // Derive kv_cache_num from mask tensor shape when possible (more reliable than K_cache vShape on some models). + int kv_cache_num_p = _attr.prefill_max_kv_cache_num_grp[prefill_grpid - 1]; + { + const auto &mask_t = llama_layers[0].layer.get_input(prefill_grpid, "mask"); + const int mask_elems = (int)(mask_t.nSize / (int)sizeof(unsigned short)); + if (_attr.prefill_token_num > 0 && mask_elems > 0 && (mask_elems % _attr.prefill_token_num) == 0) { + const int cols = mask_elems / _attr.prefill_token_num; + const int kv_from_mask = cols - _attr.prefill_token_num; + if (kv_from_mask >= 0) kv_cache_num_p = kv_from_mask; + } + } + ALOGI("prefill chunk p=%d history_len=%d grpid=%d kv_cache_num=%d input_tokens=%d", + p, history_len, prefill_grpid, kv_cache_num_p, input_num_token); + + std::vector embed_tmp(_attr.prefill_token_num * _attr.tokens_embed_size, 0); + std::vector mask_tmp(_attr.prefill_token_num * (kv_cache_num_p + _attr.prefill_token_num), bf16.data); + + build_prefill_mask(mask_tmp, kv_cache_num_p, _attr.prefill_token_num, history_len, input_num_token); + size_t copy_tokens = (p == prefill_split_num - 1) ? (size_t)(input_embed_num - p * _attr.prefill_token_num) : (size_t)_attr.prefill_token_num; + memcpy(embed_tmp.data(), test_embed.data() + p * _attr.prefill_token_num * _attr.tokens_embed_size, copy_tokens * _attr.tokens_embed_size * sizeof(unsigned short)); + + for (int m = 0; m < _attr.axmodel_num; m++) + { + if (b_stop) break; + auto &lyr = llama_layers[m]; int devid = LLM_DEVID(lyr); + auto &t_idx = lyr.layer.get_input(prefill_grpid, "indices"); + unsigned int *idx_ptr = (unsigned int *)t_idx.pVirAddr; memset(idx_ptr, 0, t_idx.nSize); + { + const int start_pos = history_len; + const int idx_elems = (int)(t_idx.nSize / (int)sizeof(unsigned int)); + int idx_rows = idx_elems / _attr.prefill_token_num; + if (idx_rows <= 0) idx_rows = 1; + if (m == 0) { + ALOGI("prefill indices shape: p=%d idx_elems=%d idx_rows=%d pos_rows=%zu", + p, idx_elems, idx_rows, vision_state.position_ids.size()); + } + + const bool use_pos_ids = has_vision_state && + idx_rows >= 3 && + vision_state.position_ids.size() >= 3 && + !vision_state.position_ids.empty(); + + for (int r = 0; r < idx_rows; ++r) + { + for (int j = 0; j < input_num_token; ++j) + { + unsigned int v = (unsigned int)(start_pos + j); + if (use_pos_ids) + { + if ((size_t)r < vision_state.position_ids.size()) + { + const auto &row = vision_state.position_ids[r]; + if ((size_t)(start_pos + j) < row.size()) + v = (unsigned int)row[start_pos + j]; + } + } + idx_ptr[r * _attr.prefill_token_num + j] = v; + } + } + } + llm_h2d(LLM_WADDR(t_idx), idx_ptr, t_idx.nSize, devid); + auto &t_mask = lyr.layer.get_input(prefill_grpid, "mask"); llm_h2d(LLM_WADDR(t_mask), mask_tmp.data(), mask_tmp.size() * sizeof(unsigned short), devid); + auto &t_in = lyr.layer.get_input(prefill_grpid, "input"); llm_h2d(LLM_WADDR(t_in), embed_tmp.data(), embed_tmp.size() * sizeof(unsigned short), devid); + lyr.layer.inference(prefill_grpid); + auto &out_k = lyr.layer.get_output(prefill_grpid, "K_cache_out"); + auto &out_v = lyr.layer.get_output(prefill_grpid, "V_cache_out"); + auto &dec_k = lyr.layer.get_input(decode_grpid, "K_cache"); + auto &dec_v = lyr.layer.get_input(decode_grpid, "V_cache"); + int kv_off = history_len * _attr.kv_cache_size; + size_t kv_sz = (size_t)input_num_token * _attr.kv_cache_size * sizeof(unsigned short); + // Sync current prefill chunk K/V into decode group on both AXCL and AX650. + // Missing this causes decode stage to ignore prefill history (AX650 output degrades badly). + llm_d2d((unsigned short *)LLM_WADDR(dec_k) + kv_off, LLM_RADDR(out_k), kv_sz, devid); + llm_d2d((unsigned short *)LLM_WADDR(dec_v) + kv_off, LLM_RADDR(out_v), kv_sz, devid); + // axcl-qwen3-vl behavior: do not write back to the current prefill group + // (group-1 K/V cache capacity can be much smaller than one prefill chunk). + // Only propagate to future prefill groups so the next chunk can reuse history. + const int ng = (int)lyr.layer.get_num_input_groups(); + const int max_gid = std::min(max_prefill_gid, ng - 1); + for (int gid = prefill_grpid + 1; gid <= max_gid; ++gid) { + auto &gk = lyr.layer.get_input(gid, "K_cache"); + auto &gv = lyr.layer.get_input(gid, "V_cache"); + const int cap_tokens_k = (int)(gk.nSize / (size_t)(_attr.kv_cache_size * (int)sizeof(unsigned short))); + const int cap_tokens_v = (int)(gv.nSize / (size_t)(_attr.kv_cache_size * (int)sizeof(unsigned short))); + if (kv_off + input_num_token <= cap_tokens_k) { + llm_d2d((unsigned short *)LLM_WADDR(gk) + kv_off, LLM_RADDR(out_k), kv_sz, devid); + } + if (kv_off + input_num_token <= cap_tokens_v) { + llm_d2d((unsigned short *)LLM_WADDR(gv) + kv_off, LLM_RADDR(out_v), kv_sz, devid); + } + } + + auto &t_out = lyr.layer.get_output(prefill_grpid, "output"); + llm_d2h(embed_tmp.data(), LLM_RADDR(t_out), embed_tmp.size() * sizeof(unsigned short), devid); + + // Optional Qwen3VL "deepstack" feature addition (legacy branches behavior). + if (has_vision_state && + !vision_state.deepstack_features.empty() && + (size_t)m < vision_state.deepstack_features.size() && + !vision_state.deepstack_features[m].empty()) + { + const int start_pos = history_len; + const int emb_sz = _attr.tokens_embed_size; + const auto &feat = vision_state.deepstack_features[m]; + + for (int j = 0; j < input_num_token; ++j) + { + const int abs_pos = start_pos + j; + if ((size_t)abs_pos >= vision_state.pos2vision.size()) continue; + const int vidx = vision_state.pos2vision[abs_pos]; + if (vidx < 0) continue; + + const float *fv = feat.data() + (size_t)vidx * (size_t)emb_sz; + unsigned short *ev = embed_tmp.data() + (size_t)j * (size_t)emb_sz; + + for (int di = 0; di < emb_sz; ++di) + { + // bf16 -> fp32 + unsigned int tmp_bf16 = ((unsigned int)ev[di]) << 16; + float fp32 = *reinterpret_cast(&tmp_bf16); + // fp32 + deepstack -> bf16 + ev[di] = bfloat16(fp32 + fv[di]).data; + } + } + } + } + + if (p == prefill_split_num - 1) + memcpy(embed.data(), embed_tmp.data() + (input_embed_num - p * _attr.prefill_token_num - 1) * _attr.tokens_embed_size, _attr.tokens_embed_size * sizeof(unsigned short)); + } + + int next_token = -1; t_cqdm cqdm = create_cqdm(_attr.max_token_len, 32); + { + auto &t_in = llama_post.get_input("input"); + llm_h2d(LLM_WADDR(t_in), embed.data(), embed.size() * sizeof(unsigned short), LLM_DEVID(llama_layers.back())); + llama_post.inference(); + auto &t_out = llama_post.get_output("output"); + llm_d2h(t_out.pVirAddr, LLM_RADDR(t_out), t_out.nSize, llama_post.get_devid()); + unsigned short *post_out = (unsigned short *)t_out.pVirAddr; + next_token = post_process(postprocess, post_out, _attr.tokens_embed_num, token_ids, nullptr); + token_ids.push_back(next_token); + ALOGI("ttft: %.2f ms", ttft_timer.cost()); + if (_attr.runing_callback) + { + auto str = utf8_filter.filter(tokenizer->decode(next_token)); + if (!str.empty()) _attr.runing_callback(str, -1, _attr.reserve); + } + } + + t_cost.start(); bool b_hit_eos = false; + unsigned int decode_start = (unsigned int)(precompute_len + input_embed_num); + if (has_vision_state && vision_state.decode_start > 0) decode_start = (unsigned int)vision_state.decode_start; + for (unsigned int indices = decode_start; indices < (unsigned int)_attr.max_token_len; indices++) + { + if (b_stop) break; + embed_selector.getByIndex(next_token, embed); + +#ifdef USE_AXCL + { + auto &l0_in = llama_layers[0].layer.get_input(decode_grpid, "input"); + llm_h2d(LLM_WADDR(l0_in), embed.data(), l0_in.nSize, llama_layers[0].layer.get_devid()); + } + for (int m = 0; m < _attr.axmodel_num; m++) + { + if (b_stop) break; auto &lyr = llama_layers[m]; int devid = lyr.layer.get_devid(); + auto &t_idx = lyr.layer.get_input(decode_grpid, "indices"); llm_h2d(LLM_WADDR(t_idx), &indices, sizeof(indices), devid); + auto &t_mask= lyr.layer.get_input(decode_grpid, "mask"); llm_h2d(LLM_WADDR(t_mask), mask.data(), mask.size() * sizeof(unsigned short), devid); + lyr.layer.inference(decode_grpid); + auto &out_k = lyr.layer.get_output(decode_grpid, "K_cache_out"); auto &out_v = lyr.layer.get_output(decode_grpid, "V_cache_out"); + auto &in_k = lyr.layer.get_input(decode_grpid, "K_cache"); auto &in_v = lyr.layer.get_input(decode_grpid, "V_cache"); + llm_d2d((unsigned short *)LLM_WADDR(in_k) + indices * _attr.kv_cache_size, LLM_RADDR(out_k), out_k.nSize, devid); + llm_d2d((unsigned short *)LLM_WADDR(in_v) + indices * _attr.kv_cache_size, LLM_RADDR(out_v), out_v.nSize, devid); + auto &cur_out = lyr.layer.get_output(decode_grpid, "output"); + if (m == _attr.axmodel_num - 1) + { + auto &post_in = llama_post.get_input("input"); + if (llama_post.get_devid() == devid) { llm_d2d(LLM_WADDR(post_in), LLM_RADDR(cur_out), post_in.nSize, devid); } + else { llm_d2h(cur_out.pVirAddr, LLM_RADDR(cur_out), cur_out.nSize, devid); llm_h2d(LLM_WADDR(post_in), cur_out.pVirAddr, post_in.nSize, llama_post.get_devid()); } + } + else + { + auto &next_in = llama_layers[m + 1].layer.get_input(decode_grpid, "input"); int next_devid = llama_layers[m + 1].layer.get_devid(); + if (next_devid == devid) { llm_d2d(LLM_WADDR(next_in), LLM_RADDR(cur_out), next_in.nSize, devid); } + else { llm_d2h(cur_out.pVirAddr, LLM_RADDR(cur_out), cur_out.nSize, devid); llm_h2d(LLM_WADDR(next_in), cur_out.pVirAddr, next_in.nSize, next_devid); } + } + } + llama_post.inference(); + { + auto &t_out = llama_post.get_output("output"); llm_d2h(t_out.pVirAddr, LLM_RADDR(t_out), t_out.nSize, llama_post.get_devid()); + unsigned short *post_out = (unsigned short *)t_out.pVirAddr; next_token = post_process(postprocess, post_out, _attr.tokens_embed_num, token_ids, nullptr); + } +#else // AX650 + for (int m = 0; m < _attr.axmodel_num; m++) + { + if (b_stop) break; auto &lyr = llama_layers[m]; + auto &in_k = lyr.layer.get_input(decode_grpid, "K_cache"); auto *in_k_ptr = (unsigned short *)in_k.pVirAddr; + auto &in_v = lyr.layer.get_input(decode_grpid, "V_cache"); auto *in_v_ptr = (unsigned short *)in_v.pVirAddr; + auto &t_idx = lyr.layer.get_input(decode_grpid, "indices"); memcpy(t_idx.pVirAddr, &indices, sizeof(indices)); + auto &t_mask= lyr.layer.get_input(decode_grpid, "mask"); memcpy(t_mask.pVirAddr, mask.data(), mask.size() * sizeof(unsigned short)); + auto &t_in = lyr.layer.get_input(decode_grpid, "input"); memcpy(t_in.pVirAddr, embed.data(), embed.size() * sizeof(unsigned short)); + lyr.layer.inference(decode_grpid); + auto &out_k = lyr.layer.get_output(decode_grpid, "K_cache_out"); memcpy(in_k_ptr + indices * _attr.kv_cache_size, out_k.pVirAddr, sizeof(unsigned short) * _attr.kv_cache_size); + auto &out_v = lyr.layer.get_output(decode_grpid, "V_cache_out"); memcpy(in_v_ptr + indices * _attr.kv_cache_size, out_v.pVirAddr, sizeof(unsigned short) * _attr.kv_cache_size); + auto &t_out= lyr.layer.get_output(decode_grpid, "output"); memcpy(embed.data(), t_out.pVirAddr, embed.size() * sizeof(unsigned short)); + } + auto &t_in = llama_post.get_input("input"); memcpy(t_in.pVirAddr, embed.data(), embed.size() * sizeof(unsigned short)); + llama_post.inference(); auto &t_out = llama_post.get_output("output"); + unsigned short *post_out = (unsigned short *)t_out.pVirAddr; next_token = post_process(postprocess, post_out, _attr.tokens_embed_num, token_ids, nullptr); +#endif + + mask[indices] = 0; + if (tokenizer->is_stop(next_token)) { b_hit_eos = true; break; } + token_ids.push_back(next_token); + if (_attr.runing_callback) + { + float t_ms = t_cost.cost(); float tps = token_ids.size() / (t_ms / 1000.0f); + auto str = utf8_filter.filter(tokenizer->decode(next_token)); + if (!str.empty()) _attr.runing_callback(str, tps, _attr.reserve); + } + if (output_max_token > 0 && (int)token_ids.size() >= output_max_token) { b_hit_eos = true; break; } + if (_attr.runing_callback == nullptr) update_cqdm(&cqdm, indices, "token", ""); + } + + printf("\n\n"); fflush(stdout); float t_ms = t_cost.cost(); ALOGN("hit eos,avg %.2f token/s\n", token_ids.size() / (t_ms / 1000.0f)); + final_out = tokenizer->decode(token_ids); return final_out; + } + + std::vector Run(std::vector history, int output_max_token = -1) + { + return Run(std::move(history), {}, output_max_token); + } + + std::vector Run(std::vector history, const std::vector<::MediaInputs> &media_inputs, int output_max_token = -1) + { + has_vision_state = false; + + std::vector new_tokens; + + if (vision && vision->enabled()) + { + // If caller provides media, we will fill num_media/num_media_tokens and build injection state. + if (!media_inputs.empty()) + { + std::vector vmins; + vmins.reserve(media_inputs.size()); + for (const auto &m : media_inputs) vmins.push_back({m.content_index, m.uris}); + + std::vector prepared_history; + std::vector input_ids; + vision::RunState st; + std::string verr; + if (!vision->Prepare(history, vmins, prepared_history, input_ids, st, verr)) + { + ALOGE("vision.Prepare failed: %s", verr.c_str()); + return history; + } + history = std::move(prepared_history); + new_tokens = std::move(input_ids); + vision_state = std::move(st); + has_vision_state = true; + } + else + { + // If history contains IMAGE/VIDEO, the caller must provide media inputs. + bool need_media = false; + for (const auto &c : history) if (c.type == IMAGE || c.type == VIDEO) { need_media = true; break; } + if (need_media) + { + ALOGE("vlm_type=%s/%d enabled but media_inputs is empty", + std::string(VLMTypeName(_attr.vlm_type)).c_str(), + (int)_attr.vlm_type); + } + new_tokens = tokenizer->encode(history); + } + } + else + { + new_tokens = tokenizer->encode(history); + } + + int offset = 0; auto tokens_diff = diff_token_ids(last_tokens_ids, new_tokens, offset); + bool not_append = !(offset == (int)last_tokens_ids.size() && (int)new_tokens.size() >= (int)last_tokens_ids.size()); + if (not_append) { ALOGW("history not append (rollback/modify). force ResetKVCache and recompute."); ResetKVCache(); tokens_diff = new_tokens; offset = 0; } + if (tokens_diff.empty()) + { + if (!new_tokens.empty()) { precompute_len = (int)new_tokens.size() - 1; tokens_diff = {new_tokens.back()}; } + else { ResetKVCache(); precompute_len = 0; } + } + SetKVCache(k_caches, v_caches, precompute_len, (int)tokens_diff.size()); + std::vector out_embed(tokens_diff.size() * _attr.tokens_embed_size); + for (size_t i = 0; i < tokens_diff.size(); i++) + { + const int abs_pos = offset + (int)i; + if (has_vision_state && (size_t)abs_pos < vision_state.pos2vision.size()) + { + int vidx = vision_state.pos2vision[abs_pos]; + if (vidx >= 0) + { + memcpy(out_embed.data() + i * _attr.tokens_embed_size, + vision_state.vision_embed.data() + (size_t)vidx * _attr.tokens_embed_size, + (size_t)_attr.tokens_embed_size * sizeof(unsigned short)); + continue; + } + } + embed_selector.getByIndex(tokens_diff[i], out_embed.data() + i * _attr.tokens_embed_size); + } + auto reply = Run(out_embed, output_max_token); + GetKVCache(k_caches, v_caches, precompute_len); + history.push_back({ASSISTANT, TEXT, reply}); + last_tokens_ids = tokenizer->encode(history); + if (last_tokens_ids.size() >= 2) last_tokens_ids.erase(last_tokens_ids.end() - 2, last_tokens_ids.end()); + + has_vision_state = false; + vision_state = {}; + + return history; + } +}; + +// Public LLM thin wrappers + +LLM::LLM() : impl_(new Impl()) {} +LLM::~LLM() = default; + +bool LLM::Init(LLMAttrType attr) { return impl_->Init(std::move(attr)); } +void LLM::Deinit() { impl_->Deinit(); } +void LLM::Stop() { impl_->Stop(); } + +LLMAttrType *LLM::getAttr() { return &impl_->_attr; } +LLMPostprocess *LLM::getPostprocess() { return &impl_->postprocess; } +LLaMaEmbedSelector *LLM::getEmbedSelector() { return &impl_->embed_selector; } + +int LLM::GenerateKVCachePrefill(std::vector &ids, std::vector> &k, std::vector> &v, int &pre_len) { return impl_->GenerateKVCachePrefill(ids, k, v, pre_len); } +int LLM::GetKVCache(std::vector> &k, std::vector> &v, int &pre_len) { return impl_->GetKVCache(k, v, pre_len); } +int LLM::SetKVCache(std::vector> &k, std::vector> &v, int pre_len, int in_tokens) { return impl_->SetKVCache(k, v, pre_len, in_tokens); } +void LLM::ResetKVCache() { impl_->ResetKVCache(); } + +std::vector LLM::Run(std::vector history, int output_max_token) { return impl_->Run(std::move(history), output_max_token); } +std::vector LLM::Run(std::vector history, const std::vector &media_inputs, int output_max_token) { return impl_->Run(std::move(history), media_inputs, output_max_token); } +std::string LLM::Run(std::vector &embed, int output_max_token) { return impl_->Run(embed, output_max_token); } diff --git a/projects/llm_framework/main_llm2/src/runner/LLM.hpp b/projects/llm_framework/main_llm2/src/runner/LLM.hpp new file mode 100644 index 0000000..9298bf2 --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/LLM.hpp @@ -0,0 +1,119 @@ +#pragma once +#include +#include +#include +#include + +#include "BaseTokenizer.hpp" // for Content/RoleType definitions +#include "VLMType.hpp" + +class LLMPostprocess; +class LLaMaEmbedSelector; + +using LLMRuningCallback = std::function; + +// Multimodal (VLM) media inputs are passed out-of-band, aligned to `history` by index. +// `history[content_index].type` must be IMAGE or VIDEO. +// For IMAGE: each uri can be a file or a directory of images (sorted). +// For VIDEO: uri is typically a directory of frames (sorted). +struct MediaInputs { + size_t content_index = 0; + std::vector uris; +}; + +struct LLMAttrType { + std::string system_prompt; + std::string template_filename_axmodel = "tinyllama-int8/tinyllama_l%d.axmodel"; + int axmodel_num = 22; + + int prefill_token_num = 96; // auto calc + int prefill_max_token_num = 512; + + std::string filename_post_axmodel = "tinyllama-int8/tinyllama_post.axmodel"; + + std::string tokenizer_type; + std::string url_tokenizer_model = "http://127.0.0.1:12345"; + bool b_bos = true, b_eos = false; + std::string filename_tokens_embed = "tinyllama.model.embed_tokens.weight.bfloat16.bin"; + int tokens_embed_num = 32000; + int tokens_embed_size = 2048; + + int max_token_len = 127; // auto calc + int kv_cache_num = 1024; // auto calc + int kv_cache_size = 256; // auto calc + + std::vector prefill_max_kv_cache_num_grp; + int prefill_grpid = -1; + std::string post_config_path = "post_config.json"; + bool b_use_mmap_load_embed = false; + + // ---- vision / VLM (optional, runtime switch by `vlm_type`) ---- + // If `vlm_type != VLMType::None`, vision encoder will be initialized and used. + // See `VLMType` in `src/runner/VLMType.hpp`. + // Names/ids (via magic_enum): `None(0)`, `Qwen2_5VL(1)`, `Qwen3VL(2)`, `InternVL3(3)`, `FastVLM(4)`, `SmolVLM2(5)`. + VLMType vlm_type = VLMType::None; + + // Vision encoder axmodel (image/video encoder). Required if `vlm_type != VLMType::None`. + std::string filename_image_encoder_axmodel = "image_encoder.axmodel"; + + // Optional: vision embedding cache directory. If empty: memory-only cache for the process lifetime. + // If set: read/write encoded embeddings for repeated images across runs. + std::string vision_cache_dir; + + // Qwen-VL patchifier params (also used to compute mRoPE indices). + int vision_width = 448; + int vision_height = 448; + int vision_temporal_patch_size = 2; + int vision_spatial_merge_size = 2; + int vision_patch_size = 14; + int vision_fps = 1; // for qwen2.5-vl time scaling + int vision_tokens_per_second = 1; + +#ifndef USE_AXCL + bool b_use_mmap_load_layer = true; +#endif + +#ifdef USE_AXCL + std::vector dev_ids = {0}; +#endif + + LLMRuningCallback runing_callback = nullptr; + void *reserve = nullptr; +}; + +class LLM { +public: + LLM(); + ~LLM(); + + bool Init(LLMAttrType attr); + void Deinit(); + void Stop(); + + LLMAttrType *getAttr(); + LLMPostprocess *getPostprocess(); + LLaMaEmbedSelector *getEmbedSelector(); + + int GenerateKVCachePrefill(std::vector &ids, + std::vector> &k, + std::vector> &v, + int &pre_len); + + int GetKVCache(std::vector> &k, + std::vector> &v, + int &pre_len); + + int SetKVCache(std::vector> &k, + std::vector> &v, + int precompute_len, int input_num_token); + + void ResetKVCache(); + + std::vector Run(std::vector history, int output_max_token = -1); + std::vector Run(std::vector history, const std::vector &media_inputs, int output_max_token = -1); + std::string Run(std::vector &embed, int output_max_token = -1); + +private: + struct Impl; + std::unique_ptr impl_; +}; diff --git a/projects/llm_framework/main_llm2/src/runner/LLMEmbedSelector.hpp b/projects/llm_framework/main_llm2/src/runner/LLMEmbedSelector.hpp new file mode 100644 index 0000000..33f1553 --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/LLMEmbedSelector.hpp @@ -0,0 +1,113 @@ +#pragma once +#include +#include +#include +#include +#include + +#include "sample_log.h" + +#include "memory_utils.hpp" + +class LLaMaEmbedSelector +{ + MMap _embed_map; + std::vector _embeds; + unsigned int _token_num, _embed_size; + bool _use_mmap = false; + +public: + bool Init(std::string embed_path, unsigned int token_num, unsigned int embed_size, bool use_mmap = false) + { + _token_num = token_num; + _embed_size = embed_size; + _use_mmap = use_mmap; + if (use_mmap) + { + ALOGI("LLaMaEmbedSelector use mmap"); + if (!_embed_map.open_file(embed_path.c_str())) + { + ALOGE("embed file(%s) open failed", embed_path.c_str()); + return false; + } + } + else + { + std::ifstream fin(embed_path, std::ios::binary); + if (!fin.is_open()) + { + ALOGE("embed file(%s) open failed", embed_path.c_str()); + return false; + } + + // get file size + fin.seekg(0, std::ios::end); + std::uint64_t file_size = (std::uint64_t)fin.tellg(); + fin.seekg(0, std::ios::beg); + std::uint64_t expect_size = (std::uint64_t)token_num * (std::uint64_t)embed_size * 2ULL; + if (file_size != expect_size) + { + ALOGE("embed file(%s) size(%llu) not equal token_num(%u) * embed_size(%u) * 2 = %llu", + embed_path.c_str(), (unsigned long long)file_size, token_num, embed_size, (unsigned long long)expect_size); + return false; + } + + _embeds.resize(token_num * embed_size); + fin.read((char *)_embeds.data(), (std::streamsize)expect_size); + fin.close(); + } + + return true; + } + + void Deinit() + { + _embed_map.close_file(); + _embeds.clear(); + } + + void getByIndex(unsigned int index, std::vector &embed) + { + if (index >= _token_num) + { + ALOGE("index(%d) > token_num(%d)", index, _token_num); + return; + } + embed.resize(_embed_size); + if (_use_mmap) + { + unsigned short *ptr = (unsigned short *)_embed_map.data(); + memcpy(embed.data(), ptr + index * _embed_size, _embed_size * sizeof(unsigned short)); + } + else + { + memcpy(embed.data(), _embeds.data() + index * _embed_size, _embed_size * sizeof(unsigned short)); + } + } + + void getByIndex(unsigned int index, unsigned short *embed) + { + if (index >= _token_num) + { + ALOGE("index(%d) > token_num(%d)", index, _token_num); + return; + } + + if (_use_mmap) + { + unsigned short *ptr = (unsigned short *)_embed_map.data(); + memcpy(embed, ptr + index * _embed_size, _embed_size * sizeof(unsigned short)); + } + else + { + memcpy(embed, _embeds.data() + index * _embed_size, _embed_size * sizeof(unsigned short)); + } + } + + std::vector getByIndex(unsigned int index) + { + std::vector embed; + this->getByIndex(index, embed); + return embed; + } +}; diff --git a/projects/llm_framework/main_llm2/src/runner/LLMPostprocess.hpp b/projects/llm_framework/main_llm2/src/runner/LLMPostprocess.hpp new file mode 100644 index 0000000..f744c7f --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/LLMPostprocess.hpp @@ -0,0 +1,333 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include "utils/json.hpp" +#include "utils/sample_log.h" + +class LLMPostprocess +{ +private: + // 控制随机性 + void apply_temperature(std::vector &logits, float temperature) + { + for (float &logit : logits) + { + logit /= temperature; + } + } + + // 防止重复 + void apply_repetition_penalty(std::vector &logits, const std::vector &history, float penalty) + { + for (int token : history) + { + if (token < logits.size()) + { + logits[token] = logits[token] < 0 ? logits[token] * penalty : logits[token] / penalty; + } + } + } + + void apply_repetition_penalty(std::vector &logits, + const std::vector &generated_tokens, + float repetition_penalty, + int penalty_window) + { + if (repetition_penalty == 1.0f || generated_tokens.empty()) + { + return; // 如果 penalty = 1.0 或者没有生成 token,则不进行修改 + } + + int start_idx = std::max(0, (int)generated_tokens.size() - penalty_window); + std::unordered_set recent_tokens(generated_tokens.begin() + start_idx, generated_tokens.end()); + + for (int token : recent_tokens) + { + if (token < 0 || token >= logits.size()) + continue; + + if (logits[token] > 0) + { + logits[token] /= std::sqrt(repetition_penalty); + } + else + { + logits[token] *= std::sqrt(repetition_penalty); + } + } + } + + // 增强多样性 + void apply_diversity_penalty(std::vector &logits, const std::vector &common_phrases, float penalty) + { + for (int token : common_phrases) + { + if (token < logits.size()) + { + logits[token] *= penalty; + } + } + } + + // Softmax function + std::vector softmax(const std::vector &logits) + { + std::vector probs(logits.size()); + float max_logit = *std::max_element(logits.begin(), logits.end()); + float sum = 0.0f; + + for (size_t i = 0; i < logits.size(); ++i) + { + probs[i] = std::exp(logits[i] - max_logit); + sum += probs[i]; + } + + for (float &p : probs) + { + p /= sum; + } + + return probs; + } + + // 动态裁剪低概率 token + int faster_top_p_sampling(const std::vector &logits, float top_p) + { + // 计算softmax + std::vector probs = softmax(logits); + + // 构建最大堆(概率和索引的配对) + std::vector> prob_index; + prob_index.reserve(logits.size()); + for (size_t i = 0; i < logits.size(); ++i) + { + prob_index.emplace_back(probs[i], i); + } + auto cmp = [](const auto &a, const auto &b) + { return a.first < b.first; }; + std::make_heap(prob_index.begin(), prob_index.end(), cmp); + + // 提取top-p元素 + std::vector filtered_indices; + std::vector filtered_probs; + float cumulative_prob = 0.0f; + + while (!prob_index.empty() && cumulative_prob < top_p) + { + std::pop_heap(prob_index.begin(), prob_index.end(), cmp); + auto [prob, index] = prob_index.back(); + prob_index.pop_back(); + + cumulative_prob += prob; + filtered_indices.push_back(index); + filtered_probs.push_back(prob); + + if (cumulative_prob >= top_p) + break; + } + + // 处理边缘情况(概率全零时返回第一个元素) + if (filtered_indices.empty()) + return 0; + + // 使用thread_local随机数生成器(线程安全) + static thread_local std::mt19937 gen(std::random_device{}()); + std::discrete_distribution dist(filtered_probs.begin(), filtered_probs.end()); + return filtered_indices[dist(gen)]; + } + int top_p_sampling(const std::vector &logits, float top_p) + { + std::vector probs = softmax(logits); + + // Sort indices by probability in descending order + std::vector indices(logits.size()); + std::iota(indices.begin(), indices.end(), 0); + std::sort(indices.begin(), indices.end(), [&](size_t i, size_t j) + { return probs[i] > probs[j]; }); + + // Compute cumulative probabilities + float cumulative_prob = 0.0f; + size_t cut_off = 0; + for (; cut_off < indices.size(); ++cut_off) + { + cumulative_prob += probs[indices[cut_off]]; + if (cumulative_prob >= top_p) + break; + } + + // Keep only the top-p probabilities + std::vector filtered_indices(indices.begin(), indices.begin() + cut_off + 1); + std::vector filtered_probs(filtered_indices.size()); + for (size_t i = 0; i < filtered_indices.size(); ++i) + { + filtered_probs[i] = probs[filtered_indices[i]]; + } + + // Normalize the probabilities + float filtered_sum = std::accumulate(filtered_probs.begin(), filtered_probs.end(), 0.0f); + for (float &p : filtered_probs) + { + p /= filtered_sum; + } + + // Sample from the filtered distribution + std::random_device rd; + std::mt19937 gen(rd()); + std::discrete_distribution dist(filtered_probs.begin(), filtered_probs.end()); + return filtered_indices[dist(gen)]; + } + + // 限制候选 token 数 + int top_k_sampling(const std::vector &logits, int k) + { + // std::vector probs = softmax(logits); + + // 获取 top-k 索引 + std::vector indices(logits.size()); + std::iota(indices.begin(), indices.end(), 0); + std::partial_sort(indices.begin(), indices.begin() + k, indices.end(), [&](size_t i, size_t j) + { return logits[i] > logits[j]; }); + + // 仅保留 top-k 概率 + std::vector filtered_indices(indices.begin(), indices.begin() + k); + std::vector filtered_probs(k); + for (size_t i = 0; i < k; ++i) + { + filtered_probs[i] = logits[filtered_indices[i]]; + } + filtered_probs = softmax(filtered_probs); + + // 归一化 + float sum = std::accumulate(filtered_probs.begin(), filtered_probs.end(), 0.0f); + for (float &p : filtered_probs) + { + p /= sum; + } + + // 采样 + std::random_device rd; + std::mt19937 gen(rd()); + std::discrete_distribution dist(filtered_probs.begin(), filtered_probs.end()); + return filtered_indices[dist(gen)]; + } + + bool enable_temperature = false; + float temperature = 1.0f; + + bool enable_repetition_penalty = false; + float repetition_penalty = 1.0f; + int penalty_window = 20; + + bool enable_diversity_penalty = false; + std::vector common_phrases; + float diversity_penalty = 1.0f; + + bool enable_top_p_sampling = false; + float top_p = 1.0f; + + bool enable_top_k_sampling = false; + int top_k = 1; + +public: + LLMPostprocess() {} + + void set_temperature(bool enable, float temperature) + { + enable_temperature = enable; + this->temperature = temperature; + } + + void set_repetition_penalty(bool enable, float penalty) + { + enable_repetition_penalty = enable; + this->repetition_penalty = penalty; + } + + void set_diversity_penalty(bool enable, const std::vector &common_phrases, float penalty) + { + enable_diversity_penalty = enable; + this->common_phrases = common_phrases; + this->diversity_penalty = penalty; + } + + void set_top_p_sampling(bool enable, float top_p) + { + enable_top_k_sampling = false; + enable_top_p_sampling = enable; + this->top_p = top_p; + } + + void set_top_k_sampling(bool enable, int top_k) + { + enable_top_p_sampling = false; + enable_top_k_sampling = enable; + this->top_k = top_k; + } + + bool load_config(std::string config_path) + { + std::ifstream config_file(config_path); + if (!config_file.is_open()) + { + ALOGE("config file(%s) open failed", config_path.c_str()); + return false; + } + nlohmann::json config = nlohmann::json::parse(config_file); + ALOGI("load config: \n%s\n", config.dump(4).c_str()); + + enable_temperature = config["enable_temperature"]; + temperature = config["temperature"]; + if (temperature <= 0.0f) temperature = 1.0f; + + enable_repetition_penalty = config["enable_repetition_penalty"]; + repetition_penalty = config["repetition_penalty"]; + penalty_window = config["penalty_window"]; + if (penalty_window < 0) penalty_window = 0; + if (repetition_penalty < 0.0f) repetition_penalty = 1.0f; + + enable_top_p_sampling = config["enable_top_p_sampling"]; + top_p = config["top_p"]; + if (top_p <= 0.0f) top_p = 0.9f; // reasonable default + if (top_p > 1.0f) top_p = 1.0f; + + enable_top_k_sampling = config["enable_top_k_sampling"]; + top_k = config["top_k"]; + if (top_k < 1) top_k = 1; + + // 互斥处理:若同时开启 top_p 与 top_k,则优先 top_p + if (enable_top_p_sampling && enable_top_k_sampling) + { + ALOGW("Both top_p and top_k enabled; prefer top_p and disable top_k"); + enable_top_k_sampling = false; + } + return true; + } + + int apply(std::vector &logits, const std::vector &history) + { + if (enable_temperature) + apply_temperature(logits, temperature); + if (enable_repetition_penalty) + apply_repetition_penalty(logits, history, repetition_penalty, penalty_window); + if (enable_diversity_penalty) + apply_diversity_penalty(logits, common_phrases, diversity_penalty); + + if (enable_top_p_sampling) + return faster_top_p_sampling(logits, top_p); + else if (enable_top_k_sampling) + return top_k_sampling(logits, top_k); + else + { + // 最大值 + float max_logit = *std::max_element(logits.begin(), logits.end()); + int max_index = std::distance(logits.begin(), std::max_element(logits.begin(), logits.end())); + return max_index; + } + } +}; diff --git a/projects/llm_framework/main_llm2/src/runner/VLMType.hpp b/projects/llm_framework/main_llm2/src/runner/VLMType.hpp new file mode 100644 index 0000000..93e86ad --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/VLMType.hpp @@ -0,0 +1,67 @@ +#pragma once + +#include +#include +#include +#include + +#include "magic_enum/magic_enum.hpp" + +// VLM type is a "wiring/profile" selector for how to: +// - preprocess image/video +// - run the vision encoder +// - inject visual embeddings into the LLM embedding stream +// - (optional) compute mRoPE position ids +// +// It is intentionally independent from `ModelType` (tokenizer model family). +enum class VLMType : int { + None = 0, + Qwen2_5VL = 1, + Qwen3VL = 2, + InternVL3 = 3, + FastVLM = 4, + SmolVLM2 = 5, +}; + +inline constexpr std::string_view VLMTypeName(VLMType t) { + return magic_enum::enum_name(t); +} + +inline std::optional VLMTypeFromInt(int v) { + return magic_enum::enum_cast(v); +} + +inline std::string VLMTypeKey(std::string_view s) { + // Lowercase and keep only [A-Za-z0-9]. This makes matching tolerant to: + // - case + // - separators like '_' '-' '.' + // Example: "QWEN3_VL" "qwen3-vl" "Qwen3VL" all -> "qwen3vl" + std::string out; + out.reserve(s.size()); + for (char c : s) { + const unsigned char uc = (unsigned char)c; + if (std::isalnum(uc)) out.push_back((char)std::tolower(uc)); + } + return out; +} + +inline std::optional VLMTypeFromString(std::string_view s) { + const std::string key = VLMTypeKey(s); + for (auto t : magic_enum::enum_values()) { + const auto name = magic_enum::enum_name(t); + if (VLMTypeKey(name) == key) return t; + } + return std::nullopt; +} + +inline std::string VLMTypeChoices() { + std::string out; + for (auto t : magic_enum::enum_values()) { + if (!out.empty()) out.append(", "); + out.append(std::string(magic_enum::enum_name(t))); + out.push_back('('); + out.append(std::to_string((int)t)); + out.push_back(')'); + } + return out; +} diff --git a/projects/llm_framework/main_llm2/src/runner/ax_model_runner/ax_model_runner.hpp b/projects/llm_framework/main_llm2/src/runner/ax_model_runner/ax_model_runner.hpp new file mode 100644 index 0000000..668cc34 --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/ax_model_runner/ax_model_runner.hpp @@ -0,0 +1,153 @@ +#pragma once +#include +#include +#include +#include + +// --------------------------------------------------------------------------- +// 通用类型:两个后端共用 +// --------------------------------------------------------------------------- +typedef enum _color_space_e +{ + axdl_color_space_unknown, + axdl_color_space_nv12, + axdl_color_space_nv21, + axdl_color_space_bgr, + axdl_color_space_rgb, +} ax_color_space_e; + +typedef struct +{ + std::string sName; + unsigned int nIdx; + std::vector vShape; + int nSize; + unsigned long long phyAddr; // 统一 64-bit;AX650 aarch64 上 unsigned long 也是 64-bit + void *pVirAddr; +} ax_runner_tensor_t; + +// --------------------------------------------------------------------------- +// ax_runner_base — 统一基类 +// +// dev_id 约定: +// -1 → AX650 片上后端(只有一个片上 NPU,无需设备号) +// >= 0 → AXCL PCIe 后端的设备 ID(从 0 开始) +// --------------------------------------------------------------------------- +class ax_runner_base +{ +protected: + std::vector moutput_tensors; + std::vector minput_tensors; + + std::vector> mgroup_output_tensors; + std::vector> mgroup_input_tensors; + + std::map map_output_tensors; + std::map map_input_tensors; + + std::map> map_group_output_tensors; + std::map> map_group_input_tensors; + + void build_tensor_maps() + { + map_input_tensors.clear(); + for (const auto &t : minput_tensors) + map_input_tensors[t.sName] = t; + + map_output_tensors.clear(); + for (const auto &t : moutput_tensors) + map_output_tensors[t.sName] = t; + + map_group_input_tensors.clear(); + for (const auto &grp : mgroup_input_tensors) + for (const auto &t : grp) + map_group_input_tensors[t.sName].push_back(t); + + map_group_output_tensors.clear(); + for (const auto &grp : mgroup_output_tensors) + for (const auto &t : grp) + map_group_output_tensors[t.sName].push_back(t); + } + +public: + int dev_id = -1; // -1 = AX650 片上,>= 0 = AXCL 设备号 + + bool _auto_sync_before_inference = false; + bool _auto_sync_after_inference = false; + + virtual ~ax_runner_base() {} + + // devid = -1 → AX650(忽略),devid >= 0 → AXCL 设备 + virtual int init(const char *model_file, int devid = -1) = 0; + virtual int init(char *model_buffer, size_t model_size, int devid = -1) = 0; + virtual void deinit() = 0; + + int get_devid() const { return dev_id; } + + void set_auto_sync_before_inference(bool v) { _auto_sync_before_inference = v; } + void set_auto_sync_after_inference(bool v) { _auto_sync_after_inference = v; } + + // 图像推理接口(LLM 不使用,提供默认实现避免子类必须重写) + virtual int get_algo_width() { return 0; } + virtual int get_algo_height() { return 0; } + virtual ax_color_space_e get_color_space() { return axdl_color_space_unknown; } + + // ---- tensor 数量 ---- + int get_num_inputs() { return (int)minput_tensors.size(); } + int get_num_outputs() { return (int)moutput_tensors.size(); } + int get_num_input_groups() { return (int)mgroup_input_tensors.size(); } + int get_num_output_groups() { return (int)mgroup_output_tensors.size(); } + + // ---- 无 group 的 tensor 访问 ---- + const ax_runner_tensor_t &get_input(int idx) { return minput_tensors[idx]; } + const ax_runner_tensor_t *get_inputs_ptr() { return minput_tensors.data(); } + + const ax_runner_tensor_t &get_input(const std::string &name) + { + auto it = map_input_tensors.find(name); + if (it == map_input_tensors.end()) + throw std::runtime_error("input tensor not found: " + name); + return it->second; + } + + const ax_runner_tensor_t &get_output(int idx) { return moutput_tensors[idx]; } + const ax_runner_tensor_t *get_outputs_ptr() { return moutput_tensors.data(); } + + const ax_runner_tensor_t &get_output(const std::string &name) + { + auto it = map_output_tensors.find(name); + if (it == map_output_tensors.end()) + throw std::runtime_error("output tensor not found: " + name); + return it->second; + } + + // ---- 带 group 的 tensor 访问 ---- + const ax_runner_tensor_t &get_input(int grpid, int idx) { return mgroup_input_tensors[grpid][idx]; } + const ax_runner_tensor_t *get_inputs_ptr(int grpid) { return mgroup_input_tensors[grpid].data(); } + + const ax_runner_tensor_t &get_input(int grpid, const std::string &name) + { + auto it = map_group_input_tensors.find(name); + if (it == map_group_input_tensors.end()) + throw std::runtime_error("input tensor not found: " + name); + if (grpid < 0 || grpid >= (int)it->second.size()) + throw std::runtime_error("group id out of range for: " + name); + return it->second[grpid]; + } + + const ax_runner_tensor_t &get_output(int grpid, int idx) { return mgroup_output_tensors[grpid][idx]; } + const ax_runner_tensor_t *get_outputs_ptr(int grpid) { return mgroup_output_tensors[grpid].data(); } + + const ax_runner_tensor_t &get_output(int grpid, const std::string &name) + { + auto it = map_group_output_tensors.find(name); + if (it == map_group_output_tensors.end()) + throw std::runtime_error("output tensor not found: " + name); + if (grpid < 0 || grpid >= (int)it->second.size()) + throw std::runtime_error("group id out of range for: " + name); + return it->second[grpid]; + } + + virtual int inference() = 0; + virtual int inference(int grpid) = 0; +}; diff --git a/projects/llm_framework/main_llm2/src/runner/ax_model_runner/ax_model_runner_ax650.cpp b/projects/llm_framework/main_llm2/src/runner/ax_model_runner/ax_model_runner_ax650.cpp new file mode 100644 index 0000000..af2d87a --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/ax_model_runner/ax_model_runner_ax650.cpp @@ -0,0 +1,400 @@ +#include "ax_model_runner_ax650.hpp" +#include +#include +#include +#include +#include // 用于去重释放物理内存 +#include +#include +#include +#include +#include "memory_utils.hpp" +#include "sample_log.h" + +#define AX_CMM_ALIGN_SIZE 128 +const char *AX_CMM_SESSION_NAME = "npu"; + +typedef enum +{ + AX_ENGINE_ABST_DEFAULT = 0, + AX_ENGINE_ABST_CACHED = 1, +} AX_ENGINE_ALLOC_BUFFER_STRATEGY_T; + +// 封装一个结构体来管理内部句柄,方便管理 +struct ax_runner_ax650_handle_t +{ + AX_ENGINE_HANDLE handle = nullptr; + AX_ENGINE_CONTEXT_T context = 0; + std::vector io_info; + std::vector io_data; +}; + +// 辅助:分配 IO 结构体数组(不分配物理内存) +static int prepare_io_struct_only(AX_ENGINE_IO_INFO_T *info, AX_ENGINE_IO_T *io_data) +{ + memset(io_data, 0, sizeof(*io_data)); + io_data->pInputs = new AX_ENGINE_IO_BUFFER_T[info->nInputSize]; + io_data->nInputSize = info->nInputSize; + memset(io_data->pInputs, 0, sizeof(AX_ENGINE_IO_BUFFER_T) * info->nInputSize); + + io_data->pOutputs = new AX_ENGINE_IO_BUFFER_T[info->nOutputSize]; + io_data->nOutputSize = info->nOutputSize; + memset(io_data->pOutputs, 0, sizeof(AX_ENGINE_IO_BUFFER_T) * info->nOutputSize); + + // 仅设置 Size,方便后续逻辑 + for (uint i = 0; i < info->nInputSize; ++i) + io_data->pInputs[i].nSize = info->pInputs[i].nSize; + for (uint i = 0; i < info->nOutputSize; ++i) + io_data->pOutputs[i].nSize = info->pOutputs[i].nSize; + + return 0; +} + +// 辅助:分配 IO 结构体数组 + 物理内存 +static int prepare_io_with_alloc(AX_ENGINE_IO_INFO_T *info, AX_ENGINE_IO_T *io_data, + std::pair strategy, std::vector skip_alloc_names = {}) +{ + int ret = prepare_io_struct_only(info, io_data); + if (ret != 0) + return ret; + + // Alloc Inputs + for (uint i = 0; i < info->nInputSize; ++i) + { + auto &buffer = io_data->pInputs[i]; + if (std::find(skip_alloc_names.begin(), skip_alloc_names.end(), info->pInputs[i].pName) != skip_alloc_names.end()) + { + continue; + } + if (strategy.first == AX_ENGINE_ABST_CACHED) + { + ret = AX_SYS_MemAllocCached((AX_U64 *)(&buffer.phyAddr), &buffer.pVirAddr, buffer.nSize, AX_CMM_ALIGN_SIZE, (const AX_S8 *)(AX_CMM_SESSION_NAME)); + } + else + { + ret = AX_SYS_MemAlloc((AX_U64 *)(&buffer.phyAddr), &buffer.pVirAddr, buffer.nSize, AX_CMM_ALIGN_SIZE, (const AX_S8 *)(AX_CMM_SESSION_NAME)); + } + if (ret != 0) + { + ALOGE("Alloc input[%d] failed", i); + return ret; // 注意:此处在实际工程中应跳转到 cleanup,为简化展示直接返回 + } + memset(buffer.pVirAddr, 0, buffer.nSize); + } + + // Alloc Outputs + for (uint i = 0; i < info->nOutputSize; ++i) + { + auto &buffer = io_data->pOutputs[i]; + if (std::find(skip_alloc_names.begin(), skip_alloc_names.end(), info->pOutputs[i].pName) != skip_alloc_names.end()) + { + continue; + } + if (strategy.second == AX_ENGINE_ABST_CACHED) + { + ret = AX_SYS_MemAllocCached((AX_U64 *)(&buffer.phyAddr), &buffer.pVirAddr, buffer.nSize, AX_CMM_ALIGN_SIZE, (const AX_S8 *)(AX_CMM_SESSION_NAME)); + } + else + { + ret = AX_SYS_MemAlloc((AX_U64 *)(&buffer.phyAddr), &buffer.pVirAddr, buffer.nSize, AX_CMM_ALIGN_SIZE, (const AX_S8 *)(AX_CMM_SESSION_NAME)); + } + if (ret != 0) + { + ALOGE("Alloc output[%d] failed", i); + return ret; + } + memset(buffer.pVirAddr, 0, buffer.nSize); + } + return 0; +} + +int ax_runner_ax650::sub_init() +{ + if (!m_handle) + return -1; + + int ret = AX_ENGINE_CreateContext(m_handle->handle); + if (ret != 0) + return ret; + + ret = AX_ENGINE_CreateContextV2(m_handle->handle, &m_handle->context); + if (ret != 0) + return ret; + + AX_U32 io_count = 0; + ret = AX_ENGINE_GetGroupIOInfoCount(m_handle->handle, &io_count); + if (ret != 0) + return ret; + + m_handle->io_info.resize(io_count); + m_handle->io_data.resize(io_count); + mgroup_input_tensors.resize(io_count); + mgroup_output_tensors.resize(io_count); + + std::vector skip_alloc_names = {"K_cache", "V_cache"}; + // 1. 分配 IO 资源 + for (size_t grpid = 0; grpid < io_count; grpid++) + { + AX_ENGINE_IO_INFO_T *io_info = nullptr; + ret = AX_ENGINE_GetGroupIOInfo(m_handle->handle, grpid, &io_info); + if (ret != 0) + return ret; + m_handle->io_info[grpid] = io_info; + + // 原有逻辑保持不变:Group 0 和 Last Group 分配物理内存,中间 Group 不分配 + if (grpid == 0) + { + ret = prepare_io_with_alloc(io_info, &m_handle->io_data[grpid], {AX_ENGINE_ABST_DEFAULT, AX_ENGINE_ABST_CACHED}); + } + else if (grpid == io_count - 1) + { + ret = prepare_io_with_alloc(io_info, &m_handle->io_data[grpid], {AX_ENGINE_ABST_DEFAULT, AX_ENGINE_ABST_CACHED}, skip_alloc_names); + } + else + { + ret = prepare_io_struct_only(io_info, &m_handle->io_data[grpid]); + } + if (ret != 0) + return ret; + } + + // 2. 处理中间 Group 的内存共享逻辑 (原有逻辑的 Hack) + if (io_count > 2) + { + auto &first_io_data = m_handle->io_data[0]; + auto &first_io_info = m_handle->io_info[0]; + auto &last_io_data = m_handle->io_data[io_count - 1]; + auto &last_io_info = m_handle->io_info[io_count - 1]; + for (uint i = 0; i < last_io_data.nInputSize; ++i) + { + if (std::find(skip_alloc_names.begin(), skip_alloc_names.end(), last_io_info->pInputs[i].pName) != skip_alloc_names.end()) + { + for (uint j = 0; j < first_io_data.nInputSize; ++j) + { + if (first_io_info->pInputs[j].pName == last_io_info->pInputs[i].pName) + { + last_io_data.pInputs[i].phyAddr = first_io_data.pInputs[j].phyAddr; + last_io_data.pInputs[i].pVirAddr = first_io_data.pInputs[j].pVirAddr; + } + } + } + } + + for (size_t grpid = 1; grpid < io_count - 1; grpid++) + { + auto &io_info = m_handle->io_info[grpid]; + auto &io_data = m_handle->io_data[grpid]; + + // 安全检查:确保维度匹配再拷贝 + size_t min_inputs = std::min(io_info->nInputSize, last_io_data.nInputSize); + for (size_t i = 0; i < min_inputs; i++) + { + io_data.pInputs[i].phyAddr = last_io_data.pInputs[i].phyAddr; + io_data.pInputs[i].pVirAddr = last_io_data.pInputs[i].pVirAddr; + } + + size_t min_outputs = std::min(io_info->nOutputSize, last_io_data.nOutputSize); + for (size_t i = 0; i < min_outputs; i++) + { + io_data.pOutputs[i].phyAddr = last_io_data.pOutputs[i].phyAddr; + io_data.pOutputs[i].pVirAddr = last_io_data.pOutputs[i].pVirAddr; + } + } + } + + // 3. 构建 Tensor 对象 + for (size_t grpid = 0; grpid < io_count; grpid++) + { + auto &io_info = m_handle->io_info[grpid]; + auto &io_data = m_handle->io_data[grpid]; + + // Process Outputs + for (size_t i = 0; i < io_info->nOutputSize; i++) + { + ax_runner_tensor_t tensor; + tensor.nIdx = i; + tensor.sName = io_info->pOutputs[i].pName ? std::string(io_info->pOutputs[i].pName) : ""; + tensor.nSize = io_info->pOutputs[i].nSize; + tensor.phyAddr = io_data.pOutputs[i].phyAddr; + tensor.pVirAddr = io_data.pOutputs[i].pVirAddr; + for (size_t j = 0; j < io_info->pOutputs[i].nShapeSize; j++) + { + tensor.vShape.push_back(io_info->pOutputs[i].pShape[j]); + } + mgroup_output_tensors[grpid].push_back(tensor); + } + + // Process Inputs + for (size_t i = 0; i < io_info->nInputSize; i++) + { + ax_runner_tensor_t tensor; + tensor.nIdx = i; + tensor.sName = io_info->pInputs[i].pName ? std::string(io_info->pInputs[i].pName) : ""; + tensor.nSize = io_info->pInputs[i].nSize; + tensor.phyAddr = io_data.pInputs[i].phyAddr; + tensor.pVirAddr = io_data.pInputs[i].pVirAddr; + for (size_t j = 0; j < io_info->pInputs[i].nShapeSize; j++) + { + tensor.vShape.push_back(io_info->pInputs[i].pShape[j]); + } + mgroup_input_tensors[grpid].push_back(tensor); + } + } + + if (!mgroup_output_tensors.empty()) + moutput_tensors = mgroup_output_tensors[0]; + if (!mgroup_input_tensors.empty()) + minput_tensors = mgroup_input_tensors[0]; + + // 4. 构建查找表 + build_tensor_maps(); + + return 0; +} + +int ax_runner_ax650::init(const char *model_file, int /*devid*/) +{ + MMap model_buffer; + if (!model_buffer.open_file(model_file)) + { + ALOGE("model file(%s) open failed", model_file); + return -1; + } + auto ret = init((char *)model_buffer.data(), model_buffer.size(), -1); + return ret; +} + +int ax_runner_ax650::init(char *model_buffer, size_t model_size, int /*devid*/) +{ + if (m_handle) + deinit(); // 防止多次 init 导致泄漏 + m_handle = new ax_runner_ax650_handle_t; + int ret = AX_ENGINE_CreateHandle(&m_handle->handle, model_buffer, model_size); + if (0 != ret) + { + ALOGE("AX_ENGINE_CreateHandle failed: 0x%x", ret); + delete m_handle; + m_handle = nullptr; + return ret; + } + return sub_init(); +} + +void ax_runner_ax650::deinit() +{ + if (!m_handle) + return; + + // 使用 Set 防止物理内存被重复释放 (Double Free) + std::unordered_set freed_phy_addrs; + + // 遍历所有 Group + for (size_t g = 0; g < m_handle->io_data.size(); ++g) + { + auto &io = m_handle->io_data[g]; + + // 1. 清理 Inputs + if (io.pInputs) + { + for (size_t j = 0; j < io.nInputSize; ++j) + { + AX_ENGINE_IO_BUFFER_T *pBuf = io.pInputs + j; + if (pBuf->phyAddr != 0) + { + // 如果这个物理地址还没被释放过,则释放 + if (freed_phy_addrs.find(pBuf->phyAddr) == freed_phy_addrs.end()) + { + AX_SYS_MemFree(pBuf->phyAddr, pBuf->pVirAddr); + freed_phy_addrs.insert(pBuf->phyAddr); + } + } + } + // 释放数组本身 (C++ heap memory) + delete[] io.pInputs; + io.pInputs = nullptr; + } + + // 2. 清理 Outputs + if (io.pOutputs) + { + for (size_t j = 0; j < io.nOutputSize; ++j) + { + AX_ENGINE_IO_BUFFER_T *pBuf = io.pOutputs + j; + if (pBuf->phyAddr != 0) + { + if (freed_phy_addrs.find(pBuf->phyAddr) == freed_phy_addrs.end()) + { + AX_SYS_MemFree(pBuf->phyAddr, pBuf->pVirAddr); + freed_phy_addrs.insert(pBuf->phyAddr); + } + } + } + delete[] io.pOutputs; + io.pOutputs = nullptr; + } + } + + if (m_handle->handle) + { + AX_ENGINE_DestroyHandle(m_handle->handle); + } + + delete m_handle; + m_handle = nullptr; + + // 清空容器 + moutput_tensors.clear(); + minput_tensors.clear(); + map_input_tensors.clear(); + map_output_tensors.clear(); + mgroup_output_tensors.clear(); + mgroup_input_tensors.clear(); + map_group_input_tensors.clear(); + map_group_output_tensors.clear(); +} + +int ax_runner_ax650::inference() +{ + if (!m_handle) + return -1; + // 刷 Cache 保证数据一致性 + for (size_t i = 0; i < get_num_inputs(); i++) + { + auto &tensor = get_input(i); + AX_SYS_MflushCache(tensor.phyAddr, tensor.pVirAddr, tensor.nSize); + } + + int ret = AX_ENGINE_RunSync(m_handle->handle, &m_handle->io_data[0]); + + for (size_t i = 0; i < get_num_outputs(); i++) + { + auto &tensor = get_output(i); + AX_SYS_MinvalidateCache(tensor.phyAddr, tensor.pVirAddr, tensor.nSize); + } + return ret; +} + +int ax_runner_ax650::inference(int grpid) +{ + if (!m_handle) + return -1; + if (grpid < 0 || grpid >= (int)m_handle->io_data.size()) + return -1; + + // 刷 Cache (Input) + for (size_t i = 0; i < mgroup_input_tensors[grpid].size(); i++) + { + auto &tensor = mgroup_input_tensors[grpid][i]; + AX_SYS_MflushCache(tensor.phyAddr, tensor.pVirAddr, tensor.nSize); + } + + int ret = AX_ENGINE_RunGroupIOSync(m_handle->handle, m_handle->context, grpid, &m_handle->io_data[grpid]); + + // 刷 Cache (Output) + for (size_t i = 0; i < mgroup_output_tensors[grpid].size(); i++) + { + auto &tensor = mgroup_output_tensors[grpid][i]; + AX_SYS_MinvalidateCache(tensor.phyAddr, tensor.pVirAddr, tensor.nSize); + } + return ret; +} \ No newline at end of file diff --git a/projects/llm_framework/main_llm2/src/runner/ax_model_runner/ax_model_runner_ax650.hpp b/projects/llm_framework/main_llm2/src/runner/ax_model_runner/ax_model_runner_ax650.hpp new file mode 100644 index 0000000..13f2d00 --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/ax_model_runner/ax_model_runner_ax650.hpp @@ -0,0 +1,24 @@ +#pragma once +#include "ax_model_runner.hpp" + +struct ax_runner_ax650_handle_t; + +class ax_runner_ax650 : public ax_runner_base +{ +protected: + struct ax_runner_ax650_handle_t *m_handle = nullptr; + int sub_init(); + +public: + ax_runner_ax650() = default; + virtual ~ax_runner_ax650() { deinit(); } + + // devid 对 AX650 片上后端无意义,忽略(保留参数以满足统一接口) + int init(const char *model_file, int devid = -1) override; + int init(char *model_buffer, size_t model_size, int devid = -1) override; + + void deinit() override; + + int inference() override; + int inference(int grpid) override; +}; diff --git a/projects/llm_framework/main_llm2/src/runner/magic_enum/include/magic_enum/magic_enum.hpp b/projects/llm_framework/main_llm2/src/runner/magic_enum/include/magic_enum/magic_enum.hpp new file mode 100644 index 0000000..29ad7a0 --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/magic_enum/include/magic_enum/magic_enum.hpp @@ -0,0 +1,1564 @@ +// __ __ _ ______ _____ +// | \/ | (_) | ____| / ____|_ _ +// | \ / | __ _ __ _ _ ___ | |__ _ __ _ _ _ __ ___ | | _| |_ _| |_ +// | |\/| |/ _` |/ _` | |/ __| | __| | '_ \| | | | '_ ` _ \ | | |_ _|_ _| +// | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_| +// |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____| +// __/ | https://github.com/Neargye/magic_enum +// |___/ version 0.9.7 +// +// Licensed under the MIT License . +// SPDX-License-Identifier: MIT +// Copyright (c) 2019 - 2024 Daniil Goncharov . +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#ifndef NEARGYE_MAGIC_ENUM_HPP +#define NEARGYE_MAGIC_ENUM_HPP + +#define MAGIC_ENUM_VERSION_MAJOR 0 +#define MAGIC_ENUM_VERSION_MINOR 9 +#define MAGIC_ENUM_VERSION_PATCH 7 + +#ifndef MAGIC_ENUM_USE_STD_MODULE +#include +#include +#include +#include +#include +#include +#include +#endif + +#if defined(MAGIC_ENUM_CONFIG_FILE) +# include MAGIC_ENUM_CONFIG_FILE +#endif + +#ifndef MAGIC_ENUM_USE_STD_MODULE +#if !defined(MAGIC_ENUM_USING_ALIAS_OPTIONAL) +# include +#endif +#if !defined(MAGIC_ENUM_USING_ALIAS_STRING) +# include +#endif +#if !defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW) +# include +#endif +#endif + +#if defined(MAGIC_ENUM_NO_ASSERT) +# define MAGIC_ENUM_ASSERT(...) static_cast(0) +#elif !defined(MAGIC_ENUM_ASSERT) +# include +# define MAGIC_ENUM_ASSERT(...) assert((__VA_ARGS__)) +#endif + +#if defined(__clang__) +# pragma clang diagnostic push +# pragma clang diagnostic ignored "-Wunknown-warning-option" +# pragma clang diagnostic ignored "-Wenum-constexpr-conversion" +# pragma clang diagnostic ignored "-Wuseless-cast" // suppresses 'static_cast('\0')' for char_type = char (common on Linux). +#elif defined(__GNUC__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // May be used uninitialized 'return {};'. +# pragma GCC diagnostic ignored "-Wuseless-cast" // suppresses 'static_cast('\0')' for char_type = char (common on Linux). +#elif defined(_MSC_VER) +# pragma warning(push) +# pragma warning(disable : 26495) // Variable 'static_str::chars_' is uninitialized. +# pragma warning(disable : 28020) // Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value. +# pragma warning(disable : 26451) // The expression '0<=_Param_(1)&&_Param_(1)<=1-1' is not true at this call. +# pragma warning(disable : 4514) // Unreferenced inline function has been removed. +#endif + +// Checks magic_enum compiler compatibility. +#if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1910 || defined(__RESHARPER__) +# undef MAGIC_ENUM_SUPPORTED +# define MAGIC_ENUM_SUPPORTED 1 +#endif + +// Checks magic_enum compiler aliases compatibility. +#if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1920 +# undef MAGIC_ENUM_SUPPORTED_ALIASES +# define MAGIC_ENUM_SUPPORTED_ALIASES 1 +#endif + +// Specify the calling convention for compilers that need it in order to get reliable mangled names under different +// compiler flags. In particular, MSVC allows changing the default calling convention on x86. +#if defined(__clang__) || defined(__GNUC__) +#define MAGIC_ENUM_CALLING_CONVENTION +#elif defined(_MSC_VER) +#define MAGIC_ENUM_CALLING_CONVENTION __cdecl +#endif + +// Enum value must be greater or equals than MAGIC_ENUM_RANGE_MIN. By default MAGIC_ENUM_RANGE_MIN = -128. +// If need another min range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN. +#if !defined(MAGIC_ENUM_RANGE_MIN) +# define MAGIC_ENUM_RANGE_MIN -128 +#endif + +// Enum value must be less or equals than MAGIC_ENUM_RANGE_MAX. By default MAGIC_ENUM_RANGE_MAX = 127. +// If need another max range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MAX. +#if !defined(MAGIC_ENUM_RANGE_MAX) +# define MAGIC_ENUM_RANGE_MAX 127 +#endif + +// Improve ReSharper C++ intellisense performance with builtins, avoiding unnecessary template instantiations. +#if defined(__RESHARPER__) +# undef MAGIC_ENUM_GET_ENUM_NAME_BUILTIN +# undef MAGIC_ENUM_GET_TYPE_NAME_BUILTIN +# if __RESHARPER__ >= 20230100 +# define MAGIC_ENUM_GET_ENUM_NAME_BUILTIN(V) __rscpp_enumerator_name(V) +# define MAGIC_ENUM_GET_TYPE_NAME_BUILTIN(T) __rscpp_type_name() +# else +# define MAGIC_ENUM_GET_ENUM_NAME_BUILTIN(V) nullptr +# define MAGIC_ENUM_GET_TYPE_NAME_BUILTIN(T) nullptr +# endif +#endif + +namespace magic_enum { + +// If need another optional type, define the macro MAGIC_ENUM_USING_ALIAS_OPTIONAL. +#if defined(MAGIC_ENUM_USING_ALIAS_OPTIONAL) +MAGIC_ENUM_USING_ALIAS_OPTIONAL +#else +using std::optional; +#endif + +// If need another string_view type, define the macro MAGIC_ENUM_USING_ALIAS_STRING_VIEW. +#if defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW) +MAGIC_ENUM_USING_ALIAS_STRING_VIEW +#else +using std::string_view; +#endif + +// If need another string type, define the macro MAGIC_ENUM_USING_ALIAS_STRING. +#if defined(MAGIC_ENUM_USING_ALIAS_STRING) +MAGIC_ENUM_USING_ALIAS_STRING +#else +using std::string; +#endif + +using char_type = string_view::value_type; +static_assert(std::is_same_v, "magic_enum::customize requires same string_view::value_type and string::value_type"); +static_assert([] { + if constexpr (std::is_same_v) { + constexpr const char c[] = "abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789|"; + constexpr const wchar_t wc[] = L"abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789|"; + static_assert(std::size(c) == std::size(wc), "magic_enum::customize identifier characters are multichars in wchar_t."); + + for (std::size_t i = 0; i < std::size(c); ++i) { + if (c[i] != wc[i]) { + return false; + } + } + } + return true; +} (), "magic_enum::customize wchar_t is not compatible with ASCII."); + +namespace customize { + template + struct enum_range; +} + +namespace detail { + template + constexpr inline std::size_t prefix_length_or_zero = 0; + + template + constexpr inline auto prefix_length_or_zero::prefix_length)>> = std::size_t{customize::enum_range::prefix_length}; +} + +namespace customize { + +template +struct adl_info_holder { + constexpr static int max = Max; + constexpr static int min = Min; + constexpr static bool is_flags =IsFlags; + constexpr static std::size_t prefix_length = PrefixLength; + + template + constexpr static adl_info_holder minmax() { return {}; } + + template + constexpr static adl_info_holder flag() { return {}; } + + template + constexpr static adl_info_holder prefix() { return {}; } +}; + +constexpr adl_info_holder<> adl_info() { return {}; } + +// Enum value must be in range [MAGIC_ENUM_RANGE_MIN, MAGIC_ENUM_RANGE_MAX]. By default MAGIC_ENUM_RANGE_MIN = -128, MAGIC_ENUM_RANGE_MAX = 127. +// If need another range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN and MAGIC_ENUM_RANGE_MAX. +// If need another range for specific enum type, add specialization enum_range for necessary enum type. +template +struct enum_range { + static constexpr int min = MAGIC_ENUM_RANGE_MIN; + static constexpr int max = MAGIC_ENUM_RANGE_MAX; +}; + +template +struct enum_range : decltype(magic_enum_define_range_adl(E{})) {}; + +static_assert(MAGIC_ENUM_RANGE_MAX > MAGIC_ENUM_RANGE_MIN, "MAGIC_ENUM_RANGE_MAX must be greater than MAGIC_ENUM_RANGE_MIN."); + +namespace detail { + +enum class customize_tag { + default_tag, + invalid_tag, + custom_tag +}; + +} // namespace magic_enum::customize::detail + +class customize_t : public std::pair { + public: + constexpr customize_t(string_view srt) : std::pair{detail::customize_tag::custom_tag, srt} {} + constexpr customize_t(const char_type* srt) : customize_t{string_view{srt}} {} + constexpr customize_t(detail::customize_tag tag) : std::pair{tag, string_view{}} { + MAGIC_ENUM_ASSERT(tag != detail::customize_tag::custom_tag); + } +}; + +// Default customize. +inline constexpr auto default_tag = customize_t{detail::customize_tag::default_tag}; +// Invalid customize. +inline constexpr auto invalid_tag = customize_t{detail::customize_tag::invalid_tag}; + +// If need custom names for enum, add specialization enum_name for necessary enum type. +template +constexpr customize_t enum_name(E) noexcept { + return default_tag; +} + +// If need custom type name for enum, add specialization enum_type_name for necessary enum type. +template +constexpr customize_t enum_type_name() noexcept { + return default_tag; +} + +} // namespace magic_enum::customize + +namespace detail { + +template +struct supported +#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED || defined(MAGIC_ENUM_NO_CHECK_SUPPORT) + : std::true_type {}; +#else + : std::false_type {}; +#endif + +template , std::enable_if_t, int> = 0> +using enum_constant = std::integral_constant; + +template +inline constexpr bool always_false_v = false; + +template +struct has_is_flags : std::false_type {}; + +template +struct has_is_flags::is_flags)>> : std::bool_constant::is_flags)>>> {}; + +template +struct range_min : std::integral_constant {}; + +template +struct range_min::min)>> : std::integral_constant::min), customize::enum_range::min> {}; + +template +struct range_max : std::integral_constant {}; + +template +struct range_max::max)>> : std::integral_constant::max), customize::enum_range::max> {}; + +struct str_view { + const char* str_ = nullptr; + std::size_t size_ = 0; +}; + +template +class static_str { + public: + constexpr explicit static_str(str_view str) noexcept : static_str{str.str_, std::make_integer_sequence{}} { + MAGIC_ENUM_ASSERT(str.size_ == N); + } + + constexpr explicit static_str(const char* const str) noexcept : static_str{ str, std::make_integer_sequence{} } { + } + + constexpr explicit static_str(string_view str) noexcept : static_str{str.data(), std::make_integer_sequence{}} { + MAGIC_ENUM_ASSERT(str.size() == N); + } + + constexpr const char_type* data() const noexcept { return chars_; } + + constexpr std::uint16_t size() const noexcept { return N; } + + constexpr string_view str() const noexcept { return string_view(data(), size()); } + + private: + template + constexpr static_str(const char* str, std::integer_sequence) noexcept : chars_{static_cast(str[I])..., static_cast('\0')} {} + + template + constexpr static_str(string_view str, std::integer_sequence) noexcept : chars_{str[I]..., static_cast('\0')} {} + + char_type chars_[static_cast(N) + 1]; +}; + +template <> +class static_str<0> { + public: + constexpr static_str() noexcept = default; + + constexpr static_str(str_view) noexcept {} + + constexpr static_str(string_view) noexcept {} + + constexpr const char_type* data() const noexcept { return chars_; } + + constexpr std::uint16_t size() const noexcept { return 0; } + + constexpr string_view str() const noexcept { return string_view(data(), size()); } + +private: + static constexpr char_type chars_[1] = {}; +}; + +template > +class case_insensitive { + static constexpr char_type to_lower(char_type c) noexcept { + return (c >= static_cast('A') && c <= static_cast('Z')) ? static_cast(c + (static_cast('a') - static_cast('A'))) : c; + } + + public: + template + constexpr auto operator()(L lhs, R rhs) const noexcept -> std::enable_if_t, char_type> && std::is_same_v, char_type>, bool> { + return Op{}(to_lower(lhs), to_lower(rhs)); + } +}; + +constexpr std::size_t find(string_view str, char_type c) noexcept { +#if defined(__clang__) && __clang_major__ < 9 && defined(__GLIBCXX__) || defined(_MSC_VER) && _MSC_VER < 1920 && !defined(__clang__) +// https://stackoverflow.com/questions/56484834/constexpr-stdstring-viewfind-last-of-doesnt-work-on-clang-8-with-libstdc +// https://developercommunity.visualstudio.com/content/problem/360432/vs20178-regression-c-failed-in-test.html + constexpr bool workaround = true; +#else + constexpr bool workaround = false; +#endif + + if constexpr (workaround) { + for (std::size_t i = 0; i < str.size(); ++i) { + if (str[i] == c) { + return i; + } + } + + return string_view::npos; + } else { + return str.find(c); + } +} + +template +inline constexpr bool is_default_predicate_v = std::is_same_v, std::equal_to> || std::is_same_v, std::equal_to<>>; + + +template +inline constexpr bool is_nothrow_invocable_v = is_default_predicate_v || std::is_nothrow_invocable_r_v; + + +template +constexpr bool cmp_equal(string_view lhs, string_view rhs, [[maybe_unused]] BinaryPredicate&& p) noexcept(is_nothrow_invocable_v) { +#if defined(_MSC_VER) && _MSC_VER < 1920 && !defined(__clang__) + // https://developercommunity.visualstudio.com/content/problem/360432/vs20178-regression-c-failed-in-test.html + // https://developercommunity.visualstudio.com/content/problem/232218/c-constexpr-string-view.html + constexpr bool workaround = true; +#else + constexpr bool workaround = false; +#endif + + if constexpr (!is_default_predicate_v || workaround) { + if (lhs.size() != rhs.size()) { + return false; + } + + const auto size = lhs.size(); + for (std::size_t i = 0; i < size; ++i) { + if (!p(lhs[i], rhs[i])) { + return false; + } + } + + return true; + } else { + return lhs == rhs; + } +} + +template +constexpr bool cmp_less(L lhs, R rhs) noexcept { + static_assert(std::is_integral_v && std::is_integral_v, "magic_enum::detail::cmp_less requires integral type."); + + if constexpr (std::is_signed_v == std::is_signed_v) { + // If same signedness (both signed or both unsigned). + return lhs < rhs; + } else if constexpr (std::is_same_v) { // bool special case + return static_cast(lhs) < rhs; + } else if constexpr (std::is_same_v) { // bool special case + return lhs < static_cast(rhs); + } else if constexpr (std::is_signed_v) { + // If 'right' is negative, then result is 'false', otherwise cast & compare. + return rhs > 0 && lhs < static_cast>(rhs); + } else { + // If 'left' is negative, then result is 'true', otherwise cast & compare. + return lhs < 0 || static_cast>(lhs) < rhs; + } +} + +template +constexpr I log2(I value) noexcept { + static_assert(std::is_integral_v, "magic_enum::detail::log2 requires integral type."); + + if constexpr (std::is_same_v) { // bool special case + return MAGIC_ENUM_ASSERT(false), value; + } else { + auto ret = I{0}; + for (; value > I{1}; value >>= I{1}, ++ret) {} + + return ret; + } +} + +#if defined(__cpp_lib_array_constexpr) && __cpp_lib_array_constexpr >= 201603L +# define MAGIC_ENUM_ARRAY_CONSTEXPR 1 +#else +template +constexpr std::array, N> to_array(T (&a)[N], std::index_sequence) noexcept { + return {{a[I]...}}; +} +#endif + +template +inline constexpr bool is_enum_v = std::is_enum_v && std::is_same_v>; + +template +constexpr auto MAGIC_ENUM_CALLING_CONVENTION n() noexcept { + static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); + + if constexpr (supported::value) { +#if defined(MAGIC_ENUM_GET_TYPE_NAME_BUILTIN) + constexpr auto name_ptr = MAGIC_ENUM_GET_TYPE_NAME_BUILTIN(E); + constexpr auto name = name_ptr ? str_view{name_ptr, std::char_traits::length(name_ptr)} : str_view{}; +#elif defined(__clang__) + str_view name; + if constexpr (sizeof(__PRETTY_FUNCTION__) == sizeof(__FUNCTION__)) { + static_assert(always_false_v, "magic_enum::detail::n requires __PRETTY_FUNCTION__."); + return str_view{}; + } else { + name.size_ = sizeof(__PRETTY_FUNCTION__) - 36; + name.str_ = __PRETTY_FUNCTION__ + 34; + } +#elif defined(__GNUC__) + auto name = str_view{__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1}; + if constexpr (sizeof(__PRETTY_FUNCTION__) == sizeof(__FUNCTION__)) { + static_assert(always_false_v, "magic_enum::detail::n requires __PRETTY_FUNCTION__."); + return str_view{}; + } else if (name.str_[name.size_ - 1] == ']') { + name.size_ -= 50; + name.str_ += 49; + } else { + name.size_ -= 40; + name.str_ += 37; + } +#elif defined(_MSC_VER) + // CLI/C++ workaround (see https://github.com/Neargye/magic_enum/issues/284). + str_view name; + name.str_ = __FUNCSIG__; + name.str_ += 40; + name.size_ += sizeof(__FUNCSIG__) - 57; +#else + auto name = str_view{}; +#endif + std::size_t p = 0; + for (std::size_t i = name.size_; i > 0; --i) { + if (name.str_[i] == ':') { + p = i + 1; + break; + } + } + if (p > 0) { + name.size_ -= p; + name.str_ += p; + } + return name; + } else { + return str_view{}; // Unsupported compiler or Invalid customize. + } +} + +template +constexpr auto type_name() noexcept { + [[maybe_unused]] constexpr auto custom = customize::enum_type_name(); + static_assert(std::is_same_v, customize::customize_t>, "magic_enum::customize requires customize_t type."); + if constexpr (custom.first == customize::detail::customize_tag::custom_tag) { + constexpr auto name = custom.second; + static_assert(!name.empty(), "magic_enum::customize requires not empty string."); + return static_str{name}; + } else if constexpr (custom.first == customize::detail::customize_tag::invalid_tag) { + return static_str<0>{}; + } else if constexpr (custom.first == customize::detail::customize_tag::default_tag) { + constexpr auto name = n(); + return static_str{name}; + } else { + static_assert(always_false_v, "magic_enum::customize invalid."); + } +} + +template +inline constexpr auto type_name_v = type_name(); + +template +constexpr auto MAGIC_ENUM_CALLING_CONVENTION n() noexcept { + static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); + + if constexpr (supported::value) { +#if defined(MAGIC_ENUM_GET_ENUM_NAME_BUILTIN) + constexpr auto name_ptr = MAGIC_ENUM_GET_ENUM_NAME_BUILTIN(V); + auto name = name_ptr ? str_view{name_ptr, std::char_traits::length(name_ptr)} : str_view{}; +#elif defined(__clang__) + str_view name; + if constexpr (sizeof(__PRETTY_FUNCTION__) == sizeof(__FUNCTION__)) { + static_assert(always_false_v, "magic_enum::detail::n requires __PRETTY_FUNCTION__."); + return str_view{}; + } else { + name.size_ = sizeof(__PRETTY_FUNCTION__) - 36; + name.str_ = __PRETTY_FUNCTION__ + 34; + } + if (name.size_ > 22 && name.str_[0] == '(' && name.str_[1] == 'a' && name.str_[10] == ' ' && name.str_[22] == ':') { + name.size_ -= 23; + name.str_ += 23; + } + if (name.str_[0] == '(' || name.str_[0] == '-' || (name.str_[0] >= '0' && name.str_[0] <= '9')) { + name = str_view{}; + } +#elif defined(__GNUC__) + auto name = str_view{__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1}; + if constexpr (sizeof(__PRETTY_FUNCTION__) == sizeof(__FUNCTION__)) { + static_assert(always_false_v, "magic_enum::detail::n requires __PRETTY_FUNCTION__."); + return str_view{}; + } else if (name.str_[name.size_ - 1] == ']') { + name.size_ -= 55; + name.str_ += 54; + } else { + name.size_ -= 40; + name.str_ += 37; + } + if (name.str_[0] == '(') { + name = str_view{}; + } +#elif defined(_MSC_VER) + str_view name; + if ((__FUNCSIG__[5] == '_' && __FUNCSIG__[35] != '(') || (__FUNCSIG__[5] == 'c' && __FUNCSIG__[41] != '(')) { + // CLI/C++ workaround (see https://github.com/Neargye/magic_enum/issues/284). + name.str_ = __FUNCSIG__; + name.str_ += 35; + name.size_ = sizeof(__FUNCSIG__) - 52; + } +#else + auto name = str_view{}; +#endif + std::size_t p = 0; + for (std::size_t i = name.size_; i > 0; --i) { + if (name.str_[i] == ':') { + p = i + 1; + break; + } + } + if (p > 0) { + name.size_ -= p; + name.str_ += p; + } + return name; + } else { + return str_view{}; // Unsupported compiler or Invalid customize. + } +} + +#if defined(_MSC_VER) && !defined(__clang__) && _MSC_VER < 1920 +# define MAGIC_ENUM_VS_2017_WORKAROUND 1 +#endif + +#if defined(MAGIC_ENUM_VS_2017_WORKAROUND) +template +constexpr auto MAGIC_ENUM_CALLING_CONVENTION n() noexcept { + static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); + +# if defined(MAGIC_ENUM_GET_ENUM_NAME_BUILTIN) + constexpr auto name_ptr = MAGIC_ENUM_GET_ENUM_NAME_BUILTIN(V); + auto name = name_ptr ? str_view{name_ptr, std::char_traits::length(name_ptr)} : str_view{}; +# else + // CLI/C++ workaround (see https://github.com/Neargye/magic_enum/issues/284). + str_view name; + name.str_ = __FUNCSIG__; + name.size_ = sizeof(__FUNCSIG__) - 17; + std::size_t p = 0; + for (std::size_t i = name.size_; i > 0; --i) { + if (name.str_[i] == ',' || name.str_[i] == ':') { + p = i + 1; + break; + } + } + if (p > 0) { + name.size_ -= p; + name.str_ += p; + } + if (name.str_[0] == '(' || name.str_[0] == '-' || (name.str_[0] >= '0' && name.str_[0] <= '9')) { + name = str_view{}; + } + return name; +# endif +} +#endif + +template +constexpr auto enum_name() noexcept { + [[maybe_unused]] constexpr auto custom = customize::enum_name(V); + static_assert(std::is_same_v, customize::customize_t>, "magic_enum::customize requires customize_t type."); + if constexpr (custom.first == customize::detail::customize_tag::custom_tag) { + constexpr auto name = custom.second; + static_assert(!name.empty(), "magic_enum::customize requires not empty string."); + return static_str{name}; + } else if constexpr (custom.first == customize::detail::customize_tag::invalid_tag) { + return static_str<0>{}; + } else if constexpr (custom.first == customize::detail::customize_tag::default_tag) { +#if defined(MAGIC_ENUM_VS_2017_WORKAROUND) + constexpr auto name = n(); +#else + constexpr auto name = n(); +#endif + return static_str>{name.str_ + prefix_length_or_zero}; + } else { + static_assert(always_false_v, "magic_enum::customize invalid."); + } +} + +template +inline constexpr auto enum_name_v = enum_name(); + +// CWG1766: Values outside the range of the values of an enumeration +// https://reviews.llvm.org/D130058, https://reviews.llvm.org/D131307 +#if defined(__clang__) && __clang_major__ >= 16 +template +inline constexpr bool is_enum_constexpr_static_cast_valid = false; +template +inline constexpr bool is_enum_constexpr_static_cast_valid(V)>>> = true; +#else +template +inline constexpr bool is_enum_constexpr_static_cast_valid = true; +#endif + +template +constexpr bool is_valid() noexcept { + if constexpr (is_enum_constexpr_static_cast_valid) { + constexpr E v = static_cast(V); + [[maybe_unused]] constexpr auto custom = customize::enum_name(v); + static_assert(std::is_same_v, customize::customize_t>, "magic_enum::customize requires customize_t type."); + if constexpr (custom.first == customize::detail::customize_tag::custom_tag) { + constexpr auto name = custom.second; + static_assert(!name.empty(), "magic_enum::customize requires not empty string."); + return name.size() != 0; + } else if constexpr (custom.first == customize::detail::customize_tag::default_tag) { +#if defined(MAGIC_ENUM_VS_2017_WORKAROUND) + return n().size_ != 0; +#else + return n().size_ != 0; +#endif + } else { + return false; + } + } else { + return false; + } +} + +enum class enum_subtype { + common, + flags +}; + +template > +constexpr U ualue(std::size_t i) noexcept { + if constexpr (std::is_same_v) { // bool special case + static_assert(O == 0, "magic_enum::detail::ualue requires valid offset."); + + return static_cast(i); + } else if constexpr (S == enum_subtype::flags) { + return static_cast(U{1} << static_cast(static_cast(i) + O)); + } else { + return static_cast(static_cast(i) + O); + } +} + +template > +constexpr E value(std::size_t i) noexcept { + return static_cast(ualue(i)); +} + +template > +constexpr int reflected_min() noexcept { + if constexpr (S == enum_subtype::flags) { + return 0; + } else { + constexpr auto lhs = range_min::value; + constexpr auto rhs = (std::numeric_limits::min)(); + + if constexpr (cmp_less(rhs, lhs)) { + return lhs; + } else { + return rhs; + } + } +} + +template > +constexpr int reflected_max() noexcept { + if constexpr (S == enum_subtype::flags) { + return std::numeric_limits::digits - 1; + } else { + constexpr auto lhs = range_max::value; + constexpr auto rhs = (std::numeric_limits::max)(); + + if constexpr (cmp_less(lhs, rhs)) { + return lhs; + } else { + return rhs; + } + } +} + +#define MAGIC_ENUM_FOR_EACH_256(T) \ + T( 0)T( 1)T( 2)T( 3)T( 4)T( 5)T( 6)T( 7)T( 8)T( 9)T( 10)T( 11)T( 12)T( 13)T( 14)T( 15)T( 16)T( 17)T( 18)T( 19)T( 20)T( 21)T( 22)T( 23)T( 24)T( 25)T( 26)T( 27)T( 28)T( 29)T( 30)T( 31) \ + T( 32)T( 33)T( 34)T( 35)T( 36)T( 37)T( 38)T( 39)T( 40)T( 41)T( 42)T( 43)T( 44)T( 45)T( 46)T( 47)T( 48)T( 49)T( 50)T( 51)T( 52)T( 53)T( 54)T( 55)T( 56)T( 57)T( 58)T( 59)T( 60)T( 61)T( 62)T( 63) \ + T( 64)T( 65)T( 66)T( 67)T( 68)T( 69)T( 70)T( 71)T( 72)T( 73)T( 74)T( 75)T( 76)T( 77)T( 78)T( 79)T( 80)T( 81)T( 82)T( 83)T( 84)T( 85)T( 86)T( 87)T( 88)T( 89)T( 90)T( 91)T( 92)T( 93)T( 94)T( 95) \ + T( 96)T( 97)T( 98)T( 99)T(100)T(101)T(102)T(103)T(104)T(105)T(106)T(107)T(108)T(109)T(110)T(111)T(112)T(113)T(114)T(115)T(116)T(117)T(118)T(119)T(120)T(121)T(122)T(123)T(124)T(125)T(126)T(127) \ + T(128)T(129)T(130)T(131)T(132)T(133)T(134)T(135)T(136)T(137)T(138)T(139)T(140)T(141)T(142)T(143)T(144)T(145)T(146)T(147)T(148)T(149)T(150)T(151)T(152)T(153)T(154)T(155)T(156)T(157)T(158)T(159) \ + T(160)T(161)T(162)T(163)T(164)T(165)T(166)T(167)T(168)T(169)T(170)T(171)T(172)T(173)T(174)T(175)T(176)T(177)T(178)T(179)T(180)T(181)T(182)T(183)T(184)T(185)T(186)T(187)T(188)T(189)T(190)T(191) \ + T(192)T(193)T(194)T(195)T(196)T(197)T(198)T(199)T(200)T(201)T(202)T(203)T(204)T(205)T(206)T(207)T(208)T(209)T(210)T(211)T(212)T(213)T(214)T(215)T(216)T(217)T(218)T(219)T(220)T(221)T(222)T(223) \ + T(224)T(225)T(226)T(227)T(228)T(229)T(230)T(231)T(232)T(233)T(234)T(235)T(236)T(237)T(238)T(239)T(240)T(241)T(242)T(243)T(244)T(245)T(246)T(247)T(248)T(249)T(250)T(251)T(252)T(253)T(254)T(255) + +template +constexpr void valid_count(bool* valid, std::size_t& count) noexcept { +#define MAGIC_ENUM_V(O) \ + if constexpr ((I + O) < Size) { \ + if constexpr (is_valid(I + O)>()) { \ + valid[I + O] = true; \ + ++count; \ + } \ + } + + MAGIC_ENUM_FOR_EACH_256(MAGIC_ENUM_V) + + if constexpr ((I + 256) < Size) { + valid_count(valid, count); + } +#undef MAGIC_ENUM_V +} + +template +struct valid_count_t { + std::size_t count = 0; + bool valid[N] = {}; +}; + +template +constexpr auto valid_count() noexcept { + valid_count_t vc; + valid_count(vc.valid, vc.count); + return vc; +} + +template +constexpr auto values() noexcept { + constexpr auto vc = valid_count(); + + if constexpr (vc.count > 0) { +#if defined(MAGIC_ENUM_ARRAY_CONSTEXPR) + std::array values = {}; +#else + E values[vc.count] = {}; +#endif + for (std::size_t i = 0, v = 0; v < vc.count; ++i) { + if (vc.valid[i]) { + values[v++] = value(i); + } + } +#if defined(MAGIC_ENUM_ARRAY_CONSTEXPR) + return values; +#else + return to_array(values, std::make_index_sequence{}); +#endif + } else { + return std::array{}; + } +} + +template > +constexpr auto values() noexcept { + constexpr auto min = reflected_min(); + constexpr auto max = reflected_max(); + constexpr auto range_size = max - min + 1; + static_assert(range_size > 0, "magic_enum::enum_range requires valid size."); + + return values(); +} + +template > +constexpr enum_subtype subtype(std::true_type) noexcept { + if constexpr (std::is_same_v) { // bool special case + return enum_subtype::common; + } else if constexpr (has_is_flags::value) { + return customize::enum_range::is_flags ? enum_subtype::flags : enum_subtype::common; + } else { +#if defined(MAGIC_ENUM_AUTO_IS_FLAGS) + constexpr auto flags_values = values(); + constexpr auto default_values = values(); + if (flags_values.size() == 0 || default_values.size() > flags_values.size()) { + return enum_subtype::common; + } + for (std::size_t i = 0; i < default_values.size(); ++i) { + const auto v = static_cast(default_values[i]); + if (v != 0 && (v & (v - 1)) != 0) { + return enum_subtype::common; + } + } + return enum_subtype::flags; +#else + return enum_subtype::common; +#endif + } +} + +template +constexpr enum_subtype subtype(std::false_type) noexcept { + // For non-enum type return default common subtype. + return enum_subtype::common; +} + +template > +inline constexpr auto subtype_v = subtype(std::is_enum{}); + +template +inline constexpr auto values_v = values(); + +template > +using values_t = decltype((values_v)); + +template +inline constexpr auto count_v = values_v.size(); + +template > +inline constexpr auto min_v = (count_v > 0) ? static_cast(values_v.front()) : U{0}; + +template > +inline constexpr auto max_v = (count_v > 0) ? static_cast(values_v.back()) : U{0}; + +template +constexpr auto names(std::index_sequence) noexcept { + constexpr auto names = std::array{{enum_name_v[I]>.str()...}}; + return names; +} + +template +inline constexpr auto names_v = names(std::make_index_sequence>{}); + +template > +using names_t = decltype((names_v)); + +template +constexpr auto entries(std::index_sequence) noexcept { + constexpr auto entries = std::array, sizeof...(I)>{{{values_v[I], enum_name_v[I]>.str()}...}}; + return entries; +} + +template +inline constexpr auto entries_v = entries(std::make_index_sequence>{}); + +template > +using entries_t = decltype((entries_v)); + +template > +constexpr bool is_sparse() noexcept { + if constexpr (count_v == 0) { + return false; + } else if constexpr (std::is_same_v) { // bool special case + return false; + } else { + constexpr auto max = (S == enum_subtype::flags) ? log2(max_v) : max_v; + constexpr auto min = (S == enum_subtype::flags) ? log2(min_v) : min_v; + constexpr auto range_size = max - min + 1; + + return range_size != count_v; + } +} + +template > +inline constexpr bool is_sparse_v = is_sparse(); + +template +struct is_reflected +#if defined(MAGIC_ENUM_NO_CHECK_REFLECTED_ENUM) + : std::true_type {}; +#else + : std::bool_constant && (count_v != 0)> {}; +#endif + +template +inline constexpr bool is_reflected_v = is_reflected, S>{}; + +template +struct enable_if_enum {}; + +template +struct enable_if_enum { + using type = R; + static_assert(supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); +}; + +template , typename D = std::decay_t> +using enable_if_t = typename enable_if_enum && std::is_invocable_r_v, R>::type; + +template >, int> = 0> +using enum_concept = T; + +template > +struct is_scoped_enum : std::false_type {}; + +template +struct is_scoped_enum : std::bool_constant>> {}; + +template > +struct is_unscoped_enum : std::false_type {}; + +template +struct is_unscoped_enum : std::bool_constant>> {}; + +template >> +struct underlying_type {}; + +template +struct underlying_type : std::underlying_type> {}; + +#if defined(MAGIC_ENUM_ENABLE_HASH) || defined(MAGIC_ENUM_ENABLE_HASH_SWITCH) + +template +struct constexpr_hash_t; + +template +struct constexpr_hash_t>> { + constexpr auto operator()(Value value) const noexcept { + using U = typename underlying_type::type; + if constexpr (std::is_same_v) { // bool special case + return static_cast(value); + } else { + return static_cast(value); + } + } + using secondary_hash = constexpr_hash_t; +}; + +template +struct constexpr_hash_t>> { + static constexpr std::uint32_t crc_table[256] { + 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, 0x706af48fL, 0xe963a535L, 0x9e6495a3L, + 0x0edb8832L, 0x79dcb8a4L, 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, 0x90bf1d91L, + 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL, 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, + 0x136c9856L, 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L, 0xfa0f3d63L, 0x8d080df5L, + 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L, 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL, + 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L, 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, + 0x26d930acL, 0x51de003aL, 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, 0xb8bda50fL, + 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L, 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, + 0x76dc4190L, 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL, 0x9fbfe4a5L, 0xe8b8d433L, + 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL, 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L, + 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL, 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, + 0x65b0d9c6L, 0x12b7e950L, 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L, 0xfbd44c65L, + 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L, 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, + 0x4369e96aL, 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L, 0xaa0a4c5fL, 0xdd0d7cc9L, + 0x5005713cL, 0x270241aaL, 0xbe0b1010L, 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL, + 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L, 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, + 0xedb88320L, 0x9abfb3b6L, 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L, 0x73dc1683L, + 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L, 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, + 0xf00f9344L, 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL, 0x196c3671L, 0x6e6b06e7L, + 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL, 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L, + 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L, 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, + 0xd80d2bdaL, 0xaf0a1b4cL, 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL, 0x4669be79L, + 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L, 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, + 0xc5ba3bbeL, 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L, 0x2cd99e8bL, 0x5bdeae1dL, + 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL, 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L, + 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL, 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, + 0x86d3d2d4L, 0xf1d4e242L, 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L, 0x18b74777L, + 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL, 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, + 0xa00ae278L, 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L, 0x4969474dL, 0x3e6e77dbL, + 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L, + 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L, 0xcdd70693L, 0x54de5729L, 0x23d967bfL, + 0xb3667a2eL, 0xc4614ab8L, 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, 0x2d02ef8dL + }; + constexpr std::uint32_t operator()(string_view value) const noexcept { + auto crc = static_cast(0xffffffffL); + for (const auto c : value) { + crc = (crc >> 8) ^ crc_table[(crc ^ static_cast(c)) & 0xff]; + } + return crc ^ 0xffffffffL; + } + + struct secondary_hash { + constexpr std::uint32_t operator()(string_view value) const noexcept { + auto acc = static_cast(2166136261ULL); + for (const auto c : value) { + acc = ((acc ^ static_cast(c)) * static_cast(16777619ULL)) & (std::numeric_limits::max)(); + } + return static_cast(acc); + } + }; +}; + +template +inline constexpr Hash hash_v{}; + +template +constexpr auto calculate_cases(std::size_t Page) noexcept { + constexpr std::array values = *GlobValues; + constexpr std::size_t size = values.size(); + + using switch_t = std::invoke_result_t; + static_assert(std::is_integral_v && !std::is_same_v); + const std::size_t values_to = (std::min)(static_cast(256), size - Page); + + std::array result{}; + auto fill = result.begin(); + { + auto first = values.begin() + static_cast(Page); + auto last = values.begin() + static_cast(Page + values_to); + while (first != last) { + *fill++ = hash_v(*first++); + } + } + + // dead cases, try to avoid case collisions + for (switch_t last_value = result[values_to - 1]; fill != result.end() && last_value != (std::numeric_limits::max)(); *fill++ = ++last_value) { + } + + { + auto it = result.begin(); + auto last_value = (std::numeric_limits::min)(); + for (; fill != result.end(); *fill++ = last_value++) { + while (last_value == *it) { + ++last_value, ++it; + } + } + } + + return result; +} + +template +constexpr R invoke_r(F&& f, Args&&... args) noexcept(std::is_nothrow_invocable_r_v) { + if constexpr (std::is_void_v) { + std::forward(f)(std::forward(args)...); + } else { + return static_cast(std::forward(f)(std::forward(args)...)); + } +} + +enum class case_call_t { + index, + value +}; + +template +inline constexpr auto default_result_type_lambda = []() noexcept(std::is_nothrow_default_constructible_v) { return T{}; }; + +template <> +inline constexpr auto default_result_type_lambda = []() noexcept {}; + +template +constexpr bool has_duplicate() noexcept { + using value_t = std::decay_t; + using hash_value_t = std::invoke_result_t; + std::arraysize()> hashes{}; + std::size_t size = 0; + for (auto elem : *Arr) { + hashes[size] = hash_v(elem); + for (auto i = size++; i > 0; --i) { + if (hashes[i] < hashes[i - 1]) { + auto tmp = hashes[i]; + hashes[i] = hashes[i - 1]; + hashes[i - 1] = tmp; + } else if (hashes[i] == hashes[i - 1]) { + return false; + } else { + break; + } + } + } + return true; +} + +#define MAGIC_ENUM_CASE(val) \ + case cases[val]: \ + if constexpr ((val) + Page < size) { \ + if (!pred(values[val + Page], searched)) { \ + break; \ + } \ + if constexpr (CallValue == case_call_t::index) { \ + if constexpr (std::is_invocable_r_v>) { \ + return detail::invoke_r(std::forward(lambda), std::integral_constant{}); \ + } else if constexpr (std::is_invocable_v>) { \ + MAGIC_ENUM_ASSERT(false && "magic_enum::detail::constexpr_switch wrong result type."); \ + } \ + } else if constexpr (CallValue == case_call_t::value) { \ + if constexpr (std::is_invocable_r_v>) { \ + return detail::invoke_r(std::forward(lambda), enum_constant{}); \ + } else if constexpr (std::is_invocable_r_v>) { \ + MAGIC_ENUM_ASSERT(false && "magic_enum::detail::constexpr_switch wrong result type."); \ + } \ + } \ + break; \ + } else [[fallthrough]]; + +template ::value_type>, + typename BinaryPredicate = std::equal_to<>, + typename Lambda, + typename ResultGetterType> +constexpr decltype(auto) constexpr_switch( + Lambda&& lambda, + typename std::decay_t::value_type searched, + ResultGetterType&& def, + BinaryPredicate&& pred = {}) { + using result_t = std::invoke_result_t; + using hash_t = std::conditional_t(), Hash, typename Hash::secondary_hash>; + static_assert(has_duplicate(), "magic_enum::detail::constexpr_switch duplicated hash found, please report it: https://github.com/Neargye/magic_enum/issues."); + constexpr std::array values = *GlobValues; + constexpr std::size_t size = values.size(); + constexpr std::array cases = calculate_cases(Page); + + switch (hash_v(searched)) { + MAGIC_ENUM_FOR_EACH_256(MAGIC_ENUM_CASE) + default: + if constexpr (size > 256 + Page) { + return constexpr_switch(std::forward(lambda), searched, std::forward(def)); + } + break; + } + return def(); +} + +#undef MAGIC_ENUM_CASE + +#endif + +} // namespace magic_enum::detail + +// Checks is magic_enum supported compiler. +inline constexpr bool is_magic_enum_supported = detail::supported::value; + +template +using Enum = detail::enum_concept; + +// Checks whether T is an Unscoped enumeration type. +// Provides the member constant value which is equal to true, if T is an [Unscoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration) type. Otherwise, value is equal to false. +template +struct is_unscoped_enum : detail::is_unscoped_enum {}; + +template +inline constexpr bool is_unscoped_enum_v = is_unscoped_enum::value; + +// Checks whether T is an Scoped enumeration type. +// Provides the member constant value which is equal to true, if T is an [Scoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations) type. Otherwise, value is equal to false. +template +struct is_scoped_enum : detail::is_scoped_enum {}; + +template +inline constexpr bool is_scoped_enum_v = is_scoped_enum::value; + +// If T is a complete enumeration type, provides a member typedef type that names the underlying type of T. +// Otherwise, if T is not an enumeration type, there is no member type. Otherwise (T is an incomplete enumeration type), the program is ill-formed. +template +struct underlying_type : detail::underlying_type {}; + +template +using underlying_type_t = typename underlying_type::type; + +template +using enum_constant = detail::enum_constant; + +// Returns type name of enum. +template +[[nodiscard]] constexpr auto enum_type_name() noexcept -> detail::enable_if_t { + constexpr string_view name = detail::type_name_v>.str(); + static_assert(!name.empty(), "magic_enum::enum_type_name enum type does not have a name."); + + return name; +} + +// Returns number of enum values. +template > +[[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_t { + return detail::count_v, S>; +} + +// Returns enum value at specified index. +// No bounds checking is performed: the behavior is undefined if index >= number of enum values. +template > +[[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_t> { + using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + + if constexpr (detail::is_sparse_v) { + return MAGIC_ENUM_ASSERT(index < detail::count_v), detail::values_v[index]; + } else { + constexpr auto min = (S == detail::enum_subtype::flags) ? detail::log2(detail::min_v) : detail::min_v; + + return MAGIC_ENUM_ASSERT(index < detail::count_v), detail::value(index); + } +} + +// Returns enum value at specified index. +template > +[[nodiscard]] constexpr auto enum_value() noexcept -> detail::enable_if_t> { + using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + static_assert(I < detail::count_v, "magic_enum::enum_value out of range."); + + return enum_value(I); +} + +// Returns std::array with enum values, sorted by enum value. +template > +[[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_t> { + using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + + return detail::values_v; +} + +// Returns integer value from enum value. +template +[[nodiscard]] constexpr auto enum_integer(E value) noexcept -> detail::enable_if_t> { + return static_cast>(value); +} + +// Returns underlying value from enum value. +template +[[nodiscard]] constexpr auto enum_underlying(E value) noexcept -> detail::enable_if_t> { + return static_cast>(value); +} + +// Obtains index in enum values from enum value. +// Returns optional with index. +template > +[[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_t> { + using D = std::decay_t; + using U = underlying_type_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + + if constexpr (detail::is_sparse_v || (S == detail::enum_subtype::flags)) { +#if defined(MAGIC_ENUM_ENABLE_HASH) + return detail::constexpr_switch<&detail::values_v, detail::case_call_t::index>( + [](std::size_t i) { return optional{i}; }, + value, + detail::default_result_type_lambda>); +#else + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (enum_value(i) == value) { + return i; + } + } + return {}; // Invalid value or out of range. +#endif + } else { + const auto v = static_cast(value); + if (v >= detail::min_v && v <= detail::max_v) { + return static_cast(v - detail::min_v); + } + return {}; // Invalid value or out of range. + } +} + +// Obtains index in enum values from enum value. +// Returns optional with index. +template +[[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_t> { + using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + + return enum_index(value); +} + +// Obtains index in enum values from static storage enum variable. +template >> +[[nodiscard]] constexpr auto enum_index() noexcept -> detail::enable_if_t {\ + using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + constexpr auto index = enum_index(V); + static_assert(index, "magic_enum::enum_index enum value does not have a index."); + + return *index; +} + +// Returns name from static storage enum variable. +// This version is much lighter on the compile times and is not restricted to the enum_range limitation. +template +[[nodiscard]] constexpr auto enum_name() noexcept -> detail::enable_if_t { + constexpr string_view name = detail::enum_name_v, V>.str(); + static_assert(!name.empty(), "magic_enum::enum_name enum value does not have a name."); + + return name; +} + +// Returns name from enum value. +// If enum value does not have name or value out of range, returns empty string. +template > +[[nodiscard]] constexpr auto enum_name(E value) noexcept -> detail::enable_if_t { + using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + + if (const auto i = enum_index(value)) { + return detail::names_v[*i]; + } + return detail::static_str<0>{}.str(); +} + +// Returns name from enum value. +// If enum value does not have name or value out of range, returns empty string. +template +[[nodiscard]] constexpr auto enum_name(E value) -> detail::enable_if_t { + using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + + return enum_name(value); +} + +// Returns std::array with names, sorted by enum value. +template > +[[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_t> { + using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + + return detail::names_v; +} + +// Returns std::array with pairs (value, name), sorted by enum value. +template > +[[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_t> { + using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + + return detail::entries_v; +} + +// Allows you to write magic_enum::enum_cast("bar", magic_enum::case_insensitive); +inline constexpr auto case_insensitive = detail::case_insensitive<>{}; + +// Obtains enum value from integer value. +// Returns optional with enum value. +template > +[[nodiscard]] constexpr auto enum_cast(underlying_type_t value) noexcept -> detail::enable_if_t>> { + using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + + if constexpr (detail::is_sparse_v || (S == detail::enum_subtype::flags)) { +#if defined(MAGIC_ENUM_ENABLE_HASH) + return detail::constexpr_switch<&detail::values_v, detail::case_call_t::value>( + [](D v) { return optional{v}; }, + static_cast(value), + detail::default_result_type_lambda>); +#else + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (value == static_cast>(enum_value(i))) { + return static_cast(value); + } + } + return {}; // Invalid value or out of range. +#endif + } else { + if (value >= detail::min_v && value <= detail::max_v) { + return static_cast(value); + } + return {}; // Invalid value or out of range. + } +} + +// Obtains enum value from name. +// Returns optional with enum value. +template , typename BinaryPredicate = std::equal_to<>> +[[nodiscard]] constexpr auto enum_cast(string_view value, [[maybe_unused]] BinaryPredicate p = {}) noexcept(detail::is_nothrow_invocable_v) -> detail::enable_if_t>, BinaryPredicate> { + using D = std::decay_t; + static_assert(detail::is_reflected_v, "magic_enum requires enum implementation and valid max and min."); + +#if defined(MAGIC_ENUM_ENABLE_HASH) + if constexpr (detail::is_default_predicate_v) { + return detail::constexpr_switch<&detail::names_v, detail::case_call_t::index>( + [](std::size_t i) { return optional{detail::values_v[i]}; }, + value, + detail::default_result_type_lambda>, + [&p](string_view lhs, string_view rhs) { return detail::cmp_equal(lhs, rhs, p); }); + } +#endif + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (detail::cmp_equal(value, detail::names_v[i], p)) { + return enum_value(i); + } + } + return {}; // Invalid value or out of range. +} + +// Checks whether enum contains value with such value. +template > +[[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_t { + using D = std::decay_t; + using U = underlying_type_t; + + return static_cast(enum_cast(static_cast(value))); +} + +// Checks whether enum contains value with such value. +template +[[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_t { + using D = std::decay_t; + using U = underlying_type_t; + + return static_cast(enum_cast(static_cast(value))); +} + +// Checks whether enum contains value with such integer value. +template > +[[nodiscard]] constexpr auto enum_contains(underlying_type_t value) noexcept -> detail::enable_if_t { + using D = std::decay_t; + + return static_cast(enum_cast(value)); +} + +// Checks whether enum contains enumerator with such name. +template , typename BinaryPredicate = std::equal_to<>> +[[nodiscard]] constexpr auto enum_contains(string_view value, BinaryPredicate p = {}) noexcept(detail::is_nothrow_invocable_v) -> detail::enable_if_t { + using D = std::decay_t; + + return static_cast(enum_cast(value, std::move(p))); +} + +// Returns true if the enum integer value is in the range of values that can be reflected. +template > +[[nodiscard]] constexpr auto enum_reflected(underlying_type_t value) noexcept -> detail::enable_if_t { + using D = std::decay_t; + + if constexpr (detail::is_reflected_v) { + constexpr auto min = detail::reflected_min(); + constexpr auto max = detail::reflected_max(); + return value >= min && value <= max; + } else { + return false; + } +} + +// Returns true if the enum value is in the range of values that can be reflected. +template > +[[nodiscard]] constexpr auto enum_reflected(E value) noexcept -> detail::enable_if_t { + using D = std::decay_t; + + return enum_reflected(static_cast>(value)); +} + +// Returns true if the enum value is in the range of values that can be reflected. +template +[[nodiscard]] constexpr auto enum_reflected(E value) noexcept -> detail::enable_if_t { + using D = std::decay_t; + + return enum_reflected(value); +} + +template +inline constexpr auto as_flags = AsFlags ? detail::enum_subtype::flags : detail::enum_subtype::common; + +template +inline constexpr auto as_common = AsFlags ? detail::enum_subtype::common : detail::enum_subtype::flags; + +namespace bitwise_operators { + +template = 0> +constexpr E operator~(E rhs) noexcept { + return static_cast(~static_cast>(rhs)); +} + +template = 0> +constexpr E operator|(E lhs, E rhs) noexcept { + return static_cast(static_cast>(lhs) | static_cast>(rhs)); +} + +template = 0> +constexpr E operator&(E lhs, E rhs) noexcept { + return static_cast(static_cast>(lhs) & static_cast>(rhs)); +} + +template = 0> +constexpr E operator^(E lhs, E rhs) noexcept { + return static_cast(static_cast>(lhs) ^ static_cast>(rhs)); +} + +template = 0> +constexpr E& operator|=(E& lhs, E rhs) noexcept { + return lhs = (lhs | rhs); +} + +template = 0> +constexpr E& operator&=(E& lhs, E rhs) noexcept { + return lhs = (lhs & rhs); +} + +template = 0> +constexpr E& operator^=(E& lhs, E rhs) noexcept { + return lhs = (lhs ^ rhs); +} + +} // namespace magic_enum::bitwise_operators + +} // namespace magic_enum + +#if defined(__clang__) +# pragma clang diagnostic pop +#elif defined(__GNUC__) +# pragma GCC diagnostic pop +#elif defined(_MSC_VER) +# pragma warning(pop) +#endif + +#undef MAGIC_ENUM_GET_ENUM_NAME_BUILTIN +#undef MAGIC_ENUM_GET_TYPE_NAME_BUILTIN +#undef MAGIC_ENUM_VS_2017_WORKAROUND +#undef MAGIC_ENUM_ARRAY_CONSTEXPR +#undef MAGIC_ENUM_FOR_EACH_256 + +#endif // NEARGYE_MAGIC_ENUM_HPP diff --git a/projects/llm_framework/main_llm2/src/runner/utils/ax_cmm_utils.hpp b/projects/llm_framework/main_llm2/src/runner/utils/ax_cmm_utils.hpp new file mode 100644 index 0000000..c739f65 --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/utils/ax_cmm_utils.hpp @@ -0,0 +1,61 @@ +#pragma once +#include +#include +#include +#include + +// #include "sample_log.h" + +static std::string exec_cmd(std::string cmd) +{ + FILE *pipe = popen(cmd.c_str(), "r"); + if (!pipe) + { + return ""; + } + char buffer[128]; + std::string result = ""; + while (!feof(pipe)) + { + if (fgets(buffer, 128, pipe) != NULL) + { + result += buffer; + } + } + pclose(pipe); + return result; +} + +static int get_remaining_cmm_size() +{ + std::string cmd = "cat /proc/ax_proc/mem_cmm_info |grep 'total size'"; + std::string result = exec_cmd(cmd); + + std::regex pattern("remain=(\\d+)KB\\((\\d+)MB \\+ (\\d+)KB\\)"); + std::smatch match; + if (std::regex_search(result, match, pattern)) + { + int remain_kb = std::stoi(match[1]); + int remain_mb = std::stoi(match[2]); + return remain_mb; + } + return -1; +} + +static int get_pcie_remaining_cmm_size(int devid) +{ + char command[128]; + sprintf(command, "/usr/bin/axcl/axcl-smi -d %d sh cat /proc/ax_proc/mem_cmm_info |grep 'total size'", devid); + // printf("%s\n", command); + std::string result = exec_cmd(command); + + std::regex pattern("remain=(\\d+)KB\\((\\d+)MB \\+ (\\d+)KB\\)"); + std::smatch match; + if (std::regex_search(result, match, pattern)) + { + int remain_kb = std::stoi(match[1]); + int remain_mb = std::stoi(match[2]); + return remain_mb; + } + return -1; +} \ No newline at end of file diff --git a/projects/llm_framework/main_llm2/src/runner/utils/ax_cv.hpp b/projects/llm_framework/main_llm2/src/runner/utils/ax_cv.hpp new file mode 100644 index 0000000..60742f1 --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/utils/ax_cv.hpp @@ -0,0 +1,96 @@ +#pragma once + +#include +#include +#include + +// Minimal OpenCV-like facade used by vision preprocessing. +// +// Backend selection: +// - If OpenCV is found by CMake: compile with -DAXLLM_USE_OPENCV and use cv::Mat. +// - Otherwise: use SimpleCV (stb-based) as a fallback. +// +// Note: SimpleCV resize/cvtColor may have slight numeric differences vs OpenCV. + +#if defined(AXLLM_USE_OPENCV) +#include + +namespace axcv { + +using Mat = cv::Mat; + +static constexpr int IMREAD_COLOR = cv::IMREAD_COLOR; + +inline bool empty(const Mat& m) { return m.empty(); } +inline int width(const Mat& m) { return m.cols; } +inline int height(const Mat& m) { return m.rows; } +inline int channels(const Mat& m) { return m.channels(); } +inline int step_bytes(const Mat& m) { return (int)m.step; } +inline const uint8_t* row_ptr(const Mat& m, int y) { return m.ptr(y); } +inline uint8_t* row_ptr(Mat& m, int y) { return m.ptr(y); } + +inline Mat imread(const std::string& path, int flags = IMREAD_COLOR) { return cv::imread(path, flags); } + +inline void resize(const Mat& src, Mat& dst, int dst_w, int dst_h) +{ + cv::resize(src, dst, cv::Size(dst_w, dst_h), 0, 0, cv::INTER_CUBIC); +} + +inline void cvtColorBGR2RGB(const Mat& src, Mat& dst) { cv::cvtColor(src, dst, cv::COLOR_BGR2RGB); } + +inline Mat crop_clone(const Mat& src, int x, int y, int w, int h) +{ + return src(cv::Rect(x, y, w, h)).clone(); +} + +} // namespace axcv + +#else + +#include "SimpleCV.hpp" + +namespace axcv { + +using Mat = SimpleCV::Mat; + +// Kept for call-site compatibility (ignored by SimpleCV wrapper). +static constexpr int IMREAD_COLOR = 1; + +inline bool empty(const Mat& m) { return m.empty(); } +inline int width(const Mat& m) { return m.width; } +inline int height(const Mat& m) { return m.height; } +inline int channels(const Mat& m) { return m.channels; } +inline int step_bytes(const Mat& m) { return m.step; } +inline const uint8_t* row_ptr(const Mat& m, int y) { return (const uint8_t*)(m.data + (size_t)y * (size_t)m.step); } +inline uint8_t* row_ptr(Mat& m, int y) { return (uint8_t*)(m.data + (size_t)y * (size_t)m.step); } + +inline Mat imread(const std::string& path, int /*flags*/ = IMREAD_COLOR) +{ + // Match OpenCV's cv::imread(IMREAD_COLOR) behavior (BGR 3-channel). + return SimpleCV::imread(path, SimpleCV::ColorSpace::BGR); +} + +inline void resize(const Mat& src, Mat& dst, int dst_w, int dst_h) { SimpleCV::resize(src, dst, dst_w, dst_h); } + +inline void cvtColorBGR2RGB(const Mat& src, Mat& dst) +{ + SimpleCV::cvtColor(src, dst, SimpleCV::ColorSpace::RGB, SimpleCV::ColorSpace::BGR); +} + +inline Mat crop_clone(const Mat& src, int x, int y, int w, int h) +{ + if (src.empty() || w <= 0 || h <= 0) return Mat(); + Mat out(h, w, src.channels); + const int c = src.channels; + for (int r = 0; r < h; ++r) { + const uint8_t* sp = row_ptr(src, y + r) + (size_t)x * (size_t)c; + uint8_t* dp = row_ptr(out, r); + std::memcpy(dp, sp, (size_t)w * (size_t)c); + } + return out; +} + +} // namespace axcv + +#endif + diff --git a/projects/llm_framework/main_llm2/src/runner/utils/bfloat16.hpp b/projects/llm_framework/main_llm2/src/runner/utils/bfloat16.hpp new file mode 100644 index 0000000..157e07d --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/utils/bfloat16.hpp @@ -0,0 +1,71 @@ +#pragma once +#include + +struct bfloat16 +{ + unsigned short int data; + +public: + bfloat16() + { + data = 0; + } + bfloat16(float float_val) + { + *this = float_val; + } + + bfloat16(unsigned short int _data) + { + data = _data; + } + + float fp32() + { + return *this; + } + + // cast to float + operator float() + { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wuninitialized" + unsigned int proc = data << 16; + return *reinterpret_cast(&proc); +#pragma GCC diagnostic pop + } + // cast to bfloat16 + bfloat16 &operator=(float float_val) + { + data = (*reinterpret_cast(&float_val)) >> 16; + return *this; + } +}; + +static std::vector> topk_bfloat16(unsigned short *arr, int size, int k) +{ + std::vector> result; + + // Create a vector of pairs with index and value + std::vector> indexedValues; + indexedValues.reserve(size); + for (int i = 0; i < size; ++i) + { + indexedValues.push_back(std::make_pair(i, bfloat16(arr[i]))); + } + + // Sort the vector based on the values in descending order + std::sort(indexedValues.begin(), indexedValues.end(), + [](const std::pair &a, const std::pair &b) + { + return a.second > b.second; + }); + + // Take the top k elements + for (int i = 0; i < k; ++i) + { + result.push_back(indexedValues[i]); + } + + return result; +} \ No newline at end of file diff --git a/projects/llm_framework/main_llm2/src/runner/utils/cqdm.cpp b/projects/llm_framework/main_llm2/src/runner/utils/cqdm.cpp new file mode 100644 index 0000000..79a1979 --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/utils/cqdm.cpp @@ -0,0 +1,48 @@ +#include "cqdm.h" + +// Returns the current time in milliseconds +long long get_msec_now() +{ + struct timeval time; + + if (gettimeofday(&time, NULL) == -1) + return (0); + return (long long)time.tv_sec * 1000LL + (long long)time.tv_usec / 1000LL; +} + +t_cqdm create_cqdm(int total, int size) +{ + t_cqdm cqdm; + + cqdm.total = total; + cqdm.count = 0; + cqdm.average_time = 0; + cqdm.last_time = get_msec_now(); + cqdm.total_time = 0; + cqdm.size = size; + return (cqdm); +} + +void update_cqdm(t_cqdm *cqdm, int x, const char *unit, const char *log_str) +{ + long long now; + double temp; + + now = get_msec_now(); + + cqdm->count++; + cqdm->total_time += ((now - cqdm->last_time) / 1000); + cqdm->average_time = cqdm->total_time / cqdm->count; + cqdm->last_time = now; + + temp = ((double)(x + 1) / (double)cqdm->total); + + fprintf(stdout, "\033[1;30;33m\r%3d%% | ", (int)(temp * 100)); + for (int i = 0; i < (temp * cqdm->size); i++) + fprintf(stdout, "█"); + for (int i = 0; i < (cqdm->size - temp * cqdm->size); i++) + fprintf(stdout, " "); + fprintf(stdout, " | %3d / %3d [%2.2fs<%2.2fs, %2.2f %s/s] \033[1;30;32m%s\033[0m", x + 1, cqdm->total, cqdm->total_time, + cqdm->average_time * cqdm->total, 1 / cqdm->average_time, unit, log_str); + fflush(stdout); +} \ No newline at end of file diff --git a/projects/llm_framework/main_llm2/src/runner/utils/cqdm.h b/projects/llm_framework/main_llm2/src/runner/utils/cqdm.h new file mode 100644 index 0000000..c35fb72 --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/utils/cqdm.h @@ -0,0 +1,28 @@ +#ifndef CQDM_H +#define CQDM_H + +#include +#include +#include + +// Structure to hold progress bar information +typedef struct s_cqdm +{ + int total; + int count; + int size; + double average_time; + double last_time; + double total_time; +} t_cqdm; +#ifdef __cplusplus +extern "C" +{ +#endif + t_cqdm create_cqdm(int total, int size); + void update_cqdm(t_cqdm *cqdm, int x, const char *unit, const char *log_str); +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/projects/llm_framework/main_llm2/src/runner/utils/files.cpp b/projects/llm_framework/main_llm2/src/runner/utils/files.cpp new file mode 100644 index 0000000..4523666 --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/utils/files.cpp @@ -0,0 +1,45 @@ +#include "files.hpp" + +#include +#include +#include +#include +#include + +bool is_directory(const std::string& path) { + struct stat st; + if (stat(path.c_str(), &st) != 0) return false; + return S_ISDIR(st.st_mode); +} + +bool is_file(const std::string& path) { + struct stat st; + if (stat(path.c_str(), &st) != 0) return false; + return S_ISREG(st.st_mode); +} + +std::vector list_files(const std::string& directory) { + std::vector files; + DIR* dir = opendir(directory.c_str()); + if (!dir) { + std::cerr << "failed to open directory: " << directory << "\n"; + return files; + } + + struct dirent* entry; + while ((entry = readdir(dir)) != nullptr) { + std::string name = entry->d_name; + if (name == "." || name == "..") continue; + + std::string full_path = directory + "/" + name; + struct stat st; + if (stat(full_path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) { + files.push_back(full_path); + } + } + closedir(dir); + + std::sort(files.begin(), files.end()); + return files; +} + diff --git a/projects/llm_framework/main_llm2/src/runner/utils/files.hpp b/projects/llm_framework/main_llm2/src/runner/utils/files.hpp new file mode 100644 index 0000000..4f79937 --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/utils/files.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include +#include + +bool is_directory(const std::string& path); +bool is_file(const std::string& path); +std::vector list_files(const std::string& directory); + diff --git a/projects/llm_framework/main_llm2/src/runner/utils/http_utils.hpp b/projects/llm_framework/main_llm2/src/runner/utils/http_utils.hpp new file mode 100644 index 0000000..5041b6a --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/utils/http_utils.hpp @@ -0,0 +1,102 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * @brief Attempts to establish a TCP connection to a specified host and port. + * + * This function creates a socket and tries to connect to a server specified by + * the host and port parameters. It returns true if the connection is successful, + * otherwise it returns false and outputs an error message to standard error. + * + * @param host The IP address of the server to connect to. + * @param port The port number of the server to connect to. + * + * @return true if the connection is successfully established, false otherwise. + */ +static bool test_connect(const std::string &host, int port) +{ + int sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) + { + // std::cerr << "Socket creation failed\n"; + return false; + } + + sockaddr_in server_addr{}; + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(port); + + if (inet_pton(AF_INET, host.c_str(), &server_addr.sin_addr) <= 0) + { + // std::cerr << "IP address conversion failed\n"; + close(sock); + return false; + } + + if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) + { + // std::cerr << "Connection failed\n"; + close(sock); + return false; + } + + close(sock); + return true; +} + +/** + * @brief Attempts to establish an HTTP connection to a specified URL with a timeout. + * + * This function parses the provided HTTP URL to extract the host and port information, + * and attempts to establish a TCP connection using the `test_connect` function. + * It retries the connection until the specified timeout is reached. + * + * @param http_url The HTTP URL of the server to connect to. + * @param timeout The maximum number of seconds to keep attempting the connection. + * + * @return true if the connection is successfully established within the timeout period, + * false otherwise. + */ +static bool test_connect_http(const std::string &http_url, int timeout) +{ + size_t pos = http_url.find("://"); + if (pos == std::string::npos) + return false; + std::string host = http_url.substr(pos + 3); + pos = host.find('/'); + if (pos != std::string::npos) + host = host.substr(0, pos); + pos = host.find(':'); + int port = 80; + if (pos != std::string::npos) + { + port = std::stoi(host.substr(pos + 1)); + host = host.substr(0, pos); + } + else + { + return false; + } + + if (host == "localhost") + host = "127.0.0.1"; + int tmp = timeout; + while (timeout--) + { + if (test_connect(host, port)) + return true; + std::this_thread::sleep_for(std::chrono::seconds(1)); + printf("\033[1;30;31m" + "connect failed %s, try again in %2d/%2d \n" + "\033[0m", + http_url.c_str(), timeout, tmp); + } + return false; +} \ No newline at end of file diff --git a/projects/llm_framework/main_llm2/src/runner/utils/image_processor.cpp b/projects/llm_framework/main_llm2/src/runner/utils/image_processor.cpp new file mode 100644 index 0000000..d784b67 --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/utils/image_processor.cpp @@ -0,0 +1,234 @@ +#include "image_processor.hpp" + +#include +#include +#include +#include + +#include "files.hpp" + +std::vector ReadImages(const std::string& path) { + std::vector src; + + if (is_file(path)) { + axcv::Mat img = axcv::imread(path, axcv::IMREAD_COLOR); + if (axcv::empty(img)) { + std::cerr << "failed to read image: " << path << "\n"; + return {}; + } + src.push_back(img); + return src; + } + + if (is_directory(path)) { + auto paths = list_files(path); + for (auto& p : paths) { + axcv::Mat img = axcv::imread(p, axcv::IMREAD_COLOR); + if (axcv::empty(img)) { + std::cerr << "failed to read image: " << p << "\n"; + continue; + } + src.push_back(img); + } + return src; + } + + std::cerr << "invalid path: " << path << "\n"; + return {}; +} + +int Qwen2VideoProcessor(std::vector& src, + std::vector>& output, + int tgt_h, int tgt_w, + int temporal_patch_size, + int merge_size, + int patch_size) { + if (src.empty()) return 0; + + std::vector imgs_resized; + imgs_resized.reserve(src.size()); + + for (auto& img : src) { + axcv::Mat img_rs; + if (axcv::width(img) != tgt_w || axcv::height(img) != tgt_h) { + axcv::resize(img, img_rs, tgt_w, tgt_h); + } else { + img_rs = img; + } + axcv::Mat rgb; + axcv::cvtColorBGR2RGB(img_rs, rgb); + img_rs = std::move(rgb); + imgs_resized.push_back(img_rs); + } + + if (imgs_resized.empty()) return 0; + + // Qwen patchifier expects even number of frames for temporal_patch_size=2. + if (temporal_patch_size > 1 && (imgs_resized.size() % (size_t)temporal_patch_size) != 0) { + while ((imgs_resized.size() % (size_t)temporal_patch_size) != 0) { + imgs_resized.push_back(imgs_resized.back()); + } + } + + const int channel = axcv::channels(imgs_resized[0]); // 3 + std::vector patches; + patches.resize(imgs_resized.size() * (size_t)tgt_w * (size_t)tgt_h * (size_t)channel); + // Pack to contiguous [T, H, W, C] regardless of backend stride. + for (size_t i = 0; i < imgs_resized.size(); ++i) { + const auto& m = imgs_resized[i]; + unsigned char* dst = patches.data() + i * (size_t)tgt_w * (size_t)tgt_h * (size_t)channel; + for (int r = 0; r < tgt_h; ++r) { + const uint8_t* sp = axcv::row_ptr(m, r); + std::memcpy(dst + (size_t)r * (size_t)tgt_w * (size_t)channel, sp, (size_t)tgt_w * (size_t)channel); + } + } + + const int grid_t = (int)imgs_resized.size() / temporal_patch_size; + const int grid_h = tgt_h / patch_size; + const int grid_w = tgt_w / patch_size; + + output.clear(); + output.reserve(grid_t); + + // Follow the reference reshape+transpose in the original branches. + for (int d0 = 0; d0 < grid_t; d0++) { + std::vector out_t; + out_t.reserve((size_t)(grid_h / merge_size) * (grid_w / merge_size) * merge_size * merge_size * + temporal_patch_size * patch_size * patch_size * channel); + for (int d2 = 0; d2 < grid_h / merge_size; d2++) { + for (int d5 = 0; d5 < grid_w / merge_size; d5++) { + for (int d3 = 0; d3 < merge_size; d3++) { + for (int d6 = 0; d6 < merge_size; d6++) { + for (int d1 = 0; d1 < temporal_patch_size; d1++) { + for (int d4 = 0; d4 < patch_size; d4++) { + for (int d7 = 0; d7 < patch_size; d7++) { + for (int d8 = 0; d8 < channel; d8++) { + size_t idx = (size_t)d0 * temporal_patch_size * grid_h * patch_size * grid_w * patch_size * channel; + idx += (size_t)d1 * grid_h * patch_size * grid_w * patch_size * channel; + idx += (size_t)d2 * merge_size * patch_size * grid_w * patch_size * channel; + idx += (size_t)d3 * patch_size * grid_w * patch_size * channel; + idx += (size_t)d4 * grid_w * patch_size * channel; + idx += (size_t)d5 * merge_size * patch_size * channel; + idx += (size_t)d6 * patch_size * channel; + idx += (size_t)d7 * channel; + idx += (size_t)d8; + + out_t.push_back(patches[idx]); + } + } + } + } + } + } + } + } + output.push_back(std::move(out_t)); + } + + return 0; +} + +static std::vector splitImageSafe(axcv::Mat src, int rows, int cols, int tile_w, int tile_h) { + std::vector subImages; + + const int full_w = std::max(1, tile_w) * cols; + const int full_h = std::max(1, tile_h) * rows; + + // SmolVLM2 reference: first resize to (2*tile_w)x(2*tile_h) then split to 2x2 tiles. + if (axcv::width(src) != full_w || axcv::height(src) != full_h) { + axcv::Mat img_rs; + axcv::resize(src, img_rs, full_w, full_h); + src = std::move(img_rs); + } + + const int subHeight = axcv::height(src) / rows; + const int subWidth = axcv::width(src) / cols; + + for (int i = 0; i < rows; i++) { + for (int j = 0; j < cols; j++) { + int x = j * subWidth; + int y = i * subHeight; + int w = (j == cols - 1) ? axcv::width(src) - x : subWidth; + int h = (i == rows - 1) ? axcv::height(src) - y : subHeight; + + if (x >= 0 && y >= 0 && x + w <= axcv::width(src) && y + h <= axcv::height(src)) { + axcv::Mat subImage = axcv::crop_clone(src, x, y, w, h); + if (axcv::width(subImage) != tile_w || axcv::height(subImage) != tile_h) { + axcv::Mat rs; + axcv::resize(subImage, rs, tile_w, tile_h); + subImage = std::move(rs); + } + subImages.push_back(std::move(subImage)); + } + } + } + + return subImages; +} + +int Smolvlm2ImageProcessor(std::vector& src, + std::vector>& output, + int tgt_w, + int tgt_h) { + if (src.empty()) return 0; + tgt_w = std::max(1, tgt_w); + tgt_h = std::max(1, tgt_h); + + std::vector resized; + resized.reserve(src.size() * 5); + + for (auto& img : src) { + auto splited = splitImageSafe(img, 2, 2, tgt_w, tgt_h); + resized.insert(resized.end(), splited.begin(), splited.end()); + if (axcv::width(img) != tgt_w || axcv::height(img) != tgt_h) { + axcv::Mat rs; + axcv::resize(img, rs, tgt_w, tgt_h); + img = std::move(rs); + } + resized.push_back(img); + } + + output.clear(); + output.reserve(resized.size()); + for (auto& img : resized) { + std::vector imgdata; + imgdata.resize((size_t)tgt_w * (size_t)tgt_h * 3); + // Pack in row-major contiguous. + for (int r = 0; r < tgt_h; ++r) { + const uint8_t* sp = axcv::row_ptr(img, r); + std::memcpy(imgdata.data() + (size_t)r * (size_t)tgt_w * 3, sp, (size_t)tgt_w * 3); + } + output.push_back(std::move(imgdata)); + } + + return 0; +} + +int Smolvlm2VideoProcessor(std::vector& src, + std::vector>& output, + int tgt_w, + int tgt_h) { + if (src.empty()) return 0; + tgt_w = std::max(1, tgt_w); + tgt_h = std::max(1, tgt_h); + + output.clear(); + output.reserve(src.size()); + + for (auto& img : src) { + if (axcv::width(img) != tgt_w || axcv::height(img) != tgt_h) { + axcv::Mat rs; + axcv::resize(img, rs, tgt_w, tgt_h); + img = std::move(rs); + } + std::vector imgdata; + imgdata.resize((size_t)tgt_w * (size_t)tgt_h * 3); + for (int r = 0; r < tgt_h; ++r) { + const uint8_t* sp = axcv::row_ptr(img, r); + std::memcpy(imgdata.data() + (size_t)r * (size_t)tgt_w * 3, sp, (size_t)tgt_w * 3); + } + output.push_back(std::move(imgdata)); + } + + return 0; +} diff --git a/projects/llm_framework/main_llm2/src/runner/utils/image_processor.hpp b/projects/llm_framework/main_llm2/src/runner/utils/image_processor.hpp new file mode 100644 index 0000000..445b2d0 --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/utils/image_processor.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include + +#include "ax_cv.hpp" + +// Read one image if `path` is file, or all regular files (sorted) if `path` is directory. +std::vector ReadImages(const std::string& path); + +// Qwen2.x/Qwen3-VL style patchifier for image/video frames. +// Output contains one entry per temporal grid segment. +int Qwen2VideoProcessor(std::vector& src, + std::vector>& output, + int tgt_h, int tgt_w, + int temporal_patch_size = 2, + int merge_size = 2, + int patch_size = 14); + +// SmolVLM2 image processor: +// For each input image, outputs 5 blocks: 2x2 tiles (4) + global resized image (1). +int Smolvlm2ImageProcessor(std::vector& src, + std::vector>& output, + int tgt_w = 512, + int tgt_h = 512); + +// SmolVLM2 video processor: +// For each frame, outputs 1 block (resized). +int Smolvlm2VideoProcessor(std::vector& src, + std::vector>& output, + int tgt_w = 512, + int tgt_h = 512); diff --git a/projects/llm_framework/main_llm2/src/runner/utils/json.hpp b/projects/llm_framework/main_llm2/src/runner/utils/json.hpp new file mode 100644 index 0000000..a858728 --- /dev/null +++ b/projects/llm_framework/main_llm2/src/runner/utils/json.hpp @@ -0,0 +1,24766 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + +/****************************************************************************\ + * Note on documentation: The source files contain links to the online * + * documentation of the public API at https://json.nlohmann.me. This URL * + * contains the most recent documentation and should also be applicable to * + * previous versions; documentation for deprecated functions is not * + * removed, but marked deprecated. See "Generate documentation" section in * + * file docs/README.md. * +\****************************************************************************/ + +#ifndef INCLUDE_NLOHMANN_JSON_HPP_ +#define INCLUDE_NLOHMANN_JSON_HPP_ + +#include // all_of, find, for_each +#include // nullptr_t, ptrdiff_t, size_t +#include // hash, less +#include // initializer_list +#ifndef JSON_NO_IO + #include // istream, ostream +#endif // JSON_NO_IO +#include // random_access_iterator_tag +#include // unique_ptr +#include // string, stoi, to_string +#include // declval, forward, move, pair, swap +#include // vector + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +// This file contains all macro definitions affecting or depending on the ABI + +#ifndef JSON_SKIP_LIBRARY_VERSION_CHECK + #if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH) + #if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 11 || NLOHMANN_JSON_VERSION_PATCH != 3 + #warning "Already included a different version of the library!" + #endif + #endif +#endif + +#define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum) +#define NLOHMANN_JSON_VERSION_MINOR 11 // NOLINT(modernize-macro-to-enum) +#define NLOHMANN_JSON_VERSION_PATCH 3 // NOLINT(modernize-macro-to-enum) + +#ifndef JSON_DIAGNOSTICS + #define JSON_DIAGNOSTICS 0 +#endif + +#ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON + #define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0 +#endif + +#if JSON_DIAGNOSTICS + #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag +#else + #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS +#endif + +#if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON + #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp +#else + #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON +#endif + +#ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION + #define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0 +#endif + +// Construct the namespace ABI tags component +#define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b) json_abi ## a ## b +#define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b) \ + NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b) + +#define NLOHMANN_JSON_ABI_TAGS \ + NLOHMANN_JSON_ABI_TAGS_CONCAT( \ + NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \ + NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON) + +// Construct the namespace version component +#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \ + _v ## major ## _ ## minor ## _ ## patch +#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \ + NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) + +#if NLOHMANN_JSON_NAMESPACE_NO_VERSION +#define NLOHMANN_JSON_NAMESPACE_VERSION +#else +#define NLOHMANN_JSON_NAMESPACE_VERSION \ + NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \ + NLOHMANN_JSON_VERSION_MINOR, \ + NLOHMANN_JSON_VERSION_PATCH) +#endif + +// Combine namespace components +#define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b +#define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \ + NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) + +#ifndef NLOHMANN_JSON_NAMESPACE +#define NLOHMANN_JSON_NAMESPACE \ + nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \ + NLOHMANN_JSON_ABI_TAGS, \ + NLOHMANN_JSON_NAMESPACE_VERSION) +#endif + +#ifndef NLOHMANN_JSON_NAMESPACE_BEGIN +#define NLOHMANN_JSON_NAMESPACE_BEGIN \ + namespace nlohmann \ + { \ + inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \ + NLOHMANN_JSON_ABI_TAGS, \ + NLOHMANN_JSON_NAMESPACE_VERSION) \ + { +#endif + +#ifndef NLOHMANN_JSON_NAMESPACE_END +#define NLOHMANN_JSON_NAMESPACE_END \ + } /* namespace (inline namespace) NOLINT(readability/namespace) */ \ + } // namespace nlohmann +#endif + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include // transform +#include // array +#include // forward_list +#include // inserter, front_inserter, end +#include // map +#include // string +#include // tuple, make_tuple +#include // is_arithmetic, is_same, is_enum, underlying_type, is_convertible +#include // unordered_map +#include // pair, declval +#include // valarray + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include // nullptr_t +#include // exception +#if JSON_DIAGNOSTICS + #include // accumulate +#endif +#include // runtime_error +#include // to_string +#include // vector + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include // array +#include // size_t +#include // uint8_t +#include // string + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include // declval, pair +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +// #include + + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +template struct make_void +{ + using type = void; +}; +template using void_t = typename make_void::type; + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +// https://en.cppreference.com/w/cpp/experimental/is_detected +struct nonesuch +{ + nonesuch() = delete; + ~nonesuch() = delete; + nonesuch(nonesuch const&) = delete; + nonesuch(nonesuch const&&) = delete; + void operator=(nonesuch const&) = delete; + void operator=(nonesuch&&) = delete; +}; + +template class Op, + class... Args> +struct detector +{ + using value_t = std::false_type; + using type = Default; +}; + +template class Op, class... Args> +struct detector>, Op, Args...> +{ + using value_t = std::true_type; + using type = Op; +}; + +template class Op, class... Args> +using is_detected = typename detector::value_t; + +template class Op, class... Args> +struct is_detected_lazy : is_detected { }; + +template class Op, class... Args> +using detected_t = typename detector::type; + +template class Op, class... Args> +using detected_or = detector; + +template class Op, class... Args> +using detected_or_t = typename detected_or::type; + +template class Op, class... Args> +using is_detected_exact = std::is_same>; + +template class Op, class... Args> +using is_detected_convertible = + std::is_convertible, To>; + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + +// #include + + +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-FileCopyrightText: 2016-2021 Evan Nemerson +// SPDX-License-Identifier: MIT + +/* Hedley - https://nemequ.github.io/hedley + * Created by Evan Nemerson + */ + +#if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 15) +#if defined(JSON_HEDLEY_VERSION) + #undef JSON_HEDLEY_VERSION +#endif +#define JSON_HEDLEY_VERSION 15 + +#if defined(JSON_HEDLEY_STRINGIFY_EX) + #undef JSON_HEDLEY_STRINGIFY_EX +#endif +#define JSON_HEDLEY_STRINGIFY_EX(x) #x + +#if defined(JSON_HEDLEY_STRINGIFY) + #undef JSON_HEDLEY_STRINGIFY +#endif +#define JSON_HEDLEY_STRINGIFY(x) JSON_HEDLEY_STRINGIFY_EX(x) + +#if defined(JSON_HEDLEY_CONCAT_EX) + #undef JSON_HEDLEY_CONCAT_EX +#endif +#define JSON_HEDLEY_CONCAT_EX(a,b) a##b + +#if defined(JSON_HEDLEY_CONCAT) + #undef JSON_HEDLEY_CONCAT +#endif +#define JSON_HEDLEY_CONCAT(a,b) JSON_HEDLEY_CONCAT_EX(a,b) + +#if defined(JSON_HEDLEY_CONCAT3_EX) + #undef JSON_HEDLEY_CONCAT3_EX +#endif +#define JSON_HEDLEY_CONCAT3_EX(a,b,c) a##b##c + +#if defined(JSON_HEDLEY_CONCAT3) + #undef JSON_HEDLEY_CONCAT3 +#endif +#define JSON_HEDLEY_CONCAT3(a,b,c) JSON_HEDLEY_CONCAT3_EX(a,b,c) + +#if defined(JSON_HEDLEY_VERSION_ENCODE) + #undef JSON_HEDLEY_VERSION_ENCODE +#endif +#define JSON_HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision)) + +#if defined(JSON_HEDLEY_VERSION_DECODE_MAJOR) + #undef JSON_HEDLEY_VERSION_DECODE_MAJOR +#endif +#define JSON_HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000) + +#if defined(JSON_HEDLEY_VERSION_DECODE_MINOR) + #undef JSON_HEDLEY_VERSION_DECODE_MINOR +#endif +#define JSON_HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000) + +#if defined(JSON_HEDLEY_VERSION_DECODE_REVISION) + #undef JSON_HEDLEY_VERSION_DECODE_REVISION +#endif +#define JSON_HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000) + +#if defined(JSON_HEDLEY_GNUC_VERSION) + #undef JSON_HEDLEY_GNUC_VERSION +#endif +#if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__) + #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) +#elif defined(__GNUC__) + #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0) +#endif + +#if defined(JSON_HEDLEY_GNUC_VERSION_CHECK) + #undef JSON_HEDLEY_GNUC_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_GNUC_VERSION) + #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GNUC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_MSVC_VERSION) + #undef JSON_HEDLEY_MSVC_VERSION +#endif +#if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) && !defined(__ICL) + #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100) +#elif defined(_MSC_FULL_VER) && !defined(__ICL) + #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10) +#elif defined(_MSC_VER) && !defined(__ICL) + #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0) +#endif + +#if defined(JSON_HEDLEY_MSVC_VERSION_CHECK) + #undef JSON_HEDLEY_MSVC_VERSION_CHECK +#endif +#if !defined(JSON_HEDLEY_MSVC_VERSION) + #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0) +#elif defined(_MSC_VER) && (_MSC_VER >= 1400) + #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch))) +#elif defined(_MSC_VER) && (_MSC_VER >= 1200) + #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch))) +#else + #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor))) +#endif + +#if defined(JSON_HEDLEY_INTEL_VERSION) + #undef JSON_HEDLEY_INTEL_VERSION +#endif +#if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && !defined(__ICL) + #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE) +#elif defined(__INTEL_COMPILER) && !defined(__ICL) + #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0) +#endif + +#if defined(JSON_HEDLEY_INTEL_VERSION_CHECK) + #undef JSON_HEDLEY_INTEL_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_INTEL_VERSION) + #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_INTEL_CL_VERSION) + #undef JSON_HEDLEY_INTEL_CL_VERSION +#endif +#if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && defined(__ICL) + #define JSON_HEDLEY_INTEL_CL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER, __INTEL_COMPILER_UPDATE, 0) +#endif + +#if defined(JSON_HEDLEY_INTEL_CL_VERSION_CHECK) + #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_INTEL_CL_VERSION) + #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_CL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_PGI_VERSION) + #undef JSON_HEDLEY_PGI_VERSION +#endif +#if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__) + #define JSON_HEDLEY_PGI_VERSION JSON_HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__) +#endif + +#if defined(JSON_HEDLEY_PGI_VERSION_CHECK) + #undef JSON_HEDLEY_PGI_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_PGI_VERSION) + #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PGI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_SUNPRO_VERSION) + #undef JSON_HEDLEY_SUNPRO_VERSION +#endif +#if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000) + #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_C >> 16) & 0xf) * 10) + ((__SUNPRO_C >> 12) & 0xf), (((__SUNPRO_C >> 8) & 0xf) * 10) + ((__SUNPRO_C >> 4) & 0xf), (__SUNPRO_C & 0xf) * 10) +#elif defined(__SUNPRO_C) + #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_C >> 8) & 0xf, (__SUNPRO_C >> 4) & 0xf, (__SUNPRO_C) & 0xf) +#elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000) + #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_CC >> 16) & 0xf) * 10) + ((__SUNPRO_CC >> 12) & 0xf), (((__SUNPRO_CC >> 8) & 0xf) * 10) + ((__SUNPRO_CC >> 4) & 0xf), (__SUNPRO_CC & 0xf) * 10) +#elif defined(__SUNPRO_CC) + #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_CC >> 8) & 0xf, (__SUNPRO_CC >> 4) & 0xf, (__SUNPRO_CC) & 0xf) +#endif + +#if defined(JSON_HEDLEY_SUNPRO_VERSION_CHECK) + #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_SUNPRO_VERSION) + #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_SUNPRO_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION) + #undef JSON_HEDLEY_EMSCRIPTEN_VERSION +#endif +#if defined(__EMSCRIPTEN__) + #define JSON_HEDLEY_EMSCRIPTEN_VERSION JSON_HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__) +#endif + +#if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK) + #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION) + #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_EMSCRIPTEN_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_ARM_VERSION) + #undef JSON_HEDLEY_ARM_VERSION +#endif +#if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION) + #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCOMPILER_VERSION / 1000000, (__ARMCOMPILER_VERSION % 1000000) / 10000, (__ARMCOMPILER_VERSION % 10000) / 100) +#elif defined(__CC_ARM) && defined(__ARMCC_VERSION) + #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCC_VERSION / 1000000, (__ARMCC_VERSION % 1000000) / 10000, (__ARMCC_VERSION % 10000) / 100) +#endif + +#if defined(JSON_HEDLEY_ARM_VERSION_CHECK) + #undef JSON_HEDLEY_ARM_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_ARM_VERSION) + #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_ARM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_IBM_VERSION) + #undef JSON_HEDLEY_IBM_VERSION +#endif +#if defined(__ibmxl__) + #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ibmxl_version__, __ibmxl_release__, __ibmxl_modification__) +#elif defined(__xlC__) && defined(__xlC_ver__) + #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff) +#elif defined(__xlC__) + #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, 0) +#endif + +#if defined(JSON_HEDLEY_IBM_VERSION_CHECK) + #undef JSON_HEDLEY_IBM_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_IBM_VERSION) + #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IBM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_TI_VERSION) + #undef JSON_HEDLEY_TI_VERSION +#endif +#if \ + defined(__TI_COMPILER_VERSION__) && \ + ( \ + defined(__TMS470__) || defined(__TI_ARM__) || \ + defined(__MSP430__) || \ + defined(__TMS320C2000__) \ + ) +#if (__TI_COMPILER_VERSION__ >= 16000000) + #define JSON_HEDLEY_TI_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) +#endif +#endif + +#if defined(JSON_HEDLEY_TI_VERSION_CHECK) + #undef JSON_HEDLEY_TI_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_TI_VERSION) + #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_TI_CL2000_VERSION) + #undef JSON_HEDLEY_TI_CL2000_VERSION +#endif +#if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C2000__) + #define JSON_HEDLEY_TI_CL2000_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) +#endif + +#if defined(JSON_HEDLEY_TI_CL2000_VERSION_CHECK) + #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_TI_CL2000_VERSION) + #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL2000_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_TI_CL430_VERSION) + #undef JSON_HEDLEY_TI_CL430_VERSION +#endif +#if defined(__TI_COMPILER_VERSION__) && defined(__MSP430__) + #define JSON_HEDLEY_TI_CL430_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) +#endif + +#if defined(JSON_HEDLEY_TI_CL430_VERSION_CHECK) + #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_TI_CL430_VERSION) + #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL430_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_TI_ARMCL_VERSION) + #undef JSON_HEDLEY_TI_ARMCL_VERSION +#endif +#if defined(__TI_COMPILER_VERSION__) && (defined(__TMS470__) || defined(__TI_ARM__)) + #define JSON_HEDLEY_TI_ARMCL_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) +#endif + +#if defined(JSON_HEDLEY_TI_ARMCL_VERSION_CHECK) + #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_TI_ARMCL_VERSION) + #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_ARMCL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_TI_CL6X_VERSION) + #undef JSON_HEDLEY_TI_CL6X_VERSION +#endif +#if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C6X__) + #define JSON_HEDLEY_TI_CL6X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) +#endif + +#if defined(JSON_HEDLEY_TI_CL6X_VERSION_CHECK) + #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_TI_CL6X_VERSION) + #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL6X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_TI_CL7X_VERSION) + #undef JSON_HEDLEY_TI_CL7X_VERSION +#endif +#if defined(__TI_COMPILER_VERSION__) && defined(__C7000__) + #define JSON_HEDLEY_TI_CL7X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) +#endif + +#if defined(JSON_HEDLEY_TI_CL7X_VERSION_CHECK) + #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_TI_CL7X_VERSION) + #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL7X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_TI_CLPRU_VERSION) + #undef JSON_HEDLEY_TI_CLPRU_VERSION +#endif +#if defined(__TI_COMPILER_VERSION__) && defined(__PRU__) + #define JSON_HEDLEY_TI_CLPRU_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) +#endif + +#if defined(JSON_HEDLEY_TI_CLPRU_VERSION_CHECK) + #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_TI_CLPRU_VERSION) + #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CLPRU_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_CRAY_VERSION) + #undef JSON_HEDLEY_CRAY_VERSION +#endif +#if defined(_CRAYC) + #if defined(_RELEASE_PATCHLEVEL) + #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, _RELEASE_PATCHLEVEL) + #else + #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, 0) + #endif +#endif + +#if defined(JSON_HEDLEY_CRAY_VERSION_CHECK) + #undef JSON_HEDLEY_CRAY_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_CRAY_VERSION) + #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_CRAY_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_IAR_VERSION) + #undef JSON_HEDLEY_IAR_VERSION +#endif +#if defined(__IAR_SYSTEMS_ICC__) + #if __VER__ > 1000 + #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000)) + #else + #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(__VER__ / 100, __VER__ % 100, 0) + #endif +#endif + +#if defined(JSON_HEDLEY_IAR_VERSION_CHECK) + #undef JSON_HEDLEY_IAR_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_IAR_VERSION) + #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IAR_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_TINYC_VERSION) + #undef JSON_HEDLEY_TINYC_VERSION +#endif +#if defined(__TINYC__) + #define JSON_HEDLEY_TINYC_VERSION JSON_HEDLEY_VERSION_ENCODE(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100) +#endif + +#if defined(JSON_HEDLEY_TINYC_VERSION_CHECK) + #undef JSON_HEDLEY_TINYC_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_TINYC_VERSION) + #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TINYC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_DMC_VERSION) + #undef JSON_HEDLEY_DMC_VERSION +#endif +#if defined(__DMC__) + #define JSON_HEDLEY_DMC_VERSION JSON_HEDLEY_VERSION_ENCODE(__DMC__ >> 8, (__DMC__ >> 4) & 0xf, __DMC__ & 0xf) +#endif + +#if defined(JSON_HEDLEY_DMC_VERSION_CHECK) + #undef JSON_HEDLEY_DMC_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_DMC_VERSION) + #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_DMC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_COMPCERT_VERSION) + #undef JSON_HEDLEY_COMPCERT_VERSION +#endif +#if defined(__COMPCERT_VERSION__) + #define JSON_HEDLEY_COMPCERT_VERSION JSON_HEDLEY_VERSION_ENCODE(__COMPCERT_VERSION__ / 10000, (__COMPCERT_VERSION__ / 100) % 100, __COMPCERT_VERSION__ % 100) +#endif + +#if defined(JSON_HEDLEY_COMPCERT_VERSION_CHECK) + #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_COMPCERT_VERSION) + #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_COMPCERT_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_PELLES_VERSION) + #undef JSON_HEDLEY_PELLES_VERSION +#endif +#if defined(__POCC__) + #define JSON_HEDLEY_PELLES_VERSION JSON_HEDLEY_VERSION_ENCODE(__POCC__ / 100, __POCC__ % 100, 0) +#endif + +#if defined(JSON_HEDLEY_PELLES_VERSION_CHECK) + #undef JSON_HEDLEY_PELLES_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_PELLES_VERSION) + #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PELLES_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_MCST_LCC_VERSION) + #undef JSON_HEDLEY_MCST_LCC_VERSION +#endif +#if defined(__LCC__) && defined(__LCC_MINOR__) + #define JSON_HEDLEY_MCST_LCC_VERSION JSON_HEDLEY_VERSION_ENCODE(__LCC__ / 100, __LCC__ % 100, __LCC_MINOR__) +#endif + +#if defined(JSON_HEDLEY_MCST_LCC_VERSION_CHECK) + #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_MCST_LCC_VERSION) + #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_MCST_LCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_GCC_VERSION) + #undef JSON_HEDLEY_GCC_VERSION +#endif +#if \ + defined(JSON_HEDLEY_GNUC_VERSION) && \ + !defined(__clang__) && \ + !defined(JSON_HEDLEY_INTEL_VERSION) && \ + !defined(JSON_HEDLEY_PGI_VERSION) && \ + !defined(JSON_HEDLEY_ARM_VERSION) && \ + !defined(JSON_HEDLEY_CRAY_VERSION) && \ + !defined(JSON_HEDLEY_TI_VERSION) && \ + !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \ + !defined(JSON_HEDLEY_TI_CL430_VERSION) && \ + !defined(JSON_HEDLEY_TI_CL2000_VERSION) && \ + !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \ + !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \ + !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \ + !defined(__COMPCERT__) && \ + !defined(JSON_HEDLEY_MCST_LCC_VERSION) + #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION +#endif + +#if defined(JSON_HEDLEY_GCC_VERSION_CHECK) + #undef JSON_HEDLEY_GCC_VERSION_CHECK +#endif +#if defined(JSON_HEDLEY_GCC_VERSION) + #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) +#else + #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0) +#endif + +#if defined(JSON_HEDLEY_HAS_ATTRIBUTE) + #undef JSON_HEDLEY_HAS_ATTRIBUTE +#endif +#if \ + defined(__has_attribute) && \ + ( \ + (!defined(JSON_HEDLEY_IAR_VERSION) || JSON_HEDLEY_IAR_VERSION_CHECK(8,5,9)) \ + ) +# define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute) +#else +# define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0) +#endif + +#if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE) + #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE +#endif +#if defined(__has_attribute) + #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute) +#else + #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_GCC_HAS_ATTRIBUTE) + #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE +#endif +#if defined(__has_attribute) + #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute) +#else + #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE) + #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE +#endif +#if \ + defined(__has_cpp_attribute) && \ + defined(__cplusplus) && \ + (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) + #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute) +#else + #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) (0) +#endif + +#if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS) + #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS +#endif +#if !defined(__cplusplus) || !defined(__has_cpp_attribute) + #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0) +#elif \ + !defined(JSON_HEDLEY_PGI_VERSION) && \ + !defined(JSON_HEDLEY_IAR_VERSION) && \ + (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) && \ + (!defined(JSON_HEDLEY_MSVC_VERSION) || JSON_HEDLEY_MSVC_VERSION_CHECK(19,20,0)) + #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(ns::attribute) +#else + #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0) +#endif + +#if defined(JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE) + #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE +#endif +#if defined(__has_cpp_attribute) && defined(__cplusplus) + #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute) +#else + #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE) + #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE +#endif +#if defined(__has_cpp_attribute) && defined(__cplusplus) + #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute) +#else + #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_HAS_BUILTIN) + #undef JSON_HEDLEY_HAS_BUILTIN +#endif +#if defined(__has_builtin) + #define JSON_HEDLEY_HAS_BUILTIN(builtin) __has_builtin(builtin) +#else + #define JSON_HEDLEY_HAS_BUILTIN(builtin) (0) +#endif + +#if defined(JSON_HEDLEY_GNUC_HAS_BUILTIN) + #undef JSON_HEDLEY_GNUC_HAS_BUILTIN +#endif +#if defined(__has_builtin) + #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin) +#else + #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_GCC_HAS_BUILTIN) + #undef JSON_HEDLEY_GCC_HAS_BUILTIN +#endif +#if defined(__has_builtin) + #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin) +#else + #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_HAS_FEATURE) + #undef JSON_HEDLEY_HAS_FEATURE +#endif +#if defined(__has_feature) + #define JSON_HEDLEY_HAS_FEATURE(feature) __has_feature(feature) +#else + #define JSON_HEDLEY_HAS_FEATURE(feature) (0) +#endif + +#if defined(JSON_HEDLEY_GNUC_HAS_FEATURE) + #undef JSON_HEDLEY_GNUC_HAS_FEATURE +#endif +#if defined(__has_feature) + #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature) +#else + #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_GCC_HAS_FEATURE) + #undef JSON_HEDLEY_GCC_HAS_FEATURE +#endif +#if defined(__has_feature) + #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature) +#else + #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_HAS_EXTENSION) + #undef JSON_HEDLEY_HAS_EXTENSION +#endif +#if defined(__has_extension) + #define JSON_HEDLEY_HAS_EXTENSION(extension) __has_extension(extension) +#else + #define JSON_HEDLEY_HAS_EXTENSION(extension) (0) +#endif + +#if defined(JSON_HEDLEY_GNUC_HAS_EXTENSION) + #undef JSON_HEDLEY_GNUC_HAS_EXTENSION +#endif +#if defined(__has_extension) + #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension) +#else + #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_GCC_HAS_EXTENSION) + #undef JSON_HEDLEY_GCC_HAS_EXTENSION +#endif +#if defined(__has_extension) + #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension) +#else + #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE) + #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE +#endif +#if defined(__has_declspec_attribute) + #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute) +#else + #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) (0) +#endif + +#if defined(JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE) + #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE +#endif +#if defined(__has_declspec_attribute) + #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute) +#else + #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE) + #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE +#endif +#if defined(__has_declspec_attribute) + #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute) +#else + #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_HAS_WARNING) + #undef JSON_HEDLEY_HAS_WARNING +#endif +#if defined(__has_warning) + #define JSON_HEDLEY_HAS_WARNING(warning) __has_warning(warning) +#else + #define JSON_HEDLEY_HAS_WARNING(warning) (0) +#endif + +#if defined(JSON_HEDLEY_GNUC_HAS_WARNING) + #undef JSON_HEDLEY_GNUC_HAS_WARNING +#endif +#if defined(__has_warning) + #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning) +#else + #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_GCC_HAS_WARNING) + #undef JSON_HEDLEY_GCC_HAS_WARNING +#endif +#if defined(__has_warning) + #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning) +#else + #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) +#endif + +#if \ + (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \ + defined(__clang__) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \ + JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \ + (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR)) + #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value) +#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) + #define JSON_HEDLEY_PRAGMA(value) __pragma(value) +#else + #define JSON_HEDLEY_PRAGMA(value) +#endif + +#if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH) + #undef JSON_HEDLEY_DIAGNOSTIC_PUSH +#endif +#if defined(JSON_HEDLEY_DIAGNOSTIC_POP) + #undef JSON_HEDLEY_DIAGNOSTIC_POP +#endif +#if defined(__clang__) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop") +#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)") +#elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop") +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push)) + #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop)) +#elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop") +#elif \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop") +#elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0) + #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)") + #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)") +#else + #define JSON_HEDLEY_DIAGNOSTIC_PUSH + #define JSON_HEDLEY_DIAGNOSTIC_POP +#endif + +/* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for + HEDLEY INTERNAL USE ONLY. API subject to change without notice. */ +#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_) + #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ +#endif +#if defined(__cplusplus) +# if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat") +# if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions") +# if JSON_HEDLEY_HAS_WARNING("-Wc++1z-extensions") +# define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \ + _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \ + _Pragma("clang diagnostic ignored \"-Wc++1z-extensions\"") \ + xpr \ + JSON_HEDLEY_DIAGNOSTIC_POP +# else +# define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \ + _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \ + xpr \ + JSON_HEDLEY_DIAGNOSTIC_POP +# endif +# else +# define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \ + xpr \ + JSON_HEDLEY_DIAGNOSTIC_POP +# endif +# endif +#endif +#if !defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(x) x +#endif + +#if defined(JSON_HEDLEY_CONST_CAST) + #undef JSON_HEDLEY_CONST_CAST +#endif +#if defined(__cplusplus) +# define JSON_HEDLEY_CONST_CAST(T, expr) (const_cast(expr)) +#elif \ + JSON_HEDLEY_HAS_WARNING("-Wcast-qual") || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) +# define JSON_HEDLEY_CONST_CAST(T, expr) (__extension__ ({ \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL \ + ((T) (expr)); \ + JSON_HEDLEY_DIAGNOSTIC_POP \ + })) +#else +# define JSON_HEDLEY_CONST_CAST(T, expr) ((T) (expr)) +#endif + +#if defined(JSON_HEDLEY_REINTERPRET_CAST) + #undef JSON_HEDLEY_REINTERPRET_CAST +#endif +#if defined(__cplusplus) + #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (reinterpret_cast(expr)) +#else + #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) ((T) (expr)) +#endif + +#if defined(JSON_HEDLEY_STATIC_CAST) + #undef JSON_HEDLEY_STATIC_CAST +#endif +#if defined(__cplusplus) + #define JSON_HEDLEY_STATIC_CAST(T, expr) (static_cast(expr)) +#else + #define JSON_HEDLEY_STATIC_CAST(T, expr) ((T) (expr)) +#endif + +#if defined(JSON_HEDLEY_CPP_CAST) + #undef JSON_HEDLEY_CPP_CAST +#endif +#if defined(__cplusplus) +# if JSON_HEDLEY_HAS_WARNING("-Wold-style-cast") +# define JSON_HEDLEY_CPP_CAST(T, expr) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \ + ((T) (expr)) \ + JSON_HEDLEY_DIAGNOSTIC_POP +# elif JSON_HEDLEY_IAR_VERSION_CHECK(8,3,0) +# define JSON_HEDLEY_CPP_CAST(T, expr) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("diag_suppress=Pe137") \ + JSON_HEDLEY_DIAGNOSTIC_POP +# else +# define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr)) +# endif +#else +# define JSON_HEDLEY_CPP_CAST(T, expr) (expr) +#endif + +#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED) + #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wdeprecated-declarations") + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") +#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)") +#elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:1478 1786)) +#elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1216,1444,1445") +#elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444") +#elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") +#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996)) +#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444") +#elif \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1291,1718") +#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && !defined(__cplusplus) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS)") +#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && defined(__cplusplus) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,symdeprecated,symdeprecated2)") +#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress=Pe1444,Pe1215") +#elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warn(disable:2241)") +#else + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED +#endif + +#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS) + #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas") + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"") +#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)") +#elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:161)) +#elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675") +#elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"") +#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068)) +#elif \ + JSON_HEDLEY_TI_VERSION_CHECK(16,9,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163") +#elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163") +#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161") +#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 161") +#else + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS +#endif + +#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES) + #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wunknown-attributes") + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("clang diagnostic ignored \"-Wunknown-attributes\"") +#elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") +#elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)") +#elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:1292)) +#elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030)) +#elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097,1098") +#elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097") +#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("error_messages(off,attrskipunsup)") +#elif \ + JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173") +#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097") +#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097") +#else + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES +#endif + +#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL) + #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wcast-qual") + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("clang diagnostic ignored \"-Wcast-qual\"") +#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("warning(disable:2203 2331)") +#elif JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("GCC diagnostic ignored \"-Wcast-qual\"") +#else + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL +#endif + +#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION) + #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wunused-function") + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("clang diagnostic ignored \"-Wunused-function\"") +#elif JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("GCC diagnostic ignored \"-Wunused-function\"") +#elif JSON_HEDLEY_MSVC_VERSION_CHECK(1,0,0) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION __pragma(warning(disable:4505)) +#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("diag_suppress 3142") +#else + #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION +#endif + +#if defined(JSON_HEDLEY_DEPRECATED) + #undef JSON_HEDLEY_DEPRECATED +#endif +#if defined(JSON_HEDLEY_DEPRECATED_FOR) + #undef JSON_HEDLEY_DEPRECATED_FOR +#endif +#if \ + JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since)) + #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement)) +#elif \ + (JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) && !defined(JSON_HEDLEY_IAR_VERSION)) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since))) + #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement))) +#elif defined(__cplusplus) && (__cplusplus >= 201402L) + #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]]) + #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]]) +#elif \ + JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) + #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__)) + #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__)) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ + JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated) + #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated) +#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) + #define JSON_HEDLEY_DEPRECATED(since) _Pragma("deprecated") + #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) _Pragma("deprecated") +#else + #define JSON_HEDLEY_DEPRECATED(since) + #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) +#endif + +#if defined(JSON_HEDLEY_UNAVAILABLE) + #undef JSON_HEDLEY_UNAVAILABLE +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since))) +#else + #define JSON_HEDLEY_UNAVAILABLE(available_since) +#endif + +#if defined(JSON_HEDLEY_WARN_UNUSED_RESULT) + #undef JSON_HEDLEY_WARN_UNUSED_RESULT +#endif +#if defined(JSON_HEDLEY_WARN_UNUSED_RESULT_MSG) + #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__)) + #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__)) +#elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L) + #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) + #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]]) +#elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) + #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) + #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) +#elif defined(_Check_return_) /* SAL */ + #define JSON_HEDLEY_WARN_UNUSED_RESULT _Check_return_ + #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) _Check_return_ +#else + #define JSON_HEDLEY_WARN_UNUSED_RESULT + #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) +#endif + +#if defined(JSON_HEDLEY_SENTINEL) + #undef JSON_HEDLEY_SENTINEL +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position))) +#else + #define JSON_HEDLEY_SENTINEL(position) +#endif + +#if defined(JSON_HEDLEY_NO_RETURN) + #undef JSON_HEDLEY_NO_RETURN +#endif +#if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) + #define JSON_HEDLEY_NO_RETURN __noreturn +#elif \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__)) +#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L + #define JSON_HEDLEY_NO_RETURN _Noreturn +#elif defined(__cplusplus) && (__cplusplus >= 201103L) + #define JSON_HEDLEY_NO_RETURN JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[noreturn]]) +#elif \ + JSON_HEDLEY_HAS_ATTRIBUTE(noreturn) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,2,0) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) + #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__)) +#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) + #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return") +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_NO_RETURN __declspec(noreturn) +#elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus) + #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;") +#elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0) + #define JSON_HEDLEY_NO_RETURN __attribute((noreturn)) +#elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0) + #define JSON_HEDLEY_NO_RETURN __declspec(noreturn) +#else + #define JSON_HEDLEY_NO_RETURN +#endif + +#if defined(JSON_HEDLEY_NO_ESCAPE) + #undef JSON_HEDLEY_NO_ESCAPE +#endif +#if JSON_HEDLEY_HAS_ATTRIBUTE(noescape) + #define JSON_HEDLEY_NO_ESCAPE __attribute__((__noescape__)) +#else + #define JSON_HEDLEY_NO_ESCAPE +#endif + +#if defined(JSON_HEDLEY_UNREACHABLE) + #undef JSON_HEDLEY_UNREACHABLE +#endif +#if defined(JSON_HEDLEY_UNREACHABLE_RETURN) + #undef JSON_HEDLEY_UNREACHABLE_RETURN +#endif +#if defined(JSON_HEDLEY_ASSUME) + #undef JSON_HEDLEY_ASSUME +#endif +#if \ + JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_ASSUME(expr) __assume(expr) +#elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume) + #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr) +#elif \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) + #if defined(__cplusplus) + #define JSON_HEDLEY_ASSUME(expr) std::_nassert(expr) + #else + #define JSON_HEDLEY_ASSUME(expr) _nassert(expr) + #endif +#endif +#if \ + (JSON_HEDLEY_HAS_BUILTIN(__builtin_unreachable) && (!defined(JSON_HEDLEY_ARM_VERSION))) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) || \ + JSON_HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable() +#elif defined(JSON_HEDLEY_ASSUME) + #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0) +#endif +#if !defined(JSON_HEDLEY_ASSUME) + #if defined(JSON_HEDLEY_UNREACHABLE) + #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, ((expr) ? 1 : (JSON_HEDLEY_UNREACHABLE(), 1))) + #else + #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, expr) + #endif +#endif +#if defined(JSON_HEDLEY_UNREACHABLE) + #if \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) + #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (JSON_HEDLEY_STATIC_CAST(void, JSON_HEDLEY_ASSUME(0)), (value)) + #else + #define JSON_HEDLEY_UNREACHABLE_RETURN(value) JSON_HEDLEY_UNREACHABLE() + #endif +#else + #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (value) +#endif +#if !defined(JSON_HEDLEY_UNREACHABLE) + #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0) +#endif + +JSON_HEDLEY_DIAGNOSTIC_PUSH +#if JSON_HEDLEY_HAS_WARNING("-Wpedantic") + #pragma clang diagnostic ignored "-Wpedantic" +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat-pedantic") && defined(__cplusplus) + #pragma clang diagnostic ignored "-Wc++98-compat-pedantic" +#endif +#if JSON_HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0) + #if defined(__clang__) + #pragma clang diagnostic ignored "-Wvariadic-macros" + #elif defined(JSON_HEDLEY_GCC_VERSION) + #pragma GCC diagnostic ignored "-Wvariadic-macros" + #endif +#endif +#if defined(JSON_HEDLEY_NON_NULL) + #undef JSON_HEDLEY_NON_NULL +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(nonnull) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) + #define JSON_HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__))) +#else + #define JSON_HEDLEY_NON_NULL(...) +#endif +JSON_HEDLEY_DIAGNOSTIC_POP + +#if defined(JSON_HEDLEY_PRINTF_FORMAT) + #undef JSON_HEDLEY_PRINTF_FORMAT +#endif +#if defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO) + #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check))) +#elif defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && defined(__USE_MINGW_ANSI_STDIO) + #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(gnu_printf, string_idx, first_to_check))) +#elif \ + JSON_HEDLEY_HAS_ATTRIBUTE(format) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check))) +#elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0) + #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check)) +#else + #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) +#endif + +#if defined(JSON_HEDLEY_CONSTEXPR) + #undef JSON_HEDLEY_CONSTEXPR +#endif +#if defined(__cplusplus) + #if __cplusplus >= 201103L + #define JSON_HEDLEY_CONSTEXPR JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(constexpr) + #endif +#endif +#if !defined(JSON_HEDLEY_CONSTEXPR) + #define JSON_HEDLEY_CONSTEXPR +#endif + +#if defined(JSON_HEDLEY_PREDICT) + #undef JSON_HEDLEY_PREDICT +#endif +#if defined(JSON_HEDLEY_LIKELY) + #undef JSON_HEDLEY_LIKELY +#endif +#if defined(JSON_HEDLEY_UNLIKELY) + #undef JSON_HEDLEY_UNLIKELY +#endif +#if defined(JSON_HEDLEY_UNPREDICTABLE) + #undef JSON_HEDLEY_UNPREDICTABLE +#endif +#if JSON_HEDLEY_HAS_BUILTIN(__builtin_unpredictable) + #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr)) +#endif +#if \ + (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) +# define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability)) +# define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability)) +# define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability)) +# define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 ) +# define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 ) +#elif \ + (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \ + JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) +# define JSON_HEDLEY_PREDICT(expr, expected, probability) \ + (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))) +# define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \ + (__extension__ ({ \ + double hedley_probability_ = (probability); \ + ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 1) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 0) : !!(expr))); \ + })) +# define JSON_HEDLEY_PREDICT_FALSE(expr, probability) \ + (__extension__ ({ \ + double hedley_probability_ = (probability); \ + ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 0) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 1) : !!(expr))); \ + })) +# define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1) +# define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0) +#else +# define JSON_HEDLEY_PREDICT(expr, expected, probability) (JSON_HEDLEY_STATIC_CAST(void, expected), (expr)) +# define JSON_HEDLEY_PREDICT_TRUE(expr, probability) (!!(expr)) +# define JSON_HEDLEY_PREDICT_FALSE(expr, probability) (!!(expr)) +# define JSON_HEDLEY_LIKELY(expr) (!!(expr)) +# define JSON_HEDLEY_UNLIKELY(expr) (!!(expr)) +#endif +#if !defined(JSON_HEDLEY_UNPREDICTABLE) + #define JSON_HEDLEY_UNPREDICTABLE(expr) JSON_HEDLEY_PREDICT(expr, 1, 0.5) +#endif + +#if defined(JSON_HEDLEY_MALLOC) + #undef JSON_HEDLEY_MALLOC +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(malloc) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_MALLOC __attribute__((__malloc__)) +#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) + #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory") +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_MALLOC __declspec(restrict) +#else + #define JSON_HEDLEY_MALLOC +#endif + +#if defined(JSON_HEDLEY_PURE) + #undef JSON_HEDLEY_PURE +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(pure) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(2,96,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) +# define JSON_HEDLEY_PURE __attribute__((__pure__)) +#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) +# define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data") +#elif defined(__cplusplus) && \ + ( \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) \ + ) +# define JSON_HEDLEY_PURE _Pragma("FUNC_IS_PURE;") +#else +# define JSON_HEDLEY_PURE +#endif + +#if defined(JSON_HEDLEY_CONST) + #undef JSON_HEDLEY_CONST +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(const) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(2,5,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_CONST __attribute__((__const__)) +#elif \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) + #define JSON_HEDLEY_CONST _Pragma("no_side_effect") +#else + #define JSON_HEDLEY_CONST JSON_HEDLEY_PURE +#endif + +#if defined(JSON_HEDLEY_RESTRICT) + #undef JSON_HEDLEY_RESTRICT +#endif +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus) + #define JSON_HEDLEY_RESTRICT restrict +#elif \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \ + JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,4) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \ + defined(__clang__) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_RESTRICT __restrict +#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus) + #define JSON_HEDLEY_RESTRICT _Restrict +#else + #define JSON_HEDLEY_RESTRICT +#endif + +#if defined(JSON_HEDLEY_INLINE) + #undef JSON_HEDLEY_INLINE +#endif +#if \ + (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \ + (defined(__cplusplus) && (__cplusplus >= 199711L)) + #define JSON_HEDLEY_INLINE inline +#elif \ + defined(JSON_HEDLEY_GCC_VERSION) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(6,2,0) + #define JSON_HEDLEY_INLINE __inline__ +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_INLINE __inline +#else + #define JSON_HEDLEY_INLINE +#endif + +#if defined(JSON_HEDLEY_ALWAYS_INLINE) + #undef JSON_HEDLEY_ALWAYS_INLINE +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(always_inline) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) +# define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) +# define JSON_HEDLEY_ALWAYS_INLINE __forceinline +#elif defined(__cplusplus) && \ + ( \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) \ + ) +# define JSON_HEDLEY_ALWAYS_INLINE _Pragma("FUNC_ALWAYS_INLINE;") +#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) +# define JSON_HEDLEY_ALWAYS_INLINE _Pragma("inline=forced") +#else +# define JSON_HEDLEY_ALWAYS_INLINE JSON_HEDLEY_INLINE +#endif + +#if defined(JSON_HEDLEY_NEVER_INLINE) + #undef JSON_HEDLEY_NEVER_INLINE +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(noinline) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ + JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ + (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ + (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ + (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ + JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ + JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \ + JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) + #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__)) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline) +#elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0) + #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline") +#elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus) + #define JSON_HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;") +#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) + #define JSON_HEDLEY_NEVER_INLINE _Pragma("inline=never") +#elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0) + #define JSON_HEDLEY_NEVER_INLINE __attribute((noinline)) +#elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0) + #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline) +#else + #define JSON_HEDLEY_NEVER_INLINE +#endif + +#if defined(JSON_HEDLEY_PRIVATE) + #undef JSON_HEDLEY_PRIVATE +#endif +#if defined(JSON_HEDLEY_PUBLIC) + #undef JSON_HEDLEY_PUBLIC +#endif +#if defined(JSON_HEDLEY_IMPORT) + #undef JSON_HEDLEY_IMPORT +#endif +#if defined(_WIN32) || defined(__CYGWIN__) +# define JSON_HEDLEY_PRIVATE +# define JSON_HEDLEY_PUBLIC __declspec(dllexport) +# define JSON_HEDLEY_IMPORT __declspec(dllimport) +#else +# if \ + JSON_HEDLEY_HAS_ATTRIBUTE(visibility) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \ + JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \ + ( \ + defined(__TI_EABI__) && \ + ( \ + (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \ + ) \ + ) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) +# define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden"))) +# define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default"))) +# else +# define JSON_HEDLEY_PRIVATE +# define JSON_HEDLEY_PUBLIC +# endif +# define JSON_HEDLEY_IMPORT extern +#endif + +#if defined(JSON_HEDLEY_NO_THROW) + #undef JSON_HEDLEY_NO_THROW +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__)) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) + #define JSON_HEDLEY_NO_THROW __declspec(nothrow) +#else + #define JSON_HEDLEY_NO_THROW +#endif + +#if defined(JSON_HEDLEY_FALL_THROUGH) + #undef JSON_HEDLEY_FALL_THROUGH +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__)) +#elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough) + #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]]) +#elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(fallthrough) + #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[fallthrough]]) +#elif defined(__fallthrough) /* SAL */ + #define JSON_HEDLEY_FALL_THROUGH __fallthrough +#else + #define JSON_HEDLEY_FALL_THROUGH +#endif + +#if defined(JSON_HEDLEY_RETURNS_NON_NULL) + #undef JSON_HEDLEY_RETURNS_NON_NULL +#endif +#if \ + JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__)) +#elif defined(_Ret_notnull_) /* SAL */ + #define JSON_HEDLEY_RETURNS_NON_NULL _Ret_notnull_ +#else + #define JSON_HEDLEY_RETURNS_NON_NULL +#endif + +#if defined(JSON_HEDLEY_ARRAY_PARAM) + #undef JSON_HEDLEY_ARRAY_PARAM +#endif +#if \ + defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \ + !defined(__STDC_NO_VLA__) && \ + !defined(__cplusplus) && \ + !defined(JSON_HEDLEY_PGI_VERSION) && \ + !defined(JSON_HEDLEY_TINYC_VERSION) + #define JSON_HEDLEY_ARRAY_PARAM(name) (name) +#else + #define JSON_HEDLEY_ARRAY_PARAM(name) +#endif + +#if defined(JSON_HEDLEY_IS_CONSTANT) + #undef JSON_HEDLEY_IS_CONSTANT +#endif +#if defined(JSON_HEDLEY_REQUIRE_CONSTEXPR) + #undef JSON_HEDLEY_REQUIRE_CONSTEXPR +#endif +/* JSON_HEDLEY_IS_CONSTEXPR_ is for + HEDLEY INTERNAL USE ONLY. API subject to change without notice. */ +#if defined(JSON_HEDLEY_IS_CONSTEXPR_) + #undef JSON_HEDLEY_IS_CONSTEXPR_ +#endif +#if \ + JSON_HEDLEY_HAS_BUILTIN(__builtin_constant_p) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,19) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \ + JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \ + (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \ + JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \ + JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) + #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr) +#endif +#if !defined(__cplusplus) +# if \ + JSON_HEDLEY_HAS_BUILTIN(__builtin_types_compatible_p) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \ + JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \ + JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,24) +#if defined(__INTPTR_TYPE__) + #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0)), int*) +#else + #include + #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((intptr_t) ((expr) * 0)) : (int*) 0)), int*) +#endif +# elif \ + ( \ + defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \ + !defined(JSON_HEDLEY_SUNPRO_VERSION) && \ + !defined(JSON_HEDLEY_PGI_VERSION) && \ + !defined(JSON_HEDLEY_IAR_VERSION)) || \ + (JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) && !defined(JSON_HEDLEY_IAR_VERSION)) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \ + JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \ + JSON_HEDLEY_ARM_VERSION_CHECK(5,3,0) +#if defined(__INTPTR_TYPE__) + #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0), int*: 1, void*: 0) +#else + #include + #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((intptr_t) * 0) : (int*) 0), int*: 1, void*: 0) +#endif +# elif \ + defined(JSON_HEDLEY_GCC_VERSION) || \ + defined(JSON_HEDLEY_INTEL_VERSION) || \ + defined(JSON_HEDLEY_TINYC_VERSION) || \ + defined(JSON_HEDLEY_TI_ARMCL_VERSION) || \ + JSON_HEDLEY_TI_CL430_VERSION_CHECK(18,12,0) || \ + defined(JSON_HEDLEY_TI_CL2000_VERSION) || \ + defined(JSON_HEDLEY_TI_CL6X_VERSION) || \ + defined(JSON_HEDLEY_TI_CL7X_VERSION) || \ + defined(JSON_HEDLEY_TI_CLPRU_VERSION) || \ + defined(__clang__) +# define JSON_HEDLEY_IS_CONSTEXPR_(expr) ( \ + sizeof(void) != \ + sizeof(*( \ + 1 ? \ + ((void*) ((expr) * 0L) ) : \ +((struct { char v[sizeof(void) * 2]; } *) 1) \ + ) \ + ) \ + ) +# endif +#endif +#if defined(JSON_HEDLEY_IS_CONSTEXPR_) + #if !defined(JSON_HEDLEY_IS_CONSTANT) + #define JSON_HEDLEY_IS_CONSTANT(expr) JSON_HEDLEY_IS_CONSTEXPR_(expr) + #endif + #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (JSON_HEDLEY_IS_CONSTEXPR_(expr) ? (expr) : (-1)) +#else + #if !defined(JSON_HEDLEY_IS_CONSTANT) + #define JSON_HEDLEY_IS_CONSTANT(expr) (0) + #endif + #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (expr) +#endif + +#if defined(JSON_HEDLEY_BEGIN_C_DECLS) + #undef JSON_HEDLEY_BEGIN_C_DECLS +#endif +#if defined(JSON_HEDLEY_END_C_DECLS) + #undef JSON_HEDLEY_END_C_DECLS +#endif +#if defined(JSON_HEDLEY_C_DECL) + #undef JSON_HEDLEY_C_DECL +#endif +#if defined(__cplusplus) + #define JSON_HEDLEY_BEGIN_C_DECLS extern "C" { + #define JSON_HEDLEY_END_C_DECLS } + #define JSON_HEDLEY_C_DECL extern "C" +#else + #define JSON_HEDLEY_BEGIN_C_DECLS + #define JSON_HEDLEY_END_C_DECLS + #define JSON_HEDLEY_C_DECL +#endif + +#if defined(JSON_HEDLEY_STATIC_ASSERT) + #undef JSON_HEDLEY_STATIC_ASSERT +#endif +#if \ + !defined(__cplusplus) && ( \ + (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \ + (JSON_HEDLEY_HAS_FEATURE(c_static_assert) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \ + JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ + defined(_Static_assert) \ + ) +# define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message) +#elif \ + (defined(__cplusplus) && (__cplusplus >= 201103L)) || \ + JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) +# define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message)) +#else +# define JSON_HEDLEY_STATIC_ASSERT(expr, message) +#endif + +#if defined(JSON_HEDLEY_NULL) + #undef JSON_HEDLEY_NULL +#endif +#if defined(__cplusplus) + #if __cplusplus >= 201103L + #define JSON_HEDLEY_NULL JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(nullptr) + #elif defined(NULL) + #define JSON_HEDLEY_NULL NULL + #else + #define JSON_HEDLEY_NULL JSON_HEDLEY_STATIC_CAST(void*, 0) + #endif +#elif defined(NULL) + #define JSON_HEDLEY_NULL NULL +#else + #define JSON_HEDLEY_NULL ((void*) 0) +#endif + +#if defined(JSON_HEDLEY_MESSAGE) + #undef JSON_HEDLEY_MESSAGE +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas") +# define JSON_HEDLEY_MESSAGE(msg) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \ + JSON_HEDLEY_PRAGMA(message msg) \ + JSON_HEDLEY_DIAGNOSTIC_POP +#elif \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,4,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) +# define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message msg) +#elif JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) +# define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(_CRI message msg) +#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) +# define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg)) +#elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,0,0) +# define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg)) +#else +# define JSON_HEDLEY_MESSAGE(msg) +#endif + +#if defined(JSON_HEDLEY_WARNING) + #undef JSON_HEDLEY_WARNING +#endif +#if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas") +# define JSON_HEDLEY_WARNING(msg) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \ + JSON_HEDLEY_PRAGMA(clang warning msg) \ + JSON_HEDLEY_DIAGNOSTIC_POP +#elif \ + JSON_HEDLEY_GCC_VERSION_CHECK(4,8,0) || \ + JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \ + JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) +# define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg) +#elif \ + JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) +# define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg)) +#else +# define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg) +#endif + +#if defined(JSON_HEDLEY_REQUIRE) + #undef JSON_HEDLEY_REQUIRE +#endif +#if defined(JSON_HEDLEY_REQUIRE_MSG) + #undef JSON_HEDLEY_REQUIRE_MSG +#endif +#if JSON_HEDLEY_HAS_ATTRIBUTE(diagnose_if) +# if JSON_HEDLEY_HAS_WARNING("-Wgcc-compat") +# define JSON_HEDLEY_REQUIRE(expr) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \ + __attribute__((diagnose_if(!(expr), #expr, "error"))) \ + JSON_HEDLEY_DIAGNOSTIC_POP +# define JSON_HEDLEY_REQUIRE_MSG(expr,msg) \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \ + __attribute__((diagnose_if(!(expr), msg, "error"))) \ + JSON_HEDLEY_DIAGNOSTIC_POP +# else +# define JSON_HEDLEY_REQUIRE(expr) __attribute__((diagnose_if(!(expr), #expr, "error"))) +# define JSON_HEDLEY_REQUIRE_MSG(expr,msg) __attribute__((diagnose_if(!(expr), msg, "error"))) +# endif +#else +# define JSON_HEDLEY_REQUIRE(expr) +# define JSON_HEDLEY_REQUIRE_MSG(expr,msg) +#endif + +#if defined(JSON_HEDLEY_FLAGS) + #undef JSON_HEDLEY_FLAGS +#endif +#if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) && (!defined(__cplusplus) || JSON_HEDLEY_HAS_WARNING("-Wbitfield-enum-conversion")) + #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__)) +#else + #define JSON_HEDLEY_FLAGS +#endif + +#if defined(JSON_HEDLEY_FLAGS_CAST) + #undef JSON_HEDLEY_FLAGS_CAST +#endif +#if JSON_HEDLEY_INTEL_VERSION_CHECK(19,0,0) +# define JSON_HEDLEY_FLAGS_CAST(T, expr) (__extension__ ({ \ + JSON_HEDLEY_DIAGNOSTIC_PUSH \ + _Pragma("warning(disable:188)") \ + ((T) (expr)); \ + JSON_HEDLEY_DIAGNOSTIC_POP \ + })) +#else +# define JSON_HEDLEY_FLAGS_CAST(T, expr) JSON_HEDLEY_STATIC_CAST(T, expr) +#endif + +#if defined(JSON_HEDLEY_EMPTY_BASES) + #undef JSON_HEDLEY_EMPTY_BASES +#endif +#if \ + (JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)) || \ + JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) + #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases) +#else + #define JSON_HEDLEY_EMPTY_BASES +#endif + +/* Remaining macros are deprecated. */ + +#if defined(JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK) + #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK +#endif +#if defined(__clang__) + #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0) +#else + #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) +#endif + +#if defined(JSON_HEDLEY_CLANG_HAS_ATTRIBUTE) + #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE +#endif +#define JSON_HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_ATTRIBUTE(attribute) + +#if defined(JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE) + #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE +#endif +#define JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) + +#if defined(JSON_HEDLEY_CLANG_HAS_BUILTIN) + #undef JSON_HEDLEY_CLANG_HAS_BUILTIN +#endif +#define JSON_HEDLEY_CLANG_HAS_BUILTIN(builtin) JSON_HEDLEY_HAS_BUILTIN(builtin) + +#if defined(JSON_HEDLEY_CLANG_HAS_FEATURE) + #undef JSON_HEDLEY_CLANG_HAS_FEATURE +#endif +#define JSON_HEDLEY_CLANG_HAS_FEATURE(feature) JSON_HEDLEY_HAS_FEATURE(feature) + +#if defined(JSON_HEDLEY_CLANG_HAS_EXTENSION) + #undef JSON_HEDLEY_CLANG_HAS_EXTENSION +#endif +#define JSON_HEDLEY_CLANG_HAS_EXTENSION(extension) JSON_HEDLEY_HAS_EXTENSION(extension) + +#if defined(JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE) + #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE +#endif +#define JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) + +#if defined(JSON_HEDLEY_CLANG_HAS_WARNING) + #undef JSON_HEDLEY_CLANG_HAS_WARNING +#endif +#define JSON_HEDLEY_CLANG_HAS_WARNING(warning) JSON_HEDLEY_HAS_WARNING(warning) + +#endif /* !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < X) */ + + +// This file contains all internal macro definitions (except those affecting ABI) +// You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them + +// #include + + +// exclude unsupported compilers +#if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK) + #if defined(__clang__) + #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400 + #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers" + #endif + #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER)) + #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800 + #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers" + #endif + #endif +#endif + +// C++ language standard detection +// if the user manually specified the used c++ version this is skipped +#if !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11) + #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) + #define JSON_HAS_CPP_20 + #define JSON_HAS_CPP_17 + #define JSON_HAS_CPP_14 + #elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 + #define JSON_HAS_CPP_17 + #define JSON_HAS_CPP_14 + #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) + #define JSON_HAS_CPP_14 + #endif + // the cpp 11 flag is always specified because it is the minimal required version + #define JSON_HAS_CPP_11 +#endif + +#ifdef __has_include + #if __has_include() + #include + #endif +#endif + +#if !defined(JSON_HAS_FILESYSTEM) && !defined(JSON_HAS_EXPERIMENTAL_FILESYSTEM) + #ifdef JSON_HAS_CPP_17 + #if defined(__cpp_lib_filesystem) + #define JSON_HAS_FILESYSTEM 1 + #elif defined(__cpp_lib_experimental_filesystem) + #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1 + #elif !defined(__has_include) + #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1 + #elif __has_include() + #define JSON_HAS_FILESYSTEM 1 + #elif __has_include() + #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1 + #endif + + // std::filesystem does not work on MinGW GCC 8: https://sourceforge.net/p/mingw-w64/bugs/737/ + #if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ == 8 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + + // no filesystem support before GCC 8: https://en.cppreference.com/w/cpp/compiler_support + #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 8 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + + // no filesystem support before Clang 7: https://en.cppreference.com/w/cpp/compiler_support + #if defined(__clang_major__) && __clang_major__ < 7 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + + // no filesystem support before MSVC 19.14: https://en.cppreference.com/w/cpp/compiler_support + #if defined(_MSC_VER) && _MSC_VER < 1914 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + + // no filesystem support before iOS 13 + #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 130000 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + + // no filesystem support before macOS Catalina + #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101500 + #undef JSON_HAS_FILESYSTEM + #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #endif + #endif +#endif + +#ifndef JSON_HAS_EXPERIMENTAL_FILESYSTEM + #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 0 +#endif + +#ifndef JSON_HAS_FILESYSTEM + #define JSON_HAS_FILESYSTEM 0 +#endif + +#ifndef JSON_HAS_THREE_WAY_COMPARISON + #if defined(__cpp_impl_three_way_comparison) && __cpp_impl_three_way_comparison >= 201907L \ + && defined(__cpp_lib_three_way_comparison) && __cpp_lib_three_way_comparison >= 201907L + #define JSON_HAS_THREE_WAY_COMPARISON 1 + #else + #define JSON_HAS_THREE_WAY_COMPARISON 0 + #endif +#endif + +#ifndef JSON_HAS_RANGES + // ranges header shipping in GCC 11.1.0 (released 2021-04-27) has syntax error + #if defined(__GLIBCXX__) && __GLIBCXX__ == 20210427 + #define JSON_HAS_RANGES 0 + #elif defined(__cpp_lib_ranges) + #define JSON_HAS_RANGES 1 + #else + #define JSON_HAS_RANGES 0 + #endif +#endif + +#ifndef JSON_HAS_STATIC_RTTI + #if !defined(_HAS_STATIC_RTTI) || _HAS_STATIC_RTTI != 0 + #define JSON_HAS_STATIC_RTTI 1 + #else + #define JSON_HAS_STATIC_RTTI 0 + #endif +#endif + +#ifdef JSON_HAS_CPP_17 + #define JSON_INLINE_VARIABLE inline +#else + #define JSON_INLINE_VARIABLE +#endif + +#if JSON_HEDLEY_HAS_ATTRIBUTE(no_unique_address) + #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]] +#else + #define JSON_NO_UNIQUE_ADDRESS +#endif + +// disable documentation warnings on clang +#if defined(__clang__) + #pragma clang diagnostic push + #pragma clang diagnostic ignored "-Wdocumentation" + #pragma clang diagnostic ignored "-Wdocumentation-unknown-command" +#endif + +// allow disabling exceptions +#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION) + #define JSON_THROW(exception) throw exception + #define JSON_TRY try + #define JSON_CATCH(exception) catch(exception) + #define JSON_INTERNAL_CATCH(exception) catch(exception) +#else + #include + #define JSON_THROW(exception) std::abort() + #define JSON_TRY if(true) + #define JSON_CATCH(exception) if(false) + #define JSON_INTERNAL_CATCH(exception) if(false) +#endif + +// override exception macros +#if defined(JSON_THROW_USER) + #undef JSON_THROW + #define JSON_THROW JSON_THROW_USER +#endif +#if defined(JSON_TRY_USER) + #undef JSON_TRY + #define JSON_TRY JSON_TRY_USER +#endif +#if defined(JSON_CATCH_USER) + #undef JSON_CATCH + #define JSON_CATCH JSON_CATCH_USER + #undef JSON_INTERNAL_CATCH + #define JSON_INTERNAL_CATCH JSON_CATCH_USER +#endif +#if defined(JSON_INTERNAL_CATCH_USER) + #undef JSON_INTERNAL_CATCH + #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER +#endif + +// allow overriding assert +#if !defined(JSON_ASSERT) + #include // assert + #define JSON_ASSERT(x) assert(x) +#endif + +// allow to access some private functions (needed by the test suite) +#if defined(JSON_TESTS_PRIVATE) + #define JSON_PRIVATE_UNLESS_TESTED public +#else + #define JSON_PRIVATE_UNLESS_TESTED private +#endif + +/*! +@brief macro to briefly define a mapping between an enum and JSON +@def NLOHMANN_JSON_SERIALIZE_ENUM +@since version 3.4.0 +*/ +#define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \ + template \ + inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \ + { \ + static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!"); \ + static const std::pair m[] = __VA_ARGS__; \ + auto it = std::find_if(std::begin(m), std::end(m), \ + [e](const std::pair& ej_pair) -> bool \ + { \ + return ej_pair.first == e; \ + }); \ + j = ((it != std::end(m)) ? it : std::begin(m))->second; \ + } \ + template \ + inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \ + { \ + static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!"); \ + static const std::pair m[] = __VA_ARGS__; \ + auto it = std::find_if(std::begin(m), std::end(m), \ + [&j](const std::pair& ej_pair) -> bool \ + { \ + return ej_pair.second == j; \ + }); \ + e = ((it != std::end(m)) ? it : std::begin(m))->first; \ + } + +// Ugly macros to avoid uglier copy-paste when specializing basic_json. They +// may be removed in the future once the class is split. + +#define NLOHMANN_BASIC_JSON_TPL_DECLARATION \ + template class ObjectType, \ + template class ArrayType, \ + class StringType, class BooleanType, class NumberIntegerType, \ + class NumberUnsignedType, class NumberFloatType, \ + template class AllocatorType, \ + template class JSONSerializer, \ + class BinaryType, \ + class CustomBaseClass> + +#define NLOHMANN_BASIC_JSON_TPL \ + basic_json + +// Macros to simplify conversion from/to types + +#define NLOHMANN_JSON_EXPAND( x ) x +#define NLOHMANN_JSON_GET_MACRO(_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, _64, NAME,...) NAME +#define NLOHMANN_JSON_PASTE(...) NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_GET_MACRO(__VA_ARGS__, \ + NLOHMANN_JSON_PASTE64, \ + NLOHMANN_JSON_PASTE63, \ + NLOHMANN_JSON_PASTE62, \ + NLOHMANN_JSON_PASTE61, \ + NLOHMANN_JSON_PASTE60, \ + NLOHMANN_JSON_PASTE59, \ + NLOHMANN_JSON_PASTE58, \ + NLOHMANN_JSON_PASTE57, \ + NLOHMANN_JSON_PASTE56, \ + NLOHMANN_JSON_PASTE55, \ + NLOHMANN_JSON_PASTE54, \ + NLOHMANN_JSON_PASTE53, \ + NLOHMANN_JSON_PASTE52, \ + NLOHMANN_JSON_PASTE51, \ + NLOHMANN_JSON_PASTE50, \ + NLOHMANN_JSON_PASTE49, \ + NLOHMANN_JSON_PASTE48, \ + NLOHMANN_JSON_PASTE47, \ + NLOHMANN_JSON_PASTE46, \ + NLOHMANN_JSON_PASTE45, \ + NLOHMANN_JSON_PASTE44, \ + NLOHMANN_JSON_PASTE43, \ + NLOHMANN_JSON_PASTE42, \ + NLOHMANN_JSON_PASTE41, \ + NLOHMANN_JSON_PASTE40, \ + NLOHMANN_JSON_PASTE39, \ + NLOHMANN_JSON_PASTE38, \ + NLOHMANN_JSON_PASTE37, \ + NLOHMANN_JSON_PASTE36, \ + NLOHMANN_JSON_PASTE35, \ + NLOHMANN_JSON_PASTE34, \ + NLOHMANN_JSON_PASTE33, \ + NLOHMANN_JSON_PASTE32, \ + NLOHMANN_JSON_PASTE31, \ + NLOHMANN_JSON_PASTE30, \ + NLOHMANN_JSON_PASTE29, \ + NLOHMANN_JSON_PASTE28, \ + NLOHMANN_JSON_PASTE27, \ + NLOHMANN_JSON_PASTE26, \ + NLOHMANN_JSON_PASTE25, \ + NLOHMANN_JSON_PASTE24, \ + NLOHMANN_JSON_PASTE23, \ + NLOHMANN_JSON_PASTE22, \ + NLOHMANN_JSON_PASTE21, \ + NLOHMANN_JSON_PASTE20, \ + NLOHMANN_JSON_PASTE19, \ + NLOHMANN_JSON_PASTE18, \ + NLOHMANN_JSON_PASTE17, \ + NLOHMANN_JSON_PASTE16, \ + NLOHMANN_JSON_PASTE15, \ + NLOHMANN_JSON_PASTE14, \ + NLOHMANN_JSON_PASTE13, \ + NLOHMANN_JSON_PASTE12, \ + NLOHMANN_JSON_PASTE11, \ + NLOHMANN_JSON_PASTE10, \ + NLOHMANN_JSON_PASTE9, \ + NLOHMANN_JSON_PASTE8, \ + NLOHMANN_JSON_PASTE7, \ + NLOHMANN_JSON_PASTE6, \ + NLOHMANN_JSON_PASTE5, \ + NLOHMANN_JSON_PASTE4, \ + NLOHMANN_JSON_PASTE3, \ + NLOHMANN_JSON_PASTE2, \ + NLOHMANN_JSON_PASTE1)(__VA_ARGS__)) +#define NLOHMANN_JSON_PASTE2(func, v1) func(v1) +#define NLOHMANN_JSON_PASTE3(func, v1, v2) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE2(func, v2) +#define NLOHMANN_JSON_PASTE4(func, v1, v2, v3) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE3(func, v2, v3) +#define NLOHMANN_JSON_PASTE5(func, v1, v2, v3, v4) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE4(func, v2, v3, v4) +#define NLOHMANN_JSON_PASTE6(func, v1, v2, v3, v4, v5) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE5(func, v2, v3, v4, v5) +#define NLOHMANN_JSON_PASTE7(func, v1, v2, v3, v4, v5, v6) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE6(func, v2, v3, v4, v5, v6) +#define NLOHMANN_JSON_PASTE8(func, v1, v2, v3, v4, v5, v6, v7) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE7(func, v2, v3, v4, v5, v6, v7) +#define NLOHMANN_JSON_PASTE9(func, v1, v2, v3, v4, v5, v6, v7, v8) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE8(func, v2, v3, v4, v5, v6, v7, v8) +#define NLOHMANN_JSON_PASTE10(func, v1, v2, v3, v4, v5, v6, v7, v8, v9) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE9(func, v2, v3, v4, v5, v6, v7, v8, v9) +#define NLOHMANN_JSON_PASTE11(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE10(func, v2, v3, v4, v5, v6, v7, v8, v9, v10) +#define NLOHMANN_JSON_PASTE12(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE11(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) +#define NLOHMANN_JSON_PASTE13(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE12(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) +#define NLOHMANN_JSON_PASTE14(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE13(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) +#define NLOHMANN_JSON_PASTE15(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE14(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) +#define NLOHMANN_JSON_PASTE16(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE15(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) +#define NLOHMANN_JSON_PASTE17(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE16(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) +#define NLOHMANN_JSON_PASTE18(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE17(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17) +#define NLOHMANN_JSON_PASTE19(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE18(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18) +#define NLOHMANN_JSON_PASTE20(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE19(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19) +#define NLOHMANN_JSON_PASTE21(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE20(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20) +#define NLOHMANN_JSON_PASTE22(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE21(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21) +#define NLOHMANN_JSON_PASTE23(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE22(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) +#define NLOHMANN_JSON_PASTE24(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE23(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23) +#define NLOHMANN_JSON_PASTE25(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE24(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24) +#define NLOHMANN_JSON_PASTE26(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE25(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25) +#define NLOHMANN_JSON_PASTE27(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE26(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26) +#define NLOHMANN_JSON_PASTE28(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE27(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27) +#define NLOHMANN_JSON_PASTE29(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE28(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28) +#define NLOHMANN_JSON_PASTE30(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE29(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29) +#define NLOHMANN_JSON_PASTE31(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE30(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30) +#define NLOHMANN_JSON_PASTE32(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE31(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31) +#define NLOHMANN_JSON_PASTE33(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE32(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32) +#define NLOHMANN_JSON_PASTE34(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE33(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33) +#define NLOHMANN_JSON_PASTE35(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE34(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34) +#define NLOHMANN_JSON_PASTE36(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE35(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35) +#define NLOHMANN_JSON_PASTE37(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE36(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36) +#define NLOHMANN_JSON_PASTE38(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE37(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37) +#define NLOHMANN_JSON_PASTE39(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE38(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38) +#define NLOHMANN_JSON_PASTE40(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE39(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39) +#define NLOHMANN_JSON_PASTE41(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE40(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40) +#define NLOHMANN_JSON_PASTE42(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE41(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41) +#define NLOHMANN_JSON_PASTE43(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE42(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42) +#define NLOHMANN_JSON_PASTE44(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE43(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43) +#define NLOHMANN_JSON_PASTE45(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE44(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44) +#define NLOHMANN_JSON_PASTE46(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE45(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45) +#define NLOHMANN_JSON_PASTE47(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE46(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46) +#define NLOHMANN_JSON_PASTE48(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE47(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47) +#define NLOHMANN_JSON_PASTE49(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE48(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48) +#define NLOHMANN_JSON_PASTE50(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE49(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49) +#define NLOHMANN_JSON_PASTE51(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE50(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50) +#define NLOHMANN_JSON_PASTE52(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE51(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51) +#define NLOHMANN_JSON_PASTE53(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE52(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52) +#define NLOHMANN_JSON_PASTE54(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE53(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53) +#define NLOHMANN_JSON_PASTE55(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE54(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54) +#define NLOHMANN_JSON_PASTE56(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE55(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55) +#define NLOHMANN_JSON_PASTE57(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE56(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56) +#define NLOHMANN_JSON_PASTE58(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE57(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57) +#define NLOHMANN_JSON_PASTE59(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE58(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58) +#define NLOHMANN_JSON_PASTE60(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE59(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59) +#define NLOHMANN_JSON_PASTE61(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE60(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60) +#define NLOHMANN_JSON_PASTE62(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE61(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61) +#define NLOHMANN_JSON_PASTE63(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE62(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62) +#define NLOHMANN_JSON_PASTE64(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE63(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63) + +#define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1; +#define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1); +#define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1); + +/*! +@brief macro +@def NLOHMANN_DEFINE_TYPE_INTRUSIVE +@since version 3.9.0 +*/ +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \ + friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ + friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } + +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \ + friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ + friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) } + +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE(Type, ...) \ + friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } + +/*! +@brief macro +@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE +@since version 3.9.0 +*/ +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \ + inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ + inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } + +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(Type, ...) \ + inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } + +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \ + inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ + inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) } + +// inspired from https://stackoverflow.com/a/26745591 +// allows to call any std function as if (e.g. with begin): +// using std::begin; begin(x); +// +// it allows using the detected idiom to retrieve the return type +// of such an expression +#define NLOHMANN_CAN_CALL_STD_FUNC_IMPL(std_name) \ + namespace detail { \ + using std::std_name; \ + \ + template \ + using result_of_##std_name = decltype(std_name(std::declval()...)); \ + } \ + \ + namespace detail2 { \ + struct std_name##_tag \ + { \ + }; \ + \ + template \ + std_name##_tag std_name(T&&...); \ + \ + template \ + using result_of_##std_name = decltype(std_name(std::declval()...)); \ + \ + template \ + struct would_call_std_##std_name \ + { \ + static constexpr auto const value = ::nlohmann::detail:: \ + is_detected_exact::value; \ + }; \ + } /* namespace detail2 */ \ + \ + template \ + struct would_call_std_##std_name : detail2::would_call_std_##std_name \ + { \ + } + +#ifndef JSON_USE_IMPLICIT_CONVERSIONS + #define JSON_USE_IMPLICIT_CONVERSIONS 1 +#endif + +#if JSON_USE_IMPLICIT_CONVERSIONS + #define JSON_EXPLICIT +#else + #define JSON_EXPLICIT explicit +#endif + +#ifndef JSON_DISABLE_ENUM_SERIALIZATION + #define JSON_DISABLE_ENUM_SERIALIZATION 0 +#endif + +#ifndef JSON_USE_GLOBAL_UDLS + #define JSON_USE_GLOBAL_UDLS 1 +#endif + +#if JSON_HAS_THREE_WAY_COMPARISON + #include // partial_ordering +#endif + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +/////////////////////////// +// JSON type enumeration // +/////////////////////////// + +/*! +@brief the JSON type enumeration + +This enumeration collects the different JSON types. It is internally used to +distinguish the stored values, and the functions @ref basic_json::is_null(), +@ref basic_json::is_object(), @ref basic_json::is_array(), +@ref basic_json::is_string(), @ref basic_json::is_boolean(), +@ref basic_json::is_number() (with @ref basic_json::is_number_integer(), +@ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()), +@ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and +@ref basic_json::is_structured() rely on it. + +@note There are three enumeration entries (number_integer, number_unsigned, and +number_float), because the library distinguishes these three types for numbers: +@ref basic_json::number_unsigned_t is used for unsigned integers, +@ref basic_json::number_integer_t is used for signed integers, and +@ref basic_json::number_float_t is used for floating-point numbers or to +approximate integers which do not fit in the limits of their respective type. + +@sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON +value with the default value for a given type + +@since version 1.0.0 +*/ +enum class value_t : std::uint8_t +{ + null, ///< null value + object, ///< object (unordered set of name/value pairs) + array, ///< array (ordered collection of values) + string, ///< string value + boolean, ///< boolean value + number_integer, ///< number value (signed integer) + number_unsigned, ///< number value (unsigned integer) + number_float, ///< number value (floating-point) + binary, ///< binary array (ordered collection of bytes) + discarded ///< discarded by the parser callback function +}; + +/*! +@brief comparison operator for JSON types + +Returns an ordering that is similar to Python: +- order: null < boolean < number < object < array < string < binary +- furthermore, each type is not smaller than itself +- discarded values are not comparable +- binary is represented as a b"" string in python and directly comparable to a + string; however, making a binary array directly comparable with a string would + be surprising behavior in a JSON file. + +@since version 1.0.0 +*/ +#if JSON_HAS_THREE_WAY_COMPARISON + inline std::partial_ordering operator<=>(const value_t lhs, const value_t rhs) noexcept // *NOPAD* +#else + inline bool operator<(const value_t lhs, const value_t rhs) noexcept +#endif +{ + static constexpr std::array order = {{ + 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */, + 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */, + 6 /* binary */ + } + }; + + const auto l_index = static_cast(lhs); + const auto r_index = static_cast(rhs); +#if JSON_HAS_THREE_WAY_COMPARISON + if (l_index < order.size() && r_index < order.size()) + { + return order[l_index] <=> order[r_index]; // *NOPAD* + } + return std::partial_ordering::unordered; +#else + return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index]; +#endif +} + +// GCC selects the built-in operator< over an operator rewritten from +// a user-defined spaceship operator +// Clang, MSVC, and ICC select the rewritten candidate +// (see GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105200) +#if JSON_HAS_THREE_WAY_COMPARISON && defined(__GNUC__) +inline bool operator<(const value_t lhs, const value_t rhs) noexcept +{ + return std::is_lt(lhs <=> rhs); // *NOPAD* +} +#endif + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +// #include + + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +/*! +@brief replace all occurrences of a substring by another string + +@param[in,out] s the string to manipulate; changed so that all + occurrences of @a f are replaced with @a t +@param[in] f the substring to replace with @a t +@param[in] t the string to replace @a f + +@pre The search string @a f must not be empty. **This precondition is +enforced with an assertion.** + +@since version 2.0.0 +*/ +template +inline void replace_substring(StringType& s, const StringType& f, + const StringType& t) +{ + JSON_ASSERT(!f.empty()); + for (auto pos = s.find(f); // find first occurrence of f + pos != StringType::npos; // make sure f was found + s.replace(pos, f.size(), t), // replace with t, and + pos = s.find(f, pos + t.size())) // find next occurrence of f + {} +} + +/*! + * @brief string escaping as described in RFC 6901 (Sect. 4) + * @param[in] s string to escape + * @return escaped string + * + * Note the order of escaping "~" to "~0" and "/" to "~1" is important. + */ +template +inline StringType escape(StringType s) +{ + replace_substring(s, StringType{"~"}, StringType{"~0"}); + replace_substring(s, StringType{"/"}, StringType{"~1"}); + return s; +} + +/*! + * @brief string unescaping as described in RFC 6901 (Sect. 4) + * @param[in] s string to unescape + * @return unescaped string + * + * Note the order of escaping "~1" to "/" and "~0" to "~" is important. + */ +template +static void unescape(StringType& s) +{ + replace_substring(s, StringType{"~1"}, StringType{"/"}); + replace_substring(s, StringType{"~0"}, StringType{"~"}); +} + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include // size_t + +// #include + + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +/// struct to capture the start position of the current token +struct position_t +{ + /// the total number of characters read + std::size_t chars_read_total = 0; + /// the number of characters read in the current line + std::size_t chars_read_current_line = 0; + /// the number of lines read + std::size_t lines_read = 0; + + /// conversion to size_t to preserve SAX interface + constexpr operator size_t() const + { + return chars_read_total; + } +}; + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + +// #include + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-FileCopyrightText: 2018 The Abseil Authors +// SPDX-License-Identifier: MIT + + + +#include // array +#include // size_t +#include // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type +#include // index_sequence, make_index_sequence, index_sequence_for + +// #include + + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +template +using uncvref_t = typename std::remove_cv::type>::type; + +#ifdef JSON_HAS_CPP_14 + +// the following utilities are natively available in C++14 +using std::enable_if_t; +using std::index_sequence; +using std::make_index_sequence; +using std::index_sequence_for; + +#else + +// alias templates to reduce boilerplate +template +using enable_if_t = typename std::enable_if::type; + +// The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h +// which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0. + +//// START OF CODE FROM GOOGLE ABSEIL + +// integer_sequence +// +// Class template representing a compile-time integer sequence. An instantiation +// of `integer_sequence` has a sequence of integers encoded in its +// type through its template arguments (which is a common need when +// working with C++11 variadic templates). `absl::integer_sequence` is designed +// to be a drop-in replacement for C++14's `std::integer_sequence`. +// +// Example: +// +// template< class T, T... Ints > +// void user_function(integer_sequence); +// +// int main() +// { +// // user_function's `T` will be deduced to `int` and `Ints...` +// // will be deduced to `0, 1, 2, 3, 4`. +// user_function(make_integer_sequence()); +// } +template +struct integer_sequence +{ + using value_type = T; + static constexpr std::size_t size() noexcept + { + return sizeof...(Ints); + } +}; + +// index_sequence +// +// A helper template for an `integer_sequence` of `size_t`, +// `absl::index_sequence` is designed to be a drop-in replacement for C++14's +// `std::index_sequence`. +template +using index_sequence = integer_sequence; + +namespace utility_internal +{ + +template +struct Extend; + +// Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency. +template +struct Extend, SeqSize, 0> +{ + using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >; +}; + +template +struct Extend, SeqSize, 1> +{ + using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >; +}; + +// Recursion helper for 'make_integer_sequence'. +// 'Gen::type' is an alias for 'integer_sequence'. +template +struct Gen +{ + using type = + typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type; +}; + +template +struct Gen +{ + using type = integer_sequence; +}; + +} // namespace utility_internal + +// Compile-time sequences of integers + +// make_integer_sequence +// +// This template alias is equivalent to +// `integer_sequence`, and is designed to be a drop-in +// replacement for C++14's `std::make_integer_sequence`. +template +using make_integer_sequence = typename utility_internal::Gen::type; + +// make_index_sequence +// +// This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`, +// and is designed to be a drop-in replacement for C++14's +// `std::make_index_sequence`. +template +using make_index_sequence = make_integer_sequence; + +// index_sequence_for +// +// Converts a typename pack into an index sequence of the same length, and +// is designed to be a drop-in replacement for C++14's +// `std::index_sequence_for()` +template +using index_sequence_for = make_index_sequence; + +//// END OF CODE FROM GOOGLE ABSEIL + +#endif + +// dispatch utility (taken from ranges-v3) +template struct priority_tag : priority_tag < N - 1 > {}; +template<> struct priority_tag<0> {}; + +// taken from ranges-v3 +template +struct static_const +{ + static JSON_INLINE_VARIABLE constexpr T value{}; +}; + +#ifndef JSON_HAS_CPP_17 + template + constexpr T static_const::value; +#endif + +template +inline constexpr std::array make_array(Args&& ... args) +{ + return std::array {{static_cast(std::forward(args))...}}; +} + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include // numeric_limits +#include // false_type, is_constructible, is_integral, is_same, true_type +#include // declval +#include // tuple +#include // char_traits + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +#include // random_access_iterator_tag + +// #include + +// #include + +// #include + + +NLOHMANN_JSON_NAMESPACE_BEGIN +namespace detail +{ + +template +struct iterator_types {}; + +template +struct iterator_types < + It, + void_t> +{ + using difference_type = typename It::difference_type; + using value_type = typename It::value_type; + using pointer = typename It::pointer; + using reference = typename It::reference; + using iterator_category = typename It::iterator_category; +}; + +// This is required as some compilers implement std::iterator_traits in a way that +// doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341. +template +struct iterator_traits +{ +}; + +template +struct iterator_traits < T, enable_if_t < !std::is_pointer::value >> + : iterator_types +{ +}; + +template +struct iterator_traits::value>> +{ + using iterator_category = std::random_access_iterator_tag; + using value_type = T; + using difference_type = ptrdiff_t; + using pointer = T*; + using reference = T&; +}; + +} // namespace detail +NLOHMANN_JSON_NAMESPACE_END + +// #include + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +// #include + + +NLOHMANN_JSON_NAMESPACE_BEGIN + +NLOHMANN_CAN_CALL_STD_FUNC_IMPL(begin); + +NLOHMANN_JSON_NAMESPACE_END + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + + + +// #include + + +NLOHMANN_JSON_NAMESPACE_BEGIN + +NLOHMANN_CAN_CALL_STD_FUNC_IMPL(end); + +NLOHMANN_JSON_NAMESPACE_END + +// #include + +// #include + +// #include +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ +// | | |__ | | | | | | version 3.11.3 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2023 Niels Lohmann +// SPDX-License-Identifier: MIT + +#ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_ + #define INCLUDE_NLOHMANN_JSON_FWD_HPP_ + + #include // int64_t, uint64_t + #include // map + #include // allocator + #include // string + #include // vector + + // #include + + + /*! + @brief namespace for Niels Lohmann + @see https://github.com/nlohmann + @since version 1.0.0 + */ + NLOHMANN_JSON_NAMESPACE_BEGIN + + /*! + @brief default JSONSerializer template argument + + This serializer ignores the template arguments and uses ADL + ([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl)) + for serialization. + */ + template + struct adl_serializer; + + /// a class to store JSON values + /// @sa https://json.nlohmann.me/api/basic_json/ + template class ObjectType = + std::map, + template class ArrayType = std::vector, + class StringType = std::string, class BooleanType = bool, + class NumberIntegerType = std::int64_t, + class NumberUnsignedType = std::uint64_t, + class NumberFloatType = double, + template class AllocatorType = std::allocator, + template class JSONSerializer = + adl_serializer, + class BinaryType = std::vector, // cppcheck-suppress syntaxError + class CustomBaseClass = void> + class basic_json; + + /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document + /// @sa https://json.nlohmann.me/api/json_pointer/ + template + class json_pointer; + + /*! + @brief default specialization + @sa https://json.nlohmann.me/api/json/ + */ + using json = basic_json<>; + + /// @brief a minimal map-like container that preserves insertion order + /// @sa https://json.nlohmann.me/api/ordered_map/ + template + struct ordered_map; + + /// @brief specialization that maintains the insertion order of object keys + /// @sa https://json.nlohmann.me/api/ordered_json/ + using ordered_json = basic_json; + + NLOHMANN_JSON_NAMESPACE_END + +#endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_ + + +NLOHMANN_JSON_NAMESPACE_BEGIN +/*! +@brief detail namespace with internal helper functions + +This namespace collects functions that should not be exposed, +implementations of some @ref basic_json methods, and meta-programming helpers. + +@since version 2.1.0 +*/ +namespace detail +{ + +///////////// +// helpers // +///////////// + +// Note to maintainers: +// +// Every trait in this file expects a non CV-qualified type. +// The only exceptions are in the 'aliases for detected' section +// (i.e. those of the form: decltype(T::member_function(std::declval()))) +// +// In this case, T has to be properly CV-qualified to constraint the function arguments +// (e.g. to_json(BasicJsonType&, const T&)) + +template struct is_basic_json : std::false_type {}; + +NLOHMANN_BASIC_JSON_TPL_DECLARATION +struct is_basic_json : std::true_type {}; + +// used by exceptions create() member functions +// true_type for pointer to possibly cv-qualified basic_json or std::nullptr_t +// false_type otherwise +template +struct is_basic_json_context : + std::integral_constant < bool, + is_basic_json::type>::type>::value + || std::is_same::value > +{}; + +////////////////////// +// json_ref helpers // +////////////////////// + +template +class json_ref; + +template +struct is_json_ref : std::false_type {}; + +template +struct is_json_ref> : std::true_type {}; + +////////////////////////// +// aliases for detected // +////////////////////////// + +template +using mapped_type_t = typename T::mapped_type; + +template +using key_type_t = typename T::key_type; + +template +using value_type_t = typename T::value_type; + +template +using difference_type_t = typename T::difference_type; + +template +using pointer_t = typename T::pointer; + +template +using reference_t = typename T::reference; + +template +using iterator_category_t = typename T::iterator_category; + +template +using to_json_function = decltype(T::to_json(std::declval()...)); + +template +using from_json_function = decltype(T::from_json(std::declval()...)); + +template +using get_template_function = decltype(std::declval().template get()); + +// trait checking if JSONSerializer::from_json(json const&, udt&) exists +template +struct has_from_json : std::false_type {}; + +// trait checking if j.get is valid +// use this trait instead of std::is_constructible or std::is_convertible, +// both rely on, or make use of implicit conversions, and thus fail when T +// has several constructors/operator= (see https://github.com/nlohmann/json/issues/958) +template +struct is_getable +{ + static constexpr bool value = is_detected::value; +}; + +template +struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json::value >> +{ + using serializer = typename BasicJsonType::template json_serializer; + + static constexpr bool value = + is_detected_exact::value; +}; + +// This trait checks if JSONSerializer::from_json(json const&) exists +// this overload is used for non-default-constructible user-defined-types +template +struct has_non_default_from_json : std::false_type {}; + +template +struct has_non_default_from_json < BasicJsonType, T, enable_if_t < !is_basic_json::value >> +{ + using serializer = typename BasicJsonType::template json_serializer; + + static constexpr bool value = + is_detected_exact::value; +}; + +// This trait checks if BasicJsonType::json_serializer::to_json exists +// Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion. +template +struct has_to_json : std::false_type {}; + +template +struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json::value >> +{ + using serializer = typename BasicJsonType::template json_serializer; + + static constexpr bool value = + is_detected_exact::value; +}; + +template +using detect_key_compare = typename T::key_compare; + +template +struct has_key_compare : std::integral_constant::value> {}; + +// obtains the actual object key comparator +template +struct actual_object_comparator +{ + using object_t = typename BasicJsonType::object_t; + using object_comparator_t = typename BasicJsonType::default_object_comparator_t; + using type = typename std::conditional < has_key_compare::value, + typename object_t::key_compare, object_comparator_t>::type; +}; + +template +using actual_object_comparator_t = typename actual_object_comparator::type; + +///////////////// +// char_traits // +///////////////// + +// Primary template of char_traits calls std char_traits +template +struct char_traits : std::char_traits +{}; + +// Explicitly define char traits for unsigned char since it is not standard +template<> +struct char_traits : std::char_traits +{ + using char_type = unsigned char; + using int_type = uint64_t; + + // Redefine to_int_type function + static int_type to_int_type(char_type c) noexcept + { + return static_cast(c); + } + + static char_type to_char_type(int_type i) noexcept + { + return static_cast(i); + } + + static constexpr int_type eof() noexcept + { + return static_cast(EOF); + } +}; + +// Explicitly define char traits for signed char since it is not standard +template<> +struct char_traits : std::char_traits +{ + using char_type = signed char; + using int_type = uint64_t; + + // Redefine to_int_type function + static int_type to_int_type(char_type c) noexcept + { + return static_cast(c); + } + + static char_type to_char_type(int_type i) noexcept + { + return static_cast(i); + } + + static constexpr int_type eof() noexcept + { + return static_cast(EOF); + } +}; + +/////////////////// +// is_ functions // +/////////////////// + +// https://en.cppreference.com/w/cpp/types/conjunction +template struct conjunction : std::true_type { }; +template struct conjunction : B { }; +template +struct conjunction +: std::conditional(B::value), conjunction, B>::type {}; + +// https://en.cppreference.com/w/cpp/types/negation +template struct negation : std::integral_constant < bool, !B::value > { }; + +// Reimplementation of is_constructible and is_default_constructible, due to them being broken for +// std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367). +// This causes compile errors in e.g. clang 3.5 or gcc 4.9. +template +struct is_default_constructible : std::is_default_constructible {}; + +template +struct is_default_constructible> + : conjunction, is_default_constructible> {}; + +template +struct is_default_constructible> + : conjunction, is_default_constructible> {}; + +template +struct is_default_constructible> + : conjunction...> {}; + +template +struct is_default_constructible> + : conjunction...> {}; + +template +struct is_constructible : std::is_constructible {}; + +template +struct is_constructible> : is_default_constructible> {}; + +template +struct is_constructible> : is_default_constructible> {}; + +template +struct is_constructible> : is_default_constructible> {}; + +template +struct is_constructible> : is_default_constructible> {}; + +template +struct is_iterator_traits : std::false_type {}; + +template +struct is_iterator_traits> +{ + private: + using traits = iterator_traits; + + public: + static constexpr auto value = + is_detected::value && + is_detected::value && + is_detected::value && + is_detected::value && + is_detected::value; +}; + +template +struct is_range +{ + private: + using t_ref = typename std::add_lvalue_reference::type; + + using iterator = detected_t; + using sentinel = detected_t; + + // to be 100% correct, it should use https://en.cppreference.com/w/cpp/iterator/input_or_output_iterator + // and https://en.cppreference.com/w/cpp/iterator/sentinel_for + // but reimplementing these would be too much work, as a lot of other concepts are used underneath + static constexpr auto is_iterator_begin = + is_iterator_traits>::value; + + public: + static constexpr bool value = !std::is_same::value && !std::is_same::value && is_iterator_begin; +}; + +template +using iterator_t = enable_if_t::value, result_of_begin())>>; + +template +using range_value_t = value_type_t>>; + +// The following implementation of is_complete_type is taken from +// https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/ +// and is written by Xiang Fan who agreed to using it in this library. + +template +struct is_complete_type : std::false_type {}; + +template +struct is_complete_type : std::true_type {}; + +template +struct is_compatible_object_type_impl : std::false_type {}; + +template +struct is_compatible_object_type_impl < + BasicJsonType, CompatibleObjectType, + enable_if_t < is_detected::value&& + is_detected::value >> +{ + using object_t = typename BasicJsonType::object_t; + + // macOS's is_constructible does not play well with nonesuch... + static constexpr bool value = + is_constructible::value && + is_constructible::value; +}; + +template +struct is_compatible_object_type + : is_compatible_object_type_impl {}; + +template +struct is_constructible_object_type_impl : std::false_type {}; + +template +struct is_constructible_object_type_impl < + BasicJsonType, ConstructibleObjectType, + enable_if_t < is_detected::value&& + is_detected::value >> +{ + using object_t = typename BasicJsonType::object_t; + + static constexpr bool value = + (is_default_constructible::value && + (std::is_move_assignable::value || + std::is_copy_assignable::value) && + (is_constructible::value && + std::is_same < + typename object_t::mapped_type, + typename ConstructibleObjectType::mapped_type >::value)) || + (has_from_json::value || + has_non_default_from_json < + BasicJsonType, + typename ConstructibleObjectType::mapped_type >::value); +}; + +template +struct is_constructible_object_type + : is_constructible_object_type_impl {}; + +template +struct is_compatible_string_type +{ + static constexpr auto value = + is_constructible::value; +}; + +template +struct is_constructible_string_type +{ + // launder type through decltype() to fix compilation failure on ICPC +#ifdef __INTEL_COMPILER + using laundered_type = decltype(std::declval()); +#else + using laundered_type = ConstructibleStringType; +#endif + + static constexpr auto value = + conjunction < + is_constructible, + is_detected_exact>::value; +}; + +template +struct is_compatible_array_type_impl : std::false_type {}; + +template +struct is_compatible_array_type_impl < + BasicJsonType, CompatibleArrayType, + enable_if_t < + is_detected::value&& + is_iterator_traits>>::value&& +// special case for types like std::filesystem::path whose iterator's value_type are themselves +// c.f. https://github.com/nlohmann/json/pull/3073 + !std::is_same>::value >> +{ + static constexpr bool value = + is_constructible>::value; +}; + +template +struct is_compatible_array_type + : is_compatible_array_type_impl {}; + +template +struct is_constructible_array_type_impl : std::false_type {}; + +template +struct is_constructible_array_type_impl < + BasicJsonType, ConstructibleArrayType, + enable_if_t::value >> + : std::true_type {}; + +template +struct is_constructible_array_type_impl < + BasicJsonType, ConstructibleArrayType, + enable_if_t < !std::is_same::value&& + !is_compatible_string_type::value&& + is_default_constructible::value&& +(std::is_move_assignable::value || + std::is_copy_assignable::value)&& +is_detected::value&& +is_iterator_traits>>::value&& +is_detected::value&& +// special case for types like std::filesystem::path whose iterator's value_type are themselves +// c.f. https://github.com/nlohmann/json/pull/3073 +!std::is_same>::value&& + is_complete_type < + detected_t>::value >> +{ + using value_type = range_value_t; + + static constexpr bool value = + std::is_same::value || + has_from_json::value || + has_non_default_from_json < + BasicJsonType, + value_type >::value; +}; + +template +struct is_constructible_array_type + : is_constructible_array_type_impl {}; + +template +struct is_compatible_integer_type_impl : std::false_type {}; + +template +struct is_compatible_integer_type_impl < + RealIntegerType, CompatibleNumberIntegerType, + enable_if_t < std::is_integral::value&& + std::is_integral::value&& + !std::is_same::value >> +{ + // is there an assert somewhere on overflows? + using RealLimits = std::numeric_limits; + using CompatibleLimits = std::numeric_limits; + + static constexpr auto value = + is_constructible::value && + CompatibleLimits::is_integer && + RealLimits::is_signed == CompatibleLimits::is_signed; +}; + +template +struct is_compatible_integer_type + : is_compatible_integer_type_impl {}; + +template +struct is_compatible_type_impl: std::false_type {}; + +template +struct is_compatible_type_impl < + BasicJsonType, CompatibleType, + enable_if_t::value >> +{ + static constexpr bool value = + has_to_json::value; +}; + +template +struct is_compatible_type + : is_compatible_type_impl {}; + +template +struct is_constructible_tuple : std::false_type {}; + +template +struct is_constructible_tuple> : conjunction...> {}; + +template +struct is_json_iterator_of : std::false_type {}; + +template +struct is_json_iterator_of : std::true_type {}; + +template +struct is_json_iterator_of : std::true_type +{}; + +// checks if a given type T is a template specialization of Primary +template