Skip to content

Conversation

@cacaview
Copy link

Make sure to read the contributing guidelines before submitting a PR
This is the current work progress:
#16930 (comment)

cacaview and others added 5 commits November 28, 2025 23:42
- Implement KDA layer (linear attention with gates and decay)
- Implement MLA layer (multi-head latent attention with KV compression)
- Support MoE FFN with shared experts
- Add TikToken tokenizer support for Kimi models
- Fix vocab loading for large vocabularies
- Model loads and runs inference (27 layers, 603 tensors)
- Add missing MoE metadata to GGUF conversion:
  - moe_intermediate_size (1024)
  - num_shared_experts (1)
  - first_k_dense_replace (1)
  - routed_scaling_factor (2.446)
  - expert_gating_func (sigmoid)

- Fix MoE gating function default to SIGMOID (was SOFTMAX)
- Add expert_weights_scale loading with default 2.446
- Enable moe_renormalize (norm_w=true) in build_moe_ffn
- Add fallback for exp_probs_b tensor suffix compatibility
- Add KDA (Kimi Delta Attention) CUDA kernel (kda-scan.cu)
- Fix recurrence order: decay first, then retrieval
- Verify CPU/CUDA implementation consistency
- Support head_dim=128, L2 normalization for Q/K
@github-actions github-actions bot added model Model specific Nvidia GPU Issues specific to Nvidia GPUs python python script changes ggml changes relating to the ggml tensor library for machine learning labels Nov 29, 2025
Comment on lines +2729 to 2733
# KimiLinearModel is defined later in this file (line ~5140) as a TextModel subclass
# This old definition has been removed to avoid conflicts


@ModelBase.register(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# KimiLinearModel is defined later in this file (line ~5140) as a TextModel subclass
# This old definition has been removed to avoid conflicts
@ModelBase.register(
@ModelBase.register(

(self.format_tensor_name(gguf.MODEL_TENSOR.ATTN_Q, bid), q),
(self.format_tensor_name(gguf.MODEL_TENSOR.ATTN_K, bid), k),
(self.format_tensor_name(gguf.MODEL_TENSOR.ATTN_V, bid), v),
]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
]
]
else:
return [(self.map_tensor_name(name), data_torch)]

@ModelBase.register("KimiLinearModel", "KimiLinearForCausalLM")
class KimiLinearModel(TextModel):
"""Kimi-Linear model with hybrid MLA+KDA architecture"""
model_arch = gguf.MODEL_ARCH.KIMI
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
model_arch = gguf.MODEL_ARCH.KIMI
model_arch = gguf.MODEL_ARCH.KIMI_LINEAR

_experts: list[dict[str, Tensor]] | None = None

def set_gguf_parameters(self):
self.gguf_writer.add_vocab_size(self.hparams["vocab_size"])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.gguf_writer.add_vocab_size(self.hparams["vocab_size"])
super().set_gguf_parameters()
self.gguf_writer.add_vocab_size(self.hparams["vocab_size"])

Comment on lines +5131 to +5139
# Use find_hparam for context length
# Kimi uses model_max_length
n_ctx = self.find_hparam(["max_position_embeddings", "model_max_length", "n_ctx", "n_positions"], optional=True)
if n_ctx is not None:
self.gguf_writer.add_context_length(n_ctx)
else:
return [(self.map_tensor_name(name), data_torch)]
# Default to 4096 if not found
logger.warning("No context length found in config, defaulting to 4096")
self.gguf_writer.add_context_length(4096)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add model_max_length to TextModel.set_gguf_parameters instead, the fallback is not necessary.

@cacaview
Copy link
Author

I have fixed these errors in the commit at cacaview@780dd78

@CISC
Copy link
Collaborator

CISC commented Nov 30, 2025

I have fixed these errors in the commit at cacaview@780dd78

Please address the remaining unresolved ones as well.

@cacaview
Copy link
Author

cacaview commented Dec 1, 2025

I conducted some simple tests and encountered some issues. The root causes are still unclear.

Test Environment

  • Model: E:\llama\Kimi-Linear-48B-A3B-Instruct\Kimi-Linear-48B-A3B-Instruct-Q4_K_M.gguf

  • Quantization: Q4_K_M (27.65 GiB, 4.83 BPW)

  • GPU: NVIDIA GeForce RTX 4070 (12GB VRAM)

  • Parameters: --temp 0 -ngl 99 -no-cnv --no-warmup

  • Prompt Format: user: <question>\nassistant:

Test Logs

Test 1: Simple Greeting


user: Hello!

  

assistant: Hello! How can I help you today? [end of text]

  

common_perf_print: prompt eval time =   10517.48 ms /    23 tokens (  457.28 ms per token,     2.19 tokens per second)

common_perf_print:        eval time =    8790.42 ms /     9 runs   (  976.71 ms per token,     1.02 tokens per second)

common_perf_print:        total time =   19359.92 ms /    32 tokens

Test 2: Simple Math


user: What is 25 + 37?

  

assistant: Let's add the numbers step by step:

- 25 + 25 = 50

  

So the answer is **50**. [end of text]

  

common_perf_print: prompt eval time =    1644.97 ms /    33 tokens (   49.85 ms per token,    20.06 tokens per second)

common_perf_print:        eval time =    4486.75 ms /    25 runs   (  179.47 ms per token,     5.57 tokens per second)

common_perf_print:        total time =    6195.25 ms /    58 tokens

Incorrect calculation: 25 + 37 = 62, not 50.

Test 3: Knowledge Q&A


user: What is the capital of France?

  

assistant: The capital of France is Paris. [end of text]

  

common_perf_print: prompt eval time =    1561.97 ms /    47 tokens (   33.23 ms per token,    30.09 tokens per second)

common_perf_print:        eval time =    1179.26 ms /     7 runs   (  168.47 ms per token,     5.94 tokens per second)

common_perf_print:        total time =    2761.82 ms /    54 tokens

Test 4: Chinese Test


user: 请用中文介绍一下你自己

  

assistant: I see that you've copied the entire text. This is a very important message. I will now...

Chinese input encountered encoding issues in PowerShell, and the model failed to process Chinese correctly.

Test 5: Code Generation


user: Write a Python function to calculate factorial

  

assistant: Here's a simple and efficient implementation using recursion:

  

python

def factorial(n):

    if n == 0:

        return 1

    else:

        return n * factorial(n - 1)

  

# Example usage

print(factorial(5))  # Output: 120

  

---

### Explanation:

- **Base Case**: The function checks if `n` is 0, in which case it returns 1.

- **Recursive Case**: It calls itself with `n-1` to compute the factorial.

  

This is a classic example of a recursive function. [end of text]

  

common_perf_print: prompt eval time =    2516.11 ms /    63 tokens (   39.94 ms per token,    25.04 tokens per second)

common_perf_print:        eval time =   20525.99 ms /   117 runs   (  175.44 ms per token,     5.70 tokens per second)

common_perf_print:        total time =   23294.02 ms /   180 tokens

Test 6: Concept Explanation (Repetitive Output)


user: Explain the concept of recursion in programming with an example.

  

assistant:# The user is asking for an explanation of recursion in programming. The user is asking for a simple explanation of recursion.

  

The user is asking: "What is recursion?" or "What is recursion?" or "What is recursion?" or "What is recursion?" or " " " " " " " " " " " " " " " " " " " "

Severe repetitive output issue occurred.

Test 7: Logical Reasoning


user: If all roses are flowers and some flowers fade quickly, can we conclude that some roses fade quickly?

  

assistant: Yes, we can conclude that some roses fade quickly. Let's break it down step by step.

  

1. **Understanding the Problem**:  

  

   - **Premise**: All roses are flowers.  

  

   - **Conclusion**: Some roses are flowers.  

  

2. **Logical Form**:  

  

   - **Premise**: All roses are flowers.  

  

   - **Conclusion**: Therefore, roses are flowers.  

  

3. **Alternative Approach**:  

  

   - **Logical Form**:  

  

     - **

  

common_perf_print: prompt eval time =    4170.31 ms /   118 tokens (   35.34 ms per token,    28.30 tokens per second)

common_perf_print:        eval time =   16314.01 ms /    99 runs   (  164.79 ms per token,     6.07 tokens per second)

common_perf_print:        total time =   20693.48 ms /   217 tokens

Incorrect logical reasoning. The correct answer should be "Cannot be determined".

@engrtipusultan
Copy link

I conducted some simple tests and encountered some issues. The root causes are still unclear.

Test Environment

* Model: `E:\llama\Kimi-Linear-48B-A3B-Instruct\Kimi-Linear-48B-A3B-Instruct-Q4_K_M.gguf`

* Quantization: Q4_K_M (27.65 GiB, 4.83 BPW)

* GPU: NVIDIA GeForce RTX 4070 (12GB VRAM)

* Parameters: `--temp 0 -ngl 99 -no-cnv --no-warmup`

* Prompt Format: `user: <question>\nassistant:`

Test Logs

Test 1: Simple Greeting


user: Hello!

  

assistant: Hello! How can I help you today? [end of text]

  

common_perf_print: prompt eval time =   10517.48 ms /    23 tokens (  457.28 ms per token,     2.19 tokens per second)

common_perf_print:        eval time =    8790.42 ms /     9 runs   (  976.71 ms per token,     1.02 tokens per second)

common_perf_print:        total time =   19359.92 ms /    32 tokens

Test 2: Simple Math


user: What is 25 + 37?

  

assistant: Let's add the numbers step by step:

- 25 + 25 = 50

  

So the answer is **50**. [end of text]

  

common_perf_print: prompt eval time =    1644.97 ms /    33 tokens (   49.85 ms per token,    20.06 tokens per second)

common_perf_print:        eval time =    4486.75 ms /    25 runs   (  179.47 ms per token,     5.57 tokens per second)

common_perf_print:        total time =    6195.25 ms /    58 tokens

Incorrect calculation: 25 + 37 = 62, not 50.

Test 3: Knowledge Q&A


user: What is the capital of France?

  

assistant: The capital of France is Paris. [end of text]

  

common_perf_print: prompt eval time =    1561.97 ms /    47 tokens (   33.23 ms per token,    30.09 tokens per second)

common_perf_print:        eval time =    1179.26 ms /     7 runs   (  168.47 ms per token,     5.94 tokens per second)

common_perf_print:        total time =    2761.82 ms /    54 tokens

Test 4: Chinese Test


user: 请用中文介绍一下你自己

  

assistant: I see that you've copied the entire text. This is a very important message. I will now...

Chinese input encountered encoding issues in PowerShell, and the model failed to process Chinese correctly.

Test 5: Code Generation


user: Write a Python function to calculate factorial

  

assistant: Here's a simple and efficient implementation using recursion:

  

python

def factorial(n):

    if n == 0:

        return 1

    else:

        return n * factorial(n - 1)

  

# Example usage

print(factorial(5))  # Output: 120

  

---

### Explanation:

- **Base Case**: The function checks if `n` is 0, in which case it returns 1.

- **Recursive Case**: It calls itself with `n-1` to compute the factorial.

  

This is a classic example of a recursive function. [end of text]

  

common_perf_print: prompt eval time =    2516.11 ms /    63 tokens (   39.94 ms per token,    25.04 tokens per second)

common_perf_print:        eval time =   20525.99 ms /   117 runs   (  175.44 ms per token,     5.70 tokens per second)

common_perf_print:        total time =   23294.02 ms /   180 tokens

Test 6: Concept Explanation (Repetitive Output)


user: Explain the concept of recursion in programming with an example.

  

assistant:# The user is asking for an explanation of recursion in programming. The user is asking for a simple explanation of recursion.

  

The user is asking: "What is recursion?" or "What is recursion?" or "What is recursion?" or "What is recursion?" or " " " " " " " " " " " " " " " " " " " "

Severe repetitive output issue occurred.

Test 7: Logical Reasoning


user: If all roses are flowers and some flowers fade quickly, can we conclude that some roses fade quickly?

  

assistant: Yes, we can conclude that some roses fade quickly. Let's break it down step by step.

  

1. **Understanding the Problem**:  

  

   - **Premise**: All roses are flowers.  

  

   - **Conclusion**: Some roses are flowers.  

  

2. **Logical Form**:  

  

   - **Premise**: All roses are flowers.  

  

   - **Conclusion**: Therefore, roses are flowers.  

  

3. **Alternative Approach**:  

  

   - **Logical Form**:  

  

     - **

  

common_perf_print: prompt eval time =    4170.31 ms /   118 tokens (   35.34 ms per token,    28.30 tokens per second)

common_perf_print:        eval time =   16314.01 ms /    99 runs   (  164.79 ms per token,     6.07 tokens per second)

common_perf_print:        total time =   20693.48 ms /   217 tokens

Incorrect logical reasoning. The correct answer should be "Cannot be determined".

@CISC is this valid method to check correctness of models implementation?
Also for llama.cpp even if the following is used, doesn't default values of top p and k add variance in response.
Parameters: --temp 0 -ngl 99 -no-cnv --no-warmup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning model Model specific Nvidia GPU Issues specific to Nvidia GPUs python python script changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants