Test end-to-end execution with the example NPU runtime
()
| 223 | |
| 224 | @example_npu_enabled |
| 225 | def test_example_npu_runtime_execution(): |
| 226 | """Test end-to-end execution with the example NPU runtime""" |
| 227 | import tvm.relax.backend.contrib.example_npu |
| 228 | |
| 229 | # Create simple test inputs |
| 230 | np.random.seed(42) |
| 231 | x_np = np.random.randn(2, 4).astype("float32") |
| 232 | w_np = np.random.randn(4, 8).astype("float32") |
| 233 | |
| 234 | # Expected output (computed with NumPy) |
| 235 | expected = np.maximum(0, np.matmul(x_np, w_np)) |
| 236 | |
| 237 | # Build and run with example NPU backend |
| 238 | mod = MatmulReLU |
| 239 | patterns = get_patterns_with_prefix("example_npu") |
| 240 | |
| 241 | # Apply transformations |
| 242 | mod = FuseOpsByPattern(patterns, bind_constants=False, annotate_codegen=True)(mod) |
| 243 | mod = MergeCompositeFunctions()(mod) |
| 244 | mod = RunCodegen()(mod) |
| 245 | |
| 246 | # Build the module |
| 247 | target = tvm.target.Target("llvm") |
| 248 | with tvm.transform.PassContext(opt_level=3): |
| 249 | built = relax.build(mod, target) |
| 250 | |
| 251 | # Create VM and run |
| 252 | vm = relax.VirtualMachine(built, tvm.cpu()) |
| 253 | |
| 254 | x_tvm = tvm.runtime.tensor(x_np, tvm.cpu()) |
| 255 | w_tvm = tvm.runtime.tensor(w_np, tvm.cpu()) |
| 256 | |
| 257 | result = vm["main"](x_tvm, w_tvm) |
| 258 | |
| 259 | # Verify the result shape is correct (the runtime is a stub and does not compute numerically) |
| 260 | assert result.numpy().shape == expected.shape |
| 261 | |
| 262 | |
| 263 | if __name__ == "__main__": |
no test coverage detected
searching dependent graphs…