├── .gitignore ├── LICENSE ├── example_bert.py ├── example_resnet18.py ├── Profile.py ├── profiler.py └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Rui Pan 潘瑞 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /example_bert.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import torch 3 | from transformers import BertForSequenceClassification, BertTokenizer 4 | 5 | from torchinfo import summary # torchinfo 6 | from deepspeed.profiling.flops_profiler import get_model_profile # deepspeed flops profiler 7 | from profiler import TIDSProfiler # our own profiler 8 | 9 | 10 | def bert_input_constructor(batch_size, seq_len, tokenizer): 11 | fake_seq = "" 12 | for _ in range(seq_len - 2): # ignore the two special tokens [CLS] and [SEP] 13 | fake_seq += tokenizer.pad_token 14 | inputs = tokenizer([fake_seq] * batch_size, 15 | padding=True, 16 | truncation=True, 17 | return_tensors="pt") 18 | labels = torch.tensor([1] * batch_size) 19 | inputs = dict(inputs) 20 | inputs.update({"labels": labels}) 21 | # inputs: dict with keys "input_ids", "token_type_ids", "attention_mask", "labels" 22 | return inputs 23 | 24 | 25 | def profile(args): 26 | with torch.cuda.device(0): 27 | tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') 28 | model = BertForSequenceClassification.from_pretrained('bert-base-uncased') 29 | batch_size = 2 30 | seq_len = 512 31 | if args.profiler == "torchinfo": 32 | # copied from https://stackoverflow.com/a/68577755/9601555 33 | summary(model, input_size=(batch_size, seq_len), dtypes=['torch.cuda.IntTensor']) 34 | elif args.profiler == "deepspeed": 35 | inputs = bert_input_constructor(batch_size, seq_len, tokenizer) 36 | flops, macs, params = get_model_profile( 37 | model, 38 | kwargs=inputs, 39 | print_profile=True, 40 | detailed=True, 41 | module_depth=-1, 42 | warm_up=10 43 | ) 44 | elif args.profiler == "tids": 45 | inputs = bert_input_constructor(batch_size, seq_len, tokenizer) 46 | prof = TIDSProfiler(model) 47 | prof.start_profile() 48 | model(**inputs) 49 | profile = prof.generate_profile() 50 | print(profile) 51 | prof.end_profile() 52 | 53 | 54 | def main(): 55 | parser = argparse.ArgumentParser() 56 | 57 | parser.add_argument( 58 | "--profiler", 59 | type=str, 60 | default="tids", 61 | choices=["tids", "torchinfo", "deepspeed"] 62 | ) 63 | 64 | args = parser.parse_args() 65 | profile(args) 66 | 67 | 68 | if __name__ == "__main__": 69 | main() -------------------------------------------------------------------------------- /example_resnet18.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import torch 3 | import torchvision.models as models 4 | 5 | from torchinfo import summary # torchinfo 6 | from deepspeed.profiling.flops_profiler import get_model_profile # deepspeed flops profiler 7 | from profiler import TIDSProfiler # our own profiler 8 | 9 | 10 | def profile(args): 11 | with torch.cuda.device(0): 12 | model = models.resnet18() 13 | batch_size = 16 14 | input_size = (batch_size, 3, 224, 224) 15 | 16 | if args.profiler == "torchinfo": 17 | summary(model, input_size=input_size) 18 | elif args.profiler == "deepspeed": 19 | flops, macs, params = get_model_profile(model=model, # model 20 | input_shape=input_size, # input shape to the model. If specified, the model takes a tensor with this shape as the only positional argument. 21 | args=None, # list of positional arguments to the model. 22 | kwargs=None, # dictionary of keyword arguments to the model. 23 | print_profile=True, # prints the model graph with the measured profile attached to each module 24 | detailed=True, # print the detailed profile 25 | module_depth=-1, # depth into the nested modules, with -1 being the inner most modules 26 | top_modules=1, # the number of top modules to print aggregated profile 27 | warm_up=10, # the number of warm-ups before measuring the time of each module 28 | as_string=True, # print raw numbers (e.g. 1000) or as human-readable strings (e.g. 1k) 29 | output_file=None, # path to the output file. If None, the profiler prints to stdout. 30 | ignore_modules=None) # the list of modules to ignore in the profiling 31 | elif args.profiler == "tids": 32 | inputs = torch.randn(input_size) 33 | prof = TIDSProfiler(model) 34 | prof.start_profile() 35 | model(inputs) 36 | profile = prof.generate_profile() 37 | print(profile) 38 | prof.end_profile() 39 | 40 | 41 | def main(): 42 | parser = argparse.ArgumentParser() 43 | 44 | parser.add_argument( 45 | "--profiler", 46 | type=str, 47 | default="tids", 48 | choices=["tids", "torchinfo", "deepspeed"] 49 | ) 50 | 51 | args = parser.parse_args() 52 | profile(args) 53 | 54 | 55 | if __name__ == "__main__": 56 | main() -------------------------------------------------------------------------------- /Profile.py: -------------------------------------------------------------------------------- 1 | 2 | class Profile(object): 3 | def __init__( 4 | self, 5 | name: str, 6 | type: str, 7 | depth: int, 8 | num_params: int, 9 | input_shape: list, 10 | output_shape: list, 11 | input_elem_bytes: int, 12 | output_elem_bytes: int, 13 | fwd_latency: float, 14 | macs: float, 15 | fwd_flops: float, 16 | ): # each Profile corresponds to the profile of a torch.nn module 17 | self.name = name # torch.nn module name 18 | self.type = type # torch.nn module type 19 | self.depth = depth # depth of current module in model 20 | self.num_params = num_params # number of parameters 21 | self.num_params_pctg = None # percentage of total params 22 | # shape of input/output tensor 23 | self.input_shape = input_shape 24 | self.output_shape = output_shape 25 | # size in bytes of an individual element in the input/output tensor 26 | self.input_elem_bytes = input_elem_bytes 27 | self.output_elem_bytes = output_elem_bytes 28 | self.fwd_latency = fwd_latency # fwd latency (forward propagation latency) in ms 29 | self.fwd_latency_pctg = None # percentage of total fwd latency 30 | # NOTE(ruipan): the following aren't matching with the 31 | # original deepspeed profiler's output... fix later if needed 32 | self.macs = macs # number of multiply-accumulate operations (MACs) 33 | self.macs_pctg = None # percentage of total MACs 34 | # number of floating-point operations (flops) OR floating-point operations per second (FLOPS)?? 35 | self.fwd_flops = fwd_flops 36 | self.fwd_flops_pctg = None 37 | self.children = [] 38 | 39 | def set_child_modules(self, children) -> None: 40 | """Sets up the child modules, and fills in 41 | the overall percentage statistics 42 | 43 | Args: 44 | children (Profile): profile of child of module 45 | of which the current profile is from 46 | """ 47 | self.children = children 48 | if self.name == "model": # outermost model 49 | total_duration = self.fwd_latency 50 | self.calculate_overall_stats(total_duration=total_duration) 51 | 52 | def __str__(self) -> str: 53 | # indent = "├─" * self.depth 54 | indent = "\t" * self.depth 55 | curr_str = (f"{indent}({self.name}): {self.type}, num_params {self.num_params}, " \ 56 | f"{round(self.fwd_latency, 3)}ms, {round(self.fwd_latency_pctg, 3)}% latency, " \ 57 | f"input shape {self.input_shape}, output shape {self.output_shape}\n") 58 | for child in self.children: 59 | curr_str += str(child) 60 | return curr_str 61 | 62 | def calculate_overall_stats(self, total_duration: float) -> None: 63 | """Recursively fills in the overall percentage statistics 64 | 65 | Args: 66 | total_duration (float): total duration of one fwd 67 | pass of the model in ms 68 | """ 69 | if self.type == "ModuleList": # latency is 0, aggregate latencies from children first 70 | self.fwd_latency = sum([c.fwd_latency for c in self.children]) 71 | 72 | self.fwd_latency_pctg = 100 * self.fwd_latency / total_duration 73 | for child in self.children: 74 | child.calculate_overall_stats(total_duration=total_duration) 75 | -------------------------------------------------------------------------------- /profiler.py: -------------------------------------------------------------------------------- 1 | # modified from https://github.com/microsoft/DeepSpeed/blob/master/deepspeed/profiling/flops_profiler/profiler.py 2 | import time 3 | import torch 4 | import torch.nn as nn 5 | import torch.nn.functional as F 6 | from functools import partial 7 | from typing import Any, Dict, Iterable, List, Optional, Sequence, Union 8 | from collections import OrderedDict 9 | import numpy as np 10 | from deepspeed.accelerator import get_accelerator 11 | 12 | from Profile import Profile 13 | 14 | Tensor = torch.Tensor 15 | 16 | module_flop_count = [] 17 | module_mac_count = [] 18 | old_functions = {} 19 | 20 | # NOTE(ruipan): copied from https://github.com/TylerYep/torchinfo/blob/main/torchinfo/layer_info.py 21 | DETECTED_INPUT_OUTPUT_TYPES = Union[ 22 | Sequence[Any], Dict[Any, torch.Tensor], torch.Tensor 23 | ] 24 | 25 | 26 | def calculate_size( 27 | # inputs: DETECTED_INPUT_OUTPUT_TYPES, batch_dim: int | None # "|" is python 3.10+ only 28 | inputs: DETECTED_INPUT_OUTPUT_TYPES, batch_dim: int = None 29 | ) -> tuple[list[int], int]: 30 | """ 31 | Set input_size or output_size using the model's inputs. 32 | Returns the corrected shape of `inputs` and the size of 33 | a single element in bytes. 34 | NOTE(ruipan): modified from https://github.com/TylerYep/torchinfo/blob/main/torchinfo/layer_info.py#L88 35 | """ 36 | if inputs is None: 37 | size, elem_bytes = [], 0 38 | 39 | # pack_padded_seq and pad_packed_seq store feature into data attribute 40 | elif ( 41 | isinstance(inputs, (list, tuple)) and inputs and hasattr(inputs[0], "data") 42 | ): 43 | size = list(inputs[0].data.size()) 44 | elem_bytes = inputs[0].data.element_size() 45 | if batch_dim is not None: 46 | size = size[:batch_dim] + [1] + size[batch_dim + 1 :] 47 | 48 | elif isinstance(inputs, dict): 49 | output = list(inputs.values())[-1] 50 | size, elem_bytes = nested_list_size(output) 51 | if batch_dim is not None: 52 | size = [size[:batch_dim] + [1] + size[batch_dim + 1 :]] 53 | 54 | elif isinstance(inputs, torch.Tensor): 55 | size = list(inputs.size()) 56 | elem_bytes = inputs.element_size() 57 | 58 | elif isinstance(inputs, (list, tuple)): 59 | size, elem_bytes = nested_list_size(inputs) 60 | if batch_dim is not None and batch_dim < len(size): 61 | size[batch_dim] = 1 62 | 63 | else: 64 | raise TypeError( 65 | "Model contains a layer with an unsupported input or output type: " 66 | f"{inputs}, type: {type(inputs)}" 67 | ) 68 | 69 | return size, elem_bytes 70 | 71 | 72 | # def nested_list_size(inputs: Sequence[Any] | torch.Tensor) -> tuple[list[int], int]: # "|" is python 3.10+ only 73 | def nested_list_size(inputs) -> tuple[list[int], int]: 74 | """ 75 | Flattens nested list size. 76 | NOTE(ruipan): copied from https://github.com/TylerYep/torchinfo/blob/main/torchinfo/layer_info.py#L312 77 | """ 78 | if hasattr(inputs, "tensors"): 79 | size, elem_bytes = nested_list_size(inputs.tensors) 80 | elif isinstance(inputs, torch.Tensor): 81 | size, elem_bytes = list(inputs.size()), inputs.element_size() 82 | elif not hasattr(inputs, "__getitem__") or not inputs: 83 | size, elem_bytes = [], 0 84 | elif isinstance(inputs, dict): 85 | size, elem_bytes = nested_list_size(list(inputs.values())) 86 | elif ( 87 | hasattr(inputs, "size") 88 | and callable(inputs.size) 89 | and hasattr(inputs, "element_size") 90 | and callable(inputs.element_size) 91 | ): 92 | size, elem_bytes = list(inputs.size()), inputs.element_size() 93 | elif isinstance(inputs, (list, tuple)): 94 | size, elem_bytes = nested_list_size(inputs[0]) 95 | else: 96 | size, elem_bytes = [], 0 97 | 98 | return size, elem_bytes 99 | 100 | 101 | class TIDSProfiler(object): 102 | """Measures the latency, number of estimated floating-point operations and parameters of each module in a PyTorch model. 103 | 104 | The flops-profiler profiles the forward pass of a PyTorch model and prints the model graph with the measured profile attached to each module. It shows how latency, flops and parameters are spent in the model and which modules or layers could be the bottleneck. It also outputs the names of the top k modules in terms of aggregated latency, flops, and parameters at depth l with k and l specified by the user. The output profile is computed for each batch of input. 105 | The DeepSpeed flops profiler can be used with the DeepSpeed runtime or as a standalone package. 106 | When using DeepSpeed for model training, the flops profiler can be configured in the deepspeed_config file and no user code change is required. 107 | 108 | If using the profiler as a standalone package, one imports the flops_profiler package and use the APIs. 109 | 110 | Here is an example for usage in a typical training workflow: 111 | 112 | .. code-block:: python 113 | 114 | model = Model() 115 | prof = TIDSProfiler(model) 116 | 117 | for step, batch in enumerate(data_loader): 118 | if step == profile_step: 119 | prof.start_profile() 120 | 121 | loss = model(batch) 122 | 123 | if step == profile_step: 124 | flops = prof.get_total_flops(as_string=True) 125 | params = prof.get_total_params(as_string=True) 126 | prof.print_model_profile(profile_step=profile_step) 127 | prof.end_profile() 128 | 129 | loss.backward() 130 | optimizer.step() 131 | 132 | To profile a trained model in inference, use the `get_model_profile` API. 133 | 134 | Args: 135 | object (torch.nn.Module): The PyTorch model to profile. 136 | """ 137 | def __init__(self, model, ds_engine=None): 138 | self.model = model 139 | self.ds_engine = ds_engine 140 | self.started = False 141 | self.func_patched = False 142 | 143 | def generate_profile(self, module=None, name="model", curr_depth=0): 144 | """Generates profiling information of a model 145 | 146 | Args: 147 | module (torch.mm.Module, optional): Module to be profiled. 148 | Defaults to None. 149 | name (str, optional): Name of the module. Defaults to "model". 150 | curr_depth (int, optional): Current depth in the model. Note 151 | that this depth is not horizontal depth. Defaults to 0. 152 | 153 | Returns: 154 | Profile: profiling result 155 | """ 156 | if module is None: 157 | module = self.model 158 | 159 | if not hasattr(module, "__input_shape__"): 160 | # post_hook is not triggered for ModuleList, so these 161 | # module attributes are never set 162 | input_shape, output_shape = None, None 163 | input_elem_bytes, output_elem_bytes = None, None 164 | else: 165 | input_shape, output_shape = module.__input_shape__, module.__output_shape__ 166 | input_elem_bytes, output_elem_bytes = module.__input_elem_bytes__, module.__output_elem_bytes__ 167 | 168 | profile = Profile( 169 | name=name, 170 | type=module.__class__.__name__, 171 | depth=curr_depth, 172 | num_params=module.__params__, 173 | input_shape=input_shape, 174 | output_shape=output_shape, 175 | input_elem_bytes=input_elem_bytes, 176 | output_elem_bytes=output_elem_bytes, 177 | fwd_latency=module.__duration__*1000, # s -> ms 178 | macs=module.__macs__, 179 | fwd_flops=module.__flops__, 180 | ) 181 | 182 | child_profiles = [] 183 | for child_name, child_module in module.named_children(): 184 | # NOTE(ruipan): module.(named_){modules,children} returns {all modules,immediate child modules} 185 | child_profile = self.generate_profile( 186 | module=child_module, name=child_name, curr_depth=curr_depth+1 187 | ) 188 | child_profiles.append(child_profile) 189 | profile.set_child_modules(child_profiles) 190 | return profile 191 | 192 | def start_profile(self, ignore_list=None): 193 | """Starts profiling. 194 | 195 | Extra attributes are added recursively to all the modules and the profiled torch.nn.functionals are monkey patched. 196 | 197 | Args: 198 | ignore_list (list, optional): the list of modules to ignore while profiling. Defaults to None. 199 | """ 200 | self.reset_profile() 201 | _patch_functionals() 202 | _patch_tensor_methods() 203 | 204 | def register_module_hooks(module, ignore_list): 205 | if ignore_list and type(module) in ignore_list: 206 | return 207 | 208 | # if computing the flops of a module directly 209 | if type(module) in MODULE_HOOK_MAPPING: 210 | if not hasattr(module, "__flops_handle__"): 211 | module.__flops_handle__ = module.register_forward_hook( 212 | MODULE_HOOK_MAPPING[type(module)]) 213 | return 214 | 215 | # if computing the flops of the functionals in a module 216 | def pre_hook(module, input): 217 | module_flop_count.append([]) 218 | module_mac_count.append([]) 219 | 220 | if not hasattr(module, "__pre_hook_handle__"): 221 | module.__pre_hook_handle__ = module.register_forward_pre_hook(pre_hook) 222 | 223 | def post_hook(module, input, output): 224 | if module_flop_count: 225 | module.__flops__ += sum([elem[1] for elem in module_flop_count[-1]]) 226 | module_flop_count.pop() 227 | module.__macs__ += sum([elem[1] for elem in module_mac_count[-1]]) 228 | module_mac_count.pop() 229 | 230 | if not hasattr(module, "__input_shape__"): 231 | size, elem_bytes = calculate_size(input) 232 | module.__input_shape__ = size 233 | module.__input_elem_bytes__ = elem_bytes 234 | 235 | if not hasattr(module, "__output_shape__"): 236 | size, elem_bytes = calculate_size(output) 237 | module.__output_shape__ = size 238 | module.__output_elem_bytes__ = elem_bytes 239 | 240 | if not hasattr(module, "__post_hook_handle__"): 241 | module.__post_hook_handle__ = module.register_forward_hook(post_hook) 242 | 243 | def start_time_hook(module, input): 244 | get_accelerator().synchronize() 245 | module.__start_time__ = time.time() 246 | 247 | if not hasattr(module, "__start_time_hook_handle"): 248 | module.__start_time_hook_handle__ = module.register_forward_pre_hook( 249 | start_time_hook) 250 | 251 | def end_time_hook(module, input, output): 252 | get_accelerator().synchronize() 253 | module.__duration__ += time.time() - module.__start_time__ 254 | 255 | if not hasattr(module, "__end_time_hook_handle__"): 256 | module.__end_time_hook_handle__ = module.register_forward_hook( 257 | end_time_hook) 258 | 259 | self.model.apply(partial(register_module_hooks, ignore_list=ignore_list)) 260 | self.started = True 261 | self.func_patched = True 262 | 263 | def stop_profile(self): 264 | """Stop profiling. 265 | 266 | All torch.nn.functionals are restored to their originals. 267 | """ 268 | if self.started and self.func_patched: 269 | _reload_functionals() 270 | _reload_tensor_methods() 271 | self.func_patched = False 272 | 273 | def remove_profile_attrs(module): 274 | if hasattr(module, "__pre_hook_handle__"): 275 | module.__pre_hook_handle__.remove() 276 | del module.__pre_hook_handle__ 277 | if hasattr(module, "__post_hook_handle__"): 278 | module.__post_hook_handle__.remove() 279 | del module.__post_hook_handle__ 280 | if hasattr(module, "__flops_handle__"): 281 | module.__flops_handle__.remove() 282 | del module.__flops_handle__ 283 | if hasattr(module, "__start_time_hook_handle__"): 284 | module.__start_time_hook_handle__.remove() 285 | del module.__start_time_hook_handle__ 286 | if hasattr(module, "__end_time_hook_handle__"): 287 | module.__end_time_hook_handle__.remove() 288 | del module.__end_time_hook_handle__ 289 | 290 | self.model.apply(remove_profile_attrs) 291 | 292 | def reset_profile(self): 293 | """Resets the profiling. 294 | 295 | Adds or resets the extra attributes. 296 | """ 297 | def add_or_reset_attrs(module): 298 | module.__flops__ = 0 299 | module.__macs__ = 0 300 | module.__params__ = sum(p.numel() for p in module.parameters()) 301 | module.__start_time__ = 0 302 | module.__duration__ = 0 303 | 304 | self.model.apply(add_or_reset_attrs) 305 | 306 | def end_profile(self): 307 | """Ends profiling. 308 | 309 | The added attributes and handles are removed recursively on all the modules. 310 | """ 311 | if not self.started: 312 | return 313 | self.stop_profile() 314 | self.started = False 315 | 316 | def remove_profile_attrs(module): 317 | if hasattr(module, "__flops__"): 318 | del module.__flops__ 319 | if hasattr(module, "__macs__"): 320 | del module.__macs__ 321 | if hasattr(module, "__params__"): 322 | del module.__params__ 323 | if hasattr(module, "__start_time__"): 324 | del module.__start_time__ 325 | if hasattr(module, "__duration__"): 326 | del module.__duration__ 327 | if hasattr(module, "__input_shape__"): 328 | del module.__input_shape__ 329 | if hasattr(module, "__output_shape__"): 330 | del module.__output_shape__ 331 | if hasattr(module, "__input_elem_bytes__"): 332 | del module.__input_elem_bytes__ 333 | if hasattr(module, "__output_elem_bytes__"): 334 | del module.__output_elem_bytes__ 335 | 336 | self.model.apply(remove_profile_attrs) 337 | 338 | def get_total_flops(self, as_string=False): 339 | """Returns the total flops of the model. 340 | 341 | Args: 342 | as_string (bool, optional): whether to output the flops as string. Defaults to False. 343 | 344 | Returns: 345 | The number of multiply-accumulate operations of the model forward pass. 346 | """ 347 | total_flops = get_module_flops(self.model) 348 | return num_to_string(total_flops) if as_string else total_flops 349 | 350 | def get_total_macs(self, as_string=False): 351 | """Returns the total MACs of the model. 352 | 353 | Args: 354 | as_string (bool, optional): whether to output the flops as string. Defaults to False. 355 | 356 | Returns: 357 | The number of multiply-accumulate operations of the model forward pass. 358 | """ 359 | total_macs = get_module_macs(self.model) 360 | return macs_to_string(total_macs) if as_string else total_macs 361 | 362 | def get_total_duration(self, as_string=False): 363 | """Returns the total duration of the model forward pass. 364 | 365 | Args: 366 | as_string (bool, optional): whether to output the duration as string. Defaults to False. 367 | 368 | Returns: 369 | The latency of the model forward pass. 370 | """ 371 | total_duration = get_module_duration(self.model) 372 | return duration_to_string(total_duration) if as_string else total_duration 373 | 374 | def get_total_params(self, as_string=False): 375 | """Returns the total parameters of the model. 376 | 377 | Args: 378 | as_string (bool, optional): whether to output the parameters as string. Defaults to False. 379 | 380 | Returns: 381 | The number of parameters in the model. 382 | """ 383 | return params_to_string( 384 | self.model.__params__) if as_string else self.model.__params__ 385 | 386 | def print_model_profile(self, 387 | profile_step=1, 388 | module_depth=-1, 389 | top_modules=1, 390 | detailed=True, 391 | output_file=None): 392 | """Prints the model graph with the measured profile attached to each module. 393 | 394 | Args: 395 | profile_step (int, optional): The global training step at which to profile. Note that warm up steps are needed for accurate time measurement. 396 | module_depth (int, optional): The depth of the model to which to print the aggregated module information. When set to -1, it prints information from the top to the innermost modules (the maximum depth). 397 | top_modules (int, optional): Limits the aggregated profile output to the number of top modules specified. 398 | detailed (bool, optional): Whether to print the detailed model profile. 399 | output_file (str, optional): Path to the output file. If None, the profiler prints to stdout. 400 | """ 401 | if not self.started: 402 | return 403 | import sys 404 | import os.path 405 | original_stdout = None 406 | f = None 407 | if output_file and output_file != "": 408 | dir_path = os.path.dirname(os.path.abspath(output_file)) 409 | if not os.path.exists(dir_path): 410 | os.makedirs(dir_path) 411 | original_stdout = sys.stdout 412 | f = open(output_file, "w") 413 | sys.stdout = f 414 | 415 | total_flops = self.get_total_flops() 416 | total_macs = self.get_total_macs() 417 | total_duration = self.get_total_duration() 418 | total_params = self.get_total_params() 419 | 420 | self.flops = total_flops 421 | self.macs = total_macs 422 | self.params = total_params 423 | 424 | print( 425 | "\n-------------------------- DeepSpeed Flops TIDSProfiler --------------------------" 426 | ) 427 | print(f'Profile Summary at step {profile_step}:') 428 | print( 429 | "Notations:\ndata parallel size (dp_size), model parallel size(mp_size),\nnumber of parameters (params), number of multiply-accumulate operations(MACs),\nnumber of floating-point operations (flops), floating-point operations per second (FLOPS),\nfwd latency (forward propagation latency), bwd latency (backward propagation latency),\nstep (weights update latency), iter latency (sum of fwd, bwd and step latency)\n" 430 | ) 431 | if self.ds_engine: 432 | print('{:<60} {:<8}'.format('world size: ', self.ds_engine.world_size)) 433 | print('{:<60} {:<8}'.format('data parallel size: ', 434 | self.ds_engine.dp_world_size)) 435 | print('{:<60} {:<8}'.format('model parallel size: ', 436 | self.ds_engine.mp_world_size)) 437 | print('{:<60} {:<8}'.format( 438 | 'batch size per GPU: ', 439 | self.ds_engine.train_micro_batch_size_per_gpu())) 440 | 441 | print('{:<60} {:<8}'.format('params per gpu: ', params_to_string(total_params))) 442 | print('{:<60} {:<8}'.format( 443 | 'params of model = params per GPU * mp_size: ', 444 | params_to_string(total_params * 445 | ((self.ds_engine.mp_world_size) if self.ds_engine else 1)))) 446 | 447 | print('{:<60} {:<8}'.format('fwd MACs per GPU: ', macs_to_string(total_macs))) 448 | 449 | print('{:<60} {:<8}'.format('fwd flops per GPU: ', num_to_string(total_flops))) 450 | 451 | print('{:<60} {:<8}'.format( 452 | 'fwd flops of model = fwd flops per GPU * mp_size: ', 453 | num_to_string(total_flops * 454 | ((self.ds_engine.mp_world_size) if self.ds_engine else 1)))) 455 | 456 | fwd_latency = self.get_total_duration() 457 | if self.ds_engine and self.ds_engine.wall_clock_breakdown(): 458 | fwd_latency = self.ds_engine.timers('forward').elapsed(False) / 1000.0 459 | print('{:<60} {:<8}'.format('fwd latency: ', duration_to_string(fwd_latency))) 460 | print('{:<60} {:<8}'.format( 461 | 'fwd FLOPS per GPU = fwd flops per GPU / fwd latency: ', 462 | flops_to_string(total_flops / fwd_latency))) 463 | 464 | if self.ds_engine and self.ds_engine.wall_clock_breakdown(): 465 | bwd_latency = self.ds_engine.timers('backward').elapsed(False) / 1000.0 466 | step_latency = self.ds_engine.timers('step').elapsed(False) / 1000.0 467 | print('{:<60} {:<8}'.format('bwd latency: ', 468 | duration_to_string(bwd_latency))) 469 | print('{:<60} {:<8}'.format( 470 | 'bwd FLOPS per GPU = 2 * fwd flops per GPU / bwd latency: ', 471 | flops_to_string(2 * total_flops / bwd_latency))) 472 | print('{:<60} {:<8}'.format( 473 | 'fwd+bwd FLOPS per GPU = 3 * fwd flops per GPU / (fwd+bwd latency): ', 474 | flops_to_string(3 * total_flops / (fwd_latency + bwd_latency)))) 475 | 476 | print('{:<60} {:<8}'.format('step latency: ', 477 | duration_to_string(step_latency))) 478 | 479 | iter_latency = fwd_latency + bwd_latency + step_latency 480 | print('{:<60} {:<8}'.format('iter latency: ', 481 | duration_to_string(iter_latency))) 482 | print('{:<60} {:<8}'.format( 483 | 'FLOPS per GPU = 3 * fwd flops per GPU / iter latency: ', 484 | flops_to_string(3 * total_flops / iter_latency))) 485 | 486 | samples_per_iter = self.ds_engine.train_micro_batch_size_per_gpu( 487 | ) * self.ds_engine.world_size 488 | print('{:<60} {:<8.2f}'.format('samples/second: ', 489 | samples_per_iter / iter_latency)) 490 | 491 | def flops_repr(module): 492 | params = module.__params__ 493 | flops = get_module_flops(module) 494 | macs = get_module_macs(module) 495 | items = [ 496 | params_to_string(params), 497 | "{:.2%} Params".format(params / total_params if total_params else 0), 498 | macs_to_string(macs), 499 | "{:.2%} MACs".format(0.0 if total_macs == 0 else macs / total_macs), 500 | ] 501 | duration = get_module_duration(module) 502 | 503 | items.append(duration_to_string(duration)) 504 | items.append( 505 | "{:.2%} latency".format(0.0 if total_duration == 0 else duration / 506 | total_duration)) 507 | items.append(flops_to_string(0.0 if duration == 0 else flops / duration)) 508 | items.append(module.original_extra_repr()) 509 | return ", ".join(items) 510 | 511 | def add_extra_repr(module): 512 | flops_extra_repr = flops_repr.__get__(module) 513 | if module.extra_repr != flops_extra_repr: 514 | module.original_extra_repr = module.extra_repr 515 | module.extra_repr = flops_extra_repr 516 | assert module.extra_repr != module.original_extra_repr 517 | 518 | def del_extra_repr(module): 519 | if hasattr(module, "original_extra_repr"): 520 | module.extra_repr = module.original_extra_repr 521 | del module.original_extra_repr 522 | 523 | self.model.apply(add_extra_repr) 524 | 525 | print( 526 | "\n----------------------------- Aggregated Profile per GPU -----------------------------" 527 | ) 528 | self.print_model_aggregated_profile(module_depth=module_depth, 529 | top_modules=top_modules) 530 | 531 | if detailed: 532 | print( 533 | "\n------------------------------ Detailed Profile per GPU ------------------------------" 534 | ) 535 | print( 536 | "Each module profile is listed after its name in the following order: \nparams, percentage of total params, MACs, percentage of total MACs, fwd latency, percentage of total fwd latency, fwd FLOPS" 537 | ) 538 | print( 539 | "\nNote: 1. A module can have torch.nn.module or torch.nn.functional to compute logits (e.g. CrossEntropyLoss). They are not counted as submodules, thus not to be printed out. However they make up the difference between a parent's MACs (or latency) and the sum of its submodules'.\n2. Number of floating-point operations is a theoretical estimation, thus FLOPS computed using that could be larger than the maximum system throughput.\n3. The fwd latency listed in the top module's profile is directly captured at the module forward function in PyTorch, thus it's less than the fwd latency shown above which is captured in DeepSpeed.\n" 540 | ) 541 | print(self.model) 542 | 543 | self.model.apply(del_extra_repr) 544 | 545 | print( 546 | "------------------------------------------------------------------------------" 547 | ) 548 | 549 | if output_file: 550 | sys.stdout = original_stdout 551 | f.close() 552 | 553 | def print_model_aggregated_profile(self, module_depth=-1, top_modules=1): 554 | """Prints the names of the top top_modules modules in terms of aggregated time, flops, and parameters at depth module_depth. 555 | 556 | Args: 557 | module_depth (int, optional): the depth of the modules to show. Defaults to -1 (the innermost modules). 558 | top_modules (int, optional): the number of top modules to show. Defaults to 1. 559 | """ 560 | info = {} 561 | if not hasattr(self.model, "__flops__"): 562 | print( 563 | "no __flops__ attribute in the model, call this function after start_profile and before end_profile" 564 | ) 565 | return 566 | 567 | def walk_module(module, curr_depth, info): 568 | if curr_depth not in info: 569 | info[curr_depth] = {} 570 | if module.__class__.__name__ not in info[curr_depth]: 571 | info[curr_depth][module.__class__.__name__] = [ 572 | 0, 573 | 0, 574 | 0, 575 | ] # macs, params, time 576 | info[curr_depth][module.__class__.__name__][0] += get_module_macs(module) 577 | info[curr_depth][module.__class__.__name__][1] += module.__params__ 578 | info[curr_depth][module.__class__.__name__][2] += get_module_duration(module) 579 | has_children = len(module._modules.items()) != 0 580 | if has_children: 581 | for child in module.children(): 582 | walk_module(child, curr_depth + 1, info) 583 | 584 | walk_module(self.model, 0, info) 585 | 586 | depth = module_depth 587 | if module_depth == -1: 588 | depth = len(info) - 1 589 | 590 | print( 591 | f'Top {top_modules} modules in terms of params, MACs or fwd latency at different model depths:' 592 | ) 593 | 594 | for d in range(depth): 595 | num_items = min(top_modules, len(info[d])) 596 | 597 | sort_macs = { 598 | k: macs_to_string(v[0]) 599 | for k, 600 | v in sorted(info[d].items(), 601 | key=lambda item: item[1][0], 602 | reverse=True)[:num_items] 603 | } 604 | sort_params = { 605 | k: params_to_string(v[1]) 606 | for k, 607 | v in sorted(info[d].items(), 608 | key=lambda item: item[1][1], 609 | reverse=True)[:num_items] 610 | } 611 | sort_time = { 612 | k: duration_to_string(v[2]) 613 | for k, 614 | v in sorted(info[d].items(), 615 | key=lambda item: item[1][2], 616 | reverse=True)[:num_items] 617 | } 618 | 619 | print(f"depth {d}:") 620 | print(f" params - {sort_params}") 621 | print(f" MACs - {sort_macs}") 622 | print(f" fwd latency - {sort_time}") 623 | 624 | 625 | def _prod(dims): 626 | p = 1 627 | for v in dims: 628 | p *= v 629 | return p 630 | 631 | 632 | def _linear_flops_compute(input, weight, bias=None): 633 | out_features = weight.shape[0] 634 | macs = input.numel() * out_features 635 | return 2 * macs, macs 636 | 637 | 638 | def _relu_flops_compute(input, inplace=False): 639 | return input.numel(), 0 640 | 641 | 642 | def _prelu_flops_compute(input: Tensor, weight: Tensor): 643 | return input.numel(), 0 644 | 645 | 646 | def _elu_flops_compute(input: Tensor, alpha: float = 1.0, inplace: bool = False): 647 | return input.numel(), 0 648 | 649 | 650 | def _leaky_relu_flops_compute(input: Tensor, 651 | negative_slope: float = 0.01, 652 | inplace: bool = False): 653 | return input.numel(), 0 654 | 655 | 656 | def _relu6_flops_compute(input: Tensor, inplace: bool = False): 657 | return input.numel(), 0 658 | 659 | 660 | def _silu_flops_compute(input: Tensor, inplace: bool = False): 661 | return input.numel(), 0 662 | 663 | 664 | def _gelu_flops_compute(input, **kwargs): 665 | return input.numel(), 0 666 | 667 | 668 | def _pool_flops_compute(input, 669 | kernel_size, 670 | stride=None, 671 | padding=0, 672 | dilation=None, 673 | ceil_mode=False, 674 | count_include_pad=True, 675 | divisor_override=None, 676 | return_indices=None): 677 | return input.numel(), 0 678 | 679 | 680 | def _conv_flops_compute(input, 681 | weight, 682 | bias=None, 683 | stride=1, 684 | padding=0, 685 | dilation=1, 686 | groups=1): 687 | assert weight.shape[1] * groups == input.shape[1] 688 | 689 | batch_size = input.shape[0] 690 | in_channels = input.shape[1] 691 | out_channels = weight.shape[0] 692 | kernel_dims = list(weight.shape[2:]) 693 | input_dims = list(input.shape[2:]) 694 | 695 | length = len(input_dims) 696 | 697 | paddings = padding if type(padding) is tuple else (padding, ) * length 698 | strides = stride if type(stride) is tuple else (stride, ) * length 699 | dilations = dilation if type(dilation) is tuple else (dilation, ) * length 700 | 701 | output_dims = [] 702 | for idx, input_dim in enumerate(input_dims): 703 | output_dim = (input_dim + 2 * paddings[idx] - 704 | (dilations[idx] * (kernel_dims[idx] - 1) + 1)) // strides[idx] + 1 705 | output_dims.append(output_dim) 706 | 707 | filters_per_channel = out_channels // groups 708 | conv_per_position_macs = int(_prod(kernel_dims)) * in_channels * filters_per_channel 709 | active_elements_count = batch_size * int(_prod(output_dims)) 710 | overall_conv_macs = conv_per_position_macs * active_elements_count 711 | overall_conv_flops = 2 * overall_conv_macs 712 | 713 | bias_flops = 0 714 | if bias is not None: 715 | bias_flops = out_channels * active_elements_count 716 | 717 | return int(overall_conv_flops + bias_flops), int(overall_conv_macs) 718 | 719 | 720 | def _conv_trans_flops_compute( 721 | input, 722 | weight, 723 | bias=None, 724 | stride=1, 725 | padding=0, 726 | output_padding=0, 727 | groups=1, 728 | dilation=1, 729 | ): 730 | batch_size = input.shape[0] 731 | in_channels = input.shape[1] 732 | out_channels = weight.shape[0] 733 | kernel_dims = list(weight.shape[2:]) 734 | input_dims = list(input.shape[2:]) 735 | 736 | length = len(input_dims) 737 | 738 | paddings = padding if type(padding) is tuple else (padding, ) * length 739 | strides = stride if type(stride) is tuple else (stride, ) * length 740 | dilations = dilation if type(dilation) is tuple else (dilation, ) * length 741 | 742 | output_dims = [] 743 | for idx, input_dim in enumerate(input_dims): 744 | 745 | output_dim = (input_dim + 2 * paddings[idx] - 746 | (dilations[idx] * (kernel_dims[idx] - 1) + 1)) // strides[idx] + 1 747 | output_dims.append(output_dim) 748 | 749 | paddings = padding if type(padding) is tuple else (padding, padding) 750 | strides = stride if type(stride) is tuple else (stride, stride) 751 | dilations = dilation if type(dilation) is tuple else (dilation, dilation) 752 | 753 | filters_per_channel = out_channels // groups 754 | conv_per_position_macs = int(_prod(kernel_dims)) * in_channels * filters_per_channel 755 | active_elements_count = batch_size * int(_prod(input_dims)) 756 | overall_conv_macs = conv_per_position_macs * active_elements_count 757 | overall_conv_flops = 2 * overall_conv_macs 758 | 759 | bias_flops = 0 760 | if bias is not None: 761 | bias_flops = out_channels * batch_size * int(_prod(output_dims)) 762 | 763 | return int(overall_conv_flops + bias_flops), int(overall_conv_macs) 764 | 765 | 766 | def _batch_norm_flops_compute( 767 | input, 768 | running_mean, 769 | running_var, 770 | weight=None, 771 | bias=None, 772 | training=False, 773 | momentum=0.1, 774 | eps=1e-05, 775 | ): 776 | has_affine = weight is not None 777 | if training: 778 | # estimation 779 | return input.numel() * (5 if has_affine else 4), 0 780 | flops = input.numel() * (2 if has_affine else 1) 781 | return flops, 0 782 | 783 | 784 | def _layer_norm_flops_compute( 785 | input: Tensor, 786 | normalized_shape: List[int], 787 | weight: Optional[Tensor] = None, 788 | bias: Optional[Tensor] = None, 789 | eps: float = 1e-5, 790 | ): 791 | has_affine = weight is not None 792 | # estimation 793 | return input.numel() * (5 if has_affine else 4), 0 794 | 795 | 796 | def _group_norm_flops_compute(input: Tensor, 797 | num_groups: int, 798 | weight: Optional[Tensor] = None, 799 | bias: Optional[Tensor] = None, 800 | eps: float = 1e-5): 801 | has_affine = weight is not None 802 | # estimation 803 | return input.numel() * (5 if has_affine else 4), 0 804 | 805 | 806 | def _instance_norm_flops_compute( 807 | input: Tensor, 808 | running_mean: Optional[Tensor] = None, 809 | running_var: Optional[Tensor] = None, 810 | weight: Optional[Tensor] = None, 811 | bias: Optional[Tensor] = None, 812 | use_input_stats: bool = True, 813 | momentum: float = 0.1, 814 | eps: float = 1e-5, 815 | ): 816 | has_affine = weight is not None 817 | # estimation 818 | return input.numel() * (5 if has_affine else 4), 0 819 | 820 | 821 | def _upsample_flops_compute(input, **kwargs): 822 | size = kwargs.get('size', None) 823 | if size is not None: 824 | if isinstance(size, tuple) or isinstance(size, list): 825 | return int(_prod(size)), 0 826 | else: 827 | return int(size), 0 828 | scale_factor = kwargs.get('scale_factor', None) 829 | assert scale_factor is not None, "either size or scale_factor should be defined" 830 | flops = input.numel() 831 | if isinstance(scale_factor, tuple) and len(scale_factor) == len(input): 832 | flops * int(_prod(scale_factor)) 833 | else: 834 | flops * scale_factor**len(input) 835 | return flops, 0 836 | 837 | 838 | def _softmax_flops_compute(input, dim=None, _stacklevel=3, dtype=None): 839 | return input.numel(), 0 840 | 841 | 842 | def _embedding_flops_compute( 843 | input, 844 | weight, 845 | padding_idx=None, 846 | max_norm=None, 847 | norm_type=2.0, 848 | scale_grad_by_freq=False, 849 | sparse=False, 850 | ): 851 | return 0, 0 852 | 853 | 854 | def _dropout_flops_compute(input, p=0.5, training=True, inplace=False): 855 | return 0, 0 856 | 857 | 858 | def _matmul_flops_compute(input, other, *, out=None): 859 | """ 860 | Count flops for the matmul operation. 861 | """ 862 | macs = _prod(input.shape) * other.shape[-1] 863 | return 2 * macs, macs 864 | 865 | 866 | def _addmm_flops_compute(input, mat1, mat2, *, beta=1, alpha=1, out=None): 867 | """ 868 | Count flops for the addmm operation. 869 | """ 870 | macs = _prod(mat1.shape) * mat2.shape[-1] 871 | return 2 * macs + _prod(input.shape), macs 872 | 873 | 874 | def _einsum_flops_compute(equation, *operands): 875 | """ 876 | Count flops for the einsum operation. 877 | """ 878 | equation = equation.replace(" ", "") 879 | input_shapes = [o.shape for o in operands] 880 | 881 | # Re-map equation so that same equation with different alphabet 882 | # representations will look the same. 883 | letter_order = OrderedDict((k, 0) for k in equation if k.isalpha()).keys() 884 | mapping = {ord(x): 97 + i for i, x in enumerate(letter_order)} 885 | equation = equation.translate(mapping) 886 | 887 | np_arrs = [np.zeros(s) for s in input_shapes] 888 | optim = np.einsum_path(equation, *np_arrs, optimize="optimal")[1] 889 | for line in optim.split("\n"): 890 | if "optimized flop" in line.lower(): 891 | flop = int(float(line.split(":")[-1])) 892 | return flop, 0 893 | raise NotImplementedError("Unsupported einsum operation.") 894 | 895 | 896 | def _tensor_addmm_flops_compute(self, mat1, mat2, *, beta=1, alpha=1, out=None): 897 | """ 898 | Count flops for the tensor addmm operation. 899 | """ 900 | macs = _prod(mat1.shape) * mat2.shape[-1] 901 | return 2 * macs + _prod(self.shape), macs 902 | 903 | 904 | def _mul_flops_compute(input, other, *, out=None): 905 | return _elementwise_flops_compute(input, other) 906 | 907 | 908 | def _add_flops_compute(input, other, *, alpha=1, out=None): 909 | return _elementwise_flops_compute(input, other) 910 | 911 | 912 | def _elementwise_flops_compute(input, other): 913 | if not torch.is_tensor(input): 914 | if torch.is_tensor(other): 915 | return _prod(other.shape), 0 916 | else: 917 | return 1, 0 918 | elif not torch.is_tensor(other): 919 | return _prod(input.shape), 0 920 | else: 921 | dim_input = len(input.shape) 922 | dim_other = len(other.shape) 923 | max_dim = max(dim_input, dim_other) 924 | 925 | final_shape = [] 926 | for i in range(max_dim): 927 | in_i = input.shape[i] if i < dim_input else 1 928 | ot_i = other.shape[i] if i < dim_other else 1 929 | if in_i > ot_i: 930 | final_shape.append(in_i) 931 | else: 932 | final_shape.append(ot_i) 933 | flops = _prod(final_shape) 934 | return flops, 0 935 | 936 | 937 | def wrapFunc(func, funcFlopCompute): 938 | oldFunc = func 939 | name = func.__str__ 940 | old_functions[name] = oldFunc 941 | 942 | def newFunc(*args, **kwds): 943 | flops, macs = funcFlopCompute(*args, **kwds) 944 | if module_flop_count: 945 | module_flop_count[-1].append((name, flops)) 946 | if module_mac_count and macs: 947 | module_mac_count[-1].append((name, macs)) 948 | return oldFunc(*args, **kwds) 949 | 950 | newFunc.__str__ = func.__str__ 951 | 952 | return newFunc 953 | 954 | 955 | def _patch_functionals(): 956 | # FC 957 | F.linear = wrapFunc(F.linear, _linear_flops_compute) 958 | 959 | # convolutions 960 | F.conv1d = wrapFunc(F.conv1d, _conv_flops_compute) 961 | F.conv2d = wrapFunc(F.conv2d, _conv_flops_compute) 962 | F.conv3d = wrapFunc(F.conv3d, _conv_flops_compute) 963 | 964 | # conv transposed 965 | F.conv_transpose1d = wrapFunc(F.conv_transpose1d, _conv_trans_flops_compute) 966 | F.conv_transpose2d = wrapFunc(F.conv_transpose2d, _conv_trans_flops_compute) 967 | F.conv_transpose3d = wrapFunc(F.conv_transpose3d, _conv_trans_flops_compute) 968 | 969 | # activations 970 | F.relu = wrapFunc(F.relu, _relu_flops_compute) 971 | F.prelu = wrapFunc(F.prelu, _prelu_flops_compute) 972 | F.elu = wrapFunc(F.elu, _elu_flops_compute) 973 | F.leaky_relu = wrapFunc(F.leaky_relu, _leaky_relu_flops_compute) 974 | F.relu6 = wrapFunc(F.relu6, _relu6_flops_compute) 975 | if hasattr(F, "silu"): 976 | F.silu = wrapFunc(F.silu, _silu_flops_compute) 977 | F.gelu = wrapFunc(F.gelu, _gelu_flops_compute) 978 | 979 | # Normalizations 980 | F.batch_norm = wrapFunc(F.batch_norm, _batch_norm_flops_compute) 981 | F.layer_norm = wrapFunc(F.layer_norm, _layer_norm_flops_compute) 982 | F.instance_norm = wrapFunc(F.instance_norm, _instance_norm_flops_compute) 983 | F.group_norm = wrapFunc(F.group_norm, _group_norm_flops_compute) 984 | 985 | # poolings 986 | F.avg_pool1d = wrapFunc(F.avg_pool1d, _pool_flops_compute) 987 | F.avg_pool2d = wrapFunc(F.avg_pool2d, _pool_flops_compute) 988 | F.avg_pool3d = wrapFunc(F.avg_pool3d, _pool_flops_compute) 989 | F.max_pool1d = wrapFunc(F.max_pool1d, _pool_flops_compute) 990 | F.max_pool2d = wrapFunc(F.max_pool2d, _pool_flops_compute) 991 | F.max_pool3d = wrapFunc(F.max_pool3d, _pool_flops_compute) 992 | F.adaptive_avg_pool1d = wrapFunc(F.adaptive_avg_pool1d, _pool_flops_compute) 993 | F.adaptive_avg_pool2d = wrapFunc(F.adaptive_avg_pool2d, _pool_flops_compute) 994 | F.adaptive_avg_pool3d = wrapFunc(F.adaptive_avg_pool3d, _pool_flops_compute) 995 | F.adaptive_max_pool1d = wrapFunc(F.adaptive_max_pool1d, _pool_flops_compute) 996 | F.adaptive_max_pool2d = wrapFunc(F.adaptive_max_pool2d, _pool_flops_compute) 997 | F.adaptive_max_pool3d = wrapFunc(F.adaptive_max_pool3d, _pool_flops_compute) 998 | 999 | # upsample 1000 | F.upsample = wrapFunc(F.upsample, _upsample_flops_compute) 1001 | F.interpolate = wrapFunc(F.interpolate, _upsample_flops_compute) 1002 | 1003 | # softmax 1004 | F.softmax = wrapFunc(F.softmax, _softmax_flops_compute) 1005 | 1006 | # embedding 1007 | F.embedding = wrapFunc(F.embedding, _embedding_flops_compute) 1008 | 1009 | 1010 | def _patch_tensor_methods(): 1011 | torch.matmul = wrapFunc(torch.matmul, _matmul_flops_compute) 1012 | torch.Tensor.matmul = wrapFunc(torch.Tensor.matmul, _matmul_flops_compute) 1013 | torch.mm = wrapFunc(torch.mm, _matmul_flops_compute) 1014 | torch.Tensor.mm = wrapFunc(torch.Tensor.mm, _matmul_flops_compute) 1015 | torch.bmm = wrapFunc(torch.bmm, _matmul_flops_compute) 1016 | torch.Tensor.bmm = wrapFunc(torch.Tensor.bmm, _matmul_flops_compute) 1017 | 1018 | torch.addmm = wrapFunc(torch.addmm, _addmm_flops_compute) 1019 | torch.Tensor.addmm = wrapFunc(torch.Tensor.addmm, _tensor_addmm_flops_compute) 1020 | 1021 | torch.mul = wrapFunc(torch.mul, _mul_flops_compute) 1022 | torch.Tensor.mul = wrapFunc(torch.Tensor.mul, _mul_flops_compute) 1023 | 1024 | torch.add = wrapFunc(torch.add, _add_flops_compute) 1025 | torch.Tensor.add = wrapFunc(torch.Tensor.add, _add_flops_compute) 1026 | 1027 | torch.einsum = wrapFunc(torch.einsum, _einsum_flops_compute) 1028 | 1029 | torch.baddbmm = wrapFunc(torch.baddbmm, _tensor_addmm_flops_compute) 1030 | 1031 | 1032 | def _reload_functionals(): 1033 | # torch.nn.functional does not support importlib.reload() 1034 | F.linear = old_functions[F.linear.__str__] 1035 | F.conv1d = old_functions[F.conv1d.__str__] 1036 | F.conv2d = old_functions[F.conv2d.__str__] 1037 | F.conv3d = old_functions[F.conv3d.__str__] 1038 | F.conv_transpose1d = old_functions[F.conv_transpose1d.__str__] 1039 | F.conv_transpose2d = old_functions[F.conv_transpose2d.__str__] 1040 | F.conv_transpose3d = old_functions[F.conv_transpose3d.__str__] 1041 | F.relu = old_functions[F.relu.__str__] 1042 | F.prelu = old_functions[F.prelu.__str__] 1043 | F.elu = old_functions[F.elu.__str__] 1044 | F.leaky_relu = old_functions[F.leaky_relu.__str__] 1045 | F.relu6 = old_functions[F.relu6.__str__] 1046 | if hasattr(F, "silu"): 1047 | F.silu = old_functions[F.silu.__str__] 1048 | F.gelu = old_functions[F.gelu.__str__] 1049 | F.batch_norm = old_functions[F.batch_norm.__str__] 1050 | F.layer_norm = old_functions[F.layer_norm.__str__] 1051 | F.instance_norm = old_functions[F.instance_norm.__str__] 1052 | F.group_norm = old_functions[F.group_norm.__str__] 1053 | F.avg_pool1d = old_functions[F.avg_pool1d.__str__] 1054 | F.avg_pool2d = old_functions[F.avg_pool2d.__str__] 1055 | F.avg_pool3d = old_functions[F.avg_pool3d.__str__] 1056 | F.max_pool1d = old_functions[F.max_pool1d.__str__] 1057 | F.max_pool2d = old_functions[F.max_pool2d.__str__] 1058 | F.max_pool3d = old_functions[F.max_pool3d.__str__] 1059 | F.adaptive_avg_pool1d = old_functions[F.adaptive_avg_pool1d.__str__] 1060 | F.adaptive_avg_pool2d = old_functions[F.adaptive_avg_pool2d.__str__] 1061 | F.adaptive_avg_pool3d = old_functions[F.adaptive_avg_pool3d.__str__] 1062 | F.adaptive_max_pool1d = old_functions[F.adaptive_max_pool1d.__str__] 1063 | F.adaptive_max_pool2d = old_functions[F.adaptive_max_pool2d.__str__] 1064 | F.adaptive_max_pool3d = old_functions[F.adaptive_max_pool3d.__str__] 1065 | F.upsample = old_functions[F.upsample.__str__] 1066 | F.interpolate = old_functions[F.interpolate.__str__] 1067 | F.softmax = old_functions[F.softmax.__str__] 1068 | F.embedding = old_functions[F.embedding.__str__] 1069 | 1070 | 1071 | def _reload_tensor_methods(): 1072 | torch.matmul = old_functions[torch.matmul.__str__] 1073 | torch.Tensor.matmul = old_functions[torch.Tensor.matmul.__str__] 1074 | torch.mm = old_functions[torch.mm.__str__] 1075 | torch.Tensor.mm = old_functions[torch.Tensor.mm.__str__] 1076 | torch.bmm = old_functions[torch.matmul.__str__] 1077 | torch.Tensor.bmm = old_functions[torch.Tensor.bmm.__str__] 1078 | torch.addmm = old_functions[torch.addmm.__str__] 1079 | torch.Tensor.addmm = old_functions[torch.Tensor.addmm.__str__] 1080 | torch.mul = old_functions[torch.mul.__str__] 1081 | torch.Tensor.mul = old_functions[torch.Tensor.mul.__str__] 1082 | torch.add = old_functions[torch.add.__str__] 1083 | torch.Tensor.add = old_functions[torch.Tensor.add.__str__] 1084 | 1085 | torch.einsum = old_functions[torch.einsum.__str__] 1086 | 1087 | torch.baddbmm = old_functions[torch.baddbmm.__str__] 1088 | 1089 | 1090 | def _rnn_flops(flops, rnn_module, w_ih, w_hh, input_size): 1091 | # matrix matrix mult ih state and internal state 1092 | flops += w_ih.shape[0] * w_ih.shape[1] 1093 | # matrix matrix mult hh state and internal state 1094 | flops += w_hh.shape[0] * w_hh.shape[1] 1095 | if isinstance(rnn_module, (nn.RNN, nn.RNNCell)): 1096 | # add both operations 1097 | flops += rnn_module.hidden_size 1098 | elif isinstance(rnn_module, (nn.GRU, nn.GRUCell)): 1099 | # hadamard of r 1100 | flops += rnn_module.hidden_size 1101 | # adding operations from both states 1102 | flops += rnn_module.hidden_size * 3 1103 | # last two hadamard _product and add 1104 | flops += rnn_module.hidden_size * 3 1105 | elif isinstance(rnn_module, (nn.LSTM, nn.LSTMCell)): 1106 | # adding operations from both states 1107 | flops += rnn_module.hidden_size * 4 1108 | # two hadamard _product and add for C state 1109 | flops += rnn_module.hidden_size + rnn_module.hidden_size + rnn_module.hidden_size 1110 | # final hadamard 1111 | flops += rnn_module.hidden_size + rnn_module.hidden_size + rnn_module.hidden_size 1112 | return flops 1113 | 1114 | 1115 | def _rnn_forward_hook(rnn_module, input, output): 1116 | flops = 0 1117 | # input is a tuple containing a sequence to process and (optionally) hidden state 1118 | inp = input[0] 1119 | batch_size = inp.shape[0] 1120 | seq_length = inp.shape[1] 1121 | num_layers = rnn_module.num_layers 1122 | 1123 | for i in range(num_layers): 1124 | w_ih = rnn_module.__getattr__("weight_ih_l" + str(i)) 1125 | w_hh = rnn_module.__getattr__("weight_hh_l" + str(i)) 1126 | if i == 0: 1127 | input_size = rnn_module.input_size 1128 | else: 1129 | input_size = rnn_module.hidden_size 1130 | flops = _rnn_flops(flops, rnn_module, w_ih, w_hh, input_size) 1131 | if rnn_module.bias: 1132 | b_ih = rnn_module.__getattr__("bias_ih_l" + str(i)) 1133 | b_hh = rnn_module.__getattr__("bias_hh_l" + str(i)) 1134 | flops += b_ih.shape[0] + b_hh.shape[0] 1135 | 1136 | flops *= batch_size 1137 | flops *= seq_length 1138 | if rnn_module.bidirectional: 1139 | flops *= 2 1140 | rnn_module.__flops__ += int(flops) 1141 | 1142 | 1143 | def _rnn_cell_forward_hook(rnn_cell_module, input, output): 1144 | flops = 0 1145 | inp = input[0] 1146 | batch_size = inp.shape[0] 1147 | w_ih = rnn_cell_module.__getattr__("weight_ih") 1148 | w_hh = rnn_cell_module.__getattr__("weight_hh") 1149 | input_size = inp.shape[1] 1150 | flops = _rnn_flops(flops, rnn_cell_module, w_ih, w_hh, input_size) 1151 | if rnn_cell_module.bias: 1152 | b_ih = rnn_cell_module.__getattr__("bias_ih") 1153 | b_hh = rnn_cell_module.__getattr__("bias_hh") 1154 | flops += b_ih.shape[0] + b_hh.shape[0] 1155 | 1156 | flops *= batch_size 1157 | rnn_cell_module.__flops__ += int(flops) 1158 | 1159 | 1160 | MODULE_HOOK_MAPPING = { 1161 | # RNN 1162 | nn.RNN: _rnn_forward_hook, 1163 | nn.GRU: _rnn_forward_hook, 1164 | nn.LSTM: _rnn_forward_hook, 1165 | nn.RNNCell: _rnn_cell_forward_hook, 1166 | nn.LSTMCell: _rnn_cell_forward_hook, 1167 | nn.GRUCell: _rnn_cell_forward_hook, 1168 | } 1169 | 1170 | 1171 | def num_to_string(num, precision=2): 1172 | if num // 10**9 > 0: 1173 | return str(round(num / 10.0**9, precision)) + " G" 1174 | elif num // 10**6 > 0: 1175 | return str(round(num / 10.0**6, precision)) + " M" 1176 | elif num // 10**3 > 0: 1177 | return str(round(num / 10.0**3, precision)) + " K" 1178 | else: 1179 | return str(num) 1180 | 1181 | 1182 | def macs_to_string(macs, units=None, precision=2): 1183 | if units is None: 1184 | if macs // 10**9 > 0: 1185 | return str(round(macs / 10.0**9, precision)) + " GMACs" 1186 | elif macs // 10**6 > 0: 1187 | return str(round(macs / 10.0**6, precision)) + " MMACs" 1188 | elif macs // 10**3 > 0: 1189 | return str(round(macs / 10.0**3, precision)) + " KMACs" 1190 | else: 1191 | return str(macs) + " MACs" 1192 | else: 1193 | if units == "GMACs": 1194 | return str(round(macs / 10.0**9, precision)) + " " + units 1195 | elif units == "MMACs": 1196 | return str(round(macs / 10.0**6, precision)) + " " + units 1197 | elif units == "KMACs": 1198 | return str(round(macs / 10.0**3, precision)) + " " + units 1199 | else: 1200 | return str(macs) + " MACs" 1201 | 1202 | 1203 | def number_to_string(num, units=None, precision=2): 1204 | if units is None: 1205 | if num // 10**9 > 0: 1206 | return str(round(num / 10.0**9, precision)) + " G" 1207 | elif num // 10**6 > 0: 1208 | return str(round(num / 10.0**6, precision)) + " M" 1209 | elif num // 10**3 > 0: 1210 | return str(round(num / 10.0**3, precision)) + " K" 1211 | else: 1212 | return str(num) + " " 1213 | else: 1214 | if units == "G": 1215 | return str(round(num / 10.0**9, precision)) + " " + units 1216 | elif units == "M": 1217 | return str(round(num / 10.0**6, precision)) + " " + units 1218 | elif units == "K": 1219 | return str(round(num / 10.0**3, precision)) + " " + units 1220 | else: 1221 | return str(num) + " " 1222 | 1223 | 1224 | def flops_to_string(flops, units=None, precision=2): 1225 | if units is None: 1226 | if flops // 10**12 > 0: 1227 | return str(round(flops / 10.0**12, precision)) + " TFLOPS" 1228 | if flops // 10**9 > 0: 1229 | return str(round(flops / 10.0**9, precision)) + " GFLOPS" 1230 | elif flops // 10**6 > 0: 1231 | return str(round(flops / 10.0**6, precision)) + " MFLOPS" 1232 | elif flops // 10**3 > 0: 1233 | return str(round(flops / 10.0**3, precision)) + " KFLOPS" 1234 | else: 1235 | return str(flops) + " FLOPS" 1236 | else: 1237 | if units == "TFLOPS": 1238 | return str(round(flops / 10.0**12, precision)) + " " + units 1239 | if units == "GFLOPS": 1240 | return str(round(flops / 10.0**9, precision)) + " " + units 1241 | elif units == "MFLOPS": 1242 | return str(round(flops / 10.0**6, precision)) + " " + units 1243 | elif units == "KFLOPS": 1244 | return str(round(flops / 10.0**3, precision)) + " " + units 1245 | else: 1246 | return str(flops) + " FLOPS" 1247 | 1248 | 1249 | def params_to_string(params_num, units=None, precision=2): 1250 | if units is None: 1251 | if params_num // 10**6 > 0: 1252 | return str(round(params_num / 10**6, 2)) + " M" 1253 | elif params_num // 10**3: 1254 | return str(round(params_num / 10**3, 2)) + " k" 1255 | else: 1256 | return str(params_num) 1257 | else: 1258 | if units == "M": 1259 | return str(round(params_num / 10.0**6, precision)) + " " + units 1260 | elif units == "K": 1261 | return str(round(params_num / 10.0**3, precision)) + " " + units 1262 | else: 1263 | return str(params_num) 1264 | 1265 | 1266 | def duration_to_string(duration, units=None, precision=2): 1267 | if units is None: 1268 | if duration > 1: 1269 | return str(round(duration, precision)) + " s" 1270 | elif duration * 10**3 > 1: 1271 | return str(round(duration * 10**3, precision)) + " ms" 1272 | elif duration * 10**6 > 1: 1273 | return str(round(duration * 10**6, precision)) + " us" 1274 | else: 1275 | return str(duration) 1276 | else: 1277 | if units == "us": 1278 | return str(round(duration * 10.0**6, precision)) + " " + units 1279 | elif units == "ms": 1280 | return str(round(duration * 10.0**3, precision)) + " " + units 1281 | else: 1282 | return str(round(duration, precision)) + " s" 1283 | 1284 | 1285 | # can not iterate over all submodules using self.model.modules() 1286 | # since modules() returns duplicate modules only once 1287 | def get_module_flops(module): 1288 | sum = module.__flops__ 1289 | # iterate over immediate children modules 1290 | for child in module.children(): 1291 | sum += get_module_flops(child) 1292 | return sum 1293 | 1294 | 1295 | def get_module_macs(module): 1296 | sum = module.__macs__ 1297 | # iterate over immediate children modules 1298 | for child in module.children(): 1299 | sum += get_module_macs(child) 1300 | return sum 1301 | 1302 | 1303 | def get_module_duration(module): 1304 | duration = module.__duration__ 1305 | if duration == 0: # e.g. ModuleList 1306 | for m in module.children(): 1307 | duration += m.__duration__ 1308 | return duration 1309 | 1310 | 1311 | def get_model_profile( 1312 | model, 1313 | input_shape=None, 1314 | args=[], 1315 | kwargs={}, 1316 | print_profile=True, 1317 | detailed=True, 1318 | module_depth=-1, 1319 | top_modules=1, 1320 | warm_up=1, 1321 | as_string=True, 1322 | output_file=None, 1323 | ignore_modules=None, 1324 | ): 1325 | """Returns the total floating-point operations, MACs, and parameters of a model. 1326 | 1327 | Example: 1328 | 1329 | .. code-block:: python 1330 | 1331 | model = torchvision.models.alexnet() 1332 | batch_size = 256 1333 | flops, macs, params = get_model_profile(model=model, input_shape=(batch_size, 3, 224, 224))) 1334 | 1335 | Args: 1336 | model ([torch.nn.Module]): the PyTorch model to be profiled. 1337 | input_shape (tuple): input shape to the model. If specified, the model takes a tensor with this shape as the only positional argument. 1338 | args (list): list of positional arguments to the model. 1339 | kwargs (dict): dictionary of keyword arguments to the model. 1340 | print_profile (bool, optional): whether to print the model profile. Defaults to True. 1341 | detailed (bool, optional): whether to print the detailed model profile. Defaults to True. 1342 | module_depth (int, optional): the depth into the nested modules. Defaults to -1 (the inner most modules). 1343 | top_modules (int, optional): the number of top modules to print in the aggregated profile. Defaults to 3. 1344 | warm_up (int, optional): the number of warm-up steps before measuring the latency of each module. Defaults to 1. 1345 | as_string (bool, optional): whether to print the output as string. Defaults to True. 1346 | output_file (str, optional): path to the output file. If None, the profiler prints to stdout. 1347 | ignore_modules ([type], optional): the list of modules to ignore during profiling. Defaults to None. 1348 | 1349 | Returns: 1350 | The number of floating-point operations, multiply-accumulate operations (MACs), and parameters in the model. 1351 | """ 1352 | assert isinstance(model, nn.Module), "model must be a PyTorch module" 1353 | prof = TIDSProfiler(model) 1354 | model.eval() 1355 | 1356 | if input_shape is not None: 1357 | assert type(input_shape) is tuple, "input_shape must be a tuple" 1358 | assert len(input_shape) >= 1, "input_shape must have at least one element" 1359 | try: 1360 | input = torch.ones(()).new_empty( 1361 | (*input_shape, 1362 | ), 1363 | dtype=next(model.parameters()).dtype, 1364 | device=next(model.parameters()).device, 1365 | ) 1366 | except StopIteration: 1367 | input = torch.ones(()).new_empty((*input_shape, )) 1368 | 1369 | args = [input] 1370 | assert (len(args) > 0) or (len(kwargs) > 0), "args and/or kwargs must be specified if input_shape is None" 1371 | 1372 | for _ in range(warm_up): 1373 | if kwargs: 1374 | _ = model(*args, **kwargs) 1375 | else: 1376 | _ = model(*args) 1377 | prof.start_profile(ignore_list=ignore_modules) 1378 | 1379 | if kwargs: 1380 | _ = model(*args, **kwargs) 1381 | else: 1382 | _ = model(*args) 1383 | 1384 | flops = prof.get_total_flops() 1385 | macs = prof.get_total_macs() 1386 | params = prof.get_total_params() 1387 | if print_profile: 1388 | prof.print_model_profile(profile_step=warm_up, 1389 | module_depth=module_depth, 1390 | top_modules=top_modules, 1391 | detailed=detailed, 1392 | output_file=output_file) 1393 | 1394 | prof.end_profile() 1395 | if as_string: 1396 | return number_to_string(flops), macs_to_string(macs), params_to_string(params) 1397 | 1398 | return flops, macs, params 1399 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # torch_profiler 2 | 3 | This profiler combines code from TylerYep/torchinfo ([github](https://github.com/TylerYep/torchinfo)) and Microsoft DeepSpeed's Flops Profiler ([github](https://github.com/microsoft/DeepSpeed/blob/master/deepspeed/profiling/flops_profiler), [tutorial](https://www.deepspeed.ai/tutorials/flops-profiler/)). The motivation behind writing this up is that DeepSpeed Flops Profiler profiles both the model training/inference speed (latency, throughput) and the efficiency (floating-point operations per second, i.e., FLOPS) of a model and its submodules but not the shape of the input/output of each module, and torchinfo is the other way around. Although this profiler only provides some basic functionalities, it achieves the best of both worlds in this aspect. 4 | 5 | This profiler is based on PyTorch hooks, so the profiling granularity is each `torch.nn.Module`. 6 | 7 | ## Getting Started/Example 8 | 9 | You should first define the PyTorch model and its dummy input: 10 | 11 | ```python 12 | import torch 13 | import torchvision.models as models 14 | from profiler import TIDSProfiler 15 | 16 | # construct model and input 17 | model = models.resnet18() 18 | batch_size = 16 19 | input_size = (batch_size, 3, 224, 224) 20 | inputs = torch.randn(input_size) 21 | ``` 22 | 23 | Then, you can start the profiling. The usage is similar to DeepSpeed Flops Profiler. 24 | 25 | ```python 26 | # start profiling 27 | prof = TIDSProfiler(model) 28 | prof.start_profile() 29 | model(inputs) 30 | profile = prof.generate_profile() 31 | print(profile) 32 | prof.end_profile() 33 | 34 | ``` 35 | 36 | The output in this example looks like: 37 | 38 | ```text 39 | (model): ResNet, num_params 11689512, 94.485ms, 100.0% latency, input shape [16, 3, 224, 224], output shape [16, 1000] 40 | (conv1): Conv2d, num_params 9408, 15.642ms, 16.555% latency, input shape [16, 3, 224, 224], output shape [16, 64, 112, 112] 41 | (bn1): BatchNorm2d, num_params 128, 5.027ms, 5.32% latency, input shape [16, 64, 112, 112], output shape [16, 64, 112, 112] 42 | (relu): ReLU, num_params 0, 0.911ms, 0.964% latency, input shape [16, 64, 112, 112], output shape [16, 64, 112, 112] 43 | (maxpool): MaxPool2d, num_params 0, 4.476ms, 4.738% latency, input shape [16, 64, 112, 112], output shape [16, 64, 56, 56] 44 | (layer1): Sequential, num_params 147968, 22.401ms, 23.708% latency, input shape [16, 64, 56, 56], output shape [16, 64, 56, 56] 45 | (0): BasicBlock, num_params 73984, 11.021ms, 11.664% latency, input shape [16, 64, 56, 56], output shape [16, 64, 56, 56] 46 | (conv1): Conv2d, num_params 36864, 4.471ms, 4.732% latency, input shape [16, 64, 56, 56], output shape [16, 64, 56, 56] 47 | (bn1): BatchNorm2d, num_params 128, 0.898ms, 0.951% latency, input shape [16, 64, 56, 56], output shape [16, 64, 56, 56] 48 | (relu): ReLU, num_params 0, 0.362ms, 0.384% latency, input shape [16, 64, 56, 56], output shape [16, 64, 56, 56] 49 | (conv2): Conv2d, num_params 36864, 3.85ms, 4.074% latency, input shape [16, 64, 56, 56], output shape [16, 64, 56, 56] 50 | (bn2): BatchNorm2d, num_params 128, 0.888ms, 0.939% latency, input shape [16, 64, 56, 56], output shape [16, 64, 56, 56] 51 | (1): BasicBlock, num_params 73984, 11.271ms, 11.929% latency, input shape [16, 64, 56, 56], output shape [16, 64, 56, 56] 52 | (conv1): Conv2d, num_params 36864, 3.817ms, 4.04% latency, input shape [16, 64, 56, 56], output shape [16, 64, 56, 56] 53 | (bn1): BatchNorm2d, num_params 128, 1.507ms, 1.595% latency, input shape [16, 64, 56, 56], output shape [16, 64, 56, 56] 54 | (relu): ReLU, num_params 0, 0.442ms, 0.468% latency, input shape [16, 64, 56, 56], output shape [16, 64, 56, 56] 55 | (conv2): Conv2d, num_params 36864, 3.981ms, 4.214% latency, input shape [16, 64, 56, 56], output shape [16, 64, 56, 56] 56 | (bn2): BatchNorm2d, num_params 128, 1.017ms, 1.077% latency, input shape [16, 64, 56, 56], output shape [16, 64, 56, 56] 57 | (layer2): Sequential, num_params 525568, 15.624ms, 16.536% latency, input shape [16, 64, 56, 56], output shape [16, 128, 28, 28] 58 | (0): BasicBlock, num_params 230144, 9.375ms, 9.923% latency, input shape [16, 64, 56, 56], output shape [16, 128, 28, 28] 59 | (conv1): Conv2d, num_params 73728, 2.725ms, 2.884% latency, input shape [16, 64, 56, 56], output shape [16, 128, 28, 28] 60 | (bn1): BatchNorm2d, num_params 256, 0.545ms, 0.577% latency, input shape [16, 128, 28, 28], output shape [16, 128, 28, 28] 61 | (relu): ReLU, num_params 0, 0.204ms, 0.216% latency, input shape [16, 128, 28, 28], output shape [16, 128, 28, 28] 62 | (conv2): Conv2d, num_params 147456, 2.508ms, 2.654% latency, input shape [16, 128, 28, 28], output shape [16, 128, 28, 28] 63 | (bn2): BatchNorm2d, num_params 256, 0.46ms, 0.487% latency, input shape [16, 128, 28, 28], output shape [16, 128, 28, 28] 64 | (downsample): Sequential, num_params 8448, 2.631ms, 2.785% latency, input shape [16, 64, 56, 56], output shape [16, 128, 28, 28] 65 | (0): Conv2d, num_params 8192, 2.038ms, 2.156% latency, input shape [16, 64, 56, 56], output shape [16, 128, 28, 28] 66 | (1): BatchNorm2d, num_params 256, 0.501ms, 0.53% latency, input shape [16, 128, 28, 28], output shape [16, 128, 28, 28] 67 | (1): BasicBlock, num_params 295424, 6.164ms, 6.524% latency, input shape [16, 128, 28, 28], output shape [16, 128, 28, 28] 68 | (conv1): Conv2d, num_params 147456, 1.831ms, 1.938% latency, input shape [16, 128, 28, 28], output shape [16, 128, 28, 28] 69 | (bn1): BatchNorm2d, num_params 256, 0.616ms, 0.652% latency, input shape [16, 128, 28, 28], output shape [16, 128, 28, 28] 70 | (relu): ReLU, num_params 0, 0.205ms, 0.217% latency, input shape [16, 128, 28, 28], output shape [16, 128, 28, 28] 71 | (conv2): Conv2d, num_params 147456, 2.761ms, 2.922% latency, input shape [16, 128, 28, 28], output shape [16, 128, 28, 28] 72 | (bn2): BatchNorm2d, num_params 256, 0.48ms, 0.508% latency, input shape [16, 128, 28, 28], output shape [16, 128, 28, 28] 73 | (layer3): Sequential, num_params 2099712, 14.438ms, 15.281% latency, input shape [16, 128, 28, 28], output shape [16, 256, 14, 14] 74 | (0): BasicBlock, num_params 919040, 8.039ms, 8.509% latency, input shape [16, 128, 28, 28], output shape [16, 256, 14, 14] 75 | (conv1): Conv2d, num_params 294912, 2.385ms, 2.524% latency, input shape [16, 128, 28, 28], output shape [16, 256, 14, 14] 76 | (bn1): BatchNorm2d, num_params 512, 0.33ms, 0.349% latency, input shape [16, 256, 14, 14], output shape [16, 256, 14, 14] 77 | (relu): ReLU, num_params 0, 0.195ms, 0.206% latency, input shape [16, 256, 14, 14], output shape [16, 256, 14, 14] 78 | (conv2): Conv2d, num_params 589824, 2.326ms, 2.462% latency, input shape [16, 256, 14, 14], output shape [16, 256, 14, 14] 79 | (bn2): BatchNorm2d, num_params 512, 0.341ms, 0.361% latency, input shape [16, 256, 14, 14], output shape [16, 256, 14, 14] 80 | (downsample): Sequential, num_params 33280, 2.147ms, 2.273% latency, input shape [16, 128, 28, 28], output shape [16, 256, 14, 14] 81 | (0): Conv2d, num_params 32768, 1.697ms, 1.796% latency, input shape [16, 128, 28, 28], output shape [16, 256, 14, 14] 82 | (1): BatchNorm2d, num_params 512, 0.369ms, 0.39% latency, input shape [16, 256, 14, 14], output shape [16, 256, 14, 14] 83 | (1): BasicBlock, num_params 1180672, 6.317ms, 6.685% latency, input shape [16, 256, 14, 14], output shape [16, 256, 14, 14] 84 | (conv1): Conv2d, num_params 589824, 2.217ms, 2.346% latency, input shape [16, 256, 14, 14], output shape [16, 256, 14, 14] 85 | (bn1): BatchNorm2d, num_params 512, 0.447ms, 0.473% latency, input shape [16, 256, 14, 14], output shape [16, 256, 14, 14] 86 | (relu): ReLU, num_params 0, 0.196ms, 0.208% latency, input shape [16, 256, 14, 14], output shape [16, 256, 14, 14] 87 | (conv2): Conv2d, num_params 589824, 2.577ms, 2.727% latency, input shape [16, 256, 14, 14], output shape [16, 256, 14, 14] 88 | (bn2): BatchNorm2d, num_params 512, 0.597ms, 0.632% latency, input shape [16, 256, 14, 14], output shape [16, 256, 14, 14] 89 | (layer4): Sequential, num_params 8393728, 14.592ms, 15.444% latency, input shape [16, 256, 14, 14], output shape [16, 512, 7, 7] 90 | (0): BasicBlock, num_params 3673088, 8.246ms, 8.727% latency, input shape [16, 256, 14, 14], output shape [16, 512, 7, 7] 91 | (conv1): Conv2d, num_params 1179648, 2.546ms, 2.695% latency, input shape [16, 256, 14, 14], output shape [16, 512, 7, 7] 92 | (bn1): BatchNorm2d, num_params 1024, 0.281ms, 0.298% latency, input shape [16, 512, 7, 7], output shape [16, 512, 7, 7] 93 | (relu): ReLU, num_params 0, 0.203ms, 0.215% latency, input shape [16, 512, 7, 7], output shape [16, 512, 7, 7] 94 | (conv2): Conv2d, num_params 2359296, 3.17ms, 3.356% latency, input shape [16, 512, 7, 7], output shape [16, 512, 7, 7] 95 | (bn2): BatchNorm2d, num_params 1024, 0.283ms, 0.3% latency, input shape [16, 512, 7, 7], output shape [16, 512, 7, 7] 96 | (downsample): Sequential, num_params 132096, 1.485ms, 1.572% latency, input shape [16, 256, 14, 14], output shape [16, 512, 7, 7] 97 | (0): Conv2d, num_params 131072, 1.112ms, 1.177% latency, input shape [16, 256, 14, 14], output shape [16, 512, 7, 7] 98 | (1): BatchNorm2d, num_params 1024, 0.283ms, 0.299% latency, input shape [16, 512, 7, 7], output shape [16, 512, 7, 7] 99 | (1): BasicBlock, num_params 4720640, 6.264ms, 6.63% latency, input shape [16, 512, 7, 7], output shape [16, 512, 7, 7] 100 | (conv1): Conv2d, num_params 2359296, 2.629ms, 2.783% latency, input shape [16, 512, 7, 7], output shape [16, 512, 7, 7] 101 | (bn1): BatchNorm2d, num_params 1024, 0.25ms, 0.264% latency, input shape [16, 512, 7, 7], output shape [16, 512, 7, 7] 102 | (relu): ReLU, num_params 0, 0.189ms, 0.2% latency, input shape [16, 512, 7, 7], output shape [16, 512, 7, 7] 103 | (conv2): Conv2d, num_params 2359296, 2.697ms, 2.855% latency, input shape [16, 512, 7, 7], output shape [16, 512, 7, 7] 104 | (bn2): BatchNorm2d, num_params 1024, 0.262ms, 0.278% latency, input shape [16, 512, 7, 7], output shape [16, 512, 7, 7] 105 | (avgpool): AdaptiveAvgPool2d, num_params 0, 0.407ms, 0.43% latency, input shape [16, 512, 7, 7], output shape [16, 512, 1, 1] 106 | (fc): Linear, num_params 513000, 0.502ms, 0.531% latency, input shape [16, 512], output shape [16, 1000] 107 | ``` 108 | 109 | ### Comparisons with TorchInfo and DeepSpeed 110 | 111 |
112 | TorchInfo output 113 | 114 | ```bash 115 | $ python3 example_resnet18.py --profiler torchinfo 116 | ========================================================================================== 117 | Layer (type:depth-idx) Output Shape Param # 118 | ========================================================================================== 119 | ResNet [16, 1000] -- 120 | ├─Conv2d: 1-1 [16, 64, 112, 112] 9,408 121 | ├─BatchNorm2d: 1-2 [16, 64, 112, 112] 128 122 | ├─ReLU: 1-3 [16, 64, 112, 112] -- 123 | ├─MaxPool2d: 1-4 [16, 64, 56, 56] -- 124 | ├─Sequential: 1-5 [16, 64, 56, 56] -- 125 | │ └─BasicBlock: 2-1 [16, 64, 56, 56] -- 126 | │ │ └─Conv2d: 3-1 [16, 64, 56, 56] 36,864 127 | │ │ └─BatchNorm2d: 3-2 [16, 64, 56, 56] 128 128 | │ │ └─ReLU: 3-3 [16, 64, 56, 56] -- 129 | │ │ └─Conv2d: 3-4 [16, 64, 56, 56] 36,864 130 | │ │ └─BatchNorm2d: 3-5 [16, 64, 56, 56] 128 131 | │ │ └─ReLU: 3-6 [16, 64, 56, 56] -- 132 | │ └─BasicBlock: 2-2 [16, 64, 56, 56] -- 133 | │ │ └─Conv2d: 3-7 [16, 64, 56, 56] 36,864 134 | │ │ └─BatchNorm2d: 3-8 [16, 64, 56, 56] 128 135 | │ │ └─ReLU: 3-9 [16, 64, 56, 56] -- 136 | │ │ └─Conv2d: 3-10 [16, 64, 56, 56] 36,864 137 | │ │ └─BatchNorm2d: 3-11 [16, 64, 56, 56] 128 138 | │ │ └─ReLU: 3-12 [16, 64, 56, 56] -- 139 | ├─Sequential: 1-6 [16, 128, 28, 28] -- 140 | │ └─BasicBlock: 2-3 [16, 128, 28, 28] -- 141 | │ │ └─Conv2d: 3-13 [16, 128, 28, 28] 73,728 142 | │ │ └─BatchNorm2d: 3-14 [16, 128, 28, 28] 256 143 | │ │ └─ReLU: 3-15 [16, 128, 28, 28] -- 144 | │ │ └─Conv2d: 3-16 [16, 128, 28, 28] 147,456 145 | │ │ └─BatchNorm2d: 3-17 [16, 128, 28, 28] 256 146 | │ │ └─Sequential: 3-18 [16, 128, 28, 28] 8,448 147 | │ │ └─ReLU: 3-19 [16, 128, 28, 28] -- 148 | │ └─BasicBlock: 2-4 [16, 128, 28, 28] -- 149 | │ │ └─Conv2d: 3-20 [16, 128, 28, 28] 147,456 150 | │ │ └─BatchNorm2d: 3-21 [16, 128, 28, 28] 256 151 | │ │ └─ReLU: 3-22 [16, 128, 28, 28] -- 152 | │ │ └─Conv2d: 3-23 [16, 128, 28, 28] 147,456 153 | │ │ └─BatchNorm2d: 3-24 [16, 128, 28, 28] 256 154 | │ │ └─ReLU: 3-25 [16, 128, 28, 28] -- 155 | ├─Sequential: 1-7 [16, 256, 14, 14] -- 156 | │ └─BasicBlock: 2-5 [16, 256, 14, 14] -- 157 | │ │ └─Conv2d: 3-26 [16, 256, 14, 14] 294,912 158 | │ │ └─BatchNorm2d: 3-27 [16, 256, 14, 14] 512 159 | │ │ └─ReLU: 3-28 [16, 256, 14, 14] -- 160 | │ │ └─Conv2d: 3-29 [16, 256, 14, 14] 589,824 161 | │ │ └─BatchNorm2d: 3-30 [16, 256, 14, 14] 512 162 | │ │ └─Sequential: 3-31 [16, 256, 14, 14] 33,280 163 | │ │ └─ReLU: 3-32 [16, 256, 14, 14] -- 164 | │ └─BasicBlock: 2-6 [16, 256, 14, 14] -- 165 | │ │ └─Conv2d: 3-33 [16, 256, 14, 14] 589,824 166 | │ │ └─BatchNorm2d: 3-34 [16, 256, 14, 14] 512 167 | │ │ └─ReLU: 3-35 [16, 256, 14, 14] -- 168 | │ │ └─Conv2d: 3-36 [16, 256, 14, 14] 589,824 169 | │ │ └─BatchNorm2d: 3-37 [16, 256, 14, 14] 512 170 | │ │ └─ReLU: 3-38 [16, 256, 14, 14] -- 171 | ├─Sequential: 1-8 [16, 512, 7, 7] -- 172 | │ └─BasicBlock: 2-7 [16, 512, 7, 7] -- 173 | │ │ └─Conv2d: 3-39 [16, 512, 7, 7] 1,179,648 174 | │ │ └─BatchNorm2d: 3-40 [16, 512, 7, 7] 1,024 175 | │ │ └─ReLU: 3-41 [16, 512, 7, 7] -- 176 | │ │ └─Conv2d: 3-42 [16, 512, 7, 7] 2,359,296 177 | │ │ └─BatchNorm2d: 3-43 [16, 512, 7, 7] 1,024 178 | │ │ └─Sequential: 3-44 [16, 512, 7, 7] 132,096 179 | │ │ └─ReLU: 3-45 [16, 512, 7, 7] -- 180 | │ └─BasicBlock: 2-8 [16, 512, 7, 7] -- 181 | │ │ └─Conv2d: 3-46 [16, 512, 7, 7] 2,359,296 182 | │ │ └─BatchNorm2d: 3-47 [16, 512, 7, 7] 1,024 183 | │ │ └─ReLU: 3-48 [16, 512, 7, 7] -- 184 | │ │ └─Conv2d: 3-49 [16, 512, 7, 7] 2,359,296 185 | │ │ └─BatchNorm2d: 3-50 [16, 512, 7, 7] 1,024 186 | │ │ └─ReLU: 3-51 [16, 512, 7, 7] -- 187 | ├─AdaptiveAvgPool2d: 1-9 [16, 512, 1, 1] -- 188 | ├─Linear: 1-10 [16, 1000] 513,000 189 | ========================================================================================== 190 | Total params: 11,689,512 191 | Trainable params: 11,689,512 192 | Non-trainable params: 0 193 | Total mult-adds (G): 29.03 194 | ========================================================================================== 195 | Input size (MB): 9.63 196 | Forward/backward pass size (MB): 635.96 197 | Params size (MB): 46.76 198 | Estimated Total Size (MB): 692.35 199 | ========================================================================================== 200 | 201 | ``` 202 |
203 | 204 |
205 | DeepSpeed output 206 | 207 | ```bash 208 | $ python3 example_resnet18.py --profiler deepspeed 209 | 210 | -------------------------- DeepSpeed Flops Profiler -------------------------- 211 | Profile Summary at step 10: 212 | Notations: 213 | data parallel size (dp_size), model parallel size(mp_size), 214 | number of parameters (params), number of multiply-accumulate operations(MACs), 215 | number of floating-point operations (flops), floating-point operations per second (FLOPS), 216 | fwd latency (forward propagation latency), bwd latency (backward propagation latency), 217 | step (weights update latency), iter latency (sum of fwd, bwd and step latency) 218 | 219 | params per gpu: 11.69 M 220 | params of model = params per GPU * mp_size: 11.69 M 221 | fwd MACs per GPU: 29.03 GMACs 222 | fwd flops per GPU: 58.18 G 223 | fwd flops of model = fwd flops per GPU * mp_size: 58.18 G 224 | fwd latency: 76.73 ms 225 | fwd FLOPS per GPU = fwd flops per GPU / fwd latency: 758.27 GFLOPS 226 | 227 | ----------------------------- Aggregated Profile per GPU ----------------------------- 228 | Top 1 modules in terms of params, MACs or fwd latency at different model depths: 229 | depth 0: 230 | params - {'ResNet': '11.69 M'} 231 | MACs - {'ResNet': '29.03 GMACs'} 232 | fwd latency - {'ResNet': '76.73 ms'} 233 | depth 1: 234 | params - {'Sequential': '11.17 M'} 235 | MACs - {'Sequential': '27.13 GMACs'} 236 | fwd latency - {'Sequential': '58.92 ms'} 237 | depth 2: 238 | params - {'BasicBlock': '11.17 M'} 239 | MACs - {'BasicBlock': '27.13 GMACs'} 240 | fwd latency - {'BasicBlock': '58.66 ms'} 241 | depth 3: 242 | params - {'Conv2d': '10.99 M'} 243 | MACs - {'Conv2d': '26.82 GMACs'} 244 | fwd latency - {'Conv2d': '44.56 ms'} 245 | 246 | ------------------------------ Detailed Profile per GPU ------------------------------ 247 | Each module profile is listed after its name in the following order: 248 | params, percentage of total params, MACs, percentage of total MACs, fwd latency, percentage of total fwd latency, fwd FLOPS 249 | 250 | Note: 1. A module can have torch.nn.module or torch.nn.functional to compute logits (e.g. CrossEntropyLoss). They are not counted as submodules, thus not to be printed out. However they make up the difference between a parent's MACs (or latency) and the sum of its submodules'. 251 | 2. Number of floating-point operations is a theoretical estimation, thus FLOPS computed using that could be larger than the maximum system throughput. 252 | 3. The fwd latency listed in the top module's profile is directly captured at the module forward function in PyTorch, thus it's less than the fwd latency shown above which is captured in DeepSpeed. 253 | 254 | ResNet( 255 | 11.69 M, 100.00% Params, 29.03 GMACs, 100.00% MACs, 76.73 ms, 100.00% latency, 758.27 GFLOPS, 256 | (conv1): Conv2d(9.41 k, 0.08% Params, 1.89 GMACs, 6.51% MACs, 10.17 ms, 13.25% latency, 371.35 GFLOPS, 3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False) 257 | (bn1): BatchNorm2d(128, 0.00% Params, 0 MACs, 0.00% MACs, 3.31 ms, 4.31% latency, 7.77 GFLOPS, 64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 258 | (relu): ReLU(0, 0.00% Params, 0 MACs, 0.00% MACs, 715.02 us, 0.93% latency, 17.96 GFLOPS, inplace=True) 259 | (maxpool): MaxPool2d(0, 0.00% Params, 0 MACs, 0.00% MACs, 2.64 ms, 3.44% latency, 4.87 GFLOPS, kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False) 260 | (layer1): Sequential( 261 | 147.97 k, 1.27% Params, 7.4 GMACs, 25.49% MACs, 22.56 ms, 29.40% latency, 657.61 GFLOPS, 262 | (0): BasicBlock( 263 | 73.98 k, 0.63% Params, 3.7 GMACs, 12.75% MACs, 11.39 ms, 14.84% latency, 651.51 GFLOPS, 264 | (conv1): Conv2d(36.86 k, 0.32% Params, 1.85 GMACs, 6.37% MACs, 4.06 ms, 5.29% latency, 911.6 GFLOPS, 64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) 265 | (bn1): BatchNorm2d(128, 0.00% Params, 0 MACs, 0.00% MACs, 1.04 ms, 1.35% latency, 6.18 GFLOPS, 64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 266 | (relu): ReLU(0, 0.00% Params, 0 MACs, 0.00% MACs, 396.73 us, 0.52% latency, 16.19 GFLOPS, inplace=True) 267 | (conv2): Conv2d(36.86 k, 0.32% Params, 1.85 GMACs, 6.37% MACs, 4.26 ms, 5.55% latency, 869.02 GFLOPS, 64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) 268 | (bn2): BatchNorm2d(128, 0.00% Params, 0 MACs, 0.00% MACs, 1.06 ms, 1.38% latency, 6.07 GFLOPS, 64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 269 | ) 270 | (1): BasicBlock( 271 | 73.98 k, 0.63% Params, 3.7 GMACs, 12.75% MACs, 11.11 ms, 14.48% latency, 667.81 GFLOPS, 272 | (conv1): Conv2d(36.86 k, 0.32% Params, 1.85 GMACs, 6.37% MACs, 4.19 ms, 5.47% latency, 882.21 GFLOPS, 64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) 273 | (bn1): BatchNorm2d(128, 0.00% Params, 0 MACs, 0.00% MACs, 558.38 us, 0.73% latency, 11.5 GFLOPS, 64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 274 | (relu): ReLU(0, 0.00% Params, 0 MACs, 0.00% MACs, 372.17 us, 0.49% latency, 17.26 GFLOPS, inplace=True) 275 | (conv2): Conv2d(36.86 k, 0.32% Params, 1.85 GMACs, 6.37% MACs, 4.21 ms, 5.48% latency, 879.61 GFLOPS, 64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) 276 | (bn2): BatchNorm2d(128, 0.00% Params, 0 MACs, 0.00% MACs, 1.04 ms, 1.36% latency, 6.15 GFLOPS, 64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 277 | ) 278 | ) 279 | (layer2): Sequential( 280 | 525.57 k, 4.50% Params, 6.58 GMACs, 22.66% MACs, 13.32 ms, 17.36% latency, 989.34 GFLOPS, 281 | (0): BasicBlock( 282 | 230.14 k, 1.97% Params, 2.88 GMACs, 9.91% MACs, 8.28 ms, 10.79% latency, 696.87 GFLOPS, 283 | (conv1): Conv2d(73.73 k, 0.63% Params, 924.84 MMACs, 3.19% MACs, 3.26 ms, 4.25% latency, 567.24 GFLOPS, 64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) 284 | (bn1): BatchNorm2d(256, 0.00% Params, 0 MACs, 0.00% MACs, 190.02 us, 0.25% latency, 16.9 GFLOPS, 128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 285 | (relu): ReLU(0, 0.00% Params, 0 MACs, 0.00% MACs, 209.33 us, 0.27% latency, 15.34 GFLOPS, inplace=True) 286 | (conv2): Conv2d(147.46 k, 1.26% Params, 1.85 GMACs, 6.37% MACs, 2.34 ms, 3.05% latency, 1.58 TFLOPS, 128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) 287 | (bn2): BatchNorm2d(256, 0.00% Params, 0 MACs, 0.00% MACs, 133.75 us, 0.17% latency, 24.01 GFLOPS, 128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 288 | (downsample): Sequential( 289 | 8.45 k, 0.07% Params, 102.76 MMACs, 0.35% MACs, 1.79 ms, 2.34% latency, 116.44 GFLOPS, 290 | (0): Conv2d(8.19 k, 0.07% Params, 102.76 MMACs, 0.35% MACs, 1.57 ms, 2.05% latency, 130.73 GFLOPS, 64, 128, kernel_size=(1, 1), stride=(2, 2), bias=False) 291 | (1): BatchNorm2d(256, 0.00% Params, 0 MACs, 0.00% MACs, 154.73 us, 0.20% latency, 20.75 GFLOPS, 128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 292 | ) 293 | ) 294 | (1): BasicBlock( 295 | 295.42 k, 2.53% Params, 3.7 GMACs, 12.75% MACs, 4.98 ms, 6.49% latency, 1.49 TFLOPS, 296 | (conv1): Conv2d(147.46 k, 1.26% Params, 1.85 GMACs, 6.37% MACs, 2.19 ms, 2.86% latency, 1.69 TFLOPS, 128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) 297 | (bn1): BatchNorm2d(256, 0.00% Params, 0 MACs, 0.00% MACs, 128.98 us, 0.17% latency, 24.9 GFLOPS, 128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 298 | (relu): ReLU(0, 0.00% Params, 0 MACs, 0.00% MACs, 186.92 us, 0.24% latency, 17.18 GFLOPS, inplace=True) 299 | (conv2): Conv2d(147.46 k, 1.26% Params, 1.85 GMACs, 6.37% MACs, 2.03 ms, 2.64% latency, 1.83 TFLOPS, 128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) 300 | (bn2): BatchNorm2d(256, 0.00% Params, 0 MACs, 0.00% MACs, 157.12 us, 0.20% latency, 20.44 GFLOPS, 128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 301 | ) 302 | ) 303 | (layer3): Sequential( 304 | 2.1 M, 17.96% Params, 6.58 GMACs, 22.66% MACs, 10.45 ms, 13.62% latency, 1.26 TFLOPS, 305 | (0): BasicBlock( 306 | 919.04 k, 7.86% Params, 2.88 GMACs, 9.91% MACs, 5.77 ms, 7.52% latency, 998.82 GFLOPS, 307 | (conv1): Conv2d(294.91 k, 2.52% Params, 924.84 MMACs, 3.19% MACs, 1.73 ms, 2.25% latency, 1.07 TFLOPS, 128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) 308 | (bn1): BatchNorm2d(512, 0.00% Params, 0 MACs, 0.00% MACs, 125.41 us, 0.16% latency, 12.8 GFLOPS, 256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 309 | (relu): ReLU(0, 0.00% Params, 0 MACs, 0.00% MACs, 232.93 us, 0.30% latency, 6.89 GFLOPS, inplace=True) 310 | (conv2): Conv2d(589.82 k, 5.05% Params, 1.85 GMACs, 6.37% MACs, 2.17 ms, 2.83% latency, 1.7 TFLOPS, 256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) 311 | (bn2): BatchNorm2d(512, 0.00% Params, 0 MACs, 0.00% MACs, 108.24 us, 0.14% latency, 14.83 GFLOPS, 256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 312 | (downsample): Sequential( 313 | 33.28 k, 0.28% Params, 102.76 MMACs, 0.35% MACs, 1.07 ms, 1.39% latency, 193.87 GFLOPS, 314 | (0): Conv2d(32.77 k, 0.28% Params, 102.76 MMACs, 0.35% MACs, 895.26 us, 1.17% latency, 229.57 GFLOPS, 128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False) 315 | (1): BatchNorm2d(512, 0.00% Params, 0 MACs, 0.00% MACs, 109.91 us, 0.14% latency, 14.61 GFLOPS, 256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 316 | ) 317 | ) 318 | (1): BasicBlock( 319 | 1.18 M, 10.10% Params, 3.7 GMACs, 12.75% MACs, 4.62 ms, 6.03% latency, 1.6 TFLOPS, 320 | (conv1): Conv2d(589.82 k, 5.05% Params, 1.85 GMACs, 6.37% MACs, 2.06 ms, 2.68% latency, 1.8 TFLOPS, 256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) 321 | (bn1): BatchNorm2d(512, 0.00% Params, 0 MACs, 0.00% MACs, 104.43 us, 0.14% latency, 15.38 GFLOPS, 256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 322 | (relu): ReLU(0, 0.00% Params, 0 MACs, 0.00% MACs, 185.01 us, 0.24% latency, 8.68 GFLOPS, inplace=True) 323 | (conv2): Conv2d(589.82 k, 5.05% Params, 1.85 GMACs, 6.37% MACs, 1.89 ms, 2.46% latency, 1.96 TFLOPS, 256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) 324 | (bn2): BatchNorm2d(512, 0.00% Params, 0 MACs, 0.00% MACs, 98.94 us, 0.13% latency, 16.23 GFLOPS, 256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 325 | ) 326 | ) 327 | (layer4): Sequential( 328 | 8.39 M, 71.81% Params, 6.58 GMACs, 22.66% MACs, 12.59 ms, 16.41% latency, 1.04 TFLOPS, 329 | (0): BasicBlock( 330 | 3.67 M, 31.42% Params, 2.88 GMACs, 9.91% MACs, 6.37 ms, 8.30% latency, 903.78 GFLOPS, 331 | (conv1): Conv2d(1.18 M, 10.09% Params, 924.84 MMACs, 3.19% MACs, 1.91 ms, 2.48% latency, 970.5 GFLOPS, 256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False) 332 | (bn1): BatchNorm2d(1.02 k, 0.01% Params, 0 MACs, 0.00% MACs, 135.42 us, 0.18% latency, 5.93 GFLOPS, 512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 333 | (relu): ReLU(0, 0.00% Params, 0 MACs, 0.00% MACs, 168.8 us, 0.22% latency, 4.76 GFLOPS, inplace=True) 334 | (conv2): Conv2d(2.36 M, 20.18% Params, 1.85 GMACs, 6.37% MACs, 2.92 ms, 3.81% latency, 1.27 TFLOPS, 512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) 335 | (bn2): BatchNorm2d(1.02 k, 0.01% Params, 0 MACs, 0.00% MACs, 107.29 us, 0.14% latency, 7.48 GFLOPS, 512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 336 | (downsample): Sequential( 337 | 132.1 k, 1.13% Params, 102.76 MMACs, 0.35% MACs, 875.95 us, 1.14% latency, 235.54 GFLOPS, 338 | (0): Conv2d(131.07 k, 1.12% Params, 102.76 MMACs, 0.35% MACs, 699.04 us, 0.91% latency, 294.0 GFLOPS, 256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False) 339 | (1): BatchNorm2d(1.02 k, 0.01% Params, 0 MACs, 0.00% MACs, 106.57 us, 0.14% latency, 7.53 GFLOPS, 512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 340 | ) 341 | ) 342 | (1): BasicBlock( 343 | 4.72 M, 40.38% Params, 3.7 GMACs, 12.75% MACs, 6.15 ms, 8.01% latency, 1.2 TFLOPS, 344 | (conv1): Conv2d(2.36 M, 20.18% Params, 1.85 GMACs, 6.37% MACs, 2.63 ms, 3.42% latency, 1.41 TFLOPS, 512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) 345 | (bn1): BatchNorm2d(1.02 k, 0.01% Params, 0 MACs, 0.00% MACs, 106.81 us, 0.14% latency, 7.52 GFLOPS, 512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 346 | (relu): ReLU(0, 0.00% Params, 0 MACs, 0.00% MACs, 239.13 us, 0.31% latency, 3.36 GFLOPS, inplace=True) 347 | (conv2): Conv2d(2.36 M, 20.18% Params, 1.85 GMACs, 6.37% MACs, 2.72 ms, 3.55% latency, 1.36 TFLOPS, 512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False) 348 | (bn2): BatchNorm2d(1.02 k, 0.01% Params, 0 MACs, 0.00% MACs, 164.99 us, 0.22% latency, 4.87 GFLOPS, 512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) 349 | ) 350 | ) 351 | (avgpool): AdaptiveAvgPool2d(0, 0.00% Params, 0 MACs, 0.00% MACs, 229.36 us, 0.30% latency, 1.75 GFLOPS, output_size=(1, 1)) 352 | (fc): Linear(513.0 k, 4.39% Params, 8.19 MMACs, 0.03% MACs, 335.45 us, 0.44% latency, 48.84 GFLOPS, in_features=512, out_features=1000, bias=True) 353 | ) 354 | ------------------------------------------------------------------------------ 355 | 356 | ``` 357 |
358 | 359 | 360 | ### BERT example 361 | 362 |
363 | Profiler output 364 | 365 | ```bash 366 | $ python3 example_bert.py 367 | (model): BertForSequenceClassification, num_params 109483778, 315.584ms, 100.0% latency, input shape [], output shape [2, 2] 368 | (bert): BertModel, num_params 109482240, 313.903ms, 99.467% latency, input shape [2, 512], output shape [2, 768] 369 | (embeddings): BertEmbeddings, num_params 23837184, 5.712ms, 1.81% latency, input shape [], output shape [2, 512, 768] 370 | (word_embeddings): Embedding, num_params 23440896, 2.5ms, 0.792% latency, input shape [2, 512], output shape [2, 512, 768] 371 | (position_embeddings): Embedding, num_params 393216, 0.384ms, 0.122% latency, input shape [1, 512], output shape [1, 512, 768] 372 | (token_type_embeddings): Embedding, num_params 1536, 0.389ms, 0.123% latency, input shape [2, 512], output shape [2, 512, 768] 373 | (LayerNorm): LayerNorm, num_params 1536, 0.88ms, 0.279% latency, input shape [2, 512, 768], output shape [2, 512, 768] 374 | (dropout): Dropout, num_params 0, 0.337ms, 0.107% latency, input shape [2, 512, 768], output shape [2, 512, 768] 375 | (encoder): BertEncoder, num_params 85054464, 306.437ms, 97.102% latency, input shape [2, 512, 768], output shape [2, 512, 768] 376 | (layer): ModuleList, num_params 85054464, 305.364ms, 96.762% latency, input shape None, output shape None 377 | (0): BertLayer, num_params 7087872, 32.938ms, 10.437% latency, input shape [2, 512, 768], output shape [2, 512, 768] 378 | (attention): BertAttention, num_params 2363904, 18.835ms, 5.968% latency, input shape [2, 512, 768], output shape [2, 512, 768] 379 | (self): BertSelfAttention, num_params 1771776, 16.574ms, 5.252% latency, input shape [2, 512, 768], output shape [2, 512, 768] 380 | (query): Linear, num_params 590592, 1.801ms, 0.571% latency, input shape [2, 512, 768], output shape [2, 512, 768] 381 | (key): Linear, num_params 590592, 1.282ms, 0.406% latency, input shape [2, 512, 768], output shape [2, 512, 768] 382 | (value): Linear, num_params 590592, 1.224ms, 0.388% latency, input shape [2, 512, 768], output shape [2, 512, 768] 383 | (dropout): Dropout, num_params 0, 0.122ms, 0.039% latency, input shape [2, 12, 512, 512], output shape [2, 12, 512, 512] 384 | (output): BertSelfOutput, num_params 592128, 2.061ms, 0.653% latency, input shape [2, 512, 768], output shape [2, 512, 768] 385 | (dense): Linear, num_params 590592, 1.263ms, 0.4% latency, input shape [2, 512, 768], output shape [2, 512, 768] 386 | (LayerNorm): LayerNorm, num_params 1536, 0.337ms, 0.107% latency, input shape [2, 512, 768], output shape [2, 512, 768] 387 | (dropout): Dropout, num_params 0, 0.079ms, 0.025% latency, input shape [2, 512, 768], output shape [2, 512, 768] 388 | (intermediate): BertIntermediate, num_params 2362368, 8.577ms, 2.718% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 389 | (dense): Linear, num_params 2362368, 2.989ms, 0.947% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 390 | (intermediate_act_fn): GELUActivation, num_params 0, 5.406ms, 1.713% latency, input shape [2, 512, 3072], output shape [2, 512, 3072] 391 | (output): BertOutput, num_params 2361600, 4.773ms, 1.512% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 392 | (dense): Linear, num_params 2360064, 3.921ms, 1.242% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 393 | (LayerNorm): LayerNorm, num_params 1536, 0.376ms, 0.119% latency, input shape [2, 512, 768], output shape [2, 512, 768] 394 | (dropout): Dropout, num_params 0, 0.082ms, 0.026% latency, input shape [2, 512, 768], output shape [2, 512, 768] 395 | (1): BertLayer, num_params 7087872, 21.594ms, 6.842% latency, input shape [2, 512, 768], output shape [2, 512, 768] 396 | (attention): BertAttention, num_params 2363904, 13.891ms, 4.402% latency, input shape [2, 512, 768], output shape [2, 512, 768] 397 | (self): BertSelfAttention, num_params 1771776, 11.753ms, 3.724% latency, input shape [2, 512, 768], output shape [2, 512, 768] 398 | (query): Linear, num_params 590592, 1.061ms, 0.336% latency, input shape [2, 512, 768], output shape [2, 512, 768] 399 | (key): Linear, num_params 590592, 0.863ms, 0.273% latency, input shape [2, 512, 768], output shape [2, 512, 768] 400 | (value): Linear, num_params 590592, 0.836ms, 0.265% latency, input shape [2, 512, 768], output shape [2, 512, 768] 401 | (dropout): Dropout, num_params 0, 0.115ms, 0.037% latency, input shape [2, 12, 512, 512], output shape [2, 12, 512, 512] 402 | (output): BertSelfOutput, num_params 592128, 2.002ms, 0.634% latency, input shape [2, 512, 768], output shape [2, 512, 768] 403 | (dense): Linear, num_params 590592, 1.146ms, 0.363% latency, input shape [2, 512, 768], output shape [2, 512, 768] 404 | (LayerNorm): LayerNorm, num_params 1536, 0.401ms, 0.127% latency, input shape [2, 512, 768], output shape [2, 512, 768] 405 | (dropout): Dropout, num_params 0, 0.086ms, 0.027% latency, input shape [2, 512, 768], output shape [2, 512, 768] 406 | (intermediate): BertIntermediate, num_params 2362368, 3.405ms, 1.079% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 407 | (dense): Linear, num_params 2362368, 2.816ms, 0.892% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 408 | (intermediate_act_fn): GELUActivation, num_params 0, 0.463ms, 0.147% latency, input shape [2, 512, 3072], output shape [2, 512, 3072] 409 | (output): BertOutput, num_params 2361600, 4.034ms, 1.278% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 410 | (dense): Linear, num_params 2360064, 3.286ms, 1.041% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 411 | (LayerNorm): LayerNorm, num_params 1536, 0.348ms, 0.11% latency, input shape [2, 512, 768], output shape [2, 512, 768] 412 | (dropout): Dropout, num_params 0, 0.08ms, 0.025% latency, input shape [2, 512, 768], output shape [2, 512, 768] 413 | (2): BertLayer, num_params 7087872, 27.773ms, 8.801% latency, input shape [2, 512, 768], output shape [2, 512, 768] 414 | (attention): BertAttention, num_params 2363904, 19.182ms, 6.078% latency, input shape [2, 512, 768], output shape [2, 512, 768] 415 | (self): BertSelfAttention, num_params 1771776, 16.907ms, 5.357% latency, input shape [2, 512, 768], output shape [2, 512, 768] 416 | (query): Linear, num_params 590592, 0.955ms, 0.303% latency, input shape [2, 512, 768], output shape [2, 512, 768] 417 | (key): Linear, num_params 590592, 0.828ms, 0.262% latency, input shape [2, 512, 768], output shape [2, 512, 768] 418 | (value): Linear, num_params 590592, 0.837ms, 0.265% latency, input shape [2, 512, 768], output shape [2, 512, 768] 419 | (dropout): Dropout, num_params 0, 0.113ms, 0.036% latency, input shape [2, 12, 512, 512], output shape [2, 12, 512, 512] 420 | (output): BertSelfOutput, num_params 592128, 2.152ms, 0.682% latency, input shape [2, 512, 768], output shape [2, 512, 768] 421 | (dense): Linear, num_params 590592, 1.292ms, 0.409% latency, input shape [2, 512, 768], output shape [2, 512, 768] 422 | (LayerNorm): LayerNorm, num_params 1536, 0.376ms, 0.119% latency, input shape [2, 512, 768], output shape [2, 512, 768] 423 | (dropout): Dropout, num_params 0, 0.081ms, 0.026% latency, input shape [2, 512, 768], output shape [2, 512, 768] 424 | (intermediate): BertIntermediate, num_params 2362368, 3.928ms, 1.245% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 425 | (dense): Linear, num_params 2362368, 3.087ms, 0.978% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 426 | (intermediate_act_fn): GELUActivation, num_params 0, 0.709ms, 0.225% latency, input shape [2, 512, 3072], output shape [2, 512, 3072] 427 | (output): BertOutput, num_params 2361600, 4.395ms, 1.393% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 428 | (dense): Linear, num_params 2360064, 3.597ms, 1.14% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 429 | (LayerNorm): LayerNorm, num_params 1536, 0.309ms, 0.098% latency, input shape [2, 512, 768], output shape [2, 512, 768] 430 | (dropout): Dropout, num_params 0, 0.08ms, 0.025% latency, input shape [2, 512, 768], output shape [2, 512, 768] 431 | (3): BertLayer, num_params 7087872, 23.726ms, 7.518% latency, input shape [2, 512, 768], output shape [2, 512, 768] 432 | (attention): BertAttention, num_params 2363904, 15.232ms, 4.826% latency, input shape [2, 512, 768], output shape [2, 512, 768] 433 | (self): BertSelfAttention, num_params 1771776, 12.968ms, 4.109% latency, input shape [2, 512, 768], output shape [2, 512, 768] 434 | (query): Linear, num_params 590592, 1.2ms, 0.38% latency, input shape [2, 512, 768], output shape [2, 512, 768] 435 | (key): Linear, num_params 590592, 0.949ms, 0.301% latency, input shape [2, 512, 768], output shape [2, 512, 768] 436 | (value): Linear, num_params 590592, 0.92ms, 0.291% latency, input shape [2, 512, 768], output shape [2, 512, 768] 437 | (dropout): Dropout, num_params 0, 0.123ms, 0.039% latency, input shape [2, 12, 512, 512], output shape [2, 12, 512, 512] 438 | (output): BertSelfOutput, num_params 592128, 2.135ms, 0.676% latency, input shape [2, 512, 768], output shape [2, 512, 768] 439 | (dense): Linear, num_params 590592, 1.271ms, 0.403% latency, input shape [2, 512, 768], output shape [2, 512, 768] 440 | (LayerNorm): LayerNorm, num_params 1536, 0.35ms, 0.111% latency, input shape [2, 512, 768], output shape [2, 512, 768] 441 | (dropout): Dropout, num_params 0, 0.076ms, 0.024% latency, input shape [2, 512, 768], output shape [2, 512, 768] 442 | (intermediate): BertIntermediate, num_params 2362368, 3.844ms, 1.218% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 443 | (dense): Linear, num_params 2362368, 3.025ms, 0.958% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 444 | (intermediate_act_fn): GELUActivation, num_params 0, 0.696ms, 0.22% latency, input shape [2, 512, 3072], output shape [2, 512, 3072] 445 | (output): BertOutput, num_params 2361600, 4.381ms, 1.388% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 446 | (dense): Linear, num_params 2360064, 3.562ms, 1.129% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 447 | (LayerNorm): LayerNorm, num_params 1536, 0.322ms, 0.102% latency, input shape [2, 512, 768], output shape [2, 512, 768] 448 | (dropout): Dropout, num_params 0, 0.094ms, 0.03% latency, input shape [2, 512, 768], output shape [2, 512, 768] 449 | (4): BertLayer, num_params 7087872, 24.349ms, 7.715% latency, input shape [2, 512, 768], output shape [2, 512, 768] 450 | (attention): BertAttention, num_params 2363904, 15.118ms, 4.79% latency, input shape [2, 512, 768], output shape [2, 512, 768] 451 | (self): BertSelfAttention, num_params 1771776, 12.685ms, 4.02% latency, input shape [2, 512, 768], output shape [2, 512, 768] 452 | (query): Linear, num_params 590592, 1.223ms, 0.388% latency, input shape [2, 512, 768], output shape [2, 512, 768] 453 | (key): Linear, num_params 590592, 0.968ms, 0.307% latency, input shape [2, 512, 768], output shape [2, 512, 768] 454 | (value): Linear, num_params 590592, 0.944ms, 0.299% latency, input shape [2, 512, 768], output shape [2, 512, 768] 455 | (dropout): Dropout, num_params 0, 0.122ms, 0.039% latency, input shape [2, 12, 512, 512], output shape [2, 12, 512, 512] 456 | (output): BertSelfOutput, num_params 592128, 2.308ms, 0.731% latency, input shape [2, 512, 768], output shape [2, 512, 768] 457 | (dense): Linear, num_params 590592, 1.48ms, 0.469% latency, input shape [2, 512, 768], output shape [2, 512, 768] 458 | (LayerNorm): LayerNorm, num_params 1536, 0.333ms, 0.106% latency, input shape [2, 512, 768], output shape [2, 512, 768] 459 | (dropout): Dropout, num_params 0, 0.078ms, 0.025% latency, input shape [2, 512, 768], output shape [2, 512, 768] 460 | (intermediate): BertIntermediate, num_params 2362368, 4.471ms, 1.417% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 461 | (dense): Linear, num_params 2362368, 3.678ms, 1.165% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 462 | (intermediate_act_fn): GELUActivation, num_params 0, 0.669ms, 0.212% latency, input shape [2, 512, 3072], output shape [2, 512, 3072] 463 | (output): BertOutput, num_params 2361600, 4.484ms, 1.421% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 464 | (dense): Linear, num_params 2360064, 3.712ms, 1.176% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 465 | (LayerNorm): LayerNorm, num_params 1536, 0.322ms, 0.102% latency, input shape [2, 512, 768], output shape [2, 512, 768] 466 | (dropout): Dropout, num_params 0, 0.083ms, 0.026% latency, input shape [2, 512, 768], output shape [2, 512, 768] 467 | (5): BertLayer, num_params 7087872, 24.154ms, 7.654% latency, input shape [2, 512, 768], output shape [2, 512, 768] 468 | (attention): BertAttention, num_params 2363904, 15.06ms, 4.772% latency, input shape [2, 512, 768], output shape [2, 512, 768] 469 | (self): BertSelfAttention, num_params 1771776, 12.672ms, 4.015% latency, input shape [2, 512, 768], output shape [2, 512, 768] 470 | (query): Linear, num_params 590592, 1.21ms, 0.383% latency, input shape [2, 512, 768], output shape [2, 512, 768] 471 | (key): Linear, num_params 590592, 0.941ms, 0.298% latency, input shape [2, 512, 768], output shape [2, 512, 768] 472 | (value): Linear, num_params 590592, 0.977ms, 0.31% latency, input shape [2, 512, 768], output shape [2, 512, 768] 473 | (dropout): Dropout, num_params 0, 0.116ms, 0.037% latency, input shape [2, 12, 512, 512], output shape [2, 12, 512, 512] 474 | (output): BertSelfOutput, num_params 592128, 2.26ms, 0.716% latency, input shape [2, 512, 768], output shape [2, 512, 768] 475 | (dense): Linear, num_params 590592, 1.427ms, 0.452% latency, input shape [2, 512, 768], output shape [2, 512, 768] 476 | (LayerNorm): LayerNorm, num_params 1536, 0.347ms, 0.11% latency, input shape [2, 512, 768], output shape [2, 512, 768] 477 | (dropout): Dropout, num_params 0, 0.077ms, 0.025% latency, input shape [2, 512, 768], output shape [2, 512, 768] 478 | (intermediate): BertIntermediate, num_params 2362368, 4.27ms, 1.353% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 479 | (dense): Linear, num_params 2362368, 3.491ms, 1.106% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 480 | (intermediate_act_fn): GELUActivation, num_params 0, 0.663ms, 0.21% latency, input shape [2, 512, 3072], output shape [2, 512, 3072] 481 | (output): BertOutput, num_params 2361600, 4.555ms, 1.444% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 482 | (dense): Linear, num_params 2360064, 3.763ms, 1.192% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 483 | (LayerNorm): LayerNorm, num_params 1536, 0.337ms, 0.107% latency, input shape [2, 512, 768], output shape [2, 512, 768] 484 | (dropout): Dropout, num_params 0, 0.083ms, 0.026% latency, input shape [2, 512, 768], output shape [2, 512, 768] 485 | (6): BertLayer, num_params 7087872, 23.972ms, 7.596% latency, input shape [2, 512, 768], output shape [2, 512, 768] 486 | (attention): BertAttention, num_params 2363904, 15.142ms, 4.798% latency, input shape [2, 512, 768], output shape [2, 512, 768] 487 | (self): BertSelfAttention, num_params 1771776, 12.743ms, 4.038% latency, input shape [2, 512, 768], output shape [2, 512, 768] 488 | (query): Linear, num_params 590592, 1.214ms, 0.385% latency, input shape [2, 512, 768], output shape [2, 512, 768] 489 | (key): Linear, num_params 590592, 0.991ms, 0.314% latency, input shape [2, 512, 768], output shape [2, 512, 768] 490 | (value): Linear, num_params 590592, 0.921ms, 0.292% latency, input shape [2, 512, 768], output shape [2, 512, 768] 491 | (dropout): Dropout, num_params 0, 0.117ms, 0.037% latency, input shape [2, 12, 512, 512], output shape [2, 12, 512, 512] 492 | (output): BertSelfOutput, num_params 592128, 2.269ms, 0.719% latency, input shape [2, 512, 768], output shape [2, 512, 768] 493 | (dense): Linear, num_params 590592, 1.436ms, 0.455% latency, input shape [2, 512, 768], output shape [2, 512, 768] 494 | (LayerNorm): LayerNorm, num_params 1536, 0.315ms, 0.1% latency, input shape [2, 512, 768], output shape [2, 512, 768] 495 | (dropout): Dropout, num_params 0, 0.083ms, 0.026% latency, input shape [2, 512, 768], output shape [2, 512, 768] 496 | (intermediate): BertIntermediate, num_params 2362368, 4.147ms, 1.314% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 497 | (dense): Linear, num_params 2362368, 3.41ms, 1.081% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 498 | (intermediate_act_fn): GELUActivation, num_params 0, 0.623ms, 0.197% latency, input shape [2, 512, 3072], output shape [2, 512, 3072] 499 | (output): BertOutput, num_params 2361600, 4.385ms, 1.39% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 500 | (dense): Linear, num_params 2360064, 3.605ms, 1.142% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 501 | (LayerNorm): LayerNorm, num_params 1536, 0.334ms, 0.106% latency, input shape [2, 512, 768], output shape [2, 512, 768] 502 | (dropout): Dropout, num_params 0, 0.08ms, 0.025% latency, input shape [2, 512, 768], output shape [2, 512, 768] 503 | (7): BertLayer, num_params 7087872, 25.682ms, 8.138% latency, input shape [2, 512, 768], output shape [2, 512, 768] 504 | (attention): BertAttention, num_params 2363904, 15.514ms, 4.916% latency, input shape [2, 512, 768], output shape [2, 512, 768] 505 | (self): BertSelfAttention, num_params 1771776, 12.894ms, 4.086% latency, input shape [2, 512, 768], output shape [2, 512, 768] 506 | (query): Linear, num_params 590592, 1.2ms, 0.38% latency, input shape [2, 512, 768], output shape [2, 512, 768] 507 | (key): Linear, num_params 590592, 0.946ms, 0.3% latency, input shape [2, 512, 768], output shape [2, 512, 768] 508 | (value): Linear, num_params 590592, 0.967ms, 0.306% latency, input shape [2, 512, 768], output shape [2, 512, 768] 509 | (dropout): Dropout, num_params 0, 0.117ms, 0.037% latency, input shape [2, 12, 512, 512], output shape [2, 12, 512, 512] 510 | (output): BertSelfOutput, num_params 592128, 2.495ms, 0.791% latency, input shape [2, 512, 768], output shape [2, 512, 768] 511 | (dense): Linear, num_params 590592, 1.601ms, 0.507% latency, input shape [2, 512, 768], output shape [2, 512, 768] 512 | (LayerNorm): LayerNorm, num_params 1536, 0.361ms, 0.115% latency, input shape [2, 512, 768], output shape [2, 512, 768] 513 | (dropout): Dropout, num_params 0, 0.087ms, 0.028% latency, input shape [2, 512, 768], output shape [2, 512, 768] 514 | (intermediate): BertIntermediate, num_params 2362368, 4.638ms, 1.47% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 515 | (dense): Linear, num_params 2362368, 3.801ms, 1.204% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 516 | (intermediate_act_fn): GELUActivation, num_params 0, 0.707ms, 0.224% latency, input shape [2, 512, 3072], output shape [2, 512, 3072] 517 | (output): BertOutput, num_params 2361600, 5.265ms, 1.668% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 518 | (dense): Linear, num_params 2360064, 4.463ms, 1.414% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 519 | (LayerNorm): LayerNorm, num_params 1536, 0.326ms, 0.103% latency, input shape [2, 512, 768], output shape [2, 512, 768] 520 | (dropout): Dropout, num_params 0, 0.07ms, 0.022% latency, input shape [2, 512, 768], output shape [2, 512, 768] 521 | (8): BertLayer, num_params 7087872, 25.846ms, 8.19% latency, input shape [2, 512, 768], output shape [2, 512, 768] 522 | (attention): BertAttention, num_params 2363904, 16.32ms, 5.171% latency, input shape [2, 512, 768], output shape [2, 512, 768] 523 | (self): BertSelfAttention, num_params 1771776, 13.755ms, 4.359% latency, input shape [2, 512, 768], output shape [2, 512, 768] 524 | (query): Linear, num_params 590592, 1.379ms, 0.437% latency, input shape [2, 512, 768], output shape [2, 512, 768] 525 | (key): Linear, num_params 590592, 1.0ms, 0.317% latency, input shape [2, 512, 768], output shape [2, 512, 768] 526 | (value): Linear, num_params 590592, 0.955ms, 0.303% latency, input shape [2, 512, 768], output shape [2, 512, 768] 527 | (dropout): Dropout, num_params 0, 0.122ms, 0.039% latency, input shape [2, 12, 512, 512], output shape [2, 12, 512, 512] 528 | (output): BertSelfOutput, num_params 592128, 2.437ms, 0.772% latency, input shape [2, 512, 768], output shape [2, 512, 768] 529 | (dense): Linear, num_params 590592, 1.585ms, 0.502% latency, input shape [2, 512, 768], output shape [2, 512, 768] 530 | (LayerNorm): LayerNorm, num_params 1536, 0.32ms, 0.101% latency, input shape [2, 512, 768], output shape [2, 512, 768] 531 | (dropout): Dropout, num_params 0, 0.077ms, 0.024% latency, input shape [2, 512, 768], output shape [2, 512, 768] 532 | (intermediate): BertIntermediate, num_params 2362368, 4.637ms, 1.469% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 533 | (dense): Linear, num_params 2362368, 3.82ms, 1.211% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 534 | (intermediate_act_fn): GELUActivation, num_params 0, 0.7ms, 0.222% latency, input shape [2, 512, 3072], output shape [2, 512, 3072] 535 | (output): BertOutput, num_params 2361600, 4.624ms, 1.465% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 536 | (dense): Linear, num_params 2360064, 3.83ms, 1.214% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 537 | (LayerNorm): LayerNorm, num_params 1536, 0.311ms, 0.099% latency, input shape [2, 512, 768], output shape [2, 512, 768] 538 | (dropout): Dropout, num_params 0, 0.065ms, 0.021% latency, input shape [2, 512, 768], output shape [2, 512, 768] 539 | (9): BertLayer, num_params 7087872, 25.207ms, 7.987% latency, input shape [2, 512, 768], output shape [2, 512, 768] 540 | (attention): BertAttention, num_params 2363904, 15.782ms, 5.001% latency, input shape [2, 512, 768], output shape [2, 512, 768] 541 | (self): BertSelfAttention, num_params 1771776, 13.32ms, 4.221% latency, input shape [2, 512, 768], output shape [2, 512, 768] 542 | (query): Linear, num_params 590592, 1.251ms, 0.397% latency, input shape [2, 512, 768], output shape [2, 512, 768] 543 | (key): Linear, num_params 590592, 0.955ms, 0.303% latency, input shape [2, 512, 768], output shape [2, 512, 768] 544 | (value): Linear, num_params 590592, 0.988ms, 0.313% latency, input shape [2, 512, 768], output shape [2, 512, 768] 545 | (dropout): Dropout, num_params 0, 0.121ms, 0.038% latency, input shape [2, 12, 512, 512], output shape [2, 12, 512, 512] 546 | (output): BertSelfOutput, num_params 592128, 2.333ms, 0.739% latency, input shape [2, 512, 768], output shape [2, 512, 768] 547 | (dense): Linear, num_params 590592, 1.469ms, 0.465% latency, input shape [2, 512, 768], output shape [2, 512, 768] 548 | (LayerNorm): LayerNorm, num_params 1536, 0.333ms, 0.106% latency, input shape [2, 512, 768], output shape [2, 512, 768] 549 | (dropout): Dropout, num_params 0, 0.088ms, 0.028% latency, input shape [2, 512, 768], output shape [2, 512, 768] 550 | (intermediate): BertIntermediate, num_params 2362368, 4.586ms, 1.453% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 551 | (dense): Linear, num_params 2362368, 3.806ms, 1.206% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 552 | (intermediate_act_fn): GELUActivation, num_params 0, 0.661ms, 0.209% latency, input shape [2, 512, 3072], output shape [2, 512, 3072] 553 | (output): BertOutput, num_params 2361600, 4.559ms, 1.445% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 554 | (dense): Linear, num_params 2360064, 3.772ms, 1.195% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 555 | (LayerNorm): LayerNorm, num_params 1536, 0.331ms, 0.105% latency, input shape [2, 512, 768], output shape [2, 512, 768] 556 | (dropout): Dropout, num_params 0, 0.076ms, 0.024% latency, input shape [2, 512, 768], output shape [2, 512, 768] 557 | (10): BertLayer, num_params 7087872, 25.231ms, 7.995% latency, input shape [2, 512, 768], output shape [2, 512, 768] 558 | (attention): BertAttention, num_params 2363904, 15.882ms, 5.033% latency, input shape [2, 512, 768], output shape [2, 512, 768] 559 | (self): BertSelfAttention, num_params 1771776, 13.411ms, 4.249% latency, input shape [2, 512, 768], output shape [2, 512, 768] 560 | (query): Linear, num_params 590592, 1.235ms, 0.391% latency, input shape [2, 512, 768], output shape [2, 512, 768] 561 | (key): Linear, num_params 590592, 0.947ms, 0.3% latency, input shape [2, 512, 768], output shape [2, 512, 768] 562 | (value): Linear, num_params 590592, 0.918ms, 0.291% latency, input shape [2, 512, 768], output shape [2, 512, 768] 563 | (dropout): Dropout, num_params 0, 0.119ms, 0.038% latency, input shape [2, 12, 512, 512], output shape [2, 12, 512, 512] 564 | (output): BertSelfOutput, num_params 592128, 2.336ms, 0.74% latency, input shape [2, 512, 768], output shape [2, 512, 768] 565 | (dense): Linear, num_params 590592, 1.504ms, 0.477% latency, input shape [2, 512, 768], output shape [2, 512, 768] 566 | (LayerNorm): LayerNorm, num_params 1536, 0.336ms, 0.107% latency, input shape [2, 512, 768], output shape [2, 512, 768] 567 | (dropout): Dropout, num_params 0, 0.077ms, 0.024% latency, input shape [2, 512, 768], output shape [2, 512, 768] 568 | (intermediate): BertIntermediate, num_params 2362368, 4.444ms, 1.408% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 569 | (dense): Linear, num_params 2362368, 3.681ms, 1.167% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 570 | (intermediate_act_fn): GELUActivation, num_params 0, 0.646ms, 0.205% latency, input shape [2, 512, 3072], output shape [2, 512, 3072] 571 | (output): BertOutput, num_params 2361600, 4.64ms, 1.47% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 572 | (dense): Linear, num_params 2360064, 3.849ms, 1.22% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 573 | (LayerNorm): LayerNorm, num_params 1536, 0.332ms, 0.105% latency, input shape [2, 512, 768], output shape [2, 512, 768] 574 | (dropout): Dropout, num_params 0, 0.078ms, 0.025% latency, input shape [2, 512, 768], output shape [2, 512, 768] 575 | (11): BertLayer, num_params 7087872, 24.892ms, 7.887% latency, input shape [2, 512, 768], output shape [2, 512, 768] 576 | (attention): BertAttention, num_params 2363904, 15.747ms, 4.99% latency, input shape [2, 512, 768], output shape [2, 512, 768] 577 | (self): BertSelfAttention, num_params 1771776, 13.273ms, 4.206% latency, input shape [2, 512, 768], output shape [2, 512, 768] 578 | (query): Linear, num_params 590592, 1.275ms, 0.404% latency, input shape [2, 512, 768], output shape [2, 512, 768] 579 | (key): Linear, num_params 590592, 0.971ms, 0.308% latency, input shape [2, 512, 768], output shape [2, 512, 768] 580 | (value): Linear, num_params 590592, 0.94ms, 0.298% latency, input shape [2, 512, 768], output shape [2, 512, 768] 581 | (dropout): Dropout, num_params 0, 0.119ms, 0.038% latency, input shape [2, 12, 512, 512], output shape [2, 12, 512, 512] 582 | (output): BertSelfOutput, num_params 592128, 2.346ms, 0.743% latency, input shape [2, 512, 768], output shape [2, 512, 768] 583 | (dense): Linear, num_params 590592, 1.478ms, 0.468% latency, input shape [2, 512, 768], output shape [2, 512, 768] 584 | (LayerNorm): LayerNorm, num_params 1536, 0.328ms, 0.104% latency, input shape [2, 512, 768], output shape [2, 512, 768] 585 | (dropout): Dropout, num_params 0, 0.084ms, 0.027% latency, input shape [2, 512, 768], output shape [2, 512, 768] 586 | (intermediate): BertIntermediate, num_params 2362368, 4.357ms, 1.381% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 587 | (dense): Linear, num_params 2362368, 3.585ms, 1.136% latency, input shape [2, 512, 768], output shape [2, 512, 3072] 588 | (intermediate_act_fn): GELUActivation, num_params 0, 0.652ms, 0.207% latency, input shape [2, 512, 3072], output shape [2, 512, 3072] 589 | (output): BertOutput, num_params 2361600, 4.523ms, 1.433% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 590 | (dense): Linear, num_params 2360064, 3.708ms, 1.175% latency, input shape [2, 512, 3072], output shape [2, 512, 768] 591 | (LayerNorm): LayerNorm, num_params 1536, 0.337ms, 0.107% latency, input shape [2, 512, 768], output shape [2, 512, 768] 592 | (dropout): Dropout, num_params 0, 0.077ms, 0.025% latency, input shape [2, 512, 768], output shape [2, 512, 768] 593 | (pooler): BertPooler, num_params 590592, 0.672ms, 0.213% latency, input shape [2, 512, 768], output shape [2, 768] 594 | (dense): Linear, num_params 590592, 0.216ms, 0.068% latency, input shape [2, 768], output shape [2, 768] 595 | (activation): Tanh, num_params 0, 0.272ms, 0.086% latency, input shape [2, 768], output shape [2, 768] 596 | (dropout): Dropout, num_params 0, 0.066ms, 0.021% latency, input shape [2, 768], output shape [2, 768] 597 | (classifier): Linear, num_params 1538, 0.104ms, 0.033% latency, input shape [2, 768], output shape [2, 2] 598 | 599 | ``` 600 |
601 | 602 |
603 | TorchInfo output 604 | 605 | ```bash 606 | $ python3 example_bert.py --profiler torchinfo 607 | ========================================================================================================= 608 | Layer (type:depth-idx) Output Shape Param # 609 | ========================================================================================================= 610 | BertForSequenceClassification [2, 2] -- 611 | ├─BertModel: 1-1 [2, 768] -- 612 | │ └─BertEmbeddings: 2-1 [2, 512, 768] -- 613 | │ │ └─Embedding: 3-1 [2, 512, 768] 23,440,896 614 | │ │ └─Embedding: 3-2 [2, 512, 768] 1,536 615 | │ │ └─Embedding: 3-3 [1, 512, 768] 393,216 616 | │ │ └─LayerNorm: 3-4 [2, 512, 768] 1,536 617 | │ │ └─Dropout: 3-5 [2, 512, 768] -- 618 | │ └─BertEncoder: 2-2 [2, 512, 768] -- 619 | │ │ └─ModuleList: 3-6 -- 85,054,464 620 | │ └─BertPooler: 2-3 [2, 768] -- 621 | │ │ └─Linear: 3-7 [2, 768] 590,592 622 | │ │ └─Tanh: 3-8 [2, 768] -- 623 | ├─Dropout: 1-2 [2, 768] -- 624 | ├─Linear: 1-3 [2, 2] 1,538 625 | ========================================================================================================= 626 | Total params: 109,483,778 627 | Trainable params: 109,483,778 628 | Non-trainable params: 0 629 | Total mult-adds (M): 218.57 630 | ========================================================================================================= 631 | Input size (MB): 0.00 632 | Forward/backward pass size (MB): 852.50 633 | Params size (MB): 437.94 634 | Estimated Total Size (MB): 1290.44 635 | ========================================================================================================= 636 | ``` 637 |
638 | 639 | 640 |
641 | DeepSpeed output 642 | 643 | ```bash 644 | $ python3 example_bert.py --profiler deepspeed 645 | -------------------------- DeepSpeed Flops Profiler -------------------------- 646 | Profile Summary at step 10: 647 | Notations: 648 | data parallel size (dp_size), model parallel size(mp_size), 649 | number of parameters (params), number of multiply-accumulate operations(MACs), 650 | number of floating-point operations (flops), floating-point operations per second (FLOPS), 651 | fwd latency (forward propagation latency), bwd latency (backward propagation latency), 652 | step (weights update latency), iter latency (sum of fwd, bwd and step latency) 653 | 654 | params per gpu: 109.48 M 655 | params of model = params per GPU * mp_size: 109.48 M 656 | fwd MACs per GPU: 96.64 GMACs 657 | fwd flops per GPU: 193.45 G 658 | fwd flops of model = fwd flops per GPU * mp_size: 193.45 G 659 | fwd latency: 297.4 ms 660 | fwd FLOPS per GPU = fwd flops per GPU / fwd latency: 650.47 GFLOPS 661 | 662 | ----------------------------- Aggregated Profile per GPU ----------------------------- 663 | Top 1 modules in terms of params, MACs or fwd latency at different model depths: 664 | depth 0: 665 | params - {'BertForSequenceClassification': '109.48 M'} 666 | MACs - {'BertForSequenceClassification': '96.64 GMACs'} 667 | fwd latency - {'BertForSequenceClassification': '297.4 ms'} 668 | depth 1: 669 | params - {'BertModel': '109.48 M'} 670 | MACs - {'BertModel': '96.64 GMACs'} 671 | fwd latency - {'BertModel': '296.95 ms'} 672 | depth 2: 673 | params - {'BertEncoder': '85.05 M'} 674 | MACs - {'BertEncoder': '96.64 GMACs'} 675 | fwd latency - {'BertEncoder': '294.59 ms'} 676 | depth 3: 677 | params - {'ModuleList': '85.05 M'} 678 | MACs - {'ModuleList': '96.64 GMACs'} 679 | fwd latency - {'ModuleList': '294.23 ms'} 680 | depth 4: 681 | params - {'BertLayer': '85.05 M'} 682 | MACs - {'BertLayer': '96.64 GMACs'} 683 | fwd latency - {'BertLayer': '294.23 ms'} 684 | depth 5: 685 | params - {'BertAttention': '28.37 M'} 686 | MACs - {'BertAttention': '38.65 GMACs'} 687 | fwd latency - {'BertAttention': '199.37 ms'} 688 | depth 6: 689 | params - {'Linear': '56.67 M'} 690 | MACs - {'Linear': '57.98 GMACs'} 691 | fwd latency - {'BertSelfAttention': '175.64 ms'} 692 | 693 | ------------------------------ Detailed Profile per GPU ------------------------------ 694 | Each module profile is listed after its name in the following order: 695 | params, percentage of total params, MACs, percentage of total MACs, fwd latency, percentage of total fwd latency, fwd FLOPS 696 | 697 | Note: 1. A module can have torch.nn.module or torch.nn.functional to compute logits (e.g. CrossEntropyLoss). They are not counted as submodules, thus not to be printed out. However they make up the difference between a parent's MACs (or latency) and the sum of its submodules'. 698 | 2. Number of floating-point operations is a theoretical estimation, thus FLOPS computed using that could be larger than the maximum system throughput. 699 | 3. The fwd latency listed in the top module's profile is directly captured at the module forward function in PyTorch, thus it's less than the fwd latency shown above which is captured in DeepSpeed. 700 | 701 | BertForSequenceClassification( 702 | 109.48 M, 100.00% Params, 96.64 GMACs, 100.00% MACs, 297.4 ms, 100.00% latency, 650.47 GFLOPS, 703 | (bert): BertModel( 704 | 109.48 M, 100.00% Params, 96.64 GMACs, 100.00% MACs, 296.95 ms, 99.85% latency, 651.46 GFLOPS, 705 | (embeddings): BertEmbeddings( 706 | 23.84 M, 21.77% Params, 0 MACs, 0.00% MACs, 1.63 ms, 0.55% latency, 2.41 GFLOPS, 707 | (word_embeddings): Embedding(23.44 M, 21.41% Params, 0 MACs, 0.00% MACs, 672.82 us, 0.23% latency, 0.0 FLOPS, 30522, 768, padding_idx=0) 708 | (position_embeddings): Embedding(393.22 k, 0.36% Params, 0 MACs, 0.00% MACs, 156.16 us, 0.05% latency, 0.0 FLOPS, 512, 768) 709 | (token_type_embeddings): Embedding(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 131.85 us, 0.04% latency, 0.0 FLOPS, 2, 768) 710 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 242.95 us, 0.08% latency, 16.19 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 711 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 36.48 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 712 | ) 713 | (encoder): BertEncoder( 714 | 85.05 M, 77.69% Params, 96.64 GMACs, 100.00% MACs, 294.59 ms, 99.06% latency, 656.65 GFLOPS, 715 | (layer): ModuleList( 716 | 85.05 M, 77.69% Params, 96.64 GMACs, 100.00% MACs, 294.23 ms, 98.94% latency, 657.45 GFLOPS, 717 | (0): BertLayer( 718 | 7.09 M, 6.47% Params, 8.05 GMACs, 8.33% MACs, 34.89 ms, 11.73% latency, 462.08 GFLOPS, 719 | (attention): BertAttention( 720 | 2.36 M, 2.16% Params, 3.22 GMACs, 3.33% MACs, 26.5 ms, 8.91% latency, 243.52 GFLOPS, 721 | (self): BertSelfAttention( 722 | 1.77 M, 1.62% Params, 2.62 GMACs, 2.71% MACs, 24.42 ms, 8.21% latency, 214.58 GFLOPS, 723 | (query): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 883.34 us, 0.30% latency, 1.37 TFLOPS, in_features=768, out_features=768, bias=True) 724 | (key): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 720.02 us, 0.24% latency, 1.68 TFLOPS, in_features=768, out_features=768, bias=True) 725 | (value): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 716.45 us, 0.24% latency, 1.69 TFLOPS, in_features=768, out_features=768, bias=True) 726 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 61.99 us, 0.02% latency, 0.0 FLOPS, p=0.1, inplace=False) 727 | ) 728 | (output): BertSelfOutput( 729 | 592.13 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 2.0 ms, 0.67% latency, 606.79 GFLOPS, 730 | (dense): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.26 ms, 0.42% latency, 960.12 GFLOPS, in_features=768, out_features=768, bias=True) 731 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 234.13 us, 0.08% latency, 16.79 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 732 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 39.1 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 733 | ) 734 | ) 735 | (intermediate): BertIntermediate( 736 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 4.16 ms, 1.40% latency, 1.16 TFLOPS, 737 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.47 ms, 1.17% latency, 1.39 TFLOPS, in_features=768, out_features=3072, bias=True) 738 | (intermediate_act_fn): GELUActivation(0, 0.00% Params, 0 MACs, 0.00% MACs, 624.18 us, 0.21% latency, 0.0 FLOPS, ) 739 | ) 740 | (output): BertOutput( 741 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 4.0 ms, 1.34% latency, 1.21 TFLOPS, 742 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.37 ms, 1.13% latency, 1.43 TFLOPS, in_features=3072, out_features=768, bias=True) 743 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 299.93 us, 0.10% latency, 13.11 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 744 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 34.33 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 745 | ) 746 | ) 747 | (1): BertLayer( 748 | 7.09 M, 6.47% Params, 8.05 GMACs, 8.33% MACs, 23.32 ms, 7.84% latency, 691.22 GFLOPS, 749 | (attention): BertAttention( 750 | 2.36 M, 2.16% Params, 3.22 GMACs, 3.33% MACs, 15.4 ms, 5.18% latency, 418.94 GFLOPS, 751 | (self): BertSelfAttention( 752 | 1.77 M, 1.62% Params, 2.62 GMACs, 2.71% MACs, 13.38 ms, 4.50% latency, 391.57 GFLOPS, 753 | (query): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.17 ms, 0.39% latency, 1.03 TFLOPS, in_features=768, out_features=768, bias=True) 754 | (key): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 863.79 us, 0.29% latency, 1.4 TFLOPS, in_features=768, out_features=768, bias=True) 755 | (value): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 783.92 us, 0.26% latency, 1.54 TFLOPS, in_features=768, out_features=768, bias=True) 756 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 57.7 us, 0.02% latency, 0.0 FLOPS, p=0.1, inplace=False) 757 | ) 758 | (output): BertSelfOutput( 759 | 592.13 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.94 ms, 0.65% latency, 623.23 GFLOPS, 760 | (dense): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.3 ms, 0.44% latency, 928.62 GFLOPS, in_features=768, out_features=768, bias=True) 761 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 254.39 us, 0.09% latency, 15.46 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 762 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 39.58 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 763 | ) 764 | ) 765 | (intermediate): BertIntermediate( 766 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.83 ms, 1.29% latency, 1.26 TFLOPS, 767 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.17 ms, 1.07% latency, 1.52 TFLOPS, in_features=768, out_features=3072, bias=True) 768 | (intermediate_act_fn): GELUActivation(0, 0.00% Params, 0 MACs, 0.00% MACs, 595.81 us, 0.20% latency, 0.0 FLOPS, ) 769 | ) 770 | (output): BertOutput( 771 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.91 ms, 1.31% latency, 1.24 TFLOPS, 772 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.31 ms, 1.11% latency, 1.46 TFLOPS, in_features=3072, out_features=768, bias=True) 773 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 274.18 us, 0.09% latency, 14.34 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 774 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 33.14 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 775 | ) 776 | ) 777 | (2): BertLayer( 778 | 7.09 M, 6.47% Params, 8.05 GMACs, 8.33% MACs, 27.64 ms, 9.29% latency, 583.3 GFLOPS, 779 | (attention): BertAttention( 780 | 2.36 M, 2.16% Params, 3.22 GMACs, 3.33% MACs, 19.89 ms, 6.69% latency, 324.37 GFLOPS, 781 | (self): BertSelfAttention( 782 | 1.77 M, 1.62% Params, 2.62 GMACs, 2.71% MACs, 17.88 ms, 6.01% latency, 293.11 GFLOPS, 783 | (query): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.16 ms, 0.39% latency, 1.05 TFLOPS, in_features=768, out_features=768, bias=True) 784 | (key): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 785.83 us, 0.26% latency, 1.54 TFLOPS, in_features=768, out_features=768, bias=True) 785 | (value): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 765.8 us, 0.26% latency, 1.58 TFLOPS, in_features=768, out_features=768, bias=True) 786 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 51.5 us, 0.02% latency, 0.0 FLOPS, p=0.1, inplace=False) 787 | ) 788 | (output): BertSelfOutput( 789 | 592.13 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.94 ms, 0.65% latency, 623.69 GFLOPS, 790 | (dense): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.3 ms, 0.44% latency, 926.07 GFLOPS, in_features=768, out_features=768, bias=True) 791 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 257.73 us, 0.09% latency, 15.26 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 792 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 39.1 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 793 | ) 794 | ) 795 | (intermediate): BertIntermediate( 796 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.7 ms, 1.24% latency, 1.31 TFLOPS, 797 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.01 ms, 1.01% latency, 1.61 TFLOPS, in_features=768, out_features=3072, bias=True) 798 | (intermediate_act_fn): GELUActivation(0, 0.00% Params, 0 MACs, 0.00% MACs, 622.75 us, 0.21% latency, 0.0 FLOPS, ) 799 | ) 800 | (output): BertOutput( 801 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.87 ms, 1.30% latency, 1.25 TFLOPS, 802 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.25 ms, 1.09% latency, 1.49 TFLOPS, in_features=3072, out_features=768, bias=True) 803 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 256.3 us, 0.09% latency, 15.34 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 804 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 35.05 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 805 | ) 806 | ) 807 | (3): BertLayer( 808 | 7.09 M, 6.47% Params, 8.05 GMACs, 8.33% MACs, 22.4 ms, 7.53% latency, 719.51 GFLOPS, 809 | (attention): BertAttention( 810 | 2.36 M, 2.16% Params, 3.22 GMACs, 3.33% MACs, 14.67 ms, 4.93% latency, 439.78 GFLOPS, 811 | (self): BertSelfAttention( 812 | 1.77 M, 1.62% Params, 2.62 GMACs, 2.71% MACs, 12.61 ms, 4.24% latency, 415.76 GFLOPS, 813 | (query): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.08 ms, 0.36% latency, 1.12 TFLOPS, in_features=768, out_features=768, bias=True) 814 | (key): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 773.19 us, 0.26% latency, 1.56 TFLOPS, in_features=768, out_features=768, bias=True) 815 | (value): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 723.6 us, 0.24% latency, 1.67 TFLOPS, in_features=768, out_features=768, bias=True) 816 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 59.13 us, 0.02% latency, 0.0 FLOPS, p=0.1, inplace=False) 817 | ) 818 | (output): BertSelfOutput( 819 | 592.13 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.98 ms, 0.66% latency, 612.86 GFLOPS, 820 | (dense): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.33 ms, 0.45% latency, 908.64 GFLOPS, in_features=768, out_features=768, bias=True) 821 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 267.51 us, 0.09% latency, 14.7 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 822 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 39.58 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 823 | ) 824 | ) 825 | (intermediate): BertIntermediate( 826 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.7 ms, 1.24% latency, 1.31 TFLOPS, 827 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.1 ms, 1.04% latency, 1.56 TFLOPS, in_features=768, out_features=3072, bias=True) 828 | (intermediate_act_fn): GELUActivation(0, 0.00% Params, 0 MACs, 0.00% MACs, 527.86 us, 0.18% latency, 0.0 FLOPS, ) 829 | ) 830 | (output): BertOutput( 831 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.86 ms, 1.30% latency, 1.25 TFLOPS, 832 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.22 ms, 1.08% latency, 1.5 TFLOPS, in_features=3072, out_features=768, bias=True) 833 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 282.76 us, 0.10% latency, 13.91 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 834 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 36.0 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 835 | ) 836 | ) 837 | (4): BertLayer( 838 | 7.09 M, 6.47% Params, 8.05 GMACs, 8.33% MACs, 22.22 ms, 7.47% latency, 725.38 GFLOPS, 839 | (attention): BertAttention( 840 | 2.36 M, 2.16% Params, 3.22 GMACs, 3.33% MACs, 14.46 ms, 4.86% latency, 446.28 GFLOPS, 841 | (self): BertSelfAttention( 842 | 1.77 M, 1.62% Params, 2.62 GMACs, 2.71% MACs, 12.58 ms, 4.23% latency, 416.67 GFLOPS, 843 | (query): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.07 ms, 0.36% latency, 1.13 TFLOPS, in_features=768, out_features=768, bias=True) 844 | (key): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 742.67 us, 0.25% latency, 1.63 TFLOPS, in_features=768, out_features=768, bias=True) 845 | (value): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 724.55 us, 0.24% latency, 1.67 TFLOPS, in_features=768, out_features=768, bias=True) 846 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 56.98 us, 0.02% latency, 0.0 FLOPS, p=0.1, inplace=False) 847 | ) 848 | (output): BertSelfOutput( 849 | 592.13 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.8 ms, 0.61% latency, 672.18 GFLOPS, 850 | (dense): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.17 ms, 0.39% latency, 1.04 TFLOPS, in_features=768, out_features=768, bias=True) 851 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 299.45 us, 0.10% latency, 13.13 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 852 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 37.91 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 853 | ) 854 | ) 855 | (intermediate): BertIntermediate( 856 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.74 ms, 1.26% latency, 1.29 TFLOPS, 857 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.06 ms, 1.03% latency, 1.58 TFLOPS, in_features=768, out_features=3072, bias=True) 858 | (intermediate_act_fn): GELUActivation(0, 0.00% Params, 0 MACs, 0.00% MACs, 595.57 us, 0.20% latency, 0.0 FLOPS, ) 859 | ) 860 | (output): BertOutput( 861 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.84 ms, 1.29% latency, 1.26 TFLOPS, 862 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.25 ms, 1.09% latency, 1.48 TFLOPS, in_features=3072, out_features=768, bias=True) 863 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 254.39 us, 0.09% latency, 15.46 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 864 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 34.57 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 865 | ) 866 | ) 867 | (5): BertLayer( 868 | 7.09 M, 6.47% Params, 8.05 GMACs, 8.33% MACs, 22.04 ms, 7.41% latency, 731.45 GFLOPS, 869 | (attention): BertAttention( 870 | 2.36 M, 2.16% Params, 3.22 GMACs, 3.33% MACs, 14.35 ms, 4.82% latency, 449.71 GFLOPS, 871 | (self): BertSelfAttention( 872 | 1.77 M, 1.62% Params, 2.62 GMACs, 2.71% MACs, 12.48 ms, 4.20% latency, 419.85 GFLOPS, 873 | (query): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.15 ms, 0.39% latency, 1.05 TFLOPS, in_features=768, out_features=768, bias=True) 874 | (key): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 761.99 us, 0.26% latency, 1.59 TFLOPS, in_features=768, out_features=768, bias=True) 875 | (value): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 735.76 us, 0.25% latency, 1.64 TFLOPS, in_features=768, out_features=768, bias=True) 876 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 54.84 us, 0.02% latency, 0.0 FLOPS, p=0.1, inplace=False) 877 | ) 878 | (output): BertSelfOutput( 879 | 592.13 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.79 ms, 0.60% latency, 675.76 GFLOPS, 880 | (dense): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.21 ms, 0.41% latency, 994.61 GFLOPS, in_features=768, out_features=768, bias=True) 881 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 221.01 us, 0.07% latency, 17.79 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 882 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 37.91 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 883 | ) 884 | ) 885 | (intermediate): BertIntermediate( 886 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.65 ms, 1.23% latency, 1.32 TFLOPS, 887 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.0 ms, 1.01% latency, 1.61 TFLOPS, in_features=768, out_features=3072, bias=True) 888 | (intermediate_act_fn): GELUActivation(0, 0.00% Params, 0 MACs, 0.00% MACs, 590.09 us, 0.20% latency, 0.0 FLOPS, ) 889 | ) 890 | (output): BertOutput( 891 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.86 ms, 1.30% latency, 1.25 TFLOPS, 892 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.24 ms, 1.09% latency, 1.49 TFLOPS, in_features=3072, out_features=768, bias=True) 893 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 257.97 us, 0.09% latency, 15.24 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 894 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 39.82 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 895 | ) 896 | ) 897 | (6): BertLayer( 898 | 7.09 M, 6.47% Params, 8.05 GMACs, 8.33% MACs, 22.66 ms, 7.62% latency, 711.37 GFLOPS, 899 | (attention): BertAttention( 900 | 2.36 M, 2.16% Params, 3.22 GMACs, 3.33% MACs, 14.71 ms, 4.95% latency, 438.72 GFLOPS, 901 | (self): BertSelfAttention( 902 | 1.77 M, 1.62% Params, 2.62 GMACs, 2.71% MACs, 12.76 ms, 4.29% latency, 410.77 GFLOPS, 903 | (query): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.07 ms, 0.36% latency, 1.13 TFLOPS, in_features=768, out_features=768, bias=True) 904 | (key): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 777.96 us, 0.26% latency, 1.55 TFLOPS, in_features=768, out_features=768, bias=True) 905 | (value): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 759.84 us, 0.26% latency, 1.59 TFLOPS, in_features=768, out_features=768, bias=True) 906 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 56.74 us, 0.02% latency, 0.0 FLOPS, p=0.1, inplace=False) 907 | ) 908 | (output): BertSelfOutput( 909 | 592.13 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.87 ms, 0.63% latency, 648.1 GFLOPS, 910 | (dense): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.22 ms, 0.41% latency, 992.66 GFLOPS, in_features=768, out_features=768, bias=True) 911 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 281.1 us, 0.09% latency, 13.99 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 912 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 41.72 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 913 | ) 914 | ) 915 | (intermediate): BertIntermediate( 916 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.94 ms, 1.32% latency, 1.23 TFLOPS, 917 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.26 ms, 1.10% latency, 1.48 TFLOPS, in_features=768, out_features=3072, bias=True) 918 | (intermediate_act_fn): GELUActivation(0, 0.00% Params, 0 MACs, 0.00% MACs, 615.6 us, 0.21% latency, 0.0 FLOPS, ) 919 | ) 920 | (output): BertOutput( 921 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.82 ms, 1.28% latency, 1.27 TFLOPS, 922 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.24 ms, 1.09% latency, 1.49 TFLOPS, in_features=3072, out_features=768, bias=True) 923 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 246.76 us, 0.08% latency, 15.93 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 924 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 35.05 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 925 | ) 926 | ) 927 | (7): BertLayer( 928 | 7.09 M, 6.47% Params, 8.05 GMACs, 8.33% MACs, 22.94 ms, 7.72% latency, 702.57 GFLOPS, 929 | (attention): BertAttention( 930 | 2.36 M, 2.16% Params, 3.22 GMACs, 3.33% MACs, 15.15 ms, 5.10% latency, 425.84 GFLOPS, 931 | (self): BertSelfAttention( 932 | 1.77 M, 1.62% Params, 2.62 GMACs, 2.71% MACs, 13.15 ms, 4.42% latency, 398.52 GFLOPS, 933 | (query): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.07 ms, 0.36% latency, 1.13 TFLOPS, in_features=768, out_features=768, bias=True) 934 | (key): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 757.69 us, 0.25% latency, 1.59 TFLOPS, in_features=768, out_features=768, bias=True) 935 | (value): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 729.8 us, 0.25% latency, 1.66 TFLOPS, in_features=768, out_features=768, bias=True) 936 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 61.04 us, 0.02% latency, 0.0 FLOPS, p=0.1, inplace=False) 937 | ) 938 | (output): BertSelfOutput( 939 | 592.13 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.92 ms, 0.64% latency, 632.22 GFLOPS, 940 | (dense): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.27 ms, 0.43% latency, 948.97 GFLOPS, in_features=768, out_features=768, bias=True) 941 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 279.19 us, 0.09% latency, 14.08 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 942 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 37.67 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 943 | ) 944 | ) 945 | (intermediate): BertIntermediate( 946 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.79 ms, 1.28% latency, 1.27 TFLOPS, 947 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.15 ms, 1.06% latency, 1.53 TFLOPS, in_features=768, out_features=3072, bias=True) 948 | (intermediate_act_fn): GELUActivation(0, 0.00% Params, 0 MACs, 0.00% MACs, 576.5 us, 0.19% latency, 0.0 FLOPS, ) 949 | ) 950 | (output): BertOutput( 951 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.82 ms, 1.28% latency, 1.27 TFLOPS, 952 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.23 ms, 1.08% latency, 1.5 TFLOPS, in_features=3072, out_features=768, bias=True) 953 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 264.17 us, 0.09% latency, 14.89 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 954 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 36.0 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 955 | ) 956 | ) 957 | (8): BertLayer( 958 | 7.09 M, 6.47% Params, 8.05 GMACs, 8.33% MACs, 22.78 ms, 7.66% latency, 707.75 GFLOPS, 959 | (attention): BertAttention( 960 | 2.36 M, 2.16% Params, 3.22 GMACs, 3.33% MACs, 14.92 ms, 5.02% latency, 432.5 GFLOPS, 961 | (self): BertSelfAttention( 962 | 1.77 M, 1.62% Params, 2.62 GMACs, 2.71% MACs, 13.02 ms, 4.38% latency, 402.55 GFLOPS, 963 | (query): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.11 ms, 0.37% latency, 1.08 TFLOPS, in_features=768, out_features=768, bias=True) 964 | (key): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 751.26 us, 0.25% latency, 1.61 TFLOPS, in_features=768, out_features=768, bias=True) 965 | (value): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 747.68 us, 0.25% latency, 1.62 TFLOPS, in_features=768, out_features=768, bias=True) 966 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 56.27 us, 0.02% latency, 0.0 FLOPS, p=0.1, inplace=False) 967 | ) 968 | (output): BertSelfOutput( 969 | 592.13 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.82 ms, 0.61% latency, 667.5 GFLOPS, 970 | (dense): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.2 ms, 0.40% latency, 1.01 TFLOPS, in_features=768, out_features=768, bias=True) 971 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 255.35 us, 0.09% latency, 15.4 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 972 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 38.62 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 973 | ) 974 | ) 975 | (intermediate): BertIntermediate( 976 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.83 ms, 1.29% latency, 1.26 TFLOPS, 977 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.17 ms, 1.07% latency, 1.52 TFLOPS, in_features=768, out_features=3072, bias=True) 978 | (intermediate_act_fn): GELUActivation(0, 0.00% Params, 0 MACs, 0.00% MACs, 588.89 us, 0.20% latency, 0.0 FLOPS, ) 979 | ) 980 | (output): BertOutput( 981 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.84 ms, 1.29% latency, 1.26 TFLOPS, 982 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.23 ms, 1.09% latency, 1.49 TFLOPS, in_features=3072, out_features=768, bias=True) 983 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 268.46 us, 0.09% latency, 14.65 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 984 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 36.24 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 985 | ) 986 | ) 987 | (9): BertLayer( 988 | 7.09 M, 6.47% Params, 8.05 GMACs, 8.33% MACs, 24.22 ms, 8.14% latency, 665.58 GFLOPS, 989 | (attention): BertAttention( 990 | 2.36 M, 2.16% Params, 3.22 GMACs, 3.33% MACs, 15.23 ms, 5.12% latency, 423.8 GFLOPS, 991 | (self): BertSelfAttention( 992 | 1.77 M, 1.62% Params, 2.62 GMACs, 2.71% MACs, 13.04 ms, 4.39% latency, 401.84 GFLOPS, 993 | (query): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.13 ms, 0.38% latency, 1.07 TFLOPS, in_features=768, out_features=768, bias=True) 994 | (key): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 770.81 us, 0.26% latency, 1.57 TFLOPS, in_features=768, out_features=768, bias=True) 995 | (value): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 739.57 us, 0.25% latency, 1.63 TFLOPS, in_features=768, out_features=768, bias=True) 996 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 56.98 us, 0.02% latency, 0.0 FLOPS, p=0.1, inplace=False) 997 | ) 998 | (output): BertSelfOutput( 999 | 592.13 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 2.11 ms, 0.71% latency, 575.66 GFLOPS, 1000 | (dense): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.34 ms, 0.45% latency, 900.72 GFLOPS, in_features=768, out_features=768, bias=True) 1001 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 308.51 us, 0.10% latency, 12.75 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 1002 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 49.35 us, 0.02% latency, 0.0 FLOPS, p=0.1, inplace=False) 1003 | ) 1004 | ) 1005 | (intermediate): BertIntermediate( 1006 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 4.26 ms, 1.43% latency, 1.13 TFLOPS, 1007 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.35 ms, 1.13% latency, 1.44 TFLOPS, in_features=768, out_features=3072, bias=True) 1008 | (intermediate_act_fn): GELUActivation(0, 0.00% Params, 0 MACs, 0.00% MACs, 787.5 us, 0.26% latency, 0.0 FLOPS, ) 1009 | ) 1010 | (output): BertOutput( 1011 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 4.49 ms, 1.51% latency, 1.08 TFLOPS, 1012 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.5 ms, 1.18% latency, 1.38 TFLOPS, in_features=3072, out_features=768, bias=True) 1013 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 408.17 us, 0.14% latency, 9.63 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 1014 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 63.66 us, 0.02% latency, 0.0 FLOPS, p=0.1, inplace=False) 1015 | ) 1016 | ) 1017 | (10): BertLayer( 1018 | 7.09 M, 6.47% Params, 8.05 GMACs, 8.33% MACs, 28.87 ms, 9.71% latency, 558.42 GFLOPS, 1019 | (attention): BertAttention( 1020 | 2.36 M, 2.16% Params, 3.22 GMACs, 3.33% MACs, 20.85 ms, 7.01% latency, 309.44 GFLOPS, 1021 | (self): BertSelfAttention( 1022 | 1.77 M, 1.62% Params, 2.62 GMACs, 2.71% MACs, 18.86 ms, 6.34% latency, 277.94 GFLOPS, 1023 | (query): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.3 ms, 0.44% latency, 931.86 GFLOPS, in_features=768, out_features=768, bias=True) 1024 | (key): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.09 ms, 0.37% latency, 1.1 TFLOPS, in_features=768, out_features=768, bias=True) 1025 | (value): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 936.51 us, 0.31% latency, 1.29 TFLOPS, in_features=768, out_features=768, bias=True) 1026 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 90.12 us, 0.03% latency, 0.0 FLOPS, p=0.1, inplace=False) 1027 | ) 1028 | (output): BertSelfOutput( 1029 | 592.13 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.9 ms, 0.64% latency, 637.53 GFLOPS, 1030 | (dense): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.22 ms, 0.41% latency, 993.64 GFLOPS, in_features=768, out_features=768, bias=True) 1031 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 291.35 us, 0.10% latency, 13.5 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 1032 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 40.53 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 1033 | ) 1034 | ) 1035 | (intermediate): BertIntermediate( 1036 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.9 ms, 1.31% latency, 1.24 TFLOPS, 1037 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.34 ms, 1.12% latency, 1.45 TFLOPS, in_features=768, out_features=3072, bias=True) 1038 | (intermediate_act_fn): GELUActivation(0, 0.00% Params, 0 MACs, 0.00% MACs, 492.33 us, 0.17% latency, 0.0 FLOPS, ) 1039 | ) 1040 | (output): BertOutput( 1041 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.91 ms, 1.31% latency, 1.24 TFLOPS, 1042 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.24 ms, 1.09% latency, 1.49 TFLOPS, in_features=3072, out_features=768, bias=True) 1043 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 270.61 us, 0.09% latency, 14.53 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 1044 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 42.92 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 1045 | ) 1046 | ) 1047 | (11): BertLayer( 1048 | 7.09 M, 6.47% Params, 8.05 GMACs, 8.33% MACs, 20.25 ms, 6.81% latency, 796.03 GFLOPS, 1049 | (attention): BertAttention( 1050 | 2.36 M, 2.16% Params, 3.22 GMACs, 3.33% MACs, 13.24 ms, 4.45% latency, 487.53 GFLOPS, 1051 | (self): BertSelfAttention( 1052 | 1.77 M, 1.62% Params, 2.62 GMACs, 2.71% MACs, 11.46 ms, 3.85% latency, 457.18 GFLOPS, 1053 | (query): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 930.55 us, 0.31% latency, 1.3 TFLOPS, in_features=768, out_features=768, bias=True) 1054 | (key): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 775.58 us, 0.26% latency, 1.56 TFLOPS, in_features=768, out_features=768, bias=True) 1055 | (value): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 714.54 us, 0.24% latency, 1.69 TFLOPS, in_features=768, out_features=768, bias=True) 1056 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 61.27 us, 0.02% latency, 0.0 FLOPS, p=0.1, inplace=False) 1057 | ) 1058 | (output): BertSelfOutput( 1059 | 592.13 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.69 ms, 0.57% latency, 716.53 GFLOPS, 1060 | (dense): Linear(590.59 k, 0.54% Params, 603.98 MMACs, 0.62% MACs, 1.13 ms, 0.38% latency, 1.07 TFLOPS, in_features=768, out_features=768, bias=True) 1061 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 219.82 us, 0.07% latency, 17.89 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 1062 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 38.86 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 1063 | ) 1064 | ) 1065 | (intermediate): BertIntermediate( 1066 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 2.98 ms, 1.00% latency, 1.62 TFLOPS, 1067 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 2.49 ms, 0.84% latency, 1.94 TFLOPS, in_features=768, out_features=3072, bias=True) 1068 | (intermediate_act_fn): GELUActivation(0, 0.00% Params, 0 MACs, 0.00% MACs, 422.24 us, 0.14% latency, 0.0 FLOPS, ) 1069 | ) 1070 | (output): BertOutput( 1071 | 2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.84 ms, 1.29% latency, 1.26 TFLOPS, 1072 | (dense): Linear(2.36 M, 2.16% Params, 2.42 GMACs, 2.50% MACs, 3.25 ms, 1.09% latency, 1.49 TFLOPS, in_features=3072, out_features=768, bias=True) 1073 | (LayerNorm): LayerNorm(1.54 k, 0.00% Params, 0 MACs, 0.00% MACs, 277.04 us, 0.09% latency, 14.19 GFLOPS, (768,), eps=1e-12, elementwise_affine=True) 1074 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 37.43 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 1075 | ) 1076 | ) 1077 | ) 1078 | ) 1079 | (pooler): BertPooler( 1080 | 590.59 k, 0.54% Params, 1.18 MMACs, 0.00% MACs, 319.0 us, 0.11% latency, 7.4 GFLOPS, 1081 | (dense): Linear(590.59 k, 0.54% Params, 1.18 MMACs, 0.00% MACs, 144.0 us, 0.05% latency, 16.38 GFLOPS, in_features=768, out_features=768, bias=True) 1082 | (activation): Tanh(0, 0.00% Params, 0 MACs, 0.00% MACs, 52.21 us, 0.02% latency, 0.0 FLOPS, ) 1083 | ) 1084 | ) 1085 | (dropout): Dropout(0, 0.00% Params, 0 MACs, 0.00% MACs, 34.09 us, 0.01% latency, 0.0 FLOPS, p=0.1, inplace=False) 1086 | (classifier): Linear(1.54 k, 0.00% Params, 3.07 KMACs, 0.00% MACs, 54.6 us, 0.02% latency, 112.53 MFLOPS, in_features=768, out_features=2, bias=True) 1087 | ) 1088 | ------------------------------------------------------------------------------ 1089 | 1090 | ``` 1091 |
1092 | 1093 | ## Requirements 1094 | 1095 | My development environment is: 1096 | * Python 3.9.13 1097 | * deepspeed 0.8.0 1098 | * torch 1.13.1 1099 | * torchinfo 1.7.2 1100 | * torchvision 0.14.1 1101 | --------------------------------------------------------------------------------