# Alterna AI chapter 17: verified standalone examples
prompt_len_17a=4
prefill_pairs_17a=sum(range(1,prompt_len_17a+1))
next_decode_pairs_17a=prompt_len_17a+1
assert prefill_pairs_17a==10 and next_decode_pairs_17a==5


layers_17b,kv_heads_17b,head_dim_17b,tokens_17b,bytes_17b=2,1,4,3,2
cache_bytes_17b=2*layers_17b*kv_heads_17b*head_dim_17b*tokens_17b*bytes_17b
assert cache_bytes_17b==96


from math import exp
logits_17c=[2.0,1.0,0.0]; temperature_17c=1.0; top_p_17c=0.8
raw_17c=[exp(v/temperature_17c-max(logits_17c)) for v in logits_17c]
probs_17c=[v/sum(raw_17c) for v in raw_17c]; order_17c=sorted(range(3),key=lambda i:(-probs_17c[i],i))
kept_17c=[]; mass_17c=0.0
for i_17c in order_17c:
    kept_17c.append(i_17c); mass_17c+=probs_17c[i_17c]
    if mass_17c>=top_p_17c: break
assert kept_17c==[0,1] and abs(sum(probs_17c[i] for i in kept_17c)-mass_17c)<1e-12


remaining_17d=[2,1,3]; active_counts_17d=[]
while any(remaining_17d):
    active_counts_17d.append(sum(value>0 for value in remaining_17d))
    remaining_17d=[max(0,value-1) for value in remaining_17d]
assert active_counts_17d==[3,2,1] and sum(active_counts_17d)==6


values_17e=[-1.0,0.2,0.9]; scale_17e=max(map(abs,values_17e))/127
codes_17e=[max(-127,min(127,round(value/scale_17e))) for value in values_17e]
restored_17e=[code*scale_17e for code in codes_17e]
max_error_17e=max(abs(a-b) for a,b in zip(values_17e,restored_17e))
assert codes_17e==[-127,25,114] and max_error_17e<scale_17e/2


from math import ceil
latencies_17f=[0.4,0.8,0.6,1.0]; ordered_17f=sorted(latencies_17f)
p95_17f=ordered_17f[ceil(0.95*len(ordered_17f))-1]
throughput_17f=12/2.0
assert p95_17f==1.0 and throughput_17f==6.0

print('Chapter 17: 6 examples PASS')
