Giter Site home page Giter Site logo

gpt-2's Introduction

gpt-2

Code from the paper "Language Models are Unsupervised Multitask Learners".

We have currently released small (117M parameter) and medium (345M parameter) versions of GPT-2. While we have not released the larger models, we have released a dataset for researchers to study their behaviors.

See more details in our blog post.

Usage

This repository is meant to be a starting point for researchers and engineers to experiment with GPT-2.

Some caveats

  • 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.
  • The dataset our GPT-2 models were trained on contains many texts with biases and factual inaccuracies, and thus GPT-2 models are likely to be biased and inaccurate as well.
  • 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.

Work with us

Please let us know 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

  • Potential malicious use cases and defenses against them (e.g. the detectability of synthetic text)
  • The extent of problematic content (e.g. bias) being baked into the models and effective mitigations

Development

See DEVELOPERS.md

Contributors

See CONTRIBUTORS.md

Fine tuning on custom datasets

To retrain GPT-2 117M model on a custom text dataset:

PYTHONPATH=src ./train.py --dataset <file|directory|glob>

If you want to precompute the dataset's encoding for multiple runs, you can instead use:

PYTHONPATH=src ./encode.py <file|directory|glob> /path/to/encoded.npz
PYTHONPATH=src ./train.py --dataset /path/to/encoded.npz

Gradient Checkpointing

https://github.com/openai/gradient-checkpointing is included to reduce the memory requirements of the model, and can be enabled by --memory_saving_gradients. The checkpoints are currently chosen manually (poorly) by just adding layer 10 to the 'checkpoints' collection in model.py. --memory_saving_gradients is enabled by default for training the 345M model.

Validation loss

Set --val_every to a number of steps N > 0, and "validation" loss against a fixed sample of the dataset will be calculated every N steps to get a better sense of training progress. N around 200 suggested. You can set --val_dataset to choose a separate validation dataset, otherwise it defaults to a sample from the train dataset (so not a real cross-validation loss!).

Optimizer

You can use SGD instead of Adam with --optimizer sgd. This also helps conserve memory when training the 345M model. Note: the learning rate needs to be adjusted for SGD, due to not having Adam's gradient normalization (0.0006 seems to be a good number from some experiments).

Multi gpu (out of date)

To do distributed on multiple GPUs or machines using Horovod:

mpirun -np 4 \
    -H localhost:4 \
    -bind-to none -map-by slot \
    -x NCCL_DEBUG=INFO -x LD_LIBRARY_PATH -x PATH \
    -x PYTHONPATH=src \
    -mca pml ob1 -mca btl ^openib \
    /home/jovyan/gpt-2/train-horovod.py --dataset encoded.npz

GPT-2 samples

WARNING: Samples are unfiltered and may contain offensive content.

While we have not yet released GPT-2 itself, you can see some samples from it in the gpt-2-samples folder. We show unconditional samples with default settings (temperature 1 and no truncation), with temperature 0.7, and with truncation with top_k 40. We show conditional samples, with contexts drawn from WebText's test set, with default settings (temperature 1 and no truncation), with temperature 0.7, and with truncation with top_k 40.

Citation

Please use the following bibtex entry:

@article{radford2019language,
  title={Language Models are Unsupervised Multitask Learners},
  author={Radford, Alec and Wu, Jeff and Child, Rewon and Luan, David and Amodei, Dario and Sutskever, Ilya},
  year={2019}
}

Future work

We may release code for evaluating the models on various benchmarks.

We are still considering release of the larger models.

License

MIT

gpt-2's People

Contributors

armaanbhullar avatar github30 avatar imgntn avatar madisonmay avatar minimaxir avatar mrene avatar natemurthy avatar nshepperd avatar shawwn avatar tenoke avatar tlkh avatar webproduktion01 avatar wuthefwasthat avatar zitterbewegung avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gpt-2's Issues

1.5B

It seems to me that other GPT-2 codes for fine-tuning 1.5B will bump into out-of-memory issue on Colab. Why does your code have no such issue? Any trick or do I miss something? Thank you for kind advice.

Correct branch for GPU fine-tuning

Hey Shawn!

Amazing work you are doing here investigating the TPU multi-core limitations.
It's very fun to also check your commits live, hahaha.

I am a long time developer by my knowledge about machine learning is still very limited, could you point me some stuff?

What's the correct branch with the most updated code to fine-tune using GPU instances? Can I use the TPU one without the tpu flag?

AFAIK, the tokenization to .npz also is somehow based on the model.
Can I use the 117M to tokenize and train the 1558M with these results? It is so much faster to load!

TPU sampling fails

I got the training for 774M running at a preemptible TPU!
However, I am not able to sample the model. It fails with this following error:

ValueError: Cannot feed value of shape (1, 1) for Tensor 'zeros:0' which has shape '(1, 1024)'

The stack is from context.load(args.batch_size * [context_tokens], session=sess).

I tried tinkering with the code as much as I could, including feeding 1024 to data_sampler.sample(1024), but then output becomes NoneType.

What can I do?

Running full cluster on Colab 345M and 2048 context

Have been following your work on twitter - thanks for taking the time and documenting your progress and process. It's been incredibly insightful for helping understand the nuances of fine-tuning and training with different parameters rather than other explanations that say "just do this"

Currently running your notebook on colab with TPU using the latest branch. I do understand that it's been intentionally capped in order to prevent OOM for the larger models.

I'm trying to test the extent of context tokens for input training to output generation using 2048 and starting with the 345M model as the base. I had set the batch size to 8 but I believe the latest TPU code only uses 1 core regardless.

Is there a specific branch to use for the full cluster TPU with the assumption that it wouldn't go OOM?

Code no longer works in Colab

I've been having some issues getting the model to work in google colab. I've been running into issues with contrib modules such as HParams that have been moved or removed.

I tried setting the tensorflow version in colab to 1.X but that sets it to 1.15 where the changes have already been made.

Does anyone know a workaround for this?

Saving to storage bucket

Hi,

I'm training on the 1588M model on gcp. I see that your notebook mentions a parameter by the name of --storage_bucket.

 # Note that there is currently no support for saving the trained model.
 # Theoretically it might work, but you'll have to create your own storage bucket and pass in --storage_bucket gs://your-bucket/gpt-2/

I need the feature and I'm in the position to test it out but I've seen the code for it is not in the train.py file. Is it something you implemented and ultimately decided not to include as you couldn't test it yourself?

Let me know if you have the code!

Loading large finetuned models?

I'm looking to load a finetuned 1.5B model to generate text (trained with a slightly modified version of the tpu-multi-snapshot branch), but it appears that there isn't support for that? The code in generate_unconditional_samples.py and interactive_conditional_samples.py only seems to support the pretrained models. I've tried messing with it myself in an attempt to load them (taking large sections from train.py), but nothing I do seems to be working.

Any chance anyone's succeeded in doing this already?

TPU Fine-tuning Speed

I am fine-tuning the 345M model with TPU on Google Colab. I'm getting ~ 0.1 it/s, whereas the original shepperd fork reaches ~ 1 it/s for the same model and parameters on a T4 GPU. I would have expected TPUs to be faster, any idea what's up?

Can't load fine-tuned checkpoint

Hi and thank you for this great fork! I fine-tuned GPT2-XL on TPU and now trying to load it to run inference. interactive_conditional_samples.py seems to fail loading the model (TF 1.15, GPU T4):

Traceback (most recent call last):
  File "/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/client/session.py", line 1365, in _do_call
    return fn(*args)
  File "/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/client/session.py", line 1350, in _run_fn
    target_list, run_metadata)
  File "/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/client/session.py", line 1443, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: 2 root error(s) found.
  (0) Failed precondition: Attempting to use uninitialized value model/h36/mlp/c_proj/w
	 [[{{node model/h36/mlp/c_proj/w/read}}]]
	 [[sample_sequence/while/Exit_3/_203]]
  (1) Failed precondition: Attempting to use uninitialized value model/h36/mlp/c_proj/w
	 [[{{node model/h36/mlp/c_proj/w/read}}]]
0 successful operations.
0 derived errors ignored.

Any ideas?

Are there any other ways to load the model from this hdf5 format and make a regular checkpoint out of it?

Just trying not to loose 3 days of TPU compute :)

Problems with getting it to work with the new Colab gpus (p100, t4)

I used your repo successfuly in Colab on TPU, however, it is quite slow (as is to be expected) and TPU instancess come with ~ 1 saved checkpoint's worth of free space.

Luckily, it seems I am now getting T4 and p100 sometimes when I switch to GPU. Does any of your branches support it (I find them a bit hard to navigate).

Currently, it just stops on its own without starting to train, I assume because it still runs out of memory.

Edit: figured it out.

Saving format question

Hi!

First of all, thanks a lot for your work, I just started exploring and it's great to find a codebase allowing gpt-2 training on TPUs.

I've encountered a little detail you might be interested in: I notice that you save snapshots in hdf5 format, at least when using the TPU. I had just ported a (117M, for now) model that I was training with nshepperd's fork, so it had the usual model-113893.data-00000-of-00001 format, with the other files as well. The loading went super smoothly, but as soon as I wanted to train a second time, it could not load the last checkpoint.

My files looked like this:
files

I had to remove the model-113893.data-00000-of-00001 as well as the .meta and .index files in order for the loading to succeed (and it did load the last snapshot).

And the command used was the following;

!PYTHONPATH=src python ./train.py \
    --dataset "../my-dataset.npz" \
    --batch_size 20 \
    --save_every 1000 \
    --restore_from latest \
    --save_on_ctrlc \
    --max_to_keep 1 \
    --sample_every 100 \
    --n_ctx 1024 \
    --n_embd 768 \
    --n_head 12 \
    --n_layer 12

Any chance I might be able to save the model using the other format, namely overwrite the current checkpoint instead of adding hdf5 files? I've been looking into your options, but couldn't find anything so far.

Thanks in any case!

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.