|
| 1 | +<!-- |
| 2 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +you may not use this file except in compliance with the License. |
| 4 | +You may obtain a copy of the License at |
| 5 | +
|
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +
|
| 8 | +Unless required by applicable law or agreed to in writing, software |
| 9 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +See the License for the specific language governing permissions and |
| 12 | +limitations under the License. |
| 13 | +--> |
| 14 | + |
| 15 | +# MLTransform Examples |
| 16 | + |
| 17 | +This directory contains Apache Beam examples for MLTransform pipelines. |
| 18 | + |
| 19 | +## MLTransform - Generate Vocab (Batch only) |
| 20 | + |
| 21 | +`mltransform_generate_vocab.py` builds a vocabulary artifact from batch input |
| 22 | +rows using `MLTransform` + `ComputeAndApplyVocabulary`. |
| 23 | + |
| 24 | +### What it does |
| 25 | + |
| 26 | +1. Reads input rows from JSONL (`--input_file`) or BigQuery (`--input_table`). |
| 27 | +2. Extracts specified columns (`--columns`). |
| 28 | +3. Normalizes text (`trim`, optional lowercasing). |
| 29 | +4. Tokenizes text (`whitespace` or `regex` tokenizer). |
| 30 | +5. Runs `ComputeAndApplyVocabulary` with top-k and min-frequency constraints. |
| 31 | +6. Ensures `--oov_token` is included first. |
| 32 | +7. Writes the vocabulary as one token per line. |
| 33 | + |
| 34 | +### Required arguments |
| 35 | + |
| 36 | +- `--output_vocab` |
| 37 | +- `--columns` |
| 38 | +- and one of: |
| 39 | + - `--input_file` |
| 40 | + - `--input_table` |
| 41 | + |
| 42 | +### Optional arguments |
| 43 | + |
| 44 | +- `--vocab_size` (default: `50000`) |
| 45 | +- `--min_frequency` (default: `1`) |
| 46 | +- `--lowercase` (default: `true`) |
| 47 | +- `--tokenizer` (`whitespace` or `regex`, default: `whitespace`) |
| 48 | +- `--oov_token` (default: `<UNK>`) |
| 49 | +- `--input_expand_factor` (default: `1`, useful for perf/load testing) |
| 50 | + |
| 51 | +### Local batch example |
| 52 | + |
| 53 | +```sh |
| 54 | +python -m apache_beam.examples.ml_transform.mltransform_generate_vocab \ |
| 55 | + --input_file=/tmp/input.jsonl \ |
| 56 | + --output_vocab=/tmp/vocab.txt \ |
| 57 | + --columns=text,category \ |
| 58 | + --vocab_size=5 \ |
| 59 | + --min_frequency=1 \ |
| 60 | + --lowercase=true \ |
| 61 | + --tokenizer=whitespace \ |
| 62 | + --oov_token=<UNK> \ |
| 63 | + --input_expand_factor=1 \ |
| 64 | + --runner=DirectRunner |
| 65 | +``` |
| 66 | + |
| 67 | +### Input format |
| 68 | + |
| 69 | +JSONL input with object rows, for example: |
| 70 | + |
| 71 | +```json |
| 72 | +{"id":"1","text":"Beam beam ML pipeline"} |
| 73 | +{"id":"2","text":"Beam pipeline dataflow"} |
| 74 | +{"id":"3","text":"ML transform beam"} |
| 75 | +{"id":"4","text":"vocab vocab vocab test"} |
| 76 | +{"id":"5","text":"rare_token_once"} |
| 77 | +{"id":"6","text":""} |
| 78 | +{"id":"7","text":null} |
| 79 | +``` |
| 80 | + |
| 81 | +The integration tests in `mltransform_generate_vocab_test.py` generate this |
| 82 | +sample data programmatically. |
| 83 | + |
| 84 | +### Output format |
| 85 | + |
| 86 | +One token per line: |
| 87 | + |
| 88 | +1. `oov_token` first |
| 89 | +2. remaining tokens follow the vocabulary order produced by |
| 90 | + `ComputeAndApplyVocabulary`. |
| 91 | + |
| 92 | +Example output: |
| 93 | + |
| 94 | +```txt |
| 95 | +<UNK> |
| 96 | +beam |
| 97 | +ml |
| 98 | +``` |
| 99 | + |
| 100 | +For this sample and config: |
| 101 | + |
| 102 | +```sh |
| 103 | +--columns=text --min_frequency=2 --vocab_size=3 |
| 104 | +``` |
| 105 | + |
| 106 | +the expected output is: |
| 107 | + |
| 108 | +```txt |
| 109 | +<UNK> |
| 110 | +beam |
| 111 | +vocab |
| 112 | +ml |
| 113 | +``` |
| 114 | + |
| 115 | +### Empty vocabulary behavior |
| 116 | + |
| 117 | +If all tokens are filtered out by `--min_frequency`, the pipeline writes only |
| 118 | +the reserved `--oov_token` and logs a warning. |
| 119 | + |
| 120 | +### Additional test datasets |
| 121 | + |
| 122 | +Test data for happy path and null/empty/missing columns is generated inline in |
| 123 | +`mltransform_generate_vocab_test.py`. |
| 124 | + |
| 125 | +### Performance testing pattern |
| 126 | + |
| 127 | +- Small local files: functional correctness and output-stability tests. |
| 128 | +- Large GCS files (or moderate file + `--input_expand_factor`): throughput/cost |
| 129 | + benchmarking on Dataflow. |
| 130 | + |
0 commit comments