Configuring GPU-Backend in Maven Dependencies

Hi guys, I’m a complete beginner and would love to get help where I am:
Currently I’m working with the DL4J-Examples and I wanted to run one neural network on my CPU and GPU as a backend. At the moment, it is running on my cpu

<nd4j.backend>nd4j-native</nd4j.backend>

org.nd4j
${nd4j.backend}
${dl4j-master.version}

and it works. But then i followed this guide (https://subscription.packtpub.com/book/data/9781788995207/1/ch01lvl1sec08/configuring-dl4j-for-a-gpu-accelerated-environment)
and at step 5 already, when I put another dependency in my POM.xl, it’s written in red and it says “Dependency ‘org.nd4j:nd4j-cuda-10.0:1.0.0-beta3’ not found” (I installed CUDA 10.0).
So maven doesn’t seem to find the artifactids and versions… What am I missing?

@blablab1 that content is over a year old. Please don’t follow anything from packt. It’s not officially done by the authors and goes out of date quickly. You can configure using this guide:
https://deeplearning4j.konduit.ai/config/backends

Make sure the cuda version matches the installation on your computer.

One thing I want to make sure of. What’s your GPU? Sometimes people getting started have gpus that aren’t really that useful for doing DL.

Tbh that was my first source of information, but it didn’t help me as well, because IntelliJ can’t find the dependencies. :frowning:
I installed Cuda 11.0 and 10.0, neither does work.
And my GPU is GeForce GTX 1050 Ti.

@blablab1 thanks for clarifying. Yeah just make sure not to use the older versions they use in the books :slight_smile: - a 1050 is enough to run some basic things, but don’t expect a lot. For cuda 11, I would recommend snapshots.

See:

https://deeplearning4j.konduit.ai/config/config-snapshots
We have cuda 11 and 11.2 snapshots up for windows:
https://oss.sonatype.org/content/repositories/snapshots/org/nd4j/nd4j-cuda-11.0/1.0.0-SNAPSHOT/

Now for your intellij issue, that’s generally due to needing to refresh the maven state. You often have to reload the project in order for it to see anything. See:

OMG. The solution was really to restart the program. :grinning:

Now the first part of my POM.xl looks like this:

<properties>
    <dl4j-master.version>1.0.0-beta7</dl4j-master.version>
    <!-- Change the nd4j.backend property to nd4j-cuda-X-platform to use CUDA GPUs -->
    <!-- <nd4j.backend>nd4j-cuda-10.2-platform</nd4j.backend> -->
    <nd4j.backend>nd4j-cuda-10.0-platform</nd4j.backend>
    <java.version>1.8</java.version>
    <maven-compiler-plugin.version>3.6.1</maven-compiler-plugin.version>
    <maven.minimum.version>3.3.1</maven.minimum.version>
    <exec-maven-plugin.version>1.4.0</exec-maven-plugin.version>
    <maven-shade-plugin.version>2.4.3</maven-shade-plugin.version>
    <jcommon.version>1.0.23</jcommon.version>
    <jfreechart.version>1.0.13</jfreechart.version>
    <logback.version>1.1.7</logback.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.29</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-common</artifactId>
            <version>4.1.48.Final</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.nd4j</groupId>
        <artifactId>nd4j-cuda-10.0</artifactId>
        <version>1.0.0-beta5</version>
    </dependency>

There’s no Errors now, BUT when i run a neural network it still says:
o.n.l.f.Nd4jBackend - Loaded [CpuBackend] backend
o.n.n.NativeOpsHolder - Number of threads used for linear algebra: 2
o.n.n.Nd4jBlas - Number of threads used for OpenMP BLAS: 2
o.n.l.a.o.e.DefaultOpExecutioner - Backend used: [CPU]; OS: [Windows 10]

Looks like my GPU still isn’t being used. What could it be now?

Thanks for your help btw.

You’re mixing dl4j versions. That’s also something you can’t do. Beyond that, you’re not showing me your whole pom. Based on the fact it’s falling back to cpu, that means you have other dependencies on your classpath. Please make sure you use a clean pom and don’t just mix and match random versions via copy and paste. That’s what creates these rabbit holes to begin with.

Could you please show me your whole pom and the whole stack trace? I would need to see the error that caused it to fallback to cpu.

Sorry for that delayed answer, I was trying to implement the Snapshot but it kinda destroyed my DL4J-Examples-Project, so I had to reimport it and it took a while…
Then I changed the 1.0.0-beta5 to 1.0.0-beta7 and RESTARTET it since you said that i mixed up the versions and… you were right.
Much love to you for helping me out.

I got one more question tho, but I’m not sure if I should post a new thread.

I wanted to ask, if you or possibly another one knows any benchmarking framework that is good for measuring performances (like inference time, latency) of neural networks (especially measuring the difference of how it reacts on the CPU/GPU) and is compatible with DL4J, because I only found out about Listeners from DL4J, which provide me PerformanceListeners to get the iteration time.

Thanks a lot again! :heart: :heart: :heart:

@blablab1 could you tell me what you ran in to exactly? Issues like that are generally related to what I just told you before. Your pom.xml is still probably mixed up.
Your best bet is going to be JMH. https://mkyong.com/java/java-jmh-benchmark-tutorial/

We have a listener here for that: https://github.com/eclipse/deeplearning4j/blob/4766032444de8e0c2c3389270576bb6e7c466211/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/optimize/listeners/PerformanceListener.java#L44

No since I reimported it, my POM.xml “resettet”.

<?xml version="1.0" encoding="UTF-8"?>


4.0.0

<groupId>org.deeplearning4j</groupId>
<artifactId>dl4j-examples</artifactId>
<version>1.0.0-beta7</version>
<name>Introduction to DL4J</name>
<description>A set of examples introducing the DL4J framework</description>

<properties>
    <dl4j-master.version>1.0.0-beta7</dl4j-master.version>
    <!-- Change the nd4j.backend property to nd4j-cuda-X-platform to use CUDA GPUs -->
    <!-- <nd4j.backend>nd4j-cuda-10.2-platform</nd4j.backend> -->
    <nd4j.backend>nd4j-cuda-10.0-platform</nd4j.backend>
    <java.version>1.8</java.version>
    <maven-compiler-plugin.version>3.6.1</maven-compiler-plugin.version>
    <maven.minimum.version>3.3.1</maven.minimum.version>
    <exec-maven-plugin.version>1.4.0</exec-maven-plugin.version>
    <maven-shade-plugin.version>2.4.3</maven-shade-plugin.version>
    <jcommon.version>1.0.23</jcommon.version>
    <jfreechart.version>1.0.13</jfreechart.version>
    <logback.version>1.1.7</logback.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.29</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-common</artifactId>
            <version>4.1.48.Final</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.nd4j</groupId>
        <artifactId>nd4j-cuda-10.0</artifactId>
        <version>1.0.0-beta7</version>
    </dependency>
    <dependency>
        <groupId>org.datavec</groupId>
        <artifactId>datavec-api</artifactId>
        <version>${dl4j-master.version}</version>
    </dependency>
    <dependency>
        <groupId>org.datavec</groupId>
        <artifactId>datavec-data-image</artifactId>
        <version>${dl4j-master.version}</version>
    </dependency>
    <dependency>
        <groupId>org.datavec</groupId>
        <artifactId>datavec-local</artifactId>
        <version>${dl4j-master.version}</version>
    </dependency>
    <dependency>
        <groupId>org.deeplearning4j</groupId>
        <artifactId>deeplearning4j-datasets</artifactId>
        <version>${dl4j-master.version}</version>
    </dependency>
    <dependency>
        <groupId>org.deeplearning4j</groupId>
        <artifactId>deeplearning4j-core</artifactId>
        <version>${dl4j-master.version}</version>
    </dependency>
    <dependency>
        <groupId>org.deeplearning4j</groupId>
        <artifactId>deeplearning4j-ui</artifactId>
        <version>${dl4j-master.version}</version>
    </dependency>
    <dependency>
        <groupId>org.deeplearning4j</groupId>
        <artifactId>deeplearning4j-zoo</artifactId>
        <version>${dl4j-master.version}</version>
    </dependency>
    <!-- ParallelWrapper & ParallelInference live here -->
    <dependency>
        <groupId>org.deeplearning4j</groupId>
        <artifactId>deeplearning4j-parallel-wrapper</artifactId>
        <version>${dl4j-master.version}</version>
    </dependency>
    <!-- Used in the feedforward/classification/MLP* and feedforward/regression/RegressionMathFunctions example -->
    <dependency>
        <groupId>jfree</groupId>
        <artifactId>jfreechart</artifactId>
        <version>${jfreechart.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jfree</groupId>
        <artifactId>jcommon</artifactId>
        <version>${jcommon.version}</version>
    </dependency>
    <!-- Used for downloading data in some of the examples -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.5</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
    </dependency>
    <dependency>
        <groupId>org.datavec</groupId>
        <artifactId>datavec-data-codec</artifactId>
        <version>${dl4j-master.version}</version>
    </dependency>
    <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacv-platform</artifactId>
        <version>1.5.2</version>
    </dependency>
</dependencies>
<!-- Maven Enforcer: Ensures user has an up to date version of Maven before building -->
<build>
    <plugins>
        <plugin>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>1.0.1</version>
            <executions>
                <execution>
                    <id>enforce-default</id>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <requireMavenVersion>
                                <version>[${maven.minimum.version},)</version>
                                <message>********** Minimum Maven Version is ${maven.minimum.version}. Please upgrade Maven before continuing (run "mvn --version" to check). **********</message>
                            </requireMavenVersion>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.lewisd</groupId>
            <artifactId>lint-maven-plugin</artifactId>
            <version>0.0.11</version>
            <configuration>
                <failOnViolation>true</failOnViolation>
                <onlyRunRules>
                    <rule>DuplicateDep</rule>
                    <rule>RedundantPluginVersion</rule>
                    <!-- Rules incompatible with Java 9
                    <rule>VersionProp</rule>
                    <rule>DotVersionProperty</rule> -->
                </onlyRunRules>
            </configuration>
            <executions>
                <execution>
                    <id>pom-lint</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>check</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>${exec-maven-plugin.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>java</executable>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>${maven-shade-plugin.version}</version>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <shadedClassifierName>${shadedClassifier}</shadedClassifierName>
                <createDependencyReducedPom>true</createDependencyReducedPom>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>org/datanucleus/**</exclude>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>reference.conf</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>com.lewisd</groupId>
                                    <artifactId>lint-maven-plugin</artifactId>
                                    <versionRange>[0.0.11,)</versionRange>
                                    <goals>
                                        <goal>check</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore/>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

I was changing up versions, as I said, and then I couldn’t build an example network anymore, because other classes had internal issues and couldn’t be run.

One more question to JMH: I followed this guide (JMH - Java Microbenchmark Harness) and have opened a new project with that class MyBenchmark which looks like this:
package com.jenkov;

import org.openjdk.jmh.annotations.Benchmark;

public class MyBenchmark {

@Benchmark
public void testMethod() {
    // This is a demo/sample template for building your JMH benchmarks. Edit as needed.
    // Put your benchmark code here.
}

}

Now as far as i understood, I have to put my neural network code in there, run “mvn clean install” in the terminal and run “java -jar target/benchmarks.jar” there, BUT how do I get to combine the pom.xlm of my dl4j-examples with the pom.xml of my new project (MyBenchmark)? Is this method even correct?

Thanks alot

@blablab1 JFYI 1.0.0-M1 is out if you want to try the benchmarks again. Sorry it took a bit, but things should be a lot better now.

I join this thread cause I’m interested in that too. I cannot make cuDNN work.

@blablab1 I am configured pom.xml to run CUDA so you may ask me about it. I can show you pom.xml and how to config it. But not cuDNN cause i don’t how to make it work.