From 225ab5bff4b37542c082d134b6a00804a7e6bc90 Mon Sep 17 00:00:00 2001 From: dianjixz <18637716021@163.com> Date: Wed, 27 Nov 2024 00:30:58 +0800 Subject: [PATCH] [update] StackFlow subscriber_event_call decode_stream,delet simdjson. fix decodebase64 --- .../StackFlow/stackflow/StackFlow.cpp | 39 ++++++------------- .../StackFlow/stackflow/StackFlowUtil.cpp | 7 ++-- projects/llm_framework/main/SConstruct | 2 +- projects/llm_framework/main/src/main.cpp | 2 +- projects/llm_framework/main_asr/SConstruct | 1 - projects/llm_framework/main_asr/src/main.cpp | 2 +- projects/llm_framework/main_audio/SConstruct | 2 +- projects/llm_framework/main_kws/SConstruct | 2 +- projects/llm_framework/main_kws/src/main.cpp | 2 +- projects/llm_framework/main_llm/SConstruct | 2 +- projects/llm_framework/main_llm/src/main.cpp | 2 +- .../llm_framework/main_melotts/SConstruct | 2 +- .../llm_framework/main_melotts/src/main.cpp | 2 +- projects/llm_framework/main_tts/SConstruct | 2 +- projects/llm_framework/main_tts/src/main.cpp | 2 +- projects/llm_framework/main_vlm/SConstruct | 2 +- projects/llm_framework/main_vlm/src/main.cpp | 2 +- projects/llm_framework/main_yolo/SConstruct | 2 +- projects/llm_framework/main_yolo/src/main.cpp | 2 +- 19 files changed, 31 insertions(+), 48 deletions(-) diff --git a/ext_components/StackFlow/stackflow/StackFlow.cpp b/ext_components/StackFlow/stackflow/StackFlow.cpp index 25aca68..046ed9f 100644 --- a/ext_components/StackFlow/stackflow/StackFlow.cpp +++ b/ext_components/StackFlow/stackflow/StackFlow.cpp @@ -5,7 +5,6 @@ */ #include "StackFlow.h" #include "sample_log.h" -#include using namespace StackFlows; @@ -33,34 +32,21 @@ llm_channel_obj::~llm_channel_obj() void llm_channel_obj::subscriber_event_call(const std::function &call, pzmq *_pzmq, const std::string &raw) { - try { - simdjson::padded_string json_string(raw); - simdjson::ondemand::document doc; - auto parser = _pzmq->getContextPtr(); - auto error = parser->iterate(json_string).get(doc); - if (error) { - return; - } - std::string action; - error = doc["action"].get_string(action); - if (action == "inference") { - std::string zmq_com; - error = doc["zmq_com"].get_string(zmq_com); + const char *user_inference_flage_str = "\"action\""; + std::size_t pos = raw.find(user_inference_flage_str); + while (true) { + if (pos == std::string::npos) { + break; + } else if ((pos > 0) && (raw[pos - 1] != '\\')) { + std::string zmq_com = sample_json_str_get(raw, "zmq_com"); if (!zmq_com.empty()) set_push_url(zmq_com); - error = doc["request_id"].get_string(request_id_); - error = doc["work_id"].get_string(work_id_); - } - std::string object; - error = doc["object"].get_string(object); - auto result = doc["data"].raw_json(); - if (result.error() == simdjson::SUCCESS) { - call(object, result.value().data()); - } else { - std::cerr << "result: " << result.value() << "error mesg:" << result.error() << std::endl; + request_id_ = sample_json_str_get(raw, "request_id"); + work_id_ = sample_json_str_get(raw, "work_id"); + break; } - } catch (simdjson::simdjson_error &e) { - std::cerr << "Error: " << simdjson::error_message(e.error()) << std::endl; + pos = raw.find(user_inference_flage_str, pos + sizeof(user_inference_flage_str)); } + call(sample_json_str_get(raw, "object"), sample_json_str_get(raw, "data")); } int llm_channel_obj::subscriber_work_id(const std::string &work_id, @@ -88,7 +74,6 @@ int llm_channel_obj::subscriber_work_id(const std::string &work_id, zmq_[id_num] = std::make_shared( subscriber_url, ZMQ_SUB, std::bind(&llm_channel_obj::subscriber_event_call, this, call, std::placeholders::_1, std::placeholders::_2)); - zmq_[id_num]->newContextPtr(); return 0; } diff --git a/ext_components/StackFlow/stackflow/StackFlowUtil.cpp b/ext_components/StackFlow/stackflow/StackFlowUtil.cpp index a4c3c0d..e83504f 100644 --- a/ext_components/StackFlow/stackflow/StackFlowUtil.cpp +++ b/ext_components/StackFlow/stackflow/StackFlowUtil.cpp @@ -6,7 +6,6 @@ #include "StackFlowUtil.h" #include #include "pzmq.hpp" -#include std::string StackFlows::sample_json_str_get(const std::string &json_str, const std::string &json_key) { @@ -185,9 +184,9 @@ bool StackFlows::decode_stream(const std::string &in, std::string &out, { int index = std::stoi(StackFlows::sample_json_str_get(in, "index")); std::string finish = StackFlows::sample_json_str_get(in, "finish"); - std::string delta = StackFlows::sample_json_str_get(in, "delta"); - stream_buff[index] = delta; - if (finish.find("true") != std::string::npos) { + stream_buff[index] = StackFlows::sample_json_str_get(in, "delta"); + // sample find flage: false:true + if (finish.find("f") == std::string::npos) { for (size_t i = 0; i < stream_buff.size(); i++) { out += stream_buff.at(i); } diff --git a/projects/llm_framework/main/SConstruct b/projects/llm_framework/main/SConstruct index 6e7374c..3284163 100644 --- a/projects/llm_framework/main/SConstruct +++ b/projects/llm_framework/main/SConstruct @@ -26,7 +26,7 @@ STATIC_FILES += [AFile('../static_lib/sherpa/ncnn/libsherpa-ncnn-core.so'), AFile('../static_lib/sherpa/ncnn/libkaldi-native-fbank-core.so'), AFile('../static_lib/libonnxruntime.so.1.14.0') ] -REQUIREMENTS += ['simdjson_component'] + env['COMPONENTS'].append({'target':'static_file', 'SRCS':SRCS, 'INCLUDE':INCLUDE, diff --git a/projects/llm_framework/main/src/main.cpp b/projects/llm_framework/main/src/main.cpp index 0fc5700..9004bd6 100644 --- a/projects/llm_framework/main/src/main.cpp +++ b/projects/llm_framework/main/src/main.cpp @@ -154,7 +154,7 @@ class llm_llm : public StackFlow { std::string tmp_msg2; if (object.find("base64") != std::string::npos) { ret = decode_base64((*next_data), tmp_msg2); - if (!ret) { + if (ret == -1) { error_body["code"] = -23; error_body["message"] = "Base64 decoding error."; send("None", "None", error_body, unit_name_); diff --git a/projects/llm_framework/main_asr/SConstruct b/projects/llm_framework/main_asr/SConstruct index dbf03fc..9a84ddd 100644 --- a/projects/llm_framework/main_asr/SConstruct +++ b/projects/llm_framework/main_asr/SConstruct @@ -26,7 +26,6 @@ REQUIREMENTS += ['ncnn', 'sherpa-ncnn-core'] STATIC_FILES += [AFile('sherpa-ncnn-streaming-zipformer-20M-2023-02-17.json'), AFile('sherpa-ncnn-streaming-zipformer-zh-14M-2023-02-23.json')] -REQUIREMENTS += ['simdjson_component'] env['COMPONENTS'].append({'target':'llm_asr', 'SRCS':SRCS, diff --git a/projects/llm_framework/main_asr/src/main.cpp b/projects/llm_framework/main_asr/src/main.cpp index 183f349..27e3798 100644 --- a/projects/llm_framework/main_asr/src/main.cpp +++ b/projects/llm_framework/main_asr/src/main.cpp @@ -379,7 +379,7 @@ class llm_asr : public StackFlow { std::string tmp_msg2; if (object.find("base64") != std::string::npos) { ret = decode_base64((*next_data), tmp_msg2); - if (!ret) { + if (ret == -1) { error_body["code"] = -23; error_body["message"] = "Base64 decoding error."; send("None", "None", error_body, unit_name_); diff --git a/projects/llm_framework/main_audio/SConstruct b/projects/llm_framework/main_audio/SConstruct index c8371f8..8e6721e 100644 --- a/projects/llm_framework/main_audio/SConstruct +++ b/projects/llm_framework/main_audio/SConstruct @@ -27,7 +27,7 @@ REQUIREMENTS += ['ax_sys', 'ax_interpreter', 'ax_audio', 'ax_audio_3a', 'ax_fdk' REQUIREMENTS += ['tinyalsa', 'opus', 'samplerate', 'fdk-aac'] STATIC_FILES += [AFile('audio.json')] -REQUIREMENTS += ['simdjson_component'] + env['COMPONENTS'].append({'target':'llm_audio', 'SRCS':SRCS, 'INCLUDE':INCLUDE, diff --git a/projects/llm_framework/main_kws/SConstruct b/projects/llm_framework/main_kws/SConstruct index 690cad3..95d3daf 100644 --- a/projects/llm_framework/main_kws/SConstruct +++ b/projects/llm_framework/main_kws/SConstruct @@ -33,7 +33,7 @@ LDFLAGS += ['-l:libcargs.a', '-l:libonnxruntime.a', STATIC_FILES += [AFile('sherpa-onnx-kws-zipformer-gigaspeech-3.3M-2024-01-01.json'), AFile('sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01.json')] -REQUIREMENTS += ['simdjson_component'] + env['COMPONENTS'].append({'target':'llm_kws', 'SRCS':SRCS, 'INCLUDE':INCLUDE, diff --git a/projects/llm_framework/main_kws/src/main.cpp b/projects/llm_framework/main_kws/src/main.cpp index 8fc3ea2..5b8f943 100644 --- a/projects/llm_framework/main_kws/src/main.cpp +++ b/projects/llm_framework/main_kws/src/main.cpp @@ -409,7 +409,7 @@ class llm_kws : public StackFlow { std::string tmp_msg2; if (object.find("base64") != std::string::npos) { ret = decode_base64((*next_data), tmp_msg2); - if (!ret) { + if (ret == -1) { error_body["code"] = -23; error_body["message"] = "Base64 decoding error."; send("None", "None", error_body, unit_name_); diff --git a/projects/llm_framework/main_llm/SConstruct b/projects/llm_framework/main_llm/SConstruct index 9b8fc77..c95c145 100644 --- a/projects/llm_framework/main_llm/SConstruct +++ b/projects/llm_framework/main_llm/SConstruct @@ -42,7 +42,7 @@ STATIC_FILES += [AFile('qwen2.5-0.5B-prefill-20e.json'), AFile('openbuddy-llama3.2-1b-ax630c_tokenizer.py'), AFile('qwen2.5-coder-0.5B-ax630c_tokenizer.py') ] -REQUIREMENTS += ['simdjson_component'] + env['COMPONENTS'].append({'target':'llm_llm', 'SRCS':SRCS, 'INCLUDE':INCLUDE, diff --git a/projects/llm_framework/main_llm/src/main.cpp b/projects/llm_framework/main_llm/src/main.cpp index 3b57ff8..8b6c557 100644 --- a/projects/llm_framework/main_llm/src/main.cpp +++ b/projects/llm_framework/main_llm/src/main.cpp @@ -317,7 +317,7 @@ class llm_llm : public StackFlow { std::string tmp_msg2; if (object.find("base64") != std::string::npos) { ret = decode_base64((*next_data), tmp_msg2); - if (!ret) { + if (ret == -1) { error_body["code"] = -23; error_body["message"] = "Base64 decoding error."; send("None", "None", error_body, unit_name_); diff --git a/projects/llm_framework/main_melotts/SConstruct b/projects/llm_framework/main_melotts/SConstruct index ad89084..6124c3e 100644 --- a/projects/llm_framework/main_melotts/SConstruct +++ b/projects/llm_framework/main_melotts/SConstruct @@ -27,7 +27,7 @@ INCLUDE += [ADir('../include')] INCLUDE += [ADir('src/runner'), ADir('../include/onnxruntime/core/session')] STATIC_FILES += [AFile('melotts_zh-cn.json')] -REQUIREMENTS += ['simdjson_component'] + env['COMPONENTS'].append({'target':'llm_melotts', 'SRCS':SRCS, 'INCLUDE':INCLUDE, diff --git a/projects/llm_framework/main_melotts/src/main.cpp b/projects/llm_framework/main_melotts/src/main.cpp index ba18fff..cd6ce1e 100644 --- a/projects/llm_framework/main_melotts/src/main.cpp +++ b/projects/llm_framework/main_melotts/src/main.cpp @@ -436,7 +436,7 @@ class llm_tts : public StackFlow { std::string tmp_msg2; if (enbase64) { ret = decode_base64((*next_data), tmp_msg2); - if (!ret) { + if (ret == -1) { return; } next_data = &tmp_msg2; diff --git a/projects/llm_framework/main_tts/SConstruct b/projects/llm_framework/main_tts/SConstruct index e7f3803..0ce3b6b 100644 --- a/projects/llm_framework/main_tts/SConstruct +++ b/projects/llm_framework/main_tts/SConstruct @@ -28,7 +28,7 @@ INCLUDE += [ADir('src/runner/eigen-3.4.0'), ADir('src/runner/src/tn/header'), AD STATIC_FILES += [AFile('single_speaker_english_fast.json'), AFile('single_speaker_fast.json') ] -REQUIREMENTS += ['simdjson_component'] + env['COMPONENTS'].append({'target':'llm_tts', 'SRCS':SRCS, 'INCLUDE':INCLUDE, diff --git a/projects/llm_framework/main_tts/src/main.cpp b/projects/llm_framework/main_tts/src/main.cpp index b87647d..cfc91b2 100644 --- a/projects/llm_framework/main_tts/src/main.cpp +++ b/projects/llm_framework/main_tts/src/main.cpp @@ -273,7 +273,7 @@ class llm_tts : public StackFlow { std::string tmp_msg2; if (enbase64) { ret = decode_base64((*next_data), tmp_msg2); - if (!ret) { + if (ret == -1) { return; } next_data = &tmp_msg2; diff --git a/projects/llm_framework/main_vlm/SConstruct b/projects/llm_framework/main_vlm/SConstruct index 7a5b874..b1f51c8 100644 --- a/projects/llm_framework/main_vlm/SConstruct +++ b/projects/llm_framework/main_vlm/SConstruct @@ -52,7 +52,7 @@ STATIC_LIB += static_file * 4 STATIC_FILES += [AFile('internvl2-1b-ax630c.json'), AFile('internvl2-1b-ax630c_tokenizer.py') ] -REQUIREMENTS += ['simdjson_component'] + env['COMPONENTS'].append({'target':'llm_vlm', 'SRCS':SRCS, 'INCLUDE':INCLUDE, diff --git a/projects/llm_framework/main_vlm/src/main.cpp b/projects/llm_framework/main_vlm/src/main.cpp index ea8ad22..4a1c1eb 100644 --- a/projects/llm_framework/main_vlm/src/main.cpp +++ b/projects/llm_framework/main_vlm/src/main.cpp @@ -327,7 +327,7 @@ class llm_llm : public StackFlow { std::string tmp_msg2; if (object.find("base64") != std::string::npos) { ret = decode_base64((*next_data), tmp_msg2); - if (!ret) { + if (ret == -1) { error_body["code"] = -23; error_body["message"] = "Base64 decoding error."; send("None", "None", error_body, unit_name_); diff --git a/projects/llm_framework/main_yolo/SConstruct b/projects/llm_framework/main_yolo/SConstruct index 06c1f55..372ee62 100644 --- a/projects/llm_framework/main_yolo/SConstruct +++ b/projects/llm_framework/main_yolo/SConstruct @@ -29,7 +29,7 @@ STATIC_LIB += static_file * 2 STATIC_FILES += [AFile('yolo11s.json'), AFile('yolo11s-seg.json'), AFile('yolo11s-pose.json')] -REQUIREMENTS += ['simdjson_component'] + env['COMPONENTS'].append({'target':'llm_yolo', 'SRCS':SRCS, 'INCLUDE':INCLUDE, diff --git a/projects/llm_framework/main_yolo/src/main.cpp b/projects/llm_framework/main_yolo/src/main.cpp index 35ddd4d..56738b2 100644 --- a/projects/llm_framework/main_yolo/src/main.cpp +++ b/projects/llm_framework/main_yolo/src/main.cpp @@ -321,7 +321,7 @@ class llm_yolo : public StackFlow { // must encode base64 std::string tmp_msg2; ret = decode_base64((*next_data), tmp_msg2); - if (!ret) { + if (ret == -1) { error_body["code"] = -23; error_body["message"] = "Base64 decoding error."; send("None", "None", error_body, unit_name_);