Skip to content

Commit 7c444e8

Browse files
Xrekiclaude
andauthored
Support extract use CPU and optimize some codes. (#707)
* Support extract use CPU and optimize some codes. * Fix workspace path. * Support configurable extract and verify timeouts with CPU/GPU defaults. - GraphNetAgent: add extract_timeout and verify_timeout parameters - parallel_extract: add --extract-timeout and --verify-timeout CLI args - Default timeouts differ by device: GPU: extract=1000s, verify=300s CPU: extract=2000s, verify=600s - Fix typo: os.envion -> os.environ Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Add --use-llm flag to parallel_extract for configurable LLM retry. - New CLI arg --use-llm (default: true) controls llm_retry in GraphNetAgent - Pass llm_retry through worker_fn and _resolve_config Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Refactor extract_sample return to ExtractionStatus enum and split success rates. - Introduce ExtractionStatus(str, Enum): OK, VERIFY_FAILED, EXTRACT_FAILED, ERROR - GraphNetAgent.extract_sample() now returns ExtractionStatus instead of bool - parallel_extract tracks and prints both overall and extraction-only success rates - Per-GPU/Worker summary also shows both rates Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 540d57f commit 7c444e8

5 files changed

Lines changed: 267 additions & 119 deletions

File tree

graph_net/agent/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
from Hugging Face models.
66
"""
77

8-
from graph_net.agent.graph_net_agent import GraphNetAgent
8+
from graph_net.agent.graph_net_agent import ExtractionStatus, GraphNetAgent
99

10-
__all__ = ["GraphNetAgent"]
10+
__all__ = ["GraphNetAgent", "ExtractionStatus"]

graph_net/agent/graph_extractor/subprocess_graph_extractor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
class SubprocessGraphExtractor(BaseGraphExtractor):
2323
"""Extractor that runs script in subprocess"""
2424

25-
def __init__(self, workspace: str, timeout: int = DEFAULT_TIMEOUT):
25+
def __init__(self, workspace: str, timeout: int | None = None):
2626
"""
2727
Args:
2828
workspace: Workspace root directory
29-
timeout: Timeout in seconds for script execution
29+
timeout: Timeout in seconds for script execution (default 1000s)
3030
"""
3131
self.workspace = Path(workspace)
32-
self.timeout = timeout
32+
self.timeout = timeout if timeout is not None else DEFAULT_TIMEOUT
3333
self.logger = logging.getLogger(self.__class__.__name__)
3434

3535
def extract(self, code_path: Path, model_id: str) -> Path:

graph_net/agent/graph_net_agent.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
import os
5+
from enum import Enum
56
from pathlib import Path
67
from typing import Optional
78

@@ -23,6 +24,15 @@
2324
from graph_net.agent.sample_verifier import ForwardVerifier
2425

2526

27+
class ExtractionStatus(str, Enum):
28+
"""Extraction result status for a single model."""
29+
30+
OK = "ok"
31+
VERIFY_FAILED = "verify_failed"
32+
EXTRACT_FAILED = "extract_failed"
33+
ERROR = "error"
34+
35+
2636
class GraphNetAgent:
2737
"""GraphNet automatic sample extraction agent"""
2838

@@ -31,16 +41,22 @@ def __init__(
3141
workspace: Optional[str] = None,
3242
hf_token: Optional[str] = None,
3343
llm_retry: bool = True,
44+
extract_timeout: Optional[int] = None,
45+
verify_timeout: Optional[int] = None,
3446
):
3547
"""
3648
Initialize GraphNet Agent
3749
3850
Args:
39-
workspace: Workspace root directory. Defaults to
40-
$GRAPH_NET_EXTRACT_WORKSPACE or ~/graphnet_workspace.
41-
hf_token: HuggingFace API token (optional)
42-
llm_retry: If True and ducc/claude CLI is available, retry failed
43-
extractions up to 2 times with LLM-fixed scripts.
51+
workspace: Workspace root directory. Defaults to
52+
$GRAPH_NET_EXTRACT_WORKSPACE or ~/graphnet_workspace.
53+
hf_token: HuggingFace API token (optional)
54+
llm_retry: If True and ducc/claude CLI is available, retry failed
55+
extractions up to 2 times with LLM-fixed scripts.
56+
extract_timeout: Timeout in seconds for graph extraction subprocess
57+
(default None -> 1000s).
58+
verify_timeout: Timeout in seconds for forward verification subprocess
59+
(default None -> 300s).
4460
"""
4561
if workspace is None:
4662
workspace = os.environ.get(
@@ -63,14 +79,15 @@ def __init__(
6379
self.metadata_analyzer = ConfigMetadataAnalyzer()
6480
self.code_generator = TemplateCodeGenerator()
6581
self.graph_extractor = SubprocessGraphExtractor(
66-
workspace=str(self.workspace.workspace_root)
82+
workspace=str(self.workspace.workspace_root),
83+
timeout=extract_timeout,
6784
)
68-
self.sample_verifier = ForwardVerifier()
85+
self.sample_verifier = ForwardVerifier(timeout=verify_timeout)
6986

7087
# LLM fixer — only created when llm_retry is requested
7188
self.llm_fixer: Optional[LLMCodeFixer] = LLMCodeFixer() if llm_retry else None
7289

73-
def extract_sample(self, model_id: str) -> bool:
90+
def extract_sample(self, model_id: str) -> ExtractionStatus:
7491
"""
7592
Execute complete sample extraction pipeline from HuggingFace model ID.
7693
@@ -82,7 +99,10 @@ def extract_sample(self, model_id: str) -> bool:
8299
model_id: HuggingFace model ID (e.g., "bert-base-uncased")
83100
84101
Returns:
85-
True if sample extraction succeeded, False otherwise
102+
ExtractionStatus.OK – extraction and verification both passed
103+
ExtractionStatus.VERIFY_FAILED – extraction succeeded but verification failed
104+
ExtractionStatus.EXTRACT_FAILED – extraction (or pre-extraction) failed
105+
ExtractionStatus.ERROR – unexpected error
86106
"""
87107
try:
88108
self.logger.info(f"Starting extraction for model: {model_id}")
@@ -104,21 +124,24 @@ def extract_sample(self, model_id: str) -> bool:
104124

105125
if self.is_duplicate_sample(sample_dir):
106126
self.logger.info("Duplicate sample detected, skipping verification")
107-
return True
127+
return ExtractionStatus.OK
108128

109129
if not self.sample_verifier.verify(sample_dir):
110130
self.logger.error("Sample verification failed")
111-
return False
131+
return ExtractionStatus.VERIFY_FAILED
112132

113133
self.logger.info(f"Successfully extracted sample for {model_id}")
114-
return True
134+
return ExtractionStatus.OK
115135

116-
except (AnalysisError, CodeGenError, ExtractionError, VerificationError) as e:
136+
except VerificationError as e:
117137
self.logger.error(f"Extraction failed for {model_id}: {e}")
118-
return False
138+
return ExtractionStatus.VERIFY_FAILED
139+
except (AnalysisError, CodeGenError, ExtractionError) as e:
140+
self.logger.error(f"Extraction failed for {model_id}: {e}")
141+
return ExtractionStatus.EXTRACT_FAILED
119142
except Exception as e:
120143
self.logger.error(f"Unexpected error for {model_id}: {e}", exc_info=True)
121-
return False
144+
return ExtractionStatus.ERROR
122145

123146
def _llm_retry(
124147
self,

0 commit comments

Comments
 (0)