The simplest, fastest repository for training/finetuning medium-sized GPTs. It is a…
- User
The simplest, fastest repository for training/finetuning medium-sized GPTs. It is a rewrite of minGPT that prioritizes teeth over education. Still under active development, but currently the file train.py reproduces GPT-2 (124M) on OpenWebText, running on a single 8XA100 40GB node in about 4 days of training. The code itself is plain and readable: train.py is a ~300-line boilerplate training loop and model.py a ~300-line GPT model definition, which can optionally load the GPT-2 weights from OpenAI. That's it. 翻译成中文
- Assistant
最简单、最快速的中等规模GPT(泛用预训练模型)训练/微调存储库。它是minGPT的重写版本,注重效率而非教育。目前仍在积极开发中,但目前的train.py文件可以在OpenWebText上复现GPT-2(124M)的训练,在单个8XA100 40GB节点上大约需要4天时间。代码本身简洁易读:train.py是一个大约300行的模板训练循环,model.py是一个大约300行的GPT模型定义,可以选择性地从OpenAI加载GPT-2的权重。就是这样。
- User
Because the code is so simple, it is very easy to hack to your needs, train new models from scratch, or finetune pretrained checkpoints (e.g. biggest one currently available as a starting point would be the GPT-2 1.3B model from OpenAI). 翻译成中文
- Assistant
由于代码非常简单,因此非常容易根据自己的需求进行修改,从头开始训练新模型,或对预训练的检查点进行微调(例如,OpenAI目前提供的最大模型为GPT-2 1.3B模型,可以作为起点)。
- User
If you are not a deep learning professional and you just want to feel the magic and get your feet wet, the fastest way to get started is to train a character-level GPT on the works of Shakespeare. First, we download it as a single (1MB) file and turn it from raw text into one large stream of integers: 翻译成中文
- Assistant
如果您不是深度学习专业人士,只是想感受一下魔力并入门,最快的方法是在莎士比亚的作品上训练一个字符级别的GPT。首先,我们将其以单个(1MB)文件的形式下载下来,并将其从原始文本转换为一个大的整数流:
- User
This creates a train.bin and val.bin in that data directory. Now it is time to train your GPT. The size of it very much depends on the computational resources of your system: 翻译成中文
- Assistant
这将在数据目录下创建一个train.bin和一个val.bin文件。现在是训练GPT的时候了。其大小非常取决于您系统的计算资源:
- User
If you peek inside it, you'll see that we're training a GPT with a context size of up to 256 characters, 384 feature channels, and it is a 6-layer Transformer with 6 heads in each layer. On one A100 GPU this training run takes about 3 minutes and the best validation loss is 1.4697. Based on the configuration, the model checkpoints are being written into the --out_dir directory out-shakespeare-char. So once the training finishes we can sample from the best model by pointing the sampling script at this directory: 翻译成中文
- Assistant
如果您查看其中的内容,您将会看到我们正在训练一个GPT模型,它的上下文大小可以达到256个字符,具有384个特征通道,是一个6层的Transformer模型,每层有6个头。在一个A100 GPU上,这个训练过程大约需要3分钟,最好的验证损失为1.4697。根据配置,模型检查点被写入到--out_dir参数指定的目录out-shakespeare-char中。因此,一旦训练完成,我们可以通过将采样脚本指向该目录来从最佳模型中进行采样:
- User
lol ¯_(ツ)_/¯. Not bad for a character-level model after 3 minutes of training on a GPU. Better results are quite likely obtainable by instead finetuning a pretrained GPT-2 model on this dataset (see finetuning section later).
I only have a macbook (or other cheap computer). No worries, we can still train a GPT but we want to dial things down a notch. I recommend getting the bleeding edge PyTorch nightly (select it here when installing) as it is currently quite likely to make your code more efficient. But even without it, a simple train run could look as follows: 翻译成中文
- Assistant
哈哈 ¯_(ツ)_/¯。在GPU上训练3分钟后,对于一个字符级别的模型来说,效果还不错。通过在这个数据集上微调预训练的GPT-2模型,很有可能获得更好的结果(请参阅稍后的微调部分)。
我只有一台MacBook(或其他廉价计算机)。别担心,我们仍然可以训练一个GPT模型,但需要降低一些要求。我建议安装最新版本的PyTorch nightly版(在安装时选择它),因为目前它很可能会使您的代码更高效。但即使没有它,一个简单的训练运行可能如下所示:
- User
Here, since we are running on CPU instead of GPU we must set both --device=cpu and also turn off PyTorch 2.0 compile with --compile=False. Then when we evaluate we get a bit more noisy but faster estimate (--eval_iters=20, down from 200), our context size is only 64 characters instead of 256, and the batch size only 12 examples per iteration, not 64. We'll also use a much smaller Transformer (4 layers, 4 heads, 128 embedding size), and decrease the number of iterations to 2000 (and correspondingly usually decay the learning rate to around max_iters with --lr_decay_iters). Because our network is so small we also ease down on regularization (--dropout=0.0). This still runs in about ~3 minutes, but gets us a loss of only 1.88 and therefore also worse samples, but it's still good fun: 翻译成中文
- Assistant
在这里,由于我们在CPU上而不是GPU上运行,我们必须设置--device=cpu,并且还要关闭PyTorch 2.0的编译,使用--compile=False。然后,当我们进行评估时,我们会得到更加嘈杂但更快的估计值(--eval_iters=20,而不是200),我们的上下文大小仅为64个字符而不是256个字符,每次迭代只有12个示例的批量大小,而不是64个。我们还将使用一个更小的Transformer模型(4层,4个头,128个嵌入维度),并将迭代次数减少到2000次(并相应地使用--lr_decay_iters来衰减学习率到最大迭代次数附近)。由于我们的网络非常小,我们也会减少正则化(--dropout=0.0)。这个训练仍然需要约3分钟,但损失只有1.88,因此样本也更差,但仍然很有趣:
- User
Not bad for ~3 minutes on a CPU, for a hint of the right character gestalt. If you're willing to wait longer, feel free to tune the hyperparameters, increase the size of the network, the context length (--block_size), the length of training, etc.
Finally, on Apple Silicon Macbooks and with a recent PyTorch version make sure to add --device=mps (short for "Metal Performance Shaders"); PyTorch then uses the on-chip GPU that can significantly accelerate training (2-3X) and allow you to use larger networks. See Issue 28 for more.
reproducing GPT-2 A more serious deep learning professional may be more interested in reproducing GPT-2 results. So here we go - we first tokenize the dataset, in this case the OpenWebText, an open reproduction of OpenAI's (private) WebText: 翻译成中文
- Assistant
对于CPU运行而言,大约3分钟的时间得到的效果还不错,可以提供一些正确字符组合的线索。如果你愿意等待更长的时间,随意调整超参数、增加网络的大小、上下文长度(--block_size)、训练长度等等。
最后,在Apple Silicon的MacBook上,并且使用了最新版本的PyTorch,请确保添加--device=mps(即“Metal Performance Shaders”);PyTorch将使用芯片上的GPU来加速训练(2-3倍),并允许您使用更大的网络。更多信息请参见Issue 28。
复现GPT-2 更专业的深度学习专家可能更感兴趣于复现GPT-2的结果。下面是具体的步骤-首先我们对数据集进行分词处理,例如OpenWebText,这是对OpenAI的(私有)WebText进行开放式复现的数据集:
- User
This downloads and tokenizes the OpenWebText dataset. It will create a train.bin and val.bin which holds the GPT2 BPE token ids in one sequence, stored as raw uint16 bytes. Then we're ready to kick off training. To reproduce GPT-2 (124M) you'll want at least an 8X A100 40GB node and run:
This will run for about 4 days using PyTorch Distributed Data Parallel (DDP) and go down to loss of ~2.85. Now, a GPT-2 model just evaluated on OWT gets a val loss of about 3.11, but if you finetune it it will come down to ~2.85 territory (due to an apparent domain gap), making the two models ~match.
If you're in a cluster environment and you are blessed with multiple GPU nodes you can make GPU go brrrr e.g. across 2 nodes like: 翻译成中文
- Assistant
这个过程会下载并对OpenWebText数据集进行分词处理。它将创建一个train.bin和val.bin文件,其中存储了以原始uint16字节形式保存的GPT2 BPE标记的序列。然后我们准备开始训练。要复现GPT-2(124M),您至少需要一台8XA100 40GB节点,并运行以下命令:
这将使用PyTorch分布式数据并行(DDP)进行大约4天的训练,并将损失降低到约2.85左右。现在,仅在OWT上评估的GPT-2模型的验证损失约为3.11,但如果您对其进行微调,它将降至约2.85的范围(由于明显的领域差异),使得这两个模型基本匹配。
如果您在一个集群环境中且有多个GPU节点,您可以像下面这样让GPU全速运行,例如跨两个节点:
- User
It is a good idea to benchmark your interconnect (e.g. iperf3). In particular, if you don't have Infiniband then also prepend NCCL_IB_DISABLE=1 to the above launches. Your multinode training will work, but most likely crawl. By default checkpoints are periodically written to the --out_dir. We can sample from the model by simply $ python sample.py.
Finally, to train on a single GPU simply run the $ python train.py script. Have a look at all of its args, the script tries to be very readable, hackable and transparent. You'll most likely want to tune a number of those variables depending on your needs.
baselines OpenAI GPT-2 checkpoints allow us to get some baselines in place for openwebtext. We can get the numbers as follows: 翻译成中文
- Assistant
对于集群内部连接的基准测试是一个好主意(例如iperf3)。特别是,如果您没有Infiniband,还需要在上述启动命令之前添加NCCL_IB_DISABLE=1。多节点训练可能会运行,但可能会很慢。默认情况下,检查点会定期写入--out_dir目录中。我们可以通过简单地运行 $ python sample.py 从模型中进行采样。
最后,要在单个GPU上进行训练,只需运行 $ python train.py 脚本即可。查看所有参数,脚本试图非常易读、易于修改和透明。根据您的需求,您很可能需要调整其中许多变量。
基准测试 OpenAI GPT-2的检查点允许我们在openwebtext上建立一些基线。我们可以按如下方式获取这些数值:
- User
However, we have to note that GPT-2 was trained on (closed, never released) WebText, while OpenWebText is just a best-effort open reproduction of this dataset. This means there is a dataset domain gap. Indeed, taking the GPT-2 (124M) checkpoint and finetuning on OWT directly for a while reaches loss down to ~2.85. This then becomes the more appropriate baseline w.r.t. reproduction.
finetuning Finetuning is no different than training, we just make sure to initialize from a pretrained model and train with a smaller learning rate. For an example of how to finetune a GPT on new text go to data/shakespeare and run prepare.py to download the tiny shakespeare dataset and render it into a train.bin and val.bin, using the OpenAI BPE tokenizer from GPT-2. Unlike OpenWebText this will run in seconds. Finetuning can take very little time, e.g. on a single GPU just a few minutes. Run an example finetuning like:
$ python train.py config/finetune_shakespeare.py This will load the config parameter overrides in config/finetune_shakespeare.py (I didn't tune them much though). Basically, we initialize from a GPT2 checkpoint with init_from and train as normal, except shorter and with a small learning rate. If you're running out of memory try decreasing the model size (they are {'gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl'}) or possibly decreasing the block_size (context length). The best checkpoint (lowest validation loss) will be in the out_dir directory, e.g. in out-shakespeare by default, per the config file. You can then run the code in sample.py --out_dir=out-shakespeare: 翻译成中文
- Assistant
然而,我们必须注意的是,GPT-2是在(封闭的、未发布的)WebText上进行训练的,而OpenWebText只是对这个数据集的最佳努力的开放复制。这意味着存在数据集领域差异。实际上,将GPT-2(124M)的检查点直接在OWT上进行微调,一段时间后的损失可以降低到约2.85。这就成为了与复现相关的更合适的基准。
微调 微调与训练没有区别,只需确保从预训练模型初始化,并使用较小的学习率进行训练。如果要了解如何在新文本上对GPT进行微调的示例,请转到data/shakespeare目录,并运行prepare.py下载微小的莎士比亚数据集,并使用来自GPT-2的OpenAI BPE分词器将其转换为train.bin和val.bin。与OpenWebText不同,这将在几秒钟内完成。微调可能只需要很少的时间,例如在单个GPU上只需几分钟。运行一个示例的微调命令,如下所示:
$ python train.py config/finetune_shakespeare.py 这将加载config/finetune_shakespeare.py中的配置参数覆盖(尽管我没有对它们进行太多调整)。基本上,我们从一个GPT2的检查点(使用init_from参数)进行初始化,并像正常训练一样进行训练,只是时间较短,使用较小的学习率。如果内存