{"id":13747203,"url":"https://github.com/nshepperd/gpt-2","last_synced_at":"2025-09-28T19:31:43.098Z","repository":{"id":34257384,"uuid":"173626887","full_name":"nshepperd/gpt-2","owner":"nshepperd","description":"Code for the paper \"Language Models are Unsupervised Multitask Learners\"","archived":true,"fork":true,"pushed_at":"2022-10-31T06:55:40.000Z","size":4557,"stargazers_count":1148,"open_issues_count":59,"forks_count":443,"subscribers_count":34,"default_branch":"finetuning","last_synced_at":"2025-01-18T19:36:31.176Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"openai/gpt-2","license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nshepperd.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}},"created_at":"2019-03-03T20:36:47.000Z","updated_at":"2025-01-08T07:38:15.000Z","dependencies_parsed_at":"2023-01-16T23:00:46.628Z","dependency_job_id":null,"html_url":"https://github.com/nshepperd/gpt-2","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nshepperd/gpt-2","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nshepperd%2Fgpt-2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nshepperd%2Fgpt-2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nshepperd%2Fgpt-2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nshepperd%2Fgpt-2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nshepperd","download_url":"https://codeload.github.com/nshepperd/gpt-2/tar.gz/refs/heads/finetuning","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nshepperd%2Fgpt-2/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":277422059,"owners_count":25815336,"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-09-28T02:00:08.834Z","response_time":79,"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":[],"created_at":"2024-08-03T06:01:20.184Z","updated_at":"2025-09-28T19:31:42.827Z","avatar_url":"https://github.com/nshepperd.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"## Fine tuning on custom datasets\n\nReference:  [\"Beginner’s Guide to Retrain GPT-2 (117M) to Generate Custom Text Content\"](https://medium.com/@ngwaifoong92/beginners-guide-to-retrain-gpt-2-117m-to-generate-custom-text-content-8bb5363d8b7f)\n\nTo retrain GPT-2 117M model on a custom text dataset:\n\n```\nPYTHONPATH=src ./train.py --dataset \u003cfile|directory|glob\u003e\n```\n\nIf you want to precompute the dataset's encoding for multiple runs, you can instead use:\n\n```\nPYTHONPATH=src ./encode.py \u003cfile|directory|glob\u003e /path/to/encoded.npz\nPYTHONPATH=src ./train.py --dataset /path/to/encoded.npz\n```\n\nMake sure `cudnn` is installed. [Some have\nreported](https://github.com/nshepperd/gpt-2/issues/8) that `train.py`\nruns without it but has worse memory usage and might OOM.\n\n### Tensor Rematerialization\n\nExperimental: a rematerialization rewriter based on `Efficient\nRematerialization for Deep Networks`\n\u003chttps://papers.nips.cc/paper/9653-efficient-rematerialization-for-deep-networks.pdf\u003e,\nwhich unlike gradient checkpointing works in tensorflow 2.0 and is\nable to automatically select checkpoints in arbitrary graphs. Using\nthis I was able to finetune GPT-2 1.5B on a single graphics card using\nslightly less than 12G of video ram with very little slowdown.\n\nTo use this is a little involved, because the graph optimization\nalgorithm is offloaded to an optimized Haskell program. First, go into\nsubdirectory `twremat`, and build it by invoking:\n\n    cabal v2-install --installdir=../bin\n\n(You'll need to install cabal if you haven't already -- but setting up\nghc and haskell compilation is beyond the scope of this README.)\n\nThen run `train.py` as normal, enabling `--twremat` and setting\n`--twremat_memlimit` to an appropriate value -- this sets the amount\nof memory assumed to be available for computation of gradients, so it\nshould be roughly the memory size of your graphics card minus whatever\nis taken up by the gpt-2 weights, and any other bookkeeping\nvariables. You may need to experiment with the memlimit until you find\nthe largest value that doesn't OOM.\n\n(You probably also want to use SGD as optimizer instead of Adam to\nminimize those bookkeeping variables, of which Adam uses a lot).\n\n### Gradient Checkpointing\n\nhttps://github.com/openai/gradient-checkpointing is included to reduce\nthe memory requirements of the model, and can be enabled by\n`--memory_saving_gradients`. The checkpoints are currently chosen\nmanually (poorly) by just adding layer 10 to the 'checkpoints'\ncollection in model.py.\n\nGradient checkpointing doesn't work in tensorflow v2.0 and later due\nto the removal of tf.contrib. You should use tensor rematerialization\ninstead if possible.\n\n### Validation loss\n\nSet `--val_every` to a number of steps `N \u003e 0`, and \"validation\" loss\nagainst a fixed sample of the dataset will be calculated every N steps\nto get a better sense of training progress. N around 200\nsuggested. You can set `--val_dataset` to choose a separate validation\ndataset, otherwise it defaults to a sample from the train dataset (so\nnot a real cross-validation loss!).\n\n### Optimizer\n\nYou can use SGD instead of Adam with `--optimizer sgd`. This also\nhelps conserve memory when training larger models. Note: the learning\nrate needs to be adjusted for SGD, due to not having Adam's gradient\nnormalization (0.0006 seems to be a good number from some\nexperiments).\n\n# Original README\n\n**Status:** Archive (code is provided as-is, no updates expected)\n\n# gpt-2\n\nCode and models from the paper [\"Language Models are Unsupervised Multitask Learners\"](https://d4mucfpksywv.cloudfront.net/better-language-models/language-models.pdf).\n\nYou can read about GPT-2 and its staged release in our [original blog post](https://blog.openai.com/better-language-models/), [6 month follow-up post](https://openai.com/blog/gpt-2-6-month-follow-up/), and [final post](https://www.openai.com/blog/gpt-2-1-5b-release/).\n\nWe have also [released a dataset](https://github.com/openai/gpt-2-output-dataset) for researchers to study their behaviors.\n\n\u003csup\u003e*\u003c/sup\u003e *Note that our original parameter counts were wrong due to an error (in our previous blog posts and paper).  Thus you may have seen small referred to as 117M and medium referred to as 345M.*\n\n## Usage\n\nThis repository is meant to be a starting point for researchers and engineers to experiment with GPT-2.\n\nFor basic information, see our [model card](./model_card.md).\n\n### Some caveats\n\n- GPT-2 models' robustness and worst case behaviors are not well-understood.  As with any machine-learned model, carefully evaluate GPT-2 for your use case, especially if used without fine-tuning or in safety-critical applications where reliability is important.\n- The dataset our GPT-2 models were trained on contains many texts with [biases](https://twitter.com/TomerUllman/status/1101485289720242177) and factual inaccuracies, and thus GPT-2 models are likely to be biased and inaccurate as well.\n- To avoid having samples mistaken as human-written, we recommend clearly labeling samples as synthetic before wide dissemination.  Our models are often incoherent or inaccurate in subtle ways, which takes more than a quick read for a human to notice.\n\n### Work with us\n\nPlease [let us know](mailto:languagequestions@openai.com) if you’re doing interesting research with or working on applications of GPT-2!  We’re especially interested in hearing from and potentially working with those who are studying\n- Potential malicious use cases and defenses against them (e.g. the detectability of synthetic text)\n- The extent of problematic content (e.g. bias) being baked into the models and effective mitigations\n\n## Development\n\nSee [DEVELOPERS.md](./DEVELOPERS.md)\n\n## Contributors\n\nSee [CONTRIBUTORS.md](./CONTRIBUTORS.md)\n\n## Citation\n\nPlease use the following bibtex entry:\n```\n@article{radford2019language,\n  title={Language Models are Unsupervised Multitask Learners},\n  author={Radford, Alec and Wu, Jeff and Child, Rewon and Luan, David and Amodei, Dario and Sutskever, Ilya},\n  year={2019}\n}\n```\n\n## Future work\n\nWe may release code for evaluating the models on various benchmarks.\n\nWe are still considering release of the larger models.\n\n## License\n\n[Modified MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnshepperd%2Fgpt-2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnshepperd%2Fgpt-2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnshepperd%2Fgpt-2/lists"}