Givens orthogonal layer#57
Conversation
|
I somehow broke @kevinchern's tests, what the hell... |
@VolodyaCO which tests? I'm seeing |
I forgot to update my tests to float64 precision. Now that I've done it, it's weird that all of the current failing tests are failing on File "/Users/distiller/project/tests/test_nn.py", line 144, in test_LinearBlock
self.assertTrue(model_probably_good(model, (din,), (dout,))) |
Ahhhhhh. OK Theo also flagged this at #50 . It's a poorly-written test.. you can ignore it. |
| angles, blocks, Ufwd_saved = ctx.saved_tensors | ||
| Ufwd = Ufwd_saved.clone() | ||
| M = grad_output.t() # dL/dU, i.e., grad_output is of shape (n, n) | ||
| n = M.size(1) | ||
| block_size = n // 2 | ||
| A = torch.zeros((block_size, n), device=angles.device, dtype=angles.dtype) |
There was a problem hiding this comment.
Same here re lowercase for Ufwd, M, and A. Avoids incorrect colour highlighting in themes.
There was a problem hiding this comment.
Hmmm, I didn't read this about the incorrect colour highlighting before I made my previous comment. I still think that it is easier to read the algorithm alongside the code if the use of lower/upper case match. For example, lower case m is usually used for an integer variable, not a tensor.
f18b476 to
46062e0
Compare
|
After a bit of git wrangling, I was able to clean my whole mess of merge commits 😆. |
anahitamansouri
left a comment
There was a problem hiding this comment.
This is a nice PR Vlad. It took me a while to go over the paper and this PR :) The only thing is the tests that are failing. Thanks for the great work.
46062e0 to
7f76571
Compare
kevinchern
left a comment
There was a problem hiding this comment.
Did a quick pass to provide some feedback before taking some time to take a deep dive into the paper.
| for _ in range(n - 1): | ||
| pairs = circle_method(sequence) | ||
| if is_odd: | ||
| # Remove pairs involving the dummy dimension: |
There was a problem hiding this comment.
| # Remove pairs involving the dummy dimension: | |
| # Remove pairs involving the dummy dimension |
There was a problem hiding this comment.
I was gonna ask why remove the colon?
1146ebb to
df400fb
Compare
|
@VolodyaCO can you rebase on main? |
df400fb to
8a00932
Compare
6587b89 to
97c2b8e
Compare
97c2b8e to
36a15c8
Compare
VolodyaCO
left a comment
There was a problem hiding this comment.
@kevinchern I have addressed all comments. Should be ready to merge now.
| __all__ = ["GivensRotation"] | ||
|
|
||
|
|
||
| class _RoundRobinGivens(torch.autograd.Function): |
There was a problem hiding this comment.
The documentation is quite thorough for a hidden class. Would this perhaps make sense moving to a nn.functions namespace and removing the underscore? @kevinchern
There was a problem hiding this comment.
I don't think it will be used beyond this class, so it's probably safe to keep her. If there's demand for it as a public function, we can move it out.
| # Initialize U^fwd from forward pass output U. Mathematically, U^fwd represents U^{1:k-1} | ||
| # at block k, defined in equation (11). It is post-multiplied by G_bk^T at each block | ||
| # iteration to "remove the effect of the block's rotations" (Section 4.1, paragraph before | ||
| # equation (12)). This corresponds to the update from U^{1:k} back to U^{1:k-1}. |
There was a problem hiding this comment.
I haven't looked at the reference link, but these comments seem a bit messy (for lack of a better term). If the algorithm in the link describes the process in a clear way, I'd perhaps shorten these comments a bit, although references to equations are always good.
Also, separation by empty lines always makes things look a bit tidier.
There was a problem hiding this comment.
I have added separation by empty lines to make things look tidier. I added these inline comments at @kevinchern 's request. I do agree that it is enough for the interested reader to read the paper. I also think it's helpful to have the inline comments too.
There was a problem hiding this comment.
shorten these comments a bit
I second this opinion
kevin's request
To clarify my suggestion: it will be very helpful for future maintenance to include references to "where" these equations appear. The idea is for the developer to be able to easily reference the material and not necessarily understand the method from comments.
There was a problem hiding this comment.
I see your point. Comments have now been shortened.
kevinchern
left a comment
There was a problem hiding this comment.
Unit tests hinge on NaiveGivensRotationLayer being correct but there aren't correctness tests for it. It checks for serial versus parallel implementations.
We need a unit test to check the forward and backward methods are correct, e.g., work out the expected forward/backward values a small input (n=3?)
| # Initialize U^fwd from forward pass output U. Mathematically, U^fwd represents U^{1:k-1} | ||
| # at block k, defined in equation (11). It is post-multiplied by G_bk^T at each block | ||
| # iteration to "remove the effect of the block's rotations" (Section 4.1, paragraph before | ||
| # equation (12)). This corresponds to the update from U^{1:k} back to U^{1:k-1}. |
There was a problem hiding this comment.
shorten these comments a bit
I second this opinion
kevin's request
To clarify my suggestion: it will be very helpful for future maintenance to include references to "where" these equations appear. The idea is for the developer to be able to easily reference the material and not necessarily understand the method from comments.
Address orthogonal module PR feedback.
9f31f15 to
c0cd080
Compare
kevinchern
left a comment
There was a problem hiding this comment.
LGTM; comments have been addressed.
This PR adds an orthogonal layer given by Givens rotations, using the parallel algorithm described by Firas in https://arxiv.org/abs/2106.00003, which gives a forward complexity of O(n) and backward complexity of O(n log(n)), even though there are O(n^2) rotations.
This PR still is in draft. I wrote it for even n. Probably some more unit tests are to be done, but I am quite lazy (will do it after all math is checked for odd n).