{"id":17982196,"url":"https://github.com/kmkolasinski/tensorflow-nanogpt","last_synced_at":"2025-10-13T00:10:01.219Z","repository":{"id":162102010,"uuid":"624823520","full_name":"kmkolasinski/tensorflow-nanoGPT","owner":"kmkolasinski","description":"Example how to train GPT-2 (XLA + AMP), export to SavedModel and serve with Tensorflow Serving","archived":false,"fork":false,"pushed_at":"2023-05-06T06:15:37.000Z","size":616,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-08-16T22:32:05.942Z","etag":null,"topics":["gpt-2","tensorflow","xla"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kmkolasinski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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-04-07T10:46:44.000Z","updated_at":"2024-06-06T09:17:56.000Z","dependencies_parsed_at":null,"dependency_job_id":"605a8878-7bb5-4139-980a-9bb454ce31b9","html_url":"https://github.com/kmkolasinski/tensorflow-nanoGPT","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/kmkolasinski/tensorflow-nanoGPT","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmkolasinski%2Ftensorflow-nanoGPT","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmkolasinski%2Ftensorflow-nanoGPT/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmkolasinski%2Ftensorflow-nanoGPT/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmkolasinski%2Ftensorflow-nanoGPT/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kmkolasinski","download_url":"https://codeload.github.com/kmkolasinski/tensorflow-nanoGPT/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmkolasinski%2Ftensorflow-nanoGPT/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279013617,"owners_count":26085298,"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","status":"online","status_checked_at":"2025-10-12T02:00:06.719Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["gpt-2","tensorflow","xla"],"created_at":"2024-10-29T18:13:25.582Z","updated_at":"2025-10-13T00:10:01.164Z","avatar_url":"https://github.com/kmkolasinski.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tensorflow-nanoGPT\n\nExample code on how to finetune GPT-2 model using Tensorflow and Keras-NLP library then\nexport it to a fully end-to-end model i.e. text-in =\u003e text-out in a SavedModel format, which\nlater can be served with tensorflow serving. Whole processing is stored in the tensorflow graph,\nso no extra libraries / tokenizers are needed to run the inference.\n\nThe target of this project was to train a generative model to extract Named Entities (NE)\nfrom the input prompt text using model trained on [conll2003](https://huggingface.co/datasets/conll2003) dataset.\nThe output of this model can be later post processed for further logic.\n\nAt the end of the notebook you can run trained model in the following way:\n\n```python\nimport tensorflow as tf\npredictor = tf.saved_model.load('/path/to/gpt2/model')\n\nprompt = \"CRICKET - LEICESTERSHIRE TAKE OVER AT TOP AFTER INNINGS VICTORY .\"\nprediction = predictor(prompt)\nprediction['outputs'].numpy().decode() == \"LEICESTERSHIRE//ORG\\n\"\n\n```\n\n# Main Features\n\n* fast training using **mixed precision**\n* even faster training with **XLA enabled (jit_compile)**\n* partial model freezing and basic implementation of **LoRA**\n* **fast data preparation** by using tokenizer from keras-nlp package (fully compatible with tf.data.Dataset)\n* **faster token generation with cached keys/values** tensors of attention head\n* export trained model to SavedModel - whole processing is stored inside TF graph (preprocessing, tokenization and prediction)\n* example how to serve model using **tensorflow serving**\n\n\n# Some numbers on Google Colab\n\n* Tested on Tesla T4\n* I used single but the same prompt for each configuration of the exported model\n* GPT-2 - with 256 sequence length\n\n    | Run Type                | Generation time  |\n    |-------------------------|------------------|\n    | Baseline                | 579 ms ± 38.5 ms |\n    | Baseline + XLA          | 369 ms ± 4.37 ms |\n    | Cached Key/Values       | 688 ms ± 125 ms  |\n    | Cached Key/Values + XLA | 245 ms ± 7.72 ms |\n\n\n# Installation\n\n* See [requirements.txt](requirements.txt) file\n* Tested with Tensorflow 2.11\n* Checkout example notebook [gpt_2_finetune_conll2003.ipynb](gpt_2_finetune_conll2003.ipynb)\n\n# Disclaimer\n\n* the aim of this project was not to create any form of SOTA model,\n* this is just a test / demo of various features of TensorFlow library,\n* the notebook shows how to go from data to production ready and servable model,\n* I'm aware that there are still many things which to try to improve the throughput and memory usage.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkmkolasinski%2Ftensorflow-nanogpt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkmkolasinski%2Ftensorflow-nanogpt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkmkolasinski%2Ftensorflow-nanogpt/lists"}