patrickvonplaten commited on
Commit
be76c6b
1 Parent(s): 8ee1677
Files changed (3) hide show
  1. config.json +26 -0
  2. run.sh +1 -1
  3. run_model.py +32 -8
config.json ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "activation_dropout": 0.0,
3
+ "activation_function": "relu",
4
+ "architectures": [
5
+ "OPTModel"
6
+ ],
7
+ "attention_dropout": 0.0,
8
+ "bos_token_id": 0,
9
+ "d_model": 1024,
10
+ "dropout": 0.1,
11
+ "eos_token_id": 2,
12
+ "ffn_dim": 4096,
13
+ "init_std": 0.02,
14
+ "layerdrop": 0.0,
15
+ "max_position_embeddings": 2048,
16
+ "model_type": "opt",
17
+ "num_attention_heads": 16,
18
+ "num_hidden_layers": 24,
19
+ "output_projection": true,
20
+ "pad_token_id": 1,
21
+ "torch_dtype": "float16",
22
+ "transformers_version": "4.19.0.dev0",
23
+ "vocab_size": 50272,
24
+ "word_embed_proj_dim": 512,
25
+ "do_layer_norm_before": false
26
+ }
run.sh CHANGED
@@ -1,2 +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
1
  #!/usr/bin/env bash
2
+ CUDA_VISIBLE_DEVICES="0,3" torchrun run_model.py --pipeline-model-parallel-size 1 --tensor-model-parallel-size 1
run_model.py CHANGED
@@ -1,9 +1,9 @@
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"
@@ -34,32 +34,56 @@ checkpoint = checkpoint_utils.load_model_ensemble_and_task(
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("-------------")
 
 
1
  #!/usr/bin/env python3
 
2
  import os
3
  from transformers import AutoTokenizer, GPT2Tokenizer
4
  from megatron.initialize import initialize_megatron
5
  from metaseq import checkpoint_utils
6
+ from transformers import OPTForCausalLM
7
  import torch
8
 
9
  path = "./model"
34
  )
35
 
36
  model = checkpoint[0][0].eval()
37
+ model = model.to("cuda:0").half()
38
+
39
+ hf_model = OPTForCausalLM.from_pretrained("../opt-350m").to("cuda:1").half()
40
 
41
 
42
  # forward passes
43
  def single_batch_forward_logits(prompts):
44
  input_ids = tokenizer(prompts, return_tensors="pt").input_ids
45
  input_ids = torch.cat([torch.tensor([[0]]), input_ids], dim=-1)
46
+ input_ids = input_ids.to("cuda:0")
47
  with torch.no_grad():
48
  logits = model(input_ids)[0]
49
  return logits
50
 
51
+ # forward hf
52
+ def forward_hf(prompts):
53
+ input_ids = tokenizer(prompts, return_tensors="pt").input_ids
54
+ input_ids = torch.cat([torch.tensor([[0]]), input_ids], dim=-1)
55
+ input_ids = input_ids.to("cuda:1")
56
+ with torch.no_grad():
57
+ logits = hf_model(input_ids)[0]
58
+ return logits
59
+
60
  prompts = [
61
+ "Today is a beautiful day and I want to",
62
+ "In the city of",
63
+ "Paris is the capital of France and",
64
+ "Computers and mobile phones have taken",
65
  ]
66
 
67
+ prompts = [
68
+ "Today is a beautiful day and I want to",
69
+ ]
70
+
71
+ #import ipdb; ipdb.set_trace()
72
  print("Next word generation")
73
  for prompt in prompts:
74
  print("-------------")
75
  print(f"Prompt: {prompt}...\n")
76
+ logits_fsq = single_batch_forward_logits(prompt)
77
+ pred_next_token = torch.argmax(logits_fsq[0, -1], -1)
78
+ next_token = tokenizer.convert_ids_to_tokens([pred_next_token])
79
+ next_token = next_token[0].replace("Ġ", "")
80
+ print(f"Next word: {next_token}")
81
+ print("-------------")
82
+ logits = forward_hf(prompt)
83
  pred_next_token = torch.argmax(logits[0, -1], -1)
84
  next_token = tokenizer.convert_ids_to_tokens([pred_next_token])
85
  next_token = next_token[0].replace("Ġ", "")
86
  print(f"Next word: {next_token}")
87
  print("-------------")
88
+
89
+ torch.allclose(logits_fsq.cpu(), logits.cpu(), atol=1e-3)