{"id":20939370,"url":"https://github.com/trekhleb/homemade-gpt-js","last_synced_at":"2026-03-06T20:03:53.339Z","repository":{"id":262118177,"uuid":"882633311","full_name":"trekhleb/homemade-gpt-js","owner":"trekhleb","description":"A minimal TensorFlow.js re-implementation of Karpathy's minGPT (Generative Pre-trained Transformer). The GPT model itself is \u003c300 lines of code.","archived":false,"fork":false,"pushed_at":"2024-11-12T17:17:31.000Z","size":33353,"stargazers_count":82,"open_issues_count":1,"forks_count":9,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-10-07T07:37:42.350Z","etag":null,"topics":["ai","artificial-intelligence","genai","gpt","machine-learning","ml","tensorflowjs","tfjs","transformers"],"latest_commit_sha":null,"homepage":"https://trekhleb.dev/homemade-gpt-js","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/trekhleb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-11-03T10:50:24.000Z","updated_at":"2025-08-30T06:31:35.000Z","dependencies_parsed_at":"2025-10-07T07:30:59.149Z","dependency_job_id":null,"html_url":"https://github.com/trekhleb/homemade-gpt-js","commit_stats":null,"previous_names":["trekhleb/homemade-gpt-js"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/trekhleb/homemade-gpt-js","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trekhleb%2Fhomemade-gpt-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trekhleb%2Fhomemade-gpt-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trekhleb%2Fhomemade-gpt-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trekhleb%2Fhomemade-gpt-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trekhleb","download_url":"https://codeload.github.com/trekhleb/homemade-gpt-js/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trekhleb%2Fhomemade-gpt-js/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30195571,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-06T19:07:06.838Z","status":"ssl_error","status_checked_at":"2026-03-06T18:57:34.882Z","response_time":250,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","artificial-intelligence","genai","gpt","machine-learning","ml","tensorflowjs","tfjs","transformers"],"created_at":"2024-11-18T23:01:00.683Z","updated_at":"2026-03-06T20:03:53.315Z","avatar_url":"https://github.com/trekhleb.png","language":"TypeScript","funding_links":[],"categories":["ai","machine-learning"],"sub_categories":[],"readme":"# Homemade GPT • JS\n\n![Homemade GPT JS](./playground-web/public/cover.jpg)\n\nA minimal TensorFlow.js re-implementation of Karpathy's [minGPT](https://github.com/karpathy/minGPT) (Generative Pre-trained Transformer).\n\nA full definition of this \"homemade\" **GPT** language model (all of it) can be found in this single [model.ts](./gpt/src/model.ts) file (less than `300` lines of code).\n\nSince [model.ts](./gpt/src/model.ts) is written in TypeScript, you can use [homemade GPT playground](https://trekhleb.dev/homemade-gpt-js) to train it, experiment with parameters, and generate its predictions directly in the browser using a GPU.\n\nThe model and the playground are written for *learning purposes*, to understand how GPT works and to use WebGPU for training.\n\nTo understand what's happening in the [model.ts](./gpt/src/model.ts) file please refer to Andrej Karpathy's well-explained, hands-on lecture \"[Let's build GPT: from scratch, in code, spelled out](https://www.youtube.com/watch?v=kCc8FmEb1nY)\" (arguably one of the best explanations of GPT out there).\n\n### GPT Folder\n\nInside the [./gpt/src/](./gpt/src/) folder you'll find the following files:\n\n- [model.ts](./gpt/src/model.ts) - this is the main file of interest, as it contains the full (yet minimalistic) definition of the decoder GPT model, as described in the [Attention Is All You Need](https://arxiv.org/pdf/1706.03762) paper.\n- [model-easier.ts](./gpt/src/model-easier.ts) - this is the same GPT model as in the previous file but simplified for easier understanding. The main difference is that it processes all `Heads` inside `CausalSelfAttention` *sequentially* (instead of in parallel). As a result, the model is a bit slower but more readable.\n- [config.ts](./gpt/src/config.ts) - contains pre-configured sets of GPT model parameters: GPT-pico, GPT-nano, GPT-mini, GPT-2, etc.\n- [dataset.ts](./gpt/src/dataset.ts) - Nothing GPT-specific here. A helper wrapper on top of any txt-file-based character-level dataset. It loads an arbitrary txt file, treats each letter as a token, splits the characters into training and testing batches, and encodes/decodes letters to indices and vice versa.\n- [trainer.ts](./gpt/src/trainer.ts) - Nothing GPT-specific here as well. This file provides a simple training loop that could apply to any arbitrary neural network.\n\nSome pre-trained models weights are published in [homemade-gpt-js-weights](https://github.com/trekhleb/homemade-gpt-js-weights) repository. You may apply them via the web playground (\"Generation\" section) or via the Node.js playground (`model.setWeights()`).\n\n### Web Playground\n\nTo experiment with model parameters, training, and text generation you may use the [Homemade GPT JS playground](https://trekhleb.dev/homemade-gpt-js).\n\n|[Homemade GPT JS playground](https://trekhleb.dev/homemade-gpt-js)|\n|---|\n|![Homemade GPT playground](./playground-web/public/playground-demo.gif)|\n\nYou may also launch the playground locally if you want to modify and experiment with the code of the transformer model itself.\n\nInstall dependencies: \n\n```sh\nnpm i\n```\n\nLaunch web playground locally:\n\n```sh\nnpm run playground-web\n```\n\nThe playground will be accessible on http://localhost:3000/homemade-gpt-js \n\nRun these commands from the root of the project. You need to have Node.js ≥ 20.0.0.\n\n### Node.js Playground\n\nYou may also experiment with the model in Node.js environment.\n\nInstall dependencies: \n\n```sh\nnpm i\n```\n\nLaunch Node.js playground:\n\n```sh\nnpm run playground-node\n```\n\nThe [./playground-node/src/index.ts](./playground-node/src/index.ts) file contains the basic example of training and text generation.\n\nRun these commands from the root of the project. You need to have Node.js ≥ 20.0.0.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrekhleb%2Fhomemade-gpt-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrekhleb%2Fhomemade-gpt-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrekhleb%2Fhomemade-gpt-js/lists"}