patrickvonplaten commited on
Commit
8ee1677
1 Parent(s): aa1513b
README.md ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - opt_metasq
4
+ ---
5
+
6
+ # This repo let's you run the following checkpoint using facebookresearch/metaseq.
7
+
8
+ Do the following:
9
+
10
+ ## 1. Install PyTorch
11
+ ```
12
+ pip3 install torch==1.10.1+cu113 torchvision==0.11.2+cu113 torchaudio==0.10.1+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
13
+ ```
14
+
15
+ ## 2. Install Megatron
16
+ ```
17
+ git clone https://github.com/patrickvonplaten/Megatron-LM.git
18
+ cd Megatron-LM
19
+ pip3 install six regex
20
+ pip3 install -e .
21
+ ```
22
+ ## 3. Install fairscale
23
+ ```
24
+ git clone https://github.com/facebookresearch/fairscale.git
25
+ cd fairscale
26
+ git checkout prefetch_fsdp_params_simple
27
+ pip3 install -e .
28
+ ```
29
+ ## 4. Install metaseq
30
+ ```
31
+ git clone https://github.com/patrickvonplaten/metaseq.git
32
+ cd metaseq
33
+ pip3 install -e .
34
+ ```
35
+
36
+ ## 5. Clone this repo (click top right on "How to clone")
37
+
38
+ ## 6. Run the following:
39
+
40
+ ```bash
41
+ cd <path/to/cloned/repo>
42
+ bash run.sh
43
+ ```
model/dict.txt ADDED
The diff for this file is too large to render. See raw diff
 
model/gpt2-merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
model/gpt2-vocab.json ADDED
The diff for this file is too large to render. See raw diff
 
model/merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
model/restored.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f76f91b201a0f295cea6cd41c09526d530559ba4ea72cd6b5c536b12d6a202d4
3
+ size 714040635
model/special_tokens_map.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"bos_token": {"content": "<|endoftext|>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}, "eos_token": {"content": "<|endoftext|>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}, "unk_token": {"content": "<|endoftext|>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}}
model/tokenizer_config.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"errors": "replace", "unk_token": {"content": "<|endoftext|>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true, "__type": "AddedToken"}, "bos_token": {"content": "<|endoftext|>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true, "__type": "AddedToken"}, "eos_token": {"content": "<|endoftext|>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true, "__type": "AddedToken"}, "add_prefix_space": false, "tokenizer_class": "GPT2Tokenizer"}
model/vocab.json ADDED
The diff for this file is too large to render. See raw diff
 
run.sh ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ #!/usr/bin/env bash
2
+ CUDA_VISIBLE_DEVICES="0" torchrun run_model.py --pipeline-model-parallel-size 1 --tensor-model-parallel-size 1
run_model.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ #!/usr/bin/env python3
3
+ import os
4
+ from transformers import AutoTokenizer, GPT2Tokenizer
5
+ from megatron.initialize import initialize_megatron
6
+ from metaseq import checkpoint_utils
7
+ import torch
8
+
9
+ path = "./model"
10
+
11
+ # just need to initialize args with something,
12
+ # => doesn't need to correspond to the "correct" architecture for this checkpoint
13
+ initialize_megatron(args_defaults={
14
+ "micro_batch_size": 1,
15
+ "num_layers": 12,
16
+ "hidden_size": 768,
17
+ "num_attention_heads": 12,
18
+ "max_position_embeddings": 2048,
19
+ "encoder_seq_length": 2048
20
+ })
21
+
22
+ vocab_file = os.path.join(path, "gpt2-vocab.json")
23
+ merges_file = os.path.join(path, "gpt2-merges.txt")
24
+
25
+ tokenizer = GPT2Tokenizer(vocab_file, merges_file)
26
+ tokenizer.save_pretrained(path)
27
+
28
+ checkpoint = checkpoint_utils.load_model_ensemble_and_task(
29
+ [os.path.join(path, "restored.pt")],
30
+ arg_overrides={
31
+ "vocab_filename": vocab_file,
32
+ "merges_filename": merges_file,
33
+ }
34
+ )
35
+
36
+ model = checkpoint[0][0].eval()
37
+ model = model.cuda().half()
38
+
39
+
40
+ # forward passes
41
+ def single_batch_forward_logits(prompts):
42
+ input_ids = tokenizer(prompts, return_tensors="pt").input_ids
43
+ input_ids = torch.cat([torch.tensor([[0]]), input_ids], dim=-1)
44
+ input_ids = input_ids.cuda()
45
+ with torch.no_grad():
46
+ logits = model(input_ids)[0]
47
+ return logits
48
+
49
+ prompts = [
50
+ "Today is a beautiful day and I want to",
51
+ "In the city of",
52
+ "Paris is the capital of France and",
53
+ "Computers and mobile phones have taken",
54
+ ]
55
+
56
+ print("Next word generation")
57
+ for prompt in prompts:
58
+ print("-------------")
59
+ print(f"Prompt: {prompt}...\n")
60
+ logits = single_batch_forward_logits(prompt)
61
+ pred_next_token = torch.argmax(logits[0, -1], -1)
62
+ next_token = tokenizer.convert_ids_to_tokens([pred_next_token])
63
+ next_token = next_token[0].replace("Ġ", "")
64
+ print(f"Next word: {next_token}")
65
+ print("-------------")