Breaking the Silicon Barrier: Hacking AMD’s Instella-3B into llama.cpp for HFT

  1. The Context & The Bottleneck
    I was building an AI-driven High-Frequency Trading (HFT) bot (“WE Trading”) powered by the Instella-3B model. My initial setup used HuggingFace transformers and
    PyTorch running through a Python wrapper (llm_engine.py), which was then bridged to my Node.js application.
    The result? Absolute disaster.
    Due to Python’s Global Interpreter Lock (GIL) and the massive overhead of mapping 16-bit precision tensors in VRAM, the AI was thinking in “slow motion.” It generated
    around 15 tokens per second. In the HFT world, this meant the bot was virtually blind. It would miss critical first-minute market operations simply because the AI was still
    computing its response. The latency alone guaranteed a loss before the trade even began.
    I needed the raw, bare-metal speed of C++. I needed llama.cpp with Vulkan acceleration.
  2. The Architecture Wall (AMD’s Custom Tensors)
    I attempted a standard conversion using convert_hf_to_gguf.py, but the compiler violently crashed:
    ValueError: Missing <MODEL_TENSOR.ATTN_K_NORM: 53> for MODEL_TENSORS of <MODEL_ARCH.LLAMA: 2>
    Upon inspecting the safetensors, I discovered that AMD didn’t just use a vanilla LLaMA architecture. They injected custom normalization layers (q_norm and k_norm)
    into the attention blocks. The strict GGUF specification for LLAMA expects exactly 9 tensors per block (query, key, value, output, gate, up, down, input_norm,
    post_attention_norm). Instella-3B had 11.
    The C++ engine panicked to prevent memory corruption and aborted the quantization.
  3. The Hack: Tensor Spoofing & Architecture Forgery
    I couldn’t just delete q_norm and k_norm without destroying the model’s mathematics. Instead, I had to trick both the Python converter and the C++ engine into natively
    accepting these tensors.
    Here is exactly how I pulled it off for anyone trying to replicate this on forums:
    Step A: Mapping the Tensors to Olmo2
    I realized that the Olmo2ForCausalLM architecture natively expects q_norm and k_norm in the llama.cpp codebase. So, I wrote a Python script to physically modify the
    .safetensors on disk, translating the LLaMA layer names into the exact nomenclature expected by Olmo2.
    import torch
    from safetensors.torch import load_file, save_file
    import json
    model_path = “model.safetensors”
    tensors = load_file(model_path)
    new_tensors = {}
    for k, v in tensors.items():
    new_k = k

Spoofing the layernorms to match Olmo2 expectations

if “input_layernorm” in new_k:
new_k = new_k.replace(“input_layernorm”, “post_attention_layernorm”)
elif “post_attention_layernorm” in new_k and “input_layernorm” not in k:
new_k = new_k.replace(“post_attention_layernorm”, “post_feedforward_layernorm”)
new_tensors[new_k] = v
save_file(new_tensors, model_path)

Forcing the configuration header

with open(“config.json”, “r”) as f:
c = json.load(f)
c[“architectures”] = [“Olmo2ForCausalLM”]
with open(“config.json”, “w”) as f:
json.dump(c, f, indent=2)
Step B: The 8-bit Quantization
With the architecture successfully forged, convert_hf_to_gguf.py parsed exactly 399 tensors flawlessly. I compiled it down to 8-bit precision (q8_0), shrinking the 15GB
monstrosity into a highly optimized 3.31 GB .gguf executable block.
python convert_hf_to_gguf.py “Instella-3B-Fused” --outfile “Instella-Trading-3B.gguf” --outtype q8_0
Step C: Native API Translation
Finally, I destroyed the Python middleman. I booted llama-server.exe using Vulkan to offload all 36 layers directly to my AMD Radeon GPU.
I patched my Node.js bot to shoot raw OpenAI-compatible REST payloads directly to the C++ server running on localhost:8000.
const options = {
hostname: ‘127.0.0.1’,
port: 8000,
path: ‘/v1/chat/completions’,
method: ‘POST’,
headers: { ‘Content-Type’: ‘application/json’ }
};
// Payload sent directly to C++ memory
4. The Result: Institutional Speed
The transformation is staggering. The bot’s AI no longer causes guaranteed first-minute losses. We pushed the inference speed from ~15 t/s to +100 tokens per second.
The AI now maps the TradingView DOM, analyzes the order flow, and executes decisions in milliseconds. The bottleneck has been entirely eradicated. From this point
forward, any losses incurred by the bot will be standard market risks (external actors, black swans, algorithmic collision) and absolutely not a failure of the AI’s reaction time.
We brought institutional, bare-metal HFT capabilities to a consumer machine.

Hello, if you are serious about HFT and want to save every single millisecond, you should drop the local HTTP REST server. Instead, compile llama.cpp as a library and write a native Node.js addon (N-API) to run inference in-process, which will bypass all the JSON and HTTP socket overhead and save you another 2–3ms per trade. But this is a very clever piece of graph surgery as well. Thanks for sharing.

1 Like

Hello, I’m sharing that by doing what I posted, I managed to increase the speed from 42–45 tokens per second (t/s) to 180–190 t/s. It was a formidable change; in fact, I’m running everything in real-time. I make adjustments and continuously check what works and what doesn’t. I’ll keep your recommendation in mind and try it out in a few days. I’ll let you know if it works for me. Thank you!

1 Like