You can replicate it but you will need to use the right concept.
Writing derivatives by hand works fine for linear of trivial problems.
If you start having networks, you might need to have the backprop done automatically using something that looks like AD. Otherwise, as you see in your repo, the code becomes hard to read (and buggy) very quickly.
I prefer to spend time making pytorch do more stuff instead of re-implementing it. But that’s a personal choice