Skip to content
Projets

À propos

Three inference engines for Llama 3: pure C for desktop systems, pure JavaScript for Node.js, and pure JavaScript for Web environments. Supports both Llama and Gemma architectures.

IA et apprentissage automatique

Comment ces chiffres sont mesurés

Progression mesurée des étoiles

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
0

Variations nettes sur des périodes qui se chevauchent, pas des totaux quotidiens. Les mesures absentes restent vides. Les étoiles indiquent l’intérêt, pas la qualité.

README.md

Original

llama3pure

Three inference engines for Llama 3: pure C for desktop systems, pure JavaScript for Node.js, and pure JavaScript for Web environments. Supports both Llama and Gemma architectures. Try the Web engine here.

demo

Table of Contents

Building the engine (macOS / Linux)

make llama3pure

Building the engine (Windows)

Use the x64 Native Tools Command Prompt for VS.

cl /O2 llama3pure-c-engine.c /Fe:llama3pure.exe

Running the engine (macOS / Linux / Windows)

# On macOS / Linux
./llama3pure -model Llama3.gguf -prompt "Tell me in one line what Microsoft is."
./llama3pure -model Llama3.gguf -chathistory chat.txt

# On Windows
llama3pure.exe -model Llama3.gguf -prompt "Tell me in one line what Microsoft is."
llama3pure.exe -model Llama3.gguf -chathistory chat.txt
Argument Required Description Default Value
-model Yes Path to a GGUF model file. -
-prompt No Input prompt text (single-turn, alternative to -chathistory). -
-chathistory No Path to a .txt file containing a JSON chat history (multi-turn, alternative to -prompt). -
-system_prompt No System prompt prepended to every conversation. You are a helpful assistant.
-max_tokens No Maximum number of tokens of a conversation, counting the prompt and the response. -1 (context size)
-context_size No Context window size (capped by the model's own limit). Model's max.
-temperature No Sampling temperature. Higher values produce more varied output. 0.9
-top_p No Nucleus sampling threshold. Only tokens whose cumulative probability reaches this value are considered. 0.9
-top_k No Top-K sampling. Only the K most probable tokens are considered at each step. 40
-debug No Show detailed model loading and performance logs (including tok/s). disabled

Sample chat history in tests.txt. A message with "type": "system" sets the system prompt, taking precedence over -system_prompt.

When the prompt doesn't fit in the context (or in -max_tokens), nothing is generated and the engine prints Context length exceeded: the prompt has N tokens and the limit is M.

Running in Node.js

  • Step 1: Load a model

Read the GGUF file into an ArrayBuffer and pass it to llama3pure with type: "load".

import llama3pure from "./llama3pure-js-engine.js"
import fs from "fs"

const readFileAsArrayBuffer = (filePath) => {
  const fd = fs.openSync(filePath, "r")
  const fileSize = fs.fstatSync(fd).size
  const arrayBuffer = new ArrayBuffer(fileSize)
  const fileUint8 = new Uint8Array(arrayBuffer)
  const chunkSize = 256 * 1024 * 1024
  let pos = 0
  while (pos < fileSize) {
    const toRead = Math.min(chunkSize, fileSize - pos)
    fs.readSync(fd, fileUint8, pos, toRead, pos)
    pos = pos + toRead
  }
  fs.closeSync(fd)
  return arrayBuffer
}

llama3pure({
  type: "load",
  model: readFileAsArrayBuffer("/path/to/your-model.gguf"),
  cbRender: (token) => {
    process.stdout.write(token)
  },
  systemPrompt: "You are a helpful assistant.",
  maxTokens: 256,
  contextSize: 2048,
  temperature: 0.9,
  topP: 0.9,
  topK: 40,
})
Parameter Type Required Description Default Value
type string Yes Must be load -
model ArrayBuffer Yes The GGUF model file contents. -
cbRender function No Callback invoked with each generated token as a string. -
systemPrompt string No System prompt prepended to every conversation. You are a helpful assistant.
maxTokens number No Maximum number of tokens of a conversation, counting the prompt and the response. -1 (context size)
contextSize number No Context window size (capped by the model's own limit). Model's max.
temperature number No Sampling temperature. Higher values produce more varied output. 0.9
topP number No Nucleus sampling threshold. Only tokens whose cumulative probability reaches this value are considered. 0.9
topK number No Top-K sampling. Only the K most probable tokens are considered at each step. 40
  • Step 2: Generate a response

Call llama3pure with type: "generate" and a chatHistory array. The engine uses the cbRender callback provided during load to stream tokens. The last message in chatHistory should have type: "user" - that is the message the model will respond to. Previous messages provide conversation context, enabling multi-turn conversations. A message with type: "system" sets the system prompt, taking precedence over systemPrompt.

llama3pure({
  type: "generate",
  chatHistory: [
    { type: "user", text: "Tell me in one line what Microsoft is." },
    {
      type: "model",
      response: [
        "Microsoft is a global technology leader known for its innovative products and services.",
      ],
    },
    { type: "user", text: "Tell me in one line who the founders are." },
  ],
})
Parameter Type Required Description
type string Yes Must be generate.
chatHistory array Yes Array of message objects representing the conversation.

When the prompt doesn't fit in the context (or in maxTokens), nothing is generated and cbRender isn't called.

Full example in llama3pure-nodejs-demo.js.

Running in Web Environments

  • Step 1: Load a model

Create the Worker as a module, read the GGUF file as an ArrayBuffer and send it to the worker with type: "load". The ArrayBuffer is transferred (not copied) for performance.

const worker = new Worker("llama3pure-js-engine.js", { type: "module" })
const reader = new FileReader()

reader.onload = (event) => {
  const arrayBuffer = event.target.result
  worker.postMessage(
    {
      type: "load",
      model: arrayBuffer,
      systemPrompt: "You are a helpful assistant.",
      maxTokens: 256,
      contextSize: 2048,
      temperature: 0.9,
      topP: 0.9,
      topK: 40,
    },
    [arrayBuffer]
  )
}

reader.readAsArrayBuffer(file)
Parameter Type Required Description Default Value
type string Yes Must be load -
model ArrayBuffer Yes The GGUF model file contents. -
systemPrompt string No System prompt prepended to every conversation. You are a helpful assistant.
maxTokens number No Maximum number of tokens of a conversation, counting the prompt and the response. -1 (context size)
contextSize number No Context window size (capped by the model's own limit). Model's max.
temperature number No Sampling temperature. Higher values produce more varied output. 0.9
topP number No Nucleus sampling threshold. Only tokens whose cumulative probability reaches this value are considered. 0.9
topK number No Top-K sampling. Only the K most probable tokens are considered at each step. 40
  • Step 2: Generate a response
worker.postMessage({
  type: "generate",
  chatHistory: [
    { type: "user", text: "Tell me in one line what Microsoft is." },
    {
      type: "model",
      response: [
        "Microsoft is a global technology leader known for its innovative products and services.",
      ],
    },
    { type: "user", text: "Tell me in one line who the founders are." },
  ],
})
Parameter Type Required Description
type string Yes Must be generate.
chatHistory array Yes Array of message objects representing the conversation.
  • Step 3: Receiving messages from the Worker
worker.onmessage = function (e) {
  var data = e.data
  switch (data.type) {
    case "progress":
      // Fired during model loading
      break

    case "loaded":
      // Fired once the model is fully loaded and ready
      break

    case "token":
      // Fired for each generated token during inference
      console.log(data.token)
      break

    case "complete":
      // Fired when generation is finished
      console.log(data.output)
      break
  }
}
Event Fields Description
progress - Emitted during model loading to indicate progress.
loaded - Emitted once when the model has been fully loaded and is ready for inference.
token token (string) Emitted for each token as it is generated, enabling real-time streaming of the response.
complete output (string) Emitted when generation finishes. Contains the full generated text.

When the prompt doesn't fit in the context (or in maxTokens), nothing is generated: complete is emitted with output set to null, together with promptTokens (tokens of the prompt) and tokenLimit (the limit that was exceeded).

Try the Web engine here.

A standalone version is available here; it offers the same functionality as the standard version but uses a base64-embedded Worker, allowing you to run it as a local file without a web server.

In both versions, the gear icon opens the settings: the theme (automatic, light or dark) and the context size, which can be chosen until a model is loaded.

Suggested Models and Engines

MODEL C NODE.JS WEB
Gemma-3-1B-it-Q8_0.gguf ✅ ✅ ✅
Llama-3.2-1B-Instruct-Q8_0.gguf ✅ ✅ ✅
Llama-3.2-3B-Instruct-Q8_0.gguf ✅ ✅ ❌
Gemma-3-4b-it-Q8_0.gguf ✅ ✅ ❌

Tested Models

MODEL C NODE.JS WEB
Gemma-3-270M-it-Q2_K_L.gguf ✅ ✅ ✅
Gemma-3-270M-it-Q3_K_M.gguf ✅ ✅ ✅
Gemma-3-270M-it-Q4_K_M.gguf ✅ ✅ ✅
Gemma-3-270M-it-Q5_K_M.gguf ✅ ✅ ✅
Gemma-3-270M-it-Q6_K.gguf ✅ ✅ ✅
Gemma-3-270M-it-Q8_0.gguf ✅ ✅ ✅
Gemma-3-270M-it-F16.gguf ✅ ✅ ✅
Gemma-3-1B-it-Q2_K_L.gguf ✅ ✅ ✅
Gemma-3-1B-it-Q3_K_M.gguf ✅ ✅ ✅
Gemma-3-1B-it-Q4_K_M.gguf ✅ ✅ ✅
Gemma-3-1B-it-Q5_K_M.gguf ✅ ✅ ✅
Gemma-3-1B-it-Q6_K.gguf ✅ ✅ ✅
Gemma-3-1B-it-Q8_0.gguf ✅ ✅ ✅
Gemma-3-1B-it-BF16.gguf ✅ ✅ ✅
Llama-3.2-1B-Instruct-Q3_K_L.gguf ✅ ✅ ✅
Llama-3.2-1B-Instruct-Q4_K_L.gguf ✅ ✅ ✅
Llama-3.2-1B-Instruct-Q5_K_L.gguf ✅ ✅ ✅
Llama-3.2-1B-Instruct-Q6_K_L.gguf ✅ ✅ ✅
Llama-3.2-1B-Instruct-Q8_0.gguf ✅ ✅ ✅
Llama-3.2-1B-Instruct-f16.gguf ✅ ✅ ❌
Llama-3.2-3B-Instruct-Q3_K_L.gguf ✅ ✅ ❌
Llama-3.2-3B-Instruct-Q4_K_L.gguf ✅ ✅ ❌
Llama-3.2-3B-Instruct-Q5_K_L.gguf ✅ ✅ ❌
Llama-3.2-3B-Instruct-Q6_K_L.gguf ✅ ✅ ❌
Llama-3.2-3B-Instruct-Q8_0.gguf ✅ ✅ ❌
Llama-3.2-3B-Instruct-f16.gguf ✅ ✅ ❌
Gemma-3-4b-it-Q2_K_L.gguf ✅ ✅ ❌
Gemma-3-4b-it-Q3_K_M.gguf ✅ ✅ ❌
Gemma-3-4b-it-Q4_K_M.gguf ✅ ✅ ❌
Gemma-3-4b-it-Q5_K_M.gguf ✅ ✅ ❌
Gemma-3-4b-it-Q6_K.gguf ✅ ✅ ❌
Gemma-3-4b-it-Q8_0.gguf ✅ ✅ ❌
Gemma-3-4b-it-BF16.gguf ✅ ✅ ❌
Llama-3-8B-Instruct-Q2_K.gguf ✅ ✅ ❌
Llama-3-8B-Instruct-Q3_K_M.gguf ✅ ✅ ❌
Llama-3-8B-Instruct-Q4_K_M.gguf ✅ ✅ ❌
Llama-3-8B-Instruct-Q5_K_M.gguf ✅ ✅ ❌
Llama-3-8B-Instruct-Q6_K.gguf ✅ ✅ ❌
Llama-3-8B-Instruct-Q8_0.gguf ✅ ✅ ❌
Llama-3-8B-Instruct-fp16.gguf ✅ ✅ ❌

Special keys

Action macOS Shortcut Windows Shortcut Safari Shortcut
Next reply Command + 1 Ctrl + 1 Ctrl + 1
Regenerate Command + 2 Ctrl + 2 Ctrl + 2
Copy to clipboard Command + 3 Ctrl + 3 Ctrl + 3
Send Enter Enter Enter
New line Shift + Enter Shift + Enter Shift + Enter
Cancel editing Escape Escape Escape

Author's Notes

  • Using quantizations below Q4 is generally discouraged because the loss in logic and coherence makes them nearly unusable for most tasks.

  • Due to universal browser memory constraints regarding ArrayBuffer size limits, the Web engine can only read GGUF files up to 2 GB.

  • There isn't a Python engine because a ported and pure version would be very slow. Using NumPy wouldn't make sense because it uses C under the hood, and for that, there is already a C engine.

Based on the work of

https://github.com/karpathy/llama2.c

Explorer des projets similaires

Chargement…