Skip to content

Commit

Permalink
ADBenchmarkModel removed
Browse files Browse the repository at this point in the history
  • Loading branch information
sshin23 committed Aug 31, 2023
1 parent f73de35 commit fe6a5f1
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 223 deletions.
7 changes: 3 additions & 4 deletions benchmark/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[weakdeps]
AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
oneAPI = "8f75cd03-7ff8-4ecb-9b8f-daf728133b1b"
AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e"

[extensions]
ExaModelsBenchmarkAMDGPU = "AMDGPU"
ExaModelsBenchmarkCUDA = "CUDA"
ExaModelsBenchmarkKernelAbstractions = "KernelAbstractions"
ExaModelsBenchmarkOneAPI = "oneAPI"
ExaModelsBenchmarkAMDGPU = "AMDGPU"

[compat]
AMDGPU = "0.5"
CUDA = "4"
KernelAbstractions = "0.9"
oneAPI = "1"
AMDGPU = "0.5"

255 changes: 64 additions & 191 deletions benchmark/src/benchmark.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,27 @@ function runbenchmark(
)
end

try
GC.enable(false)
for (name, model, args) in cases
for (cnt, arg) in enumerate(args)

@info "Benchmarking $name$cnt"
label = "$name$cnt"

jump_model = eval(Symbol("jump_"*string(model)))
ampl_model = eval(Symbol("ampl_"*string(model)))
exa_model = eval(Symbol("exa_"*string(model)))

update_benchmark_result!(result[1], "JuMP", label, jump_model, arg, neval)
update_benchmark_result!(result[2], "AMPL", label, ampl_model, arg, neval)
update_benchmark_result!(result[3], "ExaModels (single)", label, exa_model, arg, neval)

for (i,(name, hardware, backend)) in enumerate(DEVICES)
update_benchmark_result!(
result[3+i], "ExaModels ($name)", label, exa_model, arg, neval; backend = backend
)
end

for (name, model, args) in cases
for (cnt, arg) in enumerate(args)

@info "Benchmarking $name$cnt"
label = "$name$cnt"

jump_model = eval(Symbol("jump_"*string(model)))
ampl_model = eval(Symbol("ampl_"*string(model)))
exa_model = eval(Symbol("exa_"*string(model)))

update_benchmark_result!(result[1], "JuMP", label, jump_model, arg, neval)
update_benchmark_result!(result[2], "AMPL", label, ampl_model, arg, neval)
update_benchmark_result!(result[3], "ExaModels (single)", label, exa_model, arg, neval)

for (i,(name, hardware, backend)) in enumerate(DEVICES)
update_benchmark_result!(
result[3+i], "ExaModels ($name)", label, exa_model, arg, neval; backend = backend
)
end

end
catch e
throw(e)
finally
GC.enable(true)
end

if deploy
Expand Down Expand Up @@ -207,6 +200,19 @@ function project!(l, x, u; marg = 1e-4)
end
end


function btime(f, N)
f()
GC.gc()
return minimum(
begin
t = time_ns()
f()
(time_ns() - t) / 1e9
end
for i=1:N)
end

function benchmark_callbacks(m; N = 100)
nvar = m.meta.nvar
ncon = m.meta.ncon
Expand All @@ -226,44 +232,42 @@ function benchmark_callbacks(m; N = 100)

project!(m.meta.lvar, x, m.meta.uvar)

GC.enable(false)

ExaModels.obj(m, x)
tobj = (1 / N) * @elapsed for t = 1:N
ExaModels.obj(m, x)
end

ExaModels.cons!(m, x, c)
tcon = (1 / N) * @elapsed for t = 1:N
ExaModels.cons!(m, x, c)
end

ExaModels.grad!(m, x, g)
tgrad = (1 / N) * @elapsed for t = 1:N
ExaModels.grad!(m, x, g)
end

ExaModels.jac_coord!(m, x, jac)
tjac = (1 / N) * @elapsed for t = 1:N
ExaModels.jac_coord!(m, x, jac)
end

ExaModels.hess_coord!(m, x, y, hess)
thess = (1 / N) * @elapsed for t = 1:N
ExaModels.hess_coord!(m, x, y, hess)
end
tobj = btime(
()->ExaModels.obj(m, x),
N
)

ExaModels.jac_structure!(m, jrows, jcols)
tjacs = (1 / N) * @elapsed for t = 1:N
ExaModels.jac_structure!(m, jrows, jcols)
end
tcon = btime(
() -> ExaModels.cons!(m, x, c),
N
)

tgrad = btime(
() -> ExaModels.grad!(m, x, g),
N
)


ExaModels.hess_structure!(m, hrows, hcols)
thesss = (1 / N) * @elapsed for t = 1:N
ExaModels.hess_structure!(m, hrows, hcols)
end
tjac = btime(
() -> ExaModels.jac_coord!(m, x, jac),
N
)

GC.enable(true)
thess = btime(
() -> ExaModels.hess_coord!(m, x, y, hess),
N
)

tjacs = btime(
() -> ExaModels.jac_structure!(m, jrows, jcols),
N
)

thesss = btime(
() -> ExaModels.hess_structure!(m, hrows, hcols),
N
)

return (
tobj = tobj,
Expand Down Expand Up @@ -294,137 +298,6 @@ function parse_log(file)
end
end

Base.@kwdef mutable struct ADBenchmarkStats
obj_cnt::Int = 0
cons_cnt::Int = 0
grad_cnt::Int = 0
jac_coord_cnt::Int = 0
hess_coord_cnt::Int = 0
jac_structure_cnt::Int = 0
hess_structure_cnt::Int = 0
obj_time::Float64 = 0.0
cons_time::Float64 = 0.0
grad_time::Float64 = 0.0
jac_coord_time::Float64 = 0.0
hess_coord_time::Float64 = 0.0
jac_structure_time::Float64 = 0.0
hess_structure_time::Float64 = 0.0
end

struct ADBenchmarkModel{T,VT,I<:NLPModels.AbstractNLPModel{T,VT}} <:
NLPModels.AbstractNLPModel{T,VT}
inner::I
meta::NLPModels.AbstractNLPModelMeta{T,VT}
stats::ADBenchmarkStats
counters::NLPModels.Counters
end

function ADBenchmarkModel(m)
return ADBenchmarkModel(m, m.meta, ADBenchmarkStats(), NLPModels.Counters())
end
function ADBenchmarkModel(c::ExaModels.ExaCore; kwargs...)
m = ExaModels.Model(c; kwargs...)
return ADBenchmarkModel(m)
end

function NLPModels.jac_structure!(
m::M,
rows::V,
cols::V,
) where {M<:ADBenchmarkModel,V<:AbstractVector}

m.stats.jac_structure_cnt += 1
t = time()
NLPModels.jac_structure!(m.inner, rows, cols)
m.stats.jac_structure_time += time() - t
end

function NLPModels.hess_structure!(
m::M,
rows::V,
cols::V,
) where {M<:ADBenchmarkModel,V<:AbstractVector}

m.stats.hess_structure_cnt += 1
t = time()
NLPModels.hess_structure!(m.inner, rows, cols)
m.stats.hess_structure_time += time() - t
end

function NLPModels.obj(m::ADBenchmarkModel, x::AbstractVector)

m.stats.obj_cnt += 1
t = time()
o = NLPModels.obj(m.inner, x)
m.stats.obj_time += time() - t
return o
end
function NLPModels.cons!(m::ADBenchmarkModel, x::AbstractVector, g::AbstractVector)

m.stats.cons_cnt += 1
t = time()
NLPModels.cons!(m.inner, x, g)
m.stats.cons_time += time() - t
return
end
function NLPModels.grad!(m::ADBenchmarkModel, x::AbstractVector, f::AbstractVector)

m.stats.grad_cnt += 1
t = time()
NLPModels.grad!(m.inner, x, f)
m.stats.grad_time += time() - t
return
end
function NLPModels.jac_coord!(
m::ADBenchmarkModel,
x::AbstractVector,
jac::AbstractVector,
)

m.stats.jac_coord_cnt += 1
t = time()
NLPModels.jac_coord!(m.inner, x, jac)
m.stats.jac_coord_time += time() - t
return
end
function NLPModels.hess_coord!(
m::ADBenchmarkModel,
x::AbstractVector,
y::AbstractVector,
hess::AbstractVector;
obj_weight = one(eltype(x)),
)

m.stats.hess_coord_cnt += 1
t = time()
NLPModels.hess_coord!(m.inner, x, y, hess; obj_weight = obj_weight)
m.stats.hess_coord_time += time() - t
return
end


function Base.print(io::IO, e::ADBenchmarkModel)
tot = 0.0
for f in fieldnames(ADBenchmarkStats)
if endswith(string(f), "cnt")
@printf "%20s: %13i times\n" f getfield(e.stats, f)
else
t = getfield(e.stats, f)
@printf "%20s: %13.6f secs\n" f t
tot += t
end
end
println("------------------------------------------")
@printf " total AD time: %13.6f secs\n" tot
end
Base.show(io::IO, ::MIME"text/plain", e::ADBenchmarkModel) = Base.print(io, e);


adtime(m) = sum(
getproperty(m.stats, v) for
v in filter(name -> endswith(string(name), "time"), propertynames(m.stats))
)

function parsek(str)
if str[end] == 'k'
parse(Int, str[1:end-1]) * 1000
Expand Down
8 changes: 3 additions & 5 deletions benchmark/src/distillation.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
function exa_distillation_column_model(N= 3; kwargs...)
return ADBenchmarkModel(
ExaModelsExamples.distillation_column_model(N; kwargs...)
)
return ExaModelsExamples.distillation_column_model(N; kwargs...)
end

function jump_distillation_column_model(T = 3)
Expand Down Expand Up @@ -80,7 +78,7 @@ function jump_distillation_column_model(T = 3)
yA[t, i] * (1 - xA[t, i]) - alpha * xA[t, i] * (1 - yA[t, i]) == 0
)

return ADBenchmarkModel(MathOptNLPModel(m))
return MathOptNLPModel(m)
end

function ampl_distillation_column_model(T = 3)
Expand Down Expand Up @@ -197,5 +195,5 @@ function ampl_distillation_column_model(T = 3)
m.write($nlfile)
"""
end
return ADBenchmarkModel(AmplNLReader.AmplModel(nlfile))
return AmplNLReader.AmplModel(nlfile)
end
8 changes: 3 additions & 5 deletions benchmark/src/luksanvlcek.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
function exa_luksan_vlcek_model(N = 3; kwargs...)
return ADBenchmarkModel(
ExaModelsExamples.luksan_vlcek_model(N; kwargs...)
)
return ExaModelsExamples.luksan_vlcek_model(N; kwargs...)
end

function jump_luksan_vlcek_model(N = 3)
Expand All @@ -16,7 +14,7 @@ function jump_luksan_vlcek_model(N = 3)
)
JuMP.@NLobjective(jm, Min, sum(100(x[i-1]^2 - x[i])^2 + (x[i-1] - 1)^2 for i = 2:N))

return ADBenchmarkModel(MathOptNLPModel(jm))
return MathOptNLPModel(jm)
end


Expand Down Expand Up @@ -48,5 +46,5 @@ function ampl_luksan_vlcek_model(N = 3)
"""
end

return ADBenchmarkModel(AmplNLReader.AmplModel(nlfile))
return AmplNLReader.AmplModel(nlfile)
end
12 changes: 3 additions & 9 deletions benchmark/src/opf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

function exa_ac_power_model(filename = "pglib_opf_case3_lmbd.m"; kwargs...)
return ADBenchmarkModel(
ExaModelsExamples.ac_power_model(filename; kwargs...)
)
return ExaModelsExamples.ac_power_model(filename; kwargs...)
end
function jump_ac_power_model(filename = "pglib_opf_case3_lmbd.m")

Expand Down Expand Up @@ -101,9 +99,7 @@ function jump_ac_power_model(filename = "pglib_opf_case3_lmbd.m")
JuMP.@constraint(model, p_to^2 + q_to^2 <= branch["rate_a"]^2)
end

return ADBenchmarkModel(
MathOptNLPModel(model)
)
return MathOptNLPModel(model)
end


Expand Down Expand Up @@ -258,8 +254,6 @@ function ampl_ac_power_model(filename = "pglib_opf_case3_lmbd.m")
"""
end

return ADBenchmarkModel(
AmplNLReader.AmplModel(nlfile)
)
return AmplNLReader.AmplModel(nlfile)
end

Loading

0 comments on commit fe6a5f1

Please sign in to comment.