|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +"""Tests for the local tokenizer install extras declared in pyproject.toml.""" |
| 16 | + |
| 17 | +import os |
| 18 | +import pathlib |
| 19 | +import re |
| 20 | +import tomllib |
| 21 | + |
| 22 | +_TRANSFORMERS_STACK = frozenset( |
| 23 | + {'pillow', 'torch', 'torchvision', 'transformers'} |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +def _pyproject_path() -> pathlib.Path: |
| 28 | + """Finds pyproject.toml, which the public export moves to the repository root.""" |
| 29 | + candidates = [] |
| 30 | + srcdir = os.environ.get('TEST_SRCDIR') |
| 31 | + if srcdir: |
| 32 | + candidates.append( |
| 33 | + pathlib.Path(srcdir) |
| 34 | + / 'google3/third_party/py/google/genai/packaging/pyproject.toml' |
| 35 | + ) |
| 36 | + for parent in pathlib.Path(__file__).resolve().parents: |
| 37 | + candidates.append(parent / 'packaging' / 'pyproject.toml') |
| 38 | + candidates.append(parent / 'pyproject.toml') |
| 39 | + for candidate in candidates: |
| 40 | + if candidate.is_file(): |
| 41 | + return candidate |
| 42 | + raise FileNotFoundError('cannot find pyproject.toml') |
| 43 | + |
| 44 | + |
| 45 | +def _extra_packages(extra_name: str) -> set[str]: |
| 46 | + """Returns the distribution names listed in one pyproject.toml extra.""" |
| 47 | + with _pyproject_path().open('rb') as pyproject_file: |
| 48 | + pyproject = tomllib.load(pyproject_file) |
| 49 | + extras = pyproject['project']['optional-dependencies'] |
| 50 | + return {re.split(r'[<>=!~;\[ ]', spec)[0] for spec in extras[extra_name]} |
| 51 | + |
| 52 | + |
| 53 | +def test_local_tokenizer_extra_excludes_transformers_stack(): |
| 54 | + """A SentencePiece model installs without Transformers and PyTorch.""" |
| 55 | + packages = _extra_packages('local-tokenizer') |
| 56 | + |
| 57 | + assert 'sentencepiece' in packages |
| 58 | + assert 'protobuf' in packages |
| 59 | + assert (packages & _TRANSFORMERS_STACK) == set() |
| 60 | + |
| 61 | + |
| 62 | +def test_local_tokenizer_transformers_extra_has_full_stack(): |
| 63 | + """The Gemma 4 extra stays a superset of the SentencePiece extra.""" |
| 64 | + packages = _extra_packages('local-tokenizer-transformers') |
| 65 | + |
| 66 | + assert packages >= {'sentencepiece', 'protobuf'} | _TRANSFORMERS_STACK |
0 commit comments