def evs_to_policy(search_policy_evs, *, temperature=1.0, use_softmax=True):
    """Compute policy targets from EVs.

    Args:
        search_policy_evs: Float tensor [T, B, 7, max_actions]. Invalid values are marked with -1.
        temperature: temperature for softmax. Ignored if softmax is not used.
        use_softmax: whether to apply exp() before normalizing.

    Returns:
       search_policy_probs: Float tensor [T, B, 7, max_actions].
    """
    search_policy_probs = search_policy_evs.clone().float()
    invalid_mask = search_policy_evs < -0.5
    if use_softmax:
        search_policy_probs /= temperature
        # Using -1e8 instead of -inf so that softmax is defined even if all
        # orders are masked out.
        search_policy_probs[invalid_mask] = -1e8
        search_policy_probs = search_policy_probs.softmax(-1)
    else:
        search_policy_probs.masked_fill_(invalid_mask, 0.0)
        search_policy_probs /= (invalid_mask + 1e-20).sum(-1, keepdim=True)
    return search_policy_probs.to(search_policy_evs.dtype)