File size: 6,288 Bytes
cb4d9ac
 
 
c5614cb
cb4d9ac
 
 
 
 
 
c5614cb
 
 
 
 
 
cb4d9ac
 
 
c5614cb
 
 
cb4d9ac
 
c5614cb
cb4d9ac
 
 
c5614cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb4d9ac
c5614cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb4d9ac
c5614cb
 
 
 
 
 
 
 
 
 
cb4d9ac
c5614cb
cb4d9ac
c5614cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb4d9ac
 
c5614cb
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import random
import json
import operator
import math

num_samples = [20000, 50000, 230000, 500000]
min_values = [-9.99, -19.99, -99.99, -99.99]
max_values = [9.99, 19.99, 99.99, 99.99]
vals_floats = ["%.2f", "%.3f", "%.3f", "%.4f"]
result_floats = ["%.2f", "%.3f", "%.4f", "%.4f"]
seed = 42

random.seed(seed)

# Binary operations
binary_operations = {
    '+': operator.add,
    '-': operator.sub,
    '*': operator.mul,
    '/': operator.truediv,
    #'**': operator.pow, # it makes tons of troubles, but if u wanna contribute with a functional fix (L)
    '%': operator.mod
}

# Safe division to avoid division by zero
def safe_division(numerator, denominator):
    return numerator if denominator == 0 else numerator / denominator

# Unary operations
def safe_sqrt(x):
    return math.sqrt(x) if x >= 0 else 0

def safe_log(x):
    return math.log(x) if x > 0 else 0

unary_operations = {
    'sqrt': safe_sqrt,
    'sin': math.sin,
    'cos': math.cos,
    'log': safe_log
}

def format_result(result, format_spec):
    if isinstance(result, complex):
        real_part = format_spec % result.real
        imag_part = format_spec % result.imag
        return f"{real_part} + {imag_part}j"
    else:
        return format_spec % result

def safe_modulo(numerator, denominator):
    return 0 if denominator == 0 else numerator % denominator

def safe_power(base, exponent):
    if base == 0 and exponent < 0:
        return 0  # or some other value that signifies an undefined operation
    else:
        return base ** exponent

def generate_sub_expression(min_value, max_value, val_float):
    num1 = float(val_float % random.uniform(min_value, max_value))
    num2 = float(val_float % random.uniform(min_value, max_value))
    operation = random.choice(list(binary_operations.keys()))

    if operation == '/':
        result = safe_division(num1, num2)
    elif operation == '%':
        result = safe_modulo(num1, num2)
    else:
        result = binary_operations[operation](num1, num2)

    expression = f"({num1} {operation} {num2})"
    return expression, result

def generate_expression(num_samples, min_value, max_value, val_float, result_float):
    data = []
    for _ in range(num_samples):
        sub_ops_count = random.randint(0, 2)  # Number of sub-operations (0, 1, or 2), the code is not tailored for more cases, but feel free to improve it.
        sub_expressions = [generate_sub_expression(min_value, max_value, val_float) for _ in range(sub_ops_count)]

        # Main operation
        main_num1 = float(val_float % random.uniform(min_value, max_value))
        main_num2 = float(val_float % random.uniform(min_value, max_value))
        main_operation = random.choice(list(binary_operations.keys()))

        # Embed sub-expressions into main expression
        if sub_expressions:
            if len(sub_expressions) == 1:
                sub_expr, sub_result = sub_expressions[0]
                if random.choice([True, False]):
                    main_expression = f"{sub_expr} {main_operation} {main_num2}"
                    if main_operation == '/':
                        main_result = safe_division(sub_result, main_num2)
                    elif main_operation == '%':
                        main_result = safe_modulo(sub_result, main_num2)
                    else:
                        main_result = binary_operations[main_operation](sub_result, main_num2)
                else:
                    main_expression = f"{main_num1} {main_operation} {sub_expr}"
                    if main_operation == '/':
                        main_result = safe_division(main_num1, sub_result)
                    elif main_operation == '%':
                        main_result = safe_modulo(main_num1, sub_result)
                    else:
                        main_result = binary_operations[main_operation](main_num1, sub_result)
            else:
                sub_expr1, sub_result1 = sub_expressions[0]
                sub_expr2, sub_result2 = sub_expressions[1]
                main_expression = f"{sub_expr1} {main_operation} {sub_expr2}"
                if main_operation == '/':
                    main_result = safe_division(sub_result1, sub_result2)
                elif main_operation == '%':
                    main_result = safe_modulo(sub_result1, sub_result2)
                else:
                    main_result = binary_operations[main_operation](sub_result1, sub_result2)
        else:
            main_expression = f"{main_num1} {main_operation} {main_num2}"
            if main_operation == '/':
                main_result = safe_division(main_num1, main_num2)
            elif main_operation == '%':
                main_result = safe_modulo(main_num1, main_num2)
            else:
                main_result = binary_operations[main_operation](main_num1, main_num2)

        output = format_result(main_result, result_float)
        data.append({'instruction': main_expression, 'output': output})

    return data


# DPO (Follows Ultrabinarized Format)
def make_prompt(expression, output):
    prompt = {
        'prompt': 'What is the result of the following expression?',
        'chosen': [],
        'rejected': [],
        'messages': []
    }
    prompt['chosen'].append({'content': prompt['prompt'] + '\n' + expression, 'role': 'user'})
    prompt['chosen'].append({'content': output, 'role': 'assistant'})
    prompt['rejected'].append({'content': prompt['prompt'] + '\n' + expression, 'role': 'user'})
    prompt['rejected'].append({'content': output[0:2], 'role': 'assistant'})
    prompt['messages'].append({'content': prompt['prompt'] + '\n' + expression, 'role': 'user'})
    prompt['messages'].append({'content': output, 'role': 'assistant'})
    return prompt


# Generate and write data for each set of parameters
for idx, sample_count in enumerate(num_samples):
    data = generate_expression(sample_count, min_values[idx], max_values[idx], vals_floats[idx], result_floats[idx])
    dpo_data = [make_prompt(elem['instruction'], elem['output']) for elem in data]
    out_file = f'arithmetic-float-complex_{idx}.json'
    with open(out_file, 'w') as f:
        json.dump(data, f)
    out_file = f'arithmetic-float-complex_DPO_{idx}.json'
    with open(out_file, 'w') as f:
        json.dump(dpo_data, f)