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

Hash model inputs instead of parameters #324

Merged
merged 40 commits into from
Jun 22, 2023
Merged

Hash model inputs instead of parameters #324

merged 40 commits into from
Jun 22, 2023

Conversation

danielholanda
Copy link
Contributor

@danielholanda danielholanda commented Jun 14, 2023

Closes #322

Overview

This PR stops hashing the models using weights and hashes input shapes instead.

Description

This PR closes #322 by implement the long-term solution and revert the workaround mentioned here: #316 (comment) (closing #316 in the same PR).

It also adds a test to test/analysis.py to prevent this kind of break in the future.

Manually testing

CI tests were added. However, you may manually test this PR by running benchit two_calls.py --analyze-only.

two_calls.py

# labels: name::mobilenetv2_035 author::timm task::Computer_Vision
import torch
import timm
from mlagility.parser import parse

# Parsing command-line arguments
batch_size = parse(["batch_size"])

# Creating model and set it to evaluation mode
model = timm.create_model("mobilenetv2_035", pretrained=False)
model.eval()

# Creating inputs
inputs1 = torch.rand((1, 3, 28, 28))
inputs2 = torch.rand((1, 3, 224, 224))

# Calling model
model(inputs1)
model(inputs2)

Expected results:

Models discovered during profiling:

two_input_shapes.py:
        model (executed 1x - 0.01s)
                Model Type:     Pytorch (torch.nn.Module)
                Class:          EfficientNet (<class 'timm.models.efficientnet.EfficientNet'>)
                Location:       /home/dhnoronha/.local/lib/python3.8/site-packages/timm/models/efficientnet_builder.py, line 472
                Parameters:     1,677,128 (3.2 MB)
                Input Shape:    {'Positional Arg 1': (1, 3, 28, 28)}
                Hash:           b63f3042

        model (executed 1x - 0.03s)
                Model Type:     Pytorch (torch.nn.Module)
                Class:          EfficientNet (<class 'timm.models.efficientnet.EfficientNet'>)
                Location:       /home/dhnoronha/.local/lib/python3.8/site-packages/timm/models/efficientnet_builder.py, line 472
                Parameters:     1,677,128 (3.2 MB)
                Input Shape:    {'Positional Arg 1': (1, 3, 224, 224)}
                Hash:           062ae984


Woohoo! The 'benchmark' command is complete.

Note: Input Shape is only displayed when a model has more than one workload

@danielholanda danielholanda self-assigned this Jun 14, 2023
@danielholanda danielholanda marked this pull request as ready for review June 16, 2023 00:48
@danielholanda
Copy link
Contributor Author

Should we start differentiating between workloads and models in our documentation?

Examples:

  • Replace Each model in a script is identified by a unique hash with Each workload in a script is identified by a unique hash. (user guide)
  • Replace Models discovered during profiling: with Workloads discovered during profiling: (cli message)

Copy link
Contributor

@jeremyfowers jeremyfowers left a comment

Choose a reason for hiding this comment

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

Discuss architecture

src/mlagility/analysis/analysis.py Show resolved Hide resolved
src/mlagility/analysis/analysis.py Outdated Show resolved Hide resolved
src/mlagility/analysis/analysis.py Outdated Show resolved Hide resolved
src/mlagility/analysis/status.py Outdated Show resolved Hide resolved
src/mlagility/analysis/status.py Outdated Show resolved Hide resolved
@danielholanda
Copy link
Contributor Author

danielholanda commented Jun 17, 2023

Heads up: Significant changes were made to ensure that analysis also works when we have the same models with multiple inputs AND max-depth is > 1. Here is one example of things working correctly.

Note that the shapes and hashes of the "deeper" models are different when you compare the two workloads.

two_input_shapes.py:
        model (executed 1x - 0.18s)
                Model Type:     Pytorch (torch.nn.Module)
                Class:          EfficientNet (<class 'timm.models.efficientnet.EfficientNet'>)
                Location:       /home/dhnoronha/.local/lib/python3.8/site-packages/timm/models/efficientnet_builder.py, line 472
                Parameters:     1,677,128 (3.2 MB)
                Input Shape:    'Positional Arg 1': (1, 3, 28, 28)
                Hash:           4c4868c4

                        blocks (executed 1x - 0.17s)
                                Model Type:     Pytorch (torch.nn.Module)
                                Class:          Sequential (<class 'torch.nn.modules.container.Sequential'>)
                                Parameters:     249,744 (0.5 MB)
                                Input Shape:    'Positional Arg 1': (1, 16, 14, 14)
                                Hash:           2b4daad8

                        bn2 (executed 1x - 0.00s)
                                Model Type:     Pytorch (torch.nn.Module)
                                Class:          BatchNormAct2d (<class 'timm.models.layers.norm_act.BatchNormAct2d'>)
                                Parameters:     2,560 (<0.1 MB)
                                Input Shape:    'Positional Arg 1': (1, 1280, 1, 1)
                                Hash:           26e367bf

        model (executed 1x - 0.22s)
                Model Type:     Pytorch (torch.nn.Module)
                Class:          EfficientNet (<class 'timm.models.efficientnet.EfficientNet'>)
                Location:       /home/dhnoronha/.local/lib/python3.8/site-packages/timm/models/efficientnet_builder.py, line 472
                Parameters:     1,677,128 (3.2 MB)
                Input Shape:    'Positional Arg 1': (1, 3, 224, 224)
                Hash:           cbdf10c5

                        blocks (executed 1x - 0.21s)
                                Model Type:     Pytorch (torch.nn.Module)
                                Class:          Sequential (<class 'torch.nn.modules.container.Sequential'>)
                                Parameters:     249,744 (0.5 MB)
                                Input Shape:    'Positional Arg 1': (1, 16, 112, 112)
                                Hash:           7714ae20

                        bn2 (executed 1x - 0.00s)
                                Model Type:     Pytorch (torch.nn.Module)
                                Class:          BatchNormAct2d (<class 'timm.models.layers.norm_act.BatchNormAct2d'>)
                                Parameters:     2,560 (<0.1 MB)
                                Input Shape:    'Positional Arg 1': (1, 1280, 7, 7)
                                Hash:           83405a1c

@danielholanda danielholanda marked this pull request as draft June 17, 2023 00:04
@danielholanda
Copy link
Contributor Author

@jeremyfowers I converted this PR back to draft since I intend to change the messages displayed to the user as discussed in #324 (comment)

@danielholanda danielholanda marked this pull request as ready for review June 20, 2023 23:45
Copy link
Contributor

@jeremyfowers jeremyfowers left a comment

Choose a reason for hiding this comment

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

Looking great, this is pretty much good to go! Just doing "request changes" so that I have a chance to check the documentation addition before this merges.

src/mlagility/analysis/status.py Outdated Show resolved Hide resolved
models/llm_layer/llama_layer_prototype.py Outdated Show resolved Hide resolved
src/mlagility/analysis/analysis.py Outdated Show resolved Hide resolved
src/mlagility/analysis/status.py Outdated Show resolved Hide resolved
Copy link
Contributor

@jeremyfowers jeremyfowers left a comment

Choose a reason for hiding this comment

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

Thank you for all the brainstorming and multiple rounds of feedback! This change wound up in a really good place.

I pushed a few commits with minor copy-editing and I added your new example to CI (no functionality changes).

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.

Analysis fails: Model hash not found
2 participants