udiboy1209 commited on
Commit
e78b7eb
·
1 Parent(s): f0e5581

Add real world dataset

Browse files
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ __pycache__/
2
+ dataset/
3
+ tokenized/
real_world_dataset/collect_dataset.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from os import makedirs
2
+ from os.path import join as pjoin
3
+ import json
4
+ import logging
5
+ from tqdm import tqdm
6
+ import numpy as np
7
+
8
+ from remend.tools.disassemble import DisassemblerX64, DisassemblerARM32, DisassemblerAArch64
9
+
10
+ # Dont show warnings
11
+ logging.getLogger("cle").setLevel(logging.ERROR)
12
+
13
+ BUILD="build"
14
+ DATASET="dataset"
15
+ # # "linux_color_cvt": [
16
+ # # {"name": "transfer_rgb_to_oprgb", "eqn": "pow(x, 1/2.19921875)"},
17
+ # # {"name": "transfer_rgb_to_dcip3", "eqn": "pow(x, 1/2.6)"},
18
+ # # {"name": "transfer_rgb_to_smpte2084", "eqn": "pow(x, 1/2.6)"},
19
+ # # ]
20
+
21
+ with open("dataset_info.json", "r") as f:
22
+ BINS = json.load(f)
23
+
24
+ DTYPES = ["f", "d"]
25
+ OPTS = [0, 1, 2] # 3
26
+ ARCH = [
27
+ ("arm32", DisassemblerARM32),
28
+ ("aarch64", DisassemblerAArch64),
29
+ ("x64", DisassemblerX64),
30
+ ]
31
+
32
+ def match_constants(asm_c, eqn_c):
33
+ from math import pi, tan, sqrt
34
+ cmap = {}
35
+ for ac, acv in asm_c.items():
36
+ acv = float(acv)
37
+ for ec, ecv in eqn_c.items():
38
+ ecv = eval(ecv)
39
+ if abs(acv - ecv) < 1e-3:
40
+ cmap[ec] = str(ac)
41
+ return cmap
42
+
43
+ makedirs(DATASET, exist_ok=True)
44
+ for arch, Dclass in ARCH:
45
+ asmf = open(pjoin(DATASET, arch + ".asm"), "w")
46
+ eqnf = open(pjoin(DATASET, arch + ".eqn"), "w")
47
+ infof = open(pjoin(DATASET, arch + ".info"), "w")
48
+ print("Running:", arch)
49
+ samples = [(binary, func, data, opt, dtype)
50
+ for binary in BINS
51
+ for func, data in BINS[binary].items()
52
+ for opt in OPTS
53
+ for dtype in DTYPES
54
+ ]
55
+ for binary, func, data, opt, dtype in tqdm(samples):
56
+ binpath = pjoin(BUILD, f"{binary}_{arch}_O{opt}.elf")
57
+ fname = f"{func}_{dtype}"
58
+ if binary == "nn_funcs":
59
+ fname += "_"
60
+ D = Dclass(binpath)
61
+ diss = D.disassemble(fname)
62
+ info = {"constants": D.constants, "bin": binpath, "func": fname,
63
+ "eqn": data["eqn"], "eqn_constants": data["constants"]}
64
+ prefix = data["prefix"][:]
65
+
66
+ cmap = match_constants(D.constants, data["constants"])
67
+ if len(cmap) != len(data["constants"]):
68
+ info["cmap"] = cmap
69
+ else:
70
+ for ec, ac in cmap.items():
71
+ prefix = prefix.replace(ec, "<PH>"+ac)
72
+ prefix = prefix.replace("<PH>", "k")
73
+
74
+ asmf.write(diss + "\n")
75
+ eqnf.write(prefix + "\n")
76
+ json.dump(info, infof)
77
+ infof.write("\n")
78
+ asmf.close()
79
+ infof.close()
real_world_dataset/custom_source/arduino_sensorkit.c ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <math.h>
2
+ #include <stdio.h>
3
+
4
+ float calcAltitude_f(float pressure)
5
+ {
6
+ float A = pressure/101325.0f;
7
+ float B = 1.0f/5.25588f;
8
+ float C = powf(A,B);
9
+ C = 1.0f - C;
10
+ C = C /0.0000225577f;
11
+ return C;
12
+ }
13
+
14
+ float convertCtoF_f(float c) {
15
+ return c * 9.0f / 5.0f + 32.0f;
16
+ }
17
+
18
+ double calcAltitude_d(double pressure)
19
+ {
20
+ double A = pressure/101325.0;
21
+ double B = 1.0/5.25588;
22
+ double C = pow(A,B);
23
+ C = 1.0 - C;
24
+ C = C /0.0000225577;
25
+ return C;
26
+ }
27
+
28
+ double convertCtoF_d(double c) {
29
+ return c * 9.0f / 5.0f + 32.0f;
30
+ }
31
+
32
+ int main() {
33
+ float xf;
34
+ double xd;
35
+ int l;
36
+
37
+ l = scanf("%f", &xf);
38
+ l = scanf("%lf", &xd);
39
+
40
+ printf("%f\n", calcAltitude_f(xf));
41
+ printf("%lf\n", calcAltitude_d(xd));
42
+
43
+ printf("%f\n", convertCtoF_f(xf));
44
+ printf("%lf\n", convertCtoF_d(xd));
45
+ }
real_world_dataset/custom_source/ardupilot.c ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <math.h>
2
+ #include <stdio.h>
3
+
4
+ const double PI = 3.14159265359;
5
+ const float PI_F = 3.14159265359f;
6
+
7
+ #define DEG_TO_RAD_F (PI_F / 180.0f)
8
+ #define DEG_TO_RAD_D (PI / 180.0)
9
+ #define RAD_TO_DEG_F (1.0f / DEG_TO_RAD_F)
10
+ #define RAD_TO_DEG_D (1.0 / DEG_TO_RAD_D)
11
+ #define GRAVITY_MSS_F 9.80665f
12
+ #define GRAVITY_MSS_D 9.80665
13
+
14
+ float degF_to_Kelvin_f(float temp_f) { return (temp_f + 459.67f) * 0.55556F; }
15
+ double degF_to_Kelvin_d(double temp_f) { return (temp_f + 459.67) * 0.55556; }
16
+
17
+ float radians_f(float deg) { return deg * DEG_TO_RAD_F; }
18
+ float degrees_f(float rad) { return rad * RAD_TO_DEG_F; }
19
+
20
+ double radians_d(double deg) { return deg * DEG_TO_RAD_D; }
21
+ double degrees_d(double rad) { return rad * RAD_TO_DEG_D; }
22
+
23
+ float sq_f(const float v) { return v*v; }
24
+ double sq_d(const double v) { return v*v; }
25
+
26
+ double w_d(const double dHertz) { return dHertz * 2.0 * PI; }
27
+ float w_f(const float dHertz) { return dHertz * 2.0f * PI_F; }
28
+
29
+ float angle_to_accel_f(float angle_deg) { return GRAVITY_MSS_F * tanf(angle_deg*DEG_TO_RAD_F); }
30
+ float accel_to_angle_f(float accel) { return atanf(accel/GRAVITY_MSS_F)*RAD_TO_DEG_F; }
31
+ double angle_to_accel_d(double angle_deg) { return GRAVITY_MSS_D * tan(angle_deg*DEG_TO_RAD_D); }
32
+ double accel_to_angle_d(double accel) { return atan(accel/GRAVITY_MSS_D)*RAD_TO_DEG_D; }
33
+
34
+ #define SQRT_2_3_F 0.816496580927726f
35
+ #define SQRT_6_F 2.449489742783178f
36
+ #define SQRT_2_3_D 0.816496580927726
37
+ #define SQRT_6_D 2.449489742783178
38
+ static const float TAU_FACTOR_F = SQRT_6_F / 24.0f;
39
+ static const double TAU_FACTOR_D = SQRT_6_D / 24.0;
40
+
41
+ // Helper function used for Quinn's frequency estimation
42
+ float tau_f(const float x)
43
+ {
44
+ float p1 = logf(3.0f * x*x + 6.0f * x + 1.0f);
45
+ float part1 = x + 1.0f - SQRT_2_3_F;
46
+ float part2 = x + 1.0f + SQRT_2_3_F;
47
+ float p2 = logf(part1 / part2);
48
+ return (0.25f * p1 - TAU_FACTOR_F * p2);
49
+ }
50
+
51
+ // Helper function used for Quinn's frequency estimation
52
+ double tau_d(const double x)
53
+ {
54
+ double p1 = log(3.0 * x*x + 6.0 * x + 1.0);
55
+ double part1 = x + 1.0 - SQRT_2_3_D;
56
+ double part2 = x + 1.0 + SQRT_2_3_D;
57
+ double p2 = log(part1 / part2);
58
+ return (0.25 * p1 - TAU_FACTOR_D * p2);
59
+ }
60
+
61
+
62
+ int main() {
63
+ float xf;
64
+ double xd;
65
+ int l;
66
+
67
+ l = scanf("%f", &xf);
68
+ l = scanf("%lf", &xd);
69
+
70
+ printf("%f\n", degF_to_Kelvin_f(xf));
71
+ printf("%lf\n", degF_to_Kelvin_d(xd));
72
+
73
+ printf("%f\n", radians_f(xf));
74
+ printf("%lf\n", radians_d(xd));
75
+
76
+ printf("%f\n", degrees_f(xf));
77
+ printf("%lf\n", degrees_d(xd));
78
+
79
+ printf("%f\n", w_f(xf));
80
+ printf("%lf\n", w_d(xd));
81
+
82
+ printf("%f\n", angle_to_accel_f(xf));
83
+ printf("%lf\n", angle_to_accel_d(xd));
84
+
85
+ printf("%f\n", accel_to_angle_f(xf));
86
+ printf("%lf\n", accel_to_angle_d(xd));
87
+
88
+ printf("%f\n", tau_f(xf));
89
+ printf("%lf\n", tau_d(xd));
90
+
91
+ printf("%f\n", sq_f(xf));
92
+ printf("%lf\n", sq_d(xd));
93
+ }
real_world_dataset/custom_source/cleanflight.c ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <math.h>
2
+ #include <stdio.h>
3
+
4
+ #define M_PIf 3.14159265358979323846f
5
+ #define sinPolyCoef3_f -1.666665710e-1f // Double: -1.666665709650470145824129400050267289858e-1
6
+ #define sinPolyCoef5_f 8.333017292e-3f // Double: 8.333017291562218127986291618761571373087e-3
7
+ #define sinPolyCoef7_f -1.980661520e-4f // Double: -1.980661520135080504411629636078917643846e-4
8
+ #define sinPolyCoef9_f 2.600054768e-6f // Double: 2.600054767890361277123254766503271638682e-6
9
+ #define sinPolyCoef3_d -1.666665709650470145824129400050267289858e-1
10
+ #define sinPolyCoef5_d 8.333017291562218127986291618761571373087e-3
11
+ #define sinPolyCoef7_d -1.980661520135080504411629636078917643846e-4
12
+ #define sinPolyCoef9_d 2.600054767890361277123254766503271638682e-6
13
+
14
+
15
+ float invSqrt_f(float x) { return 1.0f / sqrtf(x); }
16
+ double invSqrt_d(double x) { return 1.0 / sqrt(x); }
17
+
18
+ float pressureToAltitude_f(const float pressure) { return (1.0f - powf(pressure / 101325.0f, 0.190295f)) * 4433000.0f; }
19
+ double pressureToAltitude_d(const double pressure) { return (1.0 - pow(pressure / 101325.0, 0.190295)) * 4433000.0; }
20
+
21
+ float dynThrottle_f(float throttle) { return throttle * (1.0f - (throttle * throttle) / 3.0f) * 1.5f; }
22
+ double dynThrottle_d(double throttle) { return throttle * (1.0 - (throttle * throttle) / 3.0) * 1.5; }
23
+
24
+ float calculateAccZLowPassFilterRCTimeConstant_f(float accz_lpf_cutoff) { return 0.5f / (M_PIf * accz_lpf_cutoff); }
25
+ double calculateAccZLowPassFilterRCTimeConstant_d(double accz_lpf_cutoff) { return 0.5 / (M_PI * accz_lpf_cutoff); }
26
+
27
+ float calculateThrottleAngleScale_f(float throttle_correction_angle) { return (1800.0f / M_PIf) * (900.0f / throttle_correction_angle); }
28
+ double calculateThrottleAngleScale_d(double throttle_correction_angle) { return (1800.0 / M_PI) * (900.0 / throttle_correction_angle); }
29
+
30
+ float sin_approx_f(float x)
31
+ {
32
+ float x2 = x * x;
33
+ return x + x * x2 * (sinPolyCoef3_f + x2 * (sinPolyCoef5_f + x2 * (sinPolyCoef7_f + x2 * sinPolyCoef9_f)));
34
+ }
35
+ double sin_approx_d(double x)
36
+ {
37
+ double x2 = x * x;
38
+ return x + x * x2 * (sinPolyCoef3_d + x2 * (sinPolyCoef5_d + x2 * (sinPolyCoef7_d + x2 * sinPolyCoef9_d)));
39
+ }
40
+
41
+ float acos_approx_f(float xa)
42
+ {
43
+ return sqrtf(1.0f - xa) * (1.5707288f + xa * (-0.2121144f + xa * (0.0742610f + (-0.0187293f * xa))));
44
+ }
45
+ double acos_approx_d(double xa)
46
+ {
47
+ return sqrt(1.0 - xa) * (1.5707288 + xa * (-0.2121144 + xa * (0.0742610 + (-0.0187293 * xa))));
48
+ }
49
+
50
+ int main() {
51
+ float xf;
52
+ double xd;
53
+ int l;
54
+
55
+ l = scanf("%f", &xf);
56
+ l = scanf("%lf", &xd);
57
+
58
+ printf("%f\n", invSqrt_f(xf));
59
+ printf("%lf\n", invSqrt_d(xd));
60
+
61
+ printf("%f\n", pressureToAltitude_f(xf));
62
+ printf("%lf\n", pressureToAltitude_d(xd));
63
+
64
+ printf("%f\n", dynThrottle_f(xf));
65
+ printf("%lf\n", dynThrottle_d(xd));
66
+
67
+ printf("%f\n", calculateAccZLowPassFilterRCTimeConstant_f(xf));
68
+ printf("%lf\n", calculateAccZLowPassFilterRCTimeConstant_d(xd));
69
+
70
+ printf("%f\n", calculateThrottleAngleScale_f(xf));
71
+ printf("%lf\n", calculateThrottleAngleScale_d(xd));
72
+
73
+ printf("%f\n", sin_approx_f(xf));
74
+ printf("%lf\n", sin_approx_d(xd));
75
+
76
+ printf("%f\n", acos_approx_f(xf));
77
+ printf("%lf\n", acos_approx_d(xd));
78
+ }
real_world_dataset/custom_source/linux_color_cvt.c ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <math.h>
2
+ #include <stdio.h>
3
+
4
+ static double transfer_rgb_to_oprgb_d(double v)
5
+ {
6
+ return pow(v, 1.0 / 2.19921875);
7
+ }
8
+
9
+ static double transfer_rgb_to_dcip3_d(double v)
10
+ {
11
+ return pow(v, 1.0 / 2.6);
12
+ }
13
+
14
+ static double transfer_rgb_to_smpte2084_d(double v)
15
+ {
16
+ const double m1 = (2610.0 / 4096.0) / 4.0;
17
+ const double m2 = 128.0 * 2523.0 / 4096.0;
18
+ const double c1 = 3424.0 / 4096.0;
19
+ const double c2 = 32.0 * 2413.0 / 4096.0;
20
+ const double c3 = 32.0 * 2392.0 / 4096.0;
21
+
22
+ /*
23
+ * The RGB input maps to the luminance range 0-100 cd/m^2, while
24
+ * SMPTE-2084 maps values to the luminance range of 0-10000 cd/m^2.
25
+ * Hence the factor 100.
26
+ */
27
+ v /= 100.0;
28
+ v = pow(v, m1);
29
+ return pow((c1 + c2 * v) / (1 + c3 * v), m2);
30
+ }
31
+
32
+ static float transfer_rgb_to_oprgb_f(float v)
33
+ {
34
+ return pow(v, 1.0f / 2.19921875f);
35
+ }
36
+
37
+ static float transfer_rgb_to_dcip3_f(float v)
38
+ {
39
+ return pow(v, 1.0f / 2.6f);
40
+ }
41
+
42
+ static float transfer_rgb_to_smpte2084_f(float v)
43
+ {
44
+ const float m1 = (2610.0f / 4096.0f) / 4.0f;
45
+ const float m2 = 128.0f * 2523.0f / 4096.0f;
46
+ const float c1 = 3424.0f / 4096.0f;
47
+ const float c2 = 32.0f * 2413.0f / 4096.0f;
48
+ const float c3 = 32.0f * 2392.0f / 4096.0f;
49
+
50
+ /*
51
+ * The RGB input maps to the luminance range 0-100 cd/m^2, while
52
+ * SMPTE-2084 maps values to the luminance range of 0-10000 cd/m^2.
53
+ * Hence the factor 100.
54
+ */
55
+ v /= 100.0f;
56
+ v = pow(v, m1);
57
+ return pow((c1 + c2 * v) / (1 + c3 * v), m2);
58
+ }
59
+
60
+ int main() {
61
+ float xf;
62
+ double xd;
63
+ int l;
64
+
65
+ l = scanf("%f", &xf);
66
+ l = scanf("%lf", &xd);
67
+
68
+ printf("%f\n", transfer_rgb_to_oprgb_f(xf));
69
+ printf("%lf\n", transfer_rgb_to_oprgb_d(xd));
70
+
71
+ printf("%f\n", transfer_rgb_to_dcip3_f(xf));
72
+ printf("%lf\n", transfer_rgb_to_dcip3_f(xd));
73
+
74
+ printf("%f\n", transfer_rgb_to_smpte2084_f(xf));
75
+ printf("%lf\n", transfer_rgb_to_smpte2084_d(xd));
76
+ }
real_world_dataset/custom_source/nn_funcs.f90 ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function logistic_f(x) result(y)
2
+ implicit none
3
+ real, intent(in) :: x
4
+ real :: y
5
+ y = 1/(1+exp(-x))
6
+ end function
7
+ function logistic_d(x) result(y)
8
+ implicit none
9
+ double precision, intent(in) :: x
10
+ double precision :: y
11
+ y = 1/(1+exp(-x))
12
+ end function
13
+
14
+ function rbf_f(x) result(y)
15
+ implicit none
16
+ real, intent(in) :: x
17
+ real :: y, sigmasq, mean
18
+ parameter (sigmasq = 1.9983)
19
+ parameter (mean = 0.7328)
20
+ y = exp(-(x-mean)**2/sigmasq)
21
+ end function
22
+ function rbf_d(x) result(y)
23
+ implicit none
24
+ double precision, intent(in) :: x
25
+ double precision :: y, sigmasq, mean
26
+ parameter (sigmasq = 1.9983d0)
27
+ parameter (mean = 0.7328d0)
28
+ y = exp(-(x-mean)**2/sigmasq)
29
+ end function
30
+
31
+ function multiquad_f(x) result(y)
32
+ implicit none
33
+ real, intent(in) :: x
34
+ real :: y, biassq, mean
35
+ parameter (biassq = 8.66172)
36
+ parameter (mean = 0.881)
37
+ y = sqrt((x-mean)**2 + biassq)
38
+ end function
39
+ function multiquad_d(x) result(y)
40
+ implicit none
41
+ double precision, intent(in) :: x
42
+ double precision :: y, biassq, mean
43
+ parameter (biassq = 8.66172d0)
44
+ parameter (mean = 0.881d0)
45
+ y = sqrt((x-mean)**2 + biassq)
46
+ end function
47
+
48
+ function invmultiquad_f(x) result(y)
49
+ implicit none
50
+ real, intent(in) :: x
51
+ real:: y, biassq, mean
52
+ parameter (biassq = 8.66172)
53
+ parameter (mean = 0.881)
54
+ y = 1/sqrt((x-mean)**2 + biassq)
55
+ end function
56
+ function invmultiquad_d(x) result(y)
57
+ implicit none
58
+ double precision, intent(in) :: x
59
+ double precision:: y, biassq, mean
60
+ parameter (biassq = 8.66172d0)
61
+ parameter (mean = 0.881d0)
62
+ y = 1/sqrt((x-mean)**2 + biassq)
63
+ end function
64
+
65
+ function tanh_f(x) result(y)
66
+ implicit none
67
+ real, intent(in) :: x
68
+ real:: y
69
+ y = (exp(x)-exp(-x))/(exp(x)+exp(-x))
70
+ end function
71
+ function tanh_d(x) result(y)
72
+ implicit none
73
+ double precision, intent(in) :: x
74
+ double precision:: y
75
+ y = (exp(x)-exp(-x))/(exp(x)+exp(-x))
76
+ end function
77
+
78
+ function softplus_f(x) result(y)
79
+ implicit none
80
+ real, intent(in) :: x
81
+ real:: y
82
+ y = log(1 + exp(x))
83
+ end function
84
+ function softplus_d(x) result(y)
85
+ implicit none
86
+ double precision, intent(in) :: x
87
+ double precision:: y
88
+ y = log(1 + exp(x))
89
+ end function
90
+
91
+ function silu_f(x) result(y)
92
+ implicit none
93
+ real, intent(in) :: x
94
+ real:: y
95
+ y = x/(1 + exp(-x))
96
+ end function
97
+ function silu_d(x) result(y)
98
+ implicit none
99
+ double precision, intent(in) :: x
100
+ double precision:: y
101
+ y = x/(1 + exp(-x))
102
+ end function
103
+
104
+ program main
105
+ implicit none
106
+ real :: xf
107
+ double precision :: xd
108
+
109
+ real :: logistic_f, rbf_f, multiquad_f, invmultiquad_f, tanh_f, softplus_f, silu_f
110
+ double precision :: logistic_d, rbf_d, multiquad_d, invmultiquad_d, tanh_d, softplus_d, silu_d
111
+
112
+ read(*, *) xf
113
+ read(*, *) xd
114
+
115
+ print *, ":", logistic_f(xf)
116
+ print *, ":", logistic_d(xd)
117
+
118
+ print *, ":", rbf_f(xf)
119
+ print *, ":", rbf_d(xd)
120
+
121
+ print *, ":", multiquad_f(xf)
122
+ print *, ":", multiquad_d(xd)
123
+
124
+ print *, ":", invmultiquad_f(xf)
125
+ print *, ":", invmultiquad_d(xd)
126
+
127
+ print *, ":", tanh_f(xf)
128
+ print *, ":", tanh_d(xd)
129
+
130
+ print *, ":", softplus_f(xf)
131
+ print *, ":", softplus_d(xd)
132
+
133
+ print *, ":", silu_f(xf)
134
+ print *, ":", silu_d(xd)
135
+ end program main
real_world_dataset/custom_source/ntc_thermistor.c ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #define REFERENCE_RESISTANCE_F 8000.0f
2
+ #define NOMINAL_RESISTANCE_F 100000.0f
3
+ #define NOMINAL_TEMPERATURE_F 25.0f
4
+ #define B_VALUE_F 3950.0f
5
+
6
+ #define REFERENCE_RESISTANCE_D 8000.0
7
+ #define NOMINAL_RESISTANCE_D 100000.0
8
+ #define NOMINAL_TEMPERATURE_D 25.0
9
+ #define B_VALUE_D 3950.0
10
+
11
+ #include <math.h>
12
+ #include <stdio.h>
13
+
14
+ double resistanceToKelvins_d(double resistance) {
15
+ double inverseKelvin = 1.0 / NOMINAL_TEMPERATURE_D +
16
+ log(resistance / NOMINAL_RESISTANCE_D) / B_VALUE_D;
17
+ return (1.0 / inverseKelvin);
18
+ }
19
+
20
+ double readResistance_d(double voltage) {
21
+ return REFERENCE_RESISTANCE_D / (1024.0 / voltage - 1.0);
22
+ }
23
+
24
+ double celsiusToKelvins_d(double celsius) {
25
+ return (celsius + 273.15);
26
+ }
27
+
28
+ double kelvinsToCelsius_d(double kelvins) {
29
+ return (kelvins - 273.15);
30
+ }
31
+
32
+ double celsiusToFahrenheit_d(double celsius) {
33
+ return (celsius * 1.8 + 32.0);
34
+ }
35
+
36
+ double kelvinsToFahrenheit_d(double kelvins) {
37
+ return (kelvins - 273.15) * 1.8 + 32.0;
38
+ }
39
+
40
+ float resistanceToKelvins_f(float resistance) {
41
+ float inverseKelvin = 1.0f / NOMINAL_TEMPERATURE_F +
42
+ logf(resistance / NOMINAL_RESISTANCE_F) / B_VALUE_F;
43
+ return (1.0f / inverseKelvin);
44
+ }
45
+
46
+ float readResistance_f(float voltage) {
47
+ return REFERENCE_RESISTANCE_F / (1024.0f / voltage - 1.0f);
48
+ }
49
+
50
+ float celsiusToKelvins_f(float celsius) {
51
+ return (celsius + 273.15f);
52
+ }
53
+
54
+ float kelvinsToCelsius_f(float kelvins) {
55
+ return (kelvins - 273.15f);
56
+ }
57
+
58
+ float celsiusToFahrenheit_f(float celsius) {
59
+ return (celsius * 1.8f + 32.0f);
60
+ }
61
+
62
+ float kelvinsToFahrenheit_f(float kelvins) {
63
+ return (kelvins - 273.15f) * 1.8f + 32.0f;
64
+ }
65
+
66
+ int main() {
67
+ float xf;
68
+ double xd;
69
+ int l;
70
+
71
+ l = scanf("%f", &xf);
72
+ l = scanf("%lf", &xd);
73
+
74
+ printf("%f\n", resistanceToKelvins_f(xf));
75
+ printf("%lf\n", resistanceToKelvins_d(xd));
76
+
77
+ printf("%f\n", celsiusToKelvins_f(xf));
78
+ printf("%lf\n", celsiusToKelvins_d(xd));
79
+
80
+ printf("%f\n", readResistance_f(xf));
81
+ printf("%lf\n", readResistance_d(xd));
82
+
83
+ printf("%f\n", kelvinsToCelsius_f(xf));
84
+ printf("%lf\n", kelvinsToCelsius_d(xd));
85
+
86
+ printf("%f\n", kelvinsToFahrenheit_f(xf));
87
+ printf("%lf\n", kelvinsToFahrenheit_d(xd));
88
+
89
+ printf("%f\n", celsiusToFahrenheit_f(xf));
90
+ printf("%lf\n", celsiusToFahrenheit_d(xd));
91
+
92
+ }
real_world_dataset/custom_source/paparazzi.c ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <math.h>
2
+ #include <stdio.h>
3
+
4
+ #define PI_D 3.14159265359
5
+ #define PI_F 3.14159265359f
6
+
7
+ float isometric_latitude0_f(float phi) {
8
+ return logf (tanf (PI_F/4.0f + phi / 2.0f));
9
+ }
10
+ double isometric_latitude0_d(double phi) {
11
+ return log (tan (PI_D/4.0 + phi / 2.0));
12
+ }
13
+
14
+
15
+ // Standard Atmosphere constants
16
+ /** ISA sea level standard atmospheric pressure in Pascal */
17
+ #define PPRZ_ISA_SEA_LEVEL_PRESSURE_F 101325.0f
18
+ #define PPRZ_ISA_SEA_LEVEL_PRESSURE_D 101325.0
19
+ /** ISA sea level standard temperature in Kelvin */
20
+ #define PPRZ_ISA_SEA_LEVEL_TEMP_F 288.15f
21
+ #define PPRZ_ISA_SEA_LEVEL_TEMP_D 288.15
22
+ /** temperature laps rate in K/m */
23
+ #define PPRZ_ISA_TEMP_LAPS_RATE_F 0.0065f
24
+ #define PPRZ_ISA_TEMP_LAPS_RATE_D 0.0065
25
+ /** earth-surface gravitational acceleration in m/s^2 */
26
+ #define PPRZ_ISA_GRAVITY_F 9.80665f
27
+ #define PPRZ_ISA_GRAVITY_D 9.80665
28
+ /** universal gas constant in J/(mol*K) */
29
+ #define PPRZ_ISA_GAS_CONSTANT_F 8.31447f
30
+ #define PPRZ_ISA_GAS_CONSTANT_D 8.31447
31
+ /** molar mass of dry air in kg/mol */
32
+ #define PPRZ_ISA_MOLAR_MASS_F 0.0289644f
33
+ #define PPRZ_ISA_MOLAR_MASS_D 0.0289644
34
+ /** universal gas constant / molar mass of dry air in J*kg/K */
35
+ #define PPRZ_ISA_AIR_GAS_CONSTANT_F (PPRZ_ISA_GAS_CONSTANT_F/PPRZ_ISA_MOLAR_MASS_F)
36
+ #define PPRZ_ISA_AIR_GAS_CONSTANT_D (PPRZ_ISA_GAS_CONSTANT_D/PPRZ_ISA_MOLAR_MASS_D)
37
+ /** standard air density in kg/m^3 */
38
+ #define PPRZ_ISA_AIR_DENSITY_F 1.225f
39
+ #define PPRZ_ISA_AIR_DENSITY_D 1.225
40
+
41
+ static const float PPRZ_ISA_M_OF_P_CONST_F = (PPRZ_ISA_AIR_GAS_CONSTANT_F * PPRZ_ISA_SEA_LEVEL_TEMP_F / PPRZ_ISA_GRAVITY_F);
42
+ static const double PPRZ_ISA_M_OF_P_CONST_D = (PPRZ_ISA_AIR_GAS_CONSTANT_D * PPRZ_ISA_SEA_LEVEL_TEMP_D / PPRZ_ISA_GRAVITY_D);
43
+
44
+ float pprz_isa_pressure_of_altitude_f(float altitude)
45
+ {
46
+ return (PPRZ_ISA_SEA_LEVEL_PRESSURE_F * expf((-1.0f / PPRZ_ISA_M_OF_P_CONST_F) * altitude));
47
+ }
48
+ double pprz_isa_pressure_of_altitude_d(double altitude)
49
+ {
50
+ return (PPRZ_ISA_SEA_LEVEL_PRESSURE_D * exp((-1. / PPRZ_ISA_M_OF_P_CONST_D) * altitude));
51
+ }
52
+
53
+ float pprz_isa_altitude_of_pressure_f(float pressure)
54
+ {
55
+ // if (pressure > 0.) {
56
+ return (PPRZ_ISA_M_OF_P_CONST_F * logf(PPRZ_ISA_SEA_LEVEL_PRESSURE_F / pressure));
57
+ // } else {
58
+ // return 0.;
59
+ // }
60
+ }
61
+
62
+ double pprz_isa_altitude_of_pressure_d(double pressure)
63
+ {
64
+ // if (pressure > 0.) {
65
+ return (PPRZ_ISA_M_OF_P_CONST_D * log(PPRZ_ISA_SEA_LEVEL_PRESSURE_D / pressure));
66
+ // } else {
67
+ // return 0.;
68
+ // }
69
+ }
70
+
71
+ float pprz_isa_pressure_of_height_f(float height)
72
+ {
73
+ float ref_p = 0.8f * PPRZ_ISA_SEA_LEVEL_PRESSURE_F;
74
+ return (ref_p * expf((-1.0f / PPRZ_ISA_M_OF_P_CONST_F) * height));
75
+ }
76
+
77
+ double pprz_isa_pressure_of_height_d(double height)
78
+ {
79
+ double ref_p = 0.8 * PPRZ_ISA_SEA_LEVEL_PRESSURE_D;
80
+ return (ref_p * exp((-1.0 / PPRZ_ISA_M_OF_P_CONST_D) * height));
81
+ }
82
+
83
+ float pprz_isa_height_of_pressure_full_f(float pressure)
84
+ {
85
+ const float ref_p = 0.8f * PPRZ_ISA_SEA_LEVEL_PRESSURE_F;
86
+ const float prel = pressure / ref_p;
87
+ const float inv_expo = PPRZ_ISA_GAS_CONSTANT_F * PPRZ_ISA_TEMP_LAPS_RATE_F /
88
+ PPRZ_ISA_GRAVITY_F / PPRZ_ISA_MOLAR_MASS_F;
89
+ return (1.0f - powf(prel, inv_expo)) * PPRZ_ISA_SEA_LEVEL_TEMP_F / PPRZ_ISA_TEMP_LAPS_RATE_F;
90
+ }
91
+
92
+ double pprz_isa_height_of_pressure_full_d(double pressure)
93
+ {
94
+ const double ref_p = 0.8 * PPRZ_ISA_SEA_LEVEL_PRESSURE_D;
95
+ const double prel = pressure / ref_p;
96
+ const double inv_expo = PPRZ_ISA_GAS_CONSTANT_D * PPRZ_ISA_TEMP_LAPS_RATE_D /
97
+ PPRZ_ISA_GRAVITY_D / PPRZ_ISA_MOLAR_MASS_D;
98
+ return (1.0 - pow(prel, inv_expo)) * PPRZ_ISA_SEA_LEVEL_TEMP_D / PPRZ_ISA_TEMP_LAPS_RATE_D;
99
+ }
100
+
101
+ float pprz_isa_height_of_pressure_f(float pressure)
102
+ {
103
+ const float ref_p = 0.8f * PPRZ_ISA_SEA_LEVEL_PRESSURE_F;
104
+ return (PPRZ_ISA_M_OF_P_CONST_F * logf(ref_p / pressure));
105
+ }
106
+ double pprz_isa_height_of_pressure_d(double pressure)
107
+ {
108
+ const double ref_p = 0.8 * PPRZ_ISA_SEA_LEVEL_PRESSURE_D;
109
+ return (PPRZ_ISA_M_OF_P_CONST_D * log(ref_p / pressure));
110
+ }
111
+
112
+ float pprz_isa_temperature_of_altitude_f(float alt)
113
+ {
114
+ return PPRZ_ISA_SEA_LEVEL_TEMP_F - PPRZ_ISA_TEMP_LAPS_RATE_F * alt;
115
+ }
116
+ double pprz_isa_temperature_of_altitude_d(double alt)
117
+ {
118
+ return PPRZ_ISA_SEA_LEVEL_TEMP_D - PPRZ_ISA_TEMP_LAPS_RATE_D * alt;
119
+ }
120
+
121
+ float eas_from_dynamic_pressure_f(float q)
122
+ {
123
+ const float two_div_rho_0 = 2.0f / PPRZ_ISA_AIR_DENSITY_F;
124
+ return sqrtf(q * two_div_rho_0);
125
+ }
126
+
127
+ double eas_from_dynamic_pressure_d(double q)
128
+ {
129
+ const double two_div_rho_0 = 2.0 / PPRZ_ISA_AIR_DENSITY_D;
130
+ return sqrt(q * two_div_rho_0);
131
+ }
132
+
133
+ float change_rep_f(float dir)
134
+ {
135
+ return PI_F/2.0f - dir;
136
+ }
137
+ double change_rep_d(double dir)
138
+ {
139
+ return PI_D/2.0 - dir;
140
+ }
141
+
142
+ #define NMEA_PI180_F (PI_F / 180.0f)
143
+ #define NMEA_PI180_D (PI_D / 180.0)
144
+ float nmea_degree2radian_f(float val) { return (val * NMEA_PI180_F); }
145
+ double nmea_degree2radian_d(double val) { return (val * NMEA_PI180_D); }
146
+ float nmea_radian2degree_f(float val) { return (val / NMEA_PI180_F); }
147
+ double nmea_radian2degree_d(double val) { return (val / NMEA_PI180_D); }
148
+
149
+ int main() {
150
+ float xf;
151
+ double xd;
152
+ int l;
153
+
154
+ l = scanf("%f", &xf);
155
+ l = scanf("%lf", &xd);
156
+
157
+ printf("%f\n", isometric_latitude0_f(xf));
158
+ printf("%lf\n", isometric_latitude0_d(xd));
159
+
160
+ printf("%f\n", pprz_isa_pressure_of_altitude_f(xf));
161
+ printf("%lf\n", pprz_isa_pressure_of_altitude_d(xd));
162
+
163
+ printf("%f\n", pprz_isa_altitude_of_pressure_f(xf));
164
+ printf("%lf\n", pprz_isa_altitude_of_pressure_d(xd));
165
+
166
+ printf("%f\n", pprz_isa_pressure_of_height_f(xf));
167
+ printf("%lf\n", pprz_isa_pressure_of_height_d(xd));
168
+
169
+ printf("%f\n", pprz_isa_height_of_pressure_full_f(xf));
170
+ printf("%lf\n", pprz_isa_height_of_pressure_full_d(xd));
171
+
172
+ printf("%f\n", pprz_isa_height_of_pressure_f(xf));
173
+ printf("%lf\n", pprz_isa_height_of_pressure_d(xd));
174
+
175
+ printf("%f\n", pprz_isa_temperature_of_altitude_f(xf));
176
+ printf("%lf\n", pprz_isa_temperature_of_altitude_d(xd));
177
+
178
+ printf("%f\n", eas_from_dynamic_pressure_f(xf));
179
+ printf("%lf\n", eas_from_dynamic_pressure_d(xd));
180
+
181
+ printf("%f\n", nmea_degree2radian_f(xf));
182
+ printf("%lf\n", nmea_degree2radian_d(xd));
183
+
184
+ printf("%f\n", nmea_radian2degree_f(xf));
185
+ printf("%lf\n", nmea_radian2degree_d(xd));
186
+ }
real_world_dataset/custom_source/pathtracing.c ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <math.h>
2
+ #include <stdio.h>
3
+
4
+ float fresnelReflectanceAtNormal_f(float index) {
5
+ float partial = (1.0f - index) / (1.0f + index);
6
+ return partial * partial;
7
+ }
8
+
9
+ double fresnelReflectanceAtNormal_d(double index) {
10
+ double partial = (1.0 - index) / (1.0 + index);
11
+ return partial * partial;
12
+ }
13
+
14
+ float blinToBeckmann_f(float alpha) {
15
+ return sqrtf(2.0f / (alpha + 2.0f));
16
+ }
17
+
18
+ float beckmannToBlinn_f(float slope) {
19
+ return 2.0f / (slope * slope) - 2.0f;
20
+ }
21
+
22
+ double blinToBeckmann_d(double alpha) {
23
+ return sqrtf(2.0 / (alpha + 2.0));
24
+ }
25
+
26
+ double beckmannToBlinn_d(double slope) {
27
+ return 2.0 / (slope * slope) - 2.0;
28
+ }
29
+
30
+ int main() {
31
+ float xf;
32
+ double xd;
33
+ int l;
34
+
35
+ l = scanf("%f", &xf);
36
+ l = scanf("%lf", &xd);
37
+
38
+ printf("%f\n", fresnelReflectanceAtNormal_f(xf));
39
+ printf("%lf\n", fresnelReflectanceAtNormal_d(xd));
40
+
41
+ printf("%f\n", blinToBeckmann_f(xf));
42
+ printf("%lf\n", blinToBeckmann_d(xd));
43
+
44
+ printf("%f\n", beckmannToBlinn_f(xf));
45
+ printf("%lf\n", beckmannToBlinn_d(xd));
46
+ }
real_world_dataset/dataset_info.json ADDED
@@ -0,0 +1,236 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "ardupilot": {
3
+ "w": {
4
+ "eqn": "2*pi*x",
5
+ "prefix": "mul INT+ 2 mul pi x",
6
+ "constants": {}
7
+ },
8
+ "degF_to_Kelvin": {
9
+ "eqn": "(x+459.67)*0.55556",
10
+ "prefix": "mul k0 add x k1",
11
+ "constants": {"k0": "459.67", "k1": "0.55556"}
12
+ },
13
+ "radians": {
14
+ "eqn": "x*pi/180",
15
+ "prefix": "mul k0 x",
16
+ "constants": {"k0": "pi/180"}
17
+ },
18
+ "degrees": {
19
+ "eqn": "x*180/pi",
20
+ "prefix": "mul k0 x",
21
+ "constants": {"k0": "180/pi"}
22
+ },
23
+ "sq": {
24
+ "eqn": "x**2",
25
+ "prefix": "pow x INT+ 2",
26
+ "constants": {}
27
+ },
28
+ "angle_to_accel": {
29
+ "eqn": "9.80665*tan(x*pi/180)",
30
+ "prefix": "mul k0 tan mul x k1",
31
+ "constants": {"k0": "9.80665", "k1": "pi/180"}
32
+ },
33
+ "accel_to_angle": {
34
+ "eqn": "atan(x/9.80665)*180/pi",
35
+ "prefix": "mul atan div x k0 k1",
36
+ "constants": {"k0": "9.80665", "k1": "180/pi"}
37
+ },
38
+ "tau": {
39
+ "eqn": "0.25*log(3*x**2 + 6*x + 1) - sqrt(6)/24*log((x+1-sqrt(2/3))/(x + 1 + sqrt(2/3)))",
40
+ "prefix": "sub mul div INT+ 1 INT+ 4 log add mul INT+ 3 pow x INT+ 2 add mul INT+ 6 x INT+ 1 mul k0 log div sub add x INT+ 1 k1 add x add INT+ 1 k1",
41
+ "constants": {"k0": "sqrt(6)/24", "k1": "sqrt(2/3)"}
42
+ }
43
+ },
44
+ "paparazzi": {
45
+ "isometric_latitude0": {
46
+ "eqn": "log(tan(pi/4+x/2))",
47
+ "prefix": "log tan add div pi INT+ 4 div x INT+ 2",
48
+ "constants": {}
49
+ },
50
+ "pprz_isa_pressure_of_altitude": {
51
+ "eqn": "101325*exp((-1/8434.667799)*x)",
52
+ "prefix": "mul k0 exp mul k1 x",
53
+ "constants": {"k0": "101325", "k1": "-1/8434.667799"}
54
+ },
55
+ "pprz_isa_altitude_of_pressure": {
56
+ "eqn": "8434.667799*log(101325/x)",
57
+ "prefix": "mul k0 log div k1 x",
58
+ "constants": {"k1": "101325", "k0": "8434.667799"}
59
+ },
60
+ "pprz_isa_pressure_of_height": {
61
+ "eqn": "0.8*101325*exp((-1/8434.667799)*x)",
62
+ "prefix": "mul k0 exp mul k1 x",
63
+ "constants": {"k0": "0.8*101325", "k1": "-1/8434.667799"}
64
+ },
65
+ "pprz_isa_height_of_pressure_full": {
66
+ "eqn": "(1-pow(x/(0.8*101325), 0.19029848))*288.15/0.0065",
67
+ "prefix": "mul sub INT +1 pow div x k0 k1 k2",
68
+ "constants": {"k0": "0.8*101325", "k1": "0.19029848", "k2": "288.15/0.0065"}
69
+ },
70
+ "pprz_isa_height_of_pressure": {
71
+ "eqn": "8434.667799*log(0.8*101325/x)",
72
+ "prefix": "mul k0 log div k1 x",
73
+ "constants": {"k1": "0.8*101325", "k0": "8434.667799"}
74
+ },
75
+ "pprz_isa_temperature_of_altitude": {
76
+ "eqn": "288.15 - 0.0065*x",
77
+ "prefix": "sub k0 mul k1 x",
78
+ "constants": {"k0": "288.15", "k1": "0.0065"}
79
+ },
80
+ "change_rep": {
81
+ "eqn": "pi/2-x",
82
+ "prefix": "sub div pi INT+ 2 x",
83
+ "constants": {}
84
+ },
85
+ "eas_from_dynamic_pressure": {
86
+ "eqn": "sqrt(x*2/1.225)",
87
+ "prefix": "pow mul x k0 div INT+ 1 INT+ 2",
88
+ "constants": {"k0": "2/1.225"}
89
+ },
90
+ "nmea_degree2radian": {
91
+ "eqn": "x*pi/180",
92
+ "prefix": "mul x k0",
93
+ "constants": {"k0": "pi/180"}
94
+ },
95
+ "nmea_radian2degree": {
96
+ "eqn": "x*180/pi",
97
+ "prefix": "mul x k0",
98
+ "constants": {"k0": "180/pi"}
99
+ }
100
+ },
101
+ "cleanflight": {
102
+ "invSqrt": {
103
+ "eqn": "1/sqrt(x)",
104
+ "prefix": "pow x div INT- 1 INT+ 2",
105
+ "constants": {}
106
+ },
107
+ "pressureToAltitude": {
108
+ "eqn": "1-pow(x/101325, 0.190295)*4433000",
109
+ "prefix": "sub INT+ 1 mul pow div x k0 k1 k2",
110
+ "constants": {"k0": "101325", "k1": "0.190295", "k2": "4433000"}
111
+ },
112
+ "dynThrottle": {
113
+ "eqn": "x*(1-x*x/3)*1.5",
114
+ "prefix": "mul x mul sub INT+ 1 div pow x INT+ 2 INT+ 3 div INT+ 3 INT+ 2",
115
+ "constants": {}
116
+ },
117
+ "calculateAccZLowPassFilterRCTimeConstant": {
118
+ "eqn": "0.5/pi/x",
119
+ "prefix": "div k0 x",
120
+ "constants": {"k0": "0.5/pi"}
121
+ },
122
+ "calculateThrottleAngleScale": {
123
+ "eqn": "1800/pi*900/x",
124
+ "prefix": "div k0 x",
125
+ "constants": {"k0": "1800/pi*900"}
126
+ },
127
+ "sin_approx": {
128
+ "eqn": "sin(x)",
129
+ "prefix": "",
130
+ "constants": {}
131
+ },
132
+ "acos_approx": {
133
+ "eqn": "acos(x)",
134
+ "prefix": "",
135
+ "constants": {}
136
+ }
137
+ },
138
+ "nn_funcs": {
139
+ "logistic": {
140
+ "eqn": "1/(1+exp(-x))",
141
+ "prefix": "div INT+ 1 add INT+ 1 exp mul INT- 1 x",
142
+ "constants": {}
143
+ },
144
+ "rbf": {
145
+ "eqn": "exp(-(x-0.7328)**2/1.9983)",
146
+ "prefix": "exp div pow sub x k0 INT+ 2 k1",
147
+ "constants": {"k0": "0.7328", "k1": "1.9983"}
148
+ },
149
+ "multiquad": {
150
+ "eqn": "sqrt((x-0.881)**2+8.66172)",
151
+ "prefix": "pow add pow sub x k0 INT+ 2 k1 div INT+ 1 INT+ 2",
152
+ "constants": {"k0": "0.881", "k1": "8.66172"}
153
+ },
154
+ "invmultiquad": {
155
+ "eqn": "1/sqrt((x-0.881)**2+8.66172)",
156
+ "prefix": "pow add pow sub x k0 INT+ 2 k1 div INT- 1 INT+ 2",
157
+ "constants": {"k0": "0.881", "k1": "8.66172"}
158
+ },
159
+ "tanh": {
160
+ "eqn": "tanh(x)",
161
+ "prefix": "div sub exp x exp mul INT- 1 x add exp x exp mul INT- 1 x",
162
+ "constants": {}
163
+ },
164
+ "softplus": {
165
+ "eqn": "log(1+exp(x))",
166
+ "prefix": "log add INT+ 1 exp x",
167
+ "constants": {}
168
+ },
169
+ "silu": {
170
+ "eqn": "x/(1+exp(-x))",
171
+ "prefix": "div x add INT+ 1 exp mul INT- 1 x",
172
+ "constants": {}
173
+ }
174
+ },
175
+ "ntc_thermistor": {
176
+ "resistanceToKelvins": {
177
+ "eqn": "1/25+log(x/100000)/3950",
178
+ "prefix": "add div INT+ 1 INT+ 2 5 div log div x k0 k1",
179
+ "constants": {"k0": "100000", "k1": "3950"}
180
+ },
181
+ "readResistance": {
182
+ "eqn": "8000/(1024/x - 1)",
183
+ "prefix": "div k0 sub div k1 x INT+ 1",
184
+ "constants": {"k0": "8000", "k1": "1024"}
185
+ },
186
+ "celsiusToKelvins": {
187
+ "eqn": "x+273.15",
188
+ "prefix": "add x k0",
189
+ "constants": {"k0": "273.15"}
190
+ },
191
+ "kelvinsToCelsius": {
192
+ "eqn": "x-273.15",
193
+ "prefix": "sub x k0",
194
+ "constants": {"k0": "273.15"}
195
+ },
196
+ "celsiusToFahrenheit": {
197
+ "eqn": "x*1.8+32",
198
+ "prefix": "add mul x div INT+ 9 INT+ 5 INT+ 3 2",
199
+ "constants": {}
200
+ },
201
+ "kelvinsToFahrenheit": {
202
+ "eqn": "(x-273.15)*1.8+32",
203
+ "prefix": "add mul sub x k0 div INT+ 9 INT+ 5 INT+ 3 2",
204
+ "constants": {"k0": "273.15"}
205
+ }
206
+ },
207
+ "arduino_sensorkit": {
208
+ "calcAltitude": {
209
+ "eqn": "(1-(x/101325)**(1/5.25588))/0.0000225577",
210
+ "prefix": "div sub INT+ 1 pow div x k0 k1 k2",
211
+ "constants": {"k0": "101325", "k1": "1/5.25588", "k2": "0.000225577"}
212
+ },
213
+ "convertCtoF": {
214
+ "eqn": "x*1.8+32",
215
+ "prefix": "add mul x div INT+ 9 INT+ 5 INT+ 3 2",
216
+ "constants": {}
217
+ }
218
+ },
219
+ "pathtracing": {
220
+ "fresnelReflectanceAtNormal": {
221
+ "eqn": "(1-x)**2/(1+x)**2",
222
+ "prefix": "pow div sub INT+ 1 x add INT+ 1 x INT+ 2",
223
+ "constants": {}
224
+ },
225
+ "blinToBeckmann": {
226
+ "eqn": "sqrt(2/(2+x))",
227
+ "prefix": "pow div INT+ 2 add x INT+ 2 div INT+ 1 INT+ 2",
228
+ "constants": {}
229
+ },
230
+ "beckmannToBlinn": {
231
+ "eqn": "2/x**2 - 2",
232
+ "prefix": "add div INT+ 2 mul x x INT- 2",
233
+ "constants": {}
234
+ }
235
+ }
236
+ }
real_world_dataset/eval_dataset.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sympy as sp
2
+ import numpy as np
3
+ import warnings
4
+ from sympy.abc import x
5
+ import sys
6
+ import json
7
+ from tqdm import tqdm
8
+
9
+ from remend.tools.parser import parse_prefix_to_sympy
10
+
11
+ warnings.simplefilter("ignore")
12
+
13
+ def percent(a, n):
14
+ return f"{a/n*100:0.1f}%"
15
+
16
+ def do_eval_match(orig_expr, gen_expr):
17
+ try:
18
+ origl = sp.lambdify(x, orig_expr)
19
+ genl = sp.lambdify(x, gen_expr)
20
+ count = 0
21
+
22
+ for v in np.arange(0.2, 1, 0.01):
23
+ o = origl(v)
24
+ g = genl(v)
25
+ if o != o or o == float('inf'):
26
+ continue
27
+ if g != g or g == float('inf'):
28
+ continue
29
+ # if type(o) != np.float64 or type(g) != np.float64:
30
+ # print(orig_expr, o, gen_expr, g)
31
+ # return False
32
+ if abs((o-g)/o) > 1e-5:
33
+ return False
34
+ count += 1
35
+ except:
36
+ return False
37
+ return count >= 5
38
+
39
+ if __name__ == "__main__":
40
+ import argparse
41
+ parser = argparse.ArgumentParser("Check generated expressions")
42
+ parser.add_argument("-g", required=True, help="Generated expressions file")
43
+ parser.add_argument("-i", required=True, help="Info file")
44
+ parser.add_argument("-r", required=True, help="Results file")
45
+ args = parser.parse_args()
46
+
47
+ gens = []
48
+ with open(args.g, 'r') as genf, open(args.i) as infof:
49
+ for line in tqdm(genf, desc="Reading file"):
50
+ comps = line.strip().split("\t")
51
+ if line[0] == 'H':
52
+ num = int(comps[0][2:])
53
+ tokens = comps[2].split(" ")
54
+ info = next(infof)
55
+ info = json.loads(info.strip())
56
+ if info["eqn"] == "":
57
+ continue
58
+ gens.append((num, tokens, info))
59
+
60
+ parsed = []
61
+ matched = []
62
+ results = []
63
+
64
+ for n, toks, info in tqdm(gens, desc="Evaluating expressions"):
65
+ res = {"id": n, "parsed": False, "matched": False, "orig": "", "gen": ""}
66
+ if "<<unk>>" in toks:
67
+ # Not parsed
68
+ results.append(res)
69
+ continue
70
+ try:
71
+ gen_expr = parse_prefix_to_sympy(toks)
72
+ except Exception as e:
73
+ # Not parsed
74
+ results.append(res)
75
+ continue
76
+
77
+ res["parsed"] = True
78
+ parsed.append(n)
79
+ const = info["constants"]
80
+
81
+ gen_expr = gen_expr.subs([(sp.Symbol("k"+c), const[c]) for c in const])
82
+ orig_expr = sp.parse_expr(info["eqn"], local_dict={"x0":x})
83
+ res["orig"] = str(orig_expr)
84
+ res["gen"] = str(gen_expr)
85
+
86
+ if not do_eval_match(orig_expr, gen_expr):
87
+ results.append(res)
88
+ continue
89
+ res["matched"] = True
90
+ matched.append(n)
91
+ results.append(res)
92
+
93
+ with open(args.r, "w") as resf:
94
+ for res in results:
95
+ resf.write("{id} {parsed} {matched} \"{orig}\" \"{gen}\"\n".format(**res))
96
+ resf.write("\n")
97
+ N = len(gens)
98
+ print("Total", N, file=resf)
99
+ print("Parsed", len(parsed), percent(len(parsed), N), file=resf)
100
+ print("Matched", len(matched), percent(len(matched), N), file=resf)
real_world_dataset/generate.sh ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ ARCHS=( arm32 aarch64 x64 )
4
+ TOKENIZERS=$HOME/projects/decode_ML/dlsym/tokenized
5
+ MODELS=$HOME/projects/decode_ML/dlsym/ablation
6
+ MODEL=base
7
+ DS=dataset
8
+ GEN=generated/${MODEL}
9
+
10
+ mkdir -p ${GEN}
11
+
12
+ for arch in ${ARCHS[@]}
13
+ do
14
+ tok=${TOKENIZERS}/${arch}/tokenized_dlsm_${arch}
15
+ echo python3 -m remend.tools.bpe_apply -t ${tok}/asm_tokens.json -i ${DS}/${arch}.asm -o ${GEN}/${arch}_tokenized.asm
16
+ python3 -m remend.tools.bpe_apply -t ${tok}/asm_tokens.json -i ${DS}/${arch}.asm -o ${GEN}/${arch}_tokenized.asm
17
+ fairseq-interactive ${tok} --beam 1 --path ${MODELS}/trained_${arch}_${MODEL}/checkpoint_best.pt < ${GEN}/${arch}_tokenized.asm > ${GEN}/${arch}_generated_beam1.txt 2>/dev/null
18
+ fairseq-interactive ${tok} --beam 5 --path ${MODELS}/trained_${arch}_${MODEL}/checkpoint_best.pt < ${GEN}/${arch}_tokenized.asm > ${GEN}/${arch}_generated_beam5.txt 2>/dev/null
19
+ python3 eval_dataset.py -g ${GEN}/${arch}_generated_beam1.txt -i ${DS}/${arch}.info -r ${GEN}/${arch}_res_beam1.txt
20
+ python3 eval_dataset.py -g ${GEN}/${arch}_generated_beam5.txt -i ${DS}/${arch}.info -r ${GEN}/${arch}_res_beam5.txt
21
+ done
real_world_dataset/make.sh ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ BUILD=build
4
+ SRC=src
5
+
6
+ A32=arm-linux-gnueabihf-
7
+ A64=aarch64-linux-gnu-
8
+ X64=
9
+ mkdir -p ${BUILD}
10
+
11
+ OPTS=(O0 O1 O2 O3)
12
+ CFILES=(ardupilot paparazzi cleanflight linux_color_cvt ntc_thermistor arduino_sensorkit pathtracing)
13
+
14
+ for opt in ${OPTS[@]}
15
+ do
16
+ for f in ${CFILES[@]}
17
+ do
18
+ # Compile
19
+ ${A64}gcc -${opt} -o ${BUILD}/${f}_aarch64_${opt}.elf ${SRC}/${f}.c -lm
20
+ ${X64}gcc -${opt} -o ${BUILD}/${f}_x64_${opt}.elf ${SRC}/${f}.c -lm
21
+ ${A32}gcc -${opt} -o ${BUILD}/${f}_arm32_${opt}.elf ${SRC}/${f}.c -lm
22
+
23
+ # Generate ASM source
24
+ ${A64}gcc -S -${opt} -o ${BUILD}/${f}_aarch64_${opt}.s ${SRC}/${f}.c
25
+ ${X64}gcc -S -${opt} -o ${BUILD}/${f}_x64_${opt}.s ${SRC}/${f}.c
26
+ ${A32}gcc -S -${opt} -o ${BUILD}/${f}_arm32_${opt}.s ${SRC}/${f}.c
27
+ done
28
+
29
+ # Compile
30
+ ${A64}gfortran -std=gnu -${opt} -o ${BUILD}/nn_funcs_aarch64_${opt}.elf ${SRC}/nn_funcs.f90
31
+ ${X64}gfortran -std=gnu -${opt} -o ${BUILD}/nn_funcs_x64_${opt}.elf ${SRC}/nn_funcs.f90
32
+ ${A32}gfortran -std=gnu -${opt} -o ${BUILD}/nn_funcs_arm32_${opt}.elf ${SRC}/nn_funcs.f90
33
+
34
+ # Generate ASM source
35
+ ${A64}gfortran -S -std=gnu -${opt} -o ${BUILD}/nn_funcs_aarch64_${opt}.s ${SRC}/nn_funcs.f90
36
+ ${X64}gfortran -S -std=gnu -${opt} -o ${BUILD}/nn_funcs_x64_${opt}.s ${SRC}/nn_funcs.f90
37
+ ${A32}gfortran -S -std=gnu -${opt} -o ${BUILD}/nn_funcs_arm32_${opt}.s ${SRC}/nn_funcs.f90
38
+ done
real_world_dataset/preprocess.sh ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ ARCHS=( arm32 aarch64 x64 )
4
+ TOKENIZERS=$HOME/projects/decode_ML/dlsym/tokenized
5
+ MODELS=$HOME/projects/decode_ML/dlsym/ablation
6
+ MODEL=base
7
+ DS=dataset
8
+ GEN=generated/${MODEL}
9
+
10
+ mkdir -p ${GEN}
11
+
12
+ for arch in ${ARCHS[@]}
13
+ do
14
+ tok=${TOKENIZERS}/${arch}/tokenized_dlsm_${arch}
15
+ echo python3 -m remend.tools.bpe_apply -t ${tok}/asm_tokens.json -i ${DS}/${arch}.asm -o ${GEN}/${arch}_tokenized.asm
16
+ python3 -m remend.tools.bpe_apply -t ${tok}/asm_tokens.json -i ${DS}/${arch}.asm -o ${GEN}/${arch}_tokenized.asm
17
+ fairseq-interactive ${tok} --beam 1 --path ${MODELS}/trained_${arch}_${MODEL}/checkpoint_best.pt < ${GEN}/${arch}_tokenized.asm > ${GEN}/${arch}_generated_beam1.txt 2>/dev/null
18
+ fairseq-interactive ${tok} --beam 5 --path ${MODELS}/trained_${arch}_${MODEL}/checkpoint_best.pt < ${GEN}/${arch}_tokenized.asm > ${GEN}/${arch}_generated_beam5.txt 2>/dev/null
19
+ python3 eval_dataset.py -g ${GEN}/${arch}_generated_beam1.txt -i ${DS}/${arch}.info -r ${GEN}/${arch}_res_beam1.txt
20
+ python3 eval_dataset.py -g ${GEN}/${arch}_generated_beam5.txt -i ${DS}/${arch}.info -r ${GEN}/${arch}_res_beam5.txt
21
+ done
real_world_dataset/related_evals/run_btc.sh ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ fairseq-interactive c/c_xA/data-bin/data.toked.src-tgt/ --beam 10 --nbest 4 --source-lang src --target-lang tgt --path c/c_xA/checkpoint_best.pt < btc_asm_tok.txt > btc_predictions.txt
real_world_dataset/related_evals/run_nova.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import torch
3
+ from tqdm import tqdm
4
+ from transformers import AutoTokenizer
5
+ from modeling_nova import NovaTokenizer, NovaForCausalLM
6
+ import time
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained('lt-asset/nova-6.7b-bcr', trust_remote_code=True)
9
+ if not torch.distributed.is_initialized() or torch.distributed.get_rank() == 0:
10
+ print('Vocabulary:', len(tokenizer.get_vocab())) # 32280
11
+ tokenizer.pad_token = tokenizer.eos_token
12
+ tokenizer.pad_token_id = tokenizer.eos_token_id
13
+ nova_tokenizer = NovaTokenizer(tokenizer)
14
+
15
+ model = NovaForCausalLM.from_pretrained('lt-asset/nova-6.7b-bcr').eval()
16
+
17
+ with open("../slade/slade_cps_dataset.json", "r") as f:
18
+ dataset = json.load(f)
19
+
20
+ CFI_START=".cfi_startproc"
21
+ CFI_END=".cfi_endproc"
22
+ def process_asm(asm):
23
+ if CFI_START in asm:
24
+ s = asm.index(CFI_START) + len(CFI_START)
25
+ else:
26
+ s = 0
27
+ if CFI_END in asm:
28
+ e = asm.index(CFI_END)
29
+ else:
30
+ e = 0
31
+ prompt_before = f'# This is the assembly code:\n<func0>:\n'
32
+ prompt_after = '\nWhat is the source code?\n'
33
+ lines = filter(lambda s: (".cfi_" not in s) and (s.strip() != ""), asm[s:e].split("\n\t"))
34
+ asm = "\n".join(f"{l}\t<label-{i+1}>" for i, l in enumerate(lines))
35
+ char_types = ("0" * len(prompt_before)) + ("1" * len(asm)) + ("0" * len(prompt_after))
36
+ return prompt_before + asm + prompt_after, char_types
37
+
38
+ asms = [(prog, func, asm) for prog in dataset for func, asm in dataset[prog].items() if "_x64_" in prog]
39
+ with open("nova_predictions.txt", "w") as predf:
40
+ for prog, func, asm in tqdm(asms):
41
+ start = time.time()
42
+ inputs, char_types = process_asm(asm)
43
+ toks = nova_tokenizer.encode(inputs, "", char_types)
44
+
45
+ input_ids = torch.LongTensor(toks['input_ids'].tolist()).unsqueeze(0)
46
+ nova_attention_mask = torch.LongTensor(toks['nova_attention_mask']).unsqueeze(0)
47
+ no_mask_id = torch.LongTensor([toks['no_mask_idx']])
48
+ outputs = model.generate(
49
+ inputs=input_ids, max_new_tokens=512, temperature=0.2, top_p=0.95, num_return_sequences=3,
50
+ do_sample=True, nova_attention_mask=nova_attention_mask, no_mask_idx=no_mask_id,
51
+ pad_token_id=tokenizer.pad_token_id, eos_token_id=tokenizer.eos_token_id
52
+ )
53
+ end = time.time()
54
+ predf.write(f"{prog} {func} time= {end-start}\n")
55
+ for output in outputs:
56
+ outc = tokenizer.decode(output[input_ids.size(1):], skip_special_tokens=True, clean_up_tokenization_spaces=True)
57
+ predf.write(f"\t{outc}\n")
real_world_dataset/related_evals/run_slade.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import json
4
+ import torch
5
+ import itertools as it
6
+ from tokenizers import Tokenizer
7
+ from transformers import BartForConditionalGeneration
8
+ import time
9
+
10
+ from tqdm import tqdm
11
+
12
+ class InferenceDataProcessor:
13
+ def __init__(self, tokenizer: Tokenizer):
14
+ self.tokenizer = tokenizer
15
+
16
+ def tokenize_opt3_to_c(self, one_sample): # one_sample must be a function definition (and ONLY a function definition)
17
+ # Add some specific special tokens by hand:
18
+ one_sample = f"<c> <mask:0> </c> <intel> <opt3> {one_sample} </opt> </intel>"
19
+
20
+ return self.tokenizer.encode(self.tokenizer.normalizer.normalize_str(one_sample)).ids
21
+
22
+ def tokenize_s_to_c(self, one_sample): # one_sample must be a function definition (and ONLY a function definition)
23
+ # Add some specific special tokens by hand:
24
+ one_sample = f"<c> <mask:0> </c> <intel> {one_sample} </intel>"
25
+
26
+ return self.tokenizer.encode(self.tokenizer.normalizer.normalize_str(one_sample)).ids
27
+
28
+ def tokenize_arm_to_c(self, one_sample): # one_sample must be a function definition (and ONLY a function definition)
29
+ # Add some specific special tokens by hand:
30
+ one_sample = f"<c> <mask:0> </c> <arm> {one_sample} </arm>"
31
+
32
+ return self.tokenizer.encode(self.tokenizer.normalizer.normalize_str(one_sample)).ids
33
+
34
+ def tokenize_arm_opt3_to_c(self, one_sample): # one_sample must be a function definition (and ONLY a function definition)
35
+ # Add some specific special tokens by hand:
36
+ one_sample = f"<c> <mask:0> </c> <arm> <opt3> {one_sample} </opt> </arm>"
37
+
38
+ return self.tokenizer.encode(self.tokenizer.normalizer.normalize_str(one_sample)).ids
39
+
40
+ def prepare(self, asm, pair):
41
+ if pair == 's_c-c':
42
+ return self.tokenize_s_to_c(asm)
43
+ elif pair == 'opt3_c-c':
44
+ return self.tokenize_opt3_to_c(asm)
45
+ elif pair == 'arm_c-c':
46
+ return self.tokenize_arm_to_c(asm)
47
+ elif pair == 'arm_opt3_c-c':
48
+ return self.tokenize_arm_opt3_to_c(asm)
49
+
50
+ def detokenize(self, one_sample, remove_mask=True):
51
+ # Ugly hack. We can't directly use decode() (we could if we had a proper Postprocessor) remove some special tokens by hand (they can't be skipped as the others)
52
+ detok = self.tokenizer.decode(one_sample, skip_special_tokens=False)
53
+ detok = detok.replace('<eol> ', '\n').replace('<eol>', '\n').replace('<tab> ', '\t').replace('<tab>', '\t')
54
+ if remove_mask:
55
+ detok = detok.replace('<mask:0>', '')
56
+ detok = detok.replace('<pad>', '').replace('<s>', '').replace('</s>', '')
57
+ detok = re.sub('# (/\w+)*', '', detok)
58
+ detok = detok.replace('0x ', '0x').replace(' #', '').replace('return', 'return ').replace('return ', 'return ')
59
+ detok = detok.replace('static', '').replace('inline', '')
60
+ detok = re.sub('# (/\w+)*', '', detok)
61
+ detok = detok.replace('__attribute__((used))', '')
62
+ return detok
63
+
64
+
65
+ with open("../dataset.json") as f:
66
+ FUNCS = json.load(f)
67
+ DTYPES = ["f", "d"]
68
+ DATASET = "slade_cps_dataset.json"
69
+ PREDICTIONS = "slade_predictions.json"
70
+
71
+ SLADE_PATH = "."
72
+ MODELS = {
73
+ 'opt3_c-c': 'output/export-new_train-2023-04-28-2313-1dff-748d-checkpoint_best', # x86-O3
74
+ 's_c-c': 'output/export-new_train-2023-04-09-1854-b799-06dc-checkpoint_best', # x86-O0
75
+ 'arm_opt3_c-c': 'output/export-new_train-2023-05-06-0743-1dff-ae24-checkpoint_best', # ARM O3
76
+ 'arm_c-c': 'output/export-new_train-2023-04-29-0149-1dff-7342-checkpoint_best' # ARM O0
77
+ }
78
+ BEAM=5
79
+ NBEST=1
80
+ EARLY_STOPPING = True
81
+ LENGTH_PENALTY = 1.0
82
+ MIN_LENGTH = 1
83
+ MAX_NEW_TOKENS = 512
84
+ # DIRECTION = "s_c-c" # x86-O0
85
+ # DIRECTION = 'opt3_c-c' # x86-O3
86
+ # DIRECTION = 'arm_c-c' # ARM-O0
87
+ DIRECTION = 'arm_opt3_c-c' # ARM-O3
88
+
89
+ if DIRECTION == "s_c-c":
90
+ OPTS = [0]
91
+ ARCHS = ["x64"]
92
+ if DIRECTION == "opt3_c-c":
93
+ OPTS = [1, 2]
94
+ ARCHS = ["x64"]
95
+ if DIRECTION == "arm_c-c":
96
+ OPTS = [0]
97
+ ARCHS = ["aarch64"]
98
+ if DIRECTION == "arm_opt3_c-c":
99
+ OPTS = [1, 2]
100
+ ARCHS = ["aarch64"]
101
+
102
+ model_path = os.path.join(SLADE_PATH, MODELS[DIRECTION])
103
+ tok = Tokenizer.from_file(os.path.join(model_path, 'tokenizer.json'))
104
+ model = BartForConditionalGeneration.from_pretrained(model_path).eval()
105
+ data_processor = InferenceDataProcessor(tokenizer=tok)
106
+
107
+
108
+ def predict_one(asm, pair):
109
+ tokenized = data_processor.prepare(asm, pair)
110
+ if len(tokenized) > model.config.max_position_embeddings:
111
+ print('Too long')
112
+ return ['']
113
+ batch = torch.tensor(tokenized).unsqueeze(0)
114
+ output = model.generate(batch, max_new_tokens=MAX_NEW_TOKENS, num_beams=BEAM,
115
+ num_return_sequences=NBEST, early_stopping=EARLY_STOPPING,
116
+ length_penalty=LENGTH_PENALTY, min_length=MIN_LENGTH)
117
+ hyps = []
118
+ for hyp in output:
119
+ detokenized = data_processor.detokenize(hyp.cpu().tolist())
120
+ hyps.append(detokenized)
121
+ return hyps
122
+
123
+ # print(predict_one(ASM, DIRECTION))
124
+
125
+ with open(DATASET) as f:
126
+ dataset = json.load(f)
127
+
128
+ with open(PREDICTIONS) as f:
129
+ predictions = json.load(f)
130
+
131
+ with tqdm(total=44*len(DTYPES)*len(OPTS), desc="Running") as t:
132
+ for arch, binary, opt in it.product(ARCHS, FUNCS.keys(), OPTS):
133
+ name = f"{binary}_{arch}_O{opt}"
134
+ for func, dtype in it.product(FUNCS[binary].keys(), DTYPES):
135
+ fname = f"{func}_{dtype}"
136
+ if not fname in dataset[name]:
137
+ print("Not found!!", fname, name)
138
+ continue
139
+
140
+ asm = dataset[name][fname]
141
+ start = time.time()
142
+ pred = predict_one(asm, DIRECTION)
143
+ end = time.time()
144
+
145
+ predictions[f"{name}_{fname}"] = {"eq": FUNCS[binary][func]["eqn"], "pred": pred[0], "pass": False, "time": end-start}
146
+ t.update()
147
+
148
+ with open(PREDICTIONS, "w") as f:
149
+ json.dump(predictions, f, indent=2)