1

具体的にはnn.LogSoftMax、入力テンソルのサイズが小さい場合は omp を使用したくありません。実行時間をテストするための小さなスクリプトがあります。

require 'nn'
my_lsm = function(t)
    o = torch.zeros((#t)[1])
    sum = 0.0
    for i = 1,(#t)[1] do
        o[i] = torch.exp(t[i])
        sum = sum + o[i]
    end
    o = o / sum
    return torch.log(o)
end

ii=torch.randn(arg[1])
m=nn.LogSoftMax()

timer = torch.Timer()
timer:stop()
timer:reset()
timer:resume()
my_lsm(ii)
print(timer:time().real)

timer:stop()
timer:reset()
timer:resume()
m:forward(ii)
print(timer:time().real)

が 10 の場合arg[1]、基本的なログ ソフトマックス関数ははるかに高速に実行されます。

0.00021696090698242
0.033425092697144

しかし、1 回arg[1]は 10,000,000 であり、omp は非常に役立ちます。

29.561321973801 
0.11547803878784

したがって、omp のオーバーヘッドが非常に高いと思われます。私のコードが小さい入力で log softmax を数回呼び出す必要がある場合 (テンソル サイズが 3 しかないなど)、時間がかかりすぎます。場合によっては (常にではありませんが) omp の使用を手動で無効にする方法はありますか?

4

1 に答える 1

4

場合によっては (常にではありませんが) omp の使用を手動で無効にする方法はありますか?

あなたが本当にそれをしたいのであれば、1つの可能性はそれを使用するtorch.setnumthreadsことtorch.getnumthreadsです:

local nth = torch.getnumthreads()
torch.setnumthreads(1)
-- do something
torch.setnumthreads(nth)

したがって、次のようにモンキーパッチを適用できnn.LogSoftMaxます。

nn.LogSoftMax.updateOutput = function(self, input)
  local nth = torch.getnumthreads()
  torch.setnumthreads(1)
  local out = input.nn.LogSoftMax_updateOutput(self, input)
  torch.setnumthreads(nth)
  return out
end
于 2015-05-21T08:12:31.513 に答える