This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
[MXNET-1159]Added unit tests for making Resource Scope work in Java #12955
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
scala-package/core/src/test/java/org/apache/mxnet/javaapi/ResourceScopeTestSuite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
package org.apache.mxnet.javaapi; | ||
|
||
import org.apache.mxnet.NativeResourceRef; | ||
import org.apache.mxnet.ResourceScope; | ||
import org.junit.Test; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.Callable; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class ResourceScopeTestSuite { | ||
|
||
/** | ||
* This is a placeholder class to test out whether NDArray References get collected or not when using | ||
* try-with-resources in Java. | ||
* | ||
*/ | ||
class TestNDArray { | ||
NDArray selfArray; | ||
|
||
public TestNDArray(Context context, int[] shape) { | ||
this.selfArray = NDArray.ones(context, shape); | ||
} | ||
|
||
public boolean verifyIsDisposed() { | ||
return this.selfArray.nd().isDisposed(); | ||
} | ||
|
||
public NativeResourceRef getNDArrayReference() { | ||
return this.selfArray.nd().ref(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testNDArrayAutoRelease() { | ||
TestNDArray test = null; | ||
|
||
try (ResourceScope scope = new ResourceScope()) { | ||
test = new TestNDArray(Context.cpu(), new int[]{100, 100}); | ||
} | ||
|
||
assertTrue(test.verifyIsDisposed()); | ||
} | ||
|
||
@Test | ||
public void testObjectReleaseFromList() { | ||
List<TestNDArray> list = new ArrayList<>(); | ||
|
||
try (ResourceScope scope = new ResourceScope()) { | ||
for (int i = 0;i < 10; i++) { | ||
list.add(new TestNDArray(Context.cpu(), new int[] {100, 100})); | ||
} | ||
} | ||
|
||
assertEquals(list.size() , 10); | ||
list.forEach(n -> assertTrue(n.verifyIsDisposed())); | ||
} | ||
|
||
@Test | ||
public void testObjectReleaseFromMap() { | ||
Map<String, TestNDArray> stringToNDArrayMap = new HashMap<>(); | ||
|
||
try (ResourceScope scope = new ResourceScope()) { | ||
for (int i = 0;i < 10; i++) { | ||
stringToNDArrayMap.put(String.valueOf(i),new TestNDArray(Context.cpu(), new int[] {i, i})); | ||
} | ||
} | ||
|
||
assertEquals(stringToNDArrayMap.size(), 10); | ||
stringToNDArrayMap.forEach((key, value) -> assertTrue(value.verifyIsDisposed())); | ||
|
||
Map<TestNDArray, String> ndArrayToStringMap = new HashMap<>(); | ||
|
||
try (ResourceScope scope = new ResourceScope()) { | ||
for (int i = 0;i < 10; i++) { | ||
ndArrayToStringMap.put(new TestNDArray(Context.cpu(), new int[] {i, i}), String.valueOf(i)); | ||
} | ||
} | ||
|
||
assertEquals(ndArrayToStringMap.size(), 10); | ||
ndArrayToStringMap.forEach((key, value) -> assertTrue(key.verifyIsDisposed())); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why change the version of Maven compiler plugin?
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.
It's in the PR description :
" Made Maven compiler plugin to use 1.8 in order to use Java Lambda expressions and try-with-resources in Java"
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.
But does this mean we lose support for Java7 users? Or this is just a definition for Maven
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's just for Maven compilation.
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 checking the https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html website here, this would lead to some problems for users using Java7. Here is what I have tried locally (with my JDK 1.8):
Since we have not decided to fully abandon 1.7 users, this change may risk their usages
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.
We don't have the Java APIs available publicly right now, so we do not have Java users.
I'm not sure what you are trying to indicate with the commands you pasted above.
Also, here's Oracle's end of lifecycle [public updates] roadmap link for Java : https://www.oracle.com/technetwork/java/eol-135779.html
Oracle itself has stopped supporting Java 7 starting from April 2015.
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.
In the same page you linked look at the Note in the same page, its intended to use Java8 features and throw linkage errors, we haven't used any Java8 features yet but as we keep moving with a lot more Java APIs, we should move to Java8..I don't think it breaks for Java7 users, although worth checking..
I cannot find what the behavior of this setting is for version 3.3.0 of the plugin on their doc, may should check their github for [3.3 tag] (https://github.com/apache/maven-compiler-plugin/tree/maven-compiler-plugin-3.3/)
After verifying if it indeed breaks 1.7, I suggest to send out a email on the dev@ seek more opinions and revert if necessary.
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.
@piyushghai as discussed offline, could you please revert just this change and bring back only when required - I understand it doesn't impact but to be sure. You can start a discussion on dev@ about moving to Java8 and dropping support, many frameworks have moved to support Java8+
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.
Okay sure, Will do it.
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.
Maybe I'm overlooking it but I don't see anything that we need Java 8 for. Try with resources was introduced in Java 7. The three lambda's that I see could easily be for loops.
I've got no issue with the idea of us moving to 8 and starting that discussion on the dev list. In the meantime I think we could move to 7 and rewrite the few lambdas.