-
Notifications
You must be signed in to change notification settings - Fork 57
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
Add C impl and tests with exact match to python results #10
base: master
Are you sure you want to change the base?
Conversation
test_utils.c
Outdated
RNG rng; | ||
rng_init(&rng, 42); | ||
|
||
assert(rng_random(&rng) == 0.3390852212905884f); |
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.
exact match against python impl results https://github.com/EurekaLabsAI/micrograd/pull/9/files#diff-08f62f2a3ed5f4712fad17ab35354a37d9c226335d06f3ae75e6e5927e48946fR148
Hmm, the memory management is a bit tricky with all the intermediate values. WIP |
This looks very nice thank you, on a high level. Agree that the ref counting etc looks a bit gnarly. I'll think about cleaner ways. |
No memory leaks reported by Valgrind using the ValueTracker method❯ gcc -ggdb3 -o micrograd micrograd.c && valgrind --leak-check=full --suppressions=memcheck.log ./micrograd
==51565== Memcheck, a memory error detector
==51565== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==51565== Using Valgrind-3.23.0.GIT-lbmacos and LibVEX; rerun with -h for copyright info
==51565== Command: ./micrograd
==51565==
==51565== Conditional jump or move depends on uninitialised value(s)
==51565== at 0x7FF814355E5F: ???
==51565== by 0x7FF8141D58A9: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==51565== by 0x7FF8141D3D11: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==51565== by 0x7FF8141E33FD: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==51565== by 0x7FF8142010D0: printf (in /usr/lib/system/libsystem_c.dylib)
==51565== by 0x1000028C1: ??? (in ./micrograd)
==51565== by 0x100002D5C: ??? (in ./micrograd)
==51565== by 0x10000D344: (below main) (in /usr/lib/dyld)
==51565==
==51565== Invalid read of size 32
==51565== at 0x7FF814355E3D: ???
==51565== by 0x7FF8141D58A9: __sfvwrite (in /usr/lib/system/libsystem_c.dylib)
==51565== by 0x7FF8141D3D11: __vfprintf (in /usr/lib/system/libsystem_c.dylib)
==51565== by 0x7FF8141E33FD: vfprintf_l (in /usr/lib/system/libsystem_c.dylib)
==51565== by 0x7FF8142010D0: printf (in /usr/lib/system/libsystem_c.dylib)
==51565== by 0x1000028C1: ??? (in ./micrograd)
==51565== by 0x100002D5C: ??? (in ./micrograd)
==51565== by 0x10000D344: (below main) (in /usr/lib/dyld)
==51565== Address 0x1006d8140 is 32 bytes before a block of size 32 in arena "client"
==51565==
step 0, val loss 0.917089980727
step 0, train loss 0.981189773739
step 1, train loss 0.514887446788
step 2, train loss 0.316579188913
step 3, train loss 0.225565730159
step 4, train loss 0.167966949207
step 5, train loss 0.137663783554
step 6, train loss 0.125363948209
step 7, train loss 0.114101841484
step 8, train loss 0.100239239159
step 9, train loss 0.086824783450
==51565==
==51565== HEAP SUMMARY:
==51565== in use at exit: 13,280 bytes in 178 blocks
==51565== total heap usage: 477,645 allocs, 477,467 frees, 13,418,343 bytes allocated
==51565==
==51565== LEAK SUMMARY:
==51565== definitely lost: 0 bytes in 0 blocks
==51565== indirectly lost: 0 bytes in 0 blocks
==51565== possibly lost: 0 bytes in 0 blocks
==51565== still reachable: 4,244 bytes in 4 blocks
==51565== suppressed: 9,036 bytes in 174 blocks
==51565== Reachable blocks (those to which a pointer was found) are not shown.
==51565== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==51565==
==51565== Use --track-origins=yes to see where uninitialised values come from
==51565== For lists of detected and suppressed errors, rerun with: -s
==51565== ERROR SUMMARY: 33 errors from 2 contexts (suppressed: 148 from 49) on mac I need to pass |
@karpathy I changed the memory management from ref_counting to tracking temp values in a dynamic array. It's way more cleaner and easier to comprehend this way. |
The results match up to 20 digits (below it's 12 digits).
❯ gcc -Wall -lm -O3 micrograd.c -o micrograd && ./micrograd step 0, val loss 0.917089980727 step 0, train loss 0.981189773739 step 1, train loss 0.514887446788 step 2, train loss 0.316579188913 ... step 90, val loss 0.000280126802 ... step 97, train loss 0.000479270583 step 98, train loss 0.000464833605 step 99, train loss 0.000450930456