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

PTQ with ResNet conv2x layers #1433

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

PTQ with ResNet conv2x layers #1433

wants to merge 12 commits into from

Conversation

singagan
Copy link
Collaborator

No description provided.

Copy link
Contributor

@github-actions github-actions bot left a comment


@core(cores[i][1], "conv2dk3.o")
def core_body():
if(i==2):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
if(i==2):
if i == 2:


@core(cores[i][3], "conv2dk3.o")
def core_body():
if(i==2):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
if(i==2):
if i == 2:

NpuWriteRTPOp("rtpComputeTile12", col=1, row=2, index=0, value=9)
NpuWriteRTPOp("rtpComputeTile13", col=1, row=3, index=0, value=12)
NpuWriteRTPOp("rtpComputeTile13", col=1, row=3, index=1, value=0)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change


NpuWriteRTPOp("rtpComputeTile22", col=2, row=2, index=0, value=9)
NpuWriteRTPOp("rtpComputeTile23", col=2, row=3, index=0, value=9)
NpuWriteRTPOp("rtpComputeTile25", col=2, row=5, index=0, value=9)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
NpuWriteRTPOp("rtpComputeTile25", col=2, row=5, index=0, value=9)
NpuWriteRTPOp("rtpComputeTile25", col=2, row=5, index=0, value=9)

Comment on lines 958 to 960
rtp_1=[7,10,13,-2,10]
rtp_2=[8,10,12]
rtp_3=[9,9,12]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
rtp_1=[7,10,13,-2,10]
rtp_2=[8,10,12]
rtp_3=[9,9,12]
rtp_1 = [7, 10, 13, -2, 10]
rtp_2 = [8, 10, 12]
rtp_3 = [9, 9, 12]

block_0_relu_2 * block_0_weight_scale_3 / block_0_add_scale, 2
) # after conv1x1
block_0_combined_scale4 = -math.log(
block_0_add_scale / block_0_relu_3, 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
block_0_add_scale / block_0_relu_3, 2
block_0_add_scale / block_0_relu_3, 2

) # after skip addition using init scale
# combined_scale4=-math.log(inp_scale1/inp_scale4)
block_0_combined_scale_skip = -math.log(
init_scale * block_0_weight_scale_skip / block_0_add_scale, 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
init_scale * block_0_weight_scale_skip / block_0_add_scale, 2
init_scale * block_0_weight_scale_skip / block_0_add_scale, 2

print("Block0 combined_scale after first conv1x1:", block_0_combined_scale1)
print("Block0 combined_scale after second conv3x3:", block_0_combined_scale2)
print("Block0 combined_scale after third conv1x1:", block_0_combined_scale3)
print("Block0 combined_scale after adding skip connection:", (block_0_combined_scale4))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
print("Block0 combined_scale after adding skip connection:", (block_0_combined_scale4))
print(
"Block0 combined_scale after adding skip connection:", (block_0_combined_scale4)
)

print("Block1 combined_scale after first conv1x1:", block_1_combined_scale1)
print("Block1 combined_scale after second conv3x3:", block_1_combined_scale2)
print("Block1 combined_scale after third conv1x1:", block_1_combined_scale3)
print("Block1 combined_scale after adding skip connection:", (block_1_combined_scale4))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
print("Block1 combined_scale after adding skip connection:", (block_1_combined_scale4))
print(
"Block1 combined_scale after adding skip connection:", (block_1_combined_scale4)
)

(block_2_combined_scale4),
)
print("------------------------------------------------------------------")
# ------------------------------------------------------
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
# ------------------------------------------------------
# ------------------------------------------------------

im_name = f"./cifar_images/image_{i}.png"
cv2.imwrite(im_name, im)


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change

)
quant_id_1.eval()


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change

# ------------------------------------------------------
# Main run loop
# ------------------------------------------------------

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change

# ------------------------------------------------------

for i in range(0, 64):
print("____________________________________IMAGE {}____________________________________________".format(i))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
print("____________________________________IMAGE {}____________________________________________".format(i))
print(
"____________________________________IMAGE {}____________________________________________".format(
i
)
)


# Calculate the five categories with the highest classification probability
prediction_class_index = (
torch.topk(final_output_aie, k=5, sorted=True).indices.squeeze(0).tolist()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
torch.topk(final_output_aie, k=5, sorted=True).indices.squeeze(0).tolist()
torch.topk(final_output_aie, k=5, sorted=True)
.indices.squeeze(0)
.tolist()

torch.topk(final_output_aie, k=5, sorted=True).indices.squeeze(0).tolist()
)
golden_prediction_class_index = (
torch.topk(final_output_base, k=5, sorted=True).indices.squeeze(0).tolist()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
torch.topk(final_output_base, k=5, sorted=True).indices.squeeze(0).tolist()
torch.topk(final_output_base, k=5, sorted=True)
.indices.squeeze(0)
.tolist()

Comment on lines 459 to 461
if x != y:
print("\nFailed.\n")
exit(-1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
if x != y:
print("\nFailed.\n")
exit(-1)
if x != y:
print("\nFailed.\n")
exit(-1)

print("\nPASS!\n")
exit(0)

if __name__ == "__main__":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
if __name__ == "__main__":
if __name__ == "__main__":

import cv2
import numpy as np

def unpickle(file):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
def unpickle(file):
def unpickle(file):

im = im.transpose(1, 2, 0)
im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
im_name = f"./cifar_images/image_{i}.png"
cv2.imwrite(im_name, im)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
cv2.imwrite(im_name, im)
cv2.imwrite(im_name, im)

@singagan singagan marked this pull request as ready for review May 14, 2024 18:32
@@ -0,0 +1,89 @@
# This file is licensed under the Apache License v2.0 with LLVM Exceptions.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this CMakeList.txt needed? Testing is only supported through a Python test.py file.

clean:
rm -rf build/*.elf* build/*.lst build/*.bif log* build/${mlirFileName}.mlir.prj build/*.xclbin sim \
build/chess* build/insts.txt \
build/*.log build/aie_partition.json build/*.bin build/BOOT.BIN _x test.exe
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no test.exe being build

quant_model = quantize_model(
model_aie,
backend="flexml",
scale_factor_type="po2_scale", # Ensuring scale factors are powers of two
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
scale_factor_type="po2_scale", # Ensuring scale factors are powers of two
scale_factor_type="po2_scale", # Ensuring scale factors are powers of two

if name.endswith(".bias"):
param.data.fill_(0)

# Calculate combined scales
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[black] reported by reviewdog 🐶

Suggested change
# Calculate combined scales
# Calculate combined scales

@singagan
Copy link
Collaborator Author

@denolf, added a README and made the requested changes.

@singagan singagan requested a review from denolf May 28, 2024 03:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants