-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Speedup sparse init #6899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Speedup sparse init #6899
Conversation
9890049 to
7e5ebb6
Compare
fmassa
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me, thanks!
I have a minor comment, but apart from that this is good to merge!
torch/nn/init.py
Outdated
| tensor[row_idx, col_idx] = 0 | ||
|
|
||
| t = tensor[:, col_idx] | ||
| t[zero_indices] = 0 |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
| for col_idx in range(cols): | ||
| row_indices = list(range(rows)) | ||
| random.shuffle(row_indices) | ||
| row_indices = torch.randperm(rows) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
It looks like there are some tests failing By looking at the failing test, I think that there is a bug in the sparsity check, and it should be Could you verify that this is indeed the case, and fix the test? |
|
@mttk Thanks for your PR. Could you kindly fix the test please? |
|
@ssnl @fmassa sorry -- had an extended weekend so I used it to AFK a bit. Fixed as Francisco suggested, waiting for checks. Ok -- the |
test/test_nn.py
Outdated
| for col_idx in range(input_tensor.size(1)): | ||
| column = input_tensor[:, col_idx] | ||
| assert column[column == 0].nelement() >= math.ceil(sparsity * cols) | ||
| assert column[column == 0].nelement() >= math.ceil(sparsity * cols), "{} : {}, {}".format(column[column == 0].nelement(), sparsity, cols) |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
|
Thanks a lot Martin! |
* Sparse initialization speedup * +empty line * simplify indexing * Can't reproduce locally... * Can't reproduce locally...+ * Can't reproduce locally...+ * Fix test, cleanup
* Sparse initialization speedup * +empty line * simplify indexing * Can't reproduce locally... * Can't reproduce locally...+ * Can't reproduce locally...+ * Fix test, cleanup
As per #6021
Following the discussion in the issue, I ran a shootout between the currently implemented initialization, the version proposed by @stefanonardo (Opt1) and the version proposed by @fmassa (Opt2).
I ran sparse init on tensors of sizes
[(500, 500), (100, 100), (50, 50), (10, 10)]and for sparsities of[1, 0.1, 0.01]. Each initialization was re-ran 10 times. What I learned:topkhaving complexity of k*n), where k is the number of zerosHowever, in the largest case I tested (500x500 tensor, sparsity=1), Opt2 is about the same speed as the original version, and in the other edge case (500x500 tensor, sparsity=0.01), it is ~27% slower with a higher speed deviation.
Implementation credit goes to @stefanonardo
Full (badly formatted) results are here for the next month: https://pastebin.com/fB4mHqM9