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

Commit

Permalink
Support R-package with cmake build and fix installation instructions (#…
Browse files Browse the repository at this point in the history
…17228)

* Support R-package with cmake build and fix installation instructions

* Fix typo

* Fix callback.R

* Clarify creation of personal R library

* Fix generation of R-package documentation

* Remove unused USE_BLAS variable on CI for make rpkg

* Add cmake build command for get_started/linux/r/*.md pages

* Fix zzz.R swallowing error messages

* Fix R-package/src/Makevars for OpenCV dependency

See #17282
  • Loading branch information
leezu authored Jan 14, 2020
1 parent 3971938 commit 9f2e73f
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 100 deletions.
30 changes: 0 additions & 30 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -683,36 +683,6 @@ cython3:
cyclean:
rm -rf python/mxnet/*/*.so python/mxnet/*/*.cpp

# R related shortcuts
rcpplint:
3rdparty/dmlc-core/scripts/lint.py mxnet-rcpp ${LINT_LANG} R-package/src

rpkg:
mkdir -p R-package/inst/libs
cp src/io/image_recordio.h R-package/src
cp -rf lib/libmxnet.so R-package/inst/libs

if [ -e "lib/libtvm_runtime.so" ]; then \
cp -rf lib/libtvm_runtime.so R-package/inst/libs; \
fi

mkdir -p R-package/inst/include
cp -rl include/* R-package/inst/include
Rscript -e "if(!require(devtools)){install.packages('devtools', repo = 'https://cloud.r-project.org/')}"
Rscript -e "if(!require(roxygen2)||packageVersion('roxygen2') < '6.1.1'){install.packages('roxygen2', 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)"
cp R-package/dummy.NAMESPACE R-package/NAMESPACE
echo "import(Rcpp)" >> R-package/NAMESPACE
R CMD INSTALL R-package
Rscript -e "require(mxnet); mxnet:::mxnet.export('R-package'); warnings()"
rm R-package/NAMESPACE
Rscript -e "devtools::document('R-package');warnings()"
R CMD INSTALL R-package

rpkgtest:
Rscript -e 'require(testthat);res<-test_dir("R-package/tests/testthat");if(!testthat:::all_passed(res)){stop("Test failures", call. = FALSE)}'
Rscript -e 'res<-covr:::package_coverage("R-package");fileConn<-file(paste("r-package_coverage_",toString(runif(1)),".json"));writeLines(covr:::to_codecov(res), fileConn);close(fileConn)'

scalaclean:
(cd $(ROOTDIR)/scala-package && mvn clean)

Expand Down
33 changes: 33 additions & 0 deletions R-package/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
rcpplint:
3rdparty/dmlc-core/scripts/lint.py mxnet-rcpp ${LINT_LANG} R-package/src

rpkg:
mkdir -p R-package/inst/libs
cp src/io/image_recordio.h R-package/src
if [ -d "lib" ]; then \
cp -rf lib/libmxnet.so R-package/inst/libs; \
if [ -e "lib/libtvm_runtime.so" ]; then \
cp -rf lib/libtvm_runtime.so R-package/inst/libs; \
fi; \
else \
cp -rf build/libmxnet.so R-package/inst/libs; \
if [ -e "build/libtvm_runtime.so" ]; then \
cp -rf build/libtvm_runtime.so R-package/inst/libs; \
fi; \
fi

mkdir -p R-package/inst/include
cp -rl include/* R-package/inst/include
Rscript -e "if(!require(devtools)){install.packages('devtools', repo = 'https://cloud.r-project.org/')}"
Rscript -e "if(!require(roxygen2)||packageVersion('roxygen2') < '6.1.1'){install.packages('roxygen2', 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)"
cp R-package/dummy.NAMESPACE R-package/NAMESPACE # NAMESPACE will be replaced by devtools::document later
echo "import(Rcpp)" >> R-package/NAMESPACE
R CMD INSTALL R-package
Rscript -e "require(mxnet); mxnet:::mxnet.export('R-package'); warnings()"
Rscript -e "devtools::document('R-package');warnings()"
R CMD INSTALL R-package

rpkgtest:
Rscript -e 'require(testthat);res<-test_dir("R-package/tests/testthat");if(!testthat:::all_passed(res)){stop("Test failures", call. = FALSE)}'
Rscript -e 'res<-covr:::package_coverage("R-package");fileConn<-file(paste("r-package_coverage_",toString(runif(1)),".json"));writeLines(covr:::to_codecov(res), fileConn);close(fileConn)'
1 change: 1 addition & 0 deletions R-package/R/callback.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.

#' @export mx.metric.logger
library(methods)
mx.metric.logger <- setRefClass("mx.metric.logger", fields = list(train = "numeric", eval="numeric"))

#' Log training metric each period
Expand Down
14 changes: 12 additions & 2 deletions R-package/R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,18 @@ NULL
.onLoad <- function(libname, pkgname) {
# Require methods for older versions of R
require(methods)
tryCatch(library.dynam("libmxnet", pkgname, libname, local=FALSE), error = function(e) { print('Loading local: inst/libs/libmxnet.so'); dyn.load("R-package/inst/libs/libmxnet.so", local=FALSE) })
tryCatch(library.dynam("mxnet", pkgname, libname), error = function(e) { print('Loading local: src/mxnet.so'); dyn.load("R-package/src/mxnet.so") })
tryCatch(library.dynam("libmxnet", pkgname, libname, local=FALSE), error = function(e) {
print('Loading libmxnet.so failed. Error:')
print(e)
print('Falling back to loading local: inst/libs/libmxnet.so')
dyn.load("R-package/inst/libs/libmxnet.so", local=FALSE)
})
tryCatch(library.dynam("mxnet", pkgname, libname), error = function(e) {
print('Loading mxnet.so failed. Error:')
print(e)
print('Falling back to loading local: src/mxnet.so')
dyn.load("R-package/src/mxnet.so")
})
loadModule("mxnet", TRUE)
init.symbol.methods()
init.context.default()
Expand Down
5 changes: 3 additions & 2 deletions R-package/src/Makevars
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CXX_STD = CXX11
PKG_CPPFLAGS = -I../inst/include
PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS)
PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) `pkg-config --libs opencv`
PKG_CFLAGS = `pkg-config --cflags opencv`
PKG_CPPFLAGS = -I../inst/include `pkg-config --cflags opencv` `Rscript -e 'Rcpp:::CxxFlags()'`
1 change: 0 additions & 1 deletion R-package/src/Makevars.win
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

PKG_CPPFLAGS = -I../inst/include
PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) -L../inst/libs/x64/ -llibmxnet
13 changes: 5 additions & 8 deletions ci/docker/runtime_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1223,12 +1223,11 @@ unittest_ubuntu_cpu_R() {
build_ccache_wrappers
echo "MAKEFLAGS = -j"$(nproc) > ~/.R/Makevars
# make -j not supported
make rpkg \
USE_BLAS=openblas \
make -f R-package/Makefile rpkg \
R_LIBS=/tmp/r-site-library

R CMD INSTALL --library=/tmp/r-site-library R-package
make rpkgtest R_LIBS=/tmp/r-site-library
make -f R-package/Makefile rpkgtest R_LIBS=/tmp/r-site-library
}

unittest_ubuntu_minimal_R() {
Expand All @@ -1239,8 +1238,7 @@ unittest_ubuntu_minimal_R() {
build_ccache_wrappers
echo "MAKEFLAGS = -j"$(nproc) > ~/.R/Makevars
# make -j not supported
make rpkg \
USE_BLAS=openblas \
make -f R-package/Makefile rpkg \
R_LIBS=/tmp/r-site-library

R CMD INSTALL --library=/tmp/r-site-library R-package
Expand Down Expand Up @@ -1269,11 +1267,10 @@ unittest_ubuntu_gpu_R() {
build_ccache_wrappers
echo "MAKEFLAGS = -j"$(nproc) > ~/.R/Makevars
# make -j not supported
make rpkg \
USE_BLAS=openblas \
make -f R-package/Makefile rpkg \
R_LIBS=/tmp/r-site-library
R CMD INSTALL --library=/tmp/r-site-library R-package
make rpkgtest R_LIBS=/tmp/r-site-library R_GPU_ENABLE=1
make -f R-package/Makefile rpkgtest R_LIBS=/tmp/r-site-library R_GPU_ENABLE=1
}

unittest_ubuntu_cpu_julia() {
Expand Down
5 changes: 3 additions & 2 deletions docs/static_site/src/_includes/get_started/linux/r/cpu.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ After you have setup R v3.4.4+ and MXNet, you can build and install the MXNet R

{% highlight bash %}
$ cd incubator-mxnet
$ make rpkg
{% endhighlight %}
$ mkdir build; cd build; cmake -DUSE_CUDA=OFF ..; make -j $(nproc); cd ..
$ make -f R-package/Makefile rpkg
{% endhighlight %}
5 changes: 3 additions & 2 deletions docs/static_site/src/_includes/get_started/linux/r/gpu.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ MXNet as follows:

{% highlight bash %}
$ cd incubator-mxnet
$ make rpkg
$ mkdir build; cd build; cmake ..; make -j $(nproc); cd ..
$ make -f R-package/Makefile rpkg
{% endhighlight %}

{% include /get_started/gpu_snippet.md %}
{% include /get_started/gpu_snippet.md %}
2 changes: 1 addition & 1 deletion docs/static_site/src/pages/get_started/osx_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ Run the following commands to install the MXNet dependencies and build the MXNet
cd R-package
Rscript -e "library(devtools); library(methods); options(repos=c(CRAN='https://cran.rstudio.com')); install_deps(dependencies = TRUE)"
cd ..
make rpkg
make -f R-package/Makefile rpkg
```

## Install the MXNet Package for Julia
Expand Down
71 changes: 20 additions & 51 deletions docs/static_site/src/pages/get_started/ubuntu_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,23 +363,9 @@ make install

### Install the MXNet Package for R

Building *MXNet* from source is a 2 step process.
1. Build the *MXNet* core shared library, `libmxnet.so`, from source.
2. Build the R bindings.

#### Quick MXNet-R Installation
You can quickly build MXNet-R with the following two scripts found in the `/docs/install` folder:

```bash
git clone --recursive https://github.com/apache/incubator-mxnet.git mxnet
cd mxnet/docs/install
./install_mxnet_ubuntu_python.sh
./install_mxnet_ubuntu_r.sh
```

Or you can go through a manual process described next.

#### Manual MXNet-R Installation
Before you build MXNet for R from source code, you must complete
[building the shared library](#build-the-shared-library).
source root directory to build the MXNet Perl package:

**Minimum Requirements**
1. [GCC 4.8](https://gcc.gnu.org/gcc-4.8/) or later to compile C++ 11.
Expand All @@ -389,60 +375,43 @@ Or you can go through a manual process described next.

**Build the MXNet core shared library**

**Step 1** Install build tools and git.
**Step 1** Install R, cran-devtools and dependencies

To install R and the devtools, run

```bash
sudo apt-get update
sudo apt-get install -y build-essential git
sudo apt-get install -y r-base-core r-cran-devtools libcairo2-dev libxml2-dev
```

**Step 2** Install OpenBLAS.
`libxml2-dev` is required for the `roxygen2` dependency and `libcairo2-dev` is
required for the suggested `imager` dependency.

*MXNet* uses [BLAS](https://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms)
and [LAPACK](https://en.wikipedia.org/wiki/LAPACK) libraries for accelerated numerical computations
on CPU machine. There are several flavors of BLAS/LAPACK libraries - [OpenBLAS](https://www.openblas.net/),
[ATLAS](http://math-atlas.sourceforge.net/) and [MKL](https://software.intel.com/en-us/intel-mkl). In this step we
install OpenBLAS. You can choose to install ATLAS or MKL.
To generate documentation, it is also required to install `roxygen2`.

```bash
sudo apt-get install -y libopenblas-dev liblapack-dev
R
> install.packages("roxygen2")
> Would you like to use a personal library instead? (y/n) y
> Would you like to create a personal library ... to install packages into? (y/n) y
```

**Step 3** Install OpenCV.
Note: To successfully complete the next step, you need a personal R library. If
you were able to run `install.packages("roxygen2")` above, you either had
already, or you have successfully created a personal library just now.

*MXNet* uses [OpenCV](https://opencv.org/) for efficient image loading and augmentation operations.
**Step 2** Make and install the MXNet-R bindings.

```bash
sudo apt-get install -y libopencv-dev
make -f R-package/Makefile rpkg
```

**Step 4** Download MXNet sources and build MXNet core shared library. You can clone the repository as described in the
following code block, or you may try the [download links](download) for your desired MXNet version.

```bash
git clone --recursive https://github.com/apache/incubator-mxnet mxnet
cd mxnet
echo "USE_OPENCV = 1" >> ./config.mk
echo "USE_BLAS = openblas" >> ./config.mk
make -j $(nproc)
```

*Note* - USE_OPENCV and USE_BLAS are make file flags to set compilation options to use OpenCV and BLAS library. You can
explore and use more compilation options in `make/config.mk`.

<br/>

**Step 5** Make and install the MXNet-R bindings.

```bash
make rpkg
```
#### Verify MXNet-R Installation

You can verify your MXNet-R installation as follows:

```bash
sudo -i R
R
```

At the R prompt enter the following:
Expand Down
2 changes: 1 addition & 1 deletion setup-utils/install-mxnet-ubuntu-r.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ sudo Rscript -e "library(devtools); library(methods); options(repos=c(CRAN='http
cd ..

echo "Compiling R package. This can take few minutes..."
sudo make rpkg
sudo make -f R-package/Makefile rpkg

echo "Installing R package..."
sudo R CMD INSTALL mxnet_current_r.tar.gz
Expand Down

0 comments on commit 9f2e73f

Please sign in to comment.