"""Teaching arithmetic, not a GPU benchmark. Python 3.10+, standard library."""
from math import isclose


def kv_gib(batch, tokens, layers, kv_heads, head_dim, bytes_per_value):
    values = (batch, tokens, layers, kv_heads, head_dim, bytes_per_value)
    if any(not isinstance(x, int) or isinstance(x, bool) or x <= 0 for x in values):
        raise ValueError("All dimensions and bytes per value must be positive integers")
    return 2 * batch * tokens * layers * kv_heads * head_dim * bytes_per_value / 2**30


def cost_per_success(hourly_cost, accepted_per_second):
    if hourly_cost < 0 or accepted_per_second <= 0:
        raise ValueError("Nonnegative cost and positive accepted throughput required")
    return hourly_cost / (3600 * accepted_per_second)


if __name__ == '__main__':
    small = kv_gib(8, 8192, 32, 8, 128, 2)
    long = kv_gib(8, 32768, 32, 8, 128, 2)
    assert small == 8 and long == 32
    assert kv_gib(8, 8192, 32, 32, 128, 2) == 32
    assert kv_gib(8, 8192, 32, 8, 128, 1) == 4
    assert isclose(cost_per_success(8, 2), 8/7200)
    assert cost_per_success(8, 1) == 2 * cost_per_success(8, 2)
    for args in [(0, 1, 1, 1, 1, 2), (1, -1, 1, 1, 1, 2)]:
        try:
            kv_gib(*args)
        except ValueError:
            pass
        else:
            raise AssertionError('Invalid dimensions accepted')
    print(f'KV: 8k = {small:.1f} GiB; 32k = {long:.1f} GiB')
    print(f'Cost per accepted request: ${cost_per_success(8, 2):.6f}')
    print('8 checks passed')
