Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ static hf_cache::hf_file find_best_model(const hf_cache::hf_files & files,
if (!tag.empty()) {
tags.push_back(tag);
} else {
tags = {"Q4_K_M", "Q8_0"};
tags = {"Q4_K_M", "Q4_K", "Q8_0"};
}

for (const auto & t : tags) {
Expand Down
23 changes: 21 additions & 2 deletions common/parsers/qwen3-coder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,38 @@ common_chat_params common_chat_params_init_qwen3_coder(const common_chat_templat

auto is_qwen3_coder = !supports_reasoning;

// Compact reasoning: "<think>" + reasoning + "</think>" + content with no separators (Qwen4), unlike the
// "<think>\n...\n</think>\n\n" of Qwen3.5. The template shows it when thinking is turned off.
bool compact_think = false;
if (supports_reasoning) {
autoparser::generation_params probe;
probe.messages = json::array({ { { "role", "user" }, { "content", "x" } } });
probe.add_generation_prompt = true;
probe.enable_thinking = false;
try {
compact_think = string_ends_with(common_chat_template_generation_prompt(tmpl, probe), "<think></think>");
} catch (const std::exception &) {
// a template that cannot render without thinking keeps the default layout
}
}

if (supports_reasoning) {
data.thinking_start_tag = "<think>";
// Support both </think> and <tool_call> as reasoning end sequences.
// The newline variant comes first so it is included in the forced message
// <function= is omitted, as it is a workaround for Qwen3-Coder which is not a thinking model
data.thinking_end_tags = { "\n</think>", "</think>", "<tool_call>" };
if (compact_think) {
data.thinking_end_tags = { "</think>", "<tool_call>" };
}
data.preserved_tokens.insert(data.preserved_tokens.end(), { "<think>", "</think>" });
}

data.message_delimiters = {
{ COMMON_CHAT_ROLE_ASSISTANT, "<|im_start|>assistant" },
{ COMMON_CHAT_ROLE_TOOL, "<|im_start|>user\n<tool_response>" }, // Qwen3-Coder, Qwen3.5, Nemotron Nano 3
{ COMMON_CHAT_ROLE_TOOL, "<|im_start|>tool_response" }, // StepFun-3.5-Flash
{ COMMON_CHAT_ROLE_TOOL, "<|im_start|>tool\n" }, // Qwen4
{ COMMON_CHAT_ROLE_USER, "<|im_start|>user" },
{ COMMON_CHAT_ROLE_SYSTEM, "<|im_start|>system" },
};
Expand All @@ -47,9 +66,9 @@ common_chat_params common_chat_params_init_qwen3_coder(const common_chat_templat

data.generation_prompt = GEN_PREFIX;
if (supports_reasoning) {
data.generation_prompt += "<think>\n" + msg.reasoning_content;
data.generation_prompt += (compact_think ? "<think>" : "<think>\n") + msg.reasoning_content;
if (inputs.continue_final_message == COMMON_CHAT_CONTINUATION_CONTENT) {
data.generation_prompt += "\n</think>\n\n";
data.generation_prompt += compact_think ? "</think>" : "\n</think>\n\n";
}
}
if (inputs.continue_final_message == COMMON_CHAT_CONTINUATION_CONTENT) {
Expand Down
533 changes: 487 additions & 46 deletions common/speculative.cpp

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions common/speculative.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ bool common_speculative_process(common_speculative * spec, const common_batch &
// generate drafts for the sequences specified with `common_speculative_get_draft_params`
void common_speculative_draft(common_speculative * spec);

// flush any deferred draft-context catch-up injections before the next target decode overwrites device rows
void common_speculative_flush(common_speculative * spec, llama_seq_id seq_id = -1);

// informs the speculative context that n_accepted tokens were accepted by the target model
void common_speculative_accept(common_speculative * spec, llama_seq_id, uint16_t n_accepted);

Expand Down
6 changes: 6 additions & 0 deletions conversion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@
"Qwen3_5TextModel": "qwen",
"Qwen4ExpForCausalLM": "qwen4exp",
"Qwen4ExpForConditionalGeneration": "qwen4exp",
"Qwen4MoeForCausalLM": "qwen4exp",
"Qwen4MoeForConditionalGeneration": "qwen4exp",
"Qwen4ForCausalLM": "qwen4exp",
"Qwen4ForConditionalGeneration": "qwen4exp",
"RND1": "qwen",
"RWForCausalLM": "falcon",
"RWKV6Qwen2ForCausalLM": "rwkv",
Expand Down Expand Up @@ -445,6 +449,8 @@
"Qwen3_5ForConditionalGeneration": "qwen3vl",
"Qwen3_5MoeForConditionalGeneration": "qwen3vl",
"Qwen4ExpForConditionalGeneration": "qwen4exp",
"Qwen4MoeForConditionalGeneration": "qwen4exp",
"Qwen4ForConditionalGeneration": "qwen4exp",
"RADIOModel": "nemotron",
"Sarashina2VisionForCausalLM": "sarashina2",
"SmolVLMForConditionalGeneration": "smolvlm",
Expand Down
89 changes: 89 additions & 0 deletions ggml/include/ggml-agx.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#include <stddef.h>
#include <stdint.h>

#include "ggml.h"
#include "ggml-backend.h"

Expand Down Expand Up @@ -27,6 +30,92 @@ GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_agx_host_buffer_type(vo

GGML_BACKEND_API ggml_backend_reg_t ggml_backend_agx_reg(void);

// ============================================================================
// Wave 1: 1-Submit Batched GPU Sampler & Zero-Sync Device-to-Device Copy API
// Also exported via ggml_backend_reg_get_proc_address(agx_reg, "<name>")
// so dynamic-backend builds (GGML_BACKEND_DL) can resolve them without direct link.
// ============================================================================

typedef struct agx_sample_penalty_entry {
int32_t token_id;
int32_t count; // > 0: token occurrence count in penalty_last_n window
} agx_sample_penalty_entry;

typedef struct agx_sample_bias_entry {
int32_t token_id;
float bias;
} agx_sample_bias_entry;

typedef struct agx_sample_slot_desc {
// GPU F32 logits source: if logits_tensor != NULL, uses logits_tensor + logits_byte_offset;
// otherwise uses batch-level default_logits_tensor at row `row_idx` (row_idx * nb[1]).
const struct ggml_tensor * logits_tensor;
size_t logits_byte_offset;
int32_t row_idx;
int32_t n_vocab; // <= 0 => use tensor->ne[0]

// Sparse penalties (applied AFTER logit biases, exact llama_sampler_penalties formula):
// logit = (logit <= 0.0f) ? (logit * penalty_repeat) : (logit / penalty_repeat);
// logit -= float(count) * penalty_freq + (count > 0 ? penalty_present : 0.0f);
const agx_sample_penalty_entry * penalties;
uint32_t n_penalties;

// Sparse logit biases (applied BEFORE penalties, e.g. suppress_tokens / ignore_eos)
const agx_sample_bias_entry * biases;
uint32_t n_biases;

float penalty_repeat; // 1.0f = disabled
float penalty_freq; // 0.0f = disabled
float penalty_present; // 0.0f = disabled

float temp; // <= 0.0f => exact greedy argmax (smallest token_id wins ties)
int32_t top_k; // <= 0 => disabled; == 1 => greedy
float top_p; // <= 0.0f or >= 1.0f => disabled
float min_p; // <= 0.0f => disabled
uint32_t min_keep; // minimum candidates kept by top_p/min_p (0 => 1)
uint64_t rng_seed; // per-slot per-step RNG seed (used when temp > 0 && top_k != 1)
} agx_sample_slot_desc;

enum agx_sample_flags {
AGX_SAMPLE_FLAG_NONE = 0u,
// Save & restore modified logits after sampling (default 0 allows in-place scatter on ephemeral logits)
AGX_SAMPLE_FLAG_PRESERVE_LOGITS = 1u << 0,
};

typedef bool (*agx_sample_batch_logits_fn)(
ggml_backend_t backend, // optional (NULL resolves AGX0 from logits tensor)
const struct ggml_tensor * default_logits_tensor, // optional if every slot sets logits_tensor
const agx_sample_slot_desc * slots,
uint32_t n_slots,
uint32_t flags,
int32_t * out_tokens, // [n_slots]
float * out_probs); // optional [n_slots]

typedef bool (*agx_copy_tensor_bytes_async_fn)(
ggml_backend_t backend, // optional (NULL resolves AGX0 from src/dst tensor)
const struct ggml_tensor * src,
size_t src_offset,
struct ggml_tensor * dst,
size_t dst_offset,
size_t n_bytes);

GGML_BACKEND_API bool agx_sample_batch_logits(
ggml_backend_t backend,
const struct ggml_tensor * default_logits_tensor,
const agx_sample_slot_desc * slots,
uint32_t n_slots,
uint32_t flags,
int32_t * out_tokens,
float * out_probs);

GGML_BACKEND_API bool agx_copy_tensor_bytes_async(
ggml_backend_t backend,
const struct ggml_tensor * src,
size_t src_offset,
struct ggml_tensor * dst,
size_t dst_offset,
size_t n_bytes);

#ifdef __cplusplus
}
#endif
5 changes: 3 additions & 2 deletions ggml/src/ggml-agx/agx-cdm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1545,8 +1545,9 @@ bool agx_cdm_assert_disjoint(agx_cdm_stream * s,
const agx_barrier_mode req = s->hazard.classify(reads, n_reads, writes, n_writes, /*strong_war=*/false);
if (AGX_UNLIKELY(req != AGX_BARRIER_NONE)) {
const char * tag = label ? label : "unnamed";
AGX_LOG_ERROR("GGML_AGX_HAZARD_CHECK FATAL: concurrent dispatch '%s' aliases pending wave range (required barrier=%u, pending_w=%u, pending_r=%u)!",
tag, (unsigned) req, s->hazard.n_writes, s->hazard.n_reads);
AGX_LOG_ERROR("GGML_AGX_HAZARD_CHECK FATAL: concurrent dispatch '%s' aliases pending wave range (required barrier=%u, pending_w=%u, pending_r=%u, w_ovf=%d, r_ovf=%d)!",
tag, (unsigned) req, s->hazard.n_writes, s->hazard.n_reads,
s->hazard.writes_overflow ? 1 : 0, s->hazard.reads_overflow ? 1 : 0);
for (uint32_t i = 0u; i < n_reads; ++i) {
for (uint32_t j = 0u; j < s->hazard.n_writes; ++j) {
if (reads[i].overlaps(s->hazard.pending_writes[j])) {
Expand Down
4 changes: 2 additions & 2 deletions ggml/src/ggml-agx/agx-cdm.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ struct agx_cdm_buf_access {
};

struct agx_cdm_hazard_tracker {
static constexpr uint32_t kMaxPendingWrites = 128u;
static constexpr uint32_t kMaxPendingReads = 256u;
static constexpr uint32_t kMaxPendingWrites = 1024u;
static constexpr uint32_t kMaxPendingReads = 2048u;

agx_cdm_buf_access pending_writes[kMaxPendingWrites] = {};
uint32_t n_writes = 0u;
Expand Down
Loading
Loading