:- module(bench, [go/1, go/2, go_chol/1, go_blocked/1, go_scatter/1, go_mlp/1, go_vit/1,
                  bench/6, bench_data/6, bench_stats/7, data/2, inputs_data/2]).
:- use_module(library(array)).
:- use_module(library(array/opencl), [cl_device_info/3]).
:- use_module(library(array/buffer)).
:- use_module('../examples/linalg').
:- use_module('../examples/nn').
:- set_prolog_flag(stack_limit, 4_000_000_000).

% Kernel time alone: the handle is compiled and the inputs realized on the device before the
% clock starts; a run is a call of the handle, its result left on the device. The best of Reps
% is reported.

go_chol(Device) :-
   forall(member(N, [32, 64, 256, 1024]),
          ( cholesky(A, L, N), spd(N, As), format(atom(Lb), "cholesky ~d", [N]),
            bench_data(Lb, L, [A-As], Device, [], 3) )),
   forall(member(M-N, [1024-32, 256-64]),
          ( batched_cholesky(A, L, M, N), spd(N, As1), findall(X, ( between(1, M, _), member(X, As1) ), As),
            format(atom(Lb), "cholesky ~dx~d batch", [N, M]),
            bench_data(Lb, L, [A-As], Device, [], 3) )).

go_blocked(Device) :-
   forall(member(N-NB, [256-32, 1024-32, 1024-64]),
          ( blocked_cholesky(A, L, N, NB), spd(N, As), format(atom(Lb), "blocked cholesky ~d nb ~d", [N, NB]),
            bench_data(Lb, L, [A-As], Device, [], 3) )).

% bench_data(+Label, +Out, +Data, +Device, +Options, +Reps): the best time of Reps runs of a
% handle, its inputs realized on the device beforehand and its result left there, so that no
% transfer is timed; and the memory of the intermediates: the bytes of their arena, the bytes
% they would take apart, and the peak live at one kernel.
bench_data(Label, Out, Data, Device, Options0, Reps) :-
   bench_stats(Label, Out, Data, Device, Options0, Reps, S),
   stats{kernels: NK, best: Best, bytes: B, apart: Ap, peak: Pk} :< S,
   format("~w  ~w  kernels=~d  best=~3f ms  arena: ~d KB (apart ~d KB, peak ~d KB)~n",
          [Label, Device, NK, Best*1000, B // 1024, Ap // 1024, Pk // 1024]).

% bench_stats(+Label, +Out, +Data, +Device, +Options, +Reps, -Stats): the figures bench_data/6
% prints, as a dict.
bench_stats(Label, Out, Data, Device, Options0, Reps, Stats) :-
   compile:device(Device, Dev, _, _),
   device_options(Dev, Derived),
   merge_options(Options0, Derived, Options),
   compile:kernels(Out, Options, Kernels),
   compile:flat_plan(Kernels, Flat), length(Flat, NK),
   findall(Stats0,                                       % the extents bound by the data, as run/4 does
           ( compile:bind_extents(Kernels, Data),
             arena_stats(Kernels, Out, Dev, B, Ap, Pk),
             handle:signature(Out, Named, _, _),
             maplist(device_input(Data, Device), Named, Args),
             compile(Out, [device(Device) | Options], Exe),
             run(Exe, Args, _),                                   % warm up
             findall(T, ( between(1, Reps, _), get_time(T0), run(Exe, Args, _), get_time(T1), T is T1 - T0 ), Ts),
             release(Exe),
             min_list(Ts, Best),
             Stats0 = stats{label: Label, device: Device, kernels: NK, best: Best, bytes: B, apart: Ap, peak: Pk} ),
           [Stats]).

% arena_stats(+Plan, +Out, +Dev, -Bytes, -Apart, -Peak): the arena of the intermediates at the
% extents bound, the bytes they take apart, and the peak live at one kernel.
arena_stats(Plan, Out, Dev, Bytes, Apart, Peak) :-
   compile:arena_names(Plan, Out, intermediates, Inter),
   cl_device_info(Dev, mem_base_addr_align, Bits), Align is Bits // 8,
   compile:layout(Plan, Inter, Align, layout(Slots, Goals, Bytes)),
   maplist(call, Goals),
   aggregate_all(sum(S), member(slot(_, S, _), Slots), Apart),
   compile:lifetimes(Plan, Spans),
   length(Inter, N), compile:ids(N, Ids),
   maplist(compile:aligned_bytes(Align), Inter, Sizes),
   maplist(compile:buffer_of(Spans), Ids, Inter, Sizes, Bufs),
   compile:peak(Bufs, Peak).

% device_input(+Data, +Device, +Slot, -Arg): the input of a slot realized on Device: its data,
% or zeroes; a realized name is its own argument.
device_input(_, _, slot(Name, _, _, _), Arg), core:realized(Name) =>
   Arg = Name.
device_input(Data, Device, slot(Name, Shape, Kind, _), Arg) =>
   kind_storage(Kind, Storage, T),
   compile:size(Shape, N),
   (  member(N1-Vals, Data), N1 == Name
   -> maplist(compile:coerce(T), Vals, Es), compound_name_arguments(Term, data, Es),
      buffer_from_data(Host, Storage, N, Term)
   ;  buffer_alloc(Host, Storage, N, true)
   ),
   shape(H, Shape), kind(H, Kind), value(H, Host),
   copy(H, Arg, Device),
   realize([Arg], [], []).

kind_storage(float, float32, float).
kind_storage(int, int32, int).
kind_storage(bool, int32, int).

scatter_data(M, N, Is, Ws) :-
   numlist(1, M, Ks), maplist([X, Y]>>(Y is (X * 7919) mod N), Ks, Is), maplist([X, Y]>>(Y is X mod 97), Ks, Ws).

go_scatter(Device) :-
   forall(member(M-N, [1048576-256, 1048576-1048576]),
          ( scatter(Idx, W, H, M, N), scatter_data(M, N, Is, Ws), format(atom(Lb), "scatter ~d into ~d", [M, N]),
            bench_data(Lb, H, [Idx-Is, W-Ws], Device, [], 3) )),
   embedding_backward(Idx2, G, E, 65536, 10000, 64), scatter_data(65536, 10000, Is2, _),
   numlist(1, 4194304, Gs0), maplist([X, Y]>>(Y is X mod 7), Gs0, Gs),
   bench_data('embedding backward 65536x64 into 10000', E, [Idx2-Is2, G-Gs], Device, [], 3).

go_vit(Device) :-
   S = 256, D = 768, H = 12, F = 3072,
   vit_block(X, Y, S, D, H, F),
   inputs_data(Y, Data),
   bench_data(vit_block_256x768x12, Y, Data, Device, [], 3), shape(X, _).

go_mlp(Device) :-
   S = 256, D = 768, H = 3072,
   mlp(X, Y, S, D, H),
   inputs_data(Y, Data),
   bench_data(mlp_256x768x3072, Y, Data, Device, [], 3), shape(X, _).

% inputs_data(+Out, -Data): Name-Values for every input of Out's program, from data/2.
inputs_data(Out, Data) :-
   compile:program(Out, Boxes), compile:names(Boxes, Names),
   include([N]>>(N \== Out, compile:boxes_of(N, [])), Names, Inputs),
   maplist([In, In-Vals]>>data(In, Vals), Inputs, Data).

data(Name, Vals) :-
   shape(Name, Shape), foldl([E, S0, S1]>>(S1 is S0*E), Shape, 1, N),
   numlist(1, N, Is), maplist(datum, Is, Vals).

% Centred, small values: a transformer's activations stay in range, with no denormals, which
% a CPU handles slowly.
datum(I, V) :- V is (float(I mod 97) / 97 - 0.5) * 0.2.

bench(Label, Out, Inputs, Device, Options0, Reps) :-
   maplist([In, In-Vals]>>data(In, Vals), Inputs, Data),
   bench_data(Label, Out, Data, Device, Options0, Reps).

go(Device) :- go(Device, []).
go(Device, Opts) :-
   matmul(A, B, C, 512, 512, 512), bench('matmul 512^3 ', C, [A, B], Device, Opts, 5),
   matmul(A2, B2, C2, 1024, 1024, 1024), bench('matmul 1024^3', C2, [A2, B2], Device, Opts, 3),
   softmax(X, Y, 1024, 1024), bench('softmax 1024x1024', Y, [X], Device, [local(64)], 5),
   softmax(X2, Y2, 64, 4096), bench('softmax 64x4096 ', Y2, [X2], Device, [local(128)], 5),
   dot(DA, DB, S, 1048576), bench('dot 1M          ', S, [DA, DB], Device, [local(256)], 5).
