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

Implement points array backend #154

Merged
merged 7 commits into from
Feb 16, 2025
Merged

Implement points array backend #154

merged 7 commits into from
Feb 16, 2025

Conversation

talmo
Copy link
Contributor

@talmo talmo commented Feb 16, 2025

This PR implements point storage using structured numpy arrays (similar to core SLEAP).

This provides a huge speed up when loading large datasets which can easily scale to millions or billions of points. Before this PR, the overhead of creating this many Point and PredictedPoint objects would dominate runtime. By creating fewer numpy arrays with preallocated memory, loading is super fast.

This does introduce breaking changes to anything that used the Point objects directly, but otherwise, almost all of the API is identical to before.

Benchmark

Script

import sleap_io as sio
import cProfile
import pstats
import io

if __name__ == "__main__":
    profiler = cProfile.Profile()
    profiler.enable()
    labels = sio.load_slp(
        "/Users/talmo/Downloads/cohort2.220503_153813.Camera0.mov.00000.slp"
    )
    profiler.disable()

    # Save and print results
    stream = io.StringIO()
    stats = pstats.Stats(profiler, stream=stream)
    stats.strip_dirs()
    stats.sort_stats(pstats.SortKey.TIME)
    stats.print_stats()
    print(stream.getvalue())

Data

(sleap-io) talmo@Talmos-MBP-M2 sleap-io % python -c "import sleap_io; print(sleap_io.load_slp('/Users/talmo/Downloads/cohort2.220503_153813.Camera0.mov.00000.slp'))"
Labels(labeled_frames=42500, videos=1, skeletons=1, tracks=2, suggestions=0)

(sleap-io) talmo@Talmos-MBP-M2 sleap-io % python -c "import sleap_io; print(sleap_io.load_slp('/Users/talmo/Downloads/cohort2.220503_153813.Camera0.mov.00000.slp').numpy().shape)"
(42500, 2, 15, 2)

Total: 1,275,000 points

Before (4.7 secs)

(sleap-io) talmo@Talmos-MBP-M2 sleap-io % python load_slp_profile.py
         2455609 function calls (2371009 primitive calls) in 4.702 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    3.074    3.074    3.154    3.154 slp.py:800(read_pred_points)
    84593    0.521    0.000    0.584    0.000 instance.py:211(_convert_points)
        1    0.518    0.518    1.245    1.245 slp.py:816(read_instances)
    42500    0.172    0.000    0.172    0.000 <attrs generated init sleap_io.model.labeled_frame.LabeledFrame>:1(__init__)
        1    0.097    0.097    4.696    4.696 slp.py:1041(read_labels)
    84593    0.081    0.000    0.720    0.000 <attrs generated init sleap_io.model.instance.PredictedInstance>:1(__init__)
  1268895    0.066    0.000    0.066    0.000 <attrs generated init sleap_io.model.instance.PredictedPoint>:1(__init__)
    84593    0.037    0.000    0.622    0.000 instance.py:252(__attrs_post_init__)
253821/169228    0.024    0.000    0.035    0.000 {built-in method builtins.len}
        7    0.020    0.003    0.022    0.003 dataset.py:765(__getitem__)
   169186    0.019    0.000    0.019    0.000 {method 'keys' of 'dict' objects}
    84593    0.012    0.000    0.017    0.000 validators.py:94(__call__)
    84593    0.011    0.000    0.015    0.000 skeleton.py:306(__len__)
    84594    0.010    0.000    0.010    0.000 {method 'values' of 'dict' objects}
        1    0.008    0.008    0.015    0.015 labels.py:64(update)
        1    0.006    0.006    4.702    4.702 main.py:11(load_slp)

Runtime: 4.702
Points per sec: 271161.208

After (1.7 secs)

(sleap-io) talmo@Talmos-MBP-M2 sleap-io % python load_slp_profile.py 
         2201830 function calls (2032637 primitive calls) in 1.696 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.625    0.625    1.462    1.462 slp.py:808(read_instances)
    84593    0.283    0.000    0.283    0.000 {built-in method numpy.empty}
    84593    0.183    0.000    0.242    0.000 _shape_base_impl.py:621(column_stack)
    84593    0.100    0.000    0.550    0.000 instance.py:456(empty)
        1    0.093    0.093    1.696    1.696 slp.py:1051(read_labels)
       10    0.080    0.008    0.080    0.008 files.py:215(make_fid)
423007/253821    0.047    0.000    0.069    0.000 {built-in method builtins.len}
    84593    0.038    0.000    0.074    0.000 instance.py:496(_convert_points)
   169186    0.025    0.000    0.025    0.000 {built-in method numpy.array}
    84595    0.025    0.000    0.025    0.000 skeleton.py:242(node_names)
    42500    0.022    0.000    0.022    0.000 <attrs generated init sleap_io.model.labeled_frame.LabeledFrame>:1(__init__)
   169186    0.022    0.000    0.032    0.000 skeleton.py:307(__len__)
   211747    0.020    0.000    0.020    0.000 {method 'append' of 'list' objects}
    84593    0.019    0.000    0.093    0.000 instance.py:233(__attrs_post_init__)
    84593    0.017    0.000    0.026    0.000 shape_base.py:208(_arrays_for_stack_dispatcher)
        7    0.016    0.002    0.019    0.003 dataset.py:765(__getitem__)
    84593    0.016    0.000    0.109    0.000 <attrs generated init sleap_io.model.instance.PredictedInstance>:1(__init__)
    84593    0.015    0.000    0.041    0.000 _shape_base_impl.py:617(_column_stack_dispatcher)
   169186    0.013    0.000    0.013    0.000 {built-in method numpy.asanyarray}
    84652    0.009    0.000    0.009    0.000 {built-in method builtins.hasattr}
        1    0.008    0.008    0.015    0.015 labels.py:64(update)
    84593    0.005    0.000    0.005    0.000 multiarray.py:180(concatenate)
    42500    0.005    0.000    0.007    0.000 labeled_frame.py:41(__iter__)
    42500    0.002    0.000    0.002    0.000 {built-in method builtins.iter}
        2    0.001    0.000    0.001    0.000 {method 'read' of '_io.BufferedReader' objects}
        1    0.001    0.001    1.696    1.696 main.py:11(load_slp)

Runtime: 1.696
Points per sec: 751768.868
Speed-up: 2.77240566x

Copy link

codecov bot commented Feb 16, 2025

Codecov Report

Attention: Patch coverage is 99.48980% with 1 line in your changes missing coverage. Please review.

Project coverage is 96.32%. Comparing base (95a68ed) to head (1c32c8f).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
sleap_io/io/labelstudio.py 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #154      +/-   ##
==========================================
- Coverage   96.39%   96.32%   -0.08%     
==========================================
  Files          18       18              
  Lines        2583     2637      +54     
==========================================
+ Hits         2490     2540      +50     
- Misses         93       97       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

coderabbitai bot commented Feb 16, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@talmo talmo merged commit 9811c98 into main Feb 16, 2025
9 checks passed
@talmo talmo deleted the talmo/points-array branch February 16, 2025 11:01
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.

1 participant