Skip to content

[Tests] Extract shared test_result helper into tests/_utils.py#234

Open
YWHyuk wants to merge 2 commits into
developfrom
refactor/tests-shared-helper
Open

[Tests] Extract shared test_result helper into tests/_utils.py#234
YWHyuk wants to merge 2 commits into
developfrom
refactor/tests-shared-helper

Conversation

@YWHyuk
Copy link
Copy Markdown
Collaborator

@YWHyuk YWHyuk commented May 22, 2026

Summary

  • 48 test files each had a near-identical ~12-line def test_result(name, out, cpu_out, ...) block, with two signature variants (cpu_out vs ref) and three slightly divergent body variants (including a 'pass message only' bug in some files). Net duplication: ~560 lines.
  • Adds tests/_utils.py with a single canonical test_result and migrates every file to from tests._utils import test_result.
  • The helper keeps positional-argument compatibility, so caller sites do not need updates.

Mechanical migration was applied uniformly; sample-verified locally on test_add, test_exponent, test_sort, Fusion/test_matmul_vector, Fusion/test_attention_fusion — all pass.

Net diff: 49 files changed, 208 insertions(+), 720 deletions(-).

🤖 Generated with Claude Code

Comment thread tests/Diffusion/test_diffusion.py Outdated
args = parser.parse_args()

sys.path.append(os.environ.get("TORCHSIM_DIR", "/workspace/PyTorchSim"))
from tests._utils import test_result
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place these before the imports if possible.

@YWHyuk YWHyuk force-pushed the refactor/tests-shared-helper branch from 73ab1b4 to c08e1dc Compare May 26, 2026 05:21
YWHyuk added a commit that referenced this pull request May 26, 2026
tests/ was a flat mix of op-level files, single-file model tests, and
multi-file model directories with inconsistent capitalization. This PR
introduces a hierarchy:

  tests/
    _pytorchsim_utils.py
    ops/
      elementwise/  reduce/  gemm/  conv/  attention/
      view/         sort/    sparsity/  misc/  fusion/
    models/
      DeepSeek/  Diffusion/  Llama/  MLP/  MoE/
      MobileNet/  Mixtral8x7B/  Yolov5/
      test_mlp.py  test_resnet.py  test_single_perceptron.py
      test_transformer.py  test_vit.py
    system/
      test_eager.py  test_hetro.py  test_scheduler.py
      test_stonne.py  test_vectorops.py

Mixtral_8x7B renamed to Mixtral8x7B for consistency with the other
PascalCase model dirs. Existing single-file model dirs are kept as
dirs (they may grow companion files like the Mixtral model.py).

All file moves use git mv to preserve history. External path
references rewritten across .github/workflows/pytorchsim_test.yml,
README.md, CLAUDE.md, .github/ISSUE_TEMPLATE/bug_report.md, and the
scripts/{sparsity,stonne}_experiment/ shell scripts.

Cross-test imports updated to drop the `tests.` prefix (because PR #234
puts `<repo>/tests` on sys.path[0] to avoid the ultralytics top-level
`tests` package collision):
- tests/system/test_hetro.py
- tests/system/test_scheduler.py
- tests/system/test_vectorops.py
- tests/ops/sparsity/test_sparsity.py

__init__.py added to each new subdir so e.g. `from ops.elementwise.test_add
import test_vectoradd` resolves.

Six files (test_resnet, test_vit, test_stonne, test_sparse_core,
test_gqa, test_diffusion) had their `sys.path.append` inside an
`if __name__ == "__main__":` block, which made the PR-A migration
script insert the import at the wrong indent level. Moved the
sys.path/import block to module top.

Sample-verified locally on tests/ops/elementwise/test_add.py and
tests/ops/fusion/test_matmul_vector.py (both pass) plus import-only
checks on the cross-import files.

Stacks on top of PR #234 (tests/_pytorchsim_utils.py extraction).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@YWHyuk YWHyuk force-pushed the refactor/tests-shared-helper branch from c08e1dc to 5ca0455 Compare May 26, 2026 06:12
YWHyuk added a commit that referenced this pull request May 26, 2026
tests/ was a flat mix of op-level files, single-file model tests, and
multi-file model directories with inconsistent capitalization. This PR
introduces a hierarchy:

  tests/
    _pytorchsim_utils.py
    ops/
      elementwise/  reduce/  gemm/  conv/  attention/
      view/         sort/    sparsity/  misc/  fusion/
    models/
      DeepSeek/  Diffusion/  Llama/  MLP/  MoE/
      MobileNet/  Mixtral8x7B/  Yolov5/
      test_mlp.py  test_resnet.py  test_single_perceptron.py
      test_transformer.py  test_vit.py
    system/
      test_eager.py  test_hetro.py  test_scheduler.py
      test_stonne.py  test_vectorops.py

Mixtral_8x7B renamed to Mixtral8x7B for consistency with the other
PascalCase model dirs. Existing single-file model dirs are kept as
dirs (they may grow companion files like the Mixtral model.py).

All file moves use git mv to preserve history. External path
references rewritten across .github/workflows/pytorchsim_test.yml,
README.md, CLAUDE.md, .github/ISSUE_TEMPLATE/bug_report.md, and the
scripts/{sparsity,stonne}_experiment/ shell scripts.

Cross-test imports updated to drop the `tests.` prefix (because PR #234
puts `<repo>/tests` on sys.path[0] to avoid the ultralytics top-level
`tests` package collision):
- tests/system/test_hetro.py
- tests/system/test_scheduler.py
- tests/system/test_vectorops.py
- tests/ops/sparsity/test_sparsity.py

__init__.py added to each new subdir so e.g. `from ops.elementwise.test_add
import test_vectoradd` resolves.

Six files (test_resnet, test_vit, test_stonne, test_sparse_core,
test_gqa, test_diffusion) had their `sys.path.append` inside an
`if __name__ == "__main__":` block, which made the PR-A migration
script insert the import at the wrong indent level. Moved the
sys.path/import block to module top.

Sample-verified locally on tests/ops/elementwise/test_add.py and
tests/ops/fusion/test_matmul_vector.py (both pass) plus import-only
checks on the cross-import files.

Stacks on top of PR #234 (tests/_pytorchsim_utils.py extraction).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@YWHyuk YWHyuk force-pushed the refactor/tests-shared-helper branch from 5ca0455 to 74863f1 Compare May 26, 2026 07:12
YWHyuk added a commit that referenced this pull request May 26, 2026
tests/ was a flat mix of op-level files, single-file model tests, and
multi-file model directories with inconsistent capitalization. This PR
introduces a hierarchy:

  tests/
    _pytorchsim_utils.py
    ops/
      elementwise/  reduce/  gemm/  conv/  attention/
      view/         sort/    sparsity/  misc/  fusion/
    models/
      DeepSeek/  Diffusion/  Llama/  MLP/  MoE/
      MobileNet/  Mixtral8x7B/  Yolov5/
      test_mlp.py  test_resnet.py  test_single_perceptron.py
      test_transformer.py  test_vit.py
    system/
      test_eager.py  test_hetro.py  test_scheduler.py
      test_stonne.py  test_vectorops.py

Mixtral_8x7B renamed to Mixtral8x7B for consistency with the other
PascalCase model dirs. Existing single-file model dirs are kept as
dirs (they may grow companion files like the Mixtral model.py).

All file moves use git mv to preserve history. External path
references rewritten across .github/workflows/pytorchsim_test.yml,
README.md, CLAUDE.md, .github/ISSUE_TEMPLATE/bug_report.md, and the
scripts/{sparsity,stonne}_experiment/ shell scripts.

Cross-test imports updated to drop the `tests.` prefix (because PR #234
puts `<repo>/tests` on sys.path[0] to avoid the ultralytics top-level
`tests` package collision):
- tests/system/test_hetro.py
- tests/system/test_scheduler.py
- tests/system/test_vectorops.py
- tests/ops/sparsity/test_sparsity.py

__init__.py added to each new subdir so e.g. `from ops.elementwise.test_add
import test_vectoradd` resolves.

Six files (test_resnet, test_vit, test_stonne, test_sparse_core,
test_gqa, test_diffusion) had their `sys.path.append` inside an
`if __name__ == "__main__":` block, which made the PR-A migration
script insert the import at the wrong indent level. Moved the
sys.path/import block to module top.

Sample-verified locally on tests/ops/elementwise/test_add.py and
tests/ops/fusion/test_matmul_vector.py (both pass) plus import-only
checks on the cross-import files.

Stacks on top of PR #234 (tests/_pytorchsim_utils.py extraction).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
YWHyuk and others added 2 commits May 26, 2026 19:17
…s.py

Replaces 48 near-duplicate ~12-line `test_result(name, out, ...)` defs
(two signature variants, three slightly divergent bodies — one had a
'pass message only' bug) with a single canonical helper that prints
framed pass/fail messages and exits 1 on mismatch. Positional-argument
compatible, so caller sites are unchanged.

Each migrated file replaces its local def with:

  import os, sys
  sys.path.insert(0, os.path.join(
      os.environ.get("TORCHSIM_DIR", default="/workspace/PyTorchSim"), "tests"))
  from _pytorchsim_utils import test_result

The module name is deliberately unique rather than `tests._utils`:
ultralytics ships its own top-level `tests` package in site-packages,
which shadows any generic `tests` import. `insert(0, .../tests)` puts
the repo's tests dir ahead of site-packages so the helper resolves
regardless of installed packages.

Net diff: 50 files changed, 220 insertions(+), 729 deletions(-).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
tests/ was a flat mix of op-level files, single-file model tests, and
inconsistently-cased model directories. Adds a hierarchy:

  tests/
    _pytorchsim_utils.py
    ops/
      elementwise/  reduce/  gemm/  conv/  attention/
      view/         sort/    sparsity/  misc/  fusion/
    models/
      DeepSeek/  Diffusion/  Llama/  MLP/  MoE/
      MobileNet/  Mixtral8x7B/  Yolov5/
      test_mlp.py  test_resnet.py  test_single_perceptron.py
      test_transformer.py  test_vit.py
    system/
      test_eager.py  test_hetro.py  test_scheduler.py
      test_stonne.py  test_vectorops.py

Mixtral_8x7B → Mixtral8x7B for consistency with the other PascalCase
model dirs. Existing single-file model dirs are kept as dirs (they
may grow companion files like the Mixtral model.py).

All file moves use `git mv` to preserve history. External path
references rewritten across .github/workflows/pytorchsim_test.yml,
README.md, CLAUDE.md, .github/ISSUE_TEMPLATE/bug_report.md, and
scripts/{sparsity,stonne}_experiment/.

Cross-test imports drop the `tests.` prefix (the prior commit puts
`<repo>/tests` on sys.path[0]). `__init__.py` added to each new subdir
so e.g. `from ops.elementwise.test_add import test_vectoradd` resolves.

Sample-verified locally on tests/ops/elementwise/test_add.py and
tests/ops/fusion/test_matmul_vector.py.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@YWHyuk YWHyuk force-pushed the refactor/tests-shared-helper branch from 3994d7a to dff1f10 Compare May 26, 2026 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant