{"record":{"id":"ab01e439b80f5204","repo":"deeplearning4j/deeplearning4j","slug":"differentiation-of-org-nd4j-linalg-api-ops-impl-re","errorCode":null,"errorMessage":"Differentiation of org.nd4j.linalg.api.ops.impl.reduce.MmulBp not supported","messagePattern":"Differentiation of org\\.nd4j\\.linalg\\.api\\.ops\\.impl\\.reduce\\.MmulBp not supported","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/api/ops/impl/reduce/MmulBp.java","lineNumber":87,"sourceCode":"          addIArgument(ArrayUtil.fromBoolean(mt.isTransposeA()),\n                       ArrayUtil.fromBoolean(mt.isTransposeB()),\n                       ArrayUtil.fromBoolean(mt.isTransposeResult()));\n        }\n    }\n\n\n    public MmulBp() {}\n\n\n    @Override\n    public String opName() {\n        return \"matmul_bp\";\n    }\n\n\n    @Override\n    public List<SDVariable> doDiff(List<SDVariable> i_v1) {\n        throw new UnsupportedOperationException(\"Differentiation of \" + getClass().getName() + \" not supported\");\n    }\n\n\n    @Override\n    public List<DataType> calculateOutputDataTypes(List<DataType> dataTypes) {\n        Preconditions.checkState(dataTypes != null && dataTypes.size() == 3, \"Expected exactly 3 inputs to matmul_bp op, got %s\", dataTypes);\n        Preconditions.checkState(dataTypes.get(0).isFPType() && dataTypes.get(1).isFPType() && dataTypes.get(0).isFPType(), \"Inputs to matmul_bp op must both be a floating\" +\n                \"point type: got %s\", dataTypes);\n        return dataTypes.subList(0, 2);\n    }\n}\n\n","sourceCodeStart":69,"sourceCodeEnd":100,"githubUrl":"https://github.com/deeplearning4j/deeplearning4j/blob/4c22ac5fe4a8350d05d224e7f4499429f7f69c93/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/api/ops/impl/reduce/MmulBp.java#L69-L100","documentation":"MmulBp is the backpropagation op for matrix multiplication in SameDiff. Its doDiff method is intentionally unimplemented and unconditionally throws UnsupportedOperationException, because differentiation of a backprop op itself is not supported in ND4J. Any code path asking SameDiff to differentiate through this op fails at graph execution time.","triggerScenarios":"Calling doDiff on an MmulBp instance directly, or building a SameDiff graph in which MmulBp itself is an interior node being differentiated (i.e. requesting gradients of gradients).","commonSituations":"Manually constructing MmulBp ops instead of letting SameDiff's automatic differentiation generate backprop ops; double-differentiation setups; custom training loops that misuse internal BP ops.","solutions":["Do not use *Bp ops in user graphs; call SDVariable.mmul and rely on SameDiff's differentiator (sameDiff.diff / calculateGradients) to create backprop ops automatically.","If you need gradients w.r.t. mmul inputs, compute them manually with forward ops (transpose the upstream gradient and the other operand).","If MmulBp must support doDiff for your use case, implement doDiff in a subclass or file/patch an upstream issue in deeplearning4j."],"exampleFix":"// before\nSDVariable bp = new MmulBp(sameDiff, a, b, g, ...); // manual backprop op\nSDVariable d = bp.diff(loss);\n// after\nSDVariable c = a.mmul(\"c\", b); // let SameDiff autodiff handle it\nMap<String,SDVariable> grads = sameDiff.calculateGradients(loss, a, b);","handlingStrategy":"validation","validationCode":"// before differentiating, ensure no manual *Bp ops are in the graph\nboolean hasManualBp = sameDiff.ops().stream()\n    .anyMatch(op -> op.getClass().getSimpleName().endsWith(\"Bp\"));\nif (hasManualBp) throw new IllegalStateException(\"Remove manual *Bp ops; use sd.diff()/calculateGradients\");","typeGuard":"boolean isBackpropOp(DifferentialFunction f){ return f.getClass().getSimpleName().endsWith(\"Bp\"); }\n// if (isBackpropOp(op)) -> never call doDiff on it","tryCatchPattern":"try {\n    grads = sameDiff.calculateGradients(loss, a, b);\n} catch (UnsupportedOperationException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"Differentiation of\") && e.getMessage().contains(\"not supported\")) {\n        throw new IllegalStateException(\"Graph contains a non-differentiable op: rebuild with forward ops\", e);\n    }\n    throw e;\n}","preventionTips":["Never instantiate *Bp ops in user code; rely on SameDiff automatic differentiation.","Audit custom graphs for op classes ending in _bp/Bp before calling diff/calculateGradients.","Compute manual gradients with forward ops (transpose/mul) instead of Bp ops.","Pin to nd4j versions where the ops you need have implemented doDiff."],"tags":["nd4j","samediff","autodiff","unsupported-operation"],"backgroundTag":"unsupported-operation","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"}