# Softmax

The softmax operator, defined by

$$ \sigma: \mathbb{R}^K \rightarrow (0,1)^K, $$

where $K > 1$, takes a tuple

$$ \mathbf{z} = (z\_1, \ldots, z\_K) \in \mathbb{R}^K $$

and computes each component of vector $\sigma(\mathbf{z}) \in (0,1)^K$ with

$$ \sigma(\mathbf{z})*i = \frac{e^{z\_i}}{\sum*{j=1}^{K} e^{z\_j}}. $$

We refer to $N = \sum e^{z\_j}$ as the *normalising* factor. After applying softmax, each component will be in the interval (0,1), and the components will add up to 1, so that they can be interpreted as probabilities.

To avoid numerical instability, a variant called "safe softmax" replaces the original softmax. It subtracts the maximum value of the input vector from each element before applying the exponential. This simple trick doesn’t change the output, but it significantly improves numerical stability.

$$ \sigma(\mathbf{z})*i = \frac{e^{z\_i - z*{max}}}{\sum\_{j=1}^{K} e^{z\_j - z\_{max}}}. $$

The base used in softmax is irrelevant. $e$ is used for convenience, but other bases such as 2 and 3 can also be used, since softmax is translation-invariant (adding the same constant to every logit does nothing). That is, for any base $b \geq 2$, $\sigma\_b(\mathbf{z})*i = \frac{e^{(\ln b)\\,z\_i}}{\sum*{j} e^{(\ln b)\\,z\_j}} = \sigma\left( (\ln b)\\,\mathbf{z} \right)*i$\*. To maximise the domain, we pick $b = 2$. This is what we want to prove: $\sigma(\mathbf{z})i = \frac{2^{z\_i - z\_{max}}}{\sum (2^{z\_j - z\_*{max}})}.$\*

The normalising factor $N = \sum e^{z\_j}$ needs to be known before any individual $\sigma(\mathbf{z})\_i$ can be computed. This means that at least a two-step instruction is required.

### Implementation

The sequence performs 14 virtual steps:&#x20;

| Step | Operation       | Description                           |
| ---- | --------------- | ------------------------------------- |
| 1    | VirtualConst(0) | Initialize zero tensor                |
| 2    | Gte             | Compute `ge0 = (z >= 0)`              |
| 3    | Sub             | Compute `neg_z = -z`                  |
| 4    | Select          | Compute `abs_z = select(ge0, z, -z)`  |
| 5    | VirtualPow2     | Compute \`c = 2^{                     |
| 6    | VirtualConst(Q) | Constant quantization scalar          |
| 7    | Div             | Compute `d_q_over_c = Q / c`          |
| 8    | Mul             | Compute `d_q_times_c = Q * c`         |
| 9    | Select          | Select `d = (z >= 0 ? Q * c : Q / c)` |
| 10   | Sum             | `ReduceSum(d)` to get total           |
| 11   | Broadcast       | Broadcast the sum                     |
| 12   | Mul             | Compute `f = Q * d`                   |
| 13   | Div             | Normalize `g = f / e_sum`             |
| 14   | VirtualMove     | Write final result to output tensor   |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://devs.novanet.xyz/jolt-atlas-zkml/onnx/tensor-instructions/virtual-instructions/softmax.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
