Repository navigation
[DRAFT] Support for Zaya1 8B model - #23112
Juste-Leo2 wants to merge 36 commits into
Conversation
|
Wanted to give this a try but I guess the Vulkan backend needs some more work for this, or is this unexpected? |
OK, after some trouble, I think I've found the cause. It was a type error; im2col requires f16 for the conversion when using CPU backends and the like. With CUDA, it doesn't check the kernel's internal data during the conversion only the dimensions which is why it worked correctly on CUDA. |
|
I wanted to give a try to the Zaya model with my 9060 XT and ROCM 7.1, I compiled the MTP version of llama and backported your branch. Got some issues such as: ggml.c:3647: GGML_ASSERT(ggml_is_contiguous(a)) failed This is my setup. I used AI to fix it, it made a few changes in zaya.cpp, here's the unified diff: It works so far but the token generation is low (~15 t/s) compared to a Qwen 35B-A3B (60t/s), I was expecting much more from this model considering the small amount of active params. |
Thanks for the catch! I was also unsure about the speed during implementation, and there are a few reasons for it:
I just pushed a commit with only the necessary ggml_cont calls, I think the AI added a few extras out of caution that weren't actually needed and would have added unnecessary memory copies. Could you try again and let me know how it goes @kdrapelinexto ? Thanks ! |
Ports PR ggml-org#22833 and PR ggml-org#23112 from ggml-org/llama.cpp onto our fork. - ggml: add ggml_conv_1d_grouped op (depthwise + headwise conv via ggml_view_3d slicing, falls back to existing conv1d/dw for groups=1 and groups=IC) - gguf: register ZAYA arch, CCA_VAL_PROJ1/2, CCA_CONV_GRP, CCA_K_SCALE, RES_SCALE_HS/RES/FINAL, ZAYA_ROUTER_MLP2/4/BIASES/EDA_SCALE tensors - src: add llama_model_zaya with alternating CCA (even) and MoE (odd) layers; residual scaling at every layer and final norm - conversion/zaya.py: HF→GGUF converter for ZayaModel/ZayaForCausalLM - Includes ggml_cont fixes for ROCm non-contiguous tensor compatibility and F16 cast fixes for CPU backend (from Zyphra fork review) Markovian RSA (test-time compute method) is intentionally excluded and will be a separate implementation. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This works now, thanks! I've tested both f16 and Q8_0. |
|
@Juste-Leo2 Compiled right now, I confirm it works and the perf is 2x, about 30-35t/s. Interestingly, it mixed some Italian with the French output: "identifie les sous-lesvers sensibles (weak keys), les compromissions de sous-chiave". (chiave = clé).
|
|
I performed Perplexity tests to validate the full pipeline and check for any potential data loss or tensor conversion defects. Note: The Hugging Face reference baseline was calculated using the official Zyphra fork of transformers .
Observations:
These tests therefore seem to validate the current pipeline. Note: |
|
To quickly explain the changes made to support llama-server: |
|
@Juste-Leo2 as for correctness, you should probably just run |
|
BTW thanks for doing this, I was tempted to attempt this but I figured out I have enough to do and this architecture is a bit crazy :) |
|
Thank you, I put Q6_K on my Pi5 16gb, resulted 6.5~6.9 TPS generation. A little disappointed but it's a good sub of Gemma4-e2b-it-IQ4_NL since ZAYA1 is a lot smarter! |
I had some trouble running the test initially due to a vocabulary size mismatch. The PyTorch config included some extra unused padding tokens (PyTorch vocab: 262272 vs llama.cpp vocab: 262147). Since this doesn't impact inference for current GGUFs, I made a commit to explicitly align with the Here are the detailed results of the verification: 🔍 Token & Logits Verification✅ Match: All 6 tokens match between PyTorch and llama.cpp. Here are the raw logits for the top-10 predictions:
📈 NMSE Metrics
❌ Final Verdict & ContextThe script returned an error ( Given that the NMSE is just slightly above the threshold but the top-10 tokens are aligned, @pwilkin what do you think? Is this sufficient to validate the test, or is further investigation required regarding this gap? |
|
Yeah, does look like a bug. You should probably dump intermediate tensors at this stage to see where the divergence starts. |
Thanks for the feedback! I'll investigate this during the week to find where the divergence starts. |
|
I had some time to run tests with opencode. I thought it would be nice to map it out with a Python script, so here are the results for each layer in BF16.
I noticed we have spikes on the odd layers. A few examples:
To try and fix this, I re-analyzed things against Zyphra's official VLLM fork (which I based this on), specifically looking at the MoE logic (since the odd layers are MoE layers). We have the exact same implementation for the final Residual scaling, as well as for the EDA (Exponential Decay Averaging) and MoE gate/up/down logic. I also tested disabling this EDA: the NMSE exploded to 5.72e-01 (46x worse), which confirms that it is indeed reducing the drift rather than causing it. I thought it might be a routing precision issue, so I switched the softmax from bf16 to f32 (line 382 in So after all these tests, I finally tried running everything in full F32 (which I should have done first 😅), and it passed the logit test! Here are the results: 📈 METRICS
==============================
MSE (Mean Squared Error): 2.335141e-02
Reference Variance: 6.342938e+00
NMSE: 3.681481e-03
Max Absolute Error: 0.798369
Mean Absolute Error: 0.117253
NMSE (dB): -24.34 dB
🎯 INTERPRETATION
==============================
👍 Good match
📋 GUIDANCE
==============================
👍 GOOD: Your GGML conversion is working well.
Small differences are likely due to precision/quantization.
📚 NMSE BENCHMARKS
==============================
< 1e-6: Essentially identical
< 1e-4: Excellent (typical for good conversions)
< 1e-3: Very good
< 1e-2: Good (acceptable for most use cases)
< 0.1: Acceptable (may need verification)
> 1.0: Poor (worse than random)
✅ RESULT: PASS (NMSE = 3.68e-03)
Here is the graph with all the layers in F32:
On this graph, I still observe a noticeable MSE increase at the end. In F32, layers 0-72 are correct, but I saw a sharp increase after that. I think this is probably due to the accumulation from the previous layers, and probably not a structural bug. So, given that F32 passes the logit test (NMSE = 3.68e-03, "Very good") and BF16 just barely fails due to this accumulating precision loss, the model's architecture seems consistent. @pwilkin pinging you again with these new observations. Is it worth investigating further, or is this explanation sufficient to validate the architecture? |
That probably means that more tensors need less strong quantization for this model?
Seems still useful to try to understand where this exactly comes from if the pytorch implementation on the very same model doesn't show this behaviour. I have no idea but this feels like something is going wrong somewhere |
Thanks for the feedback! Since I'm still getting used to interpreting the exact impact of quantization with these tools, I'm still a bit hesitant to draw definitive conclusions. |
|
Sorry, I'll take a look when I'm able - from my intuition, if it doesn't validate at BF16, then that suggests something is still wrong, but I'd have to look at the intermediate tensors themselves to make an informed opinion. |
2b0c8c8 to
1fc4581
Compare
|
I added comments to compare the vllm implementation with zyphra's fork (zaya.py and CCA.py) Note: For the logit test, I’m using zyphra’s transformer fork. So my next step is to compare it with this fork to see if there are any differences there as well. I also wanted to clarify that the test showing the graph with the layers was a cumulative test, not a test with separate, independent layers. |
|
After hours of debugging, I think I've finally found the root cause of the issue! For the I put together a diagram (thanks opencode) to illustrate exactly what happens: BEFORE (NMSE 1.23e-02) AFTER (NMSE 3.94e-03)
QK_dw [7,1280,1] F32 QK_dw [7,1280,1] F32
│ │
▼ ▼
ggml_conv_1d_grouped(10) ggml_conv_1d_grouped(10)
│ │
┌────┴────┐ ┌────┴────┐
│ group 0 │ ... │ group 0 │ ...
│ slice │ │ slice │
│ [7,128] │ F32 │ [7,128] │ F32
│ │ │ │ │ │
│ ▼ │ │ ▼ │
│ im2col │ │ im2col │
│ F16 ⚠️ │← precision loss │ F32 ✅ │← exact
│ -1.191 │ │ -1.191 │
│ →-1.194 │ │ →-1.191 │
│ │ │ │ │ │
│ ▼ │ │ ▼ │
│ mul_mat │ F16×F32 │ mul_mat │ F32×F32
│ │ │ │ │ │
│ 0.818 │← err amplified │ 0.818 │✓
│→0.750 │ δ=0.068 │→0.818 │ δ≈0
└────┬────┘ └────┬────┘
│ │
┌────┴────┐ ┌────┴────┐
│ concat │ │ concat │
│ groups │ │ groups │
└────┬────┘ └────┬────┘
│ │
┌────┴────┐ ┌────┴────┐
│ attn + │ err amplified │ attn + │ minimal
│ rest │ by later layers │ rest │ error
│ │ │ │
│logits │ NMSE 1.23e-02 │logits │ NMSE 3.94e-03
└─────────┘ └─────────┘
Given these results, I think the best path forward is to revert the recent adjustments and go back to the state just after this commit. The subsequent commits didn't bring any improvements (they mainly documented the code and added elements that are likely handled implicitly). We can discuss if we want to keep a few specific commits, but the metrics clearly point to this Note that this means the test works perfectly in f16: 📈 METRICS
==============================
MSE (Mean Squared Error): 2.430202e-02
Reference Variance: 6.339689e+00
NMSE: 3.833313e-03
Max Absolute Error: 0.842108
Mean Absolute Error: 0.121947
NMSE (dB): -24.16 dB
🎯 INTERPRETATION
==============================
👍 Good match
📋 GUIDANCE
==============================
👍 GOOD: Your GGML conversion is working well.
Small differences are likely due to precision/quantization.
📚 NMSE BENCHMARKS
==============================
✅ RESULT: PASS (NMSE = 3.83e-03)but not in BF16 : 📈 METRICS
==============================
MSE (Mean Squared Error): 7.868546e-02
Reference Variance: 6.339689e+00
NMSE: 1.241156e-02
Max Absolute Error: 1.791535
Mean Absolute Error: 0.202144
NMSE (dB): -19.06 dB
🎯 INTERPRETATION
==============================
⚠️ Acceptable match
📋 GUIDANCE
==============================
⚠️ ACCEPTABLE: Conversion is working but with some differences.
Check if you're using quantization (Q4, Q8, etc.)
Test generation quality to see if it's acceptable.
📚 NMSE BENCHMARKS
==============================
❌ RESULT: NEEDS REVIEW (NMSE = 1.24e-02) |
|
I found the fix! We simply need to allocate im2col dynamically: F32 for unsupported types (like BF16 or quantized) and F16 for native F16 models. This keeps F16 hardware-optimized while allowing BF16/quantized models to use precise F32 math without backend crashes. Here are the final passing results for both: BF16 : 📈 METRICS
==============================
MSE (Mean Squared Error): 2.556950e-02
Reference Variance: 6.339689e+00
NMSE: 4.033242e-03
Max Absolute Error: 0.862177
Mean Absolute Error: 0.122009
NMSE (dB): -23.94 dB
✅ RESULT: PASS (NMSE = 4.03e-03) |
Add detailed inline comments mapping each C++ code section to the corresponding zaya.py and cca.py Python lines, including code snippets for direct comparison.
zaya.py L294-296: EDA is disabled for layer 1 (first MoE layer) via (self.layer_number != zaya_first_layer). Add il != 1 guard to match.
… _FP32EmbeddingMethod
Correct line reference from zaya.py L387-389 to L459-469, and add note explaining why excluding the skip expert from gate_probs is correct (bias=-1.0 makes it effectively never selected at inference with topk=1).
|
Following the discussion in PR #22833 with am17an, I moved the grouped convolution logic into the Zaya PR, since it's the only LLM that uses it. In the future, it would be worth adding the ability to pass the number of groups as a parameter to the convolution operation (like PyTorch does), but that would require reworking the backends. Observations from my tests: grouped convolution in BF16 tends to slow down token generation compared to quantization. Since the model doesn't fit in VRAM, I couldn't fully measure this. For the BF16 version, I prioritized precision over speed (the im2col bottleneck was partially addressed by #23660).
I ran a logit comparison to verify that inference is working correctly (note: PyTorch logits are based on Zyphra's transformers fork at Zyphra, see commit db1db76). Conversion Metrics
I also ran a perplexity test on wikitext-2, but the dataset is too small and the results were too noisy (showing better perplexity with stronger quantization). I'm not including them here. Based on the logit comparison results, I believe this is ready to move from draft to a proper PR and request reviews. Notifications will go out to Georgy and CISC (hope your vacation went well !). CC @pwilkin for your input. Thanks to the maintainers and contributors for their help, especially on the intermediate PRs :) And thanks to the community for helping me fix all the issues I encountered! |
|
Hello, This PR has been inactive for a while now. I believe it's not a priority for the maintainers (which I completely understand). Notably, it is missing the fundamental building block for grouped 1D convolution operations on each backend, which requires a significant amount of development. Currently, this grouped convolution function is hardcoded and only for Zaya, in fact, it's even located directly in the model file, which is not ideal. So I'd rather close this PR for now. If one day the convolution operations are updated across the backends to support groups, or if an architecture similar to Zaya emerges (Zaya is one of a kind), I might pick this PR back up or open a new one. If you'd like to use Zaya, the branch will remain available in my repository: https://github.com/Juste-Leo2/llama.cpp/tree/Zaya1 Wishing you all a great day. The open source world moves fast! |
|
Will post a new PR addressing these comments and updated architecture for the updated model release. |



Overview
This PR adds support for the Zaya1 8B model (without Markovian RSA). (see issue #22776)
Note: This draft depends on PR #22833, hence the choice of opening a draft PR to be able to update the tree based on the requested changes.
Zaya is a hybrid recurrent/attention model. It consists of a succession of classic MoE layers and convolution-based CCA layers.
The goal of CCA is to substitute classic attention. From what I understand, the process is:
The researchers of the Zaya model also used an attention projection on the previous token.
Additional information
Heavily based on the vLLM implementation.
For context, I initially started this port on my own fork. I later got stuck on an inference issue, and the work was moved to a separate branch on the Zyphra repository where @nanduruganesh greatly helped unblock the situation (see Zyphra PR #1 and Zyphra PR #2). I then took care of the refactoring and various fixes (using OpenCode) to propose this clean version upstream. A huge thanks to him for his crucial help!
Regarding inference with an RTX 4070 Ti and 64GB of RAM:
In BF16
Q4_K_M
Requirements
I am more than willing to do my best to answer maintainers' questions about the architecture or anything else.