Skip to content

Commit

Permalink
Merge pull request #13 from timharley/thgenerator
Browse files Browse the repository at this point in the history
Make random number generator state a TH type
  • Loading branch information
andresy committed Mar 20, 2014
2 parents 86f62cf + c860cf1 commit 015eee4
Show file tree
Hide file tree
Showing 12 changed files with 284 additions and 155 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}/lib/TH")
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/lib/luaT")
LINK_DIRECTORIES("${LUA_LIBDIR}")

SET(src DiskFile.c File.c MemoryFile.c PipeFile.c Storage.c Tensor.c Timer.c utils.c init.c TensorOperator.c TensorMath.c random.c)
SET(src DiskFile.c File.c MemoryFile.c PipeFile.c Storage.c Tensor.c Timer.c utils.c init.c TensorOperator.c TensorMath.c random.c Generator.c)
SET(luasrc init.lua File.lua Tensor.lua CmdLine.lua Tester.lua test/test.lua)

# Necessary do generate wrapper
Expand Down
29 changes: 29 additions & 0 deletions Generator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <general.h>

static const struct luaL_Reg torch_Generator_table_ [] = {
{NULL, NULL}
};

int torch_Generator_new(lua_State *L)
{
THGenerator *gen = THGenerator_new();
luaT_pushudata(L, gen, torch_Generator);
return 1;
}

int torch_Generator_free(lua_State *L)
{
THGenerator *gen= luaT_checkudata(L, 1, torch_Generator);
THGenerator_free(gen);
return 0;
}

#define torch_Generator_factory torch_Generator_new

void torch_Generator_init(lua_State *L)
{
luaT_newmetatable(L, torch_Generator, NULL,
torch_Generator_new, torch_Generator_free, torch_Generator_factory);
luaL_register(L, NULL, torch_Generator_table_);
lua_pop(L, 1);
}
45 changes: 30 additions & 15 deletions TensorMath.lua
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ for _,Tensor in ipairs({"ByteTensor", "CharTensor",
string.format("TH%s_add(%s, %s, 1);", Tensor, arg:carg(), arg:carg())
}, '\n')
end},
{name="Generator", default=true},
{name="long"}})

wrap("sort",
Expand Down Expand Up @@ -470,64 +471,72 @@ for _,Tensor in ipairs({"ByteTensor", "CharTensor",
if Tensor == 'ByteTensor' then -- we declare this only once
interface:print(
[[
static int THRandom_random2__(long a, long b)
static int THRandom_random2__(THGenerator *gen, long a, long b)
{
THArgCheck(b >= a, 2, "upper bound must be larger than lower bound");
return((THRandom_random() % (b+1-a)) + a);
return((THRandom_random(gen) % (b+1-a)) + a);
}
static int THRandom_random1__(long b)
static int THRandom_random1__(THGenerator *gen, long b)
{
THArgCheck(b > 0, 1, "upper bound must be strictly positive");
return(THRandom_random() % b + 1);
return(THRandom_random(gen) % b + 1);
}
]])
end

interface:print(string.gsub(
[[
static void THTensor_random2__(THTensor *self, long a, long b)
static void THTensor_random2__(THTensor *self, THGenerator *gen, long a, long b)
{
THArgCheck(b >= a, 2, "upper bound must be larger than lower bound");
TH_TENSOR_APPLY(real, self, *self_data = ((THRandom_random() % (b+1-a)) + a);)
TH_TENSOR_APPLY(real, self, *self_data = ((THRandom_random(gen) % (b+1-a)) + a);)
}
static void THTensor_random1__(THTensor *self, long b)
static void THTensor_random1__(THTensor *self, THGenerator *gen, long b)
{
THArgCheck(b > 0, 1, "upper bound must be strictly positive");
TH_TENSOR_APPLY(real, self, *self_data = (THRandom_random() % b + 1);)
TH_TENSOR_APPLY(real, self, *self_data = (THRandom_random(gen) % b + 1);)
}
]], 'Tensor', Tensor):gsub('real', real))

wrap('random',
'THRandom_random2__',
{{name='long'},
{{name='Generator', default=true},
{name='long'},
{name='long'},
{name='long', creturned=true}},
'THRandom_random1__',
{{name='long'},
{{name='Generator', default=true},
{name='long'},
{name='long', creturned=true}},
'THRandom_random',
{{name='long', creturned=true}},
{{name='Generator', default=true},
{name='long', creturned=true}},
cname("random2__"),
{{name=Tensor, returned=true},
{name='Generator', default=true},
{name='long'},
{name='long'}},
cname("random1__"),
{{name=Tensor, returned=true},
{name='Generator', default=true},
{name='long'}},
cname("random"),
{{name=Tensor, returned=true}})
{{name=Tensor, returned=true},
{name='Generator', default=true}})

for _,f in ipairs({{name='geometric'},
{name='bernoulli', a=0.5}}) do

wrap(f.name,
string.format("THRandom_%s", f.name),
{{name="double", default=f.a},
{{name='Generator', default=true},
{name="double", default=f.a},
{name="double", creturned=true}},
cname(f.name),
{{name=Tensor, returned=true},
{name='Generator', default=true},
{name=real, default=f.a}})
end

Expand Down Expand Up @@ -846,11 +855,13 @@ static void THTensor_random1__(THTensor *self, long b)
wrap("rand",
cname("rand"),
{{name=Tensor, default=true, returned=true, method={default='nil'}},
{name='Generator', default=true},
{name="LongArg"}})

wrap("randn",
cname("randn"),
{{name=Tensor, default=true, returned=true, method={default='nil'}},
{name='Generator', default=true},
{name="LongArg"}})

for _,f in ipairs({{name='uniform', a=0, b=1},
Expand All @@ -860,11 +871,13 @@ static void THTensor_random1__(THTensor *self, long b)

wrap(f.name,
string.format("THRandom_%s", f.name),
{{name="double", default=f.a},
{{name='Generator', default=true},
{name="double", default=f.a},
{name="double", default=f.b},
{name="double", creturned=true}},
cname(f.name),
{{name=Tensor, returned=true},
{name='Generator', default=true},
{name=real, default=f.a},
{name=real, default=f.b}})
end
Expand All @@ -873,10 +886,12 @@ static void THTensor_random1__(THTensor *self, long b)

wrap(f.name,
string.format("THRandom_%s", f.name),
{{name="double", default=f.a},
{{name='Generator', default=true},
{name="double", default=f.a},
{name="double", creturned=true}},
cname(f.name),
{{name=Tensor, returned=true},
{name='Generator', default=true},
{name=real, default=f.a}})
end

Expand Down
Loading

19 comments on commit 015eee4

@clementfarabet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this commit change the Lua signature of all the random functions?

I'm getting segfaults (OSX) after that commit. I still can't pinpoint the exact reason though.

@koraykv
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Himm, I just ran torch.test and nn.test on mac with this change and did not get anything. This changes the Lua side signatures, but there is a default generator that is passed, so it is backward compatible in Lua. However, it is not backward compatible in C. Maybe you had a package that used the old C THRandom interface?

@clementfarabet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, on OSX, before this commit, everything's fine, after, this segfaults for me:

torch.randn(10)

@andresy
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't. Are you sure you do not have some old TH include files lying around?

@clementfarabet
Copy link
Member

@clementfarabet clementfarabet commented on 015eee4 Mar 28, 2014 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clementfarabet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, same thing :-(.

@soumith
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me check on my OSX.

@soumith
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely dont see it on linux.

@clementfarabet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also using the latest LuaJIT from https://github.com/torch/luajit-rocks .

@soumith
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i did a fresh local ezinstall, dont see it. I'm on Mavericks.
uname -a
Darwin a812s3pp1.local 13.1.0 Darwin Kernel Version 13.1.0: Thu Jan 16 19:40:37 PST 2014; root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64

th> a=torch.rand(10)
th> =a
0.2603
0.5489
0.6255
0.2807
0.5866
0.3169
0.6153
0.7909
0.6049
0.2710
[torch.DoubleTensor of dimension 10]

@soumith
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which compiler are you using?

@clementfarabet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing, I'm on Mavericks, using Clang 5.1.0 .

Damn, no idea why this is happening.

@soumith
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok just tried with clang 5.1.0 as well (was using gcc), not the issue.
Are you loading any other packages, or just luajit -ltorch ?

@soumith
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, maybe weird DYLD_* variables?

@clementfarabet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a fresh install, with clang 5.1.0 (so no openmp), then:
luajit -ltorch

torch.randn(10)
Segmentation fault: 11

torch is the head of the current master, and luajit is the head from luajit-rocks.

What else could interfere?

@clementfarabet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No DYLD_* defined ...

@soumith
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beats me. how's the stack looking, did you look at the coredump?

@koraykv
Copy link
Member

@koraykv koraykv commented on 015eee4 Mar 28, 2014 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clementfarabet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I checked the otool and it points to the right libTH : @executable_path/../lib/libluaT.dylib.

Ok thanks a lot for your help, I'm going keep digging, at least I know it must be an install problem on my side.

Please sign in to comment.