通过共享库接口Python和Torch7(Lua)

4 投票
3 回答
4630 浏览
提问于 2025-04-18 13:03

我正在尝试在 Python 和 Lua 之间传递数据(数组),并希望在 Lua 中使用 Torch7 框架来处理这些数据。 我发现通过 C 语言来实现这个功能是最好的选择,因为 Python 和 Lua 都可以与 C 进行交互。此外,这种方式不需要复制数据(只传递指针),速度也很快。

我实现了两个程序,一个是将 Lua 嵌入到 C 中,另一个是 Python 将数据传递给 C。 这两个程序在编译成可执行文件时都能正常工作。 但是,当我将 C 到 Lua 的程序做成共享库时,就出现了问题。

具体情况是: 我使用的是 64 位的 Ubuntu 14.04 和 12.04。 我安装了 luajit 2.0.2 和 Lua 5.1,路径在 /usr/local/。 依赖的库在 /usr/local/lib,头文件在 /usr/local/include。 我使用的是 Python 2.7。

下面是 C 到 Lua 程序的代码:

tensor.lua

require 'torch'

function hi_tensor(t)
   print(‘Hi from lua')
   torch.setdefaulttensortype('torch.FloatTensor')
   print(t)
return t*2
end

cluaf.h

void multiply (float* array, int m, int n, float *result, int m1, int n1);

cluaf.c

#include <stdio.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "luaT.h"
#include "TH/TH.h"

void multiply (float* array, int m, int n, float *result, int m1, int n1)
{
    lua_State *L = luaL_newstate();
    luaL_openlibs( L );

    // loading the lua file
    if (luaL_loadfile(L, "tensor.lua") || lua_pcall(L, 0, 0, 0))
    {
        printf("error: %s \n", lua_tostring(L, -1));
    }

    // convert the c array to Torch7 specific structure representing a tensor
    THFloatStorage* storage =  THFloatStorage_newWithData(array, m*n);
    THFloatTensor* tensor = THFloatTensor_newWithStorage2d(storage, 0, m, n, n, 1);
    luaT_newmetatable(L, "torch.FloatTensor", NULL, NULL, NULL, NULL);

    // load the lua function hi_tensor
    lua_getglobal(L, "hi_tensor");
    if(!lua_isfunction(L,-1))
    {
        lua_pop(L,1);
    }

    //this pushes data to the stack to be used as a parameter
    //to the hi_tensor function call
    luaT_pushudata(L, (void *)tensor, "torch.FloatTensor");

    // call the lua function hi_tensor
    if (lua_pcall(L, 1, 1, 0) != 0)
    {
        printf("error running function `hi_tensor': %s \n", lua_tostring(L, -1));
    }

    // get results returned from the lua function hi_tensor
    THFloatTensor* z = luaT_toudata(L, -1, "torch.FloatTensor");
    lua_pop(L, 1);
    THFloatStorage *storage_res =  z->storage;
    result = storage_res->data;

    return ;
}

然后我进行测试:

luajit -b tensor.lua tensor.o

gcc -w -c -Wall -Wl,-E -fpic cluaf.c -lluajit -lluaT -lTH -lm -ldl -L /usr/local/lib

gcc -shared cluaf.o tensor.o -L/usr/local/lib -lluajit -lluaT -lTH -lm -ldl -Wl,-E -o libcluaf.so

gcc -L. -Wall -o test main.c -lcluaf

./test

输出结果是:

Hi from lua
 1.0000  0.2000
 0.2000  5.3000
[torch.FloatTensor of dimension 2x2]

c result 2.000000 
c result 0.400000 
c result 0.400000 
c result 10.60000

到目前为止一切正常。 但是当我尝试在 Python 中使用这个共享库时,就出现了问题。

test.py

from ctypes import byref, cdll, c_int
import ctypes
import numpy as np
import cython

l = cdll.LoadLibrary(‘absolute_path_to_so/libcluaf.so')

a = np.arange(4, dtype=np.float64).reshape((2,2))
b = np.arange(4, dtype=np.float64).reshape((2,2))

l.multiply.argtypes = [ctypes.POINTER(ctypes.c_float), ctypes.c_int, ctypes.c_int,     ctypes.POINTER(ctypes.c_float), ctypes.c_int, ctypes.c_int]
a_list = []
b_list = []

for i in range(a.shape[0]):
    for j in range(a.shape[1]):
            a_list.append(a[i][j])

for i in range(b.shape[0]):
     for j in range(b.shape[1]):
        b_list.append(b[i][j])

arr_a = (ctypes.c_float * len(a_list))()
arr_b = (ctypes.c_float * len(b_list))()

l.multiply(arr_a, ctypes.c_int(2), ctypes.c_int(2), arr_b, ctypes.c_int(2), ctypes.c_int(2))

我运行:

python test.py

输出结果是:

error: error loading module 'libpaths' from file '/usr/local/lib/lua/5.1/libpaths.so':
    /usr/local/lib/lua/5.1/libpaths.so: undefined symbol: lua_gettop

我在这里和网上到处搜索这个错误,但他们要么建议 (1) 加上 -Wl,-E 来导出符号,要么 (2) 在链接时添加依赖项,我都做了。 (1) 我有加 -Wl,-E,但似乎没有任何效果。 (2) 我已经包含了依赖项(-L/usr/local/lib -lluajit -lluaT -lTH -lm -ldl)。

Python 测试失败的原因不是因为共享库导入失败,而是在 Lua 中调用 ‘require torch’ 时出错。这也是我发现的与其他情况不同的地方。

luajit.so 定义了符号 lua_gettop(可以用 nm /usr/local/lib/luajit.so 查看)。 lua.h 定义了 LUA_API int (lua_gettop) (lua_State *L);

我猜在编译 C 成二进制文件时一切正常,因为它能找到 lua.h 中的所有符号,但使用共享库时却无法从 luajit.so 中找到 lua_gettop(我不知道为什么)。

www.luajit.org/running.html 上说: '在大多数基于 ELF 的系统(例如 Linux)上,链接应用程序时需要显式导出全局符号,例如使用:-Wl,-E。 require() 尝试从导出的符号(在 Windows 上是 *.exe 或 lua51.dll)和共享库中加载嵌入的字节码数据,路径在 package.cpath 中。'

package.cpath 和 package.path 是:

./?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so

./?.lua;/usr/local/share/luajit-2.0.2/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua

这是 nm libcluaf.so 返回的结果:

00000000002020a0 B __bss_start
00000000002020a0 b completed.6972
                 w __cxa_finalize@@GLIBC_2.2.5
0000000000000a50 t deregister_tm_clones
0000000000000ac0 t __do_global_dtors_aux
0000000000201dd8 t __do_global_dtors_aux_fini_array_entry
0000000000202098 d __dso_handle
0000000000201de8 d _DYNAMIC
00000000002020a0 D _edata
00000000002020a8 B _end
0000000000000d28 T _fini
0000000000000b00 t frame_dummy
0000000000201dd0 t __frame_dummy_init_array_entry
0000000000000ed0 r __FRAME_END__
0000000000202000 d _GLOBAL_OFFSET_TABLE_
                 w __gmon_start__
0000000000000918 T _init
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000201de0 d __JCR_END__
0000000000201de0 d __JCR_LIST__
                 w _Jv_RegisterClasses
                 U lua_getfield
0000000000000d99 R luaJIT_BC_tensor
                 U luaL_loadfile
                 U luaL_newstate
                 U luaL_openlibs
                 U lua_pcall
                 U lua_settop
                 U luaT_newmetatable
                 U lua_tolstring
                 U luaT_pushudata
                 U luaT_toudata
                 U lua_type
0000000000000b35 T multiply
                 U printf@@GLIBC_2.2.5
0000000000000a80 t register_tm_clones
                 U THFloatStorage_newWithData
                 U THFloatTensor_newWithStorage2d
00000000002020a0 d __TMC_END__

提前谢谢你!

3 个回答

0

lua_gettop是一个在Lua的.so文件中定义的函数,在你的情况下应该是luajit.so。看起来你把你的库链接到了它,这很好,然后你又把主程序链接到了你的库,所以可以推测C编译器在luajit中找到了主程序用到的Lua函数。到这里为止都没问题。

现在,当你通过ctypes在Python中加载你的库时,luajit库会自动加载吗?你可能会期待它会自动加载,但你需要确认一下,可能你需要告诉ctypes去加载链接的库。还有一种可能是ctypes或者库加载器找不到luajit,可能是因为它在一些不包含luajit的地方查找。为了确保这一点,你可以尝试把所有的库放在你调用Python的同一个文件夹里。

如果这样还不行,试试你之前尝试过的另一种方法:不要在Python中加载你的模块,直接使用ctypes加载luajit,然后尝试调用它的一些方法。

1

如果你想在python/numpy和lua/torch之间交换数据,可以试试一个叫做 "lutorpy" 的库。这个库正好可以实现你想要的功能,它可以共享内存,只需要用 "asNumpyArray()" 方法传递指针就可以了。

import lutorpy as lua
import numpy as np

## run lua code in python with minimal modification:  replace ":" to "._"
t = torch.DoubleTensor(10,3)
print(t._size()) # the corresponding lua version is t:size()

## convert torch tensor to numpy array
### Note: the underlying object are sharing the same memory, so the conversion is instant
arr = t.asNumpyArray()
print(arr.shape)

## or, you can convert numpy array to torch tensor
xn = np.random.randn(100)
## convert the numpy array into torch tensor
xt = torch.fromNumpyArray(xn)
2

在Linux系统上,Lua模块并不是直接链接到Lua库,而是希望能找到已经加载的Lua API函数。通常,这是通过在解释器中使用 -Wl,-E 这个链接器标志来实现的。这个标志只对可执行文件中的符号有效,而对共享库则不适用。对于共享库,有类似的东西:就是 RTLD_GLOBAL 标志,它用于 dlopen 函数。默认情况下,编译命令行中列出的所有共享库都是使用 RTLD_LOCAL 加载的,但幸运的是,Linux会重用已经打开的库句柄。所以你可以选择:

在Lua(JIT)库被自动加载之前,使用 RTLD_GLOBAL 预加载它(自动加载发生在你加载 libcluaf.so 时):

from ctypes import byref, cdll, c_int
import ctypes

lualib = ctypes.CDLL("libluajit-5.1.so", mode=ctypes.RTLD_GLOBAL)
l = cdll.LoadLibrary('absolute_path_to_so/libcluaf.so')
# ...

或者在之后使用 RTLD_NOLOAD 标志来改变Lua(JIT)库句柄的标志,方法是调用 dlopen。不过,这个标志不在POSIX标准中,你可能需要用C语言来实现。具体可以参考 这里

撰写回答