Skip to content

Instantly share code, notes, and snippets.

@killeent
Created January 9, 2017 21:47
Show Gist options
  • Save killeent/33476c9eb3a84354026f1bd0c190e4e6 to your computer and use it in GitHub Desktop.
Save killeent/33476c9eb3a84354026f1bd0c190e4e6 to your computer and use it in GitHub Desktop.
comprehensive benchmarks
require 'torch'
require 'cutorch'
local dims = {1, 32, 128, 1024}
local counts = {2, 4, 8, 32, 128, 512, 1024, 2048}
local trials = 10
print('Sum of', trials, 'trials used to generate results')
print("----------------------------------------------------------------")
-- 1D Tensors
for _, dim in ipairs(dims) do
for _, ct in ipairs(counts) do
local tensors = {}
for i = 1, ct do
table.insert(tensors, torch.CudaTensor(dim):uniform())
end
local timer = torch.Timer()
for i = 1, trials do
local catted = torch.cat(tensors, 1)
end
cutorch.synchronize()
local sum = timer:time().real
print("CatArray for", ct, "size", dim, "1D Tensors took", sum, "seconds.")
tensors = nil
collectgarbage('collect')
end
end
print("----------------------------------------------------------------")
local dims = {1, 32, 128}
local counts = {2, 4, 8, 32, 128, 512, 1024}
-- 2D Tensors
for _, dim in ipairs(dims) do
for _, ct in ipairs(counts) do
local tensors = {}
for i = 1, ct do
table.insert(tensors, torch.CudaTensor(dim, dim):uniform())
end
local timer = torch.Timer()
for i = 1, trials do
local catted = torch.cat(tensors, 1)
end
cutorch.synchronize()
local sum = timer:time().real
print("CatArray for", ct, dim, "x", dim, "2D Tensors along dim=1 took", sum , "seconds.")
local timer = torch.Timer()
for i = 1, trials do
local catted = torch.cat(tensors, 2)
end
cutorch.synchronize()
local sum = timer:time().real
print("CatArray for", ct, dim, "x", dim, "2D Tensors along dim=2 took", sum , "seconds.")
tensors = nil
collectgarbage('collect')
end
end
print("----------------------------------------------------------------")
local dims = {16}
local counts = {2, 4, 8, 32, 128, 512}
-- 3D Tensors
for _, dim in ipairs(dims) do
for _, ct in ipairs(counts) do
local tensors = {}
for i = 1, ct do
table.insert(tensors, torch.CudaTensor(dim, dim, dim):uniform())
end
local timer = torch.Timer()
for i = 1, trials do
local catted = torch.cat(tensors, 1)
end
cutorch.synchronize()
local sum = timer:time().real
print("CatArray for", ct, dim, "x", dim, "3D Tensors along dim=1 took", sum, "seconds")
local timer = torch.Timer()
for i = 1, trials do
local catted = torch.cat(tensors, 2)
end
cutorch.synchronize()
local sum = timer:time().real
print("CatArray for", ct, dim, "x", dim, "3D Tensors along dim=1 took", sum, "seconds")
local timer = torch.Timer()
for i = 1, trials do
local catted = torch.cat(tensors, 3)
end
cutorch.synchronize()
local sum = timer:time().real
print("CatArray for", ct, dim, "x", dim, "3D Tensors along dim=1 took", sum, "seconds")
tensors = nil
collectgarbage('collect')
end
end
print("----------------------------------------------------------------")
-- Big Benchmarks (many iterations)
local counts = {32, 1024, 128}
local sizes = {600, 32, 1024}
local trials = 10000
for i = 1, 3 do
local tensors = {}
local count = counts[i]
local size = sizes[i]
for i = 1, count do
table.insert(tensors, torch.CudaTensor(1, size):uniform())
end
local timer = torch.Timer()
for i = 1, trials do
local catted = torch.cat(tensors, 1)
end
cutorch.synchronize()
local sum = timer:time().real
print("CatArray for", counts[i], "1 x", sizes[i], "tensors took", sum, "seconds to complete", trials, "cats")
end
print("----------------------------------------------------------------")
-- Big Matrix
local a = torch.CudaTensor(8192, 8192):uniform()
local b = torch.CudaTensor(8192, 8192):uniform()
local timer = torch.Timer()
local catted = torch.cat(a, b, 1)
cutorch.synchronize()
local sum = timer:time().real
print("CatArray for 2 8192x8192 tensors along dim 1 took", sum, "seconds")
local timer = torch.Timer()
local catted = torch.cat(a, b, 2)
cutorch.synchronize()
local sum = timer:time().real
print("CatArray for 2 8192x8192 tensors along dim 2 took", sum, "seconds")
print("----------------------------------------------------------------")
-- Huge # of small tensors
local tensors = {}
for i = 1, 50000 do
table.insert(tensors, torch.CudaTensor(1, 32):uniform())
end
local timer = torch.Timer()
local catted = torch.cat(tensors, 1)
cutorch.synchronize()
local sum = timer:time().real
print("CatArray for 50K 1x32 tensors along dim 1 took", sum, "seconds")
print('done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment