You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by lx...@apache.org on 2017/07/07 15:58:27 UTC

[14/50] [abbrv] incubator-mxnet-test git commit: [R] add R test into jenkins; add rpkgtest into makefile (#6733)

[R] add R test into jenkins; add rpkgtest into makefile (#6733)

* simpify the makefile;add R test into jenkins;some basic test for R

* fix GPU test

* fix GPU test for R pkg


Project: http://git-wip-us.apache.org/repos/asf/incubator-mxnet-test/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mxnet-test/commit/e906f883
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mxnet-test/tree/e906f883
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mxnet-test/diff/e906f883

Branch: refs/heads/master
Commit: e906f883642423479600abd9cc1c101af91ca908
Parents: 78b1208
Author: Qiang Kou (KK) <qk...@qkou.info>
Authored: Wed Jun 28 16:01:23 2017 -0700
Committer: Eric Junyuan Xie <pi...@users.noreply.github.com>
Committed: Wed Jun 28 16:01:23 2017 -0700

----------------------------------------------------------------------
 Jenkinsfile                                |  28 ++++++
 Makefile                                   |  13 ++-
 R-package/tests/testthat/get_data.R        |  39 ++++++++
 R-package/tests/testthat/test_io.R         |  86 +++++++++++++++++
 R-package/tests/testthat/test_model.R      | 119 ++++++++++++------------
 R-package/tests/testthat/test_ndarray.R    |   1 +
 R-package/tests/testthat/test_symbol.R     |  48 +++++++++-
 tests/ci_build/Dockerfile.cpu              |   2 +
 tests/ci_build/Dockerfile.gpu              |   2 +
 tests/ci_build/install/ubuntu_install_r.sh |  10 ++
 10 files changed, 287 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mxnet-test/blob/e906f883/Jenkinsfile
----------------------------------------------------------------------
diff --git a/Jenkinsfile b/Jenkinsfile
index f2beae0..cd6ab31 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -271,6 +271,34 @@ stage('Unit Test') {
       }
     }
   },
+  'R: CPU': {
+    node('linux') {
+      ws('workspace/ut-r-cpu') {
+        init_git()
+        unpack_lib('cpu')
+        timeout(time: max_time, unit: 'MINUTES') {
+          sh "${docker_run} cpu mkdir -p /workspace/ut-r-cpu/site-library"
+          sh "${docker_run} cpu make rpkg USE_BLAS=openblas R_LIBS=/workspace/ut-r-cpu/site-library"
+          sh "${docker_run} cpu R CMD INSTALL --library=/workspace/ut-r-cpu/site-library mxnet_current_r.tar.gz"
+          sh "${docker_run} cpu make rpkgtest R_LIBS=/workspace/ut-r-cpu/site-library"
+        }
+      }
+    }
+  },
+  'R: GPU': {
+    node('GPU' && 'linux') {
+      ws('workspace/ut-r-gpu') {
+        init_git()
+        unpack_lib('gpu')
+        timeout(time: max_time, unit: 'MINUTES') {
+          sh "${docker_run} gpu mkdir -p /workspace/ut-r-gpu/site-library"
+          sh "${docker_run} gpu make rpkg USE_BLAS=openblas R_LIBS=/workspace/ut-r-gpu/site-library"
+          sh "${docker_run} gpu R CMD INSTALL --library=/workspace/ut-r-gpu/site-library mxnet_current_r.tar.gz"
+          sh "${docker_run} gpu make rpkgtest R_LIBS=/workspace/ut-r-gpu/site-library"
+        }
+      }
+    }
+  },  
   'Python2/3: CPU Win':{
     node('windows') {
       ws('workspace/ut-python-cpu') {

http://git-wip-us.apache.org/repos/asf/incubator-mxnet-test/blob/e906f883/Makefile
----------------------------------------------------------------------
diff --git a/Makefile b/Makefile
index 69527f4..749c761 100644
--- a/Makefile
+++ b/Makefile
@@ -383,17 +383,24 @@ rpkg:
 	cp -rf include/* R-package/inst/include
 	cp -rf dmlc-core/include/* R-package/inst/include/
 	cp -rf nnvm/include/* R-package/inst/include
+	Rscript -e "if(!require(devtools)){install.packages('devtools', repo = 'https://cloud.r-project.org/')}"
+	Rscript -e "library(devtools); library(methods); options(repos=c(CRAN='https://cloud.r-project.org/')); install_deps(pkg='R-package', dependencies = TRUE)"
 	echo "import(Rcpp)" > R-package/NAMESPACE
 	echo "import(methods)" >> R-package/NAMESPACE
 	R CMD INSTALL R-package
-	Rscript -e "require(mxnet); mxnet:::mxnet.export(\"R-package\")"
+	Rscript -e "require(mxnet); mxnet:::mxnet.export('R-package')"
 	rm -rf R-package/NAMESPACE
-	Rscript -e "require(devtools); install_version(\"roxygen2\", version = \"5.0.1\", repos = \"https://cloud.r-project.org/\", quiet = TRUE)"
-	Rscript -e "require(roxygen2); roxygen2::roxygenise(\"R-package\")"
+	Rscript -e "if (!require('roxygen2')||packageVersion('roxygen2')!= '5.0.1'){\
+	devtools::install_version('roxygen2',version='5.0.1',\
+	repo='https://cloud.r-project.org/',quiet=TRUE)}"
+	Rscript -e "require(roxygen2); roxygen2::roxygenise('R-package')"
 	R CMD build --no-build-vignettes R-package
 	rm -rf mxnet_current_r.tar.gz
 	mv mxnet_*.tar.gz mxnet_current_r.tar.gz
 
+rpkgtest:
+	Rscript -e "require(testthat);res<-test_dir('R-package/tests/testthat');if(!testthat:::all_passed(res)){stop('Test failures', call. = FALSE)}"
+
 scalapkg:
 	(cd $(ROOTDIR)/scala-package; \
 		mvn clean package -P$(SCALA_PKG_PROFILE) -Dcxx="$(CXX)" \

http://git-wip-us.apache.org/repos/asf/incubator-mxnet-test/blob/e906f883/R-package/tests/testthat/get_data.R
----------------------------------------------------------------------
diff --git a/R-package/tests/testthat/get_data.R b/R-package/tests/testthat/get_data.R
new file mode 100644
index 0000000..021cbfe
--- /dev/null
+++ b/R-package/tests/testthat/get_data.R
@@ -0,0 +1,39 @@
+
+GetMNIST_ubyte <- function() {
+  if (!dir.exists("data")) {
+    dir.create("data/")
+  }
+  if (!file.exists('data/train-images-idx3-ubyte') |
+      !file.exists('data/train-labels-idx1-ubyte') |
+      !file.exists('data/t10k-images-idx3-ubyte') |
+      !file.exists('data/t10k-labels-idx1-ubyte')) {
+    download.file('http://data.mxnet.io/mxnet/data/mnist.zip', destfile = 'data/mnist.zip')
+    unzip('data/mnist.zip', exdir = 'data/')
+  }
+}
+
+GetMNIST_csv <- function() {
+  if (!dir.exists("data")) {
+    dir.create("data/")
+  }
+  if (!file.exists('data/train.csv') |
+      !file.exists('data/test.csv')) {
+    download.file('https://s3-us-west-2.amazonaws.com/apache-mxnet/R/data/mnist_csv.zip',
+                  destfile = 'data/mnist_csv.zip')
+    unzip('data/mnist_csv.zip', exdir = 'data/')
+  }
+}
+
+GetCifar10 <- function() {
+  if (!dir.exists("data")) {
+    dir.create("data/")
+  }
+  if (!file.exists('data/cifar/train.rec') |
+      !file.exists('data/cifar/test.rec') |
+      !file.exists('data/cifar/train.lst') |
+      !file.exists('data/cifar/test.lst')) {
+    download.file('http://data.mxnet.io/mxnet/data/cifar10.zip',
+                  destfile = 'data/cifar10.zip')
+    unzip('data/cifar10.zip', exdir = 'data/')
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-mxnet-test/blob/e906f883/R-package/tests/testthat/test_io.R
----------------------------------------------------------------------
diff --git a/R-package/tests/testthat/test_io.R b/R-package/tests/testthat/test_io.R
new file mode 100644
index 0000000..d619856
--- /dev/null
+++ b/R-package/tests/testthat/test_io.R
@@ -0,0 +1,86 @@
+require(mxnet)
+
+context("io")
+
+source("get_data.R")
+
+test_that("MNISTIter", {
+  GetMNIST_ubyte()
+  batch.size <- 100
+  train_dataiter <- mx.io.MNISTIter(
+    image = "data/train-images-idx3-ubyte",
+    label = "data/train-labels-idx1-ubyte",
+    data.shape = c(784),
+    batch.size = batch.size,
+    shuffle = TRUE,
+    flat = TRUE,
+    silent = 0,
+    seed = 10
+  )
+  train_dataiter$reset()
+  batch_count = 0
+  while (train_dataiter$iter.next()) {
+    batch_count = batch_count + 1
+  }
+  nbatch = 60000 / batch.size
+  expect_equal(batch_count, nbatch)
+  train_dataiter$reset()
+  train_dataiter$iter.next()
+  label_0 <- as.array(train_dataiter$value()$label)
+  train_dataiter$iter.next()
+  train_dataiter$iter.next()
+  train_dataiter$iter.next()
+  train_dataiter$iter.next()
+  train_dataiter$reset()
+  train_dataiter$iter.next()
+  label_1 <- as.array(train_dataiter$value()$label)
+  expect_equal(label_0, label_1)
+})
+
+test_that("Cifar10Rec", {
+  GetCifar10()
+  dataiter <- mx.io.ImageRecordIter(
+    path.imgrec     = "./data/cifar/train.rec",
+    path.imglist    = "./data/cifar/train.lst",
+    mean.img        = "./data/cifar/cifar10_mean.bin",
+    batch.size      = 100,
+    data.shape      = c(28, 28, 3),
+    rand.crop       = TRUE,
+    rand.mirror     = TRUE
+  )
+  labelcount = rep(0, 10)
+  dataiter$reset()
+  while (dataiter$iter.next()) {
+    label = as.array(dataiter$value()$label)
+    for (i in label) {
+      labelcount[i + 1] = labelcount[i + 1] + 1
+    }
+  }
+  
+  expect_equal(labelcount, rep(5000, 10))
+})
+
+test_that("mx.io.arrayiter", {
+  X <- matrix(c(1:10000), 100, 100)
+  y <- c(1:100)
+  dataiter <- mx.io.arrayiter(X, y, batch.size = 20, shuffle = FALSE)
+  dataiter$reset()
+  batch_count = 0
+  while (dataiter$iter.next()) {
+    batch_count = batch_count + 1
+  }
+  expect_equal(batch_count, 100 / 20)
+  
+  y <- round(y / 10)
+  dataiter <- mx.io.arrayiter(X, y, batch.size = 30, shuffle = FALSE)
+  labelcount <- rep(0, 11)
+  dataiter$reset()
+  while (dataiter$iter.next()) {
+    label <- as.array(dataiter$value()$label)
+    for (i in label) {
+      labelcount[i + 1] = labelcount[i + 1] + 1
+    }
+  }
+  
+  expect_equal(labelcount, c(5, 9, 11, 9, 11, 9, 11, 13, 22, 14, 6))
+})

http://git-wip-us.apache.org/repos/asf/incubator-mxnet-test/blob/e906f883/R-package/tests/testthat/test_model.R
----------------------------------------------------------------------
diff --git a/R-package/tests/testthat/test_model.R b/R-package/tests/testthat/test_model.R
index 93784a6..9c85afa 100644
--- a/R-package/tests/testthat/test_model.R
+++ b/R-package/tests/testthat/test_model.R
@@ -1,62 +1,67 @@
 require(mxnet)
 
+source("get_data.R")
+
 context("models")
 
-# test_that("basic symbol operation", {
+test_that("basic symbol operation", {
 #   # Network configuration
-#   batch.size <- 100
-#   data <- mx.symbol.Variable("data")
-#   fc1 <- mx.symbol.FullyConnected(data, name="fc1", num_hidden=128)
-#   act1 <- mx.symbol.Activation(fc1, name="relu1", act_type="relu")
-#   fc2 <- mx.symbol.FullyConnected(act1, name = "fc2", num_hidden = 64)
-#   act2 <- mx.symbol.Activation(fc2, name="relu2", act_type="relu")
-#   fc3 <- mx.symbol.FullyConnected(act2, name="fc3", num_hidden=10)
-#   softmax <- mx.symbol.Softmax(fc3, name = "sm")
-#   
-#   dtrain = mx.io.MNISTIter(
-#     image="data/train-images-idx3-ubyte",
-#     label="data/train-labels-idx1-ubyte",
-#     data.shape=c(784),
-#     batch.size=batch.size,
-#     shuffle=TRUE,
-#     flat=TRUE,
-#     silent=0,
-#     seed=10)
-#   
-#   dtest = mx.io.MNISTIter(
-#     image="data/t10k-images-idx3-ubyte",
-#     label="data/t10k-labels-idx1-ubyte",
-#     data.shape=c(784),
-#     batch.size=batch.size,
-#     shuffle=FALSE,
-#     flat=TRUE,
-#     silent=0)
-#   
-#   mx.set.seed(0)
-#   devices = lapply(1:2, function(i) {
-#     mx.cpu(i)
-#   })
-#   
-#   # create the model
-#   model <- mx.model.FeedForward.create(softmax, X=dtrain, eval.data=dtest,
-#                                        ctx=devices, num.round=1,
-#                                        learning.rate=0.1, momentum=0.9,
-#                                        initializer=mx.init.uniform(0.07),
-#                                        epoch.end.callback=mx.callback.save.checkpoint("chkpt"),
-#                                        batch.end.callback=mx.callback.log.train.metric(100))
-#   
-#   # do prediction
-#   pred <- predict(model, dtest)
-#   label <- mx.io.extract(dtest, "label")
-#   dataX <- mx.io.extract(dtest, "data")
-#   # Predict with R's array
-#   pred2 <- predict(model, X=dataX)
-#   
-#   accuracy <- function(label, pred) {
-#     ypred = max.col(t(as.array(pred)))
-#     return(sum((as.array(label) + 1) == ypred) / length(label))
-#   }
-#   
-#   print(paste0("Finish prediction... accuracy=", accuracy(label, pred)))
-#   print(paste0("Finish prediction... accuracy2=", accuracy(label, pred2)))
-# })
+   GetMNIST_ubyte()
+   batch.size <- 100
+   data <- mx.symbol.Variable("data")
+   fc1 <- mx.symbol.FullyConnected(data, name="fc1", num_hidden=128)
+   act1 <- mx.symbol.Activation(fc1, name="relu1", act_type="relu")
+   fc2 <- mx.symbol.FullyConnected(act1, name = "fc2", num_hidden = 64)
+   act2 <- mx.symbol.Activation(fc2, name="relu2", act_type="relu")
+   fc3 <- mx.symbol.FullyConnected(act2, name="fc3", num_hidden=10)
+   softmax <- mx.symbol.Softmax(fc3, name = "sm")
+   
+   dtrain = mx.io.MNISTIter(
+     image="data/train-images-idx3-ubyte",
+     label="data/train-labels-idx1-ubyte",
+     data.shape=c(784),
+     batch.size=batch.size,
+     shuffle=TRUE,
+     flat=TRUE,
+     silent=0,
+     seed=10)
+   
+   dtest = mx.io.MNISTIter(
+     image="data/t10k-images-idx3-ubyte",
+     label="data/t10k-labels-idx1-ubyte",
+     data.shape=c(784),
+     batch.size=batch.size,
+     shuffle=FALSE,
+     flat=TRUE,
+     silent=0)
+   
+   mx.set.seed(0)
+   devices = lapply(1:2, function(i) {
+     mx.cpu(i)
+   })
+   
+   # create the model
+   model <- mx.model.FeedForward.create(softmax, X=dtrain, eval.data=dtest,
+                                        ctx=devices, num.round=1,
+                                        learning.rate=0.1, momentum=0.9,
+                                        initializer=mx.init.uniform(0.07),
+                                        epoch.end.callback=mx.callback.save.checkpoint("chkpt"),
+                                        batch.end.callback=mx.callback.log.train.metric(100))
+   
+   # do prediction
+   pred <- predict(model, dtest)
+   label <- mx.io.extract(dtest, "label")
+   dataX <- mx.io.extract(dtest, "data")
+   # Predict with R's array
+   pred2 <- predict(model, X=dataX)
+   
+   accuracy <- function(label, pred) {
+     ypred = max.col(t(as.array(pred)))
+     return(sum((as.array(label) + 1) == ypred) / length(label))
+   }
+
+   expect_equal(accuracy(label, pred), accuracy(label, pred2))
+   
+   file.remove("chkpt-0001.params")
+   file.remove("chkpt-symbol.json")
+})

http://git-wip-us.apache.org/repos/asf/incubator-mxnet-test/blob/e906f883/R-package/tests/testthat/test_ndarray.R
----------------------------------------------------------------------
diff --git a/R-package/tests/testthat/test_ndarray.R b/R-package/tests/testthat/test_ndarray.R
index 0be603e..a6b85da 100644
--- a/R-package/tests/testthat/test_ndarray.R
+++ b/R-package/tests/testthat/test_ndarray.R
@@ -46,4 +46,5 @@ test_that("ndarray ones, zeros, save and load", {
   mat2 = mx.nd.load('temp.mat')
   expect_true(is.mx.ndarray(mat2[[1]]))
   expect_equal(as.array(mat), as.array(mat2[[1]]))
+  file.remove('temp.mat')
 })

http://git-wip-us.apache.org/repos/asf/incubator-mxnet-test/blob/e906f883/R-package/tests/testthat/test_symbol.R
----------------------------------------------------------------------
diff --git a/R-package/tests/testthat/test_symbol.R b/R-package/tests/testthat/test_symbol.R
index 7e733e8..be38762 100644
--- a/R-package/tests/testthat/test_symbol.R
+++ b/R-package/tests/testthat/test_symbol.R
@@ -8,6 +8,7 @@ test_that("basic symbol operation", {
   net1 = mx.symbol.FullyConnected(data=net1, name='fc2', num_hidden=100)
   
   expect_equal(arguments(net1), c('data', 'fc1_weight', 'fc1_bias', 'fc2_weight', 'fc2_bias'))
+  expect_equal(outputs(net1), 'fc2_output')
   
   net2 = mx.symbol.FullyConnected(name='fc3', num_hidden=10)
   net2 = mx.symbol.Activation(data=net2, act_type='relu')
@@ -16,6 +17,52 @@ test_that("basic symbol operation", {
   composed = mx.apply(net2, fc3_data=net1, name='composed')
   
   expect_equal(arguments(composed), c('data', 'fc1_weight', 'fc1_bias', 'fc2_weight', 'fc2_bias', 'fc3_weight', 'fc3_bias', 'fc4_weight', 'fc4_bias'))
+  expect_equal(outputs(composed), 'composed_output')
+  
+  multi_out = mx.symbol.Group(c(composed, net1))
+  expect_equal(outputs(multi_out), c('composed_output', 'fc2_output'))
+})
+
+test_that("symbol internal", {
+  data = mx.symbol.Variable('data')
+  oldfc = mx.symbol.FullyConnected(data=data, name='fc1', num_hidden=10)
+  net1 = mx.symbol.FullyConnected(data=oldfc, name='fc2', num_hidden=100)
+  
+  expect_equal(arguments(net1), c("data", "fc1_weight", "fc1_bias", "fc2_weight", "fc2_bias"))
+  
+  internal = net1$get.internals()
+  fc1 = internal[[match("fc1_output", internal$outputs)]]
+  
+  expect_equal(arguments(fc1), arguments(oldfc))
+})
+
+test_that("symbol infer type", {
+  num_hidden = 128
+  num_dim    = 64
+  num_sample = 10
+  
+  data = mx.symbol.Variable('data')
+  prev = mx.symbol.Variable('prevstate')
+  x2h  = mx.symbol.FullyConnected(data=data, name='x2h', num_hidden=num_hidden)
+  h2h  = mx.symbol.FullyConnected(data=prev, name='h2h', num_hidden=num_hidden)
+  
+  out  = mx.symbol.Activation(data=mx.symbol.elemwise_add(x2h, h2h), name='out', act_type='relu')
+  
+  # shape inference will fail because information is not available for h2h
+  ret = mx.symbol.infer.shape(out, data = c(num_dim, num_sample))
+  
+  expect_equal(ret, NULL)
+})
+
+test_that("symbol save/load", {
+  data <- mx.symbol.Variable("data")
+  fc1 <- mx.symbol.FullyConnected(data, num_hidden=1)
+  lro <- mx.symbol.LinearRegressionOutput(fc1)
+  mx.symbol.save(lro, "tmp_r_sym.json")
+  data2 = mx.symbol.load("tmp_r_sym.json")
+  
+  expect_equal(data2$as.json(), lro$as.json())
+  file.remove("tmp_r_sym.json")
 })
 
 test_that("symbol attributes access", {
@@ -31,4 +78,3 @@ test_that("symbol attributes access", {
   expect_equal(y$attributes$`__shape__`, str)
 })
 
-

http://git-wip-us.apache.org/repos/asf/incubator-mxnet-test/blob/e906f883/tests/ci_build/Dockerfile.cpu
----------------------------------------------------------------------
diff --git a/tests/ci_build/Dockerfile.cpu b/tests/ci_build/Dockerfile.cpu
index aabda4e..c9ba57c 100644
--- a/tests/ci_build/Dockerfile.cpu
+++ b/tests/ci_build/Dockerfile.cpu
@@ -6,3 +6,5 @@ COPY install/ubuntu_install_python.sh /install/
 RUN /install/ubuntu_install_python.sh
 COPY install/ubuntu_install_scala.sh /install/
 RUN /install/ubuntu_install_scala.sh
+COPY install/ubuntu_install_r.sh /install/
+RUN /install/ubuntu_install_r.sh

http://git-wip-us.apache.org/repos/asf/incubator-mxnet-test/blob/e906f883/tests/ci_build/Dockerfile.gpu
----------------------------------------------------------------------
diff --git a/tests/ci_build/Dockerfile.gpu b/tests/ci_build/Dockerfile.gpu
index 46d3631..cd9986e 100644
--- a/tests/ci_build/Dockerfile.gpu
+++ b/tests/ci_build/Dockerfile.gpu
@@ -6,3 +6,5 @@ COPY install/ubuntu_install_python.sh /install/
 RUN /install/ubuntu_install_python.sh
 COPY install/ubuntu_install_scala.sh /install/
 RUN /install/ubuntu_install_scala.sh
+COPY install/ubuntu_install_r.sh /install/
+RUN /install/ubuntu_install_r.sh

http://git-wip-us.apache.org/repos/asf/incubator-mxnet-test/blob/e906f883/tests/ci_build/install/ubuntu_install_r.sh
----------------------------------------------------------------------
diff --git a/tests/ci_build/install/ubuntu_install_r.sh b/tests/ci_build/install/ubuntu_install_r.sh
new file mode 100755
index 0000000..10851a6
--- /dev/null
+++ b/tests/ci_build/install/ubuntu_install_r.sh
@@ -0,0 +1,10 @@
+#!/usr/bin/env bash
+# install libraries for mxnet's r package on ubuntu
+
+echo "deb http://cran.rstudio.com/bin/linux/ubuntu trusty/" >> /etc/apt/sources.list
+gpg --keyserver keyserver.ubuntu.com --recv-key E084DAB9
+gpg -a --export E084DAB9 | apt-key add -
+
+apt-get update
+apt-get install -y r-base r-base-dev libxml2-dev libssl-dev
+