I have been trying to get Torch to run on Windows recently. Inspired by SpaceCowboy850's write-down, I thought that maybe I should share my notes too. Hopefully at least some of this information is useful to others that are trying to get Torch working on Windows!
Current status:
+ successfully linked with a BLAS library (LAPACK)
+ Torch works (all tests pass, except for abs; see below)
+ The nn package works (all tests pass)
+ Other packages that at least compile/install: optim, gnuplot, luafilesystem, inspect, image, trepl, sys
- The nnx package works partially (some tests fail)
- Packages with non-trivial issues: dataset and ipc (pthreads dependency)
- GPU acceleration: haven't looked at that yet
Also, ZeroBrane Studio integration works just fine, including debugging.
Ok, here we go!
Prerequisites
====
Git: We need to have a git tool in our path. PortableGit or Git for Windows are some possible options. Cmder comes with a git client, too (didn't try using that one). Make sure that you have the git executable in your PATH.
CMake: Install CMake and make sure that you have it's executable in your PATH.
Visual Studio with the C/C++ module installed: VS 2015 Community Edition works just fine.
As SpaceCowboy850 noted, you should check what development environments and tools you have active in your PATH and in other environment variables already, because some, like cygwin and MinGW, might interfere with the process.
Cmder (optional)
====
I chose to use Cmder to replace the Windows Developer Command Prompt to make things less painful. The Torch REPL can be run through it, too. Skip this section if you wish.
Download and install Cmder, or Cmder Lite if you already have another Git client installed.
Create a new task for VS: Go to Settings -> Startup -> Tasks and create a new task. Name it VS2015 or something, make it the default task, and add the following string as the startup command (replace the project path with whatever you have):
cmd /k ""%VS140COMNTOOLS%VsDevCmd.bat" & "%ConEmuDir%\..\init.bat"" -new_console:d:"X:\work\torch_projects":t:"VS2015"
Create a new task for Torch: Go to Settings -> Startup -> Tasks and create a new task. Name it Torch or something and add the following string as the startup command (replace the paths with whatever you are going to use):
X:\torch-luajit-rocks\luajit.exe -new_console:d:"X:\work\torch_projects":t:"Torch"
Aliases can be added as usual (they persist restarts): alias ll=ls -la --show-control-chars -F --color $*
Adding Sublime Text 3's program directory to your path lets you use the command "subl" to quickly open files (for deeper integration, see eg http://goo.gl/yF173L ).
Note: There is the option "Inject ConEmuHk" under Settings -> Features. Enabling it slows down all command execution, especially compilation. Disabling it messes up colored output for second level processes. You might want to switch this temporarily on while trying to make sense of colored make output and otherwise keep it off.
Then some patching: at least for me, command line wrapping was broken when inside git repos. If you have the same issue, then open the file C:\Program Files (x86)\cmder_mini\config\cmder.lua and comment out the line with the os.execute() call. (see https://github.com/cmderdev/cmder/issues/749 )
Install MinGW and MSYS (needed if you want to compile BLAS by yourself)
====
Use the MinGW installer and install all meta-packages from the "Basic Setup" section. As usual with tools from the other side, don't install to Program Files (due to spaces).
The package manager is at bin/mingw-get.exe.
The MSYS environment can be started via msys\1.0\msys.bat.
Note that we did _not_ add the MinGW bin directory to PATH. (I do not know whether it makes any difference)
BLAS
====
Open MSYS and cd into the extracted package, then:
mkdir build
cd build
cmake-gui
Set paths: Build into the current directory, use sources from parent dir
Configure: Select "MSYS Makefiles" and "Use default native compilers"
Set variables:
BUILD_SHARED_LIBS = TRUE
CMAKE_GNUtoMS = TRUE
CMAKE_INSTALL_PREFIX = X:/lapack (or whatever; can't recall whether the slash really had to be a forward slash)
Configure.
Generate.
Now close CMake and continue in the MSYS prompt:
make
make install
Make sure that you have the following DLLs in your path. I chose to copy them all into X:\torch-luajit-rocks, into which I eventually installed Torch, then added that directory to my PATH.
- All DLLs from X:\lapack\bin\
- The following DLLs from the bin directory of your MinGW installation: libgcc_s_dw2-1.dll, libgfortran-3.dll, libquadmath-0.dll
LuaJIT + LuaRocks
====
We will install from the main Torch repo while following diz-vara's build instructions (mostly). You get LuaJIT 2.1 if you add -DWITH_LUAJIT21=ON to both of the cmake commands below, otherwise it will be LuaJIT 2.0. I went with 2.0 for now, but maybe it would be wiser to use 2.1, after all? Also, the build commands in the instructions produced a debug build of LuaJIT for me, for some odd reason (Torch ran considerably slower with it). -DCMAKE_BUILD_TYPE=Release makes sure that we get a release version.
Choose a directory into which you want to install LuaJIT, LuaRocks and Torch. For me, it was X:\torch-luajit-rocks. Note, however, that the installation process will install stuff also into ..\share, ie, into X:\share in this case; you might want to add an additional directory level to keep things clean. Now, add the directory you chose to your PATH, while making sure that it comes _before_ CMakes bin directory.
Start Cmder and make sure that the VS2015 task is active (see above). Cd into some temporary directory, then:
cd luajit-rocks
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=X:\torch-luajit-rocks -G "NMake Makefiles" -DWIN32=1 -DCMAKE_BUILD_TYPE=Release
nmake
cmake -DCMAKE_INSTALL_PREFIX=X:/torch-luajit-rocks -G "NMake Makefiles" -DWIN32=1 -P cmake_install.cmake -DCMAKE_BUILD_TYPE=Release
Set the following environment variables: (these have been modified a bit from diz-vara's instructions, so as to get some other packages to build)
LUA_CPATH = X:\torch-luajit-rocks\?.DLL;X:\torch-luajit-rocks\LIB\?.DLL;?.DLL
LUA_DEV = X:\torch-luajit-rocks
LUA_PATH = X:\torch-luajit-rocks\?;X:\torch-luajit-rocks\?.lua;X:\torch-luajit-rocks\lua\?;X:\torch-luajit-rocks\lua\?.lua;X:\torch-luajit-rocks\lua\?\init.lua
Test LuaJIT: Completely restart Cmder, then open the Torch task (eg, via the down arrow next to the green plus sign at lower right corner). Try writing some Lua to make sure that the REPL works.
Test LuaRocks: Within Cmder, ctrl-tab back to the VS2015 task and type: luarocks. You should get its help text.
Torch
====
Create the file X:\torch-luajit-rocks\cmake.cmd and add the following content:
if %1 == -E (
cmake.exe %*
) else (
cmake.exe -G "NMake Makefiles" -DWIN32=1 -dLUA_WIN -DCMAKE_LINK_FLAGS:implib=libluajit.lib -DLUALIB=libluajit %*
)
This is from
https://github.com/torch/paths/issues/9 , except
-DLUALIB=libluajit has been added (needed to compile the sys package later on). Double-check that X:\torch-luajit-rocks is in your PATH before CMake's bin directory.
Now, in Cmder with VS2015 task active, cd into some temporary directory and write:
luarocks download torch
For some reason, I didn't manage to have CMake auto-detect LAPACK (nor any other BLAS library, for that matter). I proceeded as follows. Open the downloaded rockspec file and add the following to the configuration command (the one starting with "cmake .. -DCMAKE_BUILD_TYPE=Release -DLUA=$(LUA) [...]"), eg, add right before the "&& $(MAKE)" part:
-DBLAS_LIBRARIES=X:/lapack/lib/libblas.lib -DBLAS_INFO=generic -DLAPACK_LIBRARIES=X:/lapack/lib/liblapack.lib -DLAPACK_FOUND=TRUE
Back in Cmder's VS2015 task:
cd torch7
luarocks make ..\torch-scm-1.rockspec
Test Torch by launching the Torch task from Cmder and then type:
require('torch')
torch.test()
The nn package
====
In Cmder's VS2015 task:
luarocks download luaffi
cd luaffifb
Now open the files ffi.h and test.c, and move the lines
#include <complex.h>
#define HAVE_COMPLEX
up into the preceding #else blocks in both files (the #else branch of the #ifdef _WIN32 test).
Back in Cmder's VS2015 task:
luarocks make ..\luaffi-scm-1.rockspec
Now you should be able to install the nn package. Again in Cmder's VS2015:
luarocks install nn
At least for me, every compilation unit gives me the following kind of warning: warning C4273: 'THNN_FloatLogSigmoid_updateGradInput': inconsistent dll linkage.
Any ideas what this is about and how to avoid it?
Now test it by launching Cmder's Torch task and typing:
require('nn')
nn.test()
All tests should pass.
The trepl package
====
Add the GnuWin32 bin directory to your path now and completely restart Cmder. (note that having this in your path all the time will mess up some earlier Torch installation steps)
In Cmder's VS2015 task:
Edit the rock file and modify the build/platforms/windows/modules/readline section:
incdirs = {"windows","C:/Program Files (x86)/GnuWin32/include"},
libdirs = {"windows","C:/Program Files (x86)/GnuWin32/lib"},
libraries = {'readline'}
cd trepl
luarocks make ../trepl-scm-1.rockspec
Other packages
====
The following packages compiled/installed just fine via luarocks (luarocks install <packagename>). However, I haven't actually tested all of them yet:
luafilesystem, inspect, image, nnx, optim, gnuplot
The sys package: This installs without issues, as long as you have -DLUALIB=libluajit added to your cmake.cmd (see earlier).
The dataset and ipc packages fail non-trivially, due to pthreads dependency.
Versions
====
The preceding process always installs the most recent version of everything. Just in case that something has changed since writing this and broke something, here are the versions that I used or which were downloaded implicitly via rock installs:
ZeroBrane Studio integration
====
The examples worked fine when executed from a script file, but using Torch from the ZBS's REPL did not work well.
For me, starting a debugging session complained about a missing lua51.dll. This was simply a naming issue: ZBS looks for lua51.dll, while we have libluajit.dll. A quick fix was to copy, in X:\torch-luajit-rocks, libluajit.dll to lua51.dll.
WARNING: This will work here, apparently because the dlls are used from different processes, but in general this is probably a bad solution. Managing to load both dlls in a single application would lead to two different address spaces, if I understand correctly, and then to trouble.
Does anyone have ideas for solving this in a better way?
Remote debugging (
https://studio.zerobrane.com/doc-remote-debugging ) seems to work, too, but I didn't really test this beyond a very basic test. The only thing I had to do, in addition to the steps in the instructions, was to copy the file mobdebug.lua from C:\ZeroBraneStudio\lualibs\mobdebug to X:\torch-luajit-rocks\lua.
Phew. Hope this helps anyone facing the same task!
ps. There is still not much information available about running OpenCL or CUDA versions of Torch/BLAS/nn/... on Windows, so if you have had success with those, please do share!