-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfer_benchmark.py
More file actions
188 lines (156 loc) · 6.43 KB
/
infer_benchmark.py
File metadata and controls
188 lines (156 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import json
import os
from tqdm import tqdm
import argparse
import torch
import torch.distributed as dist
from torch.utils.data import DataLoader, DistributedSampler
from models import TimeOmni
from data_provider.dataset import Dataset_Unified, Collator_Unified
def setup_distributed():
"""Set up the distributed environment."""
if 'RANK' in os.environ and 'WORLD_SIZE' in os.environ:
rank = int(os.environ["RANK"])
world_size = int(os.environ['WORLD_SIZE'])
gpu = int(os.environ['LOCAL_RANK'])
else:
print('Not using distributed mode')
return False, 0, 1, 0
torch.cuda.set_device(gpu)
dist.init_process_group(backend='nccl', init_method='env://', world_size=world_size, rank=rank)
dist.barrier()
return True, rank, world_size, gpu
def create_dataloader(jsonl_file, tokenizer, media_root, batch_size, num_workers=16, distributed=False):
"""Create a DataLoader for a single JSONL file."""
dataset = Dataset_Unified(
tokenizer=tokenizer,
jsonl_file_path=jsonl_file,
base_dir=media_root,
use_scaler=True,
unfold_channels=True,
)
sampler = DistributedSampler(dataset) if distributed else None
data_collator = Collator_Unified(tokenizer=tokenizer)
dataloader = DataLoader(
dataset,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers,
sampler=sampler,
pin_memory=True,
drop_last=False,
collate_fn=data_collator
)
return dataloader
def inference_single_jsonl(model, dataloader, jsonl_file, generation_config, rank=0):
"""Run inference for a single JSONL file."""
results = []
if rank == 0:
print(f"\nProcessing jsonl file: {jsonl_file}")
print(f"Number of batches: {len(dataloader)}")
for batch in tqdm(dataloader, desc=f"Processing {jsonl_file}", disable=(rank != 0)):
try:
input_ts = batch['input_ts_list']
input_ts = [input.cuda() for input in input_ts]
# Batch inference using input_texts as prompts.
with torch.no_grad():
responses = model.generate(
input_ts,
batch['input_texts'],
generation_config=generation_config
)
# Save results.
for i in range(len(batch['ids'])):
results.append({
'id': batch['ids'][i],
"uid": batch['uids'][i],
"dataset_name": batch['dataset_names'][i],
"task": batch['tasks'][i],
"scene": batch['scenes'][i],
'ts_path': batch['input_ts_paths'][i],
'input_text': batch['input_texts'][i],
'generated_text': responses[i].strip(),
'ground_truth': batch['gt_texts'][i],
'gt_result': batch['gt_results'][i],
})
except Exception as e:
print(f"[ERROR] Exception in batch inference for '{jsonl_file}': {e}")
print(f"[ERROR] Batch IDs: {batch['ids']}")
# On error, skip saving results for this batch.
return results
def save_results(results, output_file):
"""Save results to a JSONL file."""
os.makedirs(os.path.dirname(output_file), exist_ok=True)
with open(output_file, 'w', encoding='utf-8') as f:
for result in results:
f.write(json.dumps(result, ensure_ascii=False) + '\n')
print(f"Results saved to {output_file}")
def main():
parser = argparse.ArgumentParser(description="Time Series LLM Evaluation with DDP")
parser.add_argument("--model_path", type=str,
help="Path to the model")
parser.add_argument("--data_file_list", type=str, nargs='+', required=True,
help="List of JSONL data files for inference")
parser.add_argument("--output_folder", type=str, required=True,
help="Folder to save the results")
parser.add_argument("--media_root", type=str, default=None,
help="Root path to prepend to relative ts_path in data")
parser.add_argument("--batch_size", type=int, default=8,
help="Batch size for inference")
parser.add_argument("--max_new_tokens", type=int, default=100,
help="Max new tokens for generation")
parser.add_argument("--num_workers", type=int, default=16,
help="Number of workers for DataLoader")
args = parser.parse_args()
# Set up distributed environment.
distributed, rank, world_size, gpu = setup_distributed()
# Load model
if rank == 0:
print("Loading model...")
model = TimeOmni.Model.load_checkpoint(
args.model_path,
).eval().to('cuda')
generation_config = dict(max_new_tokens=args.max_new_tokens, do_sample=False)
if rank == 0:
print("Model loaded successfully!")
# Process each file in data_file_list.
for data_file in args.data_file_list:
if rank == 0:
print(f"Loading dataset from {data_file}...")
if args.media_root:
print(f"Using media root: {args.media_root}")
# Run inference
if rank == 0:
print(f"Starting inference for {data_file}...")
dataloader = create_dataloader(
jsonl_file=data_file,
tokenizer=model.tokenizer,
media_root=args.media_root,
batch_size=args.batch_size,
num_workers=args.num_workers,
distributed=distributed
)
results = inference_single_jsonl(
model=model,
dataloader=dataloader,
jsonl_file=data_file,
generation_config=generation_config,
rank=rank
)
# Gather results from all processes.
if distributed:
dist.barrier()
all_results = [None] * world_size
dist.all_gather_object(all_results, results)
if rank == 0:
results = []
for rank_results in all_results:
results.extend(rank_results)
# Only the main process saves results.
if rank == 0:
# Build output filename.
base_name = os.path.basename(data_file)
output_file = os.path.join(args.output_folder, base_name)
save_results(results, output_file)
if __name__ == "__main__":
main()