这是indexloc提供的服务,不要输入任何密码
Skip to content

feat: add tool for visualizing compressed (and uncompressed) models #3099

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

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

Conversation

rkuester
Copy link
Contributor

Add a development tool that prints compressed and uncompressed
.tflite models to stdout in a human-readable, searchable,
structured, text format. Helpful annotations (indexes of lists,
names of operators, etc.) derived from the model are added as
virtual fields with names beginning with an _underscore.

Add a unit test which simply ensures the viewer does not crash
when run on several models found in the source tree.

BUG=see description

Add a development tool that prints compressed and uncompressed
.tflite models to stdout in a human-readable, searchable,
structured, text format. Helpful annotations (indexes of lists,
names of operators, etc.) derived from the model are added as
virtual fields with names beginning with an _underscore.

Add a unit test which simply ensures the viewer does not crash
when run on several models found in the source tree.

BUG=see description
@rkuester rkuester requested a review from a team as a code owner April 24, 2025 03:47
@TFLM-bot TFLM-bot removed the ci:run label Apr 24, 2025
Copy link
Member

@ddavis-2015 ddavis-2015 left a comment

Choose a reason for hiding this comment

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

Needs a way to see the compressed bitstrings as a list of LUT indices. Maybe with an optional flag?

from tensorflow.lite.python import schema_py_generated as tflite_schema

USAGE = textwrap.dedent(f"""\
Usage: {os.path.basename(sys.argv[0])} $(realpath <MODEL>)
Copy link
Member

Choose a reason for hiding this comment

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

Is that $(realpath <MODEL>) somehow expanded? I'm thinking the output should just be view <model path>. On the other hand, if tensorflow.lite.micro.compression isn't in the PyPi, then this script can only be run using Bazel, in which case the person running the Bazel command would probably know what $(realpath) is?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. See 19f7f21.

# Convert the model into a Python dictionary, expressing the hierarchial nature
# of the model, and pretty print the dictionary. Please extend as needed for
# your use case.

Copy link
Member

Choose a reason for hiding this comment

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

I would add a bazel run command line example here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. See 757e95e.

} for t in sorted(lut_tensors, key=lambda x: x.tensor)]


def is_compressed_buffer(buffer_index, unpacked_metadata):
Copy link
Member

Choose a reason for hiding this comment

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

Method not used. I believe this was part of the code I added to show compressed bitstrings as lists of indices.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. See 2d56eef.

return False, None, None


def unpack_indices(buffer, lut_data):
Copy link
Member

Choose a reason for hiding this comment

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

Method not used. I believe this was part of the code I added to show compressed bitstrings as lists of indices.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. See 2d56eef.

return indices


def unpack_compression_metadata(buffer):
Copy link
Member

Choose a reason for hiding this comment

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

Method is not used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. See 2d56eef.

return buffers


def get_compression_metadata_buffer(model):
Copy link
Member

Choose a reason for hiding this comment

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

Method is not used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. See 2d56eef.

rkuester added 5 commits June 30, 2025 17:00
Add functionality to decode and display LUT indices for compressed
tensor buffers in the model viewer. When viewing compressed models,
buffers containing compressed data now show a "_lut_indices" field
with the decoded indices displayed in a readable multi-line format.

Also improve the numpy array pretty printer to display all values
without ellipsis truncation, making large arrays more readable.
Make the usage message context-aware by detecting if the script is
running under Bazel. When run through Bazel, show the full bazel
command and include an example using $(realpath ...) to convert
relative paths to absolute paths, which is required due to Bazel's
sandboxing. When run standalone, show a simpler usage message.
Remove several functions which were part of an earlier
implementation.
@rkuester
Copy link
Contributor Author

Needs a way to see the compressed bitstrings as a list of LUT indices. Maybe with an optional flag?

Good idea! Implemented in 4919c04, without a flag. Nothing has a flag yet. It's expected you'll pipe this output a pager or write it to a file and search it.

Copy link
Member

@ddavis-2015 ddavis-2015 left a comment

Choose a reason for hiding this comment

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

Looks good to me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants