Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ Pointer getUnsafeNativeHandle() {
return operation.getUnsafeNativeHandle(index);
}

/**
* Returns whether the underlying operation has no valid handle. Makes the opposite check as
* GraphOperation.requireHandle *
*/
public boolean isClosed() {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this behave correctly on an EagerSession? Also it needs some javadoc and I think it'll fail the spotless check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the formatting problem.

I tried the following small example with EagerSession, and it appears to reply correctly false:

import org.tensorflow.EagerSession;
import org.tensorflow.op.Ops;
import org.tensorflow.framework.initializers.Zeros;
import org.tensorflow.types.TFloat32;

public class App {
    public static void main(String[] args) {
        try (EagerSession s = EagerSession.create()) {
            Ops tf = Ops.create(s);
            Zeros<TFloat32> zeroInit = new Zeros<>();
            // y = a*x + b
            var x = tf.constant(new float[] {1.0f, 3.0f, 4.0f});
            var y = tf.constant(new float[] {2.5f, 6.5f, 8.5f});
            var b = tf.constant(1.0f);
            var a = tf.constant(1.8f);
            var ypred = tf.math.add(tf.math.mul(a, x), tf.stopGradient(b));
            var loss_gen = new org.tensorflow.framework.losses.MeanSquaredError();
            var los = loss_gen.call(tf, ypred, y);
            System.out.println("IsClosed " + los.asOutput().isClosed());
        }
    }
}

Running it, the output is:
2023-01-06 16:32:31.701434: I external/org_tensorflow/tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
IsClosed false

So it appears to work correctly.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Can you add the javadoc to the method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have just added the javadoc.

Pointer handle = operation.getUnsafeNativeHandle(index);
return handle == null || handle.isNull();
}

private final AbstractOperation operation;
private final int index;
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ public Op applyGradients(List<GradAndVar<? extends TType>> gradsAndVars, String
List<Op> updateOps = new ArrayList<>();
prepOp.ifPresent(updateOps::add);
for (GradAndVar<? extends TType> pair : gradsAndVars) {
updateOps.add(applyDense(pair));
if (!pair.gradient.isClosed()) {
updateOps.add(applyDense(pair));
}
}

return finish(updateOps, name);
Expand Down