tensorflow / tensorflow/java

NdArray implementations don't throw IllegalRankException as documented if no coordinates provided

Open
#224 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
928
Forks
227
PR merge metrics
No merged PRs in 30d

Description

Instead, when no coordinates are provided, we return the scalar at (0, 0, ...). Is this an important idiom that we are preserving, or a questionable choice, or simply a bug?

Here's an example of an abstract method of FloatNdArray that is documented to throw IllegalRankException:

 /**
   * Returns the float value of the scalar found at the given coordinates.
   *
   * . . .
   *
   * @param coordinates coordinates of the scalar to resolve
   * @return value of that scalar
   * @throws IndexOutOfBoundsException if some coordinates are outside the limits of their respective dimension
   * @throws IllegalRankException if number of coordinates is not sufficient to access a scalar element
   */
  float getFloat(long... coordinates);

Here's the implementation in FloatDenseNdArray:

  @Override
  public float getFloat(long... indices) {
    return buffer.getFloat(positionOf(indices, true));
  }

But if indices.length == 0, then positionOf does not throw the claimed IllegalRankException:

  long positionOf(long[] coords, boolean isValue) {
    if (coords == null || coords.length == 0) {
      return 0;
    }
    Validator.coordinates(dimensions, coords, isValue);
    return dimensions.positionOf(coords);
  }

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the documented FloatNdArray.getFloat contract and compare it with FloatDenseNdArray.getFloat and positionOf, especially the zero-coordinate branch. Determine whether returning the scalar at position 0 is intended or violates the documented exception behavior; done means the behavior and its documentation agree, with relevant coverage added or updated if the project provides it.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.