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

Make SDK models pickleable #8746

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open

Conversation

SpecLad
Copy link
Contributor

@SpecLad SpecLad commented Nov 27, 2024

Motivation and context

Currently, they aren't (or rather, they can be pickled, but unpickling fails). This is due to a small quirk of how the model classes work, and is easily worked around.

In addition, don't create a new Configuration object for each model. These objects are pretty beefy, and they increase the size of each pickle by a full kilobyte (and of course they increase memory usage even when pickle is not involved). AFAICS, these objects are only used when assigning values to file-type fields, and it's easy enough to rewrite the logic so that it still works when the model's _configuration field is None.

How has this been tested?

Unit tests.

Checklist

  • I submit my changes into the develop branch
  • I have created a changelog fragment
  • [ ] I have updated the documentation accordingly
  • I have added tests to cover my changes
  • [ ] I have linked related issues (see GitHub docs)
  • [ ] I have increased versions of npm packages if it is necessary
    (cvat-canvas,
    cvat-core,
    cvat-data and
    cvat-ui)

License

  • I submit my code changes under the same MIT License that covers the project.
    Feel free to contact the maintainers if that's a concern.

Summary by CodeRabbit

  • New Features

    • Model instances within the SDK can now be pickled, allowing for easier serialization and transmission.
  • Tests

    • Introduced a new test to verify the pickling and unpickling functionality of the PatchedLabelRequest model.

Copy link
Contributor

coderabbitai bot commented Nov 27, 2024

Walkthrough

This update introduces the ability to pickle model instances within the SDK, allowing for their serialization and later reconstruction. A new test function has been added to verify this functionality specifically for the PatchedLabelRequest model, ensuring that instances can be serialized and deserialized correctly. The changes also include necessary imports for the new testing functionality.

Changes

File Path Change Summary
changelog.d/20241127_132256_roman_pickle_models.md New feature: Model instances can now be pickled in the SDK.
tests/python/sdk/test_api_wrappers.py Added method test_models_are_pickleable() to test pickling functionality; imported pickle. Existing tests remain unchanged.

Poem

In the meadow where models play,
A new trick has come to stay.
With a hop and a joyful squeal,
They can now be packed with zeal!
Pickled tight, they’ll travel far,
A rabbit's dream, a shining star! 🐇✨


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

Currently, they aren't (or rather, they can be pickled, but unpickling
fails). This is due to a small quirk of how the model classes work, and is
easily worked around.

In addition, don't create a new Configuration object for each model. These
objects are pretty beefy, and they increase the size of each pickle by a
full kilobyte (and of course they increase memory usage even when pickle is
not involved). AFAICS, these objects are only used when assigning values to
file-type fields, and it's easy enough to rewrite the logic so that it still
works when the model's `_configuration` field is None.
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (1)
changelog.d/20241127_132256_roman_pickle_models.md (1)

1-4: Enhance the changelog entry with implementation details and impact.

While the entry correctly documents the new feature, it would be more helpful to users if it included details about:

  • The memory optimization achieved by eliminating per-model Configuration objects
  • The behavior change regarding the _configuration field being allowed to be None
  • Any potential migration steps for users who might be relying on the previous behavior

Consider expanding the entry like this:

 ### Added
 
-- [SDK] Model instances can now be pickled
+- [SDK] Model instances can now be pickled. This change:
+  - Reduces memory usage by sharing Configuration objects between models
+  - Allows the model's `_configuration` field to be None when not needed
+  - Fixes unpickling issues in model classes
   (<https://github.com/cvat-ai/cvat/pull/8746>)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between edef764 and 5020112.

⛔ Files ignored due to path filters (6)
  • cvat-sdk/gen/templates/openapi-generator/model_templates/method_from_openapi_data_composed.mustache is excluded by !**/gen/**
  • cvat-sdk/gen/templates/openapi-generator/model_templates/method_from_openapi_data_shared.mustache is excluded by !**/gen/**
  • cvat-sdk/gen/templates/openapi-generator/model_templates/method_from_openapi_data_simple.mustache is excluded by !**/gen/**
  • cvat-sdk/gen/templates/openapi-generator/model_templates/method_init_shared.mustache is excluded by !**/gen/**
  • cvat-sdk/gen/templates/openapi-generator/model_templates/method_init_simple.mustache is excluded by !**/gen/**
  • cvat-sdk/gen/templates/openapi-generator/model_utils.mustache is excluded by !**/gen/**
📒 Files selected for processing (2)
  • changelog.d/20241127_132256_roman_pickle_models.md (1 hunks)
  • tests/python/sdk/test_api_wrappers.py (2 hunks)
🔇 Additional comments (2)
tests/python/sdk/test_api_wrappers.py (2)

5-5: LGTM: Import placement is correct

The pickle import is appropriately placed with standard library imports.


118-124: Verify Configuration object memory optimization

The PR objectives mention optimizing memory usage by allowing _configuration to be None. Let's verify this behavior:

✅ Verification successful

Let me gather more specific information about the pickle-related implementation and configuration handling.


Based on the gathered information, I can now provide the final response:

Memory optimization for Configuration object is correctly implemented

The codebase shows that:

  • The _configuration field is properly handled with a default value of None in model initialization methods
  • The pickle implementation in model_utils.mustache correctly handles object serialization without requiring the Configuration object
  • The test case test_models_are_pickleable verifies this behavior by successfully pickling and unpickling a model object
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for Configuration object usage in model classes

# Look for Configuration-related code
rg -A 5 "class Configuration" 

# Look for _configuration field usage
rg "_configuration\s*=" 

# Look for pickle-related methods
ast-grep --pattern 'def __getstate__($$$) { $$$ }'

Length of output: 2228


Script:

#!/bin/bash
# Check for pickle-related implementations and configuration handling in model classes

# Look for __getstate__ and __setstate__ methods in Python files
rg -U "def __[gs]etstate__" -A 5

# Look for specific configuration handling in model files
rg -U "_configuration" "tests/python/sdk" -A 2

# Check model base classes for configuration handling
ast-grep --pattern 'class $NAME($_):
    $$$
    _configuration = $$$
    $$$'

Length of output: 1803

tests/python/sdk/test_api_wrappers.py Show resolved Hide resolved
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.05%. Comparing base (7d0205b) to head (5020112).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8746      +/-   ##
===========================================
- Coverage    74.05%   74.05%   -0.01%     
===========================================
  Files          409      409              
  Lines        43783    43784       +1     
  Branches      3984     3984              
===========================================
  Hits         32425    32425              
- Misses       11358    11359       +1     
Components Coverage Δ
cvat-ui 78.36% <ø> (-0.01%) ⬇️
cvat-server 70.34% <100.00%> (+<0.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.

2 participants