deeplearning4j/deeplearning4j · error · IllegalStateException
Null INDArray after forward pass for variable
Error message
Null INDArray after forward pass for variable "${e.getKey()}" What it means
OpValidation.validate validates that each SameDiff variable produces a non-null INDArray after the forward pass. When a TestCase defines an expected-result function for a variable name, but executing the SameDiff graph yields no INDArray registered under that variable's name, this IllegalStateException is thrown. It signals that the forward pass silently failed to produce output for an expected variable.
Solutions
- Check that the name passed to TestCase.expectedOutput matches an existing SDVariable via sameDiff.variableMap.keySet() or sameDiff.variables()
- Ensure the variable participates in the forward pass outputs (it must be reachable/executed); use sameDiff.exec() results and verify the name is present
- Run sameDiff.summary() or print variables to confirm names before validating
- Update the test case after any rename/removal of SDVariables in the graph
Example fix
// before
TestCase tc = new TestCase(sd).expectedOutput("outpu", expected);
// after
TestCase tc = new TestCase(sd).expectedOutput("output", expected); Defensive patterns
Strategy: validation
Validate before calling
if (!sd.variableMap().containsKey("output")) throw new IllegalArgumentException("variable 'output' not in SameDiff graph"); Type guard
boolean varExists(SameDiff sd, String name) { return sd.variableMap().containsKey(name); } Try / catch
try { OpValidation.validate(tc); } catch (IllegalStateException e) { if (e.getMessage().contains("Null INDArray")) { log.error("variable produced no output: {}", e.getMessage()); } throw e; } Prevention
- Extract variable names from sameDiff.variables() rather than hard-coding strings
- Call sameDiff.exec()/output once manually and assert all expected names are present before validating
- Re-run tests after any SDVariable rename
When it happens
Trigger: Calling OpValidation.validate(new TestCase().expectedOutput("varName", fn)) where the SameDiff instance's exec().get("varName") returns null — e.g. the variable name doesn't exist in the graph, or the variable isn't produced by the executed forward pass.
Common situations: Typo in the variable name passed to expectedOutput(); test written against a renamed or removed SDVariable; graph pruned so the variable is no longer an output; using op validation (validateOp) while passing SameDiff variable names.
Related errors
- Error checking forward pass for variable
- Exception encountered during gradient check
- Exception thrown during op output validation for output
- Ratio value should be in range of 0.0 > X < 1.0
- Ratio value should be in range of 0.0 > X < 1.0
AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07).
Data as JSON: /api/errors/ce5897a0fc96b1a2.
Report an issue: GitHub.
Appendix: source
Thrown at nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/autodiff/validation/OpValidation.java:174
Set<String> reqVars = testCase.fwdTestFns().keySet();
Map<String,INDArray> out;
try {
out = sd.output(testCase.placeholderValues(), new ArrayList<>(reqVars));
} catch (Exception e) {
throw new RuntimeException("Error during forward pass testing" + testCase.testNameErrMsg(), e);
}
for (Map.Entry<String, Function<INDArray, String>> e : testCase.fwdTestFns().entrySet()) {
SDVariable v = sd.getVariable(e.getKey());
if (v == null) {
throw new IllegalStateException("Test case has expected result function defined for variable \"" +
e.getKey() + "\" but SameDiff instance does not have a variable for this name" + testCase.testNameErrMsg());
}
INDArray actual = out.get(v.name());
if (actual == null) {
throw new IllegalStateException("Null INDArray after forward pass for variable \"" + e.getKey() + "\"");
}
String error;
try {
error = e.getValue().apply(actual);
} catch (Throwable t) {
throw new IllegalStateException("Error checking forward pass for variable \"" + e.getKey() + "\": exception was" +
" thrown by forward pass validation function", t);
}
if (error != null) {
return testCase.testNameErrMsg() + ": Variable " + e.getKey() + " failed: " + error;
}
}
ByteBuffer serializedAfterExec = null;
if(testCase.testFlatBufferSerialization() == TestCase.TestSerialization.BEFORE_EXEC || testCase.testFlatBufferSerialization() == TestCase.TestSerialization.BOTH){
serializedAfterExec = testCase.sameDiff().asFlatBuffers(true);View on GitHub (pinned to 4c22ac5fe4)