| 
 | 1 | + | 
 | 2 | +using Metal, GPUArrays, LinearAlgebra, Printf, AppleAccelerate  | 
 | 3 | + | 
 | 4 | +testing = (@isdefined TESTING) && TESTING  | 
 | 5 | + | 
 | 6 | +@static if !testing  | 
 | 7 | +    using Plots  | 
 | 8 | +    using Plots.Measures  | 
 | 9 | +end  | 
 | 10 | + | 
 | 11 | +const Ts=[  | 
 | 12 | +        (Int8, Float16),  | 
 | 13 | +        (Int8, Float32),  | 
 | 14 | +        (Int16, Float32),  | 
 | 15 | +        (Float16, Float16),  | 
 | 16 | +        (Float16, Float32),  | 
 | 17 | +        (Float32, Float32),  | 
 | 18 | +    ]  | 
 | 19 | + | 
 | 20 | +n_gpu_cores = "??"  | 
 | 21 | +# Comment this out if scary. Please mention number of cores in your comment when uploading the figure  | 
 | 22 | +system_prof = read(`system_profiler SPDisplaysDataType`, String)  | 
 | 23 | +n_gpu_cores = only(match(r"Total Number of Cores:\s*(\d+)", system_prof).captures)  | 
 | 24 | + | 
 | 25 | +PLOT_TITLE = "Matmul peakflops for $(device().name) ($n_gpu_cores GPU cores)"  | 
 | 26 | + | 
 | 27 | +function cpupeakflops(; n::Integer=4096,  | 
 | 28 | +                        n_batch::Integer=1,  | 
 | 29 | +                        inT::DataType=Float32,  | 
 | 30 | +                        outT::DataType=inT,  | 
 | 31 | +                        ntrials::Integer=4,  | 
 | 32 | +                        verify=true)  | 
 | 33 | +    t = Base.zeros(Float64, ntrials)  | 
 | 34 | +    n_batch == 1 || @warn "n_batch > 1 not supported for `mul!`, running with n_batch=1"  | 
 | 35 | +    n_batch = 1  | 
 | 36 | +    shape = (n, n)  | 
 | 37 | +    for i=1:ntrials  | 
 | 38 | +        c = zeros(outT, shape...)  | 
 | 39 | +        a = ones(inT, shape...)  | 
 | 40 | +        b = ones(inT, shape...)  | 
 | 41 | +        t[i] = @elapsed mul!(c, a, b)  | 
 | 42 | +        verify && @assert only(unique(Array(c))) == n  | 
 | 43 | +    end  | 
 | 44 | + | 
 | 45 | +    return n_batch*2*Float64(n)^3 / minimum(t)  | 
 | 46 | +end  | 
 | 47 | +function _peakflops(f, n, n_batch, inT, outT, ntrials; verify=true)  | 
 | 48 | +    t = Base.zeros(Float64, ntrials)  | 
 | 49 | +    shape = n_batch == 1 ? (n, n) : (n, n, n_batch)  | 
 | 50 | +    for i=1:ntrials  | 
 | 51 | +        c = mtl(zeros(outT, shape...))  | 
 | 52 | +        a = mtl(ones(inT, shape...))  | 
 | 53 | +        b = mtl(ones(inT, shape...))  | 
 | 54 | +        t[i] = @elapsed Metal.@sync f(c, a, b)  | 
 | 55 | +        verify && @assert only(unique(Array(c))) == n  | 
 | 56 | +    end  | 
 | 57 | + | 
 | 58 | +    return n_batch*2*Float64(n)^3 / minimum(t)  | 
 | 59 | +end  | 
 | 60 | +function gpuarrpeakflops(; n::Integer=4096,  | 
 | 61 | +                           n_batch::Integer=1,  | 
 | 62 | +                           inT::DataType=Float32,  | 
 | 63 | +                           outT::DataType=inT,  | 
 | 64 | +                           ntrials::Integer=3,  | 
 | 65 | +                           verify=true)  | 
 | 66 | +    n_batch == 1 || @warn "n_batch > 1 not supported for `GPUArrays.generic_matmatmul!`, running with n_batch=1"  | 
 | 67 | +    _peakflops(n, 1, inT, outT, ntrials; verify) do c, a, b  | 
 | 68 | +        GPUArrays.generic_matmatmul!(c, LinearAlgebra.wrap(a, 'N'), LinearAlgebra.wrap(b, 'N'), 1, 0)  | 
 | 69 | +    end  | 
 | 70 | +end  | 
 | 71 | +function mpspeakflops(; n::Integer=4096,  | 
 | 72 | +                        n_batch::Integer=1,  | 
 | 73 | +                        inT::DataType=Float32,  | 
 | 74 | +                        outT::DataType=inT,  | 
 | 75 | +                        ntrials::Integer=3,  | 
 | 76 | +                        verify=true)  | 
 | 77 | +    _peakflops(MPS.matmul!, n, n_batch, inT, outT, ntrials; verify)  | 
 | 78 | +end  | 
 | 79 | +function graphpeakflops(; n::Integer=4096,  | 
 | 80 | +                          n_batch::Integer=1,  | 
 | 81 | +                          inT::DataType=Float32,  | 
 | 82 | +                          outT::DataType=inT,  | 
 | 83 | +                          ntrials::Integer=3,  | 
 | 84 | +                          verify=true)  | 
 | 85 | +    _peakflops(MPSGraphs.graph_matmul!, n, n_batch, inT, outT, ntrials; verify)  | 
 | 86 | +end  | 
 | 87 | +function anepeakflops(; kwargs...)  | 
 | 88 | +    # VERY HACKY  | 
 | 89 | +    newDesc = MPSGraphs.MPSGraphCompilationDescriptor()  | 
 | 90 | +    # Use optimization level 0 to avoid operations being moved to the neural engine  | 
 | 91 | +    newDesc.optimizationLevel = MPSGraphs.MPSGraphOptimizationLevel1  | 
 | 92 | + | 
 | 93 | +    oldDesc = MPSGraphs._default_exec_desc[].compilationDescriptor  | 
 | 94 | + | 
 | 95 | +    MPSGraphs._default_exec_desc[].compilationDescriptor = newDesc  | 
 | 96 | +    res = graphpeakflops(; kwargs...)  | 
 | 97 | +    MPSGraphs._default_exec_desc[].compilationDescriptor = oldDesc  | 
 | 98 | + | 
 | 99 | +    return res  | 
 | 100 | +end  | 
 | 101 | + | 
 | 102 | +function compare(Ns, Fs, inT, outT=inT; n_batch=1, ntrials)  | 
 | 103 | +    results = Dict()  | 
 | 104 | + | 
 | 105 | +    newFs = if (outT == Float16 || (outT == Float32 && inT == Float16))  | 
 | 106 | +        Fs  | 
 | 107 | +    else  | 
 | 108 | +        filter(x -> !occursin("ANE", x[2]),Fs)  | 
 | 109 | +    end  | 
 | 110 | + | 
 | 111 | +    for (_, info_str) in newFs  | 
 | 112 | +        results[info_str] = Float64[]  | 
 | 113 | +    end  | 
 | 114 | + | 
 | 115 | +    prefixstr = "\33[2K\r($inT, $outT) "  | 
 | 116 | +    @time "$((inT, outT))" begin  | 
 | 117 | +        for n in Ns  | 
 | 118 | +            verify = (n < maxintfloat(outT) && (inT != Float16 || (n < maxintfloat(inT))))  | 
 | 119 | +            n_str = "$n: "  | 
 | 120 | +            for (f, info_str) in newFs  | 
 | 121 | +                print(prefixstr, n_str, info_str)  | 
 | 122 | +                push!(results[info_str], f(; inT, outT, n, n_batch, ntrials, verify))  | 
 | 123 | +                GC.gc()  | 
 | 124 | +            end  | 
 | 125 | +        end  | 
 | 126 | +        print("\33[2K\r")  | 
 | 127 | +    end  | 
 | 128 | +    return results  | 
 | 129 | +end  | 
 | 130 | + | 
 | 131 | +function runcomparison(; Ns=[50, 64, 100, 128, 250, 256, 500, 512, 1000, 1024, 2000, 2048, 4000, 4096, 6000, 6144, 8000, 8192],#, 10000],  | 
 | 132 | +                Fs=[  | 
 | 133 | +                    (mpspeakflops, "MPS"),  | 
 | 134 | +                    (graphpeakflops, "MPSGraph"),  | 
 | 135 | +                    (anepeakflops, "MPSGraph (ANE)"),  | 
 | 136 | +                    # (gpuarrpeakflops, "GPUArrays"),  | 
 | 137 | +                    # (cpupeakflops, "CPU (AppleAccelerate)"), # Uncomment to test CPU performance  | 
 | 138 | +                   ],  | 
 | 139 | +                n_batch=1,  | 
 | 140 | +                ntrials=5)  | 
 | 141 | +    res = Dict()  | 
 | 142 | + | 
 | 143 | +    for (inT, outT) in Ts  | 
 | 144 | +        res[(inT,outT)] = (n_batch, Ns, compare(Ns, Fs, inT, outT; n_batch, ntrials))  | 
 | 145 | +    end  | 
 | 146 | +    return res  | 
 | 147 | +end  | 
 | 148 | + | 
 | 149 | +function plot_results(res, Fs=["MPS", "MPSGraph", "MPSGraph (ANE)"]; outpath=nothing, outtype="svg", plt_title=PLOT_TITLE)  | 
 | 150 | +    ylim_upper = 9e12  | 
 | 151 | +    resplts = []  | 
 | 152 | + | 
 | 153 | +    n_batches = []  | 
 | 154 | + | 
 | 155 | +    for (inT, outT) in Ts  | 
 | 156 | +        n_batch, Ns, tmpres = res[(inT,outT)]  | 
 | 157 | + | 
 | 158 | +        plt = plot(xlabel="N, n_batch=$(n_batch)", legendtitle="($inT, $outT)")  | 
 | 159 | +        for info_str in Fs  | 
 | 160 | +            haskey(tmpres, info_str) || continue  | 
 | 161 | + | 
 | 162 | +            flops = tmpres[info_str]  | 
 | 163 | +            peakf = @sprintf("%.3e", maximum(flops))  | 
 | 164 | +            if maximum(flops) > ylim_upper  | 
 | 165 | +                ylim_upper = maximum(flops) * 1.02  | 
 | 166 | +            end  | 
 | 167 | +            plot!(plt, Ns, tmpres[info_str]; linewidth=1.5, label="$(peakf) peak: $info_str")  | 
 | 168 | +        end  | 
 | 169 | +        push!(resplts, plt)  | 
 | 170 | +        push!(n_batches, n_batch)  | 
 | 171 | +    end  | 
 | 172 | + | 
 | 173 | +    finalplot = plot(resplts...; layout=(2,3),  | 
 | 174 | +                     ylim=(0,ylim_upper),  | 
 | 175 | +                     plot_title=plt_title,  | 
 | 176 | +                     tickfonthalign=:left,  | 
 | 177 | +                     bottommargin=15pt,  | 
 | 178 | +                     size=(2000,1200))  | 
 | 179 | +    if !isnothing(outpath)  | 
 | 180 | +        savefig(plot(finalplot, dpi=500), joinpath(outpath, "bench_all_$(first(n_batches)).$outtype"))  | 
 | 181 | +    end  | 
 | 182 | +    return finalplot  | 
 | 183 | +end  | 
 | 184 | + | 
 | 185 | +if testing  | 
 | 186 | +    runcomparison(Ns=[50, 64, 100, 128, 250, 256, 500, 512])  | 
 | 187 | +end  | 
0 commit comments