Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Enhance profile_model.py to output JSON file for benchmark #691

Merged
merged 10 commits into from
Dec 24, 2019

Conversation

hadusam
Copy link
Contributor

@hadusam hadusam commented Dec 18, 2019

Motivation and Context

Current profile_model.py creates a markdown file of profile result.
We need to get a structured file of profile results for the benchmark program, so I enhanced it to create JSON file.

Description

How has this been tested?

$ docker-compose run --rm blueoil /bin/bash -c "cd lmnet; python executor/profile_model.py -i train_20191216084759 -b 1"

...some warnings

----------------- config value --------------------
{'BATCH_SIZE': 1,
 'CLASSES': ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
 'DATASET': {'AUGMENTOR': [Blur({'min_value': 0, 'max_value': 1})],
             'BATCH_SIZE': 1,
             'DATA_FORMAT': 'NHWC',
             'ENABLE_PREFETCH': True,
             'PRE_PROCESSOR': [Resize({'size': [128, 128]}), DivideBy255({})]},
 'DATASET_CLASS': <class 'abc.DATASET_CLASS'>,
 'DATA_FORMAT': 'NHWC',
 'IMAGE_SIZE': [128, 128],
 'IS_DEBUG': False,
 'IS_PRETRAIN': False,
 'KEEP_CHECKPOINT_MAX': 5,
 'MAX_EPOCHS': 1,
 'NETWORK': {'ACTIVATION_QUANTIZER': <function linear_mid_tread_half_quantizer at 0x7f8adb71c598>,
             'ACTIVATION_QUANTIZER_KWARGS': {'bit': 2, 'max_value': 2},
             'BATCH_SIZE': 1,
             'DATA_FORMAT': 'NHWC',
             'IMAGE_SIZE': [128, 128],
             'LEARNING_RATE_FUNC': None,
             'LEARNING_RATE_KWARGS': None,
             'OPTIMIZER_CLASS': <class 'tensorflow.python.training.adam.AdamOptimizer'>,
             'OPTIMIZER_KWARGS': {'learning_rate': 0.001},
             'WEIGHT_DECAY_RATE': 0.0005,
             'WEIGHT_QUANTIZER': <function binary_mean_scaling_quantizer at 0x7f8adb71c488>,
             'WEIGHT_QUANTIZER_KWARGS': {}},
 'NETWORK_CLASS': <class 'lmnet.networks.classification.lmnet_v1.LmnetV1Quantize'>,
 'POST_PROCESSOR': None,
 'PRETRAIN_DIR': '',
 'PRETRAIN_FILE': '',
 'PRETRAIN_VARS': [],
 'PRE_PROCESSOR': [Resize({'size': [128, 128]}), DivideBy255({})],
 'SAVE_CHECKPOINT_STEPS': 1000,
 'SUMMARISE_STEPS': 100,
 'TASK': <Tasks.CLASSIFICATION: 'IMAGE.CLASSIFICATION'>,
 'TEST_STEPS': 1000}
----------------- config value --------------------

...some warnings

=========================Options=============================
-max_depth                  10000
-min_bytes                  0
-min_peak_bytes             0
-min_residual_bytes         0
-min_output_bytes           0
-min_micros                 0
-min_accelerator_micros     0
-min_cpu_micros             0
-min_params                 0
-min_float_ops              0
-min_occurrence             0
-step                       -1
-order_by                   name
-account_type_regexes       _trainable_variables
-start_name_regexes         .*
-trim_name_regexes          
-show_name_regexes          .*
-hide_name_regexes          
-account_displayed_op_only  true
-select                     params
-output                     stdout:

==================Model Analysis Report======================

Doc:
scope: The nodes in the model graph are organized by their names, which is hierarchical like filesystem.
param: Number of parameters (in the Variable).

Profile:
node name | # parameters
_TFProfRoot (--/717.23k params)
  conv1 (--/928 params)
    conv1/BatchNorm (--/64 params)
      conv1/BatchNorm/beta (32, 32/32 params)
      conv1/BatchNorm/gamma (32, 32/32 params)
    conv1/conv2d (--/864 params)
      conv1/conv2d/kernel (3x3x3x32, 864/864 params)
  conv2 (--/18.56k params)
    conv2/BatchNorm (--/128 params)
      conv2/BatchNorm/beta (64, 64/64 params)
      conv2/BatchNorm/gamma (64, 64/64 params)
    conv2/conv2d (--/18.43k params)
      conv2/conv2d/kernel (3x3x32x64, 18.43k/18.43k params)
  conv3 (--/295.17k params)
    conv3/BatchNorm (--/256 params)
      conv3/BatchNorm/beta (128, 128/128 params)
      conv3/BatchNorm/gamma (128, 128/128 params)
    conv3/conv2d (--/294.91k params)
      conv3/conv2d/kernel (3x3x256x128, 294.91k/294.91k params)
  conv4 (--/73.86k params)
    conv4/BatchNorm (--/128 params)
      conv4/BatchNorm/beta (64, 64/64 params)
      conv4/BatchNorm/gamma (64, 64/64 params)
    conv4/conv2d (--/73.73k params)
      conv4/conv2d/kernel (3x3x128x64, 73.73k/73.73k params)
  conv5 (--/295.17k params)
    conv5/BatchNorm (--/256 params)
      conv5/BatchNorm/beta (128, 128/128 params)
      conv5/BatchNorm/gamma (128, 128/128 params)
    conv5/conv2d (--/294.91k params)
      conv5/conv2d/kernel (3x3x256x128, 294.91k/294.91k params)
  conv6 (--/32.90k params)
    conv6/BatchNorm (--/128 params)
      conv6/BatchNorm/beta (64, 64/64 params)
      conv6/BatchNorm/gamma (64, 64/64 params)
    conv6/conv2d (--/32.77k params)
      conv6/conv2d/kernel (1x1x512x64, 32.77k/32.77k params)
  conv7 (--/650 params)
    conv7/bias (10, 10/10 params)
    conv7/kernel (1x1x64x10, 640/640 params)

======================End of Report==========================
Parsing Inputs...

=========================Options=============================
-max_depth                  10000
-min_bytes                  0
-min_peak_bytes             0
-min_residual_bytes         0
-min_output_bytes           0
-min_micros                 0
-min_accelerator_micros     0
-min_cpu_micros             0
-min_params                 0
-min_float_ops              1
-min_occurrence             0
-step                       -1
-order_by                   float_ops
-account_type_regexes       .*
-start_name_regexes         .*
-trim_name_regexes          
-show_name_regexes          .*
-hide_name_regexes          
-account_displayed_op_only  true
-select                     float_ops
-output                     stdout:

==================Model Analysis Report======================

Doc:
scope: The nodes in the model graph are organized by their names, which is hierarchical like filesystem.
flops: Number of float operations. Note: Please read the implementation for the math behind it.

Profile:
node name | # float_ops
_TFProfRoot (--/4.27b flops)
  import/conv3/conv2d/Conv2D (2.42b/2.42b flops)
  import/conv2/conv2d/Conv2D (603.98m/603.98m flops)
  import/conv4/conv2d/Conv2D (603.98m/603.98m flops)
  import/conv5/conv2d/Conv2D (603.98m/603.98m flops)
  import/conv1/conv2d/Conv2D (28.31m/28.31m flops)
  import/conv6/conv2d/Conv2D (16.78m/16.78m flops)
  import/conv7/Conv2D (327.68k/327.68k flops)
  import/conv7/BiasAdd (2.56k/2.56k flops)
  import/pool7/AvgPool (2.56k/2.56k flops)
  import/Softmax (50/50 flops)

======================End of Report==========================
Model's profile has been saved into /home/blueoil/saved/train_20191216084759/LmnetV1Quantize_profile.md
save json: /home/blueoil/saved/train_20191216084759/LmnetV1Quantize_profile.json
$ cat LmnetV1Quantize_profile.json 
{
    "model_name": "LmnetV1Quantize",
    "image_size_height": 128,
    "image_size_width": 128,
    "num_classes": 10,
    "flops": {
        "total_flops": 4273280050,
        "children": [
            {
                "name": "conv3",
                "flops": 2415919104
            },
            {
                "name": "conv2",
                "flops": 603979776
            },
            {
                "name": "conv4",
                "flops": 603979776
            },
            {
                "name": "conv5",
                "flops": 603979776
            },
            {
                "name": "conv1",
                "flops": 28311552
            },
            {
                "name": "conv6",
                "flops": 16777216
            },
            {
                "name": "conv7",
                "flops": 330240
            },
            {
                "name": "pool7",
                "flops": 2560
            },
            {
                "name": "Softmax",
                "flops": 50
            }
        ]
    },
    "parameters": {
        "total_parameters": 717226,
        "total_size": 22951232,
        "quant_bit": 1,
        "total_quant_size": 747296,
        "children": [
            {
                "name": "conv1",
                "parameters": 928,
                "size": 29696,
                "quant_size": 2912,
                "children": [
                    {
                        "name": "conv1/BatchNorm",
                        "parameters": 64,
                        "size": 2048,
                        "quant_size": 2048,
                        "children": [
                            {
                                "name": "conv1/BatchNorm/beta",
                                "parameters": 32,
                                "size": 1024,
                                "quant_size": 1024,
                                "children": [],
                                "is_quant_kernel": false
                            },
                            {
                                "name": "conv1/BatchNorm/gamma",
                                "parameters": 32,
                                "size": 1024,
                                "quant_size": 1024,
                                "children": [],
                                "is_quant_kernel": false
                            }
                        ]
                    },
                    {
                        "name": "conv1/conv2d",
                        "parameters": 864,
                        "size": 27648,
                        "quant_size": 864,
                        "children": [
                            {
                                "name": "conv1/conv2d/kernel",
                                "parameters": 864,
                                "size": 27648,
                                "quant_size": 864,
                                "children": [],
                                "is_quant_kernel": true
                            }
                        ]
                    }
                ]
            },
            {
                "name": "conv2",
                "parameters": 18560,
                "size": 593920,
                "quant_size": 22528,
                "children": [
                    {
                        "name": "conv2/BatchNorm",
                        "parameters": 128,
                        "size": 4096,
                        "quant_size": 4096,
                        "children": [
                            {
                                "name": "conv2/BatchNorm/beta",
                                "parameters": 64,
                                "size": 2048,
                                "quant_size": 2048,
                                "children": [],
                                "is_quant_kernel": false
                            },
                            {
                                "name": "conv2/BatchNorm/gamma",
                                "parameters": 64,
                                "size": 2048,
                                "quant_size": 2048,
                                "children": [],
                                "is_quant_kernel": false
                            }
                        ]
                    },
                    {
                        "name": "conv2/conv2d",
                        "parameters": 18432,
                        "size": 589824,
                        "quant_size": 18432,
                        "children": [
                            {
                                "name": "conv2/conv2d/kernel",
                                "parameters": 18432,
                                "size": 589824,
                                "quant_size": 18432,
                                "children": [],
                                "is_quant_kernel": true
                            }
                        ]
                    }
                ]
            },
            {
                "name": "conv3",
                "parameters": 295168,
                "size": 9445376,
                "quant_size": 303104,
                "children": [
                    {
                        "name": "conv3/BatchNorm",
                        "parameters": 256,
                        "size": 8192,
                        "quant_size": 8192,
                        "children": [
                            {
                                "name": "conv3/BatchNorm/beta",
                                "parameters": 128,
                                "size": 4096,
                                "quant_size": 4096,
                                "children": [],
                                "is_quant_kernel": false
                            },
                            {
                                "name": "conv3/BatchNorm/gamma",
                                "parameters": 128,
                                "size": 4096,
                                "quant_size": 4096,
                                "children": [],
                                "is_quant_kernel": false
                            }
                        ]
                    },
                    {
                        "name": "conv3/conv2d",
                        "parameters": 294912,
                        "size": 9437184,
                        "quant_size": 294912,
                        "children": [
                            {
                                "name": "conv3/conv2d/kernel",
                                "parameters": 294912,
                                "size": 9437184,
                                "quant_size": 294912,
                                "children": [],
                                "is_quant_kernel": true
                            }
                        ]
                    }
                ]
            },
            {
                "name": "conv4",
                "parameters": 73856,
                "size": 2363392,
                "quant_size": 77824,
                "children": [
                    {
                        "name": "conv4/BatchNorm",
                        "parameters": 128,
                        "size": 4096,
                        "quant_size": 4096,
                        "children": [
                            {
                                "name": "conv4/BatchNorm/beta",
                                "parameters": 64,
                                "size": 2048,
                                "quant_size": 2048,
                                "children": [],
                                "is_quant_kernel": false
                            },
                            {
                                "name": "conv4/BatchNorm/gamma",
                                "parameters": 64,
                                "size": 2048,
                                "quant_size": 2048,
                                "children": [],
                                "is_quant_kernel": false
                            }
                        ]
                    },
                    {
                        "name": "conv4/conv2d",
                        "parameters": 73728,
                        "size": 2359296,
                        "quant_size": 73728,
                        "children": [
                            {
                                "name": "conv4/conv2d/kernel",
                                "parameters": 73728,
                                "size": 2359296,
                                "quant_size": 73728,
                                "children": [],
                                "is_quant_kernel": true
                            }
                        ]
                    }
                ]
            },
            {
                "name": "conv5",
                "parameters": 295168,
                "size": 9445376,
                "quant_size": 303104,
                "children": [
                    {
                        "name": "conv5/BatchNorm",
                        "parameters": 256,
                        "size": 8192,
                        "quant_size": 8192,
                        "children": [
                            {
                                "name": "conv5/BatchNorm/beta",
                                "parameters": 128,
                                "size": 4096,
                                "quant_size": 4096,
                                "children": [],
                                "is_quant_kernel": false
                            },
                            {
                                "name": "conv5/BatchNorm/gamma",
                                "parameters": 128,
                                "size": 4096,
                                "quant_size": 4096,
                                "children": [],
                                "is_quant_kernel": false
                            }
                        ]
                    },
                    {
                        "name": "conv5/conv2d",
                        "parameters": 294912,
                        "size": 9437184,
                        "quant_size": 294912,
                        "children": [
                            {
                                "name": "conv5/conv2d/kernel",
                                "parameters": 294912,
                                "size": 9437184,
                                "quant_size": 294912,
                                "children": [],
                                "is_quant_kernel": true
                            }
                        ]
                    }
                ]
            },
            {
                "name": "conv6",
                "parameters": 32896,
                "size": 1052672,
                "quant_size": 36864,
                "children": [
                    {
                        "name": "conv6/BatchNorm",
                        "parameters": 128,
                        "size": 4096,
                        "quant_size": 4096,
                        "children": [
                            {
                                "name": "conv6/BatchNorm/beta",
                                "parameters": 64,
                                "size": 2048,
                                "quant_size": 2048,
                                "children": [],
                                "is_quant_kernel": false
                            },
                            {
                                "name": "conv6/BatchNorm/gamma",
                                "parameters": 64,
                                "size": 2048,
                                "quant_size": 2048,
                                "children": [],
                                "is_quant_kernel": false
                            }
                        ]
                    },
                    {
                        "name": "conv6/conv2d",
                        "parameters": 32768,
                        "size": 1048576,
                        "quant_size": 32768,
                        "children": [
                            {
                                "name": "conv6/conv2d/kernel",
                                "parameters": 32768,
                                "size": 1048576,
                                "quant_size": 32768,
                                "children": [],
                                "is_quant_kernel": true
                            }
                        ]
                    }
                ]
            },
            {
                "name": "conv7",
                "parameters": 650,
                "size": 20800,
                "quant_size": 960,
                "children": [
                    {
                        "name": "conv7/bias",
                        "parameters": 10,
                        "size": 320,
                        "quant_size": 320,
                        "children": [],
                        "is_quant_kernel": false
                    },
                    {
                        "name": "conv7/kernel",
                        "parameters": 640,
                        "size": 20480,
                        "quant_size": 640,
                        "children": [],
                        "is_quant_kernel": true
                    }
                ]
            }
        ]
    }
}

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature / Optimization (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

@blueoil-butler blueoil-butler bot added the CI: auto-run Run CI automatically label Dec 18, 2019
@bo-code-review-bot
Copy link

This PR needs Approvals as follows.

  • Ownership Approval for / from iizukak, tkng, ruimashita
  • Readability Approval for Python from tkng, tsawada

Please choose reviewers and requet reviews!

Click to see how to approve each reviews

You can approve this PR by triggered comments as follows.

  • Approve all reviews requested to you (readability and ownership) and LGTM review
    Approval, LGTM

  • Approve all ownership reviews
    Ownership Approval or OA

  • Approve all readability reviews
    Readability Approval or RA

  • Approve specified review targets

    • Example of Ownership Reviewer of /: Ownership Approval for / or OA for /
    • Example of Readability Reviewer of Python: Readability Approval for Python or RA for Python
  • Approve LGTM review
    LGTM

See all trigger comments

Please replace [Target] to review target

  • Ownership Approval
    • Ownership Approval for [Target]
    • OA for [Target]
    • Ownership Approval
    • OA
    • Approval
  • Readability Approval
    • Readability Approval for [Target]
    • RA for [Target]
    • [Target] Readability Approval
    • [Target] RA
    • Readability Approval
    • RA
    • Approval
  • LGTM
    • LGTM
    • lgtm

@hadusam hadusam requested review from iizukak and suttang December 18, 2019 17:38
@hadusam hadusam self-assigned this Dec 18, 2019
Copy link
Member

@iizukak iizukak left a comment

Choose a reason for hiding this comment

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

I'm happy to add feature for the benchmark.
Few comments added.


helper(prof, 0)
if node_name == "total":
node_param_dict["total_quant_size"] = res[idx][-1]
Copy link
Member

Choose a reason for hiding this comment

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

res[idx][-1] appear several times.
It's better to add name as normal variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iizukak
Thanks! Fixed 👍

'name': node_name,
'parameters': node.total_parameters,
'size': node.total_parameters * 32,
'quant_size': node.total_parameters * bits,
Copy link
Member

Choose a reason for hiding this comment

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

Just question. Conv layer can be quantized and parameter size is bits.
Other layer type (for example, fully connected) will not be quantized.
This size calculation assume all of our parameters are Conv?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iizukak Thank you for your question!
No, if a layer is not a quantizable kernel, quant_size will be a same value of size.
quant_size is determined as following steps.

  1. check node_name is kernel or not, see is_quant_kernel
  2. set bits = bit if is_quant_kernel else 32
  3. calc 'quant_size': node.total_parameters * bits,

This is the previous implementation, but it is difficult to understand as you mentioned.

Ummm. OK, I am planing to change these logics as follows.

node_params = node.total_parameters
node_size = node_params * 32
node_quant_size = (node_params * bit) if is_quant_kernel else None

This plan is to change quant_size is None if it cannot be quantized.
What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This plan is to change quant_size is None if it cannot be quantized.

I tried and implement that, but it is a little complicated to use None and int in the same dictionary key's value and this implementation changes the current Markdown file format. So, I would not like to use None to follow the current format. Then, node_quant_size will be (node_params * bit) if is_quant_kernel else node_size

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@iizukak
I changed to use named values for node parameters so this becomes better to understand, I think. Please re-review this! 👍

Copy link
Contributor

@suttang suttang left a comment

Choose a reason for hiding this comment

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

LGTM

@hadusam hadusam requested a review from iizukak December 23, 2019 02:48
Copy link
Member

@iizukak iizukak left a comment

Choose a reason for hiding this comment

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

Ownership Approval

@hadusam hadusam requested a review from tsawada December 23, 2019 04:58
@hadusam
Copy link
Contributor Author

hadusam commented Dec 23, 2019

@tsawada
Please review this PR for python readability.

Copy link
Contributor

@tsawada tsawada left a comment

Choose a reason for hiding this comment

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

I don't fully understand but it seems this PR does multiple things that's not explained in the commit message, such as
a. changing the default --bit option to 1
b. changing print to logger

Are these changes necessary for JSON? Especially, I feel (a.) needs some wide announcement.

return new_res
node_flops_dict = {
'total_flops': float_res_dict["total"],
'children': [{"name": k, "flops": float_res_dict[k]} for k in float_res_dict.keys() if k != "total"]
Copy link
Contributor

Choose a reason for hiding this comment

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

[{"name": k, "flops": v for k, v in dict.items()] ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed 👍

'children': node_children,
}
# Add is_quant_kernel flag to leaf node
if len(node.children) == 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

if not node.children:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed 👍

@hadusam
Copy link
Contributor Author

hadusam commented Dec 24, 2019

@tsawada
Thanks for reviewing this!

OK, I will revert them and separate it 😄
a. changing the default --bit option to 1
b. changing print to logger

@hadusam
Copy link
Contributor Author

hadusam commented Dec 24, 2019

@tsawada
I reverted stashed changes as you mentioned and updated codes following your comments.
Please review this again! 🙇

Copy link
Contributor

@tsawada tsawada left a comment

Choose a reason for hiding this comment

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

Readability Approval

@iizukak
Copy link
Member

iizukak commented Dec 24, 2019

/ready

@bo-mergebot
Copy link
Contributor

bo-mergebot bot commented Dec 24, 2019

⏳Merge job is queued...

@bo-mergebot
Copy link
Contributor

bo-mergebot bot commented Dec 24, 2019

😥Status check failed. Please fix problems and send /ready again.

@hadusam
Copy link
Contributor Author

hadusam commented Dec 24, 2019

/ready

@bo-mergebot
Copy link
Contributor

bo-mergebot bot commented Dec 24, 2019

⏳Merge job is queued...

@bo-mergebot
Copy link
Contributor

bo-mergebot bot commented Dec 24, 2019

😥Status check failed. Please fix problems and send /ready again.

@hadusam
Copy link
Contributor Author

hadusam commented Dec 24, 2019

/ready

@bo-mergebot
Copy link
Contributor

bo-mergebot bot commented Dec 24, 2019

⏳Merge job is queued...

@bo-mergebot bo-mergebot bot merged commit a2e2d41 into blue-oil:master Dec 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CI: auto-run Run CI automatically
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants