diff --git a/api/src/main/java/ai/djl/ndarray/NDArray.java b/api/src/main/java/ai/djl/ndarray/NDArray.java index 74f8b64fb47..f8d1427c8a9 100644 --- a/api/src/main/java/ai/djl/ndarray/NDArray.java +++ b/api/src/main/java/ai/djl/ndarray/NDArray.java @@ -4582,7 +4582,17 @@ default NDArray countNonzero(int axis) { * @return the debug string representation of this {@code NDArray} */ default String toDebugString() { - return toDebugString(100, 10, 10, 20); + return NDFormat.format(this, 100, 10, 10, 20); + } + + /** + * Runs the debug string representation of this {@code NDArray}. + * + * @param withContent true to show the content of NDArray + * @return the debug string representation of this {@code NDArray} + */ + default String toDebugString(boolean withContent) { + return toDebugString(1000, 10, 10, 20, withContent); } /** @@ -4592,10 +4602,12 @@ default String toDebugString() { * @param maxDepth the maximum depth to print out * @param maxRows the maximum rows to print out * @param maxColumns the maximum columns to print out + * @param withContent true to show the content of NDArray * @return the debug string representation of this {@code NDArray} */ - default String toDebugString(int maxSize, int maxDepth, int maxRows, int maxColumns) { - return NDFormat.format(this, maxSize, maxDepth, maxRows, maxColumns); + default String toDebugString( + int maxSize, int maxDepth, int maxRows, int maxColumns, boolean withContent) { + return NDFormat.format(this, maxSize, maxDepth, maxRows, maxColumns, withContent); } /** {@inheritDoc} */ diff --git a/api/src/main/java/ai/djl/ndarray/internal/NDFormat.java b/api/src/main/java/ai/djl/ndarray/internal/NDFormat.java index 40a2ad62181..c60998c13bf 100644 --- a/api/src/main/java/ai/djl/ndarray/internal/NDFormat.java +++ b/api/src/main/java/ai/djl/ndarray/internal/NDFormat.java @@ -29,9 +29,10 @@ public abstract class NDFormat { private static final int PRECISION = 8; private static final String LF = System.getProperty("line.separator"); private static final Pattern PATTERN = Pattern.compile("\\s*\\d\\.(\\d*?)0*e[+-](\\d+)"); - private static final boolean DEBUG = - ManagementFactory.getRuntimeMXBean().getInputArguments().stream() - .anyMatch(arg -> arg.startsWith("-agentlib:jdwp")); + private static final boolean DEBUGGER = + !Boolean.getBoolean("jshell") + && ManagementFactory.getRuntimeMXBean().getInputArguments().stream() + .anyMatch(arg -> arg.startsWith("-agentlib:jdwp")); /** * Formats the contents of an array as a pretty printable string. @@ -45,6 +46,27 @@ public abstract class NDFormat { */ public static String format( NDArray array, int maxSize, int maxDepth, int maxRows, int maxColumns) { + return format(array, maxSize, maxDepth, maxRows, maxColumns, !DEBUGGER); + } + + /** + * Formats the contents of an array as a pretty printable string. + * + * @param array the array to print + * @param maxSize the maximum elements to print out + * @param maxDepth the maximum depth to print out + * @param maxRows the maximum rows to print out + * @param maxColumns the maximum columns to print out + * @param withContent true to show the content of NDArray + * @return the string representation of the array + */ + public static String format( + NDArray array, + int maxSize, + int maxDepth, + int maxRows, + int maxColumns, + boolean withContent) { StringBuilder sb = new StringBuilder(1000); String name = array.getName(); if (name != null) { @@ -60,7 +82,7 @@ public static String format( if (array.hasGradient()) { sb.append(" hasGradient"); } - if (DEBUG) { + if (!withContent) { return sb.toString(); } diff --git a/docs/development/development_guideline.md b/docs/development/development_guideline.md index 4c7afebaa6c..1b043de3b50 100644 --- a/docs/development/development_guideline.md +++ b/docs/development/development_guideline.md @@ -159,7 +159,8 @@ You can create your own NDArray renderer as follows: Please make sure to: - Check the "On-demand" option, which causes IntelliJ to only render the NDArray when you click on the variable. -- Change the "Use following expression" field to something like [toDebugString(100, 10, 10, 20)](https://javadoc.io/static/ai.djl/api/0.19.0/ai/djl/ndarray/NDArray.html#toDebugString-int-int-int-int-) +- Change the "Use following expression" field to `toDebugString(true)` to show NDArray content +- Change the "Use following expression" field to something like `toDebugString(1000, 10, 10, 20, true)` if you want to adjust the range of NDArray's debug output. ## Common Problems