Skip to content

Commit 043ceaa

Browse files
committed
some lower bounds made
1 parent ffafb16 commit 043ceaa

File tree

2 files changed

+53
-38
lines changed

2 files changed

+53
-38
lines changed

eval/adjust.rkt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
(require "tricks.rkt"
44
"../ops/all.rkt"
5-
"machine.rkt")
5+
"machine.rkt"
6+
racket/list)
67

78
(provide backward-pass)
89

@@ -96,7 +97,8 @@
9697
(define output (vector-ref vregs n)) ; output of the current instr
9798

9899
(define intro (vector-ref vprecs-new (- n varc))) ; intro for the current instruction
99-
(define ampls (get-bounds op output srcs)) ; ampls for the tail instructions
100+
(define bounds (get-bounds op output srcs)) ; ampl bounds for the tail instructions
101+
(define ampls (map second bounds))
100102

101103
(define final-parent-precision
102104
(max (+ intro (vector-ref vstart-precs (- n varc))) (*base-tuning-precision*)))

eval/tricks.rkt

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#lang racket/base
22

3-
(provide get-bounds
4-
get-slack)
5-
63
(require racket/match
74
racket/function
85
racket/list)
@@ -11,6 +8,9 @@
118
"../ops/all.rkt"
129
"machine.rkt")
1310

11+
(provide get-bounds
12+
get-slack)
13+
1414
(define (get-slack [iter (*sampling-iteration*)])
1515
(match iter
1616
[0 0]
@@ -25,7 +25,8 @@
2525

2626
; We assume the interval x is valid. Critical not to take mpfr-exp of inf or 0,
2727
; the results are platform-dependant
28-
(define (maxlog x [iter (*sampling-iteration*)])
28+
(define (maxlog x #:underestimate [underestimate #f])
29+
(define iter (if underestimate (- (*sampling-iteration*) 1) (*sampling-iteration*)))
2930
(define lo (ival-lo x))
3031
(define hi (ival-hi x))
3132
(cond
@@ -36,7 +37,8 @@
3637
[else
3738
(+ (max (mpfr-exp lo) (mpfr-exp hi)) 1)])) ; x does not contain inf, safe with respect to 0.bf
3839

39-
(define (minlog x [iter (*sampling-iteration*)])
40+
(define (minlog x #:underestimate [underestimate #f])
41+
(define iter (if underestimate (- (*sampling-iteration*) 1) (*sampling-iteration*)))
4042
(define lo (ival-lo x))
4143
(define hi (ival-hi x))
4244
(cond
@@ -72,38 +74,42 @@
7274
; Function calculates an ampl factor per input for a certain output and inputs using condition formulas,
7375
; where an ampl is an additional precision that needs to be added to srcs evaluation so,
7476
; that the output will be fixed in its precision when evaluating again
77+
; Additionaly, the function retures lower bound of ampl factor for the early exit mechanism
78+
; Output: '( '(upper-ampl-bound lower-ampl-bound) ...) with len(srcs) number of elements
7579
(define (get-bounds op z srcs)
7680
(case (object-name op)
7781
[(ival-mult)
7882
; k = 1: logspan(y)
7983
; k = 2: logspan(x)
8084
(define x (first srcs))
8185
(define y (second srcs))
82-
(list (logspan y) ; exponent per x
83-
(logspan x))] ; exponent per y
86+
(list (list (logspan y) 0) ; bounds per x
87+
(list (logspan x) 0))] ; bounds per y
8488

8589
[(ival-div)
8690
; k = 1: logspan(y)
8791
; k = 2: logspan(x) + 2 * logspan(y)
8892
(define x (first srcs))
8993
(define y (second srcs))
90-
(list (logspan y) ; exponent per x
91-
(+ (logspan x) (* 2 (logspan y))))] ; exponent per y
94+
(list (list (logspan y) 0) ; bounds per x
95+
(list (+ (logspan x) (* 2 (logspan y))) 0))] ; bounds per y
9296

9397
[(ival-sqrt ival-cbrt)
9498
; sqrt: logspan(x)/2 - 1
9599
; cbrt: logspan(x)*2/3 - 1
96100
(define x (first srcs))
97-
(list (quotient (logspan x) 2))]
101+
(list (list (quotient (logspan x) 2) 0))]
98102

99103
[(ival-add ival-sub)
100104
; k = 1: maxlog(x) - minlog(z)
101105
; k = 2: maxlog(y) - minlog(z)
102106
(define x (first srcs))
103107
(define y (second srcs))
104108

105-
(list (- (maxlog x) (minlog z)) ; exponent per x
106-
(- (maxlog y) (minlog z)))] ; exponent per y
109+
(list (list (- (maxlog x) (minlog z))
110+
(- (minlog x #:underestimate #t) (maxlog z #:underestimate #t))) ; bounds per x
111+
(list (- (maxlog y) (minlog z))
112+
(- (minlog y #:underestimate #t) (maxlog z #:underestimate #t))))] ; bounds per y
107113

108114
[(ival-pow)
109115
; k = 1: maxlog(y) + logspan(x) + logspan(z)
@@ -115,46 +121,52 @@
115121
; solution - add more slack for y to converge
116122
(define slack (if (and (crosses-zero? z) (bfnegative? (ival-lo x))) (get-slack) 0))
117123

118-
(list (+ (maxlog y) (logspan x) (logspan z)) ; exponent per x
119-
(+ (maxlog y) (max (abs (maxlog x)) (abs (minlog x))) (logspan z) slack))] ; exponent per y
124+
(list (list (+ (maxlog y) (logspan x) (logspan z)) (minlog y #:underestimate #t)) ; bounds per x
125+
(list (+ (maxlog y) (max (abs (maxlog x)) (abs (minlog x))) (logspan z) slack)
126+
(minlog y #:underestimate #t)))] ; bounds per y
120127

121128
[(ival-exp ival-exp2)
122129
; maxlog(x) + logspan(z)
123130
(define x (car srcs))
124-
(list (+ (maxlog x) (logspan z)))]
131+
(list (list (+ (maxlog x) (logspan z)) (minlog x #:underestimate #t)))]
125132

126133
[(ival-tan)
127134
; maxlog(x) + max(|minlog(z)|,|maxlog(z)|) + logspan(z) + 1
128135
(define x (first srcs))
129-
(list (+ (maxlog x) (max (abs (maxlog z)) (abs (minlog z))) (logspan z) 1))]
136+
(list (list (+ (maxlog x) (max (abs (maxlog z)) (abs (minlog z))) (logspan z) 1)
137+
(+ (minlog x #:underestimate #t)
138+
(min (abs (maxlog z #:underestimate #t)) (abs (minlog z #:underestimate #t))))))]
130139

131140
[(ival-sin)
132141
; maxlog(x) - minlog(z)
133142
(define x (first srcs))
134-
(list (- (maxlog x) (minlog z)))]
143+
(list (list (- (maxlog x) (minlog z)) (- (maxlog z #:underestimate #t))))]
135144

136145
[(ival-cos)
137146
; maxlog(x) - minlog(z) + min(maxlog(x), 0)
138147
(define x (first srcs))
139-
(list (+ (- (maxlog x) (minlog z)) (min (maxlog x) 0)))]
148+
(list (list (+ (- (maxlog x) (minlog z)) (min (maxlog x) 0)) (- (maxlog z #:underestimate #t))))]
140149

141150
[(ival-sinh)
142151
; maxlog(x) + logspan(z) - min(minlog(x), 0)
143152
(define x (first srcs))
144-
(list (- (+ (maxlog x) (logspan z)) (min (minlog x) 0)))]
153+
(list (list (- (+ (maxlog x) (logspan z)) (min (minlog x) 0))
154+
(max 0 (minlog x #:underestimate #t))))]
145155

146156
[(ival-cosh)
147157
; maxlog(x) + logspan(z) + min(maxlog(x), 0)
148158
(define x (first srcs))
149-
(list (+ (maxlog x) (logspan z) (min (maxlog x) 0)))]
159+
(list (list (+ (maxlog x) (logspan z) (min (maxlog x) 0))
160+
(max 0 (minlog x #:underestimate #t))))]
150161

151162
[(ival-log ival-log2 ival-log10)
152163
; log: logspan(x) - minlog(z)
153164
; log2: logspan(x) - minlog(z) + 1
154165
; log10: logspan(x) - minlog(z) - 1
155166
(define x (first srcs))
156-
(list (+ (- (logspan x) (minlog z)) 1))]
167+
(list (list (+ (- (logspan x) (minlog z)) 1) (- (maxlog z #:underestimate #t))))]
157168

169+
; ---------------------------------------- TODO: LOWER BOUND NEEDED BELOW -----------------
158170
[(ival-asin)
159171
; maxlog(x) - log[1-x^2]/2 - minlog(z)
160172
; ^^^^^^^^^^^^
@@ -165,7 +177,7 @@
165177
(get-slack) ; assumes that log[1-x^2]/2 is equal to slack
166178
0))
167179

168-
(list (+ (- (maxlog x) (minlog z)) slack))]
180+
(list (list (+ (- (maxlog x) (minlog z)) slack) 0))]
169181

170182
[(ival-acos)
171183
; maxlog(x) - log[1-x^2]/2 - minlog(z)
@@ -177,12 +189,12 @@
177189
(get-slack) ; assumes that log[1-x^2]/2 is equal to slack
178190
0))
179191

180-
(list (+ (- (maxlog x) (minlog z)) slack))]
192+
(list (list (+ (- (maxlog x) (minlog z)) slack) 0))]
181193

182194
[(ival-atan)
183195
; logspan(x) - min(|minlog(x)|, |maxlog(x)|) - minlog(z)
184196
(define x (first srcs))
185-
(list (- (logspan x) (min (abs (minlog x)) (abs (maxlog x))) (minlog z)))]
197+
(list (list (- (logspan x) (min (abs (minlog x)) (abs (maxlog x))) (minlog z)) 0))]
186198

187199
[(ival-fmod ival-remainder)
188200
; x mod y = x - y*q, where q is rnd_down(x/y)
@@ -198,8 +210,8 @@
198210
(get-slack) ; y crosses zero
199211
0))
200212

201-
(list (- (maxlog x) (minlog z)) ; exponent per x
202-
(+ (- (maxlog x) (minlog z)) slack))] ; exponent per y
213+
(list (list (- (maxlog x) (minlog z)) 0) ; bounds per x
214+
(list (+ (- (maxlog x) (minlog z)) slack) 0))] ; bounds per y
203215

204216
; Currently log1p has a very poor approximation
205217
[(ival-log1p)
@@ -215,32 +227,33 @@
215227
(get-slack) ; if x in negative
216228
0))
217229

218-
(list (+ (- (maxlog x) (minlog z)) slack))]
230+
(list (list (+ (- (maxlog x) (minlog z)) slack) 0))]
219231

220232
; Currently expm1 has a very poor solution for negative values
221233
[(ival-expm1)
222234
; log[Гexpm1] = log[x * e^x / expm1] <= max(1 + maxlog(x), 1 + maxlog(x) - minlog(z))
223235
(define x (first srcs))
224-
(list (max (+ 1 (maxlog x)) (+ 1 (- (maxlog x) (minlog z)))))]
236+
(list (list (max (+ 1 (maxlog x)) (+ 1 (- (maxlog x) (minlog z)))) 0))]
225237

226238
[(ival-atan2)
227239
; maxlog(x) + maxlog(y) - 2*max(minlog(x), minlog(y)) - minlog(z)
228240
(define x (first srcs))
229241
(define y (second srcs))
230242

231-
(make-list 2 (- (+ (maxlog x) (maxlog y)) (* 2 (max (minlog x) (minlog y))) (minlog z)))]
243+
(make-list 2
244+
(list (- (+ (maxlog x) (maxlog y)) (* 2 (max (minlog x) (minlog y))) (minlog z)) 0))]
232245

233246
[(ival-tanh)
234247
; logspan(z) + logspan(x)
235248
(define x (first srcs))
236-
(list (+ (logspan z) (logspan x)))]
249+
(list (list (+ (logspan z) (logspan x)) 0))]
237250

238251
[(ival-atanh)
239252
; log[Гarctanh] = maxlog(x) - log[(1-x^2)] - minlog(z) = 1 if x < 0.5, otherwise slack
240253
; ^^^^^^^
241254
; a possible uncertainty
242255
(define x (first srcs))
243-
(list (if (>= (maxlog x) 1) (get-slack) 1))]
256+
(list (list (if (>= (maxlog x) 1) (get-slack) 1) 0))]
244257

245258
[(ival-acosh)
246259
; log[Гacosh] = log[x / (sqrt(x-1) * sqrt(x+1) * acosh)] <= -minlog(z) + slack
@@ -250,16 +263,16 @@
250263
(get-slack)
251264
0))
252265

253-
(list (- slack z-exp))]
266+
(list (list (- slack z-exp) 0))]
254267

255268
[(ival-pow2)
256269
; same as multiplication
257270
(define x (first srcs))
258-
(list (+ (logspan x) 1))]
271+
(list (list (+ (logspan x) 1) 0))]
259272

260273
; TODO
261-
[(ival-erfc ival-erf ival-lgamma ival-tgamma ival-asinh ival-logb) (list (get-slack))]
274+
[(ival-erfc ival-erf ival-lgamma ival-tgamma ival-asinh ival-logb) (list (list (get-slack) 0))]
262275
; TODO
263-
[(ival-ceil ival-floor ival-rint ival-round ival-trunc) (list (get-slack))]
276+
[(ival-ceil ival-floor ival-rint ival-round ival-trunc) (list (list (get-slack) 0))]
264277

265-
[else (map (const 0) srcs)])) ; exponents for arguments
278+
[else (map (list (const 0) (const 0)) srcs)])) ; exponents for arguments

0 commit comments

Comments
 (0)