2023-05-01 13:10:05 +00:00
|
|
|
#include "chatllm.h"
|
2023-05-04 19:31:41 +00:00
|
|
|
#include "chat.h"
|
2024-03-13 22:23:57 +00:00
|
|
|
#include "chatapi.h"
|
2024-02-05 21:46:16 +00:00
|
|
|
#include "localdocs.h"
|
2023-06-22 19:44:49 +00:00
|
|
|
#include "modellist.h"
|
2023-05-01 13:10:05 +00:00
|
|
|
#include "network.h"
|
2023-06-27 15:54:34 +00:00
|
|
|
#include "mysettings.h"
|
2023-05-31 21:04:01 +00:00
|
|
|
#include "../gpt4all-backend/llmodel.h"
|
2023-05-01 13:10:05 +00:00
|
|
|
|
2024-04-25 17:16:52 +00:00
|
|
|
#include <QElapsedTimer>
|
|
|
|
|
2023-05-01 13:10:05 +00:00
|
|
|
//#define DEBUG
|
2023-05-13 23:05:35 +00:00
|
|
|
//#define DEBUG_MODEL_LOADING
|
2023-05-01 13:10:05 +00:00
|
|
|
|
2023-05-08 21:23:02 +00:00
|
|
|
#define GPTJ_INTERNAL_STATE_VERSION 0
|
|
|
|
#define LLAMA_INTERNAL_STATE_VERSION 0
|
|
|
|
|
2023-05-13 23:05:35 +00:00
|
|
|
class LLModelStore {
|
|
|
|
public:
|
|
|
|
static LLModelStore *globalInstance();
|
|
|
|
|
|
|
|
LLModelInfo acquireModel(); // will block until llmodel is ready
|
|
|
|
void releaseModel(const LLModelInfo &info); // must be called when you are done
|
|
|
|
|
|
|
|
private:
|
|
|
|
LLModelStore()
|
|
|
|
{
|
|
|
|
// seed with empty model
|
|
|
|
m_availableModels.append(LLModelInfo());
|
|
|
|
}
|
|
|
|
~LLModelStore() {}
|
|
|
|
QVector<LLModelInfo> m_availableModels;
|
|
|
|
QMutex m_mutex;
|
|
|
|
QWaitCondition m_condition;
|
|
|
|
friend class MyLLModelStore;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MyLLModelStore : public LLModelStore { };
|
|
|
|
Q_GLOBAL_STATIC(MyLLModelStore, storeInstance)
|
|
|
|
LLModelStore *LLModelStore::globalInstance()
|
|
|
|
{
|
|
|
|
return storeInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLModelInfo LLModelStore::acquireModel()
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
while (m_availableModels.isEmpty())
|
|
|
|
m_condition.wait(locker.mutex());
|
|
|
|
return m_availableModels.takeFirst();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLModelStore::releaseModel(const LLModelInfo &info)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
m_availableModels.append(info);
|
|
|
|
Q_ASSERT(m_availableModels.count() < 2);
|
|
|
|
m_condition.wakeAll();
|
|
|
|
}
|
|
|
|
|
2023-05-13 23:33:19 +00:00
|
|
|
ChatLLM::ChatLLM(Chat *parent, bool isServer)
|
2023-05-01 13:10:05 +00:00
|
|
|
: QObject{nullptr}
|
|
|
|
, m_promptResponseTokens(0)
|
2023-05-11 20:46:25 +00:00
|
|
|
, m_promptTokens(0)
|
2023-05-01 13:10:05 +00:00
|
|
|
, m_isRecalc(false)
|
2024-02-07 14:37:59 +00:00
|
|
|
, m_shouldBeLoaded(false)
|
|
|
|
, m_forceUnloadModel(false)
|
2024-03-06 17:59:34 +00:00
|
|
|
, m_markedForDeletion(false)
|
2024-02-07 14:37:59 +00:00
|
|
|
, m_shouldTrySwitchContext(false)
|
2023-06-19 22:26:04 +00:00
|
|
|
, m_stopGenerating(false)
|
2023-06-19 18:34:53 +00:00
|
|
|
, m_timer(nullptr)
|
2023-05-13 23:33:19 +00:00
|
|
|
, m_isServer(isServer)
|
2023-06-27 15:54:34 +00:00
|
|
|
, m_forceMetal(MySettings::globalInstance()->forceMetal())
|
|
|
|
, m_reloadingToChangeVariant(false)
|
2023-07-01 15:34:21 +00:00
|
|
|
, m_processedSystemPrompt(false)
|
2023-10-10 20:43:02 +00:00
|
|
|
, m_restoreStateFromText(false)
|
2023-05-01 13:10:05 +00:00
|
|
|
{
|
|
|
|
moveToThread(&m_llmThread);
|
2023-06-01 18:13:12 +00:00
|
|
|
connect(this, &ChatLLM::shouldBeLoadedChanged, this, &ChatLLM::handleShouldBeLoadedChanged,
|
|
|
|
Qt::QueuedConnection); // explicitly queued
|
2024-02-07 14:37:59 +00:00
|
|
|
connect(this, &ChatLLM::shouldTrySwitchContextChanged, this, &ChatLLM::handleShouldTrySwitchContextChanged,
|
|
|
|
Qt::QueuedConnection); // explicitly queued
|
2023-06-19 23:51:28 +00:00
|
|
|
connect(parent, &Chat::idChanged, this, &ChatLLM::handleChatIdChanged);
|
2023-06-19 18:34:53 +00:00
|
|
|
connect(&m_llmThread, &QThread::started, this, &ChatLLM::handleThreadStarted);
|
2023-06-27 15:54:34 +00:00
|
|
|
connect(MySettings::globalInstance(), &MySettings::forceMetalChanged, this, &ChatLLM::handleForceMetalChanged);
|
2023-09-13 14:32:08 +00:00
|
|
|
connect(MySettings::globalInstance(), &MySettings::deviceChanged, this, &ChatLLM::handleDeviceChanged);
|
2023-06-01 18:13:12 +00:00
|
|
|
|
|
|
|
// The following are blocking operations and will block the llm thread
|
|
|
|
connect(this, &ChatLLM::requestRetrieveFromDB, LocalDocs::globalInstance()->database(), &Database::retrieveFromDB,
|
|
|
|
Qt::BlockingQueuedConnection);
|
|
|
|
|
2023-06-19 23:51:28 +00:00
|
|
|
m_llmThread.setObjectName(parent->id());
|
2023-05-01 13:10:05 +00:00
|
|
|
m_llmThread.start();
|
|
|
|
}
|
|
|
|
|
2023-05-12 18:06:03 +00:00
|
|
|
ChatLLM::~ChatLLM()
|
|
|
|
{
|
2024-03-06 21:42:59 +00:00
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatLLM::destroy() {
|
2023-07-09 18:42:11 +00:00
|
|
|
m_stopGenerating = true;
|
2023-05-12 18:06:03 +00:00
|
|
|
m_llmThread.quit();
|
|
|
|
m_llmThread.wait();
|
2023-05-13 23:05:35 +00:00
|
|
|
|
|
|
|
// The only time we should have a model loaded here is on shutdown
|
|
|
|
// as we explicitly unload the model in all other circumstances
|
|
|
|
if (isModelLoaded()) {
|
2023-06-22 19:44:49 +00:00
|
|
|
delete m_llModelInfo.model;
|
|
|
|
m_llModelInfo.model = nullptr;
|
2023-05-13 23:05:35 +00:00
|
|
|
}
|
2023-05-12 18:06:03 +00:00
|
|
|
}
|
|
|
|
|
2023-06-19 18:34:53 +00:00
|
|
|
void ChatLLM::handleThreadStarted()
|
|
|
|
{
|
|
|
|
m_timer = new TokenTimer(this);
|
|
|
|
connect(m_timer, &TokenTimer::report, this, &ChatLLM::reportSpeed);
|
|
|
|
emit threadStarted();
|
|
|
|
}
|
|
|
|
|
2023-06-27 15:54:34 +00:00
|
|
|
void ChatLLM::handleForceMetalChanged(bool forceMetal)
|
|
|
|
{
|
|
|
|
#if defined(Q_OS_MAC) && defined(__arm__)
|
|
|
|
m_forceMetal = forceMetal;
|
|
|
|
if (isModelLoaded() && m_shouldBeLoaded) {
|
|
|
|
m_reloadingToChangeVariant = true;
|
|
|
|
unloadModel();
|
|
|
|
reloadModel();
|
|
|
|
m_reloadingToChangeVariant = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:32:08 +00:00
|
|
|
void ChatLLM::handleDeviceChanged()
|
|
|
|
{
|
|
|
|
if (isModelLoaded() && m_shouldBeLoaded) {
|
|
|
|
m_reloadingToChangeVariant = true;
|
|
|
|
unloadModel();
|
|
|
|
reloadModel();
|
|
|
|
m_reloadingToChangeVariant = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-04 19:31:41 +00:00
|
|
|
bool ChatLLM::loadDefaultModel()
|
2023-05-01 13:10:05 +00:00
|
|
|
{
|
2023-06-22 19:44:49 +00:00
|
|
|
ModelInfo defaultModel = ModelList::globalInstance()->defaultModelInfo();
|
2023-07-01 15:34:21 +00:00
|
|
|
if (defaultModel.filename().isEmpty()) {
|
2023-06-22 19:44:49 +00:00
|
|
|
emit modelLoadingError(QString("Could not find any model to load"));
|
2023-05-01 13:10:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2023-06-22 19:44:49 +00:00
|
|
|
return loadModel(defaultModel);
|
2023-05-01 13:10:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 14:37:59 +00:00
|
|
|
bool ChatLLM::trySwitchContextOfLoadedModel(const ModelInfo &modelInfo)
|
|
|
|
{
|
|
|
|
// We're trying to see if the store already has the model fully loaded that we wish to use
|
|
|
|
// and if so we just acquire it from the store and switch the context and return true. If the
|
|
|
|
// store doesn't have it or we're already loaded or in any other case just return false.
|
|
|
|
|
|
|
|
// If we're already loaded or a server or we're reloading to change the variant/device or the
|
|
|
|
// modelInfo is empty, then this should fail
|
|
|
|
if (isModelLoaded() || m_isServer || m_reloadingToChangeVariant || modelInfo.name().isEmpty()) {
|
|
|
|
m_shouldTrySwitchContext = false;
|
|
|
|
emit trySwitchContextOfLoadedModelCompleted(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString filePath = modelInfo.dirpath + modelInfo.filename();
|
|
|
|
QFileInfo fileInfo(filePath);
|
|
|
|
|
|
|
|
m_llModelInfo = LLModelStore::globalInstance()->acquireModel();
|
|
|
|
#if defined(DEBUG_MODEL_LOADING)
|
|
|
|
qDebug() << "acquired model from store" << m_llmThread.objectName() << m_llModelInfo.model;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// The store gave us no already loaded model, the wrong type of model, then give it back to the
|
|
|
|
// store and fail
|
|
|
|
if (!m_llModelInfo.model || m_llModelInfo.fileInfo != fileInfo) {
|
|
|
|
LLModelStore::globalInstance()->releaseModel(m_llModelInfo);
|
|
|
|
m_llModelInfo = LLModelInfo();
|
|
|
|
m_shouldTrySwitchContext = false;
|
|
|
|
emit trySwitchContextOfLoadedModelCompleted(false);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(DEBUG_MODEL_LOADING)
|
|
|
|
qDebug() << "store had our model" << m_llmThread.objectName() << m_llModelInfo.model;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// We should be loaded and now we are
|
|
|
|
m_shouldBeLoaded = true;
|
|
|
|
m_shouldTrySwitchContext = false;
|
|
|
|
|
|
|
|
// Restore, signal and process
|
|
|
|
restoreState();
|
|
|
|
emit modelLoadingPercentageChanged(1.0f);
|
|
|
|
emit trySwitchContextOfLoadedModelCompleted(true);
|
|
|
|
processSystemPrompt();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-22 19:44:49 +00:00
|
|
|
bool ChatLLM::loadModel(const ModelInfo &modelInfo)
|
2023-05-01 13:10:05 +00:00
|
|
|
{
|
2023-05-13 23:05:35 +00:00
|
|
|
// This is a complicated method because N different possible threads are interested in the outcome
|
|
|
|
// of this method. Why? Because we have a main/gui thread trying to monitor the state of N different
|
|
|
|
// possible chat threads all vying for a single resource - the currently loaded model - as the user
|
|
|
|
// switches back and forth between chats. It is important for our main/gui thread to never block
|
|
|
|
// but simultaneously always have up2date information with regards to which chat has the model loaded
|
|
|
|
// and what the type and name of that model is. I've tried to comment extensively in this method
|
|
|
|
// to provide an overview of what we're doing here.
|
|
|
|
|
|
|
|
// We're already loaded with this model
|
2023-06-22 19:44:49 +00:00
|
|
|
if (isModelLoaded() && this->modelInfo() == modelInfo)
|
2023-05-01 13:10:05 +00:00
|
|
|
return true;
|
|
|
|
|
2023-07-01 15:34:21 +00:00
|
|
|
QString filePath = modelInfo.dirpath + modelInfo.filename();
|
2023-05-13 23:05:35 +00:00
|
|
|
QFileInfo fileInfo(filePath);
|
|
|
|
|
|
|
|
// We have a live model, but it isn't the one we want
|
|
|
|
bool alreadyAcquired = isModelLoaded();
|
|
|
|
if (alreadyAcquired) {
|
2023-05-30 22:17:59 +00:00
|
|
|
resetContext();
|
2023-05-13 23:05:35 +00:00
|
|
|
#if defined(DEBUG_MODEL_LOADING)
|
2023-06-22 19:44:49 +00:00
|
|
|
qDebug() << "already acquired model deleted" << m_llmThread.objectName() << m_llModelInfo.model;
|
2023-05-13 23:05:35 +00:00
|
|
|
#endif
|
2023-06-22 19:44:49 +00:00
|
|
|
delete m_llModelInfo.model;
|
|
|
|
m_llModelInfo.model = nullptr;
|
2024-02-21 14:54:27 +00:00
|
|
|
emit modelLoadingPercentageChanged(std::numeric_limits<float>::min()); // small non-zero positive value
|
2023-05-13 23:33:19 +00:00
|
|
|
} else if (!m_isServer) {
|
2023-05-13 23:05:35 +00:00
|
|
|
// This is a blocking call that tries to retrieve the model we need from the model store.
|
|
|
|
// If it succeeds, then we just have to restore state. If the store has never had a model
|
|
|
|
// returned to it, then the modelInfo.model pointer should be null which will happen on startup
|
2023-06-22 19:44:49 +00:00
|
|
|
m_llModelInfo = LLModelStore::globalInstance()->acquireModel();
|
2023-05-13 23:05:35 +00:00
|
|
|
#if defined(DEBUG_MODEL_LOADING)
|
2023-06-27 15:54:34 +00:00
|
|
|
qDebug() << "acquired model from store" << m_llmThread.objectName() << m_llModelInfo.model;
|
2023-05-13 23:05:35 +00:00
|
|
|
#endif
|
|
|
|
// At this point it is possible that while we were blocked waiting to acquire the model from the
|
|
|
|
// store, that our state was changed to not be loaded. If this is the case, release the model
|
|
|
|
// back into the store and quit loading
|
|
|
|
if (!m_shouldBeLoaded) {
|
2023-05-13 23:33:19 +00:00
|
|
|
#if defined(DEBUG_MODEL_LOADING)
|
2023-06-22 19:44:49 +00:00
|
|
|
qDebug() << "no longer need model" << m_llmThread.objectName() << m_llModelInfo.model;
|
2023-05-13 23:33:19 +00:00
|
|
|
#endif
|
2023-06-22 19:44:49 +00:00
|
|
|
LLModelStore::globalInstance()->releaseModel(m_llModelInfo);
|
|
|
|
m_llModelInfo = LLModelInfo();
|
2024-02-07 14:37:59 +00:00
|
|
|
emit modelLoadingPercentageChanged(0.0f);
|
2023-05-13 23:05:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the store just gave us exactly the model we were looking for
|
2023-06-27 15:54:34 +00:00
|
|
|
if (m_llModelInfo.model && m_llModelInfo.fileInfo == fileInfo && !m_reloadingToChangeVariant) {
|
2023-05-13 23:05:35 +00:00
|
|
|
#if defined(DEBUG_MODEL_LOADING)
|
2023-06-22 19:44:49 +00:00
|
|
|
qDebug() << "store had our model" << m_llmThread.objectName() << m_llModelInfo.model;
|
2023-05-13 23:05:35 +00:00
|
|
|
#endif
|
|
|
|
restoreState();
|
2024-02-07 14:37:59 +00:00
|
|
|
emit modelLoadingPercentageChanged(1.0f);
|
2023-07-09 18:52:08 +00:00
|
|
|
setModelInfo(modelInfo);
|
2023-07-01 15:34:21 +00:00
|
|
|
Q_ASSERT(!m_modelInfo.filename().isEmpty());
|
|
|
|
if (m_modelInfo.filename().isEmpty())
|
|
|
|
emit modelLoadingError(QString("Modelinfo is left null for %1").arg(modelInfo.filename()));
|
|
|
|
else
|
|
|
|
processSystemPrompt();
|
2023-05-13 23:05:35 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Release the memory since we have to switch to a different model.
|
|
|
|
#if defined(DEBUG_MODEL_LOADING)
|
2023-06-22 19:44:49 +00:00
|
|
|
qDebug() << "deleting model" << m_llmThread.objectName() << m_llModelInfo.model;
|
2023-05-13 23:05:35 +00:00
|
|
|
#endif
|
2023-06-22 19:44:49 +00:00
|
|
|
delete m_llModelInfo.model;
|
|
|
|
m_llModelInfo.model = nullptr;
|
2023-05-13 23:05:35 +00:00
|
|
|
}
|
2023-05-01 13:10:05 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 23:05:35 +00:00
|
|
|
// Guarantee we've released the previous models memory
|
2023-06-22 19:44:49 +00:00
|
|
|
Q_ASSERT(!m_llModelInfo.model);
|
2023-05-13 23:05:35 +00:00
|
|
|
|
|
|
|
// Store the file info in the modelInfo in case we have an error loading
|
2023-06-22 19:44:49 +00:00
|
|
|
m_llModelInfo.fileInfo = fileInfo;
|
2023-05-01 13:10:05 +00:00
|
|
|
|
2023-05-13 23:05:35 +00:00
|
|
|
if (fileInfo.exists()) {
|
2024-04-25 17:16:52 +00:00
|
|
|
QVariantMap modelLoadProps;
|
2024-03-13 22:23:57 +00:00
|
|
|
if (modelInfo.isOnline) {
|
2023-05-15 00:12:15 +00:00
|
|
|
QString apiKey;
|
2024-03-13 22:23:57 +00:00
|
|
|
QString modelName;
|
2023-05-15 00:12:15 +00:00
|
|
|
{
|
|
|
|
QFile file(filePath);
|
2024-04-18 18:51:13 +00:00
|
|
|
bool success = file.open(QIODeviceBase::ReadOnly);
|
|
|
|
(void)success;
|
|
|
|
Q_ASSERT(success);
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
|
2024-03-13 22:23:57 +00:00
|
|
|
QJsonObject obj = doc.object();
|
|
|
|
apiKey = obj["apiKey"].toString();
|
|
|
|
modelName = obj["modelName"].toString();
|
2023-05-15 00:12:15 +00:00
|
|
|
}
|
2024-03-13 22:23:57 +00:00
|
|
|
m_llModelType = LLModelType::API_;
|
|
|
|
ChatAPI *model = new ChatAPI();
|
|
|
|
model->setModelName(modelName);
|
|
|
|
model->setRequestURL(modelInfo.url());
|
2023-05-15 00:12:15 +00:00
|
|
|
model->setAPIKey(apiKey);
|
2023-06-22 19:44:49 +00:00
|
|
|
m_llModelInfo.model = model;
|
2023-05-01 13:10:05 +00:00
|
|
|
} else {
|
2024-04-25 17:16:52 +00:00
|
|
|
QElapsedTimer modelLoadTimer;
|
|
|
|
modelLoadTimer.start();
|
|
|
|
|
2023-12-16 22:58:15 +00:00
|
|
|
auto n_ctx = MySettings::globalInstance()->modelContextLength(modelInfo);
|
|
|
|
m_ctx.n_ctx = n_ctx;
|
2024-01-31 19:17:44 +00:00
|
|
|
auto ngl = MySettings::globalInstance()->modelGpuLayers(modelInfo);
|
2023-12-16 22:58:15 +00:00
|
|
|
|
|
|
|
std::string buildVariant = "auto";
|
2023-06-27 15:54:34 +00:00
|
|
|
#if defined(Q_OS_MAC) && defined(__arm__)
|
|
|
|
if (m_forceMetal)
|
2023-12-16 22:58:15 +00:00
|
|
|
buildVariant = "metal";
|
2023-06-27 15:54:34 +00:00
|
|
|
#endif
|
2024-04-25 17:16:52 +00:00
|
|
|
QString constructError;
|
|
|
|
m_llModelInfo.model = nullptr;
|
|
|
|
try {
|
|
|
|
m_llModelInfo.model = LLModel::Implementation::construct(filePath.toStdString(), buildVariant, n_ctx);
|
|
|
|
} catch (const LLModel::MissingImplementationError &e) {
|
|
|
|
modelLoadProps.insert("error", "missing_model_impl");
|
|
|
|
constructError = e.what();
|
|
|
|
} catch (const LLModel::UnsupportedModelError &e) {
|
|
|
|
modelLoadProps.insert("error", "unsupported_model_file");
|
|
|
|
constructError = e.what();
|
|
|
|
} catch (const LLModel::BadArchError &e) {
|
|
|
|
constructError = e.what();
|
|
|
|
modelLoadProps.insert("error", "unsupported_model_arch");
|
|
|
|
modelLoadProps.insert("model_arch", QString::fromStdString(e.arch()));
|
|
|
|
}
|
2023-06-27 15:54:34 +00:00
|
|
|
|
2023-06-22 19:44:49 +00:00
|
|
|
if (m_llModelInfo.model) {
|
2024-02-21 20:45:32 +00:00
|
|
|
if (m_llModelInfo.model->isModelBlacklisted(filePath.toStdString())) {
|
2024-03-06 22:14:54 +00:00
|
|
|
static QSet<QString> warned;
|
|
|
|
auto fname = modelInfo.filename();
|
|
|
|
if (!warned.contains(fname)) {
|
|
|
|
emit modelLoadingWarning(QString(
|
|
|
|
"%1 is known to be broken. Please get a replacement via the download dialog."
|
|
|
|
).arg(fname));
|
|
|
|
warned.insert(fname); // don't warn again until restart
|
|
|
|
}
|
2024-02-21 20:45:32 +00:00
|
|
|
}
|
2024-02-07 14:37:59 +00:00
|
|
|
|
|
|
|
m_llModelInfo.model->setProgressCallback([this](float progress) -> bool {
|
|
|
|
emit modelLoadingPercentageChanged(progress);
|
|
|
|
return m_shouldBeLoaded;
|
|
|
|
});
|
|
|
|
|
2023-09-13 14:32:08 +00:00
|
|
|
// Pick the best match for the device
|
2023-09-14 12:25:37 +00:00
|
|
|
QString actualDevice = m_llModelInfo.model->implementation().buildVariant() == "metal" ? "Metal" : "CPU";
|
2023-09-13 14:32:08 +00:00
|
|
|
const QString requestedDevice = MySettings::globalInstance()->device();
|
2023-10-02 14:23:11 +00:00
|
|
|
if (requestedDevice == "CPU") {
|
|
|
|
emit reportFallbackReason(""); // fallback not applicable
|
|
|
|
} else {
|
2024-01-31 19:17:44 +00:00
|
|
|
const size_t requiredMemory = m_llModelInfo.model->requiredMem(filePath.toStdString(), n_ctx, ngl);
|
2023-09-13 14:32:08 +00:00
|
|
|
std::vector<LLModel::GPUDevice> availableDevices = m_llModelInfo.model->availableGPUDevices(requiredMemory);
|
2023-09-29 18:25:37 +00:00
|
|
|
LLModel::GPUDevice *device = nullptr;
|
|
|
|
|
2023-09-13 23:30:27 +00:00
|
|
|
if (!availableDevices.empty() && requestedDevice == "Auto" && availableDevices.front().type == 2 /*a discrete gpu*/) {
|
2023-09-29 18:25:37 +00:00
|
|
|
device = &availableDevices.front();
|
2023-09-13 14:32:08 +00:00
|
|
|
} else {
|
|
|
|
for (LLModel::GPUDevice &d : availableDevices) {
|
2023-09-13 19:32:42 +00:00
|
|
|
if (QString::fromStdString(d.name) == requestedDevice) {
|
2023-09-29 18:25:37 +00:00
|
|
|
device = &d;
|
2023-09-13 19:32:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-09-13 14:32:08 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-29 18:25:37 +00:00
|
|
|
|
2023-10-04 19:51:46 +00:00
|
|
|
emit reportFallbackReason(""); // no fallback yet
|
|
|
|
std::string unavail_reason;
|
|
|
|
if (!device) {
|
|
|
|
// GPU not available
|
2024-01-31 19:17:44 +00:00
|
|
|
} else if (!m_llModelInfo.model->initializeGPUDevice(device->index, &unavail_reason)) {
|
2023-10-06 15:30:55 +00:00
|
|
|
emit reportFallbackReason(QString::fromStdString("<br>" + unavail_reason));
|
2023-09-29 18:25:37 +00:00
|
|
|
} else {
|
|
|
|
actualDevice = QString::fromStdString(device->name);
|
|
|
|
}
|
2023-09-13 14:32:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 12:25:37 +00:00
|
|
|
// Report which device we're actually using
|
|
|
|
emit reportDevice(actualDevice);
|
|
|
|
|
2024-01-31 19:17:44 +00:00
|
|
|
bool success = m_llModelInfo.model->loadModel(filePath.toStdString(), n_ctx, ngl);
|
2023-09-29 18:25:37 +00:00
|
|
|
if (actualDevice == "CPU") {
|
|
|
|
// we asked llama.cpp to use the CPU
|
|
|
|
} else if (!success) {
|
|
|
|
// llama_init_from_file returned nullptr
|
2023-09-14 20:52:31 +00:00
|
|
|
emit reportDevice("CPU");
|
2023-10-06 15:30:55 +00:00
|
|
|
emit reportFallbackReason("<br>GPU loading failed (out of VRAM?)");
|
2024-04-25 17:16:52 +00:00
|
|
|
modelLoadProps.insert("cpu_fallback_reason", "gpu_load_failed");
|
2024-01-31 19:17:44 +00:00
|
|
|
success = m_llModelInfo.model->loadModel(filePath.toStdString(), n_ctx, 0);
|
2023-09-29 18:25:37 +00:00
|
|
|
} else if (!m_llModelInfo.model->usingGPUDevice()) {
|
|
|
|
// ggml_vk_init was not called in llama.cpp
|
|
|
|
// We might have had to fallback to CPU after load if the model is not possible to accelerate
|
|
|
|
// for instance if the quantization method is not supported on Vulkan yet
|
|
|
|
emit reportDevice("CPU");
|
2023-10-06 15:30:55 +00:00
|
|
|
emit reportFallbackReason("<br>model or quant has no GPU support");
|
2024-04-25 17:16:52 +00:00
|
|
|
modelLoadProps.insert("cpu_fallback_reason", "gpu_unsupported_model");
|
2023-09-14 20:52:31 +00:00
|
|
|
}
|
|
|
|
|
2023-06-19 23:51:28 +00:00
|
|
|
if (!success) {
|
2023-09-14 20:52:31 +00:00
|
|
|
delete m_llModelInfo.model;
|
|
|
|
m_llModelInfo.model = nullptr;
|
2023-06-19 23:51:28 +00:00
|
|
|
if (!m_isServer)
|
2023-06-22 19:44:49 +00:00
|
|
|
LLModelStore::globalInstance()->releaseModel(m_llModelInfo); // release back into the store
|
|
|
|
m_llModelInfo = LLModelInfo();
|
2023-07-01 15:34:21 +00:00
|
|
|
emit modelLoadingError(QString("Could not load model due to invalid model file for %1").arg(modelInfo.filename()));
|
2024-04-25 17:16:52 +00:00
|
|
|
modelLoadProps.insert("error", "loadmodel_failed");
|
2023-06-19 23:51:28 +00:00
|
|
|
} else {
|
2023-07-08 14:04:38 +00:00
|
|
|
switch (m_llModelInfo.model->implementation().modelType()[0]) {
|
2023-06-22 19:44:49 +00:00
|
|
|
case 'L': m_llModelType = LLModelType::LLAMA_; break;
|
|
|
|
case 'G': m_llModelType = LLModelType::GPTJ_; break;
|
2023-06-19 23:51:28 +00:00
|
|
|
default:
|
|
|
|
{
|
2023-09-14 20:52:31 +00:00
|
|
|
delete m_llModelInfo.model;
|
|
|
|
m_llModelInfo.model = nullptr;
|
2023-06-19 23:51:28 +00:00
|
|
|
if (!m_isServer)
|
2023-06-22 19:44:49 +00:00
|
|
|
LLModelStore::globalInstance()->releaseModel(m_llModelInfo); // release back into the store
|
|
|
|
m_llModelInfo = LLModelInfo();
|
2023-07-01 15:34:21 +00:00
|
|
|
emit modelLoadingError(QString("Could not determine model type for %1").arg(modelInfo.filename()));
|
2023-06-19 23:51:28 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-25 17:16:52 +00:00
|
|
|
|
|
|
|
modelLoadProps.insert("$duration", modelLoadTimer.elapsed() / 1000.);
|
2023-05-31 21:04:01 +00:00
|
|
|
}
|
2023-06-04 18:55:05 +00:00
|
|
|
} else {
|
|
|
|
if (!m_isServer)
|
2023-06-22 19:44:49 +00:00
|
|
|
LLModelStore::globalInstance()->releaseModel(m_llModelInfo); // release back into the store
|
|
|
|
m_llModelInfo = LLModelInfo();
|
2024-04-25 17:16:52 +00:00
|
|
|
emit modelLoadingError(QString("Error loading %1: %2").arg(modelInfo.filename()).arg(constructError));
|
2023-05-15 00:12:15 +00:00
|
|
|
}
|
2023-05-01 13:10:05 +00:00
|
|
|
}
|
2023-05-13 23:05:35 +00:00
|
|
|
#if defined(DEBUG_MODEL_LOADING)
|
2023-06-22 19:44:49 +00:00
|
|
|
qDebug() << "new model" << m_llmThread.objectName() << m_llModelInfo.model;
|
2023-05-13 23:05:35 +00:00
|
|
|
#endif
|
2023-05-07 15:24:07 +00:00
|
|
|
restoreState();
|
|
|
|
#if defined(DEBUG)
|
2023-06-19 23:51:28 +00:00
|
|
|
qDebug() << "modelLoadedChanged" << m_llmThread.objectName();
|
2023-05-13 23:05:35 +00:00
|
|
|
fflush(stdout);
|
2023-05-07 15:24:07 +00:00
|
|
|
#endif
|
2024-02-07 14:37:59 +00:00
|
|
|
emit modelLoadingPercentageChanged(isModelLoaded() ? 1.0f : 0.0f);
|
2023-05-01 13:10:05 +00:00
|
|
|
|
2024-04-25 17:16:52 +00:00
|
|
|
modelLoadProps.insert("requestedDevice", MySettings::globalInstance()->device());
|
|
|
|
modelLoadProps.insert("model", modelInfo.filename());
|
|
|
|
Network::globalInstance()->trackChatEvent("model_load", modelLoadProps);
|
2023-05-04 19:31:41 +00:00
|
|
|
} else {
|
2023-05-13 23:33:19 +00:00
|
|
|
if (!m_isServer)
|
2023-06-22 19:44:49 +00:00
|
|
|
LLModelStore::globalInstance()->releaseModel(m_llModelInfo); // release back into the store
|
|
|
|
m_llModelInfo = LLModelInfo();
|
2023-07-01 15:34:21 +00:00
|
|
|
emit modelLoadingError(QString("Could not find file for model %1").arg(modelInfo.filename()));
|
2023-05-01 13:10:05 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 16:41:48 +00:00
|
|
|
if (m_llModelInfo.model) {
|
2023-06-22 19:44:49 +00:00
|
|
|
setModelInfo(modelInfo);
|
2023-09-21 16:41:48 +00:00
|
|
|
processSystemPrompt();
|
|
|
|
}
|
2023-06-22 19:44:49 +00:00
|
|
|
return m_llModelInfo.model;
|
2023-05-01 13:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatLLM::isModelLoaded() const
|
|
|
|
{
|
2023-06-22 19:44:49 +00:00
|
|
|
return m_llModelInfo.model && m_llModelInfo.model->isModelLoaded();
|
2023-05-01 13:10:05 +00:00
|
|
|
}
|
|
|
|
|
2023-10-28 20:53:42 +00:00
|
|
|
std::string remove_leading_whitespace(const std::string& input) {
|
|
|
|
auto first_non_whitespace = std::find_if(input.begin(), input.end(), [](unsigned char c) {
|
|
|
|
return !std::isspace(c);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (first_non_whitespace == input.end())
|
|
|
|
return std::string();
|
|
|
|
|
|
|
|
return std::string(first_non_whitespace, input.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string trim_whitespace(const std::string& input) {
|
|
|
|
auto first_non_whitespace = std::find_if(input.begin(), input.end(), [](unsigned char c) {
|
|
|
|
return !std::isspace(c);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (first_non_whitespace == input.end())
|
|
|
|
return std::string();
|
|
|
|
|
|
|
|
auto last_non_whitespace = std::find_if(input.rbegin(), input.rend(), [](unsigned char c) {
|
|
|
|
return !std::isspace(c);
|
|
|
|
}).base();
|
|
|
|
|
|
|
|
return std::string(first_non_whitespace, last_non_whitespace);
|
|
|
|
}
|
|
|
|
|
2023-05-01 13:10:05 +00:00
|
|
|
void ChatLLM::regenerateResponse()
|
|
|
|
{
|
2023-05-15 18:08:08 +00:00
|
|
|
// ChatGPT uses a different semantic meaning for n_past than local models. For ChatGPT, the meaning
|
|
|
|
// of n_past is of the number of prompt/response pairs, rather than for total tokens.
|
2024-03-13 22:23:57 +00:00
|
|
|
if (m_llModelType == LLModelType::API_)
|
2023-05-15 18:08:08 +00:00
|
|
|
m_ctx.n_past -= 1;
|
|
|
|
else
|
|
|
|
m_ctx.n_past -= m_promptResponseTokens;
|
2023-05-01 13:10:05 +00:00
|
|
|
m_ctx.n_past = std::max(0, m_ctx.n_past);
|
2023-10-03 16:42:31 +00:00
|
|
|
m_ctx.tokens.erase(m_ctx.tokens.end() - m_promptResponseTokens, m_ctx.tokens.end());
|
2023-05-01 13:10:05 +00:00
|
|
|
m_promptResponseTokens = 0;
|
2023-05-11 20:46:25 +00:00
|
|
|
m_promptTokens = 0;
|
2023-05-01 13:10:05 +00:00
|
|
|
m_response = std::string();
|
2023-06-20 20:14:30 +00:00
|
|
|
emit responseChanged(QString::fromStdString(m_response));
|
2023-05-01 13:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatLLM::resetResponse()
|
|
|
|
{
|
2023-05-11 20:46:25 +00:00
|
|
|
m_promptTokens = 0;
|
2023-05-01 13:10:05 +00:00
|
|
|
m_promptResponseTokens = 0;
|
|
|
|
m_response = std::string();
|
2023-06-20 20:14:30 +00:00
|
|
|
emit responseChanged(QString::fromStdString(m_response));
|
2023-05-01 13:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatLLM::resetContext()
|
|
|
|
{
|
2024-02-27 18:46:47 +00:00
|
|
|
resetResponse();
|
2023-07-01 15:34:21 +00:00
|
|
|
m_processedSystemPrompt = false;
|
2023-05-01 13:10:05 +00:00
|
|
|
m_ctx = LLModel::PromptContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ChatLLM::response() const
|
|
|
|
{
|
|
|
|
return QString::fromStdString(remove_leading_whitespace(m_response));
|
|
|
|
}
|
|
|
|
|
2023-06-22 19:44:49 +00:00
|
|
|
ModelInfo ChatLLM::modelInfo() const
|
2023-05-01 13:10:05 +00:00
|
|
|
{
|
2023-06-22 19:44:49 +00:00
|
|
|
return m_modelInfo;
|
2023-05-01 13:10:05 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 19:44:49 +00:00
|
|
|
void ChatLLM::setModelInfo(const ModelInfo &modelInfo)
|
2023-05-01 13:10:05 +00:00
|
|
|
{
|
2023-06-22 19:44:49 +00:00
|
|
|
m_modelInfo = modelInfo;
|
|
|
|
emit modelInfoChanged(modelInfo);
|
2023-05-01 13:10:05 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 19:44:49 +00:00
|
|
|
void ChatLLM::modelChangeRequested(const ModelInfo &modelInfo)
|
2023-05-01 13:10:05 +00:00
|
|
|
{
|
2024-02-07 14:37:59 +00:00
|
|
|
m_shouldBeLoaded = true;
|
2023-06-22 19:44:49 +00:00
|
|
|
loadModel(modelInfo);
|
2023-05-01 13:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatLLM::handlePrompt(int32_t token)
|
|
|
|
{
|
2023-06-19 22:23:05 +00:00
|
|
|
// m_promptResponseTokens is related to last prompt/response not
|
2023-05-01 13:10:05 +00:00
|
|
|
// the entire context window which we can reset on regenerate prompt
|
2023-05-07 15:24:07 +00:00
|
|
|
#if defined(DEBUG)
|
2023-06-19 23:51:28 +00:00
|
|
|
qDebug() << "prompt process" << m_llmThread.objectName() << token;
|
2023-05-07 15:24:07 +00:00
|
|
|
#endif
|
2023-05-11 20:46:25 +00:00
|
|
|
++m_promptTokens;
|
2023-05-01 13:10:05 +00:00
|
|
|
++m_promptResponseTokens;
|
2023-08-30 13:43:56 +00:00
|
|
|
m_timer->start();
|
2023-05-01 13:10:05 +00:00
|
|
|
return !m_stopGenerating;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatLLM::handleResponse(int32_t token, const std::string &response)
|
|
|
|
{
|
|
|
|
#if defined(DEBUG)
|
|
|
|
printf("%s", response.c_str());
|
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// check for error
|
|
|
|
if (token < 0) {
|
|
|
|
m_response.append(response);
|
2023-10-28 20:53:42 +00:00
|
|
|
emit responseChanged(QString::fromStdString(remove_leading_whitespace(m_response)));
|
2023-05-01 13:10:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-19 22:23:05 +00:00
|
|
|
// m_promptResponseTokens is related to last prompt/response not
|
2023-05-01 13:10:05 +00:00
|
|
|
// the entire context window which we can reset on regenerate prompt
|
|
|
|
++m_promptResponseTokens;
|
2023-06-19 18:34:53 +00:00
|
|
|
m_timer->inc();
|
2023-05-01 13:10:05 +00:00
|
|
|
Q_ASSERT(!response.empty());
|
|
|
|
m_response.append(response);
|
2023-10-28 20:53:42 +00:00
|
|
|
emit responseChanged(QString::fromStdString(remove_leading_whitespace(m_response)));
|
2023-05-01 13:10:05 +00:00
|
|
|
return !m_stopGenerating;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatLLM::handleRecalculate(bool isRecalc)
|
|
|
|
{
|
2023-07-11 16:37:21 +00:00
|
|
|
#if defined(DEBUG)
|
|
|
|
qDebug() << "recalculate" << m_llmThread.objectName() << isRecalc;
|
|
|
|
#endif
|
2023-05-01 13:10:05 +00:00
|
|
|
if (m_isRecalc != isRecalc) {
|
|
|
|
m_isRecalc = isRecalc;
|
|
|
|
emit recalcChanged();
|
|
|
|
}
|
|
|
|
return !m_stopGenerating;
|
|
|
|
}
|
2023-07-01 15:34:21 +00:00
|
|
|
bool ChatLLM::prompt(const QList<QString> &collectionList, const QString &prompt)
|
|
|
|
{
|
2023-10-28 20:39:25 +00:00
|
|
|
if (m_restoreStateFromText) {
|
|
|
|
Q_ASSERT(m_state.isEmpty());
|
|
|
|
processRestoreStateFromText();
|
|
|
|
}
|
|
|
|
|
2023-07-01 15:34:21 +00:00
|
|
|
if (!m_processedSystemPrompt)
|
|
|
|
processSystemPrompt();
|
|
|
|
const QString promptTemplate = MySettings::globalInstance()->modelPromptTemplate(m_modelInfo);
|
|
|
|
const int32_t n_predict = MySettings::globalInstance()->modelMaxLength(m_modelInfo);
|
|
|
|
const int32_t top_k = MySettings::globalInstance()->modelTopK(m_modelInfo);
|
|
|
|
const float top_p = MySettings::globalInstance()->modelTopP(m_modelInfo);
|
2024-02-24 22:51:34 +00:00
|
|
|
const float min_p = MySettings::globalInstance()->modelMinP(m_modelInfo);
|
2023-07-01 15:34:21 +00:00
|
|
|
const float temp = MySettings::globalInstance()->modelTemperature(m_modelInfo);
|
|
|
|
const int32_t n_batch = MySettings::globalInstance()->modelPromptBatchSize(m_modelInfo);
|
|
|
|
const float repeat_penalty = MySettings::globalInstance()->modelRepeatPenalty(m_modelInfo);
|
|
|
|
const int32_t repeat_penalty_tokens = MySettings::globalInstance()->modelRepeatPenaltyTokens(m_modelInfo);
|
2024-02-24 22:51:34 +00:00
|
|
|
return promptInternal(collectionList, prompt, promptTemplate, n_predict, top_k, top_p, min_p, temp, n_batch,
|
2023-07-01 15:34:21 +00:00
|
|
|
repeat_penalty, repeat_penalty_tokens);
|
|
|
|
}
|
2023-05-01 13:10:05 +00:00
|
|
|
|
2023-07-01 15:34:21 +00:00
|
|
|
bool ChatLLM::promptInternal(const QList<QString> &collectionList, const QString &prompt, const QString &promptTemplate,
|
2024-02-24 22:51:34 +00:00
|
|
|
int32_t n_predict, int32_t top_k, float top_p, float min_p, float temp, int32_t n_batch, float repeat_penalty,
|
2023-07-01 15:34:21 +00:00
|
|
|
int32_t repeat_penalty_tokens)
|
2023-05-01 13:10:05 +00:00
|
|
|
{
|
|
|
|
if (!isModelLoaded())
|
|
|
|
return false;
|
|
|
|
|
2023-06-19 22:23:54 +00:00
|
|
|
QList<ResultInfo> databaseResults;
|
2023-06-29 00:42:40 +00:00
|
|
|
const int retrievalSize = MySettings::globalInstance()->localDocsRetrievalSize();
|
2023-12-04 17:58:40 +00:00
|
|
|
if (!collectionList.isEmpty()) {
|
|
|
|
emit requestRetrieveFromDB(collectionList, prompt, retrievalSize, &databaseResults); // blocks
|
|
|
|
emit databaseResultsChanged(databaseResults);
|
|
|
|
}
|
2023-06-01 18:13:12 +00:00
|
|
|
|
|
|
|
// Augment the prompt template with the results if any
|
2024-02-21 20:45:32 +00:00
|
|
|
QList<QString> docsContext;
|
2023-06-19 22:23:54 +00:00
|
|
|
if (!databaseResults.isEmpty())
|
2024-02-21 20:45:32 +00:00
|
|
|
docsContext.append("### Context:");
|
2023-06-19 22:23:54 +00:00
|
|
|
for (const ResultInfo &info : databaseResults)
|
2024-02-21 20:45:32 +00:00
|
|
|
docsContext.append(info.text);
|
2023-05-01 13:10:05 +00:00
|
|
|
|
2023-07-01 15:34:21 +00:00
|
|
|
int n_threads = MySettings::globalInstance()->threadCount();
|
|
|
|
|
2023-05-01 13:10:05 +00:00
|
|
|
m_stopGenerating = false;
|
|
|
|
auto promptFunc = std::bind(&ChatLLM::handlePrompt, this, std::placeholders::_1);
|
|
|
|
auto responseFunc = std::bind(&ChatLLM::handleResponse, this, std::placeholders::_1,
|
|
|
|
std::placeholders::_2);
|
|
|
|
auto recalcFunc = std::bind(&ChatLLM::handleRecalculate, this, std::placeholders::_1);
|
2023-05-21 00:04:36 +00:00
|
|
|
emit promptProcessing();
|
2023-05-01 13:10:05 +00:00
|
|
|
m_ctx.n_predict = n_predict;
|
|
|
|
m_ctx.top_k = top_k;
|
|
|
|
m_ctx.top_p = top_p;
|
2024-02-24 22:51:34 +00:00
|
|
|
m_ctx.min_p = min_p;
|
2023-05-01 13:10:05 +00:00
|
|
|
m_ctx.temp = temp;
|
|
|
|
m_ctx.n_batch = n_batch;
|
|
|
|
m_ctx.repeat_penalty = repeat_penalty;
|
|
|
|
m_ctx.repeat_last_n = repeat_penalty_tokens;
|
2023-06-22 19:44:49 +00:00
|
|
|
m_llModelInfo.model->setThreadCount(n_threads);
|
2023-05-21 00:03:59 +00:00
|
|
|
#if defined(DEBUG)
|
2024-02-21 20:45:32 +00:00
|
|
|
printf("%s", qPrintable(prompt));
|
2023-05-01 13:10:05 +00:00
|
|
|
fflush(stdout);
|
2023-05-21 00:03:59 +00:00
|
|
|
#endif
|
2024-04-25 17:16:52 +00:00
|
|
|
QElapsedTimer totalTime;
|
|
|
|
totalTime.start();
|
2023-06-19 18:34:53 +00:00
|
|
|
m_timer->start();
|
2024-02-21 20:45:32 +00:00
|
|
|
if (!docsContext.isEmpty()) {
|
|
|
|
auto old_n_predict = std::exchange(m_ctx.n_predict, 0); // decode localdocs context without a response
|
|
|
|
m_llModelInfo.model->prompt(docsContext.join("\n").toStdString(), "%1", promptFunc, responseFunc, recalcFunc, m_ctx);
|
|
|
|
m_ctx.n_predict = old_n_predict; // now we are ready for a response
|
|
|
|
}
|
|
|
|
m_llModelInfo.model->prompt(prompt.toStdString(), promptTemplate.toStdString(), promptFunc, responseFunc, recalcFunc, m_ctx);
|
2023-05-21 00:03:59 +00:00
|
|
|
#if defined(DEBUG)
|
2023-05-01 13:10:05 +00:00
|
|
|
printf("\n");
|
|
|
|
fflush(stdout);
|
2023-05-21 00:03:59 +00:00
|
|
|
#endif
|
2023-06-19 18:34:53 +00:00
|
|
|
m_timer->stop();
|
2024-04-25 17:16:52 +00:00
|
|
|
qint64 elapsed = totalTime.elapsed();
|
2023-05-01 13:10:05 +00:00
|
|
|
std::string trimmed = trim_whitespace(m_response);
|
|
|
|
if (trimmed != m_response) {
|
|
|
|
m_response = trimmed;
|
2023-06-20 20:14:30 +00:00
|
|
|
emit responseChanged(QString::fromStdString(m_response));
|
2023-05-01 13:10:05 +00:00
|
|
|
}
|
2024-04-25 17:16:52 +00:00
|
|
|
emit responseStopped(elapsed);
|
2023-05-01 13:10:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
2023-05-02 00:27:07 +00:00
|
|
|
|
2023-05-13 23:05:35 +00:00
|
|
|
void ChatLLM::setShouldBeLoaded(bool b)
|
2023-05-02 00:27:07 +00:00
|
|
|
{
|
2023-05-13 23:05:35 +00:00
|
|
|
#if defined(DEBUG_MODEL_LOADING)
|
2023-06-22 19:44:49 +00:00
|
|
|
qDebug() << "setShouldBeLoaded" << m_llmThread.objectName() << b << m_llModelInfo.model;
|
2023-05-07 15:24:07 +00:00
|
|
|
#endif
|
2023-05-13 23:05:35 +00:00
|
|
|
m_shouldBeLoaded = b; // atomic
|
|
|
|
emit shouldBeLoadedChanged();
|
|
|
|
}
|
|
|
|
|
2024-02-07 14:37:59 +00:00
|
|
|
void ChatLLM::setShouldTrySwitchContext(bool b)
|
|
|
|
{
|
|
|
|
m_shouldTrySwitchContext = b; // atomic
|
|
|
|
emit shouldTrySwitchContextChanged();
|
|
|
|
}
|
|
|
|
|
2023-05-13 23:05:35 +00:00
|
|
|
void ChatLLM::handleShouldBeLoadedChanged()
|
|
|
|
{
|
|
|
|
if (m_shouldBeLoaded)
|
|
|
|
reloadModel();
|
|
|
|
else
|
|
|
|
unloadModel();
|
|
|
|
}
|
|
|
|
|
2024-02-07 14:37:59 +00:00
|
|
|
void ChatLLM::handleShouldTrySwitchContextChanged()
|
2023-05-13 23:05:35 +00:00
|
|
|
{
|
2024-02-07 14:37:59 +00:00
|
|
|
if (m_shouldTrySwitchContext)
|
|
|
|
trySwitchContextOfLoadedModel(modelInfo());
|
2023-05-13 23:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatLLM::unloadModel()
|
|
|
|
{
|
2023-05-15 22:48:24 +00:00
|
|
|
if (!isModelLoaded() || m_isServer)
|
2023-05-13 23:05:35 +00:00
|
|
|
return;
|
|
|
|
|
2024-02-21 21:05:49 +00:00
|
|
|
if (!m_forceUnloadModel || !m_shouldBeLoaded)
|
|
|
|
emit modelLoadingPercentageChanged(0.0f);
|
|
|
|
else
|
|
|
|
emit modelLoadingPercentageChanged(std::numeric_limits<float>::min()); // small non-zero positive value
|
|
|
|
|
2024-03-06 17:59:34 +00:00
|
|
|
if (!m_markedForDeletion)
|
|
|
|
saveState();
|
|
|
|
|
2023-05-13 23:05:35 +00:00
|
|
|
#if defined(DEBUG_MODEL_LOADING)
|
2023-06-22 19:44:49 +00:00
|
|
|
qDebug() << "unloadModel" << m_llmThread.objectName() << m_llModelInfo.model;
|
2023-05-13 23:05:35 +00:00
|
|
|
#endif
|
2024-02-07 14:37:59 +00:00
|
|
|
|
|
|
|
if (m_forceUnloadModel) {
|
|
|
|
delete m_llModelInfo.model;
|
|
|
|
m_llModelInfo.model = nullptr;
|
|
|
|
m_forceUnloadModel = false;
|
|
|
|
}
|
|
|
|
|
2023-06-22 19:44:49 +00:00
|
|
|
LLModelStore::globalInstance()->releaseModel(m_llModelInfo);
|
|
|
|
m_llModelInfo = LLModelInfo();
|
2023-05-02 00:27:07 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 23:05:35 +00:00
|
|
|
void ChatLLM::reloadModel()
|
2023-05-02 00:27:07 +00:00
|
|
|
{
|
2024-02-07 14:37:59 +00:00
|
|
|
if (isModelLoaded() && m_forceUnloadModel)
|
|
|
|
unloadModel(); // we unload first if we are forcing an unload
|
|
|
|
|
2023-05-15 22:48:24 +00:00
|
|
|
if (isModelLoaded() || m_isServer)
|
2023-05-13 23:05:35 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
#if defined(DEBUG_MODEL_LOADING)
|
2023-06-22 19:44:49 +00:00
|
|
|
qDebug() << "reloadModel" << m_llmThread.objectName() << m_llModelInfo.model;
|
2023-05-07 15:24:07 +00:00
|
|
|
#endif
|
2023-06-22 19:44:49 +00:00
|
|
|
const ModelInfo m = modelInfo();
|
2023-07-01 15:34:21 +00:00
|
|
|
if (m.name().isEmpty())
|
2023-05-04 19:31:41 +00:00
|
|
|
loadDefaultModel();
|
2023-06-20 20:14:30 +00:00
|
|
|
else
|
|
|
|
loadModel(m);
|
2023-05-02 00:27:07 +00:00
|
|
|
}
|
2023-05-02 15:19:17 +00:00
|
|
|
|
|
|
|
void ChatLLM::generateName()
|
|
|
|
{
|
|
|
|
Q_ASSERT(isModelLoaded());
|
|
|
|
if (!isModelLoaded())
|
|
|
|
return;
|
|
|
|
|
2024-04-15 15:39:48 +00:00
|
|
|
std::string instructPrompt("### Instruction:\n%1\n### Response:\n"); // standard Alpaca
|
2023-05-02 15:19:17 +00:00
|
|
|
auto promptFunc = std::bind(&ChatLLM::handleNamePrompt, this, std::placeholders::_1);
|
2024-04-15 15:39:48 +00:00
|
|
|
auto responseFunc = std::bind(&ChatLLM::handleNameResponse, this, std::placeholders::_1, std::placeholders::_2);
|
2023-05-02 15:19:17 +00:00
|
|
|
auto recalcFunc = std::bind(&ChatLLM::handleNameRecalculate, this, std::placeholders::_1);
|
|
|
|
LLModel::PromptContext ctx = m_ctx;
|
2024-04-15 15:39:48 +00:00
|
|
|
m_llModelInfo.model->prompt("Describe response above in three words.", instructPrompt, promptFunc, responseFunc,
|
|
|
|
recalcFunc, ctx);
|
2023-05-02 15:19:17 +00:00
|
|
|
std::string trimmed = trim_whitespace(m_nameResponse);
|
|
|
|
if (trimmed != m_nameResponse) {
|
|
|
|
m_nameResponse = trimmed;
|
2023-06-20 20:14:30 +00:00
|
|
|
emit generatedNameChanged(QString::fromStdString(m_nameResponse));
|
2023-05-02 15:19:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-19 23:51:28 +00:00
|
|
|
void ChatLLM::handleChatIdChanged(const QString &id)
|
2023-05-04 19:31:41 +00:00
|
|
|
{
|
2023-06-19 23:51:28 +00:00
|
|
|
m_llmThread.setObjectName(id);
|
|
|
|
}
|
|
|
|
|
2023-05-02 15:19:17 +00:00
|
|
|
bool ChatLLM::handleNamePrompt(int32_t token)
|
|
|
|
{
|
2023-07-11 16:37:21 +00:00
|
|
|
#if defined(DEBUG)
|
|
|
|
qDebug() << "name prompt" << m_llmThread.objectName() << token;
|
|
|
|
#endif
|
2023-05-02 15:19:17 +00:00
|
|
|
Q_UNUSED(token);
|
|
|
|
qt_noop();
|
2023-07-09 18:42:11 +00:00
|
|
|
return !m_stopGenerating;
|
2023-05-02 15:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatLLM::handleNameResponse(int32_t token, const std::string &response)
|
|
|
|
{
|
2023-07-11 16:37:21 +00:00
|
|
|
#if defined(DEBUG)
|
|
|
|
qDebug() << "name response" << m_llmThread.objectName() << token << response;
|
|
|
|
#endif
|
2023-05-02 15:19:17 +00:00
|
|
|
Q_UNUSED(token);
|
2023-05-08 16:02:31 +00:00
|
|
|
|
2023-05-02 15:19:17 +00:00
|
|
|
m_nameResponse.append(response);
|
2023-06-20 20:14:30 +00:00
|
|
|
emit generatedNameChanged(QString::fromStdString(m_nameResponse));
|
2023-05-08 16:02:31 +00:00
|
|
|
QString gen = QString::fromStdString(m_nameResponse).simplified();
|
|
|
|
QStringList words = gen.split(' ', Qt::SkipEmptyParts);
|
|
|
|
return words.size() <= 3;
|
2023-05-02 15:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatLLM::handleNameRecalculate(bool isRecalc)
|
|
|
|
{
|
2023-07-11 16:37:21 +00:00
|
|
|
#if defined(DEBUG)
|
|
|
|
qDebug() << "name recalc" << m_llmThread.objectName() << isRecalc;
|
|
|
|
#endif
|
2023-05-02 15:19:17 +00:00
|
|
|
Q_UNUSED(isRecalc);
|
2023-07-09 15:32:51 +00:00
|
|
|
qt_noop();
|
|
|
|
return true;
|
2023-05-02 15:19:17 +00:00
|
|
|
}
|
2023-05-04 19:31:41 +00:00
|
|
|
|
2023-07-01 15:34:21 +00:00
|
|
|
bool ChatLLM::handleSystemPrompt(int32_t token)
|
|
|
|
{
|
2023-07-11 16:37:21 +00:00
|
|
|
#if defined(DEBUG)
|
|
|
|
qDebug() << "system prompt" << m_llmThread.objectName() << token << m_stopGenerating;
|
|
|
|
#endif
|
2023-07-01 15:34:21 +00:00
|
|
|
Q_UNUSED(token);
|
2023-07-09 18:42:11 +00:00
|
|
|
return !m_stopGenerating;
|
2023-07-01 15:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatLLM::handleSystemRecalculate(bool isRecalc)
|
|
|
|
{
|
2023-07-11 16:37:21 +00:00
|
|
|
#if defined(DEBUG)
|
|
|
|
qDebug() << "system recalc" << m_llmThread.objectName() << isRecalc;
|
|
|
|
#endif
|
2023-07-01 15:34:21 +00:00
|
|
|
Q_UNUSED(isRecalc);
|
2023-07-11 16:37:21 +00:00
|
|
|
return false;
|
2023-07-01 15:34:21 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 20:43:02 +00:00
|
|
|
bool ChatLLM::handleRestoreStateFromTextPrompt(int32_t token)
|
|
|
|
{
|
|
|
|
#if defined(DEBUG)
|
|
|
|
qDebug() << "restore state from text prompt" << m_llmThread.objectName() << token << m_stopGenerating;
|
|
|
|
#endif
|
|
|
|
Q_UNUSED(token);
|
|
|
|
return !m_stopGenerating;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatLLM::handleRestoreStateFromTextRecalculate(bool isRecalc)
|
|
|
|
{
|
|
|
|
#if defined(DEBUG)
|
|
|
|
qDebug() << "restore state from text recalc" << m_llmThread.objectName() << isRecalc;
|
|
|
|
#endif
|
|
|
|
Q_UNUSED(isRecalc);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-16 22:58:15 +00:00
|
|
|
// this function serialized the cached model state to disk.
|
|
|
|
// we want to also serialize n_ctx, and read it at load time.
|
2023-10-10 20:43:02 +00:00
|
|
|
bool ChatLLM::serialize(QDataStream &stream, int version, bool serializeKV)
|
2023-05-04 19:31:41 +00:00
|
|
|
{
|
2023-05-08 21:23:02 +00:00
|
|
|
if (version > 1) {
|
2023-06-22 19:44:49 +00:00
|
|
|
stream << m_llModelType;
|
|
|
|
switch (m_llModelType) {
|
2023-05-08 21:23:02 +00:00
|
|
|
case GPTJ_: stream << GPTJ_INTERNAL_STATE_VERSION; break;
|
|
|
|
case LLAMA_: stream << LLAMA_INTERNAL_STATE_VERSION; break;
|
|
|
|
default: Q_UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
2023-05-04 19:31:41 +00:00
|
|
|
stream << response();
|
|
|
|
stream << generatedName();
|
|
|
|
stream << m_promptResponseTokens;
|
2023-10-10 20:43:02 +00:00
|
|
|
|
|
|
|
if (!serializeKV) {
|
|
|
|
#if defined(DEBUG)
|
|
|
|
qDebug() << "serialize" << m_llmThread.objectName() << m_state.size();
|
|
|
|
#endif
|
|
|
|
return stream.status() == QDataStream::Ok;
|
|
|
|
}
|
|
|
|
|
2023-06-19 22:23:05 +00:00
|
|
|
if (version <= 3) {
|
2023-10-11 15:31:34 +00:00
|
|
|
int responseLogits = 0;
|
2023-06-19 22:23:05 +00:00
|
|
|
stream << responseLogits;
|
|
|
|
}
|
2023-05-04 19:31:41 +00:00
|
|
|
stream << m_ctx.n_past;
|
2024-01-22 15:01:31 +00:00
|
|
|
if (version >= 7) {
|
2023-12-16 22:58:15 +00:00
|
|
|
stream << m_ctx.n_ctx;
|
|
|
|
}
|
2023-05-04 19:31:41 +00:00
|
|
|
stream << quint64(m_ctx.logits.size());
|
|
|
|
stream.writeRawData(reinterpret_cast<const char*>(m_ctx.logits.data()), m_ctx.logits.size() * sizeof(float));
|
|
|
|
stream << quint64(m_ctx.tokens.size());
|
|
|
|
stream.writeRawData(reinterpret_cast<const char*>(m_ctx.tokens.data()), m_ctx.tokens.size() * sizeof(int));
|
|
|
|
saveState();
|
2023-05-06 00:11:24 +00:00
|
|
|
QByteArray compressed = qCompress(m_state);
|
|
|
|
stream << compressed;
|
2023-05-07 15:24:07 +00:00
|
|
|
#if defined(DEBUG)
|
2023-06-19 23:51:28 +00:00
|
|
|
qDebug() << "serialize" << m_llmThread.objectName() << m_state.size();
|
2023-05-07 15:24:07 +00:00
|
|
|
#endif
|
2023-05-04 19:31:41 +00:00
|
|
|
return stream.status() == QDataStream::Ok;
|
|
|
|
}
|
|
|
|
|
2023-10-10 20:43:02 +00:00
|
|
|
bool ChatLLM::deserialize(QDataStream &stream, int version, bool deserializeKV, bool discardKV)
|
2023-05-04 19:31:41 +00:00
|
|
|
{
|
2023-05-08 21:23:02 +00:00
|
|
|
if (version > 1) {
|
|
|
|
int internalStateVersion;
|
2023-06-22 19:44:49 +00:00
|
|
|
stream >> m_llModelType;
|
2023-05-08 21:23:02 +00:00
|
|
|
stream >> internalStateVersion; // for future use
|
|
|
|
}
|
2023-05-04 19:31:41 +00:00
|
|
|
QString response;
|
|
|
|
stream >> response;
|
|
|
|
m_response = response.toStdString();
|
|
|
|
QString nameResponse;
|
|
|
|
stream >> nameResponse;
|
|
|
|
m_nameResponse = nameResponse.toStdString();
|
|
|
|
stream >> m_promptResponseTokens;
|
2023-10-10 20:43:02 +00:00
|
|
|
|
|
|
|
// If we do not deserialize the KV or it is discarded, then we need to restore the state from the
|
|
|
|
// text only. This will be a costly operation, but the chat has to be restored from the text archive
|
|
|
|
// alone.
|
|
|
|
m_restoreStateFromText = !deserializeKV || discardKV;
|
|
|
|
|
|
|
|
if (!deserializeKV) {
|
|
|
|
#if defined(DEBUG)
|
|
|
|
qDebug() << "deserialize" << m_llmThread.objectName();
|
|
|
|
#endif
|
|
|
|
return stream.status() == QDataStream::Ok;
|
|
|
|
}
|
|
|
|
|
2023-06-19 22:23:05 +00:00
|
|
|
if (version <= 3) {
|
|
|
|
int responseLogits;
|
|
|
|
stream >> responseLogits;
|
|
|
|
}
|
2023-10-10 20:43:02 +00:00
|
|
|
|
|
|
|
int32_t n_past;
|
|
|
|
stream >> n_past;
|
|
|
|
if (!discardKV) m_ctx.n_past = n_past;
|
|
|
|
|
2024-01-22 15:01:31 +00:00
|
|
|
if (version >= 7) {
|
2023-12-16 22:58:15 +00:00
|
|
|
uint32_t n_ctx;
|
|
|
|
stream >> n_ctx;
|
|
|
|
if (!discardKV) m_ctx.n_ctx = n_ctx;
|
|
|
|
}
|
|
|
|
|
2023-05-04 19:31:41 +00:00
|
|
|
quint64 logitsSize;
|
|
|
|
stream >> logitsSize;
|
2023-10-10 20:43:02 +00:00
|
|
|
if (!discardKV) {
|
|
|
|
m_ctx.logits.resize(logitsSize);
|
|
|
|
stream.readRawData(reinterpret_cast<char*>(m_ctx.logits.data()), logitsSize * sizeof(float));
|
|
|
|
} else {
|
|
|
|
stream.skipRawData(logitsSize * sizeof(float));
|
|
|
|
}
|
|
|
|
|
2023-05-04 19:31:41 +00:00
|
|
|
quint64 tokensSize;
|
|
|
|
stream >> tokensSize;
|
2023-10-10 20:43:02 +00:00
|
|
|
if (!discardKV) {
|
|
|
|
m_ctx.tokens.resize(tokensSize);
|
|
|
|
stream.readRawData(reinterpret_cast<char*>(m_ctx.tokens.data()), tokensSize * sizeof(int));
|
|
|
|
} else {
|
|
|
|
stream.skipRawData(tokensSize * sizeof(int));
|
|
|
|
}
|
|
|
|
|
2023-05-08 09:52:57 +00:00
|
|
|
if (version > 0) {
|
|
|
|
QByteArray compressed;
|
|
|
|
stream >> compressed;
|
2023-10-10 20:43:02 +00:00
|
|
|
if (!discardKV)
|
|
|
|
m_state = qUncompress(compressed);
|
2023-05-08 09:52:57 +00:00
|
|
|
} else {
|
2023-12-12 16:45:03 +00:00
|
|
|
if (!discardKV) {
|
2023-10-10 20:43:02 +00:00
|
|
|
stream >> m_state;
|
2023-12-12 16:45:03 +00:00
|
|
|
} else {
|
2023-10-10 20:43:02 +00:00
|
|
|
QByteArray state;
|
2023-12-12 16:45:03 +00:00
|
|
|
stream >> state;
|
2023-10-10 20:43:02 +00:00
|
|
|
}
|
2023-05-08 09:52:57 +00:00
|
|
|
}
|
2023-10-10 20:43:02 +00:00
|
|
|
|
2023-05-07 15:24:07 +00:00
|
|
|
#if defined(DEBUG)
|
2023-06-19 23:51:28 +00:00
|
|
|
qDebug() << "deserialize" << m_llmThread.objectName();
|
2023-05-07 15:24:07 +00:00
|
|
|
#endif
|
2023-05-04 19:31:41 +00:00
|
|
|
return stream.status() == QDataStream::Ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatLLM::saveState()
|
|
|
|
{
|
|
|
|
if (!isModelLoaded())
|
|
|
|
return;
|
|
|
|
|
2024-03-13 22:23:57 +00:00
|
|
|
if (m_llModelType == LLModelType::API_) {
|
2023-05-15 22:36:41 +00:00
|
|
|
m_state.clear();
|
|
|
|
QDataStream stream(&m_state, QIODeviceBase::WriteOnly);
|
2024-02-05 09:37:59 +00:00
|
|
|
stream.setVersion(QDataStream::Qt_6_4);
|
2024-03-13 22:23:57 +00:00
|
|
|
ChatAPI *chatAPI = static_cast<ChatAPI*>(m_llModelInfo.model);
|
|
|
|
stream << chatAPI->context();
|
2023-05-15 22:36:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-22 19:44:49 +00:00
|
|
|
const size_t stateSize = m_llModelInfo.model->stateSize();
|
2023-05-04 19:31:41 +00:00
|
|
|
m_state.resize(stateSize);
|
2023-05-07 15:24:07 +00:00
|
|
|
#if defined(DEBUG)
|
2023-06-19 23:51:28 +00:00
|
|
|
qDebug() << "saveState" << m_llmThread.objectName() << "size:" << m_state.size();
|
2023-05-07 15:24:07 +00:00
|
|
|
#endif
|
2023-06-22 19:44:49 +00:00
|
|
|
m_llModelInfo.model->saveState(static_cast<uint8_t*>(reinterpret_cast<void*>(m_state.data())));
|
2023-05-04 19:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatLLM::restoreState()
|
|
|
|
{
|
2023-10-10 20:43:02 +00:00
|
|
|
if (!isModelLoaded())
|
2023-05-04 19:31:41 +00:00
|
|
|
return;
|
|
|
|
|
2024-03-13 22:23:57 +00:00
|
|
|
if (m_llModelType == LLModelType::API_) {
|
2023-05-15 22:36:41 +00:00
|
|
|
QDataStream stream(&m_state, QIODeviceBase::ReadOnly);
|
2024-02-05 09:37:59 +00:00
|
|
|
stream.setVersion(QDataStream::Qt_6_4);
|
2024-03-13 22:23:57 +00:00
|
|
|
ChatAPI *chatAPI = static_cast<ChatAPI*>(m_llModelInfo.model);
|
2023-05-15 22:36:41 +00:00
|
|
|
QList<QString> context;
|
|
|
|
stream >> context;
|
2024-03-13 22:23:57 +00:00
|
|
|
chatAPI->setContext(context);
|
2023-05-15 22:36:41 +00:00
|
|
|
m_state.clear();
|
2023-12-12 16:45:03 +00:00
|
|
|
m_state.squeeze();
|
2023-05-15 22:36:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-07 15:24:07 +00:00
|
|
|
#if defined(DEBUG)
|
2023-06-19 23:51:28 +00:00
|
|
|
qDebug() << "restoreState" << m_llmThread.objectName() << "size:" << m_state.size();
|
2023-05-07 15:24:07 +00:00
|
|
|
#endif
|
2023-10-10 20:43:02 +00:00
|
|
|
|
|
|
|
if (m_state.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2023-12-12 16:45:03 +00:00
|
|
|
if (m_llModelInfo.model->stateSize() == m_state.size()) {
|
|
|
|
m_llModelInfo.model->restoreState(static_cast<const uint8_t*>(reinterpret_cast<void*>(m_state.data())));
|
|
|
|
m_processedSystemPrompt = true;
|
|
|
|
} else {
|
2024-03-06 22:14:54 +00:00
|
|
|
qWarning() << "restoring state from text because" << m_llModelInfo.model->stateSize() << "!=" << m_state.size();
|
2023-12-12 16:45:03 +00:00
|
|
|
m_restoreStateFromText = true;
|
|
|
|
}
|
|
|
|
|
2023-05-09 01:05:50 +00:00
|
|
|
m_state.clear();
|
2023-12-12 16:45:03 +00:00
|
|
|
m_state.squeeze();
|
2023-05-04 19:31:41 +00:00
|
|
|
}
|
2023-07-01 15:34:21 +00:00
|
|
|
|
|
|
|
void ChatLLM::processSystemPrompt()
|
|
|
|
{
|
|
|
|
Q_ASSERT(isModelLoaded());
|
2023-11-21 15:42:12 +00:00
|
|
|
if (!isModelLoaded() || m_processedSystemPrompt || m_restoreStateFromText || m_isServer)
|
2023-07-01 15:34:21 +00:00
|
|
|
return;
|
|
|
|
|
2023-07-12 18:27:48 +00:00
|
|
|
const std::string systemPrompt = MySettings::globalInstance()->modelSystemPrompt(m_modelInfo).toStdString();
|
2023-07-12 18:30:11 +00:00
|
|
|
if (QString::fromStdString(systemPrompt).trimmed().isEmpty()) {
|
2023-07-12 18:27:48 +00:00
|
|
|
m_processedSystemPrompt = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-10 20:43:02 +00:00
|
|
|
// Start with a whole new context
|
2023-07-09 18:42:11 +00:00
|
|
|
m_stopGenerating = false;
|
2023-10-10 20:43:02 +00:00
|
|
|
m_ctx = LLModel::PromptContext();
|
|
|
|
|
2023-07-01 15:34:21 +00:00
|
|
|
auto promptFunc = std::bind(&ChatLLM::handleSystemPrompt, this, std::placeholders::_1);
|
|
|
|
auto recalcFunc = std::bind(&ChatLLM::handleSystemRecalculate, this, std::placeholders::_1);
|
|
|
|
|
|
|
|
const int32_t n_predict = MySettings::globalInstance()->modelMaxLength(m_modelInfo);
|
|
|
|
const int32_t top_k = MySettings::globalInstance()->modelTopK(m_modelInfo);
|
|
|
|
const float top_p = MySettings::globalInstance()->modelTopP(m_modelInfo);
|
2024-02-24 22:51:34 +00:00
|
|
|
const float min_p = MySettings::globalInstance()->modelMinP(m_modelInfo);
|
2023-07-01 15:34:21 +00:00
|
|
|
const float temp = MySettings::globalInstance()->modelTemperature(m_modelInfo);
|
|
|
|
const int32_t n_batch = MySettings::globalInstance()->modelPromptBatchSize(m_modelInfo);
|
|
|
|
const float repeat_penalty = MySettings::globalInstance()->modelRepeatPenalty(m_modelInfo);
|
|
|
|
const int32_t repeat_penalty_tokens = MySettings::globalInstance()->modelRepeatPenaltyTokens(m_modelInfo);
|
|
|
|
int n_threads = MySettings::globalInstance()->threadCount();
|
|
|
|
m_ctx.n_predict = n_predict;
|
|
|
|
m_ctx.top_k = top_k;
|
|
|
|
m_ctx.top_p = top_p;
|
2024-02-24 22:51:34 +00:00
|
|
|
m_ctx.min_p = min_p;
|
2023-07-01 15:34:21 +00:00
|
|
|
m_ctx.temp = temp;
|
|
|
|
m_ctx.n_batch = n_batch;
|
|
|
|
m_ctx.repeat_penalty = repeat_penalty;
|
|
|
|
m_ctx.repeat_last_n = repeat_penalty_tokens;
|
|
|
|
m_llModelInfo.model->setThreadCount(n_threads);
|
|
|
|
#if defined(DEBUG)
|
|
|
|
printf("%s", qPrintable(QString::fromStdString(systemPrompt)));
|
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
2024-02-21 20:45:32 +00:00
|
|
|
auto old_n_predict = std::exchange(m_ctx.n_predict, 0); // decode system prompt without a response
|
2024-04-15 15:39:48 +00:00
|
|
|
// use "%1%2" and not "%1" to avoid implicit whitespace
|
|
|
|
m_llModelInfo.model->prompt(systemPrompt, "%1%2", promptFunc, nullptr, recalcFunc, m_ctx, true);
|
2024-02-21 20:45:32 +00:00
|
|
|
m_ctx.n_predict = old_n_predict;
|
2023-07-01 15:34:21 +00:00
|
|
|
#if defined(DEBUG)
|
|
|
|
printf("\n");
|
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
2023-10-10 20:43:02 +00:00
|
|
|
|
2023-11-21 15:42:12 +00:00
|
|
|
m_processedSystemPrompt = m_stopGenerating == false;
|
2023-10-10 20:43:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatLLM::processRestoreStateFromText()
|
|
|
|
{
|
|
|
|
Q_ASSERT(isModelLoaded());
|
|
|
|
if (!isModelLoaded() || !m_restoreStateFromText || m_isServer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_isRecalc = true;
|
|
|
|
emit recalcChanged();
|
|
|
|
|
|
|
|
m_stopGenerating = false;
|
|
|
|
m_ctx = LLModel::PromptContext();
|
|
|
|
|
|
|
|
auto promptFunc = std::bind(&ChatLLM::handleRestoreStateFromTextPrompt, this, std::placeholders::_1);
|
|
|
|
auto recalcFunc = std::bind(&ChatLLM::handleRestoreStateFromTextRecalculate, this, std::placeholders::_1);
|
|
|
|
|
|
|
|
const QString promptTemplate = MySettings::globalInstance()->modelPromptTemplate(m_modelInfo);
|
|
|
|
const int32_t n_predict = MySettings::globalInstance()->modelMaxLength(m_modelInfo);
|
|
|
|
const int32_t top_k = MySettings::globalInstance()->modelTopK(m_modelInfo);
|
|
|
|
const float top_p = MySettings::globalInstance()->modelTopP(m_modelInfo);
|
2024-02-24 22:51:34 +00:00
|
|
|
const float min_p = MySettings::globalInstance()->modelMinP(m_modelInfo);
|
2023-10-10 20:43:02 +00:00
|
|
|
const float temp = MySettings::globalInstance()->modelTemperature(m_modelInfo);
|
|
|
|
const int32_t n_batch = MySettings::globalInstance()->modelPromptBatchSize(m_modelInfo);
|
|
|
|
const float repeat_penalty = MySettings::globalInstance()->modelRepeatPenalty(m_modelInfo);
|
|
|
|
const int32_t repeat_penalty_tokens = MySettings::globalInstance()->modelRepeatPenaltyTokens(m_modelInfo);
|
|
|
|
int n_threads = MySettings::globalInstance()->threadCount();
|
|
|
|
m_ctx.n_predict = n_predict;
|
|
|
|
m_ctx.top_k = top_k;
|
|
|
|
m_ctx.top_p = top_p;
|
2024-02-24 22:51:34 +00:00
|
|
|
m_ctx.min_p = min_p;
|
2023-10-10 20:43:02 +00:00
|
|
|
m_ctx.temp = temp;
|
|
|
|
m_ctx.n_batch = n_batch;
|
|
|
|
m_ctx.repeat_penalty = repeat_penalty;
|
|
|
|
m_ctx.repeat_last_n = repeat_penalty_tokens;
|
|
|
|
m_llModelInfo.model->setThreadCount(n_threads);
|
2024-02-21 20:45:32 +00:00
|
|
|
|
|
|
|
auto it = m_stateFromText.begin();
|
|
|
|
while (it < m_stateFromText.end()) {
|
|
|
|
auto &prompt = *it++;
|
|
|
|
Q_ASSERT(prompt.first == "Prompt: ");
|
|
|
|
Q_ASSERT(it < m_stateFromText.end());
|
|
|
|
|
|
|
|
auto &response = *it++;
|
|
|
|
Q_ASSERT(response.first != "Prompt: ");
|
|
|
|
auto responseText = response.second.toStdString();
|
|
|
|
|
|
|
|
m_llModelInfo.model->prompt(prompt.second.toStdString(), promptTemplate.toStdString(), promptFunc, nullptr,
|
|
|
|
recalcFunc, m_ctx, false, &responseText);
|
2023-10-10 20:43:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_stopGenerating) {
|
|
|
|
m_restoreStateFromText = false;
|
|
|
|
m_stateFromText.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_isRecalc = false;
|
|
|
|
emit recalcChanged();
|
2023-09-29 17:53:43 +00:00
|
|
|
}
|