{"id":13640979,"url":"https://github.com/EugenHotaj/zig_gpt2","last_synced_at":"2025-04-20T07:31:28.233Z","repository":{"id":173351435,"uuid":"650406930","full_name":"EugenHotaj/zig_gpt2","owner":"EugenHotaj","description":"GPT-2 inference engine written in Zig","archived":false,"fork":false,"pushed_at":"2023-07-24T01:38:54.000Z","size":18002,"stargazers_count":26,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-05-22T18:12:30.041Z","etag":null,"topics":["gpt-2","inference-engine","zig"],"latest_commit_sha":null,"homepage":"","language":"Zig","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/EugenHotaj.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}},"created_at":"2023-06-07T02:25:03.000Z","updated_at":"2024-06-14T05:49:43.274Z","dependencies_parsed_at":null,"dependency_job_id":"ed8de547-a422-4e86-a4cf-c3fc950c8a05","html_url":"https://github.com/EugenHotaj/zig_gpt2","commit_stats":null,"previous_names":["eugenhotaj/zig_inference","eugenhotaj/zig_gpt2"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EugenHotaj%2Fzig_gpt2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EugenHotaj%2Fzig_gpt2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EugenHotaj%2Fzig_gpt2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EugenHotaj%2Fzig_gpt2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EugenHotaj","download_url":"https://codeload.github.com/EugenHotaj/zig_gpt2/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223821972,"owners_count":17208775,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["gpt-2","inference-engine","zig"],"created_at":"2024-08-02T01:01:16.427Z","updated_at":"2024-11-09T11:30:33.102Z","avatar_url":"https://github.com/EugenHotaj.png","language":"Zig","funding_links":[],"categories":["Data \u0026 Science"],"sub_categories":["Large Language Model"],"readme":"# zig_gpt2\nGPT-2 inference engine written in Zig. Generation time: ~28ms per token.\n\n### Features:\n* No third-party dependencies besides BLAS (Accelerate or OpenBLAS).\n* No memory allocations at runtime.\n* Can run [NanoGPT](https://github.com/karpathy/nanoGPT).\n\n### How to Run:\n\nDownload the GPT-2 checkpoint from OpenAI.\n```bash\npython3 download_weights.py\n```\n\nBuild the Zig binary and run it with a prompt to generate completions:\n```bash\nzig build -DOptimize=ReleaseFast\n./zig-out/bin/zig_gpt2 \"Marcus Aurelius said\"\n```\n\n### How to Test:\n\nGenerate test data by forwarding random tensors through PyTorch ops.\n```bash\npython3 generate_test_data.py\n```\n\nRun tests. Verifies Zig ops produce the same output as PyTorch.\n```bash\nzig build test\n```\n\n---\n\n### TODO\n\nImplementation:\n* ✅ Implement basic ops: Embedding, Linear, LayerNorm, GELU, Softmax, CausalSelfAttention.\n* ✅ Implement transformer modules: MLP, Transformer block.\n* ✅ Implement the full GPT model.\n* ✅ Implement sampling from the model.\n* ✅ Implement BPE encoding/decoding.\n    \nEfficiency:\n* ✅ Replace custom linear algebra kernels with BLAS.\n* ✅ Stream output as each new token is generated.\n* ✅ Create central set of memory buffers and reuse them for each layer. No allocations at runtime.\n* ✅ Add KV cache.\n* Parallelize `softmax` and `gelu` operations.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEugenHotaj%2Fzig_gpt2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FEugenHotaj%2Fzig_gpt2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FEugenHotaj%2Fzig_gpt2/lists"}