Giter Club home page Giter Club logo

llm.java's Introduction

llm.java

Large Language Model (LLM) example in java i.e. GPT2. This is a port of the Llm.c code that lives here written by @karpathy

Before Running ChatGPT2 in Java

Before attempting to run this some prep work needs to happen. If you check the llm.c repository these steps are very similar. The reason the same code is in this repository is because LLM.c is still a moving target.

I highly recommend running the original llm.c to see it work. It's wonderful.

python -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
python prepro_tinyshakespeare.py
python train_gpt2.py

JVM Requirements

I used the GraalVM for this running version 21. If you're using sdkman.

sdk default java 21.0.2-graalce

I tested the following JVM version and they all seem to work. I have not investigated why some are slower than others.

  1. Temurin: This ran at half the speed of Graal. I stopped it at step 10
sdk install java 21-tem
sdk use java 21-tem
  1. Correto: This VM was also really slow compared to Graal. So I stopped it after step 10
sdk install java 21.0.3-amzn
sdk use java 21.0.3-amzn

Running

Note the arguments passed to the JVM. Of particular note is "-Djava.util.concurrent.ForkJoinPool.common.parallelism=10", adjust this based on how many cores you have. The matrix multiplication methods are entirely CPU bound so adding more threads than cores will just slow things down.

mvn clean install;
java -jar -ea --add-modules jdk.incubator.vector --enable-preview -Xmx8g -Djava.util.concurrent.ForkJoinPool.common.parallelism=10 target/gpt2-1.0-SNAPSHOT.jar

Performance

I've made no attempt to tune this for performance. The C version is still much faster than this version. There are some low-hanging fruit like parallelizing some of the loops. I made the matmul_forward and matmul_backward both parallel because it was painfully slow without it.

llm.java's People

Contributors

harryjackson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.