Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[MXNET-1159]Added unit tests for making Resource Scope work in Java #12955

Merged
merged 1 commit into from
Oct 24, 2018
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
@@ -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()));

}
}
4 changes: 2 additions & 2 deletions scala-package/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.8</source>
Copy link
Member

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?

Copy link
Contributor Author

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"

Copy link
Member

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"

But does this mean we lose support for Java7 users? Or this is just a definition for Maven

Copy link
Contributor Author

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.

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"

But does this mean we lose support for Java7 users? Or this is just a definition for Maven

Copy link
Member

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):

8c85904324fa:incubator-mxnet qingla$ javac -source 1.9
javac: invalid source release: 1.9
Usage: javac <options> <source files>
use -help for a list of possible options
8c85904324fa:incubator-mxnet qingla$ javac -source 1.8
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options
8c85904324fa:incubator-mxnet qingla$ javac -source 1.7
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options

Since we have not decided to fully abandon 1.7 users, this change may risk their usages

Copy link
Contributor Author

@piyushghai piyushghai Oct 24, 2018

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):

8c85904324fa:incubator-mxnet qingla$ javac -source 1.9
javac: invalid source release: 1.9
Usage: javac <options> <source files>
use -help for a list of possible options
8c85904324fa:incubator-mxnet qingla$ javac -source 1.8
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options
8c85904324fa:incubator-mxnet qingla$ javac -source 1.7
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options

Since we have not decided to fully abandon 1.7 users, this change may risk their usages

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.

Copy link
Member

@nswamy nswamy Oct 24, 2018

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/)

Note: Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. The pitfall is unintended usage of APIs that only exist in later JREs which would make your code fail at runtime with a linkage error.

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.

Copy link
Member

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+

Copy link
Contributor Author

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.

Copy link
Contributor

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.

<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
Expand Down