File: dialect.py

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-20
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,496,436 kB
  • sloc: cpp: 5,593,990; ansic: 986,873; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,547; xml: 953; cs: 573; fortran: 567
file content (90 lines) | stat: -rw-r--r-- 3,061 bytes parent folder | download | duplicates (6)
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
# RUN: %PYTHON %s | FileCheck %s

from mlir.ir import *
from mlir.dialects import sparse_tensor as st

def run(f):
  print("\nTEST:", f.__name__)
  f()
  return f


# CHECK-LABEL: TEST: testEncodingAttr1D
@run
def testEncodingAttr1D():
  with Context() as ctx:
    parsed = Attribute.parse(
      '#sparse_tensor.encoding<{ dimLevelType = [ "compressed" ], '
      'pointerBitWidth = 16, indexBitWidth = 32 }>')
    print(parsed)

    casted = st.EncodingAttr(parsed)
    # CHECK: equal: True
    print(f"equal: {casted == parsed}")

    # CHECK: dim_level_types: [<DimLevelType.compressed: 1>]
    print(f"dim_level_types: {casted.dim_level_types}")
    # CHECK: dim_ordering: None
    # Note that for 1D, the ordering is None, which exercises several special
    # cases.
    print(f"dim_ordering: {casted.dim_ordering}")
    # CHECK: pointer_bit_width: 16
    print(f"pointer_bit_width: {casted.pointer_bit_width}")
    # CHECK: index_bit_width: 32
    print(f"index_bit_width: {casted.index_bit_width}")

    created = st.EncodingAttr.get(casted.dim_level_types, None, 16, 32)
    print(created)
    # CHECK: created_equal: True
    print(f"created_equal: {created == casted}")

    # Verify that the factory creates an instance of the proper type.
    # CHECK: is_proper_instance: True
    print(f"is_proper_instance: {isinstance(created, st.EncodingAttr)}")
    # CHECK: created_pointer_bit_width: 16
    print(f"created_pointer_bit_width: {created.pointer_bit_width}")


# CHECK-LABEL: TEST: testEncodingAttr2D
@run
def testEncodingAttr2D():
  with Context() as ctx:
    parsed = Attribute.parse(
      '#sparse_tensor.encoding<{ dimLevelType = [ "dense", "compressed" ], '
      'dimOrdering = affine_map<(d0, d1) -> (d0, d1)>, '
      'pointerBitWidth = 16, indexBitWidth = 32 }>')
    print(parsed)

    casted = st.EncodingAttr(parsed)
    # CHECK: equal: True
    print(f"equal: {casted == parsed}")

    # CHECK: dim_level_types: [<DimLevelType.dense: 0>, <DimLevelType.compressed: 1>]
    print(f"dim_level_types: {casted.dim_level_types}")
    # CHECK: dim_ordering: (d0, d1) -> (d0, d1)
    print(f"dim_ordering: {casted.dim_ordering}")
    # CHECK: pointer_bit_width: 16
    print(f"pointer_bit_width: {casted.pointer_bit_width}")
    # CHECK: index_bit_width: 32
    print(f"index_bit_width: {casted.index_bit_width}")

    created = st.EncodingAttr.get(casted.dim_level_types, casted.dim_ordering,
        16, 32)
    print(created)
    # CHECK: created_equal: True
    print(f"created_equal: {created == casted}")


# CHECK-LABEL: TEST: testEncodingAttrOnTensor
@run
def testEncodingAttrOnTensor():
  with Context() as ctx, Location.unknown():
    encoding = st.EncodingAttr(Attribute.parse(
      '#sparse_tensor.encoding<{ dimLevelType = [ "compressed" ], '
      'pointerBitWidth = 16, indexBitWidth = 32 }>'))
    tt = RankedTensorType.get((1024,), F32Type.get(), encoding=encoding)
    # CHECK: tensor<1024xf32, #sparse_tensor
    print(tt)
    # CHECK: #sparse_tensor.encoding
    print(tt.encoding)
    assert tt.encoding == encoding