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

Netron output (ONNX JSON) #3717

Merged
merged 31 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fc41a6f
initial
CharlieL7 May 1, 2024
7f79ce2
Merge branch 'develop' of github.com:ROCmSoftwarePlatform/AMDMIGraphX…
CharlieL7 Oct 3, 2024
0796820
something
CharlieL7 Oct 28, 2024
0c01550
Merge branch 'develop' of github.com:ROCmSoftwarePlatform/AMDMIGraphX…
CharlieL7 Nov 11, 2024
b49a762
Merge branch 'develop' of github.com:ROCmSoftwarePlatform/AMDMIGraphX…
CharlieL7 Nov 28, 2024
0dd2876
Merge branch 'develop' of github.com:ROCmSoftwarePlatform/AMDMIGraphX…
CharlieL7 Dec 5, 2024
74870fb
more progress
CharlieL7 Dec 7, 2024
25e76a0
first draft
CharlieL7 Dec 7, 2024
e7a40ab
First functional
CharlieL7 Dec 8, 2024
0e16a1a
Merge branch 'develop' of github.com:ROCmSoftwarePlatform/AMDMIGraphX…
CharlieL7 Dec 8, 2024
a375111
handle attributes
CharlieL7 Dec 16, 2024
fe76f7e
Merge branch 'develop' of github.com:ROCmSoftwarePlatform/AMDMIGraphX…
CharlieL7 Dec 16, 2024
552638b
PR updates, base64 encode only and tests
CharlieL7 Dec 18, 2024
1d89e41
Merge branch 'develop' into onnx_json_output
CharlieL7 Dec 18, 2024
755ce18
Fix the bug add RFC tests
CharlieL7 Dec 18, 2024
d2b16cc
formatting
CharlieL7 Dec 18, 2024
936dba9
Merge branch 'onnx_json_output' of github.com:ROCmSoftwarePlatform/AM…
CharlieL7 Dec 18, 2024
8bde3ef
Remove requirements change
CharlieL7 Dec 18, 2024
18b373f
Remove requirements change
CharlieL7 Dec 18, 2024
b3f99eb
Merge branch 'onnx_json_output' of github.com:ROCmSoftwarePlatform/AM…
CharlieL7 Dec 18, 2024
f89dfd5
fix tidy and another base64 refactor
CharlieL7 Dec 19, 2024
965644a
Licensing
CharlieL7 Dec 19, 2024
937836f
Merge branch 'develop' of github.com:ROCmSoftwarePlatform/AMDMIGraphX…
CharlieL7 Dec 19, 2024
cf9271b
more tidy fixes
CharlieL7 Dec 19, 2024
a195d07
Codecov ignore, tidy fixes
CharlieL7 Dec 19, 2024
a758744
even more tidy fixes
CharlieL7 Dec 19, 2024
7a2ee8d
PR comments
CharlieL7 Dec 19, 2024
211b2d7
fix back to unsigned char (byte)
CharlieL7 Dec 19, 2024
18019ed
move throw
CharlieL7 Dec 19, 2024
a062ef8
formatting
CharlieL7 Dec 19, 2024
5e92ff9
pacify cppcheck
CharlieL7 Dec 19, 2024
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
1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ ignore:
- "test/"
- "src/driver"
- "build/"
- "src/netron_output.cpp"
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ pybind/pybind11@3e9dfa2866941655c56877882565e7577de6fc7b --build
msgpack/[email protected] -DMSGPACK_BUILD_TESTS=Off
[email protected] -DCMAKE_POSITION_INDEPENDENT_CODE=On
ROCm/composable_kernel@b7775add2d28251674d81e220cd4a857b90b997a -DCK_BUILD_JIT_LIB=On -DCMAKE_POSITION_INDEPENDENT_CODE=On
ROCm/rocMLIR@13065c4b3a216e1b13dfb8f746b8a0d421f124e8 -DBUILD_FAT_LIBROCKCOMPILER=On
ROCm/rocMLIR@13065c4b3a216e1b13dfb8f746b8a0d421f124e8 -DBUILD_FAT_LIBROCKCOMPILER=On
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ add_library(migraphx
argument.cpp
autocast_fp8.cpp
auto_contiguous.cpp
base64.cpp
common.cpp
common_dims.cpp
compile_src.cpp
Expand Down Expand Up @@ -73,6 +74,7 @@ add_library(migraphx
memory_coloring.cpp
module.cpp
msgpack.cpp
netron_output.cpp
normalize_attributes.cpp
normalize_ops.cpp
op_enums.cpp
Expand Down
81 changes: 81 additions & 0 deletions src/base64.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <migraphx/base64.hpp>
#include <vector>
#include <array>
#include <iostream>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

namespace {
using byte = unsigned char;

std::array<char, 64> constexpr b64_chars{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};

/// base64 encoder snippet altered from https://stackoverflow.com/a/37109258
std::string encode(const std::vector<byte>& buf)
{
std::size_t len = buf.size();
std::vector<byte> res_vec((len + 2) / 3 * 4, '=');
Copy link
Collaborator

Choose a reason for hiding this comment

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

The result should be stored as std::string, there is no reason to prefill it with =, you just push_back the characters.

Copy link
Contributor

Choose a reason for hiding this comment

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

push_back onto strings of very large lengths can be a performance issue. Ideally the space should be just reserved, rather than filled with =.

Copy link
Collaborator

Choose a reason for hiding this comment

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

reserve can be called if there is a perf issue. I dont think its that important here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can't use std::string as is because the type being unsigned char is important.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

could probably use basic_string<unsigned char>, but that's more code changes for what looks like a marginal benefit

std::size_t j = 0;
std::size_t remaining = len % 3;
const size_t last = len - remaining;

for(size_t i = 0; i < last; i += 3)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would use the iterators directly for the loop:

for(auto it = buf.begin(); it < buf.end(); it += 3)
{
    std::size_t n = to_int(it[0]) << 16u | to_int(it[1]) << 8u | to_int(it[2]);

    ...
}

And add the to_int function to bit_cast it and convert it to std::size_t.

Copy link
Collaborator

@pfultz2 pfultz2 Dec 19, 2024

Choose a reason for hiding this comment

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

Actually it would be better to make a function to encode the triplet since you repeat the same below for the "padding":

template<class Input>
std::array<char, 4> encode(Input input)
{
    std::size_t n = to_int(it[0]) << 16u | to_int(it[1]) << 8u | to_int(it[2]);
    return {b64_chars.at(n >> 18u), b64_chars.at(n >> 12u & 0x3Fu), b64_chars.at(n >> 6u & 0x3Fu), b64_chars.at(n & 0x3Fu) };
}

Then the loop can do:

for(auto it = buf.begin(); it < buf.end(); it += 3)
{
    copy(encode(it), std::back_inserter(result));
}

Copy link
Contributor

Choose a reason for hiding this comment

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

As far as iterator usage is concerned, lt or gt comparison is not recommended against buf.end(). It should strictly be used by an equality comparison: == or !=.

Copy link
Collaborator

Choose a reason for hiding this comment

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

As far as iterator usage is concerned, lt or gt comparison is not recommended against buf.end()

Not recommended, by who? These are random access iterators so they support comparison operators just like pointers. The < operator should definitely be used here since we are skipping over by increments of 3, which means it could skip past the end, and it would become an infinite loop since we would never reach the end as we already past it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

But it could be UB if its past buf.end() so we probably need to do buf.end() - remaining to avoid that.

{
std::size_t n = static_cast<std::size_t>(buf.at(i)) << 16u |
static_cast<std::size_t>(buf.at(i + 1)) << 8u |
static_cast<std::size_t>(buf.at(i + 2));
res_vec.at(j++) = b64_chars.at(n >> 18u);
res_vec.at(j++) = b64_chars.at(n >> 12u & 0x3Fu);
res_vec.at(j++) = b64_chars.at(n >> 6u & 0x3Fu);
res_vec.at(j++) = b64_chars.at(n & 0x3Fu);
}
// Set padding
if(remaining != 0)
{
std::size_t n = --remaining == 0 ? static_cast<std::size_t>(buf.at(last))
: static_cast<std::size_t>(buf.at(last)) << 8u |
static_cast<std::size_t>(buf.at(last + 1));
res_vec.at(j++) = b64_chars.at(remaining == 0 ? n >> 2u : n >> 10u & 0x3Fu);
res_vec.at(j++) = b64_chars.at(remaining == 0 ? n << 4u & 0x3Fu : n >> 4u & 0x03Fu);
res_vec.at(j++) = remaining == 0 ? '=' : b64_chars.at(n << 2u & 0x3Fu);
}
Copy link
Collaborator

@pfultz2 pfultz2 Dec 19, 2024

Choose a reason for hiding this comment

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

You can reuse the encode function so it doesnt repeat the code. You can also copy the chars into an array(this way pad_cond is not getting sublty modified):

assert(pad_cond < 3);
std::array<char, 3> triple = {0};
// Get the remaining characters to encode
std::copy(buf.end() - pad_cond, buf.end(), triple.begin());
auto e = encode(triple);
// Add the encoded characters
std::copy(e.begin(), e.begin() + 1 + pad_cond, std::back_inserter(result));
// Pad string with `=`
result.append(3 - pad_cond, '=');

return {res_vec.begin(), res_vec.end()};
}

} // namespace

std::string base64_encode(const std::string& str)
{
return encode(std::vector<byte>(str.begin(), str.end()));
}

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
8 changes: 8 additions & 0 deletions src/driver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
#include <migraphx/simplify_reshapes.hpp>
#include <migraphx/register_target.hpp>

#include <migraphx/netron_output.hpp>

#include <fstream>

namespace migraphx {
Expand Down Expand Up @@ -166,6 +168,10 @@ struct loader
{"--binary"},
ap.help("Print out program in binary format."),
ap.set_value("binary"));
ap(output_type,
{"--netron"},
ap.help("Print out program as Netron readable json."),
ap.set_value("netron"));
ap(output, {"--output", "-o"}, ap.help("Output to file."));
}

Expand Down Expand Up @@ -418,6 +424,8 @@ struct loader
*os << to_json_string(p.to_value()) << std::endl;
else if(type == "binary")
write(*os, save_buffer(p));
else if(type == "netron")
*os << make_netron_output(p) << std::endl;
}
};

Expand Down
39 changes: 39 additions & 0 deletions src/include/migraphx/base64.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MIGRAPHX_GUARD_RTGLIB_BASE64_HPP
#define MIGRAPHX_GUARD_RTGLIB_BASE64_HPP

#include <string>
#include <migraphx/config.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

/// encode string to base64
std::string base64_encode(const std::string& str);

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx

#endif
39 changes: 39 additions & 0 deletions src/include/migraphx/netron_output.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MIGRAPHX_GUARD_RTGLIB_NETRON_OUTPUT_HPP
#define MIGRAPHX_GUARD_RTGLIB_NETRON_OUTPUT_HPP

#include <string>
#include <migraphx/config.hpp>
#include <migraphx/program.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {

MIGRAPHX_EXPORT std::string make_netron_output(const program& prog);

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx

#endif
Loading
Loading