gorgonia / tensor

package tensor provides efficient and generic n-dimensional arrays in Go that are useful for machine learning and deep learning purposes
Apache License 2.0
359 stars 49 forks source link

handleReuse: add `safe` flag to skip expensive call to BorrowInt #107

Closed khezen closed 3 years ago

khezen commented 3 years ago

Hi fellows,

First of all thanks for the great packages.

I've been using your package to build some MLP models. I was profiling cpu usage with pprof and I shown that most of cpu time was spent on

l.output, err = l.kernel.MatVecMul(
    input,
    tensor.WithReuse(l.output),
    tensor.UseUnsafe(),
)

I wasn't surprised by that until It show that cpu was spending more than half of it on handleReuse. As it turns out reuseCheckShape(retVal, expectedShape) and more specifically BorrowInts can be very expensive. It bugged me because I assumed that passing UseUnsafe should prevent the shape checking if I know what I'm passing in WithReuse.

After I made theses changes my program ran twice as fast.

coveralls commented 3 years ago

Coverage Status

Coverage increased (+0.2%) to 72.127% when pulling e52677802b07a7018c95a16c767cc31e30c99e84 on khezen:master into d5ff158e8ba02c3e4ad3de4932959c4be1bfce94 on gorgonia:master.

chewxy commented 3 years ago

LGTM. Thanks for the PR and sorry for doing this so late. I had a bit of a personal issue. I will merge this