root commited on
Commit
7a4ca68
1 Parent(s): 34eb6e3

add test code

Browse files
Files changed (1) hide show
  1. test_modeling.py +31 -0
test_modeling.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # torchrun --nproc_per_node=2 test_modeling.py --local_path {local_path}
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+ import argparse
5
+ parser = argparse.ArgumentParser()
6
+ parser.add_argument("--model_path", type=str, default="petil777/srv1_parallel")
7
+ parser.add_argument("--revision", type=str, default=None)
8
+ parser.add_argument("--local_path", type=str, required=True)
9
+ args = parser.parse_args()
10
+
11
+ model_path = "petil777/srv1_parallel"
12
+
13
+ # Distributed.launch will occur inside.
14
+ model = AutoModelForCausalLM.from_pretrained(args.model_path,local_path=args.local_path,
15
+ revision=args.revision, trust_remote_code=True)
16
+
17
+ model.eval()
18
+ tokenizer = model.tokenizer
19
+
20
+ rank = torch.distributed.get_rank() if torch.distributed.is_initialized() else 0
21
+ model = model.to(f"cuda:{rank}")
22
+
23
+
24
+ input_str="apple is red and banana is"
25
+ input_dict = tokenizer(input_str, return_tensors="pt") #input_ids, attention_mask
26
+ input_ids= input_dict.input_ids
27
+ input_ids=input_ids.to(f"cuda:{rank}")
28
+
29
+ out_tensor = model.generate(input_ids, top_k=0,return_dict_in_generate=True,output_scores=True,output_hidden_states=True)
30
+ if rank == 0:
31
+ print(tokenizer.decode(out_tensor.sequences[0]))