|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +import pytest |
| 16 | +import re |
| 17 | +from dataclasses import dataclass |
| 18 | +from typing import List, Optional |
| 19 | + |
| 20 | +from tripy.flat_ir.flat_ir import FlatIR |
| 21 | +from tripy.flat_ir.ops.base import FlatIRFunction, BaseFlatIROp |
| 22 | +from tripy.flat_ir.ops import ConstantOp |
| 23 | +from tripy.flat_ir.tensor import FlatIRTensor |
| 24 | +from tripy.common.device import device |
| 25 | +from tripy.common.datatype import float32, int32 |
| 26 | + |
| 27 | + |
| 28 | +@dataclass(repr=False, eq=False) |
| 29 | +class MockOp(BaseFlatIROp): |
| 30 | + def __init__(self, inputs, outputs): |
| 31 | + self.inputs = inputs |
| 32 | + self.outputs = outputs |
| 33 | + self.trace_input_names = [] |
| 34 | + self.trace_output_names = [] |
| 35 | + for output in outputs: |
| 36 | + output.producer = self |
| 37 | + |
| 38 | + def __eq__(self, other): |
| 39 | + return True |
| 40 | + |
| 41 | + def to_mlir(self, operands): |
| 42 | + assert "Not implemented" |
| 43 | + |
| 44 | + |
| 45 | +def test_is_structurally_equivalent(): |
| 46 | + """Test the structural equivalence of two FlatIR functions.""" |
| 47 | + flat_ir = FlatIR() |
| 48 | + |
| 49 | + def create_tensor(reason_details: str, name: Optional[str] = None) -> FlatIRTensor: |
| 50 | + """Create and register a FlatIRTensor.""" |
| 51 | + t = FlatIRTensor.build( |
| 52 | + shape=[3], |
| 53 | + rank=1, |
| 54 | + dtype=float32, |
| 55 | + device=device("gpu"), |
| 56 | + reason_details=reason_details, |
| 57 | + ) |
| 58 | + if name: |
| 59 | + t.name = name |
| 60 | + flat_ir.register_tensor(t) |
| 61 | + return t |
| 62 | + |
| 63 | + def create_function( |
| 64 | + name: str, |
| 65 | + input_tensor: FlatIRTensor, |
| 66 | + output_tensors: List[FlatIRTensor], |
| 67 | + ) -> FlatIRFunction: |
| 68 | + """Create a FlatIRFunction with associated operations.""" |
| 69 | + callee_input = input_tensor.clone(reason_details=f"{name} input cloned from {input_tensor}") |
| 70 | + callee_outputs = [out.clone(reason_details=f"{name} output cloned from {out}") for out in output_tensors] |
| 71 | + |
| 72 | + flat_ir.register_tensor(callee_input) |
| 73 | + setattr(callee_input, "caller_tensor", input_tensor) |
| 74 | + |
| 75 | + for callee_out, original_out in zip(callee_outputs, output_tensors): |
| 76 | + flat_ir.register_tensor(callee_out) |
| 77 | + setattr(callee_out, "caller_tensor", original_out) |
| 78 | + |
| 79 | + func = FlatIRFunction(name, [callee_input], callee_outputs) |
| 80 | + mock_op = MockOp([callee_input], [callee_outputs[0]]) |
| 81 | + const_op = ConstantOp.build([], [callee_outputs[1]], data=[3, 4, 5]) |
| 82 | + callee_outputs[1].producer = const_op |
| 83 | + |
| 84 | + func.ops.extend([mock_op, const_op]) |
| 85 | + for out in output_tensors: |
| 86 | + out.producer = func |
| 87 | + |
| 88 | + return func |
| 89 | + |
| 90 | + # Create main tensors |
| 91 | + input_tensor = create_tensor("Function 1 input", "main_input_tensor") |
| 92 | + intermediates = [create_tensor(f"Function 1 output {i}", f"intermediate_tensor_{i}") for i in range(2)] |
| 93 | + outputs = [create_tensor(f"Function 2 output {i}", f"main_output_tensor_{i}") for i in range(2)] |
| 94 | + |
| 95 | + # Create two structurally equivalent functions |
| 96 | + func_1 = create_function("Func1", input_tensor, intermediates) |
| 97 | + func_2 = create_function("Func2", intermediates[0], outputs) |
| 98 | + |
| 99 | + # Assert structural equivalence |
| 100 | + assert func_1.is_structurally_equivalent(func_2) |
| 101 | + |
| 102 | + # Set up FlatIR inputs and outputs |
| 103 | + flat_ir.inputs = [input_tensor] |
| 104 | + flat_ir.outputs = outputs |
| 105 | + |
| 106 | + # Integrate subgraphs |
| 107 | + for in_tensor, out_tensors in [(input_tensor, intermediates), (intermediates[0], outputs)]: |
| 108 | + flat_ir.integrate_subgraph([in_tensor], out_tensors) |
| 109 | + |
| 110 | + flat_ir_str = str(flat_ir) |
| 111 | + |
| 112 | + # Check Func1 structure |
| 113 | + func_pattern = re.compile(r"function\s+Func1\s*\(\s*\w+:.*?\)\s*->\s*\(.*?\)\s*{.*?return.*?}", re.DOTALL) |
| 114 | + assert func_pattern.search(flat_ir_str), "Function Func1 structure is incorrect" |
| 115 | + |
| 116 | + # Check Main Function structure |
| 117 | + main_pattern = re.compile( |
| 118 | + r"Main Function:.*?inputs:.*?=\s*function Func1.*?=\s*function Func1.*?outputs:", re.DOTALL |
| 119 | + ) |
| 120 | + assert main_pattern.search(flat_ir_str), "Main Function structure is incorrect" |
| 121 | + |
| 122 | + print("All assertions passed. Function structures are correct.") |
0 commit comments