block_scale_quantize.py 2.01 KB
Newer Older
yanjl1's avatar
Initial  
yanjl1 committed
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
import hipdnn
import torch


def build_block_scale_quantize_graph(hipdnn_handle, torch_tensor_x, block_size, hipdnn_data_type):
    # Create graph
    graph = hipdnn.pygraph(
        handle=hipdnn_handle,
        io_data_type=hipdnn_data_type,
        intermediate_data_type=hipdnn.data_type.FLOAT,
        compute_data_type=hipdnn.data_type.FLOAT,
        name="block_scale_quantize",
    )

    # Create hipdnn tensors
    hipdnn_tensor_x = graph.tensor_like(torch_tensor_x)

    # Create block scale op
    hipdnn_tensor_y, hipdnn_tensor_scale = graph.block_scale_quantize(
        input=hipdnn_tensor_x,
        block_size=block_size,
        axis=2,
        transpose=False,
        name="block_scale_quantize",
    )
    hipdnn_tensor_y.set_output(True)
    hipdnn_tensor_scale.set_output(True)
    graph.build(hipdnn_handle)

    return (graph, hipdnn_tensor_x, hipdnn_tensor_y, hipdnn_tensor_scale)


if __name__ == "__main__":
    batch, channels, height, width, block_size = 1, 32, 32, 32, 32

    hipdnn_data_type = hipdnn.data_type.FLOAT
    torch_data_type = torch.float32

    torch_tensor_x = torch.rand(
        batch, channels, height, width, dtype=torch_data_type, device="cuda"
    )

    hipdnn_handle = hipdnn.create_handle()

    # graph, hipdnn_tensor_x, hipdnn_tensor_y, hipdnn_tensor_scale = build_block_scale_quantize_graph(
    #     hipdnn_handle, torch_tensor_x, block_size, hipdnn_data_type)

    # torch_tensor_y = torch.empty(hipdnn_tensor_y.get_dim(), dtype=torch_data_type, device="cuda")
    # torch_tensor_scale = torch.empty(hipdnn_tensor_scale.get_dim(), dtype=torch_data_type, device="cuda")
    # variant_pack = {
    #     hipdnn_tensor_x: torch_tensor_x.data_ptr(),
    #     hipdnn_tensor_y: torch_tensor_y.data_ptr(),
    #     hipdnn_tensor_scale: torch_tensor_scale.data_ptr()
    # }
    # workspace = torch.empty(graph.get_workspace_size(), dtype=torch.uint8, device="cuda")

    # graph.exec(variant_pack=variant_pack, workspace=workspace.data_ptr())
    # print("Block scale quantize graph execution complete.")