lyi029 commited on
Commit
6e2340f
·
1 Parent(s): 11a7573

performance

Browse files
Files changed (1) hide show
  1. app.py +6 -60
app.py CHANGED
@@ -15,33 +15,6 @@ import io
15
  import numpy as np
16
  from datasets import load_dataset
17
 
18
- import time
19
- import psutil
20
-
21
- def measure(name: str):
22
- """
23
- 通用效能測量工具:
24
- 用法:
25
- with measure("inference"):
26
- outputs = model(**inputs)
27
- """
28
- class PerfBlock:
29
- def __enter__(self):
30
- self.start = time.perf_counter()
31
- self.process = psutil.Process()
32
- self.mem_before = self.process.memory_info().rss
33
- return self
34
-
35
- def __exit__(self, exc_type, exc_val, exc_tb):
36
- self.end = time.perf_counter()
37
- self.duration_ms = (self.end - self.start) * 1000
38
- self.mem_after = self.process.memory_info().rss
39
- self.mem_usage = self.mem_after - self.mem_before
40
- print(f"⏱️ [{name}] {self.duration_ms:.2f} ms, RAM +{self.mem_usage/1024:.1f} KB")
41
-
42
- return PerfBlock()
43
-
44
-
45
  # ⚠️ 注意: som_analyzer.py 必須存在於同一目錄
46
  try:
47
  from som_analyzer import SOMAnalyzer
@@ -396,13 +369,6 @@ async def predict_malware(files: List[UploadFile] = File(...), original_filename
396
 
397
  predictions = []
398
  all_embeddings = []
399
-
400
- performance = {
401
- "inference_ms": 0.0,
402
- "embedding_ms": 0.0,
403
- "heatmap_ms": 0.0
404
- }
405
-
406
 
407
  try:
408
  with torch.no_grad():
@@ -427,11 +393,7 @@ async def predict_malware(files: List[UploadFile] = File(...), original_filename
427
  inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
428
 
429
  # === Forward ===
430
- with measure("inference") as m:
431
- outputs = model(**inputs)
432
-
433
- performance["inference_ms"] += m.duration_ms
434
- #outputs = model(**inputs)
435
  logits = outputs.logits
436
 
437
  # === Softmax 機率 ===
@@ -449,21 +411,11 @@ async def predict_malware(files: List[UploadFile] = File(...), original_filename
449
  print(f" 🔍 Predicted ID: {predicted_id}, Label: {label_name}")
450
 
451
  # === Embedding ===
452
- # if hasattr(outputs, "hidden_states") and outputs.hidden_states is not None:
453
- # last_hidden = outputs.hidden_states[-1]
454
- # cls_embedding = last_hidden[:, 0, :].cpu().numpy()[0]
455
- # else:
456
- # cls_embedding = logits.cpu().numpy()[0]
457
- with measure("embedding") as m:
458
- if hasattr(outputs, "hidden_states"):
459
- last_hidden = outputs.hidden_states[-1]
460
- cls_embedding = last_hidden[:, 0, :].cpu().numpy()[0]
461
- else:
462
- cls_embedding = logits.cpu().numpy()[0]
463
-
464
- performance["embedding_ms"] += m.duration_ms
465
-
466
-
467
 
468
  # === Attention ===
469
  max_attention_score = 0.0
@@ -658,12 +610,6 @@ def _block_attention_score(tokens, chunk_size=512):
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(
 
15
  import numpy as np
16
  from datasets import load_dataset
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  # ⚠️ 注意: som_analyzer.py 必須存在於同一目錄
19
  try:
20
  from som_analyzer import SOMAnalyzer
 
369
 
370
  predictions = []
371
  all_embeddings = []
 
 
 
 
 
 
 
372
 
373
  try:
374
  with torch.no_grad():
 
393
  inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
394
 
395
  # === Forward ===
396
+ outputs = model(**inputs)
 
 
 
 
397
  logits = outputs.logits
398
 
399
  # === Softmax 機率 ===
 
411
  print(f" 🔍 Predicted ID: {predicted_id}, Label: {label_name}")
412
 
413
  # === Embedding ===
414
+ if hasattr(outputs, "hidden_states") and outputs.hidden_states is not None:
415
+ last_hidden = outputs.hidden_states[-1]
416
+ cls_embedding = last_hidden[:, 0, :].cpu().numpy()[0]
417
+ else:
418
+ cls_embedding = logits.cpu().numpy()[0]
 
 
 
 
 
 
 
 
 
 
419
 
420
  # === Attention ===
421
  max_attention_score = 0.0
 
610
  - 多個 chunk 取平均
611
  """
612
  scores = []
 
 
 
 
 
 
613
  for i in range(0, len(tokens), chunk_size):
614
  chunk = tokens[i:i+chunk_size]
615
  inputs = tokenizer(