Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify bit packing #9

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions test/Ne16MemoryLayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ def weightEncode(
# (cout, cinMajor, Bits, flattened spatial, cinMinor)
weight = weight.transpose(0, 1, 4, 3, 2)

# Prepare for packing
# (cout, cinMajor, Bits, flattened spatial, cinMinorBytes, 8)
cinMinorBytes = int(np.ceil(cinMinor / 8))
weight = np.stack(np.split(weight, cinMinorBytes, axis=-1), axis=-2)

# Pack
# (cout, cinMajor, Bits, flattened spatial, cinMinorBytes)
# Pack bits
# (-1, 8)
weight = weight.reshape(-1, 8)
# (-1, 1)
weight = np.packbits(weight, axis=-1, bitorder="little")

# Flatten the weights
# (-1, )
return weight.flatten()

@staticmethod
Expand Down
11 changes: 5 additions & 6 deletions test/NeurekaMemoryLayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,14 @@ def weightEncode(
cout * cinMajor, NeurekaMemoryLayout._WEIGHT_BANDWIDTH
) # cout*cinMajor, 256b

# Prepare for packing
# (-1, Weight Bandwidth Bytes, 8)
weightBandwidthBytes = int(np.ceil(NeurekaMemoryLayout._WEIGHT_BANDWIDTH / 8))
weight = np.stack(np.split(weight, weightBandwidthBytes, axis=-1), axis=-2)

# Pack bits
# (-1, Weight Bandwidth Bytes)
# (-1, 8)
weight = weight.reshape(-1, 8)
# (-1, 1)
weight = np.packbits(weight, axis=-1, bitorder="little")

# Flatten the weights
# (-1, )
return weight.flatten()

@staticmethod
Expand Down
Loading