Skip to content

Commit

Permalink
core: Fix parsing for hex denseAttrs (#3239)
Browse files Browse the repository at this point in the history
Fixes #2876.

The original parsing implementation for hex denseAttrs uses format
strings in `struct.unpack_from()`, but the format strings actually do
not include the count of the chunks contained in the bytes list.

This patch fixes this behavior by embedding the number of chunks in the
respective format string.
  • Loading branch information
vinayakdsci authored Oct 3, 2024
1 parent 335d200 commit 2ae41dd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 3 additions & 0 deletions tests/filecheck/parser-printer/builtin_attrs.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@

// CHECK: 3.060000e+02 : f32

"func.func"() ({}) {function_type = () -> (), value = dense<"0xEEA7CC3DF47612BE2BA4173E8B75E8BDE0B915BDA3191CBE8388E0BDC826DB3DFE78273E6B037E3DEF140D3EF0B5803D4026693CD6B6E1BCE08B4DBDC3A9E63D943B163EE64E46BD808C253EB8F4893D30270CBE36696C3D045E1DBED06A703DA33EBBBD66D646BD36507BBD764D8FBD7010FA3DB6E1B53D9B83C8BDD33FA73D58AD293EB0A6123EAB2627BA40B4CB3C20E9B6BD805AB2BDE047BDBC809A743DE01ADD3D9B77D5BDCEE7043E00B8C1BDCBA80A3DBB03DA3D787C993D163968BC208510BDABFDB1BD8C07213EA34614BEAB06B73A0091413B8013B3BD768F193E7B6515BE7306833D363183BC36BC8B3CA016B7BD3E05D33DE67C28BDCABB0EBEDA2A013EA67DF6BD007EB5BA782A04BEAB69F73D16DD703D3B93A43D1BE45B3DEBAEE8BD8891F1BDF8B18F3D20EC923CE67101BE8382A8BDAB9EE7BA0006CA3AA3F224BE1B56A5BDC06B8A3DC3E6BE3D562310BB964B713C2CC11FBE4BC68F3DAEACD7BDFB093A3D00070F3EC3E4C93D5BCF0D3D1B01E13D9B7D7F3D537CD43D6BEDFBBC4BD9AEBD17BA023E569906BB86599CBD4E28073E1639F5BDF60909BE8B4727BEE4AD153EDF3C05BEB01913BEEB1A59BD03E8D4BD4BD3123D9EA381BD6058F03CD0EFF73D00747FBADBC5AEBD5054273E204DB4BD00CA683B1E28C93D3BCC2A3D9B0E683D4302923D9A3408BEABC89D3A565336BCC0A7F3BD76D1F93D68A3B93D44891C3E1685243E1B3FDBBD5E06A4BD2B4192BD2B19983C50C97B3D40A808BEC0994C3D4B3435BD0B88293D506749BDFC13063E2B7ADF3CF3B013BE"> : tensor<4x4x3x3xf32>, sym_name = "hex_f32_large_attr"} : () -> ()

// CHECK: "value" = dense<[[[[9.992968e-02, -1.430319e-01, 1.480872e-01], [-1.135054e-01, -3.655422e-02, -1.524415e-01], [-1.096354e-01, 1.070076e-01, 1.635475e-01]], [[6.201498e-02, 1.377752e-01, 6.284702e-02], [1.423031e-02, -2.755300e-02, -5.018222e-02], [1.126285e-01, 1.467116e-01, -4.841509e-02]], [[1.616688e-01, 6.736130e-02, -1.368682e-01], [5.771752e-02, -1.536790e-01, 5.869561e-02], [-9.142806e-02, -4.854431e-02, -6.135579e-02]], [[-6.997196e-02, 1.221017e-01, 8.880942e-02], [-9.790727e-02, 8.166470e-02, 1.657003e-01], [1.432140e-01, -6.376306e-04, 2.486622e-02]]], [[[-8.931184e-02, -8.708668e-02, -2.310556e-02], [5.971766e-02, 1.079614e-01, -1.042320e-01], [1.297905e-01, -9.458923e-02, 3.385238e-02]], [[1.064524e-01, 7.494444e-02, -1.417377e-02], [-3.528321e-02, -8.690961e-02, 1.572554e-01], [-1.448007e-01, 1.396378e-03, 2.953589e-03]], [[-8.743954e-02, 1.499613e-01, -1.458949e-01], [6.397714e-02, -1.601468e-02, 1.705752e-02], [-8.939862e-02, 1.030373e-01, -4.113474e-02]], [[-1.393882e-01, 1.261400e-01, -1.203568e-01], [-1.384676e-03, -1.290683e-01, 1.208070e-01], [5.880459e-02, 8.035894e-02, 5.368434e-02]]], [[[-1.136149e-01, -1.179534e-01, 7.016367e-02], [1.793486e-02, -1.264111e-01, -8.228018e-02], [-1.767119e-03, 1.541317e-03, -1.610818e-01]], [[-8.073064e-02, 6.758833e-02, 9.321358e-02], [-2.199372e-03, 1.472749e-02, -1.560103e-01], [7.020243e-02, -1.053098e-01, 4.541967e-02]], [[1.396751e-01, 9.858086e-02, 3.462158e-02], [1.098654e-01, 6.237565e-02, 1.037528e-01], [-3.075286e-02, -8.537539e-02, 1.276630e-01]], [[-2.053817e-03, -7.634263e-02, 1.319897e-01], [-1.197378e-01, -1.338271e-01, -1.633589e-01], [1.461712e-01, -1.301150e-01, -1.436527e-01]]], [[[-5.300419e-02, -1.039582e-01, 3.584604e-02], [-6.330036e-02, 2.933902e-02, 1.210629e-01], [-9.744763e-04, -8.533832e-02, 1.634076e-01]], [[-8.803773e-02, 3.552079e-03, 9.822105e-02], [4.169868e-02, 5.665455e-02, 7.129338e-02], [-1.330132e-01, 1.203795e-03, -1.112827e-02]], [[-1.189723e-01, 1.219815e-01, 9.064370e-02], [1.528674e-01, 1.606639e-01, -1.070540e-01], [-8.009027e-02, -7.141336e-02, 1.856669e-02]], [[6.147128e-02, -1.334543e-01, 4.995131e-02], [-4.423932e-02, 4.138951e-02, -4.917079e-02], [1.309356e-01, 2.727993e-02, -1.442297e-01]]]]> : tensor<4x4x3x3xf32>

"func.func"() ({}) {function_type = () -> (), value = "foo", sym_name = "string_attr"} : () -> ()

Expand Down
11 changes: 8 additions & 3 deletions xdsl/parser/attribute_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,24 +721,29 @@ def _parse_builtin_dense_attr_hex(

# Use struct builtin package for unpacking f32, f64
format_str: str = ""
num_chunks = 0
match element_type:
case Float32Type():
chunk_size = 4
format_str = "@f" # @ in format string implies native endianess
num_chunks = len(byte_list) // chunk_size
format_str = (
f"@{num_chunks}f" # @ in format string implies native endianess
)
case Float64Type():
chunk_size = 8
format_str = "@d"
num_chunks = len(byte_list) // chunk_size
format_str = f"@{num_chunks}d"
case IntegerType():
if element_type.width.data % 8 != 0:
self.raise_error(
"Hex strings for dense literals only support integer types that are a multiple of 8 bits"
)
chunk_size = element_type.width.data // 8
num_chunks = len(byte_list) // chunk_size
case _:
self.raise_error(
"Hex strings for dense literals are only supported for int, f32 and f64 types"
)
num_chunks = len(byte_list) // chunk_size

data_values: list[int] | list[float] = []

Expand Down

0 comments on commit 2ae41dd

Please sign in to comment.