| # | |
| # 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. | |
| # | |
| # pytype: skip-file | |
| """ | |
| Util/helper functions used in apache_beam.ml.inference. | |
| """ | |
| import os | |
| from collections.abc import Iterable | |
| from typing import Any | |
| from typing import Optional | |
| from typing import Union | |
| import apache_beam as beam | |
| from apache_beam.coders import FloatCoder | |
| from apache_beam.io.fileio import EmptyMatchTreatment | |
| from apache_beam.io.fileio import MatchContinuously | |
| from apache_beam.io.filesystem import FileMetadata | |
| from apache_beam.ml.inference.base import ModelMetadata | |
| from apache_beam.ml.inference.base import PredictionResult | |
| from apache_beam.transforms import trigger | |
| from apache_beam.transforms import window | |
| from apache_beam.transforms.userstate import ReadModifyWriteStateSpec | |
| from apache_beam.utils.timestamp import MAX_TIMESTAMP | |
| from apache_beam.utils.timestamp import Timestamp | |
| # Beam keeps DoFn state per key. Keying each file by its own path gives every | |
| # path a fresh timestamp and never compares models with each other, so every | |
| # watched file is keyed the same way. | |
| _GLOBAL_MODEL_KEY = 'WatchFilePattern' | |
| def _convert_to_result( | |
| batch: Iterable, | |
| predictions: Union[Iterable, dict[Any, Iterable]], | |
| model_id: Optional[str] = None, | |
| ) -> Iterable[PredictionResult]: | |
| if isinstance(predictions, dict): | |
| # Go from one dictionary of type: {key_type1: Iterable<val_type1>, | |
| # key_type2: Iterable<val_type2>, ...} where each Iterable is of | |
| # length batch_size, to a list of dictionaries: | |
| # [{key_type1: value_type1, key_type2: value_type2}] | |
| predictions_per_tensor = [ | |
| dict(zip(predictions.keys(), v)) for v in zip(*predictions.values()) | |
| ] | |
| results = [ | |
| PredictionResult(x, y, model_id) | |
| for x, y in zip(batch, predictions_per_tensor) | |
| ] | |
| else: | |
| results = [ | |
| PredictionResult(x, y, model_id) for x, y in zip(batch, predictions) | |
| ] | |
| if len(results) < len(batch): | |
| raise ValueError( | |
| f'Expected one prediction per input, but got {len(results)} for a ' | |
| f'batch of size {len(batch)}.') | |
| return results | |
| def _as_seconds(timestamp: Union[Timestamp, float]) -> float: | |
| return float(timestamp) | |
| def _pipeline_start_seconds( | |
| start_timestamp: Optional[Union[Timestamp, float]]) -> float: | |
| """Seconds since the epoch for the pipeline construction time. | |
| ``None`` means the time at which the transform is constructed on the machine | |
| that submits the job. The result is stored on the DoFn and serialized with | |
| the pipeline. Workers must not call ``Timestamp.now()`` themselves: that is | |
| when the worker imports this module, which can be minutes later. | |
| """ | |
| if start_timestamp is None: | |
| start_timestamp = Timestamp.now() | |
| return _as_seconds(start_timestamp) | |
| def _is_newer( | |
| candidate: Optional[FileMetadata], | |
| incumbent: Optional[FileMetadata]) -> bool: | |
| """Return true when candidate should replace incumbent as the latest file. | |
| The newest modification time wins. Equal timestamps are broken by path so | |
| the choice is the same no matter which order the files are merged in. | |
| """ | |
| if candidate is None: | |
| return False | |
| if incumbent is None: | |
| return True | |
| candidate_ts = _as_seconds(candidate.last_updated_in_seconds) | |
| incumbent_ts = _as_seconds(incumbent.last_updated_in_seconds) | |
| if candidate_ts != incumbent_ts: | |
| return candidate_ts > incumbent_ts | |
| return candidate.path > incumbent.path | |
| def _with_global_key(metadata: FileMetadata): | |
| return _GLOBAL_MODEL_KEY, metadata | |
| def _model_metadata(path: str) -> ModelMetadata: | |
| model_name = os.path.splitext(os.path.basename(path))[0] | |
| return ModelMetadata(model_id=path, model_name=model_name) | |
| class _LatestFileCombineFn(beam.CombineFn): | |
| """Selects the FileMetadata with the newest modification time. | |
| Internal only; no backwards compatibility. | |
| """ | |
| def create_accumulator(self): | |
| return None | |
| def add_input(self, accumulator, element): | |
| if _is_newer(element, accumulator): | |
| return element | |
| return accumulator | |
| def merge_accumulators(self, accumulators): | |
| merged = None | |
| for accumulator in accumulators: | |
| if _is_newer(accumulator, merged): | |
| merged = accumulator | |
| return merged | |
| def extract_output(self, accumulator): | |
| return accumulator | |
| class _EmitLatestModel(beam.DoFn): | |
| """Emits a model only when it is newer than every model seen so far. | |
| Internal only; no backwards compatibility. | |
| Input is ``(key, FileMetadata)`` and every file shares one key, so this | |
| state is the newest timestamp across the whole watch, not per path. The | |
| first time the newest file is at or before pipeline construction, one | |
| ``ModelMetadata(model_id='')`` is emitted so a streaming side input is | |
| available and ``RunInference`` uses the model configured on the handler. | |
| A later file is emitted only when it is newer than that. Emitting at most | |
| one value per pane keeps the output usable as a singleton side input. | |
| Args: | |
| start_timestamp: Pipeline construction time. Defaults to the time this | |
| DoFn is created. Stored on the instance so a worker uses the submitting | |
| machine's time. | |
| """ | |
| LATEST_TS = ReadModifyWriteStateSpec('latest_ts', FloatCoder()) | |
| def __init__(self, start_timestamp: Optional[Union[Timestamp, float]] = None): | |
| self._start_ts = _pipeline_start_seconds(start_timestamp) | |
| def process(self, element, latest_ts=beam.DoFn.StateParam(LATEST_TS)): | |
| _, file_metadata = element | |
| if file_metadata is None: | |
| return | |
| new_ts = _as_seconds(file_metadata.last_updated_in_seconds) | |
| start_ts = self._start_ts | |
| old_ts = latest_ts.read() | |
| if old_ts is None and new_ts <= start_ts: | |
| # Remember that the configured model was published so a later poll of | |
| # the same files does not publish it again. | |
| latest_ts.write(start_ts) | |
| yield ModelMetadata(model_id='', model_name='') | |
| return | |
| if old_ts is None: | |
| old_ts = start_ts | |
| if new_ts <= old_ts: | |
| return | |
| latest_ts.write(new_ts) | |
| yield _model_metadata(file_metadata.path) | |
| class _SelectLatestModel(beam.PTransform): | |
| """Reduce watched files to singleton updates of the newest model. | |
| Internal only; no backwards compatibility. | |
| Args: | |
| start_timestamp: Pipeline construction time forwarded to | |
| ``_EmitLatestModel``. Defaults to the time this transform is created. | |
| """ | |
| def __init__(self, start_timestamp: Optional[Union[Timestamp, float]] = None): | |
| super().__init__() | |
| self._start_timestamp = ( | |
| Timestamp.now() if start_timestamp is None else start_timestamp) | |
| def expand(self, pcoll) -> beam.PCollection[ModelMetadata]: | |
| # Window first so one poll's files are reduced together. RunInference | |
| # reads this output with AsSingleton, which rejects a pane that contains | |
| # more than one model. The timestamp state above then drops a later file | |
| # whose modification time is older than a model already emitted. | |
| # The early processing-time trigger publishes streaming updates before the | |
| # watermark reaches the end of the global window. The watermark trigger is | |
| # what publishes the pane when a batch pipeline finishes; a processing-time | |
| # trigger alone never fires there, so the side input would stay empty. | |
| return ( | |
| pcoll | |
| | 'AttachKey' >> beam.Map(_with_global_key) | |
| | 'ApplyGlobalWindow' >> beam.WindowInto( | |
| window.GlobalWindows(), | |
| trigger=trigger.AfterWatermark( | |
| early=trigger.AfterProcessingTime(1)), | |
| accumulation_mode=trigger.AccumulationMode.DISCARDING) | |
| | 'LatestFilePerPane' >> beam.CombinePerKey(_LatestFileCombineFn()) | |
| | 'EmitLatestModel' >> beam.ParDo( | |
| _EmitLatestModel(self._start_timestamp))) | |
| class WatchFilePattern(beam.PTransform): | |
| def __init__( | |
| self, | |
| file_pattern, | |
| interval=360, | |
| stop_timestamp=MAX_TIMESTAMP, | |
| start_timestamp: Optional[Union[Timestamp, float]] = None, | |
| ): | |
| """Watches a directory for updates to files matching a given file pattern. | |
| Emits the matching file with the newest modification time, one model per | |
| pane, so the result can be read with | |
| :class:`~apache_beam.pvalue.AsSingleton`. | |
| Args: | |
| file_pattern: The file path to read from, as a local path or a GCS | |
| ``gs://`` path. The path can contain glob characters (``*``, ``?``, | |
| and ``[...]`` sets). | |
| interval: Interval, in seconds, at which to check for files matching | |
| ``file_pattern``. | |
| stop_timestamp: Timestamp after which no more files will be checked. | |
| start_timestamp: Time of pipeline construction. A file last modified at | |
| or before this time is the model already configured on the handler. | |
| Defaults to the time this transform is created. The value is saved on | |
| the transform and sent with the job, so workers compare against this | |
| time rather than the time they start. | |
| **Note**: | |
| 1. Each model update must use a new filename. A path is emitted only once, | |
| and ``RunInference`` reloads a model only when that path changes. To | |
| roll back, upload the desired model under a new name whose modification | |
| time is newer than the model currently in use. A copy that keeps an | |
| older timestamp (for example ``cp -p``) is not selected. On GCS the | |
| timestamp is the object upload time. | |
| 2. A poll may observe several matching files. Only the file with the | |
| newest modification time is emitted. | |
| 3. At least one file matching ``file_pattern`` must exist before the | |
| pipeline starts. Files whose modification time is at or before pipeline | |
| construction produce a single ``ModelMetadata(model_id='')``, meaning | |
| "use the model configured on the handler". That first value makes the | |
| side input available; without it ``RunInference`` buffers its main | |
| input until a newer file is uploaded. | |
| 4. This transform is supported in streaming mode. | |
| ``MatchContinuously`` is an unbounded source, and running it in batch | |
| mode can stall the pipeline or drop updates. | |
| """ | |
| self.file_pattern = file_pattern | |
| self.interval = interval | |
| self.stop_timestamp = stop_timestamp | |
| # Captured here, on the machine that constructs the pipeline, and passed | |
| # into the DoFn so worker startup cannot move this threshold later. | |
| self._start_timestamp = ( | |
| Timestamp.now() if start_timestamp is None else start_timestamp) | |
| def expand(self, pcoll) -> beam.PCollection[ModelMetadata]: | |
| return ( | |
| pcoll | |
| | 'MatchContinuously' >> MatchContinuously( | |
| file_pattern=self.file_pattern, | |
| interval=self.interval, | |
| stop_timestamp=self.stop_timestamp, | |
| empty_match_treatment=EmptyMatchTreatment.DISALLOW) | |
| | 'SelectLatestModel' >> _SelectLatestModel(self._start_timestamp)) |