Skip to content
项目

关于

Machine learning framework written in C.

人工智能与机器学习

这些数字如何统计

可以做什么

  • Layers - 28 layers: Linear, Conv1d/2d/3d, ConvTranspose, RNN, LSTM, GRU, Transformer, BatchNorm, LayerNorm, RMSNorm, pooling, Dropout, PixelShuffle
  • Training - 11 optimizers · 13 losses · 8 LR schedulers · gradient checkpointing · DDP · pipeline and tensor parallelism
  • LLMs - LLaMA 7B–70B · LoRA / QLoRA · Flash Attention · GQA · ALiBi · RoPE scaling · paged and sliding-window KV cache · MoE · ring attention · speculative decoding · continuous-batching server
  • Compiler - IR with 5 fusion patterns · linearization · codegen to C, PTX, SPIR-V, WGSL and MSL · AOT and JIT · kernel cache

实测星标增长

24小时
0
7天
+1
15天
+2
1个月
+3

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

C-ML: machine learning in pure C

CI fuzz & sanitizers wheels website

Release C11 Python 3.8+ Platforms Zero dependencies License Stars Last commit

Website · Getting started · API reference · Python · Examples · Releases

C-ML is a machine learning library written from scratch in C11. It covers tensors, autograd, 28 layers and the optimizers to train them, a compiler that fuses ops and emits GPU kernels (PTX, SPIR-V, WGSL, MSL), distributed training, LLM inference, and model I/O for GGUF, ONNX and SafeTensors. It has no required dependencies.

Quick start

Build the library, tests and examples:

git clone https://github.com/jaywyawhare/C-ML.git
cd C-ML && mkdir -p build && cd build
cmake -DBUILD_EXAMPLES=ON -DBUILD_TESTS=ON .. && make -j$(nproc)

Fit y = 2x + 1 with one linear layer:

#include "cml.h"
#include <stdio.h>

int main(void) {
    cml_init();

    float xs[8], ys[8];
    for (int i = 0; i < 8; i++) {
        xs[i] = i / 7.0f;
        ys[i] = 2.0f * xs[i] + 1.0f;
    }
    Tensor* x = cml_tensor_2d(xs, 8, 1);
    Tensor* y = cml_tensor_2d(ys, 8, 1);

    Module* model = (Module*)cml_nn_linear(1, 1, DTYPE_FLOAT32, DEVICE_CPU, true);
    Optimizer* opt = cml_optim_sgd_for_model(model, 0.1f, 0.0f, 0.0f);

    for (int step = 1; step <= 200; step++) {
        cml_optim_zero_grad(opt);
        Tensor* loss = cml_nn_mse_loss(cml_nn_module_forward(model, x), y);
        cml_backward(loss, NULL, false, false);
        cml_optim_step(opt);
        if (step % 50 == 0)
            printf("step %d  loss %.5f\n", step, tensor_get_float(loss, 0));
        tensor_free(loss);
    }

    cml_cleanup();
    return 0;
}

The examples guide has 17 more tutorials, from XOR and linear regression to RNNs, GANs and a Transformer encoder.

Python

cd python && pip install cffi && python3 cml/build_cffi.py && pip install -e .
python3 -c "import cml; cml.init(); print('ok'); cml.cleanup()"

See the Python guide for the PyTorch-style API.

Features

Area What's included
Layers 28 layers: Linear, Conv1d/2d/3d, ConvTranspose, RNN, LSTM, GRU, Transformer, BatchNorm, LayerNorm, RMSNorm, pooling, Dropout, PixelShuffle
Training 11 optimizers · 13 losses · 8 LR schedulers · gradient checkpointing · DDP · pipeline and tensor parallelism
LLMs LLaMA 7B–70B · LoRA / QLoRA · Flash Attention · GQA · ALiBi · RoPE scaling · paged and sliding-window KV cache · MoE · ring attention · speculative decoding · continuous-batching server
Compiler IR with 5 fusion patterns · linearization · codegen to C, PTX, SPIR-V, WGSL and MSL · AOT and JIT · kernel cache
Runtime LLVM JIT with shape-specialized SIMD and an interpreter fallback · f32, f64, f16, bf16 and int dtypes · BLAS · TLSF allocator · memory and thread pools
Model I/O GGUF · SafeTensors · ONNX · PyTorch checkpoints · int8, int4 and NF4 quantization
Python CFFI bindings · NumPy interop · operator overloading

Backend status

CI runs every GPU path through the CPU reference backend, mocks and emulators. Driver calls are only proven correct on real hardware, so the GPU backends are marked accordingly. Status as of 2026-08:

Backend Status What that means
CPU: SIMD, BLAS, LLVM JIT Stable Default backend; the full test suite runs on it
HCQ queues, signals, pipelines Stable Same dispatch path for every backend, tested on the CPU reference
CUDA, ROCm, OpenCL, Vulkan, WebGPU Build-verified Compile everywhere, load at runtime and fail cleanly without hardware; not yet validated on a GPU
NV (RM ioctl), AMD (KFD) drivers Experimental Covered by mock tests; real ioctl paths need hardware
Multi-GPU: peer copy, device placement Simulated Runs on the device simulator only
Adreno, Hexagon, Thunder, USB Placeholder Stub or generic kernels; not for production

Architecture

---
config:
  flowchart:
    wrappingWidth: 640
    nodeSpacing: 24
    rankSpacing: 28
---
flowchart TB
    user["<b>Your code</b><br/>C or Python"]
    api["<b>Public API</b><br/>28 layers · optimizers · losses · LLM ops · serving"]
    autograd["<b>Autograd</b><br/>dynamic graphs · checkpointing · distributed"]
    ops["<b>Tensor ops</b><br/>broadcasting · SIMD · BLAS"]
    subgraph compiler["Compiler"]
        direction LR
        ir["IR"] --> sched["schedule"] --> lin["linearize"] --> cg["codegen<br/>C · PTX · SPIR-V · WGSL · MSL"] --> cache["kernel cache"]
    end
    drivers["<b>Runtime & drivers</b><br/>LLVM JIT · HCQ · NV (RM ioctl) · AM (KFD) · TLSF · pools"]
    hw["<b>Hardware</b><br/>CPU · CUDA · ROCm · Vulkan · Metal · WebGPU · OpenCL · Adreno · Hexagon"]

    user --> api --> autograd --> ops --> compiler --> drivers --> hw

    classDef layer fill:#F8F2E7,stroke:#201C19,stroke-width:1.5px,color:#201C19
    classDef step fill:#F8F2E7,stroke:#BB5015,stroke-width:1.5px,color:#201C19
    classDef edge fill:#201C19,stroke:#BB5015,stroke-width:1.5px,color:#F8F2E7
    class api,autograd,ops,drivers layer
    class ir,sched,lin,cg,cache step
    class user,hw edge
    style compiler fill:transparent,stroke:#BB5015,stroke-width:1.5px,stroke-dasharray:4 3,color:#BB5015
    linkStyle default stroke:#BB5015,stroke-width:1.5px

Documentation

Topic Guides
Start here Getting started · API reference · Examples · Python bindings
Models NN layers · Advanced NN and LLMs · Speculative decoding · BEAM search
Training Training · Autograd · Datasets · Distributed training
Compiler Compiler pipeline · IR graph management · Graph mode · Linearization · Optimizations
Runtime GPU backends · Memory management · Kernel Studio (quick reference)
Reference Model I/O · Benchmarks · External dependencies · Miscellaneous · All docs

License

DBaJ-NC-CFL, the "Don't Be a Jerk" non-commercial license.

探索相关项目

加载中…