Skip to content

Improve graph build time #2329

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

Merged
merged 3 commits into from
Jul 25, 2023
Merged

Conversation

slaren
Copy link
Member

@slaren slaren commented Jul 22, 2023

Instead of checking the list of current nodes in ggml_visit_parents() to determine if a node has already been visited, a visited flag is added to ggml_tensor.

Unfortunately this requires resetting the visited flag to reuse the tensors in a different graph, and because in some cases graphs are built in multiple steps, it is not easy to do this automatically. Currently, this is done automatically when calling ggml_graph_plan(), but there may be some cases where it can cause current ggml code to fail silently.

@slaren
Copy link
Member Author

slaren commented Jul 22, 2023

@JohannesGaessler this may explain the performance uplift that you observed in #2230 (comment).

@slaren slaren force-pushed the visit-parents-opt branch 2 times, most recently from 7a007fb to cec3185 Compare July 23, 2023 16:12
@slaren slaren force-pushed the visit-parents-opt branch from cec3185 to 6542a03 Compare July 24, 2023 00:41
@slaren
Copy link
Member Author

slaren commented Jul 24, 2023

Replaced the visited flag with a very simple hash table in ggml_cgraph. The performance is almost the same and it won't break old code, but it increases the size of ggml_cgraph by 70%.

@slaren slaren marked this pull request as ready for review July 24, 2023 11:13
@slaren
Copy link
Member Author

slaren commented Jul 24, 2023

I think this is as good as it is going to get, ultimately it is a choice between maintaining backwards compatibility or memory usage. I am inclined to prefer the hash table version, but it increases the size of a struct that is already very large. Won't be an issue after we change graphs to allocate them on the heap/context.

Some performance numbers:

commit graph build time ms
b47b8a9 0.963
e76d630 0.642
2f9cf97 0.534
PR (flag) 0.159
PR (hash) 0.168

Some recent changes in master reduced the graph build times as well, I have no idea why.

Copy link
Member

@ggerganov ggerganov left a comment

Choose a reason for hiding this comment

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

Nice, we should implement ggml-org/ggml#299 soon.

@ggerganov ggerganov merged commit da18898 into ggml-org:master Jul 25, 2023
@slaren slaren deleted the visit-parents-opt branch July 25, 2023 12:36
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