deeplearning4j/deeplearning4j · error · UnsupportedOperationException

Differentiation of…

Error message

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

What it means

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.

Solutions

  1. 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.
  2. If you need gradients w.r.t. mmul inputs, compute them manually with forward ops (transpose the upstream gradient and the other operand).
  3. If MmulBp must support doDiff for your use case, implement doDiff in a subclass or file/patch an upstream issue in deeplearning4j.

Example fix

// before
SDVariable bp = new MmulBp(sameDiff, a, b, g, ...); // manual backprop op
SDVariable d = bp.diff(loss);
// after
SDVariable c = a.mmul("c", b); // let SameDiff autodiff handle it
Map<String,SDVariable> grads = sameDiff.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 *Bp ops; use sd.diff()/calculateGradients");

Type guard

boolean isBackpropOp(DifferentialFunction f){ return f.getClass().getSimpleName().endsWith("Bp"); }
// if (isBackpropOp(op)) -> never call doDiff on it

Try / catch

try {
    grads = sameDiff.calculateGradients(loss, a, b);
} catch (UnsupportedOperationException e) {
    if (e.getMessage() != null && e.getMessage().contains("Differentiation of") && e.getMessage().contains("not supported")) {
        throw new IllegalStateException("Graph contains a non-differentiable op: rebuild with forward ops", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

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/ab01e439b80f5204. 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/MmulBp.java:87

          addIArgument(ArrayUtil.fromBoolean(mt.isTransposeA()),
                       ArrayUtil.fromBoolean(mt.isTransposeB()),
                       ArrayUtil.fromBoolean(mt.isTransposeResult()));
        }
    }


    public MmulBp() {}


    @Override
    public String opName() {
        return "matmul_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() == 3, "Expected exactly 3 inputs to matmul_bp op, got %s", dataTypes);
        Preconditions.checkState(dataTypes.get(0).isFPType() && dataTypes.get(1).isFPType() && dataTypes.get(0).isFPType(), "Inputs to matmul_bp op must both be a floating" +
                "point type: got %s", dataTypes);
        return dataTypes.subList(0, 2);
    }
}

View on GitHub (pinned to 4c22ac5fe4)