FrozenGene commented on code in PR #18247: URL: https://github.com/apache/tvm/pull/18247#discussion_r2343576498
########## python/tvm/relax/backend/contrib/example_npu/README.md: ########## @@ -0,0 +1,220 @@ +<!--- Licensed to the Apache Software Foundation (ASF) under one --> +<!--- or more contributor license agreements. See the NOTICE file --> +<!--- distributed with this work for additional information --> +<!--- regarding copyright ownership. The ASF licenses this file --> +<!--- to you under the Apache License, Version 2.0 (the --> +<!--- "License"); you may not use this file except in compliance --> +<!--- with the License. You may obtain a copy of the License at --> + +<!--- http://www.apache.org/licenses/LICENSE-2.0 --> + +<!--- Unless required by applicable law or agreed to in writing, --> +<!--- software distributed under the License is distributed on an --> +<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY --> +<!--- KIND, either express or implied. See the License for the --> +<!--- specific language governing permissions and limitations --> +<!--- under the License. --> + +# Example NPU Backend + +A hands-on example showing how to build a Neural Processing Unit (NPU) backend for TVM's Relax framework using Bring Your Own Codegen (BYOC). + +## What This Is + +This is an educational template that demonstrates real NPU concepts without requiring actual NPU hardware. It shows developers how to: + +- **Pattern-based partitioning**: Identify and group operations that should run on specialized hardware +- **Memory hierarchy management**: Handle different memory tiers (L0/L1/L2/L3) common in NPUs +- **Automatic tiling**: Break large tensors into smaller chunks that fit in on-chip memory +- **Quantization support**: Handle different data precisions efficiently +- **BYOC integration**: Connect custom backends to TVM's compilation pipeline +- **Operator availability checking**: Gracefully handle operators that may not be available in all TVM builds + +## Quick Start + +```python +import tvm +from tvm import relax +from tvm.relax.backend.pattern_registry import get_patterns_with_prefix +from tvm.relax.transform import FuseOpsByPattern, RunCodegen + +# Import to register patterns +import tvm.relax.backend.contrib.example_npu + +# Get available patterns +patterns = get_patterns_with_prefix("example_npu") +print(f"Available patterns: {[p.name for p in patterns]}") + +# Your model gets automatically partitioned +# Operations matching patterns get fused into "Composite" functions +# Those get lowered to the example NPU backend +``` + +The snippet above shows how to discover registered patterns. A minimal runnable example that demonstrates the BYOC flow (partition -> merge -> codegen) using the example test module looks like this: + +```python +# This imports the example module used in the tests. Importing the test +# module path directly works when running from the repo root (pytest does +# this automatically). +from tests.python.contrib.test_example_npu import MatmulReLU Review Comment: I think it is worthy moving this to our doc: https://github.com/apache/tvm/tree/main/docs/how_to/tutorials not just one README.md -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
