clementfarabet / torch-ios

Torch7 for iOS.
Other
196 stars 42 forks source link

Adding other (Lua-only) libraries? #30

Open lazerwalker opened 8 years ago

lazerwalker commented 8 years ago

Hi!

I'm trying to add some third-party libraries that have no C code or cmake compilation step, only Lua code (in my case: nngraph, optim, and their subdependencies, so I can sample a pre-trained char-rnn dataset).

Given that they don't have C code and thus don't generate static libraries, the only thing I've figured out to do with them is dump them in my /framework/lua folder, and load them as appropriate in Torch.m, as well as evaluate their init.lua files at runtime (see this gist, modified from this example repo).

A minor problem with this is that it requires I manually go into each init.lua file (and a few others) and manually wrap require calls in if foo == nil checks (as recommended in this blog post associated with the example repo above). Not a deal-breaker, but still feels like a code smell I'm taking the wrong approach.

More crucially, one library that requires xlua can't find it, despite the already-included image library being able to reference it just fine. Going down the rabbithole of manually including and initializing xlua at runtime (same deal, adding it to /framework/lua within the iOS project, and loading it within Torch.m and my TorchInterface.m), and then doing that recursively for each of its dependencies, eventually leads me to needing to include the paths library, which gives me compilation errors when I try to include it as part of the generate_ios_framework script.

Not being familiar with CMake or anything surrounding Lua (its require/package system, embedding it within iOS, etc), I'm very clearly in over my head. My instinct is that trying to get paths to build is too far deep in the yak shave, and there's an overall cleaner approach I should be taking, but darned if I know what it is.

It seems like this repo is relatively dormant, but figured I'd put a call out in case any of the maintainers or anyone else stumbling onto this has any thoughts or ideas.

Thanks!

EliseSo commented 7 years ago

Hi @lazerwalker, can you use the run_torch_script function to require module successfully? I followed your instructions and added the code as the gist, but I came with the error message like this: Error in Torch Script: ...D0/Torch7iOSExample.app/framework/lua/optim/init.lua:2: module 'torch' not found: no field package.preload['torch'] no file '/torch.lua' no file '/Users/~/Library/Developer/CoreSimulator/Devices/2927F705-D406-472B-A296-A04543B68394/data/Containers/Bundle/Application/4AF58CCA-ADB2-4AD4-9971-4BB010F5B7D0/Torch7iOSExample.app/framework/lua/torch.lua' no file './torch.lua' no file '//share/lua/5.1/torch.lua' no file '//share/lua/5.1/torch/init.lua' no file '//lib/lua/5.1/torch.lua' no file '//lib/lua/5.1/torch/init.lua' no file './torch.so' no file './torch.dylib' no file '//lib/lua/5.1/torch.so' no file '//lib/lua/5.1/torch.dylib' no file '//lib/lua/5.1/loadall.so//lib/lua/5.1/loadall.dylib' Almost every added lua script would throw an error message expect framework/lua/torch/init.lua

Can you give me some advice to solve the problem? Thank you!