Skip to content

Commit 0ac028a

Browse files
committed
Merge branch 'master' into release-1.4
2 parents c064433 + f775348 commit 0ac028a

File tree

2 files changed

+81
-67
lines changed

2 files changed

+81
-67
lines changed

src/web/common.rkt

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88
(match-define (list _ args expr) prog)
99
(list 'FPCore args expr))
1010

11+
(define (fpcore-add-props core props)
12+
(match-define (list 'FPCore args expr) core)
13+
`(FPCore ,args ,@props ,expr))
14+
15+
(define (fpcore->string core)
16+
(match-define (list 'FPCore args props ... expr) core)
17+
(define props* ; make sure each property (name, value) gets put on the same line
18+
(for/list ([(prop name) (in-dict (apply dict-set* '() props))]) ; how to make a list of pairs from a list
19+
(format "~a ~a" prop name)))
20+
(pretty-format `(,(format "FPCore ~a" args) ,@props* ,expr) #:mode 'display))
21+
1122
(define/contract (render-menu sections links)
1223
(-> (listof (cons/c string? string?)) (listof (cons/c string? string?)) xexpr?)
1324
`(nav ([id "links"])
@@ -37,49 +48,55 @@
3748
`(div ,name ": " (span ([class "number"]
3849
,@(if title `([title ,title]) '()))
3950
,@values)))
40-
41-
;; TODO(interface): currently program->c doesn't take the repr into account
51+
4252
(define languages
43-
`(("TeX" . ,core->tex)
53+
`(("TeX" . ,core->tex)
54+
("FPCore" . ,fpcore->string)
4455
("C" . ,(curryr core->c "code"))))
4556

4657
(define (render-program #:to [result #f] test)
4758
(define output-prec (test-output-prec test))
48-
(define output-repr (get-representation output-prec))
49-
5059
(define in-prog (program->fpcore (resugar-program (test-program test) output-prec)))
5160
(define out-prog (and result (program->fpcore (resugar-program result output-prec))))
5261

62+
(define in-prog* (fpcore-add-props in-prog (list ':precision output-prec)))
63+
(define out-prog* (and out-prog (fpcore-add-props out-prog (list ':precision output-prec))))
64+
5365
(define versions
5466
(reap [sow]
5567
(for ([(lang converter) (in-dict languages)])
56-
(when (and (fpcore? in-prog) (or (not out-prog) (fpcore? out-prog)))
57-
(sow (cons lang (cons (converter in-prog)
58-
(and out-prog (converter out-prog)))))))))
68+
(let ([ext (string-downcase lang)]) ; FPBench organizes compilers by extension
69+
(when (and (fpcore? in-prog*) (or (not out-prog*) (fpcore? out-prog*))
70+
(or (equal? ext "fpcore")
71+
(and (supported-by-lang? in-prog* ext) ; must be valid in a given language
72+
(or (not out-prog*) (supported-by-lang? out-prog* ext)))))
73+
(sow (cons lang (cons (converter in-prog*)
74+
(and out-prog* (converter out-prog*)))))
75+
)))))
5976

6077
`(section ([id "program"])
6178
,(if (equal? (program-body (test-precondition test)) 'TRUE)
6279
""
6380
`(div ([id "precondition"])
6481
(div ([class "program math"])
65-
"\\[" ,(expr->tex (program-body (test-precondition test))) "\\]")))
82+
"\\[" ,(expr->tex (resugar-program (program-body (test-precondition test)) output-prec)) "\\]")))
6683
(select ([id "language"])
6784
(option "Math")
6885
,@(for/list ([lang (in-dict-keys versions)])
6986
`(option ,lang)))
7087
(div ([class "implementation"] [data-language "Math"])
71-
(div ([class "program math"]) "\\[" ,(core->tex in-prog) "\\]")
88+
(div ([class "program math"]) "\\[" ,(core->tex in-prog*) "\\]")
7289
,@(if result
7390
`((div ([class "arrow"]) "")
74-
(div ([class "program math"]) "\\[" ,(core->tex out-prog) "\\]"))
91+
(div ([class "program math"]) "\\[" ,(core->tex out-prog*) "\\]"))
7592
`()))
7693
,@(for/list ([(lang outs) (in-dict versions)])
7794
(match-define (cons out-input out-output) outs)
7895
`(div ([class "implementation"] [data-language ,lang])
79-
(pre ([class "program"]) ,out-input)
80-
,@(if out-output
96+
(pre ([class "program"]) ,out-input)
97+
,@(if out-output
8198
`((div ([class "arrow"]) "")
82-
(pre ([class "program"]) ,out-output))
99+
(pre ([class "program"]) ,out-output))
83100
`())))))
84101

85102
(define/contract (render-command-line)

src/web/plot.rkt

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,34 @@
1212
(define *blue-theme* (color-theme "lightblue" "blue" "navy"))
1313
(define *green-theme* (color-theme "lightgreen" "green" "darkgreen"))
1414

15-
(define (double-transform)
16-
;; TODO: needs to be repr-aware
17-
(define repr (get-representation* 'real))
15+
(define (repr-transform repr)
1816
(invertible-function
1917
(compose (representation-repr->ordinal repr) (curryr ->flonum repr))
2018
(compose (representation-ordinal->repr repr) round)))
2119

22-
(define (double-axis)
23-
(make-axis-transform (double-transform)))
20+
(define (repr-axis repr)
21+
(make-axis-transform (repr-transform repr)))
2422

25-
(define (power10-upto x)
26-
(define ->repr (if (flag-set? 'precision 'double) real->double-flonum real->single-flonum))
23+
(define (power10-upto x repr)
2724
(if (= x 0)
2825
'()
2926
(reverse
3027
(let loop ([power (round (/ (log x) (log 10)))])
31-
(define value (->repr (expt 10.0 power)))
28+
(define value (fl->repr (expt 10.0 power) repr))
3229
(if (= value 0) '() (cons value (loop (- power 1))))))))
3330

34-
(define (possible-ticks min max)
31+
(define (possible-ticks min max repr)
3532
;; Either
3633
;; + 0 is between min and max
3734
;; + 0 is one of min and max (two cases)
3835
;; + min and max are on the same side of 0 (two cases)
3936
(sort
4037
(cond
41-
[(< (* min max) 0) (append (map - (power10-upto (- min))) '(0.0) (power10-upto max))]
42-
[(= min 0) (cons 0 (power10-upto max))]
43-
[(= max 0) (append (map - (power10-upto (abs min))) '(0.0))]
44-
[(> min 0) (set-subtract (power10-upto max) (power10-upto min))]
45-
[(< max 0) (map - (set-subtract (power10-upto (abs min)) (power10-upto (abs max))))])
38+
[(< (* min max) 0) (append (map - (power10-upto (- min) repr)) '(0.0) (power10-upto max repr))]
39+
[(= min 0) (cons 0 (power10-upto max repr))]
40+
[(= max 0) (append (map - (power10-upto (abs min) repr)) '(0.0))]
41+
[(> min 0) (set-subtract (power10-upto max repr) (power10-upto min repr))]
42+
[(< max 0) (map - (set-subtract (power10-upto (abs min) repr) (power10-upto (abs max) repr)))])
4643
<))
4744

4845
(define (pick-spaced-indices necessary possible number)
@@ -82,26 +79,25 @@
8279
(define stopper (if (null? necessary) 0 (apply max necessary)))
8380
(car (argmin (λ (x) (+ (cdr x) (sqr (- (- possible 1) (caar x))))) (drop (vector->list final) stopper))))
8481

85-
(define (choose-ticks min max)
86-
(define possible (possible-ticks min max))
87-
82+
(define (choose-ticks min max repr)
83+
(define possible (possible-ticks min max repr))
8884
(cond
8985
[(< (length possible) 12)
9086
;; If there aren't enough possible big ticks, we fall back to the standard method
9187
(append
9288
(if (<= min 1.0 max) (list (pre-tick 1.0 #t)) '())
9389
(if (<= min 0.0 max) (list (pre-tick 0.0 #t)) '())
9490
(if (<= min -1.0 max) (list (pre-tick -1.0 #t)) '())
95-
((ticks-layout (ticks-scale (linear-ticks #:number 6 #:base 10 #:divisors '(1 2 5)) (double-transform))) min max))]
91+
((ticks-layout (ticks-scale (linear-ticks #:number 6 #:base 10 #:divisors '(1 2 5)) (repr-transform repr))) min max))]
9692
[else
9793
(define necessary (filter identity (map (curry index-of possible) '(1.0 0.0 -1.0))))
9894
(define major-indices (pick-spaced-indices necessary (length possible) 12))
9995
(for/list ([idx major-indices])
10096
(pre-tick (list-ref possible idx) #t))]))
10197

102-
(define (double-ticks)
98+
(define (repr-ticks repr)
10399
(ticks
104-
choose-ticks
100+
(curryr choose-ticks repr)
105101
(λ (lft rgt pticks)
106102
(for/list ([ptick pticks])
107103
(define val (pre-tick-value ptick))
@@ -155,11 +151,11 @@
155151
(y-tick-lines)
156152
(error-points (map (const 1) pts) pts #:axis axis #:alpha 0)))
157153

158-
(define (with-herbie-plot #:title [title #f] thunk)
154+
(define (with-herbie-plot repr #:title [title #f] thunk)
159155
(parameterize ([plot-width 800] [plot-height 300]
160156
[plot-background-alpha 0]
161-
[plot-x-transform (double-axis)]
162-
[plot-x-ticks (double-ticks)]
157+
[plot-x-transform (repr-axis repr)]
158+
[plot-x-ticks (repr-ticks repr)]
163159
[plot-x-tick-label-anchor 'top]
164160
[plot-x-label #f]
165161
[plot-x-far-axis? #t]
@@ -177,19 +173,19 @@
177173
(if port
178174
(lambda () (plot-file (cons (y-axis) renderers) port kind #:y-min 0 #:y-max bit-width))
179175
(lambda () (plot-pict (cons (y-axis) renderers) #:y-min 0 #:y-max bit-width))))
180-
(with-herbie-plot #:title title thunk))
176+
(with-herbie-plot repr #:title title thunk))
181177

182-
(define (with-alt-plot #:title [title #f] thunk)
178+
(define (with-alt-plot repr #:title [title #f] thunk)
183179
(parameterize ([plot-width 800] [plot-height 800]
184180
[plot-background-alpha 1]
185-
[plot-x-transform (double-axis)]
186-
[plot-x-ticks (double-ticks)]
181+
[plot-x-transform (repr-axis repr)]
182+
[plot-x-ticks (repr-ticks repr)]
187183
[plot-x-tick-label-anchor 'top]
188184
[plot-x-label #f]
189185
[plot-x-far-axis? #t]
190186
[plot-x-far-ticks no-ticks]
191-
[plot-y-transform (double-axis)]
192-
[plot-y-ticks (double-ticks)]
187+
[plot-y-transform (repr-axis repr)]
188+
[plot-y-ticks (repr-ticks repr)]
193189
[plot-y-tick-label-anchor 'left]
194190
[plot-y-label #f]
195191
[plot-y-far-axis? #t]
@@ -198,10 +194,10 @@
198194
[plot-y-label title])
199195
(thunk)))
200196

201-
(define (alt-plot #:port [port #f] #:kind [kind 'auto] #:title [title #f] . renderers)
197+
(define (alt-plot repr #:port [port #f] #:kind [kind 'auto] #:title [title #f] . renderers)
202198
(define thunk
203199
(lambda () (plot-file renderers port kind)))
204-
(with-alt-plot #:title title thunk))
200+
(with-alt-plot repr #:title title thunk))
205201

206202
(define (errors-by x errs pts)
207203
(sort (map (λ (pt err) (cons (apply x pt) err)) pts errs) < #:key car))
@@ -243,25 +239,30 @@
243239
(for/list ([i (in-range idx-min (+ idx-min bin-size))]) (vector-ref errs i))])
244240
<)))))
245241

246-
(define (error-avg errs pts #:axis [axis 0] #:vars [vars '()]
242+
(define (error-avg errs pts repr #:axis [axis 0] #:vars [vars '()]
247243
#:color [color *blue-theme*] #:bin-size [bin-size 128])
248244
(define get-coord
249245
(if (number? axis)
250246
(λ x (list-ref x axis))
251247
(eval-prog `(λ ,vars ,axis) 'fl)))
248+
249+
; works for binary64, binary32 (probably not for posits)
250+
(define-values (maxbound minbound)
251+
(let ([hi (sub1 ((representation-repr->ordinal repr) (fl->repr +inf.0 repr)))]
252+
[lo (add1 ((representation-repr->ordinal repr) (fl->repr -inf.0 repr)))])
253+
(values ((representation-ordinal->repr repr) hi)
254+
((representation-ordinal->repr repr) lo))))
255+
252256
(define eby (errors-by get-coord errs pts))
253257
(define histogram-f (histogram-function eby #:bin-size bin-size))
254258
(define (avg-fun x)
255259
(define h (histogram-f x))
256260
(/ (apply + (vector->list h)) (vector-length h)))
257-
;; TODO: This is a weird hack in several ways, and ideally wouldn't exist
258-
;; TODO: This doesn't work in single-precision
259-
(define-values (min max)
261+
(define-values (min* max*) ; plot requires finite bounds
260262
(match* ((car (first eby)) (car (last eby)))
261263
[(x x) (values #f #f)]
262-
[(x y) (values (flmax (flnext -inf.0) x) (flmin (flprev +inf.0) y))]))
263-
(function avg-fun min max
264-
#:width 2 #:color (color-theme-fit color)))
264+
[(x y) (values (max minbound x) (min maxbound y))])) ; hence this
265+
(function avg-fun min* max* #:width 2 #:color (color-theme-fit color)))
265266

266267
(define (error-mark x-val)
267268
(inverse (const x-val) #:color "gray" #:width 3))
@@ -282,17 +283,11 @@
282283
(define info (regime-info altn))
283284
(and info (sp-bexpr (car info))))
284285

285-
(define (points->doubles pts repr)
286-
(cond
287-
[(or (real? (caar pts)) (complex? (caar pts))) pts]
288-
[else
289-
(map (curry map (curryr repr->fl repr)) pts)]))
290-
291286
(define (make-axis-plot result out idx)
292287
(define var (list-ref (test-vars (test-result-test result)) idx))
293288
(define split-var? (equal? var (regime-var (test-success-end-alt result))))
294289
(define repr (get-representation (test-output-prec (test-result-test result))))
295-
(define pts (points->doubles (test-success-newpoints result) repr))
290+
(define pts (test-success-newpoints result))
296291
(herbie-plot
297292
#:port out #:kind 'png
298293
repr
@@ -307,18 +302,19 @@
307302
['b (values *blue-theme* test-success-end-error)]))
308303

309304
(define repr (get-representation (test-output-prec (test-result-test result))))
310-
(define pts (points->doubles (test-success-newpoints result) repr))
305+
(define pts (test-success-newpoints result))
311306
(define err (accessor result))
312307

313308
(herbie-plot
314309
#:port out #:kind 'png
315310
repr
316311
(error-points err pts #:axis idx #:color theme)
317-
(error-avg err pts #:axis idx #:color theme)))
312+
(error-avg err pts repr #:axis idx #:color theme)))
318313

319-
(define (make-alt-plots point-alt-idxs alt-idxs title out)
314+
(define (make-alt-plots point-alt-idxs alt-idxs title out result)
320315
(define best-alt-point-renderers (best-alt-points point-alt-idxs alt-idxs))
321-
(alt-plot best-alt-point-renderers #:port out #:kind 'png #:title title))
316+
(define repr (get-representation (test-output-prec (test-result-test result))))
317+
(alt-plot best-alt-point-renderers repr #:port out #:kind 'png #:title title))
322318

323319
(define (make-point-alt-idxs result)
324320
(define repr (get-representation (test-output-prec (test-result-test result))))
@@ -328,9 +324,10 @@
328324
(define newexacts (test-success-newexacts result))
329325
(oracle-error-idx all-alt-bodies newpoints newexacts repr))
330326

331-
(define (make-contour-plot point-colors var-idxs title out)
327+
(define (make-contour-plot point-colors var-idxs title out result)
332328
(define point-renderers (herbie-ratio-point-renderers point-colors var-idxs))
333-
(alt-plot point-renderers #:port out #:kind 'png #:title title))
329+
(define repr (get-representation (test-output-prec (test-result-test result))))
330+
(alt-plot point-renderers repr #:port out #:kind 'png #:title title))
334331

335332
#;
336333
(define (make-plots result rdir profile? debug?)
@@ -350,9 +347,9 @@
350347
(define alt-idxs (list i j))
351348
(define title (format "~a vs ~a" (list-ref vars j) (list-ref vars i)))
352349
(open-file (- (+ j (* i (- (length vars)))) 1) #:type 'best-alts
353-
make-alt-plots point-alt-idxs alt-idxs title)
350+
make-alt-plots point-alt-idxs alt-idxs title result)
354351
(open-file (- (+ j (* i (- (length vars)))) 1) #:type 'contours
355-
make-contour-plot point-colors alt-idxs title)))
352+
make-contour-plot point-colors alt-idxs title result)))
356353

357354
(for ([var (test-vars (test-result-test result))] [idx (in-naturals)])
358355
(when (> (length (remove-duplicates (map (curryr list-ref idx) (test-success-newpoints result)))) 1)

0 commit comments

Comments
 (0)