Numerically stable softmax

Write codeMedium18 minOpenAI and Anthropic style

Your task

Implement a numerically stable softmax over a non-empty list of real-valued logits. Softmax(x)_i = exp(x_i) / sum_j exp(x_j). To avoid overflow on large logits, subtract the maximum logit from every element before exponentiating (this does not change the result mathematically). Return the list of probabilities, each rounded to 6 decimal places.

Examples

softmax(logits: list[float]) -> list[float]
Example 1
Inputsoftmax([0, 0])
Output[0.5, 0.5]