Skip to content

Xzdes/slmnetGPT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

slmnetGPT v3.0: A Dual-Engine AI Testbed

slmnetGPT began as a lightweight, zero-dependency neural network framework written in vanilla JavaScript. It has since evolved into a unique dual-engine testbed that allows you to build, train, and run a Generative Transformer (GPT) model using two different backends:

  1. A pure JavaScript engine (slmnet.js): Transparent, easy to debug, and perfect for educational purposes.
  2. A high-performance Rust engine (RustyGradients): Compiled to WebAssembly (WASM) for near-native speed, safety, and reliability.

This project successfully trains a character-level model directly in the browser, allows you to save and load its "brain" using IndexedDB, and serves as a powerful tool for comparing the trade-offs between different web technologies for client-side AI.


The Journey Continues: From a Working GPT to a High-Performance Lab

This project is the result of a multi-stage journey. The story began with a simple challenge: could I build a working GPT in the browser from scratch?

The first part of that journey is detailed in my Dev.to post: I Built a GPT in my Browser... The Journey from Amnesia to Stable Learning. It covers the meticulous process of debugging a pure JavaScript implementation and solving fundamental challenges like exploding gradients, unstable Layer Normalization, and inefficient optimization.

But once the JavaScript version was working, a new question emerged: "How far can we push this?"

This led to the second chapter: supercharging the application with Rust and WebAssembly. I developed a separate, more robust deep learning framework from the ground up, called RustyGradients, designed with performance and safety in mind. By compiling it to WebAssembly, I could create a second, high-performance engine for the same model architecture.

The final step was to merge these two worlds into a single, cohesive application, creating a unique laboratory where anyone can explore and compare these two powerful approaches to client-side AI.


Core Features & What It Can Do

The Frameworks: Two Philosophies

  • slmnet.js (The Transparent Engine)

    This is the original, pure JavaScript framework. Its strength lies in its clarity. Every operation, from Tensor manipulation to backpropagation, is written in easy-to-read JS, making it an invaluable tool for learning.

    • Tensor.js: A fundamental data container with autograd capabilities.
    • Ops.js, Layers.js, etc.: A complete set of building blocks for creating neural networks.
  • RustyGradients (The High-Performance Engine)

    This is a separate, more powerful framework written in Rust and compiled to WebAssembly. It brings the performance and safety of a systems-level language to the browser.

    • Near-Native Speed: All heavy computations (matrix multiplication, backpropagation) are executed in a highly optimized WASM module.
    • Type Safety: The Rust compiler eliminates entire classes of bugs before the code even runs.
    • Seamless Integration: Interacts with the JavaScript UI through a clean API, providing the best of both worlds.

The Application: An Interactive Laboratory

The index.html file is a sophisticated single-page application that provides:

  • Dual-Engine Switcher: Instantly switch between the JavaScript and Rust (WASM) backends to compare their performance and behavior.
  • Model Persistence with IndexedDB: Save your trained models directly in the browser. You can close the tab, come back later, and load the model's "brain" to continue training or generating text. You can save, load, and delete models for each engine independently.
  • Live Training & Generation: Train a complete GPT model on sample text and watch the loss decrease in real-time. Then, use the trained model to generate new text from a prompt.
  • Advanced Generation Control: Uses Top-K Sampling to produce more coherent and creative text, avoiding the repetitive loops common in simpler models.
  • Localization: A language switcher (RU/EN) for a user-friendly experience.

Project Structure

The project now consists of two main parts: the UI application (slmnetGPT) and the Rust engine (RustyGradients).

slmnetGPT/                  # The main application repository
├── pkg/                  # Compiled Rust (WASM) module from RustyGradients
│   ├── rusty_gradients_bg.wasm
│   └── rusty_gradients.js
├── slmnet/                 # The original JavaScript engine
│   ├── Layers.js
│   ├── Tensor.js
│   └── ...
├── index.html              # The runnable application and UI
└── README.md

How to Run

Running the project involves building the Rust engine and then serving the main application.

  1. Clone both repositories:

    git clone https://github.com/Xzdes/slmnetGPT.git
    git clone https://github.com/Xzdes/RustyGradients.git
  2. Build the Rust (WASM) Engine:

    • Navigate into the RustyGradients directory.
    • Ensure you have wasm-pack installed (cargo install wasm-pack).
    • Run the build command:
      cd RustyGradients
      wasm-pack build --target web
  3. Copy the WASM module:

    • After the build is complete, a pkg directory will be created inside RustyGradients.
    • Copy this entire pkg directory and place it inside the slmnetGPT directory.
  4. Start a Local Server:

    • Navigate back to the slmnetGPT directory.
    • Because the project uses ES Modules, you must serve index.html via a local web server. The easiest way is with a code editor extension like "Live Server" for VS Code, or by running a simple server command:
      # If you have Python 3
      python -m http.server
  5. Open in Browser: Navigate to the local address provided by your server (e.g., http://localhost:8000).


Using the Application

  1. Choose Your Engine: Start by clicking either "JavaScript Engine" or "Rust Engine". The application will initialize the chosen backend.
  2. Load or Train: You can either click "Load Model" to see if a previously saved model exists in your browser's database, or click "Start Training" to train a new model from scratch.
  3. Save Your Progress: After training, click "Save Model". The model's weights will be stored in IndexedDB, ready for your next session.
  4. Generate Text: Once a model is trained or loaded, you can enter a prompt and click "Generate" to see what it has learned.
  5. Switch and Compare: Feel free to switch engines at any time. The application will manage the state for each one independently.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors