Repository navigation
Expand file tree
/
Copy pathtest_bytecode.py
More file actions
143 lines (124 loc) · 6.17 KB
/
Copy pathtest_bytecode.py
File metadata and controls
143 lines (124 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# SPDX-FileCopyrightText: Copyright (c) <2025> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
from pathlib import Path
from tempfile import NamedTemporaryFile
import torch.cuda
import cuda.tile._bytecode as bc
from cuda.tile._bytecode.attribute import encode_ap_int
from cuda.tile._compiler_options import CompilerOptions
from cuda.tile._compile import compile_cubin, get_sm_arch
from cuda.tile import _cext
from cuda.tile._annotated_function import LeafAnnotationNode
import cuda.tile as ct
def test_write_simple_module():
buf = bytearray()
with bc.write_bytecode(1, buf, version=bc.BytecodeVersion.V_13_1) as writer:
tt = writer.type_table
with writer.function(name="foo",
parameter_types=[tt.tile(tt.pointer(
tt.F32,
bc.PtrAttr.Missing),
()),
tt.tile(tt.I32, ()),
tt.tile(tt.I32, ()),
tt.tile(tt.pointer(
tt.F32,
bc.PtrAttr.Missing),
()),
tt.tile(tt.I32, ()),
tt.tile(tt.I32, ()),
tt.tile(tt.pointer(
tt.F32,
bc.PtrAttr.Missing),
()),
tt.tile(tt.I32, ()),
tt.tile(tt.I32, ())],
result_types=[],
entry_point=True,
hints=None,
debug_attr=bc.MISSING_DEBUG_ATTR_ID) as (builder, parameters):
x, x_len, x_stride, y, y_len, y_stride, result, result_len, result_stride = parameters
view_ty = tt.tensor_view(tt.F32, (bc.DYNAMIC_SHAPE,), (1,), bc.PtrAttr.Missing)
partition_ty = tt.partition_view((1,), view_ty, [0], bc.PaddingValue.Zero)
partitions = []
for ptr, size in [(x, x_len), (y, y_len), (result, result_len)]:
view = bc.encode_MakeTensorViewOp(builder,
result_type=view_ty,
base=ptr,
dynamicShape=[size],
dynamicStrides=[])
partitions.append(bc.encode_MakePartitionViewOp(builder, partition_ty, view))
x_pv, y_pv, result_pv = partitions
index_ty = tt.tile(tt.I32, ())
zero_i32 = bc.encode_ConstantOp(builder, index_ty, (0).to_bytes(4, "little"))
tile_ty = tt.tile(tt.F32, (1,))
tiles = []
for pv in x_pv, y_pv:
tile, _ = bc.encode_LoadViewTkoOp(
builder,
tile_type=tile_ty,
result_token_type=tt.Token,
view=pv,
index=[zero_i32],
memory_ordering_semantics=bc.MemoryOrderingSemantics.WEAK,
memory_scope=None,
optimization_hints=None,
token=None,
inbounds=(False,))
tiles.append(tile)
x_tile, y_tile = tiles
result_tile = bc.encode_AddFOp(
builder,
lhs=x_tile,
rhs=y_tile,
result_type=tile_ty,
rounding_mode=bc.RoundingMode.NEAREST_EVEN,
flush_to_zero=False)
bc.encode_StoreViewTkoOp(
builder,
result_token_type=tt.Token,
tile=result_tile,
view=result_pv,
index=[zero_i32],
token=None,
memory_ordering_semantics=bc.MemoryOrderingSemantics.WEAK,
memory_scope=None,
optimization_hints=None,
inbounds=(False,))
bc.encode_ReturnOp(builder, [])
with NamedTemporaryFile() as f:
f.write(buf)
f.flush()
cubin_path = compile_cubin(f.name, CompilerOptions(), get_sm_arch(), None)
cubin = Path(cubin_path).read_bytes()
kernel = _HackKernel(cubin, "foo")
x_tensor = torch.tensor([3.0], dtype=torch.float32, device="cuda:0")
y_tensor = torch.tensor([5.0], dtype=torch.float32, device="cuda:0")
result = torch.tensor([0.0], dtype=torch.float32, device="cuda:0")
ct.launch(torch.cuda.current_stream(), (1,), kernel, (x_tensor, y_tensor, result))
assert result.cpu().item() == 8.0
class _HackKernel(_cext.TileDispatcher):
def __init__(self, cubin: bytes, func_name: str):
self._cubin = cubin
self._func_name = func_name
leaf = LeafAnnotationNode(constant=False)
super().__init__((leaf, leaf, leaf))
def _compile(self, signature, ctx, compute_capability):
assert len(signature.parameters) == 3
for x in signature.parameters:
assert x.ndim == 1
assert x.dtype == ct.float32
return self._cubin, self._func_name, None, ()
def test_encode_f64_max_identity():
bits = _cext.encode_float(-float("inf"), ct.float64)
assert bits == 0xFFF0000000000000
buf = bytearray()
encode_ap_int(bits, 64, buf)
# TileIR encodes this raw word as a signed int64, then applies zigzag:
# 0xFFF0000000000000 - 2**64 = -2**52
# zigzag(-2**52) = 2**53 - 1 (53 one bits).
# Varints store seven payload bits per byte, least significant first.
# Seven full groups use 0x7f | 0x80 (continuation); the final four bits
# use 0x0f without a continuation bit.
assert buf == bytes.fromhex("ff ff ff ff ff ff ff 0f")