Files
apache--tvm/tests/python/relax/test_fast_math_transform.py
Tianqi Chen 4d28424268 [REFACTOR][IR] Phase out diagnostic.h for visit-context-aware pass errors (#19722)
Replace TVM's `Diagnostic` / `DiagnosticContext` machinery with the
tvm-ffi
`visit_error_context` mechanism. Validators throw an `ffi::Error` seeded
with the
offending node; leaf pass executors (`ModulePass` / relax `Function` /
`DataflowBlock`) catch and rethrow `EnrichPassErrorWithContext`, which
appends the
failing pass name and a TVMScript-rendered, underlined source location.

`relax.analysis.well_formed` now throws on the first violation; a new
`check_well_formed` returns a bool, and all C++/Python/test callers are
routed
accordingly. `include/tvm/ir/diagnostic.h` and `src/ir/diagnostic.cc`
are deleted.
The enrichment renders with `num_context_lines=10` so a small function
shows in
full with no skipped-lines marker, while a large module stays bounded.

The TVMScript parser diagnostics
(`python/tvm/script/parser/core/diagnostics.py`)
stay self-contained pure-Python with no `DiagnosticContext` dependency,
and
restore multi-line source rendering: a diagnostic whose offending AST
node spans
multiple source lines now renders every spanned line with its gutter
line number
and an underline covering the span. `tvm.error.DiagnosticError` (used by
the
TVMScript parser) is retained.

A rendered end-to-end enriched-error example is posted as a comment
below.
2026-06-10 20:13:33 -04:00

63 lines
2.2 KiB
Python

# 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.
# ruff: noqa: F401
"""Tests to validate relax fast math tranform pass."""
import pytest
import tvm.testing
from tvm import relax, topi
from tvm.ir.base import assert_structural_equal
from tvm.relax.transform import FastMathTransform
from tvm.script import ir as I
from tvm.script import relax as R
def _run_pass_compare_output(Before, Expected):
fast_mod = FastMathTransform()(Before)
if not relax.analysis.check_well_formed(fast_mod):
print("IRModule is not well-formed")
assert_structural_equal(Expected, fast_mod)
def test_optimize_transform_layout_pass_one_arg():
@I.ir_module
class Before:
@R.function
def main(x: R.Tensor((16,), dtype="float32")) -> R.Tensor((16,), dtype="float32"):
lv1: R.Tensor((16,), dtype="float32") = R.nn.softmax(x)
lv2: R.Tensor((16,), dtype="float32") = R.exp(lv1)
lv3: R.Tensor((16,), dtype="float32") = R.erf(lv2)
lv4: R.Tensor((16,), dtype="float32") = R.tanh(lv3)
return lv4
bb = relax.BlockBuilder()
x = relax.Var("x", R.Tensor((16,), "float32"))
with bb.function("main", [x]):
lv1 = bb.emit_te(topi.nn.fast_softmax, x)
lv2 = bb.emit_te(topi.fast_exp, lv1)
lv3 = bb.emit_te(topi.fast_erf, lv2)
lv4 = bb.emit_te(topi.fast_tanh, lv3)
bb.emit_func_output(lv4)
Expected = bb.get()
_run_pass_compare_output(Before, Expected)
if __name__ == "__main__":
tvm.testing.main()