Implement softmax(t, dim) that computes the softmax of a tensor along a given dimension in a numerically stable way.
Softmax maps real values to a probability distribution:
Naive exp can overflow for large inputs. Stabilize by subtracting the max along dim before the exponential, then normalize so the result sums to 1 along that dimension.
Requirements
softmax(t: torch.Tensor, dim: int) -> torch.Tensordim (keep dimensions for broadcasting)exp, then divide by the sum along dimtorch.softmax or F.softmax