Torch debugging with ZeroBrane Studio

Torch is a powerful computing framework that includes various machine learning algorithms and is built on LuaJIT, so it looked like a natural fit for support in ZeroBrane Studio. Soumith Chintala already did bulk of the work with ZeroBrane Studio clone that included Torch7 interpreter with debugging support, but it required some core changes, so it couldn't be easily combined with ZeroBrane Studio.

As the plugin API was extended with new functions in the most recent versions of the IDE, it became possible to implement the integration with Torch as a plugin, which is now available in the plugin repository.

Let's see how you can debug a Torch script or an application using this plugin.

ZeroBrane Studio configuration.

1. Get ZeroBrane Studio (1.10+). These instructions are for Windows, but the debugging should work on Linux and OSX as well.

2. Get torch plugin and save it to HOME/.zbstudio/packages/ or ZBS/packages/ folder (where ZBS is the path to ZeroBrane Studio location and HOME is the path specified by the HOME environment variable); the first option may also be preferable for Mac OS X users as the packages/ folder may be overwritten during an application upgrade.

3. If you have TORCH_BIN environmental variable defined, you can skip this step; if not, you can open ZeroBrane Studio config file (Edit | Preferences | Settings: User) and set path.torch value pointing to the th executable or the directory with the torch library (libtorch);for example, path.torch = [[D:/torch7/]]. You'll need to restart the IDE for the configuration changes to have effect.

4. Start ZeroBrane Studio (zbstudio.exe or zbstudio.sh).

Script debugging.

We can now debug a simple Torch script:

local torch = require 'torch'
local data = torch.Tensor{
  {68, 24, 20},
  {74, 26, 21},
  {80, 32, 24},
}
print(data)

Save it as test.lua and set the project directory to the folder where you saved the script (Project | Project Directory | Set From Current File) and select Torch as the current interpreter by going to Project | Lua Interpreter | Torch-7 (this option is only available when the Torch plugin is installed).

Now if you Run the script, you should see the following output:

 68  24  20
 74  26  21
 80  32  24
[torch.DoubleTensor of size 3x3]

If you select Debug, you should see the debugging started at line 1 and should be able to step through the script, set breakpoints, look at the stack trace, and use all other debugging functionality available in ZeroBrane Studio. If you set a breakpoint on line 7 and run to that line, you should see something similar to the following screenshot:

Torch7 debugging

For those of you familiar with ZBS-torch, the plugin provides similar functionality, but implements visualization of Torch elements in a way similar to how it's done in Torch REPL and also removes backspaces in Torch output that is shown in the Output window.

You should get a copy of my slick ZeroBrane Studio IDE and follow me on twitter here.

16 Comments

Does ZeroBrane Studio support auto-completion for 'torch'? Do I need to change some configurations to enable the feature?

@Amy, not yet; I discussed the details with Torch authors, but haven't finished processing and adding the descriptions yet. I'm curious, what you first be interested in the core module or any particular submodules?

I find that while debugging Torch code in ZBS, there's often a long delay (sometimes up to a minute or two) between when the code runs up to a breakpoint (the green arrow appears at the breakpoint) and the debug command line in the docked "Remote Console" becomes usable. This is not true for all breakpoints, just a few.

Until this point, the output window at the bottom continues to show "(running)", and all the debug-related buttons (like the step-in, step-over, etc.) on the top bar remain grayed out. Only after "(running)" turns to "(suspended)" does the remote console start working.

Is this a known issue? I'm running Ubuntu 14.04.

@Paul Kulchenko I am learning deep learning with torch.nn. It would be cool if auto-completion for torch.nn is supported. ZeroBrane Studio is awesome. :)

Inside sbstudio, I get the following error

Program starting as '"/home/tempuser/torch/install/bin/luajit" -e "io.stdout:setvbuf('no')" "/home/tempuser/neuraltalk2/eval.lua" -gpuid -1 -model /home/tempuser/neuraltalk2-model/modelid1-501-1448236541.t7cpu.t7 -imagefolder /home/tempuser/neuraltalk2-test/ -numimages 3'. Program 'luajit' started in '/home/tempuser/neuraltalk2' (pid: 11869). Lua 5.1 Loading model... /home/tempuser/neuraltalk2-model/modelid1-501-1448236541.t7cpu.t7 Done. DataLoaderRaw loading images from folder: /home/tempuser/neuraltalk2-test/ listing all images in directory /home/tempuser/neuraltalk2-test/ /home/tempuser/torch/install/bin/luajit:

#

./misc/DataLoaderRaw.lua:45: attempt to index global 'path' (a nil value)

#

stack traceback: ./misc/DataLoaderRaw.lua:45: in function '__init' /home/tempuser/torch/install/share/lua/5.1/torch/init.lua:91: in function [C]: in function 'DataLoaderRaw' /home/tempuser/neuraltalk2/eval.lua:92: in main chunk [C]: at 0x00406670 Program completed in 0.59 seconds (pid: 11869).

Please ignore my post (above).

sbstudio is working as expected.

I had to select the torch7 interpreter instead of the lua interpreter.

HI, I would like to ask some question about the setting in MAC OSX

I download the "ZeroBranePackage/*.lua" and put all of them into my application folder

/Applications/ZeroBraneStudio.app/Contents/ZeroBraneStudio/packages/

and then I set

path.torch = [[/Users/shamangary/torch/pkg/torch/build/]]

in user.lua. The above path is the location of libtorch.so.

And then I restart the IDE and set the Interpreter to Torch7, but error still occurs.

Plz help me with this. Thank you very much!

Program starting as '"/Applications/ZeroBraneStudio.app/Contents/ZeroBraneStudio/bin/lua.app/Contents/MacOS/lua" "/var/folders/v7/sfntqnx6q1bmtdxm2phx7wr0000gn/T/.QmvZR4" '. Program 'lua' started in '/Volumes/Transcend/Torch project' (pid: 87747). /Applications/ZeroBraneStudio.app/Contents/ZeroBraneStudio/bin/lua.app/Contents/MacOS/lua: error loading module 'socket.core' from file '/usr/local/lib/lua/5.1/socket/core.so': dlopen(/usr/local/lib/lua/5.1/socket/core.so, 6): no suitable image found. Did find: /usr/local/lib/lua/5.1/socket/core.so: mach-o, but wrong architecture stack traceback: [C]: at 0x00091340 [C]: in function 'require' /usr/local/share/lua/5.1/socket.lua:12: in main chunk [C]: in function 'require' /usr/local/share/lua/5.1/mobdebug.lua:77: in main chunk [C]: in function 'require' /var/folders/v7/sfntqnx6q1bmtdxm2phx7wr0000gn/T/.QmvZR4:1: in main chunk [C]: at 0x00001c80 Program completed in 0.04 seconds (pid: 87747).

@TYY, this probably happens because the lua executable included with the IDE is 32-bit application, but luasocket library you have installed at '/usr/local/lib/lua/5.1/socket/core.so' is 64-bit binary, hence "mach-o, but wrong architecture" error you get.

The solution is to use the system lua version by adding path.lua = '/usr/local/bin/lua' (or whatever else the full path to lua5.1 executable is on your machine).

See this FAQ answer for more details: https://studio.zerobrane.com/doc-faq#why-am-i-getting-mach-o-but-wrong-architecture-error-on-osx

@auro, that's correct; thank you for the update.

path.lua = '/usr/local/share/lua/5.1/'

After I add this line to the user.lua ,the following error occurs.

Program starting as '"/usr/local/share/lua/5.1/" "/var/folders/v7/sfntqnx6q1bmtdxm2phx7wr0000gn/T/.UAxJZO" '. Program '"' started in '/Volumes/Transcend/Torch project' (pid: 88773). execvp(/usr/local/share/lua/5.1/, /var/folders/v7/sfntqnx6q1bmtdxm2phx7wr0000gn/T/.UAxJZO) failed with error 13! Program completed in 0.00 seconds (pid: 88773).

@TTY, you only specified the path to the executable, but you need to include the name of the executable as well, so use '/usr/local/share/lua/5.1/lua' instead of '/usr/local/share/lua/5.1/'.

Program starting as 'bash -c "cd $(dirname /home/xuenan/draw-master/draw-master/drawattention.lua) && /home/xuenan/torch/install/bin/th /home/xuenan/draw-master/draw-master/drawattention.lua "'. Program 'bash' started in '/home/xuenan/draw-master/draw-master' (pid: 7292). Could not connect to localhost:8172: connection refused /home/xuenan/torch/install/bin/luajit: ...e/xuenan/torch/install/share/lua/5.1/nngraph/gmodule.lua:176: node declared on [/home/xuenan/draw-master/draw-master/drawattention.lua]:40 does not connect to gmodule output stack traceback: [C]: in function 'assert' ...e/xuenan/torch/install/share/lua/5.1/nngraph/gmodule.lua:176: in function '_init' /home/xuenan/torch/install/share/lua/5.1/torch/init.lua:91: in function [C]: in function 'gModule' /home/xuenan/draw-master/draw-master/drawattention.lua:125: in main chunk [C]: in function 'dofile' ...enan/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:145: in main chunk [C]: at 0x00406620 I think maybe I met the same problem with you,but I have changed Lua interpreter to be torch7,it did not work? Do you know the solution? Thank you for your help.

Great work! Is there a way to save the 'command line parameters' for a project?

Anton, yes, this functionality will be available in the coming v1.40. Command line parameters will be saved on a per-project basis and can be toggled directly from the status bar.

Nice work Paul, ZeroBrane is great. What I wonder is how did you managed to install Torch7 in Windows? :)

Fatih Mehmet, I compiled it myself using mingw. I submitted a pull request that fixes mingw compilation on Windows, which has been merged. See this SO link for details: http://stackoverflow.com/a/28058692/1442917

Leave a comment

what will you say?
(required)
(required)
Close