println profiling
Browse files- expand.py +2 -1
- expand_llm.py +15 -1
expand.py
CHANGED
|
@@ -86,7 +86,8 @@ def expand(batch: Batch, expander: BatchExpander, completion_criterion: Callable
|
|
| 86 |
completed_series: list[Series] = []
|
| 87 |
current_batch = batch
|
| 88 |
while len(current_batch.items) > 0:
|
| 89 |
-
print(f"Expanding {len(current_batch.items)} series: {current_batch.items}")
|
|
|
|
| 90 |
current_batch_items = []
|
| 91 |
start_time = time.time()
|
| 92 |
expanded = expander.expand(current_batch)
|
|
|
|
| 86 |
completed_series: list[Series] = []
|
| 87 |
current_batch = batch
|
| 88 |
while len(current_batch.items) > 0:
|
| 89 |
+
# print(f"Expanding {len(current_batch.items)} series: {current_batch.items}")
|
| 90 |
+
print(f"Expanding {len(current_batch.items)} series")
|
| 91 |
current_batch_items = []
|
| 92 |
start_time = time.time()
|
| 93 |
expanded = expander.expand(current_batch)
|
expand_llm.py
CHANGED
|
@@ -2,19 +2,28 @@ import torch
|
|
| 2 |
from expand import *
|
| 3 |
from transformers import PreTrainedModel, PreTrainedTokenizer, PreTrainedTokenizerFast, BatchEncoding
|
| 4 |
from dataclasses import dataclass
|
|
|
|
| 5 |
|
| 6 |
type Tokenizer = PreTrainedTokenizer | PreTrainedTokenizerFast
|
| 7 |
|
| 8 |
def find_next_tokens(model: PreTrainedModel, inputs: BatchEncoding, tokenizer: Tokenizer) -> list[list[tuple[int, float]]]:
|
| 9 |
input_ids = inputs["input_ids"]
|
| 10 |
attention_mask = inputs["attention_mask"]
|
|
|
|
|
|
|
| 11 |
with torch.no_grad():
|
| 12 |
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
|
|
|
|
|
|
|
| 13 |
logits: torch.Tensor = outputs.logits[:, -1, :]
|
| 14 |
log_probs: torch.Tensor = torch.log_softmax(logits, dim=-1)
|
|
|
|
|
|
|
| 15 |
result = []
|
|
|
|
| 16 |
for probs in log_probs:
|
| 17 |
result.append([(i, p.item()) for i, p in enumerate(probs)])
|
|
|
|
| 18 |
return result
|
| 19 |
|
| 20 |
def prepare_inputs(contexts: list[list[int]], tokenizer: Tokenizer, device: torch.device) -> BatchEncoding:
|
|
@@ -29,10 +38,15 @@ class LLMBatchExpander(BatchExpander):
|
|
| 29 |
def expand(self, batch: Batch) -> BatchCandidates:
|
| 30 |
inputs = prepare_inputs([s.get_all_tokens() for s in batch.items], self.tokenizer, self.model.device)
|
| 31 |
next_tokens = find_next_tokens(self.model, inputs, self.tokenizer)
|
|
|
|
| 32 |
results = []
|
|
|
|
| 33 |
for s, next_tokens in zip(batch.items, next_tokens):
|
|
|
|
| 34 |
expansions = [Expansion(token=token, cost=cost) for token, cost in next_tokens]
|
| 35 |
results.append(TokenCandidates(series=s, expansions=expansions))
|
|
|
|
|
|
|
| 36 |
return BatchCandidates(items=results)
|
| 37 |
|
| 38 |
def create_stopping_criterion_llm(tokenizer: Tokenizer) -> Callable[[Series, Expansion], bool]:
|
|
@@ -42,7 +56,7 @@ def create_stopping_criterion_llm(tokenizer: Tokenizer) -> Callable[[Series, Exp
|
|
| 42 |
return d
|
| 43 |
token_str = tokenizer.decode([expansion.token])
|
| 44 |
starts_with_space = token_str.startswith(" ")
|
| 45 |
-
print(f"-----{token_str}-----, {starts_with_space=}")
|
| 46 |
is_first_token = len(series.expansions) == 0
|
| 47 |
if is_first_token and not starts_with_space:
|
| 48 |
return True
|
|
|
|
| 2 |
from expand import *
|
| 3 |
from transformers import PreTrainedModel, PreTrainedTokenizer, PreTrainedTokenizerFast, BatchEncoding
|
| 4 |
from dataclasses import dataclass
|
| 5 |
+
import time
|
| 6 |
|
| 7 |
type Tokenizer = PreTrainedTokenizer | PreTrainedTokenizerFast
|
| 8 |
|
| 9 |
def find_next_tokens(model: PreTrainedModel, inputs: BatchEncoding, tokenizer: Tokenizer) -> list[list[tuple[int, float]]]:
|
| 10 |
input_ids = inputs["input_ids"]
|
| 11 |
attention_mask = inputs["attention_mask"]
|
| 12 |
+
print("Running inference")
|
| 13 |
+
start_time = time.time()
|
| 14 |
with torch.no_grad():
|
| 15 |
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
|
| 16 |
+
print(f"Inference done, took {time.time() - start_time} seconds")
|
| 17 |
+
start_time = time.time()
|
| 18 |
logits: torch.Tensor = outputs.logits[:, -1, :]
|
| 19 |
log_probs: torch.Tensor = torch.log_softmax(logits, dim=-1)
|
| 20 |
+
print(f"Log probs done, took {time.time() - start_time} seconds")
|
| 21 |
+
start_time = time.time()
|
| 22 |
result = []
|
| 23 |
+
print(f"Resulting tensor: {log_probs.shape}")
|
| 24 |
for probs in log_probs:
|
| 25 |
result.append([(i, p.item()) for i, p in enumerate(probs)])
|
| 26 |
+
print(f"Result done, took {time.time() - start_time} seconds")
|
| 27 |
return result
|
| 28 |
|
| 29 |
def prepare_inputs(contexts: list[list[int]], tokenizer: Tokenizer, device: torch.device) -> BatchEncoding:
|
|
|
|
| 38 |
def expand(self, batch: Batch) -> BatchCandidates:
|
| 39 |
inputs = prepare_inputs([s.get_all_tokens() for s in batch.items], self.tokenizer, self.model.device)
|
| 40 |
next_tokens = find_next_tokens(self.model, inputs, self.tokenizer)
|
| 41 |
+
start_time = time.time()
|
| 42 |
results = []
|
| 43 |
+
print(f"Batch size: {len(batch.items)}, next tokens size: {len(next_tokens)}")
|
| 44 |
for s, next_tokens in zip(batch.items, next_tokens):
|
| 45 |
+
print(f"Series {s.id}, {len(next_tokens)=}")
|
| 46 |
expansions = [Expansion(token=token, cost=cost) for token, cost in next_tokens]
|
| 47 |
results.append(TokenCandidates(series=s, expansions=expansions))
|
| 48 |
+
print()
|
| 49 |
+
print(f"Token candidates done, took {time.time() - start_time} seconds")
|
| 50 |
return BatchCandidates(items=results)
|
| 51 |
|
| 52 |
def create_stopping_criterion_llm(tokenizer: Tokenizer) -> Callable[[Series, Expansion], bool]:
|
|
|
|
| 56 |
return d
|
| 57 |
token_str = tokenizer.decode([expansion.token])
|
| 58 |
starts_with_space = token_str.startswith(" ")
|
| 59 |
+
# print(f"-----{token_str}-----, {starts_with_space=}")
|
| 60 |
is_first_token = len(series.expansions) == 0
|
| 61 |
if is_first_token and not starts_with_space:
|
| 62 |
return True
|