You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2021/08/31 16:35:43 UTC

[GitHub] [incubator-mxnet] lanking520 commented on a change in pull request #20461: Java2.0 proto

lanking520 commented on a change in pull request #20461:
URL: https://github.com/apache/incubator-mxnet/pull/20461#discussion_r699481157



##########
File path: java-package/integration/src/main/java/org/apache/mxnet/integration/tests/engine/ModelTest.java
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.integration.tests.engine;
+
+import java.io.IOException;
+import org.apache.mxnet.engine.BaseMxResource;
+import org.apache.mxnet.engine.Model;
+import org.apache.mxnet.engine.MxResource;
+import org.apache.mxnet.engine.Predictor;
+import org.apache.mxnet.integration.tests.jna.JnaUtilTest;
+import org.apache.mxnet.integration.util.Assertions;
+import org.apache.mxnet.ndarray.NDArray;
+import org.apache.mxnet.ndarray.NDList;
+import org.apache.mxnet.ndarray.types.Shape;
+import org.apache.mxnet.repository.Item;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.annotations.Test;
+
+public class ModelTest {
+    private static final Logger logger = LoggerFactory.getLogger(JnaUtilTest.class);
+
+    @Test
+    public void modelLoadAndPredictTest() {
+        try (MxResource base = BaseMxResource.getSystemMxResource()) {
+            Model model = Model.loadModel(Item.MLP);
+            //            Model model = Model.loadModel("test",

Review comment:
       remove comments

##########
File path: docker/dev/docker_run.sh
##########
@@ -0,0 +1,44 @@
+#

Review comment:
       put to Java-package folder

##########
File path: java-package/native/src/main/resources/META-INF/backport.txt
##########
@@ -0,0 +1,2 @@
+The file is based off Apache MXNet 1.7 branch (https://github.com/apache/incubator-mxnet/commit/7a84fe11dd7399d1c2bdd0fb679c46af69e48861)

Review comment:
       check what does this line do

##########
File path: src/initialize.cc
##########
@@ -373,7 +373,18 @@ std::shared_ptr<void(int)> HANDLER_NAME(                             \
       }                                                              \
     }                                                                \
   }),                                                                \
-  [](auto f) { signal(SIGNAL, f); });
+  [](auto f) {                                                       \

Review comment:
       add a comment above and tell why you make this change

##########
File path: java-package/mxnet-engine/src/main/java/org/apache/mxnet/engine/GradReq.java
##########
@@ -0,0 +1,51 @@
+/*
+ * 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.engine;
+
+/** An enum that indicates whether gradient is required. */
+public enum GradReq {

Review comment:
       Check the usage of this class. Not sure this is being used

##########
File path: java-package/mxnet-engine/src/main/java/org/apache/mxnet/ndarray/types/SparseNDArray.java
##########
@@ -0,0 +1,33 @@
+/*
+ * 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.ndarray.types;
+
+import com.sun.jna.Pointer;
+import org.apache.mxnet.ndarray.NDArray;
+
+/**
+ * An interface representing a Sparse NDArray.
+ *
+ * @see SparseFormat
+ * @see <a href="https://software.intel.com/en-us/node/471374">Sparse Matrix Storage Formats</a>
+ */
+public class SparseNDArray extends NDArray {

Review comment:
       Consider remove

##########
File path: java-package/mxnet-engine/src/main/java/org/apache/mxnet/ndarray/types/SparseFormat.java
##########
@@ -0,0 +1,78 @@
+/*
+ * 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.ndarray.types;
+
+/**
+ * An enum representing Sparse matrix storage formats.
+ *
+ * <ul>
+ *   <li>DENSE: Stride format
+ *   <li>ROW_SPARSE: Row Sparse
+ *   <li>CSR: Compressed Sparse Row
+ * </ul>
+ *
+ * @see <a href="https://software.intel.com/en-us/node/471374">Sparse Matrix Storage Formats</a>
+ */
+public enum SparseFormat {
+    // the dense format is accelerated by MKLDNN by default
+    DENSE("default", 0),
+    ROW_SPARSE("row_sparse", 1),
+    CSR("csr", 2),
+    COO("coo", 3);

Review comment:
       We don't use COO

##########
File path: java-package/mxnet-engine/src/main/java/org/apache/mxnet/nn/Parameter.java
##########
@@ -0,0 +1,327 @@
+/*
+ * 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.nn;
+
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.util.Objects;
+import java.util.UUID;
+import org.apache.mxnet.engine.Device;
+import org.apache.mxnet.engine.MxResource;
+import org.apache.mxnet.exception.MalformedModelException;
+import org.apache.mxnet.ndarray.NDArray;
+import org.apache.mxnet.ndarray.NDSerializer;
+import org.apache.mxnet.ndarray.types.DataType;
+import org.apache.mxnet.ndarray.types.Shape;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * {@code Parameter} is a container class that holds a learnable parameter of a model.
+ *
+ * <p>Every {@code Parameter} is associated with a {@link SymbolBlock}. The output of the block's
+ * forward function depends on the values in the {@code Parameter}. During training, the values in
+ * the {@code Parameter} are updated to reflect the training data. This process forms the crux of
+ * learning.
+ *
+ * @see <a href="https://d2l.djl.ai/chapter_deep-learning-computation/parameters.html">The D2L
+ *     chapter on parameter management</a>
+ */
+public class Parameter extends MxResource {
+    private static final Logger logger = LoggerFactory.getLogger(Parameter.class);
+
+    private static final byte VERSION = 1;
+
+    private String id;
+    private String name;
+    private Shape shape;
+    private Type type;
+    private NDArray array;
+    private boolean requiresGrad;
+
+    Parameter(Builder builder) {
+        this.id = UUID.randomUUID().toString();
+        this.name = builder.name;
+        this.shape = builder.shape;
+        this.type = builder.type;
+        this.array = builder.array;
+        this.requiresGrad = builder.requiresGrad;
+    }
+
+    /**
+     * Gets the ID of this {@code Parameter}.
+     *
+     * @return the ID of this {@code Parameter}
+     */
+    public String getId() {
+        return id;
+    }
+
+    /**
+     * Gets the name of this {@code Parameter}.
+     *
+     * @return the name of this {@code Parameter}
+     */
+    public String getName() {
+        return name == null ? "" : name;
+    }
+
+    /**
+     * Gets the type of this {@code Parameter}.
+     *
+     * @return the type of this {@code Parameter}
+     */
+    public Type getType() {
+        return type;
+    }
+
+    /**
+     * Sets the values of this {@code Parameter}.
+     *
+     * @param array the {@link NDArray} that contains values of this {@code Parameter}
+     */
+    public void setArray(NDArray array) {
+        if (shape != null) {
+            throw new IllegalStateException("array has been set! Use either setArray or setShape");
+        }
+        this.array = array;
+        shape = array.getShape();
+        array.setName(name);
+    }
+
+    /**
+     * Sets the shape of this {@code Parameter}.
+     *
+     * @param shape the shape of this {@code Parameter}
+     */
+    public void setShape(Shape shape) {
+        if (array != null) {
+            throw new IllegalStateException("array has been set! Use either setArray or setShape");
+        }
+        this.shape = shape;
+    }
+
+    /**
+     * Gets the values of this {@code Parameter} as an {@link NDArray}.
+     *
+     * @return an {@link NDArray} that contains values of this {@code Parameter}
+     */
+    public NDArray getArray() {
+        if (!isInitialized()) {
+            throw new IllegalStateException("The array has not been initialized");
+        }
+        return array;
+    }
+
+    /**
+     * Returns whether this parameter needs gradients to be computed.
+     *
+     * @return whether this parameter needs gradients to be computed
+     */
+    public boolean requiresGradient() {
+        return requiresGrad;

Review comment:
       remove this

##########
File path: java-package/integration/src/main/java/org/apache/mxnet/integration/tests/jna/JnaUtilTest.java
##########
@@ -0,0 +1,176 @@
+/*
+ * 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.integration.tests.jna;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.mxnet.engine.BaseMxResource;
+import org.apache.mxnet.engine.Device;
+import org.apache.mxnet.engine.MxResource;
+import org.apache.mxnet.engine.Symbol;
+import org.apache.mxnet.jna.JnaUtils;
+import org.apache.mxnet.ndarray.NDArray;
+import org.apache.mxnet.ndarray.NDList;
+import org.apache.mxnet.ndarray.types.Shape;
+import org.apache.mxnet.nn.Parameter;
+import org.apache.mxnet.nn.SymbolBlock;
+import org.apache.mxnet.repository.Item;
+import org.apache.mxnet.repository.Repository;
+import org.apache.mxnet.util.PairList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class JnaUtilTest {
+
+    private static final Logger logger = LoggerFactory.getLogger(JnaUtilTest.class);
+
+    @Test
+    public void doForwardTest() throws IOException {
+        try (MxResource base = BaseMxResource.getSystemMxResource()) {
+            Path modelPath = Repository.initRepository(Item.MLP);
+            Path symbolPath = modelPath.resolve("mlp-symbol.json");
+            Path paramsPath = modelPath.resolve("mlp-0000.params");
+            Symbol symbol = Symbol.loadSymbol(base, symbolPath);
+            SymbolBlock block = new SymbolBlock(base, symbol);
+            Device device = Device.defaultIfNull();
+            NDList mxNDArray = JnaUtils.loadNdArray(base, paramsPath, Device.defaultIfNull(null));
+
+            // load parameters
+            List<Parameter> parameters = block.getAllParameters();
+            Map<String, Parameter> map = new ConcurrentHashMap<>();
+            parameters.forEach(p -> map.put(p.getName(), p));
+
+            for (NDArray nd : mxNDArray) {
+                String key = nd.getName();
+                if (key == null) {
+                    throw new IllegalArgumentException(
+                            "Array names must be present in parameter file");
+                }
+
+                String paramName = key.split(":", 2)[1];
+                Parameter parameter = map.remove(paramName);
+                parameter.setArray(nd);
+            }
+            block.setInputNames(new ArrayList<>(map.keySet()));
+
+            NDArray arr = NDArray.create(base, new Shape(1, 28, 28), device).ones();
+            block.forward(new NDList(arr), new PairList<>(), device);
+            logger.info(
+                    "Number of MxResource managed by baseMxResource: {}",
+                    BaseMxResource.getSystemMxResource().getSubResource().size());
+        } catch (IOException e) {
+            logger.error(e.getMessage(), e);
+            throw e;
+        }
+        Assert.assertEquals(BaseMxResource.getSystemMxResource().getSubResource().size(), 0);
+    }
+
+    @Test
+    public void createNdArray() {
+        try {
+            try (BaseMxResource base = BaseMxResource.getSystemMxResource()) {
+                int[] originIntegerArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
+                float[] originFloatArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
+                double[] originDoubleArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
+                long[] originLongArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
+                boolean[] originBooleanArray = {
+                    true, false, false, true, true, true, true, false, false, true, true, true
+                };
+                byte[] originByteArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
+                NDArray intArray = NDArray.create(base, originIntegerArray, new Shape(3, 4));
+                NDArray floatArray = NDArray.create(base, originFloatArray, new Shape(3, 4));
+                NDArray doubleArray = NDArray.create(base, originDoubleArray, new Shape(3, 4));
+                NDArray longArray = NDArray.create(base, originLongArray, new Shape(3, 4));
+                NDArray booleanArray = NDArray.create(base, originBooleanArray, new Shape(3, 4));
+                NDArray byteArray = NDArray.create(base, originByteArray, new Shape(3, 4));
+                NDArray intArray2 = NDArray.create(base, originIntegerArray);
+                NDArray floatArray2 = NDArray.create(base, originFloatArray);
+                NDArray doubleArray2 = NDArray.create(base, originDoubleArray);
+                NDArray longArray2 = NDArray.create(base, originLongArray);
+                NDArray booleanArray2 = NDArray.create(base, originBooleanArray);
+                NDArray byteArray2 = NDArray.create(base, originByteArray);
+
+                int[] ndArrayInt = intArray.toIntArray();
+                Assert.assertEquals(originIntegerArray, ndArrayInt);
+                // Float -> Double
+                float[] floats = floatArray.toFloatArray();
+                Assert.assertEquals(originFloatArray, floats);
+                double[] ndArrayDouble = doubleArray.toDoubleArray();
+                Assert.assertEquals(originDoubleArray, ndArrayDouble);
+                long[] ndArrayLong = longArray.toLongArray();
+                Assert.assertEquals(originLongArray, ndArrayLong);
+                boolean[] ndArrayBoolean = booleanArray.toBooleanArray();
+                Assert.assertEquals(originBooleanArray, ndArrayBoolean);
+                byte[] ndArrayByte = byteArray.toByteArray();
+                Assert.assertEquals(originByteArray, ndArrayByte);
+
+                int[] ndArrayInt2 = intArray2.toIntArray();
+                Assert.assertEquals(originIntegerArray, ndArrayInt2);
+
+                // Float -> Double
+                float[] floats2 = floatArray2.toFloatArray();
+                Assert.assertEquals(originFloatArray, floats2);
+                double[] ndArrayDouble2 = doubleArray2.toDoubleArray();
+                Assert.assertEquals(originDoubleArray, ndArrayDouble2);
+                long[] ndArrayLong2 = longArray2.toLongArray();
+                Assert.assertEquals(originLongArray, ndArrayLong2);
+                boolean[] ndArrayBoolean2 = booleanArray2.toBooleanArray();
+                Assert.assertEquals(originBooleanArray, ndArrayBoolean2);
+                byte[] ndArrayByte2 = byteArray2.toByteArray();
+                Assert.assertEquals(originByteArray, ndArrayByte2);
+            } catch (ClassCastException e) {
+                logger.error(e.getMessage());
+                throw e;
+            }
+            BaseMxResource base = BaseMxResource.getSystemMxResource();
+            Assert.assertEquals(base.getSubResource().size(), 0);
+        } catch (ClassCastException e) {
+            logger.error(e.getMessage());
+            throw e;
+        }
+    }
+
+    //    @Test

Review comment:
       fix 139 error in the C code and uncomment this line

##########
File path: ci/docker/runtime_functions.sh
##########
@@ -1404,6 +1429,21 @@ test_artifact_repository() {
     popd
 }
 
+integration_test() {
+    # install gradle
+    add-apt-repository ppa:cwchien/gradle
+    apt-get update -y
+    apt-get install gradle -y

Review comment:
       remove this line

##########
File path: java-package/tools/gradle/cpp-formatter.gradle
##########
@@ -0,0 +1,86 @@
+apply plugin: CppFormatterPlugin

Review comment:
       Not necessary to keep




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@mxnet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org