Running Tests¶
torch-webgpu has both Python and C++ tests.
Python Tests¶
Run All Tests¶
Run Specific Test File¶
Run with Verbose Output¶
Run with Print Statements¶
Run Qwen Compilation Tests¶
C++ Tests¶
Build Tests¶
Run Tests¶
Individual Test Files¶
C++ tests are in ctests/:
test_add.cpp- Addition operationstest_mm.cpp- Matrix multiplicationtest_cos.cpp- Cosine function- etc.
Writing Tests¶
Python Test Example¶
# tests/ops/test_my_op.py
import pytest
import torch
import torch_webgpu
from torch_webgpu.compiler.webgpu_compiler import webgpu_backend
def test_my_op_basic():
"""Test basic functionality."""
x = torch.randn(3, 4)
expected = torch.my_op(x)
@torch.compile(backend=webgpu_backend)
def fn(x):
return torch.my_op(x)
result = fn(x)
torch.testing.assert_close(result, expected, rtol=1e-3, atol=1e-3)
def test_my_op_shapes():
"""Test with different shapes."""
for shape in [(1,), (10,), (3, 4), (2, 3, 4)]:
x = torch.randn(shape)
expected = torch.my_op(x)
@torch.compile(backend=webgpu_backend)
def fn(x):
return torch.my_op(x)
result = fn(x)
torch.testing.assert_close(result, expected, rtol=1e-3, atol=1e-3)
C++ Test Example¶
// ctests/test_my_op.cpp
#include <torch/torch.h>
#include <cassert>
#include <iostream>
int main() {
// Create input
auto x = torch::randn({3, 4});
auto x_webgpu = x.to("webgpu");
// Run operation
auto result = torch::my_op(x_webgpu);
// Move back and compare
auto result_cpu = result.to("cpu");
auto expected = torch::my_op(x);
auto diff = (result_cpu - expected).abs().max().item<float>();
assert(diff < 1e-3);
std::cout << "test_my_op passed!" << std::endl;
return 0;
}
Test Coverage¶
Key test areas:
| Area | Test Files |
|---|---|
| Basic ops | tests/ops/test_*.py |
| Compilation | tests/compile/test_ir.py |
| Qwen model | tests/test_qwen_compile.py |
| C++ ops | ctests/test_*.cpp |
Debugging Test Failures¶
Enable Debug Output¶
In python/torch_webgpu/compiler/lowering.py:
This prints shape information for each operation.