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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from typing import List
|
| 3 |
+
from tqdm import tqdm
|
| 4 |
+
from random import randint, random, sample
|
| 5 |
+
from astpretty import pprint
|
| 6 |
+
from copy import deepcopy
|
| 7 |
+
from string import ascii_letters
|
| 8 |
+
import ast
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def write_rows(rows):
|
| 12 |
+
with open('data.10_simple_variations.jsonl', 'a') as f:
|
| 13 |
+
f.writelines(rows)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class AlternativeList(ast.NodeTransformer):
|
| 17 |
+
def __init__(self):
|
| 18 |
+
self.n_changes = 0
|
| 19 |
+
super().__init__()
|
| 20 |
+
|
| 21 |
+
def visit_List(self, node: List):
|
| 22 |
+
code = ast.unparse(node)
|
| 23 |
+
try:
|
| 24 |
+
list_val = eval(code)
|
| 25 |
+
except Exception:
|
| 26 |
+
return node
|
| 27 |
+
|
| 28 |
+
for _ in range(randint(0, 3)):
|
| 29 |
+
list_val.append(randint(-999, 999))
|
| 30 |
+
|
| 31 |
+
if not list_val:
|
| 32 |
+
list_val.append(randint(-999, 999))
|
| 33 |
+
|
| 34 |
+
list_val = sample(list_val, randint(1, len(list_val)))
|
| 35 |
+
|
| 36 |
+
self.n_changes += 1
|
| 37 |
+
return ast.parse(str(list_val)).body[0].value
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class AlternativeConstant(ast.NodeTransformer):
|
| 41 |
+
def __init__(self):
|
| 42 |
+
self.n_changes = 0
|
| 43 |
+
super().__init__()
|
| 44 |
+
|
| 45 |
+
def visit_Constant(self, node):
|
| 46 |
+
'''
|
| 47 |
+
Switch constant values to simple variations
|
| 48 |
+
'''
|
| 49 |
+
if type(node.value) is int:
|
| 50 |
+
if randint(0, 1) == 1:
|
| 51 |
+
node.value = randint(-9, 9)
|
| 52 |
+
else:
|
| 53 |
+
node.value = randint(-999, 999)
|
| 54 |
+
elif type(node.value) is str:
|
| 55 |
+
if randint(0, 1) == 1 and node.value:
|
| 56 |
+
if node.value:
|
| 57 |
+
node.value = ''.join(sample(node.value, randint(1, len(node.value))))
|
| 58 |
+
else:
|
| 59 |
+
self.n_changes -= 1
|
| 60 |
+
else:
|
| 61 |
+
node.value = ''.join(sample(ascii_letters, randint(1, 4)))
|
| 62 |
+
elif type(node.value) is float:
|
| 63 |
+
if randint(0, 1) == 1:
|
| 64 |
+
node.value = random()
|
| 65 |
+
else:
|
| 66 |
+
node.value = random() * randint(-999, 999)
|
| 67 |
+
elif type(node.value) is bool:
|
| 68 |
+
node.value = bool(randint(0, 1))
|
| 69 |
+
else:
|
| 70 |
+
self.n_changes -= 1
|
| 71 |
+
self.n_changes += 1
|
| 72 |
+
return super().visit_Constant(node)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
class AlternativeNames(ast.NodeTransformer):
|
| 76 |
+
|
| 77 |
+
def visit_Name(self, node):
|
| 78 |
+
return ast.copy_location(ast.Subscript(
|
| 79 |
+
value=ast.Name(id='data', ctx=ast.Load()),
|
| 80 |
+
slice=ast.Index(value=ast.Str(s=node.id)),
|
| 81 |
+
ctx=node.ctx
|
| 82 |
+
), node)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def state_dict_to_str(state):
|
| 86 |
+
vals = []
|
| 87 |
+
for k, v in state.items():
|
| 88 |
+
vals.append(
|
| 89 |
+
f'{k} = {v}'
|
| 90 |
+
)
|
| 91 |
+
vals = sorted(vals)
|
| 92 |
+
return ';'.join(vals)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def trace_code(start_state: str, code: str):
|
| 96 |
+
state = {}
|
| 97 |
+
try:
|
| 98 |
+
exec(start_state, {}, state)
|
| 99 |
+
except Exception:
|
| 100 |
+
return
|
| 101 |
+
start_state = dict(state)
|
| 102 |
+
try:
|
| 103 |
+
exec(code, {}, state)
|
| 104 |
+
except Exception:
|
| 105 |
+
return
|
| 106 |
+
return state_dict_to_str(start_state), code, state_dict_to_str(state)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def make_alternative_rows(start, code):
|
| 110 |
+
variations = {}
|
| 111 |
+
n_tries = 0
|
| 112 |
+
state_root = ast.parse(start)
|
| 113 |
+
|
| 114 |
+
while len(variations) < 10 and n_tries < 20:
|
| 115 |
+
|
| 116 |
+
node_transformer = AlternativeList()
|
| 117 |
+
alt_state_root = node_transformer.visit(deepcopy(state_root))
|
| 118 |
+
|
| 119 |
+
if node_transformer.n_changes < 1:
|
| 120 |
+
node_transformer = AlternativeConstant()
|
| 121 |
+
alt_state_root = node_transformer.visit(deepcopy(alt_state_root))
|
| 122 |
+
if node_transformer.n_changes < 1:
|
| 123 |
+
n_tries += 10
|
| 124 |
+
|
| 125 |
+
alt_start = ast.unparse(alt_state_root)
|
| 126 |
+
|
| 127 |
+
alt_start_code_end = trace_code(alt_start, code)
|
| 128 |
+
if alt_start_code_end:
|
| 129 |
+
variations[alt_start] = alt_start_code_end
|
| 130 |
+
n_tries += 1
|
| 131 |
+
|
| 132 |
+
'''
|
| 133 |
+
for each variations
|
| 134 |
+
switch out variable names (maintain alphabetical order)
|
| 135 |
+
'''
|
| 136 |
+
return [
|
| 137 |
+
json.dumps({'start': st, 'code': cd, 'end': en}) + '\n' for st, cd, en in variations.values()
|
| 138 |
+
]
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
with open('new_all_states.txt', 'r') as f:
|
| 142 |
+
rows = []
|
| 143 |
+
prev_lines = []
|
| 144 |
+
for i, line in tqdm(enumerate(f), total=9_000_000):
|
| 145 |
+
line = line.strip()
|
| 146 |
+
|
| 147 |
+
if line[-1] != ';':
|
| 148 |
+
prev_lines.append(line)
|
| 149 |
+
continue
|
| 150 |
+
elif prev_lines:
|
| 151 |
+
line = '\n'.join(prev_lines) + '\n' + line
|
| 152 |
+
prev_lines = []
|
| 153 |
+
|
| 154 |
+
start, code_end = line.split('; code: ')
|
| 155 |
+
start = start.removeprefix('state: ')
|
| 156 |
+
code, end = code_end.split('; output: ')
|
| 157 |
+
end = end.removesuffix(';')
|
| 158 |
+
|
| 159 |
+
rows.append(json.dumps({
|
| 160 |
+
'start': start, 'code': code, 'end': end
|
| 161 |
+
}) + '\n')
|
| 162 |
+
rows += make_alternative_rows(start, code)
|
| 163 |
+
if len(rows) > 100_000:
|
| 164 |
+
breakpoint()
|
| 165 |
+
write_rows(rows)
|
| 166 |
+
rows = []
|