Skip to content

Commit

Permalink
[api] Allow show NDArray content in debugger (#2078)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfliu authored Oct 12, 2022
1 parent 2896f40 commit 56b24a5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
18 changes: 15 additions & 3 deletions api/src/main/java/ai/djl/ndarray/NDArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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} */
Expand Down
30 changes: 26 additions & 4 deletions api/src/main/java/ai/djl/ndarray/internal/NDFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -60,7 +82,7 @@ public static String format(
if (array.hasGradient()) {
sb.append(" hasGradient");
}
if (DEBUG) {
if (!withContent) {
return sb.toString();
}

Expand Down
3 changes: 2 additions & 1 deletion docs/development/development_guideline.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 56b24a5

Please sign in to comment.