Skip to content

Instantly share code, notes, and snippets.

@pengsun
Last active April 21, 2016 03:53
Show Gist options
  • Select an option

  • Save pengsun/aa0c9ee1ad1e37f52032208e6ae28821 to your computer and use it in GitHub Desktop.

Select an option

Save pengsun/aa0c9ee1ad1e37f52032208e6ae28821 to your computer and use it in GitHub Desktop.

Revisions

  1. pengsun revised this gist Apr 21, 2016. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions timing_lt_sl.lua
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@ require'cunn'

    V = 30000 -- vocabulary size
    C = 500 -- output dim
    B = 100 -- #batches
    B = 100*500 -- #batches

    nloop = 3

    @@ -21,15 +21,17 @@ gOutput = torch.CudaTensor(B, C):normal()
    function timing_module(input, m)
    time = torch.tic()
    for i = 1, nloop do
    m:forward(input)
    m:updateOutput(input)
    end
    cutorch.synchronize()
    time = torch.toc(time)
    print(torch.type(m) .. ' fprop time ' .. time/nloop)

    time = torch.tic()
    for i = 1, nloop do
    m:backward(input, gOutput)
    m:accGradParameters(input, gOutput)
    end
    cutorch.synchronize()
    time = torch.toc(time)
    print(torch.type(m) .. ' bprop time ' .. time/nloop)
    end
    @@ -40,4 +42,4 @@ timing_module(input, lt)

    -- SparseLinear
    sl = nn.SparseLinear(V, C):cuda()
    timing_module(inputTable, sl)
    timing_module(inputTable, sl)
  2. pengsun created this gist Apr 20, 2016.
    43 changes: 43 additions & 0 deletions timing_lt_sl.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    -- nn.LookupTable vs nn.SparseLinear
    require'cunn'

    V = 30000 -- vocabulary size
    C = 500 -- output dim
    B = 100 -- #batches

    nloop = 3

    -- onehot input
    input = torch.LongTensor(B):random(V):cuda()
    inputTable = {}
    for i = 1, B do
    inputTable[i] = torch.CudaTensor(1, 2)
    inputTable[i][1][1] = input[i]
    inputTable[i][1][2] = 1.0
    end
    -- dense grad output
    gOutput = torch.CudaTensor(B, C):normal()

    function timing_module(input, m)
    time = torch.tic()
    for i = 1, nloop do
    m:forward(input)
    end
    time = torch.toc(time)
    print(torch.type(m) .. ' fprop time ' .. time/nloop)

    time = torch.tic()
    for i = 1, nloop do
    m:backward(input, gOutput)
    end
    time = torch.toc(time)
    print(torch.type(m) .. ' bprop time ' .. time/nloop)
    end

    -- LookupTable
    lt = nn.LookupTable(V, C):cuda()
    timing_module(input, lt)

    -- SparseLinear
    sl = nn.SparseLinear(V, C):cuda()
    timing_module(inputTable, sl)