-
Notifications
You must be signed in to change notification settings - Fork 214
Adds a closeable session result #411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
IMO we should. I'm not sure what happens if you double close a tensor but I think it's worthy checking that its nothing bad, or doing something to avoid it. Using the |
I think I'd prefer that it returned |
(sorry for the delay @Craigacp) I definitely think that we need to support both In addition, if that's possible, the same structure could be used also to pass the input tensors to a function call, meaning that a user could instantiate it by passing a list of tensors (so instead of try (Tensor.mapOf(
"x", TFloat64.tensorOf(...),
"y", TFloat32.vectorOf(...),
"z", TInt32.vectorOf(...)
)) {
...
} What will happen with |
Yeah, so the separate lifetime issue is why it isn't used as a way of supplying arguments to a call. My justification is that users are in control of Tensor creation for arguments, so they can manage the lifetimes, however for the return values of |
… Session.Result to be a top level class.
I've refactored |
tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/Result.java
Show resolved
Hide resolved
* | ||
* <p>When this is closed it closes all the {@link Tensor}s inside it. If you maintain a | ||
* reference to a value after this object has been closed it will throw an {@link | ||
* IllegalStateException} upon access. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it can still be useful in some cases for a user to retain the lifetime of a specific tensor beyond the scope of the Result
, and it shouldn't be hard to do using reference counts internally.
What about adding a getAndRetain(...)
endpoint to do this? If not being added as part of this PR, I can play around it after.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than do reference counting it could have a BitSet
denoting if the tensor should be closed? It would be a one way operation, you could getAndRetain
and then the lifetime of the tensor is up to you with no way of putting it back under the Result
management.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I already use reference counting for managing sparse tensors, I think if we can stick internally to a single method for extending the life of a tensor, it would be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After some investigation we couldn't use the retainReference
idiom to extend the lifespan of the Tensor
as then the RawTensor
's PointerScope
loses track of it. So we're going to leave the getAndRetain
method as a todo for when we have a look at managing Tensor
lifespans better.
tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/Result.java
Outdated
Show resolved
Hide resolved
tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/Result.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to run spotless now @Craigacp ?
Done. |
This PR changes the output of Session.Runner.run() from
List<Tensor>
to aSession.Result
class which is autocloseable and has both string and index accessors. This is an intermediate step before we have proper resource scopes (#354) which we don't currently have time to work on.One open question is if we should change the return type of
TensorFunction.call(Map<String,Tensor>)
so it returns theResult
class instead ofMap<String,Tensor>
. If so, then it probably should be promoted into a top level class rather than a static inner class onSession
.I'll run it through the formatter after review.