Files
diffuse-cpp/src/diffuse-common.h
T
iafiscal1212andClaude Opus 4.6 6022454e29 Initial scaffold: diffuse-cpp inference engine for Diffusion LLMs
- Project structure with CMake build system, GGML submodule
- Core library: model loader (GGUF), transformer forward pass, diffusion sampler
- CLI tools: diffuse-cli (generation) and diffuse-bench (benchmarks)
- Converter: convert-llada.py (SafeTensors → GGUF with tensor name mapping)
- Tests: unit tests, converter roundtrip test, end-to-end forward pass test
- Bidirectional attention (no causal mask, no KV cache)
- SwiGLU FFN, RoPE, RMSNorm — full LLaDA-8B Llama backbone
- Verified: compiles clean, forward pass produces valid logits on tiny model

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-18 21:43:36 +01:00

74 lines
2.7 KiB
C++

#pragma once
#include "diffuse.h"
#include "ggml.h"
#include "gguf.h"
#include "ggml-backend.h"
#include "ggml-cpu.h"
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <memory>
#include <cmath>
// ── Logging ────────────────────────────────────────────────────
#define DIFFUSE_LOG(fmt, ...) fprintf(stderr, "[diffuse] " fmt "\n", ##__VA_ARGS__)
#define DIFFUSE_DIE(fmt, ...) do { \
fprintf(stderr, "[diffuse] FATAL: " fmt "\n", ##__VA_ARGS__); \
exit(1); \
} while(0)
// ── Tensor name helpers ────────────────────────────────────────
static inline std::string fmt_layer(const char * pattern, int i) {
char buf[128];
snprintf(buf, sizeof(buf), pattern, i);
return buf;
}
// ── Per-layer weight struct ────────────────────────────────────
struct diffuse_layer {
// Attention
struct ggml_tensor * attn_norm; // RMSNorm weight
struct ggml_tensor * wq; // Q projection
struct ggml_tensor * wk; // K projection
struct ggml_tensor * wv; // V projection
struct ggml_tensor * wo; // output projection
// FFN (SwiGLU)
struct ggml_tensor * ffn_norm; // RMSNorm weight
struct ggml_tensor * ffn_gate; // gate projection (w1)
struct ggml_tensor * ffn_up; // up projection (w3)
struct ggml_tensor * ffn_down; // down projection (w2)
};
// ── Full model struct ──────────────────────────────────────────
struct diffuse_model {
diffuse_hparams hparams;
// Embeddings
struct ggml_tensor * tok_embd; // token embeddings
struct ggml_tensor * output_norm; // final RMSNorm
struct ggml_tensor * output; // lm_head (may be tied to tok_embd)
// Layers
std::vector<diffuse_layer> layers;
// GGML backend
ggml_backend_t backend = nullptr;
ggml_backend_buffer_t buf = nullptr;
struct ggml_context * ctx = nullptr; // weight context
};
// ── Compute context ────────────────────────────────────────────
struct diffuse_context {
const diffuse_model * model;
int n_ctx;
int n_threads;
ggml_backend_t backend = nullptr;
ggml_backend_buffer_t buf = nullptr;
struct ggml_context * ctx = nullptr; // compute context
};