-
Notifications
You must be signed in to change notification settings - Fork 1.6k
generate gemm sampel for 3.4 branch #983
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
import onnxsim | ||
import google.protobuf.text_format | ||
import io | ||
|
||
from typing import Optional | ||
|
||
def assertExpected(s): | ||
if not (isinstance(s, str) or (sys.version_info[0] == 2 and isinstance(s, unicode))): | ||
|
@@ -73,6 +73,24 @@ def save_onnx_data_and_model(input, output, name, operation, *args, **kwargs): | |
model = onnx.helper.make_model(graph, producer_name=name) | ||
onnx.save(model, models_files) | ||
|
||
def save_data_and_onnx_model(name, input_np, output_np, onnx_model): | ||
print(name + " input has sizes", input_np.shape) | ||
input_files = os.path.join("data", "input_" + name) | ||
np.save(input_files, input_np.data) | ||
|
||
print(name + " output has sizes", output_np.shape) | ||
print() | ||
output_files = os.path.join("data", "output_" + name) | ||
np.save(output_files, np.ascontiguousarray(output_np.data)) | ||
|
||
models_files = os.path.join("models", name + ".onnx") | ||
|
||
onnx_model_pb = onnx._serialize(onnx_model) | ||
model_def = assertONNXExpected(onnx_model_pb) | ||
with open(models_files, 'wb') as file: | ||
file.write(model_def.SerializeToString()) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and two empty lines instead of one here |
||
|
||
def simplify(name, rename=False, **kwargs): | ||
model, check = onnxsim.simplify(name, **kwargs) | ||
assert check, "couldn't valide" | ||
|
@@ -1665,4 +1683,46 @@ def forward(self, a, b): | |
save_data_and_model_multy_inputs('output_registration', model, a, b) | ||
model = onnx.load('models/output_registration.onnx') | ||
model.graph.node[0].name = model.graph.output[0].name | ||
onnx.save(model, 'models/output_registration.onnx') | ||
onnx.save(model, 'models/output_registration.onnx') | ||
|
||
# ########################## GEMM ########################## | ||
# The original code is : https://github.com/onnx/onnx/blob/main/onnx/backend/test/case/node/gemm.py | ||
def gemm_reference_implementation(A: np.ndarray, B: np.ndarray, C: Optional[np.ndarray] = None, alpha: float = 1., beta: float = 1., transA: int = 0, | ||
transB: int = 0) -> np.ndarray: | ||
A = A if transA == 0 else A.T | ||
B = B if transB == 0 else B.T | ||
C = C if C is not None else np.array(0) | ||
|
||
Y = alpha * np.dot(A, B) + beta * C | ||
|
||
return Y | ||
|
||
## gemm without transB | ||
input_np = np.random.rand(2, 10).astype("float32") | ||
inputs = [onnx.helper.make_tensor_value_info("input1", onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[input_np.dtype], shape=input_np.shape)] | ||
|
||
weight_np = np.random.rand(10, 3).astype("float32") | ||
weight_tensor = onnx.helper.make_tensor('weight_tensor', data_type=onnx.mapping.NP_TYPE_TO_TENSOR_TYPE[weight_np.dtype], dims=weight_np.shape, vals=weight_np) | ||
|
||
outputs = [onnx.helper.make_tensor_value_info("output", onnx.TensorProto.FLOAT, shape=(2, 3))] | ||
|
||
nodes = [onnx.helper.make_node("Gemm", ["input1", "weight_tensor"], ["output"])] | ||
|
||
graph = onnx.helper.make_graph(nodes, | ||
"gemm_test", | ||
inputs, | ||
outputs, initializer=[weight_tensor]) | ||
gemm_model = onnx.helper.make_model(graph) | ||
output_np = gemm_reference_implementation(input_np, weight_np) | ||
save_data_and_onnx_model("gemm_no_transB", input_np, output_np, gemm_model) | ||
|
||
## gemm with transB = 0 | ||
|
||
nodes2 = [onnx.helper.make_node("Gemm", ["input1", "weight_tensor"], ["output"], transB=0)] | ||
graph2 = onnx.helper.make_graph(nodes2, | ||
"gemm_test", | ||
inputs, | ||
outputs, initializer=[weight_tensor]) | ||
gemm_model2 = onnx.helper.make_model(graph2) | ||
output_np = gemm_reference_implementation(input_np, weight_np) | ||
save_data_and_onnx_model("gemm_transB_0", input_np, output_np, gemm_model2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the master branch there is only one empty line here
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Now I understand, we need to make sure that the bits of PR of the two branches are exactly the same.
Short question, if we encounter the same thing in the future, can we do this via git? rather than manual copying.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forwarding this question to @alalek
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4.6.0 - 2 empty lines: https://github.com/opencv/opencv_extra/blob/4.6.0/testdata/dnn/onnx/generate_onnx_models.py
one line is here for the last few hours only: #982
Git works well in one-way. Two-way merging is a mess in general (some changes are just lost).
Proposed patches propagation is 3.4 -> 4.x -> 5.x: https://github.com/opencv/opencv/wiki/Branches
So this is why we need to put code into 3.4 branch first instead of 4.x if it is applicable and needed.
Manual backporting 4.x -> 3.4 is possible, but it is still manual (through
git cherry-pick
and resolving conflicts).