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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2026-06-10
 * Description : llama.cpp-based inference backend (GGUF models).
 *               First concrete backend.
 *               TinyLlama 1.1B Q4 as baseline, Qwen2.5-1.5B-Instruct
 *               as primary candidate. Inference runs on a worker
 *               thread; results are delivered via queued signals.
 *
 * SPDX-FileCopyrightText: 2026 by Srirupa Datta <srirupa dot sps at gmail dot com>
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "searchllamabackend.h"

// C++ includes

#include <cstdint>

// Qt includes

#include <QFileInfo>
#include <vector>
#include <QElapsedTimer>

// KDE includes

#include <klocalizedstring.h>

// Local includes

#include "digikam_debug.h"

#ifdef HAVE_LLAMACPP
#   include <llama.h>
#endif

namespace Digikam
{

#ifdef HAVE_LLAMACPP

namespace
{

/**
 * @brief Route llama.cpp's internal logging through digiKam's NL-search log
 * category instead of letting it print directly to stderr.
 */
void s_llamaLogCallback(ggml_log_level level, const char* text, void* userData)
{
    Q_UNUSED(level);
    Q_UNUSED(userData);

    if (text)
    {
        const QString msg = QString::fromUtf8(text).trimmed();

        if (!msg.isEmpty())
        {
            qCDebug(DIGIKAM_NLSEARCH_LOG).noquote() << msg;
        }
    }
}

/**
 * @brief Abort callback for llama.cpp. Returns true to stop an in-progress
 * decode. The void* carries the worker instance, so a cancellation requested
 * from the GUI interrupts computation mid-decode, not only between tokens.
 */
bool s_llamaAbortCallback(void* userData)<--- Parameter 'userData' can be declared as pointer to const
{
    if (userData)
    {
        const SearchLlamaWorker* const worker = static_cast<const SearchLlamaWorker*>(userData);

        return worker->abortRequested();
    }

    return false;
}

/**
 * @brief Model-load progress callback for llama.cpp. Forwards the 0.0-1.0
 * load progress to the worker, which relays it to the GUI. Returns true to
 * continue loading.
 */
bool s_llamaProgressCallback(float progress, void* userData)
{
    if (userData)
    {
        SearchLlamaWorker* const worker = static_cast<SearchLlamaWorker*>(userData);
        worker->reportLoadProgress(progress);
    }

    return true;
}

} // anonymous namespace

#endif // HAVE_LLAMACPP

SearchLlamaBackend::SearchLlamaBackend(QObject* const parent)
    : SearchLanguageBackend(parent)
{
    m_worker = new SearchLlamaWorker;
    m_worker->moveToThread(&m_workerThread);

    connect(&m_workerThread, &QThread::finished,
            m_worker, &QObject::deleteLater);

    connect(this, &SearchLlamaBackend::signalRequestLoad,
            m_worker, &SearchLlamaWorker::slotDoLoad);

    connect(this, &SearchLlamaBackend::signalRequestInference,
            m_worker, &SearchLlamaWorker::slotDoInference);

    connect(m_worker, &SearchLlamaWorker::signalLoaded,
            this, [this](bool ok)
        {
            m_modelLoaded = ok;

            Q_EMIT signalModelLoaded(ok);
        }
    );

    connect(m_worker, &SearchLlamaWorker::signalOutputReady,
            this, &SearchLlamaBackend::signalRawOutputReady);

    connect(m_worker, &SearchLlamaWorker::signalError,
            this, &SearchLlamaBackend::signalInferenceError);

    connect(m_worker, &SearchLlamaWorker::signalProgress,
            this, &SearchLlamaBackend::signalInferenceProgress);

    connect(m_worker, &SearchLlamaWorker::signalLoadProgress,
            this, &SearchLlamaBackend::signalModelLoadProgress);

    connect(m_worker, &SearchLlamaWorker::signalCancelled,
            this, &SearchLlamaBackend::signalInferenceCancelled);

    m_workerThread.start();
}

SearchLlamaBackend::~SearchLlamaBackend()
{
    m_worker->requestCancel();
    m_workerThread.quit();
    m_workerThread.wait();
}

bool SearchLlamaBackend::loadModel(const QString& modelPath)
{
    if (!QFileInfo::exists(modelPath))
    {
        qCWarning(DIGIKAM_NLSEARCH_LOG) << "NL search: model file not found:" << modelPath;

        return false;
    }

    m_modelPath = modelPath;

    Q_EMIT signalRequestLoad(modelPath);

    return true;
}

void SearchLlamaBackend::unloadModel()
{
    QMetaObject::invokeMethod(m_worker, "slotDoUnload", Qt::QueuedConnection);
    m_modelLoaded = false;
}

bool SearchLlamaBackend::isModelLoaded() const
{
    return m_modelLoaded;
}

QString SearchLlamaBackend::modelPath() const
{
    return m_modelPath;
}

QString SearchLlamaBackend::backendName() const
{
    return QLatin1String("llama.cpp");
}

void SearchLlamaBackend::setMaxTokens(int maxTokens)
{
    m_maxTokens = maxTokens;
}

void SearchLlamaBackend::setTemperature(float temperature)
{
    m_temperature = temperature;
}

void SearchLlamaBackend::slotRunInference(const QString& prompt)
{
    if (!m_modelLoaded)
    {
        Q_EMIT signalInferenceError(QLatin1String("Model is not loaded."));

        return;
    }

    Q_EMIT signalRequestInference(prompt, m_maxTokens, m_temperature);
}

void SearchLlamaBackend::slotCancel()
{
    m_worker->requestCancel();
}

SearchLlamaWorker::SearchLlamaWorker(QObject* const parent)
    : QObject(parent)
{
}

SearchLlamaWorker::~SearchLlamaWorker()
{
    slotDoUnload();
}

void SearchLlamaWorker::requestCancel()
{
    m_cancelRequested.storeRelaxed(1);
}

void SearchLlamaWorker::slotDoLoad(const QString& modelPath)
{

#ifdef HAVE_LLAMACPP

    // clear any previously loaded model

    slotDoUnload();

    // Initialize the llama.cpp backend

    llama_backend_init();

    // Route llama.cpp's internal logging through digiKam's log category.

    llama_log_set(s_llamaLogCallback, nullptr);

    // Model parameters: CPU-only

    llama_model_params mparams = llama_model_default_params();
    mparams.n_gpu_layers       = 0;

    // Report model-load progress (0.0-1.0) to the GUI while the ~1 GB model loads.

    mparams.progress_callback           = s_llamaProgressCallback;
    mparams.progress_callback_user_data = this;

    const QByteArray pathUtf8  = modelPath.toUtf8();
    llama_model* const model   = llama_model_load_from_file(pathUtf8.constData(), mparams);

    if (!model)
    {
        qCWarning(DIGIKAM_NLSEARCH_LOG) << "NL search: failed to load model:" << modelPath;

        Q_EMIT signalError(i18n("Failed to load the language model."));
        Q_EMIT signalLoaded(false);

        return;
    }

    // Context window large enough for the prompt and the structured output, and thread counts sized to the machine.

    llama_context_params cparams = llama_context_default_params();

    // Context window: the maximum combined prompt + generated tokens the model
    // holds at once. 4096 comfortably fits the system prompt, the field schema,
    // collection hints, and the user query, with headroom to spare.

    cparams.n_ctx                = 4096;
    cparams.n_threads            = QThread::idealThreadCount();
    cparams.n_threads_batch      = QThread::idealThreadCount();

    llama_context* const ctx     = llama_init_from_model(model, cparams);

    if (!ctx)
    {
        qCWarning(DIGIKAM_NLSEARCH_LOG) << "NL search: failed to create llama context";

        llama_model_free(model);

        Q_EMIT signalError(i18n("Failed to initialize the model context."));
        Q_EMIT signalLoaded(false);

        return;
    }

    m_model   = model;
    m_context = ctx;

    // Allow the GUI to abort a long decode mid-computation (checked by
    // llama.cpp during llama_decode), in addition to the between-token
    // check in slotDoInference.

    llama_set_abort_callback(ctx, s_llamaAbortCallback, this);<--- You might need to cast the function pointer here

    Q_EMIT signalLoaded(true);

#else

    Q_UNUSED(modelPath);
    Q_EMIT signalError(i18n("digiKam was built without llama.cpp support."));
    Q_EMIT signalLoaded(false);

#endif

}

void SearchLlamaWorker::slotDoInference(const QString& prompt, int maxTokens, float temperature)
{
    m_cancelRequested.storeRelaxed(0);

#ifdef HAVE_LLAMACPP

    Q_UNUSED(temperature);  // greedy decoding, determinism preferred for structured output

    if (!m_model || !m_context)
    {
        Q_EMIT signalError(i18n("Model is not loaded."));

        return;
    }

    const llama_model* const model = static_cast<llama_model*>(m_model);
    llama_context*     const ctx   = static_cast<llama_context*>(m_context);
    const llama_vocab* const vocab = llama_model_get_vocab(model);

    llama_memory_clear(llama_get_memory(ctx), true);

    // 1. Tokenize the prompt

    const QByteArray promptUtf8 = prompt.toUtf8();

    const int32_t tokenized     = llama_tokenize(vocab, promptUtf8.constData(),
                                                 promptUtf8.size(), nullptr, 0, true, true);
    const int n_prompt          = (tokenized == INT32_MIN) ? 0 : -tokenized;

    if (n_prompt <= 0)
    {
        Q_EMIT signalError(i18n("Failed to tokenize the prompt."));

        return;
    }

    std::vector<llama_token> tokens(n_prompt);

    if (llama_tokenize(vocab, promptUtf8.constData(), promptUtf8.size(),
                       tokens.data(), tokens.size(), true, true) < 0)
    {
        Q_EMIT signalError(i18n("Failed to tokenize the prompt."));

        return;
    }

    // 2. Greedy sampler (deterministic)

    llama_sampler* const smpl = llama_sampler_chain_init(llama_sampler_chain_default_params());
    llama_sampler_chain_add(smpl, llama_sampler_init_greedy());

    // 3. Decode the prompt, then generate token by token

    QString     result;
    llama_batch batch     = llama_batch_get_one(tokens.data(), tokens.size());
    int         generated = 0;

    while (generated < maxTokens)
    {
        if (m_cancelRequested.loadRelaxed())
        {
            llama_sampler_free(smpl);

            Q_EMIT signalCancelled();

            return;
        }

        if (llama_decode(ctx, batch) != 0)
        {
            llama_sampler_free(smpl);

            if (m_cancelRequested.loadRelaxed())
            {
                Q_EMIT signalCancelled();
            }
            else
            {
                Q_EMIT signalError(i18n("Model decode failed."));
            }

            return;
        }

        const llama_token newToken = llama_sampler_sample(smpl, ctx, -1);

        if (llama_vocab_is_eog(vocab, newToken))
        {
            break;
        }

        char      piece[256] = { 0 };
        const int n          = llama_token_to_piece(vocab, newToken, piece, sizeof(piece), 0, true);

        if (n > 0)
        {
            result += QString::fromUtf8(piece, n);
        }

        Q_EMIT signalProgress(++generated);

        const QString trimmed = result.trimmed();
        int depth             = 0;
        bool sawOpen          = false;
        bool balanced         = false;

        for (const QChar& ch : trimmed)
        {
            if      (ch == QLatin1Char('{'))
            {
                ++depth;
                sawOpen = true;
            }
            else if (ch == QLatin1Char('}'))
            {
                --depth;
            }
        }

        balanced = (sawOpen && depth == 0);

        if (balanced)
        {
            break;
        }

        m_singleToken = newToken;
        batch         = llama_batch_get_one(&m_singleToken, 1);
    }

    llama_sampler_free(smpl);

    Q_EMIT signalOutputReady(result);

#else

    Q_UNUSED(prompt);
    Q_UNUSED(maxTokens);
    Q_UNUSED(temperature);
    Q_EMIT signalError(i18n("digiKam was built without llama.cpp support."));

#endif

}

void SearchLlamaWorker::slotDoUnload()
{

#ifdef HAVE_LLAMACPP

    if (m_context)
    {
        llama_free(static_cast<llama_context*>(m_context));
        m_context = nullptr;
    }

    if (m_model)
    {
        llama_model_free(static_cast<llama_model*>(m_model));
        m_model = nullptr;
    }

#endif

}

} // namespace Digikam

#include "moc_searchllamabackend.cpp"