|
2 | 2 |
|
3 | 3 | (require |
4 | 4 | "../src/api/sandbox.rkt" |
5 | | - "../src/syntax/types.rkt" |
6 | 5 | "../src/core/points.rkt" |
7 | 6 | "../src/core/rules.rkt" |
8 | 7 | "../src/config.rkt" |
9 | 8 | "../src/core/batch.rkt" |
10 | 9 | "../src/core/egg-herbie.rkt" |
11 | | - "../src/syntax/read.rkt" |
12 | 10 | "../src/syntax/load-platform.rkt" |
13 | | - "../src/syntax/types.rkt" |
14 | 11 | "../src/core/points.rkt" |
15 | 12 | "../src/core/rules.rkt" |
16 | 13 | "../src/config.rkt" |
17 | 14 | "../src/core/batch.rkt" |
18 | 15 | "../src/core/egg-herbie.rkt" |
19 | | - "../src/syntax/read.rkt" |
20 | 16 | "../src/syntax/load-platform.rkt" |
21 | | - "../src/syntax/platform.rkt" |
22 | 17 | "../src/syntax/sugar.rkt" |
23 | 18 | "../src/core/programs.rkt" |
24 | 19 | "../src/syntax/syntax.rkt" |
25 | | - "../src/reports/common.rkt" |
26 | 20 | "../src/syntax/platform-language.rkt") |
27 | 21 |
|
28 | 22 | (activate-platform! "no-accelerators") |
29 | 23 |
|
| 24 | +(define (strip-approx expr) |
| 25 | + (match expr |
| 26 | + [(? approx?) (strip-approx (approx-impl expr))] |
| 27 | + [(? hole?) (strip-approx (hole-spec expr))] |
| 28 | + [`(if ,c ,t ,f) `(if ,(strip-approx c) |
| 29 | + ,(strip-approx t) |
| 30 | + ,(strip-approx f))] |
| 31 | + [(list op args ...) (cons op (map strip-approx args))] |
| 32 | + [_ expr])) |
| 33 | + |
| 34 | +(define (comparison-symbol? sym) |
| 35 | + (define name (symbol->string sym)) |
| 36 | + (or (string-contains? name "=") |
| 37 | + (string-contains? name "<") |
| 38 | + (string-contains? name ">"))) |
| 39 | + |
| 40 | +(define (contains-comparison? expr) |
| 41 | + (match expr |
| 42 | + [(? symbol?) #f] |
| 43 | + [(? number?) #f] |
| 44 | + [(? literal?) #f] |
| 45 | + [`(if ,c ,t ,f) (or (contains-comparison? c) |
| 46 | + (contains-comparison? t) |
| 47 | + (contains-comparison? f))] |
| 48 | + [(list (? symbol? op) args ...) |
| 49 | + (or (comparison-symbol? op) |
| 50 | + (ormap contains-comparison? args))] |
| 51 | + [(list args ...) |
| 52 | + (ormap contains-comparison? args)] |
| 53 | + [_ #f])) |
| 54 | + |
| 55 | +(define (sanitize expr) |
| 56 | + (strip-approx expr)) |
| 57 | + |
30 | 58 | (define (get-error expr) |
31 | 59 | (with-handlers ([exn? (lambda (exn) 0)]) |
32 | 60 | (define ctx (get-ctx expr)) |
|
94 | 122 |
|
95 | 123 | ht) |
96 | 124 |
|
97 | | -(define (has-approx expr) |
98 | | - (define str (format "~v" expr)) |
99 | | - (or (string-contains? str "approx") |
100 | | - (string-contains? str "=") |
101 | | - (string-contains? str ">") |
102 | | - (string-contains? str "<"))) |
103 | | - |
104 | 125 | (define (to-fpcore-str pair) |
105 | 126 | (define expr (car pair)) |
106 | 127 | (define vars (free-variables expr)) |
|
118 | 139 | (define lines (file->list (string-append report-dir "/expr_dump.txt"))) |
119 | 140 | (define unflattened-subexprs (map all-subexpressions lines)) |
120 | 141 |
|
121 | | -(define subexprs (apply append unflattened-subexprs)) |
122 | | -(define filtered-subexprs (filter (lambda (n) |
123 | | - (not (or (symbol? n) (literal? n) (approx? n) (has-approx n)))) subexprs)) |
| 142 | +(define subexprs (map sanitize (apply append unflattened-subexprs))) |
| 143 | +(define filtered-subexprs |
| 144 | + (filter (lambda (n) |
| 145 | + (not (or (symbol? n) |
| 146 | + (literal? n) |
| 147 | + (number? n) |
| 148 | + (contains-comparison? n)))) |
| 149 | + subexprs)) |
124 | 150 | (define filtered-again (filter (lambda (n) |
125 | 151 | (and (> (length (free-variables n)) 0) |
126 | 152 | (< (length (free-variables n)) 4))) filtered-subexprs)) |
127 | | -;;; (define filtered-again (filter (lambda (n) |
128 | | -;;; (> (length (free-variables n)) 0)) filtered-subexprs)) |
129 | 153 |
|
130 | 154 | (define renamed-subexprs (map rename-vars filtered-again)) |
131 | 155 | (define pairs (hash->list (count-frequencies renamed-subexprs))) |
|
135 | 159 | (define sorted-pairs (sort deduplicated-pairs (lambda (p1 p2) (> (cdr p1) (cdr p2))))) |
136 | 160 | (define first-2000 (take sorted-pairs (min (length sorted-pairs) 2000))) |
137 | 161 |
|
138 | | -;;; (define filtered (filter (lambda (p) (< 0.1 (get-error (car p)))) first-2000)) |
139 | | -(define filtered first-2000) |
| 162 | +(define filtered (filter (lambda (p) (< 0.1 (get-error (car p)))) first-2000)) |
| 163 | +;;; (define filtered first-2000) |
140 | 164 | (define first-500 (take filtered (min (length filtered) 500))) |
141 | 165 | (define fpcores-out (map to-fpcore-str first-500)) |
142 | 166 | (define counts-out (map to-count-print first-500)) |
|
151 | 175 | (for-each displayln fpcores-out)) |
152 | 176 | #:exists 'replace) |
153 | 177 |
|
154 | | -;;; ;;; (print-lines sorted-triples) |
155 | | -;;; ;;; (define cost-proc (platform-cost-proc (*active-platform*))) |
156 | | -;;; ;;; (define quads (map (lambda (p1) (append p1 (list (cost-proc (first p1) (get-representation 'binary64))))) sorted-triples)) |
157 | | -;;; ;;; (print-lines quads) |
158 | | - |
159 | | -;;; ;;; (define expr '(-.f64 #s(literal 1 binary64) (sqrt.f64 z0))) |
160 | | -;;; ;;; (define spec (prog->spec expr)) |
161 | | -;;; ;;; (define ctx (context (free-variables expr) (get-representation 'binary64) (make-list (length (free-variables expr)) (get-representation 'binary64)))) |
162 | | -;;; ;;; (define error (get-spec-error expr spec ctx)) |
163 | | -;;; ;;; (displayln error) |
164 | | - |
165 | | -;;; (module+ test |
166 | | -;;; (require rackunit) |
167 | | -;;; (check-equal? (rename-vars '(+ x y)) '(+ z0 z1))) |
168 | | - |
169 | | - |
170 | | -;;; (define expr '(+ x0 x0)) |
171 | | -;;; (displayln (best-exprs (list (get-ctx expr)) (list expr))) |
| 178 | +(module+ test |
| 179 | + (require rackunit) |
| 180 | + (check-equal? (rename-vars '(+ x y)) '(+ z0 z1))) |
0 commit comments