Skip to content
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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

panaali
Copy link

@panaali panaali commented Aug 4, 2024

The results match up to 20 digits (below it's 12 digits).

❯ python micrograd.py 
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
❯  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
❯ gcc -Wall -O3 test_utils.c -o test_utils && ./test_utils
test_rng_initialization passed
test_random_u32 passed
test_random passed
test_uniform passed
test_gen_data_determinism passed
test_gen_data_splits passed
test_gen_data_labels passed
test_random_hardcoded passed
test_uniform_hardcoded passed
test_gen_data_hardcoded passed
All tests passed!
❯ gcc -Wall -O3 test_micrograd.c -o test_micrograd && ./test_micrograd
test_sanity_check passed
test_more_ops passed
test_value_creation passed
test_value_add passed
test_value_mul passed
test_value_pow passed
test_value_relu passed
test_value_tanh passed
test_neuron passed
test_layer passed
test_mlp passed
All tests passed!

test_utils.c Outdated
RNG rng;
rng_init(&rng, 42);

assert(rng_random(&rng) == 0.3390852212905884f);
Copy link
Author

@panaali panaali Aug 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@panaali panaali changed the title add c impl and tests add C impl and tests with exact match to python results Aug 4, 2024
@panaali panaali marked this pull request as ready for review August 4, 2024 10:13
@panaali panaali changed the title add C impl and tests with exact match to python results Add C impl and tests with exact match to python results Aug 4, 2024
@panaali panaali changed the title Add C impl and tests with exact match to python results Add C impl and tests Aug 4, 2024
@panaali panaali marked this pull request as draft August 4, 2024 19:41
@panaali panaali changed the title Add C impl and tests Add C impl and tests with exact match to python results Aug 4, 2024
@panaali panaali marked this pull request as ready for review August 4, 2024 21:25
@panaali panaali marked this pull request as draft August 9, 2024 07:33
@panaali
Copy link
Author

panaali commented Aug 9, 2024

Hmm, the memory management is a bit tricky with all the intermediate values. WIP

@karpathy
Copy link
Contributor

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.

@panaali
Copy link
Author

panaali commented Sep 18, 2024

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 suppressions to hide reported errors not related to our code.

@panaali panaali marked this pull request as ready for review September 18, 2024 16:34
@panaali
Copy link
Author

panaali commented Sep 18, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants