{"record":{"id":"9381fb4d27a5d06e","repo":"deeplearning4j/deeplearning4j","slug":"could-not-resolve-tensor-for-merge-operation-s-in","errorCode":null,"errorMessage":"Could not resolve tensor for Merge operation %s input %s (frame=%s, iteration=%s)","messagePattern":"Could not resolve tensor for Merge operation (.+?) input (.+?) \\(frame=(.+?), iteration=(.+?)\\)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/autodiff/samediff/internal/InferenceSession.java","lineNumber":1753,"sourceCode":"                }\n\n                // Use first available input (standard Merge behavior)\n                SDValue selectedValue = availableValues.get(0);\n                VarId selectedVarId = candidateInputs.get(0);\n\n                log.trace(\"Merge {} selected input: {} from frame {}\",\n                        m.getOwnName(), selectedVarId.getVariable(), selectedVarId.getFrame());\n\n                ExecutionResult result;\n                if(selectedValue.getSdValueType() == SDValueType.LIST) {\n                    result = ExecutionResult.createValue(selectedVarId.getVariable(), selectedValue);\n                } else if(selectedValue.getSdValueType() == SDValueType.TENSOR) {\n                    INDArray inArr = getTensorFromOutputs(selectedVarId);\n                    if (inArr == null) {\n                        inArr = selectedValue.getTensorValue();\n                    }\n                    if (inArr == null) {\n                        throw new IllegalStateException(String.format(\n                                \"Could not resolve tensor for Merge operation %s input %s (frame=%s, iteration=%s)\",\n                                op.getOwnName(), selectedVarId.getVariable(), outputFrameIter.getFrame(), outputFrameIter.getIteration()));\n                    }\n                    result = ExecutionResult.createFrom(Arrays.asList(selectedVarId.getVariable()), new INDArray[]{inArr});\n                } else {\n                    throw new IllegalStateException(\"Illegal value type \" + selectedValue.getSdValueType() + \" for Merge input \" + selectedVarId);\n                }\n\n                return result;\n            }else if (op instanceof LoopCond) {\n                //LoopCond just forwards scalar boolean to output\n                LoopCond lc = (LoopCond) op;\n                String[] argNames = lc.argNames();\n                Preconditions.checkState(argNames.length == 1, \"Expected only 1 arg name in LoopCond op, got %s\", (Object) argNames);\n                VarId vid = outputFrameIter.toVarId(argNames[0]);\n                SDValue getValue = getSdValue(vid);\n                if(getValue.getTensorValue() == null) {\n                    throw new IllegalStateException(\"Node value output at \" + vid.getVariable() + \" was not a boolean tensor!\");","sourceCodeStart":1735,"sourceCodeEnd":1771,"githubUrl":"https://github.com/deeplearning4j/deeplearning4j/blob/4c22ac5fe4a8350d05d224e7f4499429f7f69c93/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/autodiff/samediff/internal/InferenceSession.java#L1735-L1771","documentation":"During SameDiff graph execution, the InferenceSession resolved a value for an input of a Merge op (typically inside an TF-style while-loop frame/iteration), but the resulting value carried no actual INDArray tensor. The session throws IllegalStateException because Merge cannot execute without concrete tensor inputs. This indicates the upstream variable was resolved to a non-tensor value or was never materialized.","triggerScenarios":"A variable feeding a Merge node was produced as an SDValue of type TENSOR whose getTensorValue() is null (e.g. the producing op placed output in a list, or getTensorFromOutputs returned null from the outputs map and no tensor was stored). Happens in while-loop execution when the merge input was never assigned in the current frame/iteration.","commonSituations":"Imported TensorFlow while_loop/switch-merge control-flow graphs where one branch of a switch never executes; custom loops built with SameDiff.where the loop body does not write to all merge inputs; version changes in how outputs are registered per frame/iteration.","solutions":["Check that every op writing into a Merge input actually produces an INDArray output (inspect ops feeding the Merge node)","Verify loop-body ops write outputs for both merge branches (true and false switch paths)","Ensure you are not reusing an SDVariable across frames without proper placement in the loop body","Capture the execution with the visualizer (visualizationEnabled) to see which variable is unresolved at that frame/iteration","Upgrade/align nd4j version: output registration behavior for control-flow frames changed between releases"],"exampleFix":"// before: loop body only assigns the 'true' branch of a switch feeding a Merge\nSDVariable out = sd.switchFn(cond, x);\nSDVariable mergeIn = out.get(0); // false branch never produced\n// after: ensure both branches or use the merge output directly with a defined initializer\nSDVariable init = sd.zerosLike(x);\nSDVariable merged = sd.fwdMerge(init, out.get(0)); // both inputs always defined","handlingStrategy":"validation","validationCode":"SDValue v = session.getSdValue(varId);\nif (v == null || v.getSdValueType() != SDValueType.TENSOR || v.getTensorValue() == null)\n    throw new IllegalStateException(\"Merge input not materialized: \" + varId.getVariable());","typeGuard":"boolean isResolvedTensor(SDValue v) {\n    return v != null && v.getSdValueType() == SDValueType.TENSOR && v.getTensorValue() != null;\n}","tryCatchPattern":"try {\n    sd.output(allVars, placeholders);\n} catch (IllegalStateException e) {\n    if (e.getMessage().contains(\"Could not resolve tensor for Merge\")) {\n        log.error(\"Unresolved merge input; check loop body outputs\", e);\n        // rebuild graph / fix loop body\n    } else throw e;\n}","preventionTips":["Ensure loop bodies write outputs to every Merge input in each iteration","Inspect ops feeding Merge nodes after importing TF while_loop graphs","Use the SameDiff visualizer to trace per-frame/iteration output registration","Keep nd4j versions aligned when control-flow behavior changed between releases"],"tags":["samediff","control-flow","while-loop","tensor"],"backgroundTag":"internal-invariant-violation","analyzedSha":"4c22ac5fe4a8350d05d224e7f4499429f7f69c93","analyzedAt":"2026-09-07T05:27:04.714Z","contentChangedAt":"2026-09-07T05:27:04.714Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}