shangetang commited on
Commit
5ede588
·
verified ·
1 Parent(s): f057857

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +26 -73
README.md CHANGED
@@ -3,51 +3,19 @@ license: apache-2.0
3
  ---
4
 
5
  ```
 
 
6
  import re
7
- from transformers import AutoTokenizer
8
- from vllm import LLM, SamplingParams
9
- import json
10
- import argparse
11
-
12
- # === Argument Parser Setup ===
13
- # We still allow specifying the model, GPU, etc., but no input/output files.
14
- parser = argparse.ArgumentParser(description="Demonstrate autoformalization for a single hardcoded example.")
15
- parser.add_argument('--model_path', default="/scratch/gpfs/st3812/models/Qwen3-32B", type=str, help="Path to the model.")
16
- parser.add_argument('--seed', default=42, type=int, help="Random seed for reproducibility.")
17
- parser.add_argument('--gpu', default=1, type=int, help="Number of GPUs for tensor parallelism.")
18
- parser.add_argument('--max_model_len', default=8192, type=int, help="Maximum model length for VLLM.")
19
-
20
- args = parser.parse_args()
21
-
22
- # --- 1. Hardcoded Example Problem ---
23
- # Instead of reading from a file, we define our single problem here.
24
- problem_entry = {
25
- "name": "mathd_algebra_47",
26
- "problem": "Let $f(x) = x^2 + 3x + 4$. Find the sum of the roots of $f(x) - 10 = 0$.",
27
- "answer": "-3"
28
- }
29
- print("📝 Using hardcoded example problem:")
30
- print(json.dumps(problem_entry, indent=2))
31
- print("-" * 30)
32
-
33
-
34
- # --- 2. Load Model and Tokenizer ---
35
- print(f"🚀 Loading model: {args.model_path}...")
36
- model = LLM(
37
- model=args.model_path,
38
- seed=args.seed,
39
- trust_remote_code=True,
40
- tensor_parallel_size=args.gpu,
41
- max_model_len=args.max_model_len
42
- )
43
- tokenizer = AutoTokenizer.from_pretrained(args.model_path, trust_remote_code=True)
44
- print("✅ Model and tokenizer loaded.")
45
- print("-" * 30)
46
 
47
 
48
- # --- 3. Prepare Prompt and Sampling Parameters ---
49
- problem_name = problem_entry.get("name")
50
- informal_statement_content = f"{problem_entry.get('problem', '')} The answer is {problem_entry.get('answer', '')}"
51
 
52
  # Construct the prompt for the model
53
  user_prompt_content = (
@@ -55,32 +23,24 @@ user_prompt_content = (
55
  f"Use the following theorem name: {problem_name}\n"
56
  f"The natural language statement is: \n"
57
  f"{informal_statement_content}"
 
58
  )
59
 
60
- messages = [{"role": "user", "content": user_prompt_content}]
61
- formatted_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
62
 
63
- # Set sampling parameters for generating one sample
64
- sampling_params = SamplingParams(
65
- temperature=0.7,
66
- top_p=0.8,
67
- max_tokens=2048, # Max new tokens to generate
68
- n=1, # Generate exactly one sample
69
- seed=args.seed,
70
- )
71
 
72
- print("💬 Generated Prompt:\n" + formatted_prompt)
73
- print("-" * 30)
 
74
 
 
75
 
76
- # --- 4. Run Generation ---
77
- print("🧠 Generating formal statement...")
78
- raw_model_outputs = model.generate([formatted_prompt], sampling_params, use_tqdm=False)
79
- print("✅ Generation complete.")
80
- print("-" * 30)
81
 
82
 
83
- # --- 5. Process and Print the Result ---
 
84
  def extract_code(text_input):
85
  """Extracts the last Lean 4 code block from the model's output."""
86
  try:
@@ -89,19 +49,12 @@ def extract_code(text_input):
89
  except Exception:
90
  return "Error during code extraction."
91
 
92
- # Since we generated one output for one prompt, we access the first element.
93
- model_output_text = raw_model_outputs[0].outputs[0].text.strip()
94
  extracted_code = extract_code(model_output_text)
95
 
96
- # Prepare a final dictionary with all relevant information
97
- final_result = {
98
- "problem_name": problem_name,
99
- "informal_statement": informal_statement_content,
100
- "full_model_output": model_output_text,
101
- "extracted_lean_code": extracted_code
102
- }
103
-
104
- # Print the final, structured result
105
- print("🎉 Final Result:")
106
- print(json.dumps(final_result, indent=4))
107
  ```
 
3
  ---
4
 
5
  ```
6
+ from transformers import AutoModelForCausalLM, AutoTokenizer
7
+ import torch
8
  import re
9
+ torch.manual_seed(30)
10
+
11
+ model_id = "Goedel-LM/Goedel-Formalizer-V2-8B"
12
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
13
+ model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
 
16
+ problem_name = "test_problem"
17
+ informal_statement_content = "Let $f(x) = x^2 + 3x + 4$. Find the sum of the roots of $f(x) - 10 = 0$. Show the answer is -3."
18
+
19
 
20
  # Construct the prompt for the model
21
  user_prompt_content = (
 
23
  f"Use the following theorem name: {problem_name}\n"
24
  f"The natural language statement is: \n"
25
  f"{informal_statement_content}"
26
+ f"Think before you provide the lean statement."
27
  )
28
 
 
 
29
 
 
 
 
 
 
 
 
 
30
 
31
+ chat = [
32
+ {"role": "user", "content": user_prompt_content},
33
+ ]
34
 
35
+ inputs = tokenizer.apply_chat_template(chat, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(model.device)
36
 
37
+ import time
38
+ start = time.time()
39
+ outputs = model.generate(inputs, max_new_tokens=16384, temperature = 0.9, do_sample = True, top_k=50, top_p=0.95)
 
 
40
 
41
 
42
+ model_output_text = tokenizer.batch_decode(outputs)[0]
43
+
44
  def extract_code(text_input):
45
  """Extracts the last Lean 4 code block from the model's output."""
46
  try:
 
49
  except Exception:
50
  return "Error during code extraction."
51
 
52
+
 
53
  extracted_code = extract_code(model_output_text)
54
 
55
+ print(time.time() - start)
56
+ print("output:", model_output_text)
57
+ print("lean4 statement:", extracted_code)
58
+
59
+
 
 
 
 
 
 
60
  ```