AMOOOMA commented on code in PR #40907:
URL: https://github.com/apache/beam/pull/40907#discussion_r4486349627


##########
sdks/python/apache_beam/ml/inference/autoscaling_metrics.py:
##########
@@ -0,0 +1,575 @@
+#
+# 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.
+#
+
+"""Capacity-based autoscaling signal for model handlers that run a model 
server.
+
+Dataflow's GPU autoscaling reads the process-wide distribution
+``num_loaded_models_<tag>`` in the ``BeamML_ModelManager`` namespace as how
+many requests each worker can handle, and sizes the pool roughly as
+``ceil(backlog / value)``. ``ModelManager`` publishes the number of loaded
+model copies there. A handler that serves inference through an out-of-process
+model server (vLLM, Triton, ...) always has exactly one copy per worker, so it
+publishes an estimate of the worker's capacity under that metric instead.
+
+Nothing here depends on a particular model server. A handler for a model server
+that exposes a completed-requests counter and an in-flight gauge creates a
+:class:`NumLoadedModelsPublisher`, supplying only what it knows: the two metric
+names, how to find the server's port, and the RunInference model identifier::
+
+  publisher = NumLoadedModelsPublisher(
+      metrics_port_fn=lambda: server.port if server.is_up else None,
+      completed_metric='vllm:request_success_total',
+      running_metric='vllm:num_requests_running',
+      model_identifier=model_identifier,
+      model_name=model_name,
+      config=autoscaling_config)  # An AutoscalingMetricConfig, or None.
+  publisher.start()  # Once the server is reachable; publisher.stop() at exit.
+
+The pieces:
+
+* :class:`NumLoadedModelsPublisher` reads the server, estimates its capacity
+  and publishes it from a background thread.
+* :class:`CompletionRateCapacityEstimator` is the estimate itself: pure
+  computation on the counter and the gauge, no I/O.
+* :class:`AutoscalingMetricConfig` holds the user-facing knobs. Plain data.
+* :func:`scrape_prometheus_metrics` and :func:`parse_prometheus_metric` read a
+  server's Prometheus ``/metrics`` endpoint.
+"""
+
+# pytype: skip-file
+
+import logging
+import re
+import threading
+import time
+import urllib.request
+from collections.abc import Callable
+from collections.abc import Mapping
+from dataclasses import dataclass
+from typing import NamedTuple
+from typing import Optional
+
+from apache_beam.metrics.execution import MetricsEnvironment
+from apache_beam.metrics.metricbase import MetricName
+
+__all__ = [
+    'AutoscalingMetricConfig',
+    'CompletionRateCapacityEstimator',
+    'NumLoadedModelsPublisher',
+    'parse_prometheus_metric',
+    'sanitize_metric_tag',
+    'scrape_prometheus_metrics',

Review Comment:
   Ah yes, we can only expose two for the user, the others can be private.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to