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

Fix the perf issue in building nodes from splits. #10766

Conversation

preemoDez
Copy link
Contributor

@preemoDez preemoDez commented Feb 15, 2024

Description

Create the relationships object only once. Otherwise, it recomputes the whole text's hash for every node. It is very inefficient for long text.

An alternative approach would be to cache the hash property. However, it wasn't so straightforward as Document isn't a cacheable type. I also do not know Python very well, maybe it would be enough to store a simple null and if it isn't null, then don't recompute? However, the most important reason is I'm not sure about the side effects and the existing assumption that the node is mutable and the hash always reflects the state during the call (unless we modify the object in multiple threads). This change doesn't break any assumptions. If the document was modified while we were creating nodes extracted from it, something would be very wrong.

Fixes #10554

Type of Change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Local benchmarks taken on a document attached to the bug:

Before: Execution time for build_nodes_from_splits: 53.69 seconds

After: Execution time for build_nodes_from_splits: 0.18 seconds

  • Added new unit/integration tests
  • Added new notebook (that tests end-to-end)
  • I stared at the code and made sure it makes sense

Suggested Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added Google Colab support for the newly added notebooks.
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I ran make format; make lint to appease the lint gods

Create the `relationships` object only once. Otherwise, it recomputes the whole text's hash for every node. It is very inefficient for long text.

An alternative approach would be to cache the hash property. However, it wasn't so straightforward as `Document` isn't a cacheable type. I also do not know Python very well, maybe it would be enough to store a simple null and if it isn't null, then don't recompute? However, the most important reason is I'm not sure about the side effects and the existing assumption that the node is mutable and the hash always reflects the state during the call (unless we modify the object in multiple threads). This change doesn't break any assumptions. If the document was modified while we were creating nodes extracted from it, something would be very wrong.

Benchmarks taken on a document attached to the bug:

Before: Execution time for build_nodes_from_splits: 53.69 seconds

After: Execution time for build_nodes_from_splits: 0.18 seconds
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Feb 15, 2024
Copy link
Contributor

@hatianzhang hatianzhang left a comment

Choose a reason for hiding this comment

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

lgtm good fix cc @logan-markewich for a check

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Feb 15, 2024
@hatianzhang hatianzhang merged commit f2d9472 into run-llama:main Feb 15, 2024
8 checks passed
@preemoDez preemoDez deleted the do_not_recalculate_document_hash_for_every_node_bug_10554 branch February 15, 2024 23:38
Dominastorm pushed a commit to uptrain-ai/llama_index that referenced this pull request Feb 28, 2024
* Fix the perf issue in building nodes from splits.

Create the `relationships` object only once. Otherwise, it recomputes the whole text's hash for every node. It is very inefficient for long text.

An alternative approach would be to cache the hash property. However, it wasn't so straightforward as `Document` isn't a cacheable type. I also do not know Python very well, maybe it would be enough to store a simple null and if it isn't null, then don't recompute? However, the most important reason is I'm not sure about the side effects and the existing assumption that the node is mutable and the hash always reflects the state during the call (unless we modify the object in multiple threads). This change doesn't break any assumptions. If the document was modified while we were creating nodes extracted from it, something would be very wrong.

Benchmarks taken on a document attached to the bug:

Before: Execution time for build_nodes_from_splits: 53.69 seconds

After: Execution time for build_nodes_from_splits: 0.18 seconds

* Fix the formatting
Izukimat pushed a commit to Izukimat/llama_index that referenced this pull request Mar 29, 2024
* Fix the perf issue in building nodes from splits.

Create the `relationships` object only once. Otherwise, it recomputes the whole text's hash for every node. It is very inefficient for long text.

An alternative approach would be to cache the hash property. However, it wasn't so straightforward as `Document` isn't a cacheable type. I also do not know Python very well, maybe it would be enough to store a simple null and if it isn't null, then don't recompute? However, the most important reason is I'm not sure about the side effects and the existing assumption that the node is mutable and the hash always reflects the state during the call (unless we modify the object in multiple threads). This change doesn't break any assumptions. If the document was modified while we were creating nodes extracted from it, something would be very wrong.

Benchmarks taken on a document attached to the bug:

Before: Execution time for build_nodes_from_splits: 53.69 seconds

After: Execution time for build_nodes_from_splits: 0.18 seconds

* Fix the formatting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Python get_nodes_from_documents is notably slow on large text files
3 participants