Download stackgo/tests/test_stackbuild.py from s0duku/the-stack-go: direct link, hf CLI and curl.
- Browser
- Download file 11.1 kB
-
https://huggingface.co/datasets/s0duku/the-stack-go/resolve/main/stackgo/tests/test_stackbuild.py
- Command line
-
hf download hf://datasets/s0duku/the-stack-go/stackgo/tests/test_stackbuild.py
-
curl -L -o test_stackbuild.py https://huggingface.co/datasets/s0duku/the-stack-go/resolve/main/stackgo/tests/test_stackbuild.py
11.1 kB
| import json | |
| from stackbuild import ( | |
| BuildConfig, | |
| Repo, | |
| RepoFile, | |
| SPECIAL_TOKENS, | |
| build_samples, | |
| dependency_components, | |
| scan, | |
| write_jsonl, | |
| ) | |
| from stackbuild.builder import _Counter, _chunks, _file_block, _repository_header | |
| class CharacterTokenizer: | |
| def encode(self, text, add_special_tokens=False): | |
| return list(text) | |
| class TiktokenLikeTokenizer: | |
| def encode(self, text, *, disallowed_special): | |
| assert disallowed_special == () | |
| return list(text.encode()) | |
| class BoundedCharacterTokenizer: | |
| def __init__(self, maximum_input): | |
| self.maximum_input = maximum_input | |
| self.largest_input = 0 | |
| def encode(self, text, add_special_tokens=False): | |
| self.largest_input = max(self.largest_input, len(text)) | |
| if len(text) > self.maximum_input: | |
| raise AssertionError(f"unbounded tokenizer input: {len(text)}") | |
| return range(len(text)) | |
| def demo_repo(): | |
| return Repo("owner/demo", "rev1", ( | |
| RepoFile("README.md", "# Demo\n", "Markdown"), | |
| RepoFile("model.py", "VALUE = 1\n", "Python"), | |
| RepoFile("service.py", "import model\ndef get():\n return model.VALUE\n", "Python"), | |
| RepoFile("main.py", "import service\nprint(service.get())\n", "Python"), | |
| )) | |
| def test_dependency_components_are_complete_and_dependency_ordered(): | |
| info = scan(demo_repo(), use_treesitter=False) | |
| components = dependency_components(info) | |
| assert {frozenset(component) for component in components} == { | |
| frozenset({"model.py", "service.py", "main.py"}), | |
| frozenset({"README.md"}), | |
| } | |
| code = next(component for component in components if "main.py" in component) | |
| assert code.index("model.py") < code.index("service.py") < code.index("main.py") | |
| def test_build_emits_only_dependency_samples_in_dependency_order(): | |
| samples = build_samples( | |
| demo_repo(), config=BuildConfig(max_tokens=1000), tokenizer=CharacterTokenizer(), | |
| ) | |
| assert samples | |
| assert {sample.kind for sample in samples} == {"dependency"} | |
| assert all(sample.token_count == len(sample.text) <= 1000 for sample in samples) | |
| assert {file.path for sample in samples for file in sample.files} == { | |
| "README.md", "model.py", "service.py", "main.py", | |
| } | |
| code = next(sample for sample in samples if any(file.path == "main.py" for file in sample.files)) | |
| paths = [file.path for file in code.files] | |
| assert paths.index("model.py") < paths.index("service.py") < paths.index("main.py") | |
| assert code.text.index("model.py") < code.text.index("service.py") < code.text.index("main.py") | |
| def test_large_file_is_split_at_budget(): | |
| content = "".join(f"line_{index:03d} = {index}\n" for index in range(100)) | |
| repo = Repo("large", "r1", (RepoFile("large.py", content, "Python"),)) | |
| samples = build_samples( | |
| repo, config=BuildConfig(max_tokens=180), tokenizer=CharacterTokenizer(), | |
| ) | |
| assert len(samples) > 1 | |
| assert all(sample.token_count <= 180 for sample in samples) | |
| files = [file for sample in samples for file in sample.files] | |
| assert [file.part for file in files] == list(range(1, files[0].parts + 1)) | |
| def test_huge_file_never_sends_the_whole_file_to_tokenizer(): | |
| tokenizer = BoundedCharacterTokenizer(280_000) | |
| content = ("x" * 1_000_000) + "\n" | |
| samples = build_samples( | |
| Repo("huge", "r1", (RepoFile("huge.txt", content, None),)), | |
| config=BuildConfig(max_tokens=8192), tokenizer=tokenizer, | |
| ) | |
| assert len(samples) > 100 | |
| assert tokenizer.largest_input <= 280_000 | |
| assert all(sample.token_count <= 8192 for sample in samples) | |
| assert sum(file.bytes for sample in samples for file in sample.files) == len(content) | |
| def _reference_chunks(path, content, policy, role, budget, counter): | |
| max_candidate_chars = 262_144 | |
| if len(content) <= max_candidate_chars and counter( | |
| _file_block(path, content, policy, role) | |
| ) <= budget: | |
| return (content,) | |
| def split_block(value): | |
| return _file_block(path, value, policy, role, part=999_999, parts=999_999) | |
| units = [] | |
| for line in content.splitlines(keepends=True): | |
| units.extend(line[index:index + 16_384] for index in range(0, len(line), 16_384)) | |
| chunks, current = [], "" | |
| for unit in units: | |
| candidate = current + unit | |
| if len(candidate) <= max_candidate_chars and counter(split_block(candidate)) <= budget: | |
| current = candidate | |
| continue | |
| if current: | |
| chunks.append(current) | |
| current = "" | |
| while unit: | |
| if counter(split_block(unit)) <= budget: | |
| current = unit | |
| break | |
| low, high, chars = 1, len(unit), 0 | |
| while low <= high: | |
| middle = (low + high) // 2 | |
| if counter(split_block(unit[:middle])) <= budget: | |
| chars, low = middle, middle + 1 | |
| else: | |
| high = middle - 1 | |
| assert chars | |
| chunks.append(unit[:chars]) | |
| unit = unit[chars:] | |
| if current: | |
| chunks.append(current) | |
| return tuple(chunks) | |
| def test_chunk_binary_search_preserves_exact_greedy_boundaries(): | |
| content = "".join(f"row_{index:04d} = {index}\n" for index in range(2_000)) | |
| counter = _Counter(CharacterTokenizer()) | |
| budget = 900 - counter(_repository_header(Repo("repo", "rev", ()))) | |
| expected = _reference_chunks("large.py", content, "full", "source", budget, counter) | |
| actual = _chunks("large.py", content, "full", "source", "Python", budget, counter) | |
| assert actual == expected | |
| def test_notebook_is_structured_and_binary_is_metadata_only(): | |
| notebook = json.dumps({ | |
| "metadata": {"kernelspec": {"language": "python"}}, | |
| "cells": [ | |
| {"cell_type": "markdown", "source": ["Explain x"]}, | |
| {"cell_type": "code", "source": ["x = 1\n"], | |
| "outputs": [{"output_type": "stream", "text": ["1\n"]}]}, | |
| ], | |
| }) | |
| repo = Repo("mixed", "r1", ( | |
| RepoFile("analysis.ipynb", notebook, "Jupyter Notebook"), | |
| RepoFile("image.png", b"\x89PNG\x00payload", None), | |
| )) | |
| samples = build_samples(repo, config=BuildConfig(max_tokens=1_000)) | |
| text = "".join(sample.text for sample in samples) | |
| assert "# Cell: Markdown\nExplain x" in text | |
| assert "# Cell: Code\nx = 1" in text | |
| assert "# Cell: Output\n1" in text | |
| assert "<|begin▁of▁file▁name|>image.png<|end▁of▁file▁name|>" in text | |
| binary = next(file for sample in samples for file in sample.files if file.path == "image.png") | |
| assert binary.content_policy == "metadata_only" | |
| def test_isolated_files_are_packed_instead_of_one_sample_each(): | |
| repo = Repo("isolated", "r1", tuple( | |
| RepoFile(f"docs/{index}.md", f"document {index}\n", "Markdown") | |
| for index in range(20) | |
| )) | |
| samples = build_samples( | |
| repo, config=BuildConfig(max_tokens=400), tokenizer=CharacterTokenizer(), | |
| ) | |
| assert len(samples) < 20 | |
| assert {file.path for sample in samples for file in sample.files} == { | |
| f"docs/{index}.md" for index in range(20) | |
| } | |
| def test_tiktoken_style_encoder_is_supported(): | |
| samples = build_samples( | |
| demo_repo(), config=BuildConfig(max_tokens=400), tokenizer=TiktokenLikeTokenizer(), | |
| ) | |
| assert samples | |
| assert all(sample.token_count <= 400 for sample in samples) | |
| def test_one_shot_repo_file_iterable_is_supported(): | |
| files = (file for file in ( | |
| RepoFile("base.py", "VALUE = 1\n", "Python"), | |
| RepoFile("main.py", "import base\nprint(base.VALUE)\n", "Python"), | |
| )) | |
| samples = build_samples( | |
| Repo("generator", "r1", files), config=BuildConfig(max_tokens=400), | |
| tokenizer=CharacterTokenizer(), | |
| ) | |
| assert {file.path for sample in samples for file in sample.files} == {"base.py", "main.py"} | |
| def test_samples_round_trip_as_jsonl(tmp_path): | |
| samples = build_samples( | |
| demo_repo(), config=BuildConfig(max_tokens=400), tokenizer=CharacterTokenizer(), | |
| ) | |
| target = tmp_path / "samples.jsonl" | |
| write_jsonl(samples, target) | |
| rows = [json.loads(line) for line in target.read_text().splitlines()] | |
| assert [row["sample_id"] for row in rows] == [sample.sample_id for sample in samples] | |
| assert rows[0]["files"][0]["role"] | |
| def test_samples_use_only_repository_and_file_special_tokens(): | |
| repo = Repo("comments", "rev", ( | |
| RepoFile("a.py", "VALUE = 1\n", "Python"), | |
| RepoFile("b.ts", "export const value = 1;\n", "TypeScript"), | |
| RepoFile("README.md", "Documentation\n", "Markdown"), | |
| )) | |
| text = "".join(sample.text for sample in build_samples( | |
| repo, config=BuildConfig(max_tokens=1_000), tokenizer=CharacterTokenizer(), | |
| )) | |
| assert len(SPECIAL_TOKENS) == 6 | |
| assert "<|begin▁of▁repo▁name|>comments<|end▁of▁repo▁name|>\n" in text | |
| assert "<|begin▁of▁file▁name|>a.py<|end▁of▁file▁name|>" in text | |
| assert "<|begin▁of▁file▁name|>b.ts<|end▁of▁file▁name|>" in text | |
| assert "<|begin▁of▁file▁name|>README.md<|end▁of▁file▁name|>" in text | |
| assert "rev" not in text | |
| assert "<|endoftext|>" not in text | |
| assert "<|fim_" not in text | |
| custom_tokens = { | |
| "<|" + part.split("|>", 1)[0] + "|>" | |
| for part in text.split("<|")[1:] | |
| } | |
| assert custom_tokens == set(SPECIAL_TOKENS) | |
| samples = build_samples( | |
| repo, config=BuildConfig(max_tokens=1_000), tokenizer=CharacterTokenizer(), | |
| ) | |
| assert all(sample.text.startswith("<|begin▁of▁repo▁name|>comments<|end▁of▁repo▁name|>\n") for sample in samples) | |
| assert all( | |
| sample.text.count("<|begin▁of▁file▁name|>") == len(sample.files) | |
| for sample in samples | |
| ) | |
| def test_target_context_samples_select_targets_and_put_dependencies_first(): | |
| samples = build_samples( | |
| demo_repo(), | |
| config=BuildConfig( | |
| max_tokens=1_000, target_context=True, max_targets_per_repo=8, | |
| max_dependency_depth=3, | |
| ), | |
| tokenizer=CharacterTokenizer(), | |
| ) | |
| contexts = [sample for sample in samples if sample.kind == "target_context"] | |
| assert contexts | |
| main = next(sample for sample in contexts if sample.metadata["target_path"] == "main.py") | |
| paths = [file.path for file in main.files] | |
| assert paths[-1] == "main.py" | |
| assert paths.index("model.py") < paths.index("service.py") < paths.index("main.py") | |
| assert main.files[-1].relation == "target" | |
| assert all(sample.token_count <= 1_000 for sample in contexts) | |
| def test_target_context_skips_target_only_duplicates(): | |
| repo = Repo("single", "r1", ( | |
| RepoFile("src/only.py", "VALUE = 1\n", "Python"), | |
| )) | |
| samples = build_samples( | |
| repo, | |
| config=BuildConfig(target_context=True, max_targets_per_repo=8), | |
| tokenizer=CharacterTokenizer(), | |
| ) | |
| assert len(samples) == 1 | |
| assert samples[0].kind == "dependency" | |