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

Edge only inference with cloud training #101

Merged
merged 36 commits into from
Oct 8, 2024
Merged

Edge only inference with cloud training #101

merged 36 commits into from
Oct 8, 2024

Conversation

f-wright
Copy link
Collaborator

@f-wright f-wright commented Oct 7, 2024

[COM-1567] This PR adds an edge_only_inference mode which allows the model to escalate to the cloud for future training, while still always returning the edge inference answer for fast results.

Tested with 3 binary detectors with

  1. edge_only enabled -- nothing escalated to cloud
  2. edge_only_inference enabled -- low confidence IQs escalated to cloud, edge answer returned
  3. Neither edge_only nor edge_only_inference enabled -- low confidence edge IQs escalated to cloud and cloud answer returned.
  4. edge_only and edge_only_inference enabled -- pods do not launch, logs show the config validation error

Currently not adding many unit tests as they aren't set up to test post_image_query, and we want to get this change out quickly (discussed with Tyler).

This is a first step -- in the future we may want to add:

  1. A task queueing system, instead of using FastAPI background tasks
  2. Rate limiting for IQs sent to the cloud (either add an element of randomness for whether a query is sent to the cloud, or limit escalation to a certain number of queries over a period of time)

f-wright and others added 30 commits October 4, 2024 15:50
@f-wright f-wright marked this pull request as ready for review October 8, 2024 19:54
@f-wright f-wright changed the title Edge only inference + cloud training Edge only inference with cloud training Oct 8, 2024
Copy link
Contributor

@CoreyEWood CoreyEWood left a comment

Choose a reason for hiding this comment

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

This looks great overall! Awesome functionality to have. I just left a few comments about making sure things aren't confusing if external users are going to be using the edge-only modes.

As an additional thought, I wonder if we should change the default refresh_rate so that it looks for new model binaries for frequently - the current default of 120 seconds is pretty slow, and users might want to see improvements more quickly if they're sending a lot of queries.

@@ -50,23 +50,32 @@ print(f"The answer is {image_query.result}")
See the [SDK's getting started guide](https://code.groundlight.ai/python-sdk/docs/getting-started) for more info.

### Experimental: getting only edge model answers
If you only want to receive answers from the edge model for a detector, you can enable edge-only mode for it. To do this, edit the detector's configuration in the [edge config file](./configs/edge-config.yaml) like so:
If you only want to receive answers from the edge model for a detector, you can enable edge-only mode for it. This will prevent the edge endpoint from sending image queries to the cloud API. If you want fast edge answers regardless of confidence but still want the edge model to improve, you can enable edge-only inference for that detector. This mode will always return the edge model's answer, but it will also submit low confidence image queries to the cloud API for training.
Copy link
Contributor

@CoreyEWood CoreyEWood Oct 8, 2024

Choose a reason for hiding this comment

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

I wonder if there's a different naming we could have for these different modes - I worry that the difference between "edge-only mode" and "edge-only inference" would be unclear to anyone external. Depending on how experimental/temporary these are, this might not matter, but if we expect external customers to use these at some point I think having more clear names might be helpful.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree that it's confusing. Maybe I could change them to edge_only_no_training and edge_only_cloud_training? That gets a little wordy, but might be more clear. Definitely open to naming suggestions, wasn't sure what to call it.

README.md Outdated
- detector_id: 'det_abc'
motion_detection_template: "default"
local_inference_template: "default"
```
In this example, `det_xyz` will have edge-only mode enabled because `edge_only` is set to `true`. If `edge_only` is not specified, it defaults to false, so `det_abc` will have edge-only mode disabled.
In this example, `det_xyz` will have edge-only mode enabled because `edge_only` is set to `true`. `det_ijk` will have edge-only inference enabled because `edge_only_inference` is set to `true`. If `edge_only` or `edge_only_inference` are not specified, they defaults to false, so `det_abc` will have edge-only mode disabled. Only one of `edge_only` or `edge_only_inference` can be set to `true` for a detector.
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a tiny typo but this says "they defaults to false".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oops thank you!

README.md Outdated

With edge-only mode enabled for a detector, when you make requests to it, you will only receive answers from the edge model (regardless of the confidence). Additionally, note that no image queries submitted this way will show up in the web app or be used to train the model. This option should therefore only be used if you don't need the model to improve and only want fast answers from the edge model.

If edge-only mode is enabled on a detector and the edge inference model for that detector is not available, attempting to send image queries to that detector will return a 500 error response.
With edge-only inference enabled for a detector, when you make requests to it, you will only receive answers from the edge model (regardless of the confidence). However, image queries submitted this way with confidences below the threshold will be used to train the model. This option should be used when you want fast edge answers regardless of confidence but still want the model to improve.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe to be more clear this should say something like, "However, when the edge model makes a prediction with confidence below the threshold, that image query will also be escalated to the cloud and used to train the model"?

from app.core.configs import DetectorConfig


def test_detector_config():
Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome to have this test, maybe it would be good to name it something more specific like "test_detector_config_with_both_edge_modes" in case we want to add more detector config tests in the future?

Comment on lines +229 to +234
if edge_only_inference and not _is_confident_enough(
confidence=confidence,
confidence_threshold=confidence_threshold,
):
logger.info("Escalating to the cloud API server for future training due to low confidence.")
background_tasks.add_task(safe_call_sdk, gl.ask_async, detector=detector_id, image=image)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is great! Love how simple this is.

Copy link
Collaborator

@brandon-groundlight brandon-groundlight left a comment

Choose a reason for hiding this comment

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

Looks good to me. I do think deduping the images will become important in the near future, but for internal demos this should be great

@f-wright f-wright merged commit 3755ba4 into main Oct 8, 2024
6 checks passed
@f-wright f-wright deleted the edge-with-training branch October 8, 2024 23:07
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.

3 participants