-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Support for RateLimiter in Beam Remote Model Handler #37218
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
Changes from 17 commits
26f23ca
92fd0cd
c3ef5a4
5369f42
4967fe7
6868614
a42a97d
7cbba0f
fa30212
a548fef
c71edca
167d484
8239ccb
1f23f7f
84d2457
c1cf403
058bf43
7d50f60
1756bef
6ee520a
f1fa3e6
d5cdbfd
2c82420
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| # (the "License"); you may not use this file except in compliance with | ||
| # the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| """A simple example demonstrating usage of the EnvoyRateLimiter with Vertex AI. | ||
| """ | ||
|
|
||
| import argparse | ||
| import logging | ||
|
|
||
| import apache_beam as beam | ||
| from apache_beam.io.components.rate_limiter import EnvoyRateLimiter | ||
| from apache_beam.ml.inference.base import RunInference | ||
| from apache_beam.ml.inference.vertex_ai_inference import VertexAIModelHandlerJSON | ||
| from apache_beam.options.pipeline_options import PipelineOptions | ||
| from apache_beam.options.pipeline_options import SetupOptions | ||
|
|
||
|
|
||
| def run(argv=None): | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument( | ||
| '--project', | ||
| dest='project', | ||
| help='The Google Cloud project ID for Vertex AI.') | ||
| parser.add_argument( | ||
| '--location', | ||
| dest='location', | ||
| help='The Google Cloud location (e.g. us-central1) for Vertex AI.') | ||
| parser.add_argument( | ||
| '--endpoint_id', | ||
| dest='endpoint_id', | ||
| help='The ID of the Vertex AI endpoint.') | ||
| parser.add_argument( | ||
| '--rls_address', | ||
| dest='rls_address', | ||
| help='The address of the Envoy Rate Limit Service (e.g. localhost:8081).') | ||
|
|
||
| known_args, pipeline_args = parser.parse_known_args(argv) | ||
| pipeline_options = PipelineOptions(pipeline_args) | ||
| pipeline_options.view_as(SetupOptions).save_main_session = True | ||
|
|
||
| # Initialize the EnvoyRateLimiter | ||
| rate_limiter = EnvoyRateLimiter( | ||
| service_address=known_args.rls_address, | ||
| domain="mongo_cps", | ||
| descriptors=[{ | ||
| "database": "users" | ||
| }], | ||
| namespace='example_pipeline') | ||
|
|
||
| # Initialize the VertexAIModelHandler with the rate limiter | ||
| model_handler = VertexAIModelHandlerJSON( | ||
| endpoint_id=known_args.endpoint_id, | ||
| project=known_args.project, | ||
| location=known_args.location, | ||
| rate_limiter=rate_limiter) | ||
|
|
||
| # Input features for the model | ||
| features = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], | ||
| [10.0, 11.0, 12.0], [13.0, 14.0, 15.0]] | ||
|
|
||
| with beam.Pipeline(options=pipeline_options) as p: | ||
| _ = ( | ||
| p | ||
| | 'CreateInputs' >> beam.Create(features) | ||
| | 'RunInference' >> RunInference(model_handler) | ||
| | 'PrintPredictions' >> beam.Map(logging.info)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| logging.getLogger().setLevel(logging.INFO) | ||
| run() |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2071,6 +2071,70 @@ def run_inference(self, | |||||||||||||||||||||||||||||||||
| responses.append(model.predict(example)) | ||||||||||||||||||||||||||||||||||
| return responses | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def test_run_inference_with_rate_limiter(self): | ||||||||||||||||||||||||||||||||||
| class FakeRateLimiter(base.RateLimiter): | ||||||||||||||||||||||||||||||||||
| def __init__(self): | ||||||||||||||||||||||||||||||||||
| super().__init__(namespace='test_namespace') | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def throttle(self, hits_added=1): | ||||||||||||||||||||||||||||||||||
| self.requests_counter.inc() | ||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+2079
to
+2081
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is going back to the base implementation of the rate limiter, but throttle() returning True logically makes me think that the request should be throttled, not that it's approved. See the adaptive throttler definition of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Context is different in AdaptiveThrottler and RateLimiter. In AdaptiveThrottler throttling is always possible throttle_request is giving caller info to either apply throttle delay or allow. In RateLimiter throttling might not be always possible, and if possible, delay is already applied by RateLimiter. So true indicates that it is throttled and false indicates its not. If the naming of throttle() is confusing for people coming from AdaptiveThrottler context we can change it to allow/acquire()
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, I'm still not following this logic based based on the description in the RateLimiter class: beam/sdks/python/apache_beam/io/components/rate_limiter.py Lines 63 to 77 in 6bedec3
"Check if request should be throttled" as a docstring, but then responding with a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing to the source of confusion. I have updated the doc string to set the expectations of the functions. |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| limiter = FakeRateLimiter() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| with TestPipeline() as pipeline: | ||||||||||||||||||||||||||||||||||
| examples = [1, 5] | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| class ConcreteRemoteModelHandler(base.RemoteModelHandler): | ||||||||||||||||||||||||||||||||||
| def create_client(self): | ||||||||||||||||||||||||||||||||||
| return FakeModel() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def request(self, batch, model, inference_args=None): | ||||||||||||||||||||||||||||||||||
| return [model.predict(example) for example in batch] | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| model_handler = ConcreteRemoteModelHandler( | ||||||||||||||||||||||||||||||||||
| rate_limiter=limiter, namespace='test_namespace') | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| pcoll = pipeline | 'start' >> beam.Create(examples) | ||||||||||||||||||||||||||||||||||
| actual = pcoll | base.RunInference(model_handler) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| expected = [2, 6] | ||||||||||||||||||||||||||||||||||
| assert_that(actual, equal_to(expected)) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| result = pipeline.run() | ||||||||||||||||||||||||||||||||||
| result.wait_until_finish() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| metrics_filter = MetricsFilter().with_name( | ||||||||||||||||||||||||||||||||||
| 'RatelimitRequestsTotal').with_namespace('test_namespace') | ||||||||||||||||||||||||||||||||||
| metrics = result.metrics().query(metrics_filter) | ||||||||||||||||||||||||||||||||||
| self.assertGreaterEqual(metrics['counters'][0].committed, 0) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def test_run_inference_with_rate_limiter_exceeded(self): | ||||||||||||||||||||||||||||||||||
| class FakeRateLimiter(base.RateLimiter): | ||||||||||||||||||||||||||||||||||
| def __init__(self): | ||||||||||||||||||||||||||||||||||
| super().__init__(namespace='test_namespace') | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def throttle(self, hits_added=1): | ||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| with self.assertRaises(RuntimeError): | ||||||||||||||||||||||||||||||||||
| with TestPipeline() as pipeline: | ||||||||||||||||||||||||||||||||||
| examples = [1] | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| class ConcreteRemoteModelHandler(base.RemoteModelHandler): | ||||||||||||||||||||||||||||||||||
| def create_client(self): | ||||||||||||||||||||||||||||||||||
| return FakeModel() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def request(self, batch, model, inference_args=None): | ||||||||||||||||||||||||||||||||||
| return [model.predict(example) for example in batch] | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| model_handler = ConcreteRemoteModelHandler( | ||||||||||||||||||||||||||||||||||
| rate_limiter=FakeRateLimiter(), | ||||||||||||||||||||||||||||||||||
| namespace='test_namespace', | ||||||||||||||||||||||||||||||||||
| num_retries=0) | ||||||||||||||||||||||||||||||||||
| pcoll = pipeline | 'start' >> beam.Create(examples) | ||||||||||||||||||||||||||||||||||
| _ = pcoll | base.RunInference(model_handler) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if __name__ == '__main__': | ||||||||||||||||||||||||||||||||||
| unittest.main() | ||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.