{"record":{"id":"31cb09353c24a0c8","repo":"deeplearning4j/deeplearning4j","slug":"differentiation-of-org-nd4j-linalg-api-ops-impl-re-31cb09","errorCode":null,"errorMessage":"Differentiation of org.nd4j.linalg.api.ops.impl.reduce.TensorMmulBp not supported","messagePattern":"Differentiation of org\\.nd4j\\.linalg\\.api\\.ops\\.impl\\.reduce\\.TensorMmulBp 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/TensorMmulBp.java","lineNumber":80,"sourceCode":"        this(x, y, gradAtOutput, dldx, dldy, axes[0], axes[1] );\n    }\n\n    public TensorMmulBp(INDArray x, INDArray y, INDArray gradAtOutput, INDArray dldx, INDArray dldy, int[] axesX, int[] axesY ) {\n            super(null, new INDArray[]{x, y, gradAtOutput}, new INDArray[]{dldx, dldy});\n            addIArgument(axesX.length);\n            addIArgument(axesX);\n            addIArgument(axesY.length);\n            addIArgument(axesY);\n    }\n\n    @Override\n    public String opName() {\n        return \"tensormmul_bp\";\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    @Override\n    public List<DataType> calculateOutputDataTypes(List<DataType> dataTypes) {\n        Preconditions.checkState(dataTypes != null && dataTypes.size() == 4, \"Expected exactly 3 inputs to tensormmul_bp op, got %s\", dataTypes);\n        Preconditions.checkState(dataTypes.get(0).isFPType() && dataTypes.get(1).isFPType() && dataTypes.get(0).isFPType(), \"Inputs to tensormmul_bp op must both be a floating\" +\n                \"point type: got %s\", dataTypes);\n        return dataTypes.subList(0, 2);\n    }\n\n}\n","sourceCodeStart":62,"sourceCodeEnd":92,"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/TensorMmulBp.java#L62-L92","documentation":"TensorMmulBp is the backprop op for tensormmul (batched tensor contraction). Its doDiff is a deliberate stub that throws UnsupportedOperationException, because a backprop op is itself not differentiated. It only fails when something asks for gradients of the gradient.","triggerScenarios":"Calling doDiff on a TensorMmulBp instance directly, or including a TensorMmulBp op as an interior node of a SameDiff graph that is later differentiated.","commonSituations":"Hand-building tensor-contraction backprop ops instead of using automatic differentiation; attempting second-order gradients through tensormmul with manual BP ops.","solutions":["Use the forward tensormmul/mmul op and let SameDiff generate the backprop op automatically.","If gradients are needed manually, compute them with standard tensor-contraction gradient formulas using forward ops, not the Bp op.","If your framework genuinely requires differentiating TensorMmulBp, implement doDiff in a subclass or raise it with the deeplearning4j maintainers."],"exampleFix":"// before\nSDVariable bp = new TensorMmulBp(sd, a, b, g, dimsA, dimsB);\nSDVariable d = bp.diff(loss);\n// after\nSDVariable c = sd.tensormmul(a, b, dimsA, dimsB);\nMap<String,SDVariable> grads = sd.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 TensorMmulBp; use sd.tensormmul + autodiff\");","typeGuard":"boolean isBackpropOp(DifferentialFunction f){ return f.getClass().getSimpleName().endsWith(\"Bp\"); }","tryCatchPattern":"try {\n    grads = sameDiff.calculateGradients(loss, a, b);\n} catch (UnsupportedOperationException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"TensorMmulBp\")) {\n        throw new IllegalStateException(\"Rebuild contraction with forward tensormmul; autodiff supplies the backward\", e);\n    }\n    throw e;\n}","preventionTips":["Use sd.tensormmul/mmul forward ops and let autodiff build backwards.","Avoid second-order differentiation through manually placed Bp ops.","Review graphs for TensorMmulBp before calling diff.","Implement doDiff in a subclass only if the contraction gradient math is well understood."],"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"}