stanfordnlp/CoreNLP · error · RuntimeException

Can't sample from NaN

Error message

Can't sample from NaN

What it means

ArrayMath.sampleFromDistribution(double[], Random) draws an index according to the distribution d. If any entry (except the last, treated as remainder) is NaN, cumulative comparison becomes meaningless, so a RuntimeException("Can't sample from NaN") is thrown while walking the cumulative sums.

Solutions

  1. Validate the distribution before sampling: reject or repair arrays containing NaN (e.g. rebuild from a uniform distribution as fallback).
  2. Check the upstream probability computation (softmax inputs, normalization constant) for overflow or zero-sum issues.
  3. Replace NaN entries with 0.0 or renormalize with ArrayMath.normalize(d) prior to sampling when NaNs indicate dead outcomes.
  4. Catch RuntimeException and fall back to a uniform or argmax choice if occasional degenerate distributions are tolerable.

Example fix

// before
int i = ArrayMath.sampleFromDistribution(probs, rand);
// after
boolean clean = true;
for (double p : probs) { if (Double.isNaN(p)) { clean = false; break; } }
int i = clean ? ArrayMath.sampleFromDistribution(probs, rand) : rand.nextInt(probs.length);
Defensive patterns

Strategy: validation

Validate before calling

boolean clean = true;
for (int i = 0; i < d.length - 1; i++) {
  if (Double.isNaN(d[i])) { clean = false; break; }
}
if (!clean) { /* rebuild distribution or use uniform fallback */ }

Try / catch

try {
  idx = ArrayMath.sampleFromDistribution(d, rand);
} catch (RuntimeException e) {
  idx = rand.nextInt(d.length); // uniform fallback
}

Prevention

When it happens

Trigger: Calling ArrayMath.sampleFromDistribution(double[] d, Random r) where any d[i] for i < d.length - 1 is NaN — typically from an unnormalized or degenerate probability computation upstream.

Common situations: Sampling from a softmax/language-model output distribution that was computed from Inf/NaN logits, or from probabilities that were divided by a zero total mass.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/810b0678b84683fd. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/math/ArrayMath.java:1464

  public static int sampleFromDistribution(double[] d) {
    return sampleFromDistribution(d, rand);
  }

  /**
   * Samples from the distribution over values 0 through d.length given by d.
   * Assumes that the distribution sums to 1.0.
   *
   * @param d the distribution to sample from
   * @return a value from 0 to d.length
   */
  public static int sampleFromDistribution(double[] d, Random random) {
    // sample from the uniform [0,1]
    double r = random.nextDouble();
    // now compare its value to cumulative values to find what interval it falls in
    double total = 0;
    for (int i = 0; i < d.length - 1; i++) {
      if (Double.isNaN(d[i])) {
        throw new RuntimeException("Can't sample from NaN");
      }
      total += d[i];
      if (r < total) {
        return i;
      }
    }
    return d.length - 1; // in case the "double-math" didn't total to exactly 1.0
  }

  /**
   * Samples from the distribution over values 0 through d.length given by d.
   * Assumes that the distribution sums to 1.0.
   *
   * @param d the distribution to sample from
   * @return a value from 0 to d.length
   */
  public static int sampleFromDistribution(float[] d, Random random) {
    // sample from the uniform [0,1]

View on GitHub (pinned to 1b7edd19c4)