{"record":{"id":"f4cd1b810fbef617","repo":"deeplearning4j/deeplearning4j","slug":"cannot-merge-time-series-with-mask-rank","errorCode":null,"errorMessage":"Cannot merge time series with mask rank ","messagePattern":"Cannot merge time series with mask rank ","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/dataset/api/DataSetUtil.java","lineNumber":546,"sourceCode":"                    long thisNExamples = a.size(0);\n                    long thisLength = a.size(2);\n                    arr.put(new INDArrayIndex[] {NDArrayIndex.interval(examplesSoFar, examplesSoFar + thisNExamples),\n                                    NDArrayIndex.all(), NDArrayIndex.interval(0, thisLength)}, a);\n\n                    if (m == null) {\n                        //This mask is null -> equivalent to \"all present\"\n                        mask.get(NDArrayIndex.interval(examplesSoFar, examplesSoFar + thisNExamples),\n                                        NDArrayIndex.all(), NDArrayIndex.interval(0, thisLength)).assign(1);\n                    } else {\n                        mask.put(new INDArrayIndex[] {\n                                        NDArrayIndex.interval(examplesSoFar, examplesSoFar + thisNExamples),\n                                        NDArrayIndex.all(), NDArrayIndex.interval(0, thisLength)}, m);\n                    }\n\n                    examplesSoFar += thisNExamples;\n                }\n            } else {\n                throw new UnsupportedOperationException(\"Cannot merge time series with mask rank \" + maskRank);\n            }\n        }\n\n        return new Pair<>(arr, mask);\n    }\n\n    /**\n     * Merge the specified 4d arrays and masks. See {@link #mergeFeatures(INDArray[], INDArray[])}\n     * and {@link #mergeLabels(INDArray[], INDArray[])}\n     *\n     * @param arrays   Arrays to merge\n     * @param masks    Mask arrays to merge\n     * @param inOutIdx Index to extract out before merging\n     * @return Merged arrays and mask\n     */\n    public static Pair<INDArray, INDArray> merge4d(INDArray[][] arrays, INDArray[][] masks, int inOutIdx) {\n        Pair<INDArray[], INDArray[]> p = selectColumnFromMDSData(arrays, masks, inOutIdx);\n        return merge4d(p.getFirst(), p.getSecond());","sourceCodeStart":528,"sourceCodeEnd":564,"githubUrl":"https://github.com/deeplearning4j/deeplearning4j/blob/4c22ac5fe4a8350d05d224e7f4499429f7f69c93/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/linalg/dataset/api/DataSetUtil.java#L528-L564","documentation":"When merging time series that need masks (different lengths or explicit masks present), DataSetUtil.mergeTimeSeries supports only rank-2 (per-example, per-time-step) and rank-3 (per-output) masks. Any other mask rank reaches the final else branch and an UnsupportedOperationException 'Cannot merge time series with mask rank N' is thrown. Note rank-3 feature masks are documented as unsupported, so unusual mask construction is typically the cause.","triggerScenarios":"Calling DataSetUtil.mergeTimeSeries(arrays, masks) where a non-null mask has rank 1 or rank > 3 (e.g. a 4d mask, or a collapsed 1d per-example mask), while the merge takes the masked path (lengths differ or hasMask is true).","commonSituations":"Passing 4d CNN masks to time-series data; a mask reshaped/reshaped incorrectly upstream (e.g. squeezed to 1d); labels wrapped or expanded an extra dimension by a custom layer so the mask inherits rank 4; typo building the masks INDArray[][] so the wrong element is selected.","solutions":["Confirm each mask is rank 2 ([mb, timeSteps]) or rank 3 ([mb, numOutputs, timeSteps]); reshape it before merging.","Replace rank-1 masks with their 2d equivalent via mask.reshape(mb, timeSteps).","If the masks come from a custom layer/output, regenerate them in the standard per-time-step (2d) format used by RNN layers.","If the mask is genuinely unsupported (e.g. per-input 3d feature masks), drop it (pass null) and handle masking manually after the merge."],"exampleFix":"// before\nINDArray m = Nd4j.create(4, 20, 10, 1); // rank 4\nPair<INDArray, INDArray> p = DataSetUtil.mergeTimeSeries(new INDArray[]{ts}, new INDArray[]{m});\n// after\nINDArray m = Nd4j.create(4, 20, 10, 1);\nINDArray m2 = m.rank() == 2 || m.rank() == 3 ? m : m.reshape(m.size(0), m.size(m.rank()-1)); // -> rank 2 [mb, steps]\nPair<INDArray, INDArray> p = DataSetUtil.mergeTimeSeries(new INDArray[]{ts}, new INDArray[]{m2});","handlingStrategy":"validation","validationCode":"for (INDArray m : masks)\n    if (m != null && m.rank() != 2 && m.rank() != 3)\n        throw new IllegalArgumentException(\"Time-series mask must be rank 2 or 3, got rank \" + m.rank());","typeGuard":"boolean isValidTsMask(INDArray m) { return m == null || m.rank() == 2 || m.rank() == 3; }","tryCatchPattern":"try {\n    p = DataSetUtil.mergeTimeSeries(arrays, masks);\n} catch (UnsupportedOperationException e) {\n    if (e.getMessage().startsWith(\"Cannot merge time series with mask rank\")) {\n        masks = java.util.Arrays.stream(masks).map(m -> (m == null || m.rank() <= 3) ? m : m.reshape(m.size(0), m.size(m.rank()-1))).toArray(INDArray[]::new);\n        p = DataSetUtil.mergeTimeSeries(arrays, masks);\n    } else throw e;\n}","preventionTips":["Build time-series masks as rank 2 [mb, timeSteps] (standard) or rank 3 [mb, numOutputs, timeSteps] (per-output) only.","Never reuse CNN/4d masks for time-series merges; regenerate them for the correct rank.","Avoid squeeze/expandDims operations on masks in custom layers that change their rank.","Check for null-vs-empty mask mistakes so wrong arrays do not land in the masks slot."],"tags":["nd4j","deeplearning4j","time-series","mask","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"}