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

optimize: dont load padacioso if not needed by default #629

Merged
merged 4 commits into from
Dec 10, 2024

Conversation

JarbasAl
Copy link
Member

@JarbasAl JarbasAl commented Dec 9, 2024

to account for optional padatious, padacioso and padatious are both loaded into memory to register intents

this PR makes padacioso only load if padatious is not available

side effects: if we want to use padacioso in the pipeline we need to set "disable_padacioso": false explicitly in config to have it load side by side with padatious

Summary by CodeRabbit

  • New Features

    • Enhanced control flow for loading and using intent matching services based on configuration settings.
    • Conditional loading of services to optimize memory usage.
    • Improved handling of matchers to utilize available services effectively.
  • Bug Fixes

    • Refined logic for assigning matchers based on service availability, ensuring correct behavior.
    • Updated shutdown logic to prevent errors when shutting down non-existent services.

Copy link

coderabbitai bot commented Dec 9, 2024

Walkthrough

The changes in this pull request focus on the IntentService class within the ovos_core/intent_services/__init__.py file. Key modifications include the introduction of conditional loading for the PadaciosoService, adjustments to the get_pipeline method for matcher assignments based on service availability, and refinements in the shutdown method to ensure proper handling of the service's lifecycle. These changes optimize memory usage and enhance error handling related to the loading and usage of intent matching services.

Changes

File Change Summary
ovos_core/intent_services/init.py - Updated _load_pipeline_plugins to conditionally load PadaciosoService based on disable_padacioso.
- Modified get_pipeline to conditionally assign matchers for padatious and padacioso.
- Refined shutdown to conditionally shut down PadaciosoService if instantiated.

Possibly related PRs

  • fix:delayed_padatious_training #627: The changes in this PR also modify the _load_pipeline_plugins method in the IntentService class, focusing on the conditional loading of the padacioso service based on the availability of the padatious service, which aligns closely with the changes made in the main PR.

Suggested reviewers

  • goldyfruit

Poem

🐰 In the meadow where services play,
A new path for intent leads the way.
With Padacioso now in the light,
We dance through the code, oh what a sight!
Conditional loading, a clever delight,
For a smoother tomorrow, we hop into the night! 🌙


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. (Experiment)
  • @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.

Copy link

codecov bot commented Dec 9, 2024

Codecov Report

Attention: Patch coverage is 78.94737% with 4 lines in your changes missing coverage. Please review.

Project coverage is 72.39%. Comparing base (23f0bab) to head (9a87227).
Report is 146 commits behind head on dev.

Files with missing lines Patch % Lines
ovos_core/intent_services/__init__.py 78.94% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #629      +/-   ##
==========================================
- Coverage   75.33%   72.39%   -2.95%     
==========================================
  Files          15       15              
  Lines        3094     1583    -1511     
==========================================
- Hits         2331     1146    -1185     
+ Misses        763      437     -326     
Flag Coverage Δ
end2end 55.21% <68.42%> (?)
unittests 49.46% <78.94%> (-25.88%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

Copy link

@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: 0

🧹 Outside diff range and nitpick comments (3)
ovos_core/intent_services/__init__.py (3)

106-107: Fix indentation in debug log message.

The debug message indentation is inconsistent with Python's style guide (PEP 8).

-            LOG.debug("Padacioso pipeline is disabled, only padatious is loaded. "
-                      "set 'disable_padacioso': false in mycroft.conf if you want it to load alongside padatious")
+            LOG.debug("Padacioso pipeline is disabled, only padatious is loaded. "
+                     "set 'disable_padacioso': false in mycroft.conf if you want it to load alongside padatious")

179-185: Enhance warning message about performance impact.

The warning about padacioso being "extremely slow" could be more specific to help users understand the performance implications.

-                    LOG.warning("padatious is not available! using padacioso in it's place, "
-                                "intent matching will be extremely slow in comparison")
+                    LOG.warning("padatious is not available! using padacioso as fallback. "
+                               "Note: padacioso's intent matching can be significantly slower "
+                               "due to its different matching algorithm")

202-215: Consider simplifying the matcher dictionary updates.

The current implementation has separate conditional blocks for padacioso and padatious updates. This could be simplified to reduce code duplication and improve maintainability.

-        if self._padacioso_service is not None:
-            matchers.update({
-                "padacioso_high": self._padacioso_service.match_high,
-                "padacioso_medium": self._padacioso_service.match_medium,
-                "padacioso_low": self._padacioso_service.match_low,
-
-            })
-        if self._padatious_service is not None:
-            matchers.update({
-                "padatious_high": padatious_matcher.match_high,
-                "padatious_medium": padatious_matcher.match_medium,
-                "padatious_low": padatious_matcher.match_low,
-
-            })
+        # Add matchers for the active intent service (padatious or padacioso)
+        if padatious_matcher is not None:
+            service_name = "padatious" if self._padatious_service is not None else "padacioso"
+            matchers.update({
+                f"{service_name}_high": padatious_matcher.match_high,
+                f"{service_name}_medium": padatious_matcher.match_medium,
+                f"{service_name}_low": padatious_matcher.match_low,
+            })
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 88b094d and b2e1494.

📒 Files selected for processing (1)
  • ovos_core/intent_services/__init__.py (4 hunks)
🔇 Additional comments (2)
ovos_core/intent_services/__init__.py (2)

19-21: LGTM: Import changes are clean and follow conventions.

The imports are properly organized and the alias for PadaciosoPipeline as PadaciosoService improves code clarity.


100-104: LGTM: Optimized padacioso loading logic.

The implementation correctly achieves the PR's goal by:

  1. Only loading padacioso by default when padatious is unavailable
  2. Allowing explicit control via the disable_padacioso config option

Copy link

@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: 0

🧹 Outside diff range and nitpick comments (5)
ovos_core/intent_services/__init__.py (5)

102-103: Fix typos in comments

There are typos in the comments at lines 102 and 103. Correct spelling improves code readability.

Apply this diff to correct the typos:

-# by default only load padacioso is padatious is not available
+# By default, only load padacioso if padatious is not available
-# save memory if padacioso isnt needed
+# Save memory if padacioso isn't needed

108-109: Update configuration file reference in log message

The log message mentions mycroft.conf, which may not be the correct configuration file for this project.

If the correct file is ovos.conf or another file, update the log message accordingly.


185-186: Correct typo in log message

There's a typo in the log message at line 185. "It's place" should be "its place".

Apply this diff to fix the typo:

 LOG.warning("padatious is not available! using padacioso in it's place, "
             "intent matching will be extremely slow in comparison")
+LOG.warning("padatious is not available! using padacioso in its place, "
+            "intent matching will be extremely slow in comparison")

204-210: Remove unnecessary blank lines in matcher updates

There are unnecessary blank lines at lines 209 and 210. Removing them improves code readability.

Apply this diff to clean up the code:

     "padacioso_high": self._padacioso_service.match_high,
     "padacioso_medium": self._padacioso_service.match_medium,
     "padacioso_low": self._padacioso_service.match_low,
-
-
 })

211-217: Ensure consistent matcher updates for Padatious and Padacioso

Consider aligning the structure of the matcher updates for padatious and padacioso services for consistency.

This can enhance readability and maintainability. For example:

 if self._padacioso_service is not None:
     matchers.update({
         "padacioso_high": self._padacioso_service.match_high,
         "padacioso_medium": self._padacioso_service.match_medium,
         "padacioso_low": self._padacioso_service.match_low
     })
 if self._padatious_service is not None:
     matchers.update({
         "padatious_high": self._padatious_service.match_high,
         "padatious_medium": self._padatious_service.match_medium,
         "padatious_low": self._padatious_service.match_low
     })
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between b2e1494 and 86d412f.

📒 Files selected for processing (1)
  • ovos_core/intent_services/__init__.py (3 hunks)
🔇 Additional comments (1)
ovos_core/intent_services/__init__.py (1)

104-109: 🛠️ Refactor suggestion

Simplify and clarify the logic for disabling Padacioso

The current logic for disabling Padacioso can be simplified for better readability and maintainability.

Consider refactoring the code as follows:

-disable_padacioso = self.config.get("disable_padacioso", self._padatious_service is not None)
+disable_padacioso = self.config.get("disable_padacioso", self._padatious_service is not None)

 if not disable_padacioso:
     self._padacioso_service = PadaciosoService(self.bus, self.config["padatious"])
-elif "disable_padacioso" not in self.config:
+else:
     LOG.debug("Padacioso pipeline is disabled, only padatious is loaded. "
               "Set 'disable_padacioso': false in the configuration if you want it to load alongside padatious")

This refactoring ensures that the default behavior is clear and the conditions are straightforward.

Likely invalid or redundant comment.

Copy link

@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 (4)
ovos_core/intent_services/__init__.py (4)

102-103: Correct typographical errors in comments

The comments contain typographical errors that may lead to confusion. Please correct them for clarity.

Apply this diff to fix the comments:

-# by default only load padacioso is padatious is not available
+# By default, only load padacioso if padatious is not available

-# save memory if padacioso isnt needed
+# Save memory if padacioso isn't needed

185-186: Fix grammatical error in log message

The log message contains a grammatical error. The possessive form "its" should be used instead of "it's".

Apply this diff to correct the log message:

-LOG.warning("padatious is not available! using padacioso in it's place, "
+LOG.warning("padatious is not available! using padacioso in its place, "

181-187: Improve clarity of the PadatiousMatcher substitution logic

The current logic for substituting PadaciosoService when PadatiousService is not available can be refactored for better readability.

Consider restructuring the code as follows:

padatious_matcher = None
if self._padatious_service is not None:
    padatious_matcher = self._padatious_service
elif self._padacioso_service is not None:
    if any("padatious" in p for p in session.pipeline):
        LOG.warning("padatious is not available! using padacioso in its place, "
                    "intent matching will be extremely slow in comparison")
    padatious_matcher = self._padacioso_service

This refactor makes the substitution logic more explicit and easier to follow.


498-499: Apply consistent checks before shutting down services

In the shutdown method, you conditionally shut down self._adapt_service and self._padacioso_service based on their existence. For consistency and to prevent potential AttributeErrors, apply the same checks to all services.

Consider the following refactor:

-if self._padacioso_service:
+if self._padacioso_service is not None:
    self._padacioso_service.shutdown()
-if self._padatious_service:
+if self._padatious_service is not None:
    self._padatious_service.shutdown()
-if self._common_qa:
+if self._common_qa is not None:
    self._common_qa.shutdown()
-if self._converse:
+if self._converse is not None:
    self._converse.shutdown()
-if self._fallback:
+if self._fallback is not None:
    self._fallback.shutdown()
-if self._ocp:
+if self._ocp is not None:
    self._ocp.shutdown()

This ensures all services are checked for None before attempting to call their shutdown methods.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 86d412f and e954576.

📒 Files selected for processing (1)
  • ovos_core/intent_services/__init__.py (4 hunks)
🔇 Additional comments (1)
ovos_core/intent_services/__init__.py (1)

104-109: Verify the logic for disabling PadaciosoService

The condition used to set disable_padacioso may not be immediately clear. Ensure that it aligns with the intended behavior of loading PadaciosoService only when PadatiousService is unavailable unless explicitly configured.

Here's the logic breakdown:

  • disable_padacioso is obtained from the configuration with a default value of self._padatious_service is not None.
    • This means if PadatiousService is available, disable_padacioso defaults to True, disabling PadaciosoService.
    • If PadatiousService is not available, it defaults to False, enabling PadaciosoService.
  • The subsequent check if not disable_padacioso: determines whether to instantiate PadaciosoService.

Ensure this logic correctly reflects the desired default behavior and consider adding comments for clarity.

ovos_core/intent_services/__init__.py Show resolved Hide resolved
Copy link

@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: 0

🧹 Outside diff range and nitpick comments (2)
ovos_core/intent_services/__init__.py (2)

102-103: Fix typos in comments for clarity

The comments in lines 102 and 103 contain typos that could cause confusion.

  • Line 102: Replace "is" with "if" to read "by default only load padacioso if padatious is not available".
  • Line 103: Add an apostrophe in "isn't" for correct contraction.

Apply this diff to correct the typos:

-        # by default only load padacioso is padatious is not available
+        # by default only load padacioso if padatious is not available
-        # save memory if padacioso isnt needed
+        # save memory if padacioso isn't needed

104-109: Enhance logging to inform users when 'PadaciosoService' is disabled

Currently, the log message about the PadaciosoService being disabled is only displayed if the disable_padacioso config option is not set. This may cause users to miss the information if disable_padacioso is explicitly set to True. Consider logging the message whenever PadaciosoService is not loaded, regardless of how disable_padacioso was set.

Apply this diff to adjust the logging condition:

-        elif "disable_padacioso" not in self.config:
+        else:
             LOG.debug("Padacioso pipeline is disabled, only padatious is loaded. "
                       "set 'disable_padacioso': false in mycroft.conf if you want it to load alongside padatious")

This change ensures that users are always informed when PadaciosoService is disabled, providing clearer insight into the system's configuration.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e954576 and a26ea92.

📒 Files selected for processing (1)
  • ovos_core/intent_services/__init__.py (4 hunks)
🔇 Additional comments (3)
ovos_core/intent_services/__init__.py (3)

181-190: Ensure 'padatious_matcher' is properly initialized

The logic for initializing padatious_matcher correctly handles different service availability scenarios:

  • If self._padatious_service is available, it assigns it to padatious_matcher.
  • If self._padatious_service is not available but self._padacioso_service is, it assigns self._padacioso_service to padatious_matcher and logs a warning.
  • If neither service is available, padatious_matcher remains None.

This approach prevents potential AttributeErrors when accessing matchers later in the code.


204-217: Safely update matchers based on available services

The code appropriately checks for the existence of self._padacioso_service and padatious_matcher before adding their matchers to the matchers dictionary. This prevents potential errors when the services are unavailable and ensures that only valid matchers are included.


498-499: Conditional shutdown of 'PadaciosoService'

In the shutdown method, the code correctly checks if self._padacioso_service exists before calling its shutdown() method. This prevents potential AttributeErrors if the service was never initialized.

Copy link

@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: 0

🧹 Outside diff range and nitpick comments (4)
ovos_core/intent_services/__init__.py (4)

102-103: Fix typos in comments for clarity

There are minor typos in the comments that can be corrected for better readability.

Apply this diff to correct the typos:

- # by default only load padacioso is padatious is not available
+ # By default, only load Padacioso if Padatious is not available
- # save memory if padacioso isnt needed
+ # Save memory if Padacioso isn't needed

104-109: Simplify the conditional logic for better readability

The current conditional statements can be streamlined to improve code clarity.

Consider refactoring the logic as follows:

disable_padacioso = self.config.get("disable_padacioso", self._padatious_service is not None)
if not disable_padacioso:
    self._padacioso_service = PadaciosoService(self.bus, self.config["padatious"])
- elif "disable_padacioso" not in self.config:
+ else:
    LOG.debug("Padacioso pipeline is disabled, only Padatious is loaded. "
              "Set 'disable_padacioso': false in mycroft.conf if you want it to load alongside Padatious")

This change uses an else clause instead of checking "disable_padacioso" not in self.config, making the logic clearer.


186-186: Correct typo in logging message

In the log message, "it's place" should be "its place".

Apply this diff to correct the typo:

- LOG.warning("padatious is not available! using padacioso in it's place, "
+ LOG.warning("Padatious is not available! Using Padacioso in its place, "

183-183: Ensure accurate pipeline component checks

The condition any("padatious" in p for p in session.pipeline) might unintentionally match components that contain "padatious" as a substring.

It's better to check for exact matches to avoid false positives.

- needs_pada = any("padatious" in p for p in session.pipeline)
+ needs_pada = any(p == "padatious_high" or p == "padatious_medium" or p == "padatious_low" for p in session.pipeline)

Alternatively, if pipeline component names are standardized, consider using a more precise condition.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between a26ea92 and 9a87227.

📒 Files selected for processing (1)
  • ovos_core/intent_services/__init__.py (4 hunks)
🔇 Additional comments (2)
ovos_core/intent_services/__init__.py (2)

215-220: Verified safe addition of matchers when services are available

The code now correctly checks if padatious_matcher is not None before adding matchers, addressing the previous concern about potential AttributeError.


501-502: Safely shutting down Padacioso service

The inclusion of a check before shutting down self._padacioso_service ensures that the shutdown method is only called if the service has been instantiated.

@JarbasAl JarbasAl merged commit f18b9db into dev Dec 10, 2024
6 of 7 checks passed
@JarbasAl JarbasAl deleted the optimize/padacioso branch December 10, 2024 22:15
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