Torch on Windows

2,125 views
Skip to first unread message

SpaceCowboy850

unread,
Jul 13, 2016, 4:19:44 PM7/13/16
to torch7
I thought I'd write down my notes of what I did to get Torch compiling on Windows.  Hopefully this can be of use to others.  The information is out there, so this sort of cobbles together what I've found from various different sources.  

Note, I'm still having trouble getting cunn to work in Windows, so if anyone has solved this, please reply below.

Prerequisites:

* Visual Studio 2013 (It should work with other versions, but this is what I used)
   * Make sure cmake is on your path.  I had to reboot for this to take effect.  Put the cmake bin folder at the front of your path, as there is another cmake.cmd in some of diz-vara's stuff that can cause oddities.

Anti-Requisites?

I had MinGW installed at first, and if you have this as well, be aware that it might screw up some of these steps, as depending on where it is in your path, some of its compiler tools are named the same as the msbuild compiler tools.  
Likewise, if you already have Lua or Python installed, they might screw with this setup as well.  Just be aware of the variables that are set.

The Process:

For most of these steps, I recommend using the Visual Studio Command Prompt as it will have the environment variables set up to use msbuild properly that 
the normal command prompt won't.  For VS2013, this can be found here by default:

C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\Shortcuts

You will be downloading a number of Git repositories to compile them correctly for Windows or to fix issues before they can compile.
I've found it easiest to do this in one location.  I'll put these in a location callled C:\TorchDownloads\, feel free to use whatever directory
you like.

Installing diz-vara's Luajit-rocks branch, torch and nnx

At the command line, type

cd TorchDownloads

The instructions follow from a branch of Torch set up here:

As such, it is unclear how up to date this branch is.  You can follow the instructions there, or just follow the instructions below.
 
Type:

cd luajit-rocks
mkdir build
cd build

From here, I'll be using C:\Torch to denote the actual torch directory, and C:\TorchDownloads\ for our intermidiary files will be downloading to 
fix and install.

cmake .. -DCMAKE_INSTALL_PREFIX=C:\Torch -DWITH_LUAJIT21=ON -G "NMake Makefiles"  -DWIN32=1
nmake
cmake  -DCMAKE_INSTALL_PREFIX=C:\Torch -DWITH_LUAJIT21=ON -G "NMake Makefiles"  -DWIN32=1 -P cmake_install.cmake

You will now need to create or update the following environment variables:

set LUA_CPATH=C:\Torch?.DLL;C:\Torch\LIB\?.DLL;?.DLL
set LUA_DEV=C:\Torch
set LUA_PATH=;;C:\Torch\?;C:\Torch\lua\?\init.lua;C:\Torch\?.lua

From a new command line (to ensure the environment variables are set), switch to the Torch directory,

cd Torch
luarocks install torch


Type:
luarocks install nnx

This should install nnx without issue.

Installing NN
If you type:
luarocks install nn
You will likely get a number of compile errors in luaffi.  We will need to get the repository, fix up a compile error and install it ourselves

Switch to your TorchDownload directory, then type
 
..\Torch\luarocks download luaffi

Open the luaffi-scm-1.rockspec that has been downloaded in your favorite text editor, and get the github line out of there, and at the
command line, type:


Now, we need to modify it.  you can read how at https://github.com/facebook/luaffifb/issues/10 or you keep reading:

Open ffi.h and test.c and move the 
#include <complex.h>
#define HAVE_COMPLEX 

into the #else block right above it in both files

Now Change directory into the luaffifb directory and type:

c:\TorchDownloads\luaffifb>..\..\Torch\luarocks make luaffi-scm-1.rockspec

And this will compile and install the rockspec that the repository has with the changes we have made.  It is important that you run this
on the repository's rockspec, and not the one we downloaded with luarocks, as one will build from the local source, and the other will get
from the internet.

At this point luaffi should be installed.

Go back to C:\Torch and type
 
luarocks install nn

And NN should install.

Testing it Out: 
 
At this point you should have a functioning non-Cuda Torch install on Windows.  You can test this by opening up your torch directory and typing:

luajit

to open up a torch command line.  Then at that command line type:

th> require 'torch'
th> t = torch.Tensor{1,2,3} 
th> print( t )
 1
 2 
 3
th>

If this doesn't work, go back and check your environment variables to make sure your LUA_CPATH, LUA_DEV, and LUA_PATH lines are correct.


I have attached a BasicNN.lua that is derived from here:

However, the problem you are going to have is that the binaries that it wants to download will not load in Windows.  I was lucky and able to do this on a Unix machine, the save them as ascii, and load the ascii versions on Windows.  Each file is about 30MB, so I can't upload it to the thread.

If you do manage to get a txt version of those files, you'll also need to go here:

C:\Torch\lua\nn\init.lua

and comment out the line 

require('nn.test')

As this version of Torch is missing TestSuite.

to comment it out.

If you have all three files, you can run the BasicNN.lua like:

C:\Torch>luajit basicNN.lua

At this point, you should be able to take this basicNN.lua and be able to run it in a non-cuda powered mode in Windows by typing

c:\Torch>luajit basicNN.lua

What the NN is doing is taking a small image database, classifying it into 10 categories (horse, car, dog, truck, etc...), and then given 
an image tells you what it thinks it is.  Much more impressive with images turned on, but as a quick gut check fully formed NN, it works
for our purposes.

Hopefully this is useful to someone.  The information is out there, but I found it was sort of scattered, so I'm trying to condense it in one spot here.  If this is useful, I'll try to keep it updated as I figure out how to get CUDA working and get closer to main line.


BasicNN.lua

soumith

unread,
Jul 15, 2016, 7:22:16 AM7/15/16
to torch7 on behalf of SpaceCowboy850
This is really nicely consolidated! Thanks a ton for writing this down.

--
You received this message because you are subscribed to the Google Groups "torch7" group.
To unsubscribe from this group and stop receiving emails from it, send an email to torch7+un...@googlegroups.com.
To post to this group, send email to tor...@googlegroups.com.
Visit this group at https://groups.google.com/group/torch7.
For more options, visit https://groups.google.com/d/optout.

SpaceCowboy850

unread,
Jul 15, 2016, 3:02:02 PM7/15/16
to torch7
Thanks for the kind words.  It seems somewhat difficult to get information on Windows, so I thought I'd try to give back as much as I'm able.


On Friday, July 15, 2016 at 6:22:16 AM UTC-5, smth chntla wrote:
This is really nicely consolidated! Thanks a ton for writing this down.

Marti

unread,
Jul 16, 2016, 8:14:50 AM7/16/16
to torch7
Great great work!

Just one quick thing, have you also run the test by Torch and nn packge?

Can try the following two commands:

th> torch.test()
th> nn.test()

Are all tests passed on your Windows machine?  

Thanks!

SpaceCowboy850

unread,
Jul 18, 2016, 11:52:15 AM7/18/16
to torch7
Looks like there are about 26 errors with the torch.test(), which is not terribly surprising, since I don't know how old/out of sync diz-vara's branch is that I'm using.  One thing I want to do is go back and see what code changes were needed for his original branch and do something similar off of the main torch7 branch.

nn.test won't run because TestSuite() doesn't exist in this version of Torch (which again, known issue that needs to be dealt with).

Andreas Kölsch

unread,
Aug 5, 2016, 9:47:23 AM8/5/16
to torch7
So, I tried it as well, but have a problem at the following step:
 
cd Torch
luarocks install torch



C:\Torch>rem=rem --[[
Installing https://raw.githubusercontent.com/diz-vara/rocks/master/torch-scm-1.r
ockspec
...
Using https://raw.githubusercontent.com/diz-vara/rocks/master/torch-scm-1.rocksp
ec
... switching to 'build' mode


Missing dependencies for torch:
paths
>= 1.0
cwrap
>= 1.0


Using https://raw.githubusercontent.com/torch/rocks/master/paths-scm-1.rockspec.
.. switching to 'build' mode
Der Befehl ""git"" ist entweder falsch geschrieben oder
konnte nicht gefunden werden
.


Error: Failed installing dependency: https://raw.githubusercontent.com/torch/roc
ks
/master/paths-scm-1.rockspec - Failed cloning git repository.


So, it says, git cannot be found. However, if I just type git into the same console, it runs the git command... Any help on this is appreciated

Paul Wagner

unread,
Aug 29, 2016, 3:14:05 AM8/29/16
to torch7
Did you have some BLAS library installed while compiling Torch? At least for me, not having BLAS resulted in torch.test() failing for around that many tests. The errors disappeared after installing a BLAS lib and recompiling Torch (notes on compiling with LAPACK here: https://groups.google.com/d/topic/torch7/iQpNfJB_oy0/discussion ).

BTNC

unread,
Oct 27, 2016, 11:55:39 AM10/27/16
to torch7
I wrote up a customized torch/distro for windows with msvc that can install the whole pack of torch. Please take a try on your need and let me know if it does not work for you.

在 2016年8月29日星期一 UTC+8下午3:14:05,Paul Wagner写道:

宝宝

unread,
Mar 4, 2017, 4:16:09 AM3/4/17
to torch7
Hi, I use you branch, and it works well, but I also have trouble with installing the CUNN even I try to install from source.
Reply all
Reply to author
Forward
0 new messages