deeplearning4j/deeplearning4j · error · UnsupportedOperationException

Differentiation of…

Error message

Differentiation of org.nd4j.linalg.api.ops.impl.reduce.TensorMmulBp not supported

What it means

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.

Solutions

  1. Use the forward tensormmul/mmul op and let SameDiff generate the backprop op automatically.
  2. If gradients are needed manually, compute them with standard tensor-contraction gradient formulas using forward ops, not the Bp op.
  3. If your framework genuinely requires differentiating TensorMmulBp, implement doDiff in a subclass or raise it with the deeplearning4j maintainers.

Example fix

// before
SDVariable bp = new TensorMmulBp(sd, a, b, g, dimsA, dimsB);
SDVariable d = bp.diff(loss);
// after
SDVariable c = sd.tensormmul(a, b, dimsA, dimsB);
Map<String,SDVariable> grads = sd.calculateGradients(loss, a, b);
Defensive patterns

Strategy: validation

Validate before calling

// before differentiating, ensure no manual *Bp ops are in the graph
boolean hasManualBp = sameDiff.ops().stream()
    .anyMatch(op -> op.getClass().getSimpleName().endsWith("Bp"));
if (hasManualBp) throw new IllegalStateException("Remove manual TensorMmulBp; use sd.tensormmul + autodiff");

Type guard

boolean isBackpropOp(DifferentialFunction f){ return f.getClass().getSimpleName().endsWith("Bp"); }

Try / catch

try {
    grads = sameDiff.calculateGradients(loss, a, b);
} catch (UnsupportedOperationException e) {
    if (e.getMessage() != null && e.getMessage().contains("TensorMmulBp")) {
        throw new IllegalStateException("Rebuild contraction with forward tensormmul; autodiff supplies the backward", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling doDiff on a TensorMmulBp instance directly, or including a TensorMmulBp op as an interior node of a SameDiff graph that is later differentiated.

Common situations: Hand-building tensor-contraction backprop ops instead of using automatic differentiation; attempting second-order gradients through tensormmul with manual BP ops.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07). Data as JSON: /api/errors/31cb09353c24a0c8. Report an issue: GitHub.

Appendix: source

Thrown at nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/api/ops/impl/reduce/TensorMmulBp.java:80

        this(x, y, gradAtOutput, dldx, dldy, axes[0], axes[1] );
    }

    public TensorMmulBp(INDArray x, INDArray y, INDArray gradAtOutput, INDArray dldx, INDArray dldy, int[] axesX, int[] axesY ) {
            super(null, new INDArray[]{x, y, gradAtOutput}, new INDArray[]{dldx, dldy});
            addIArgument(axesX.length);
            addIArgument(axesX);
            addIArgument(axesY.length);
            addIArgument(axesY);
    }

    @Override
    public String opName() {
        return "tensormmul_bp";
    }

    @Override
    public List<SDVariable> doDiff(List<SDVariable> i_v1) {
        throw new UnsupportedOperationException("Differentiation of " + getClass().getName() + " not supported");
    }

    @Override
    public List<DataType> calculateOutputDataTypes(List<DataType> dataTypes) {
        Preconditions.checkState(dataTypes != null && dataTypes.size() == 4, "Expected exactly 3 inputs to tensormmul_bp op, got %s", dataTypes);
        Preconditions.checkState(dataTypes.get(0).isFPType() && dataTypes.get(1).isFPType() && dataTypes.get(0).isFPType(), "Inputs to tensormmul_bp op must both be a floating" +
                "point type: got %s", dataTypes);
        return dataTypes.subList(0, 2);
    }

}

View on GitHub (pinned to 4c22ac5fe4)