[go: nahoru, domu]

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 size assert, refactor #3

Merged
merged 4 commits into from
Jul 29, 2024
Merged

Conversation

gordicaleksa
Copy link
Contributor
@gordicaleksa gordicaleksa commented Jul 27, 2024

Minor refactor + added size assert.

inline was causing compile failures on my Linux machine, i think if we want to make it inline we'd have to move it to the header file (but then again see my next comment on simplicity vs robustness tradeoff so if we err on the simplicity side i say let's just omit this minor optimization (?)).

@gordicaleksa
Copy link
Contributor Author
gordicaleksa commented Jul 27, 2024

@karpathy similarly to the circular buffer logic PR from ngram repo (see EurekaLabsAI/ngram#10) - happy to modify tensor_to_string to make it more flexible/dynamic akin to how Python's list or C++ std::vector work (when we hit the limit of the underlying buffer reallocate a 2x larger buffer and copy the elements over).

Having said that it's a tradeoff between simplicity and robustness. Lmk if you'd like to see that change.

tensor1d.c Outdated
if (start > t->size) { start = t->size; }
if (end > t->size) { end = t->size; }
// 2) handle out-of-bounds indices: clip to [0, t->size] range
start = fmin(fmax(start, 0), t->size);
Copy link
Contributor

Choose a reason for hiding this comment

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

The fmin and fmax in C are on double, so there will be a weird cast from int to double here, would prefer to not do that. An alternative to the ifs would possibly be a ternary expression, I'd be ok with that

https://www.educative.io/answers/what-is-fmin-in-c

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@karpathy ok good point, my bad here -> fixed, see whether you like this solution

tensor1d.c Outdated
@@ -41,6 +41,7 @@ inline int ceil_div(int a, int b) {
// similar to torch.Storage

Storage* storage_new(int size) {
assert(size > 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

good call

@@ -30,7 +30,7 @@ void *malloc_check(size_t size, const char *file, int line) {
// ----------------------------------------------------------------------------
// utils

inline int ceil_div(int a, int b) {
Copy link
Contributor

Choose a reason for hiding this comment

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

agree on getting rid of inline if it causes issues. I didn't realize inline was contentious and has sharp edges.

@Jake-Song Jake-Song mentioned this pull request Jul 29, 2024
@karpathy karpathy merged commit e5c2d24 into EurekaLabsAI:master Jul 29, 2024
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