performance
Browse files
app.py
CHANGED
|
@@ -17,7 +17,6 @@ from datasets import load_dataset
|
|
| 17 |
|
| 18 |
import time
|
| 19 |
import psutil
|
| 20 |
-
global_heatmap_time = []
|
| 21 |
|
| 22 |
def measure(name: str):
|
| 23 |
"""
|
|
@@ -610,8 +609,6 @@ async def predict_malware(files: List[UploadFile] = File(...), original_filename
|
|
| 610 |
# 將 SOM 結果附加到最終回傳
|
| 611 |
result["som_analysis"] = som_analysis
|
| 612 |
print(f"✅ SOM analysis complete.")
|
| 613 |
-
result["performance"] = performance
|
| 614 |
-
|
| 615 |
|
| 616 |
return result
|
| 617 |
|
|
@@ -653,57 +650,22 @@ def _split_into_basic_blocks(tokens):
|
|
| 653 |
blocks.append(cur)
|
| 654 |
return blocks
|
| 655 |
|
| 656 |
-
# def _block_attention_score(tokens, chunk_size=512):
|
| 657 |
-
# """
|
| 658 |
-
# 對單一 block 計算注意力:
|
| 659 |
-
# - 以 512 token 為單位分塊
|
| 660 |
-
# - 取最後一層 attention → head 平均 → 整體平均
|
| 661 |
-
# - 多個 chunk 取平均
|
| 662 |
-
# """
|
| 663 |
-
# scores = []
|
| 664 |
-
# with measure("heatmap_block") as m:
|
| 665 |
-
# out = model(**inputs, output_attentions=True)
|
| 666 |
-
|
| 667 |
-
# # 可累積到總 heatmap 時間
|
| 668 |
-
# global_heatmap_time.append(m.duration_ms)
|
| 669 |
-
|
| 670 |
-
# for i in range(0, len(tokens), chunk_size):
|
| 671 |
-
# chunk = tokens[i:i+chunk_size]
|
| 672 |
-
# inputs = tokenizer(
|
| 673 |
-
# chunk,
|
| 674 |
-
# return_tensors="pt",
|
| 675 |
-
# is_split_into_words=True,
|
| 676 |
-
# truncation=True,
|
| 677 |
-
# max_length=chunk_size,
|
| 678 |
-
# padding=False
|
| 679 |
-
# )
|
| 680 |
-
# inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
|
| 681 |
-
# with torch.no_grad():
|
| 682 |
-
# out = model(**inputs, output_attentions=True)
|
| 683 |
-
# last_attn = out.attentions[-1] # (1, heads, seq, seq)
|
| 684 |
-
# mean_attn = last_attn.mean(dim=1).mean() # scalar
|
| 685 |
-
# scores.append(mean_attn.item())
|
| 686 |
-
# performance_heatmap = sum(global_heatmap_time)
|
| 687 |
-
# global_heatmap_time.clear()
|
| 688 |
-
|
| 689 |
-
# return float(np.mean(scores)) if scores else 0.0
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
global_heatmap_time = []
|
| 693 |
-
|
| 694 |
def _block_attention_score(tokens, chunk_size=512):
|
| 695 |
"""
|
| 696 |
-
|
| 697 |
-
- 512
|
| 698 |
-
-
|
| 699 |
-
-
|
| 700 |
-
同時記錄 heatmap 計算的時間(ms)
|
| 701 |
"""
|
| 702 |
scores = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 703 |
|
| 704 |
for i in range(0, len(tokens), chunk_size):
|
| 705 |
chunk = tokens[i:i+chunk_size]
|
| 706 |
-
|
| 707 |
inputs = tokenizer(
|
| 708 |
chunk,
|
| 709 |
return_tensors="pt",
|
|
@@ -713,23 +675,13 @@ def _block_attention_score(tokens, chunk_size=512):
|
|
| 713 |
padding=False
|
| 714 |
)
|
| 715 |
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
out = model(**inputs, output_attentions=True)
|
| 721 |
-
|
| 722 |
-
# 記錄每次 block 的時間
|
| 723 |
-
global_heatmap_time.append(m.duration_ms)
|
| 724 |
-
|
| 725 |
-
# attention score
|
| 726 |
-
last_attn = out.attentions[-1] # (1, heads, seq, seq)
|
| 727 |
-
mean_attn = last_attn.mean(dim=1).mean() # scalar
|
| 728 |
scores.append(mean_attn.item())
|
| 729 |
-
|
| 730 |
return float(np.mean(scores)) if scores else 0.0
|
| 731 |
|
| 732 |
-
|
| 733 |
@app.post("/attention-heatmap")
|
| 734 |
async def attention_heatmap(file: UploadFile = File(...)):
|
| 735 |
"""
|
|
|
|
| 17 |
|
| 18 |
import time
|
| 19 |
import psutil
|
|
|
|
| 20 |
|
| 21 |
def measure(name: str):
|
| 22 |
"""
|
|
|
|
| 609 |
# 將 SOM 結果附加到最終回傳
|
| 610 |
result["som_analysis"] = som_analysis
|
| 611 |
print(f"✅ SOM analysis complete.")
|
|
|
|
|
|
|
| 612 |
|
| 613 |
return result
|
| 614 |
|
|
|
|
| 650 |
blocks.append(cur)
|
| 651 |
return blocks
|
| 652 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 653 |
def _block_attention_score(tokens, chunk_size=512):
|
| 654 |
"""
|
| 655 |
+
對單一 block 計算注意力:
|
| 656 |
+
- 以 512 token 為單位分塊
|
| 657 |
+
- 取最後一層 attention → head 平均 → 整體平均
|
| 658 |
+
- 多個 chunk 取平均
|
|
|
|
| 659 |
"""
|
| 660 |
scores = []
|
| 661 |
+
with measure("heatmap_block") as m:
|
| 662 |
+
out = model(**inputs, output_attentions=True)
|
| 663 |
+
|
| 664 |
+
# 可累積到總 heatmap 時間
|
| 665 |
+
global_heatmap_time.append(m.duration_ms)
|
| 666 |
|
| 667 |
for i in range(0, len(tokens), chunk_size):
|
| 668 |
chunk = tokens[i:i+chunk_size]
|
|
|
|
| 669 |
inputs = tokenizer(
|
| 670 |
chunk,
|
| 671 |
return_tensors="pt",
|
|
|
|
| 675 |
padding=False
|
| 676 |
)
|
| 677 |
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
|
| 678 |
+
with torch.no_grad():
|
| 679 |
+
out = model(**inputs, output_attentions=True)
|
| 680 |
+
last_attn = out.attentions[-1] # (1, heads, seq, seq)
|
| 681 |
+
mean_attn = last_attn.mean(dim=1).mean() # scalar
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 682 |
scores.append(mean_attn.item())
|
|
|
|
| 683 |
return float(np.mean(scores)) if scores else 0.0
|
| 684 |
|
|
|
|
| 685 |
@app.post("/attention-heatmap")
|
| 686 |
async def attention_heatmap(file: UploadFile = File(...)):
|
| 687 |
"""
|