| 1 |
################################################################################ |
| 2 |
# |
| 3 |
# Copyright (C) 2025 Advanced Micro Devices, Inc. All rights reserved. |
| 4 |
# |
| 5 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
# of this software and associated documentation files (the "Software"), to deal |
| 7 |
# in the Software without restriction, including without limitation the rights |
| 8 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cop- |
| 9 |
# ies of the Software, and to permit persons to whom the Software is furnished |
| 10 |
# to do so, subject to the following conditions: |
| 11 |
# |
| 12 |
# The above copyright notice and this permission notice shall be included in all |
| 13 |
# copies or substantial portions of the Software. |
| 14 |
# |
| 15 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM- |
| 16 |
# PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 17 |
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 18 |
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 19 |
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNE- |
| 20 |
# CTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
################################################################################ |
| 22 |
|
| 23 |
import rocisa |
| 24 |
from rocisa.container import sgpr, vgpr |
| 25 |
from copy import deepcopy |
| 26 |
import math |
| 27 |
|
| 28 |
def test_instruction_common(): |
| 29 |
from rocisa.instruction import SMovB32 |
| 30 |
|
| 31 |
inst = SMovB32(dst=sgpr(1), src=1.6) |
| 32 |
assert str(inst) == "s_mov_b32 s1, 1.6000000000000001\n" |
| 33 |
assert str(inst.dst) == "s1" |
| 34 |
# Due to conversion between C++ and Python |
| 35 |
assert math.isclose(inst.srcs[0], 1.6, abs_tol=0.000001) |
| 36 |
# You cannot use srcs[0] = 1.3 because it may break the C++ memory layout |
| 37 |
# so nanobind disabled it |
| 38 |
inst.srcs = [1.3] |
| 39 |
assert math.isclose(inst.srcs[0], 1.3, abs_tol=0.000001) |
| 40 |
inst.setSrc(0, 1.4) |
| 41 |
assert math.isclose(inst.srcs[0], 1.4, abs_tol=0.000001) |
| 42 |
|
| 43 |
inst2 = deepcopy(inst) |
| 44 |
inst.setSrc(0, 2.0) |
| 45 |
assert math.isclose(inst2.srcs[0], 1.4, abs_tol=0.000001) |
| 46 |
|
| 47 |
from rocisa.instruction import InstructionInputVector |
| 48 |
iiv = InstructionInputVector() |
| 49 |
iiv.append(1.0) |
| 50 |
iiv.append("hello") |
| 51 |
iiv.append(sgpr(1)) |
| 52 |
print(iiv[2]) |
| 53 |
|
| 54 |
from rocisa.code import Module |
| 55 |
from rocisa.container import vgpr |
| 56 |
from rocisa.instruction import BufferLoadB64 |
| 57 |
module = Module("Test") |
| 58 |
module.add(BufferLoadB64(vgpr(1), vgpr(2), vgpr(3), 3)) |
| 59 |
assert rocisa.countGlobalRead(module) == 1 |
| 60 |
|
| 61 |
def test_instruction_cvt(): |
| 62 |
from rocisa.instruction import VCvtF16toF32, VCvtF32toF16, VCvtF32toU32, VCvtU32toF32, \ |
| 63 |
VCvtI32toF32, VCvtF32toI32, VCvtFP8toF32, VCvtBF8toF32, VCvtPkFP8toF32, VCvtPkBF8toF32, \ |
| 64 |
VCvtPkF32toFP8, VCvtPkF32toBF8, VCvtSRF32toFP8, VCvtSRF32toBF8 |
| 65 |
|
| 66 |
# Test VCvtF16toF32 |
| 67 |
inst = VCvtF16toF32(dst=vgpr(1), src=vgpr(2), comment="test comment") |
| 68 |
assert str(inst) == "v_cvt_f32_f16 v1, v2 // test comment\n" |
| 69 |
assert str(inst.dst) == "v1" |
| 70 |
assert str(inst.srcs[0]) == "v2" |
| 71 |
assert inst.comment == "test comment" |
| 72 |
|
| 73 |
# Test VCvtF32toF16 |
| 74 |
inst = VCvtF32toF16(dst=vgpr(1), src=vgpr(2), comment="test comment") |
| 75 |
assert str(inst) == "v_cvt_f16_f32 v1, v2 // test comment\n" |
| 76 |
assert str(inst.dst) == "v1" |
| 77 |
assert str(inst.srcs[0]) == "v2" |
| 78 |
assert inst.comment == "test comment" |
| 79 |
|
| 80 |
# Test VCvtF32toU32 |
| 81 |
inst = VCvtF32toU32(dst=vgpr(1), src=vgpr(2), comment="test comment") |
| 82 |
assert str(inst) == "v_cvt_u32_f32 v1, v2 // test comment\n" |
| 83 |
assert str(inst.dst) == "v1" |
| 84 |
assert str(inst.srcs[0]) == "v2" |
| 85 |
assert inst.comment == "test comment" |
| 86 |
|
| 87 |
# Test VCvtU32toF32 |
| 88 |
inst = VCvtU32toF32(dst=vgpr(1), src=vgpr(2), comment="test comment") |
| 89 |
assert str(inst) == "v_cvt_f32_u32 v1, v2 // test comment\n" |
| 90 |
assert str(inst.dst) == "v1" |
| 91 |
assert str(inst.srcs[0]) == "v2" |
| 92 |
assert inst.comment == "test comment" |
| 93 |
|
| 94 |
# Test VCvtI32toF32 |
| 95 |
inst = VCvtI32toF32(dst=vgpr(1), src=vgpr(2), comment="test comment") |
| 96 |
assert str(inst) == "v_cvt_f32_i32 v1, v2 // test comment\n" |
| 97 |
assert str(inst.dst) == "v1" |
| 98 |
assert str(inst.srcs[0]) == "v2" |
| 99 |
assert inst.comment == "test comment" |
| 100 |
|
| 101 |
# Test VCvtF32toI32 |
| 102 |
inst = VCvtF32toI32(dst=vgpr(1), src=vgpr(2), comment="test comment") |
| 103 |
assert str(inst) == "v_cvt_i32_f32 v1, v2 // test comment\n" |
| 104 |
assert str(inst.dst) == "v1" |
| 105 |
assert str(inst.srcs[0]) == "v2" |
| 106 |
assert inst.comment == "test comment" |
| 107 |
|
| 108 |
# Test VCvtFP8toF32 |
| 109 |
inst = VCvtFP8toF32(dst=vgpr(1), src=vgpr(2), comment="test comment") |
| 110 |
assert str(inst) == "v_cvt_f32_fp8 v1, v2 // test comment\n" |
| 111 |
assert str(inst.dst) == "v1" |
| 112 |
assert str(inst.srcs[0]) == "v2" |
| 113 |
assert inst.comment == "test comment" |
| 114 |
|
| 115 |
# Test VCvtBF8toF32 |
| 116 |
inst = VCvtBF8toF32(dst=vgpr(1), src=vgpr(2), comment="test comment") |
| 117 |
assert str(inst) == "v_cvt_f32_bf8 v1, v2 // test comment\n" |
| 118 |
assert str(inst.dst) == "v1" |
| 119 |
assert str(inst.srcs[0]) == "v2" |
| 120 |
assert inst.comment == "test comment" |
| 121 |
|
| 122 |
# Test VCvtPkFP8toF32 |
| 123 |
inst = VCvtPkFP8toF32(dst=vgpr(1), src=vgpr(2), comment="test comment") |
| 124 |
assert str(inst) == "v_cvt_pk_f32_fp8 v1, v2 // test comment\n" |
| 125 |
assert str(inst.dst) == "v1" |
| 126 |
assert str(inst.srcs[0]) == "v2" |
| 127 |
assert inst.comment == "test comment" |
| 128 |
|
| 129 |
# Test VCvtPkBF8toF32 |
| 130 |
inst = VCvtPkBF8toF32(dst=vgpr(1), src=vgpr(2), comment="test comment") |
| 131 |
assert str(inst) == "v_cvt_pk_f32_bf8 v1, v2 // test comment\n" |
| 132 |
assert str(inst.dst) == "v1" |
| 133 |
assert str(inst.srcs[0]) == "v2" |
| 134 |
assert inst.comment == "test comment" |
| 135 |
|
| 136 |
# Test VCvtPkF32toFP8 |
| 137 |
inst = VCvtPkF32toFP8(dst=vgpr(1), src0=vgpr(2), src1=vgpr(3), comment="test comment") |
| 138 |
assert str(inst) == "v_cvt_pk_fp8_f32 v1, v2, v3 // test comment\n" |
| 139 |
assert str(inst.dst) == "v1" |
| 140 |
assert str(inst.srcs[0]) == "v2" |
| 141 |
assert str(inst.srcs[1]) == "v3" |
| 142 |
assert inst.comment == "test comment" |
| 143 |
|
| 144 |
# Test VCvtPkF32toBF8 |
| 145 |
inst = VCvtPkF32toBF8(dst=vgpr(1), src0=vgpr(2), src1=vgpr(3), comment="test comment") |
| 146 |
assert str(inst) == "v_cvt_pk_bf8_f32 v1, v2, v3 // test comment\n" |
| 147 |
assert str(inst.dst) == "v1" |
| 148 |
assert str(inst.srcs[0]) == "v2" |
| 149 |
assert str(inst.srcs[1]) == "v3" |
| 150 |
assert inst.comment == "test comment" |
| 151 |
|
| 152 |
# Test VCvtSRF32toFP8 |
| 153 |
inst = VCvtSRF32toFP8(dst=vgpr(1), src0=vgpr(2), src1=vgpr(3), comment="test comment") |
| 154 |
assert str(inst) == "v_cvt_sr_fp8_f32 v1, v2, v3 // test comment\n" |
| 155 |
assert str(inst.dst) == "v1" |
| 156 |
assert str(inst.srcs[0]) == "v2" |
| 157 |
assert str(inst.srcs[1]) == "v3" |
| 158 |
assert inst.comment == "test comment" |
| 159 |
|
| 160 |
# Test VCvtSRF32toBF8 |
| 161 |
inst = VCvtSRF32toBF8(dst=vgpr(1), src0=vgpr(2), src1=vgpr(3), comment="test comment") |
| 162 |
assert str(inst) == "v_cvt_sr_bf8_f32 v1, v2, v3 // test comment\n" |
| 163 |
assert str(inst.dst) == "v1" |
| 164 |
assert str(inst.srcs[0]) == "v2" |
| 165 |
assert str(inst.srcs[1]) == "v3" |
| 166 |
assert inst.comment == "test comment" |
| 167 |
|
| 168 |
test_instruction_common() |
| 169 |
test_instruction_cvt() |