-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from JuliaDiff/api-refactor
Allow chunk-sizing options, and a more robust caching layer to fix leaky generated functions
- Loading branch information
Showing
26 changed files
with
1,655 additions
and
880 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using ForwardDiff | ||
|
||
################## | ||
# Test functions # | ||
################## | ||
sqr(i) = i^2 | ||
twopicos(i) = cos(2*π*i) | ||
|
||
function ackley(x) | ||
len_recip = 1/length(x) | ||
sum_sqrs = zero(eltype(x)) | ||
sum_cos = sum_sqrs | ||
for i in x | ||
sum_cos += twopicos(i) | ||
sum_sqrs += sqr(i) | ||
end | ||
return -20 * exp(-0.2 * sqrt(len_recip*sum_sqrs)) - exp(len_recip * sum_cos) + 20 + e | ||
end | ||
|
||
############################# | ||
# Benchmark utility methods # | ||
############################# | ||
# Usage: | ||
# | ||
# benchmark ackley where length(x) = 10:10:100, taking the minimum of 4 trials: | ||
# bench_fad(ackley, 10:10:100, 4) | ||
# | ||
# benchmark ackley where len(x) = 400, taking the minimum of 8 trials: | ||
# bench_fad(ackley, 400, 4) | ||
|
||
function bench_fad(f, range, repeat=3) | ||
g = ForwardDiff.gradient(f) | ||
h = ForwardDiff.hessian(f) | ||
|
||
# warm up | ||
bench_range(f, range, 1) | ||
bench_range(g, range, 1) | ||
bench_range(h, range, 1) | ||
|
||
# actual | ||
return Dict( | ||
:ftimes => bench_range(f,range,repeat), | ||
:gtimes => bench_range(g,range,repeat), | ||
:htimes => bench_range(h,range,repeat) | ||
) | ||
end | ||
|
||
function bench_func(f, xlen, repeat) | ||
x=rand(xlen) | ||
min_time = Inf | ||
for i in 1:repeat | ||
this_time = (tic(); f(x); toq()) | ||
min_time = min(this_time, min_time) | ||
end | ||
return min_time | ||
end | ||
|
||
function bench_range(f, range, repeat=3) | ||
return [bench_func(f, xlen, repeat) for xlen in range] | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import numpy, algopy, math, timeit | ||
|
||
################ | ||
# AD functions # | ||
################ | ||
def gradient(f): | ||
def gradf(x): | ||
y = algopy.UTPM.init_jacobian(x) | ||
return algopy.UTPM.extract_jacobian(f(y)) | ||
return gradf | ||
|
||
def hessian(f): | ||
def hessf(x): | ||
y = algopy.UTPM.init_hessian(x) | ||
return algopy.UTPM.extract_hessian(x.size, f(y)) | ||
return hessf | ||
|
||
################## | ||
# Test functions # | ||
################## | ||
def ackley(x): | ||
sum_sqrs = sum(i**2 for i in x) | ||
sum_cos = sum(algopy.cos(2*math.pi*i) for i in x) | ||
len_recip = 1/len(x) | ||
return (-20 * algopy.exp(-0.2*algopy.sqrt(len_recip*sum_sqrs)) - | ||
algopy.exp(0.5*sum_cos) + 20 + math.e) | ||
|
||
############################# | ||
# Benchmark utility methods # | ||
############################# | ||
# Usage: | ||
# | ||
# benchmark ackley where len(x) = range(10,100,10), taking the minimum of 4 trials: | ||
# bench_fad(ackley, range(10,100,10), 4) | ||
# | ||
# benchmark ackley where len(x) = 400, taking the minimum of 8 trials: | ||
# bench_fad(ackley, (400,), 8) | ||
|
||
def bench_fad(f, itr, repeat): | ||
fname = f.__name__ | ||
import_stmt = 'import numpy, algopy, math; from __main__ import ' + fname + ', gradient, hessian;' | ||
return {'ftimes': bench_range(fname + '(x)', import_stmt, itr, repeat), | ||
'gtimes': bench_range('g(x)', import_stmt + 'g = gradient(' + fname + ');', itr, repeat), | ||
'htimes': bench_range('h(x)', import_stmt + 'h = hessian(' + fname + ');', itr, repeat)} | ||
|
||
def bench_range(stmt, setup, itr, repeat): | ||
x_stmt = lambda xlen: 'x = numpy.random.rand(' + str(xlen) + ')' | ||
return [min(timeit.repeat(stmt, setup=(setup + x_stmt(i)), number=1, repeat=repeat)) for i in itr] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.