Deep C++ Integration & RoPE Memory Fixfor Instella-Math on Ryzen AI (LocalGGUF)

  1. Abstract: The release of AMD’s Instella
    models brings advanced reasoning capabilities to open-source developers. While standard GGUF conversion scripts can partially process the model,executing
    Instella-3B-Math locally on llama.cpp often leads to silent memory failures orassertion crashes (specifically GGML_ASSERT((mode & 1) == 0)).
    This report documents the methodology for reverse-engineering the Python quantization pipeline and patching the C++ inference engine to achieve native, error-free execution of Instella-3B-Math on consumer-grade Ryzen AI hardware.
  2. Phase 1: Python Tensor Mapping: Instella utilizes specific normalization layers (pre_attention_layernorm and pre_feedforward_layernorm). Standard gguf-py conversion pipelines fail to map these outof the box, throwing a ValueError during serialization.
    Implementation: i injected explicit Regex rules into the global tensor mapping configuration prior to conversion: ```python from gguf.tensor_mapping importTensorNameMap import gguf
    TensorNameMap.block_mappings_cfg[gguf.MODEL_TENSOR.ATTN_NORM] += (“model.layers.{bid}.pre_attention_layernorm”, ) TensorNameMap.block_mappings_cfg[gguf.MODEL_TENSOR.FFN_NORM] += (“model.layers.{bid}.pre_feedforward_layernorm”, ) ``` This successfully yielded an optimizedFP16 GGUF file.
  3. Phase 2: C++ Architectural Registration: To de couple Instella’s execution graph from standard LLaMA models, i explicitly registered a new architecture in the C++ backend.
    Modifications in src/models/models.h : cpp enum llm_arch { LLM_ARCH_LLAMA,LLM_ARCH_INSTELLA, // ← Added Architecture // … }; subsequently bound this architecture in the llama-model.cpp switch statement to ensure proper tensor allocation during runtime.
  4. Phase 3: Resolving Rotary Positional Embeddings(RoPE) Assertions: During context generation, the engine collapsed, returning a fatal GGML_ASSERT inside ggml.c. Because LLM_ARCH_INSTELLA lacked an explicit RoPE type declaration, the engine defaulted to
    LLAMA_ROPE_TYPE_NONE (-1). At the bitwise level, passing -1 into ggml_rope_ext() triggered the memory assertion.
    Patch in src/llama-model.cpp : i bypassed the crash by forcing the standardnormalization allocation: cpp llama_rope_type llama_model_rope_type(const llama_model *model) { switch (model->arch) { // … case LLM_ARCH_LLAMA: case LLM_ARCH_LLADA: caseLLM_ARCH_LLAMA4: case LLM_ARCH_INSTELLA: // ← Patch: Forced NORM allocation caseLLM_ARCH_DECI: return LLAMA_ROPE_TYPE_NORM; // …
  5. Local Hardware Metrics: After rebuilding the binary via CMake (cmake --build build --config Release -j 8), the model was successfully loaded into local memory. The following metrics were captured during interactive CLI inference: Processor:
    AMD Ryzen AI 7 350 Model Parameters: 3 Billion (FP16),
    Prompt Evaluation Speed: 3.8 tokens/s
    Generation Speed: 2.1 tokens/s.
    Conclusion
    By executing these Python regex injections and C++ memory patches, i successfully stabilized the Instella-3B-Math model for high-performance local environments. I provide these findings to the AMD developer community to facilitate native Ryzen AI implementations of the Instella family without relying on cloud-based GPU instances.

Hi, that is a really clever fix for the RoPE assertion crash, thanks for sharing with us. Though I noticed that your generation speed (2.1 t/s) is very slow for a 3B model because you are running it in raw, unquantized FP16. Now that your tensor mapping and C++ registration patches are working, you should run llama-quantize to convert the model to Q4_K_M or Q8_0. That will shrink the model size and let the engine use optimized MMQ kernels, which should push your speed past 30+ t/s on your Ryzen AI chip.

The speed I mentioned earlier was measured while I was actively troubleshooting the crash. Since then, I’ve implemented further optimizations, and the model is now running at 42–45 tokens per second. I’m continuing to tune the engine and expect even better results soon.

wow those are really good numbers, good job, keep up the pace.

1 Like