1 ## Python API Tutorials 2 3 * [Parameters and Shapes](#parameters-and-shapes) 4 * [Float Operations](#float-operations) 5 * [Time and Memory](#time-and-memory) 6 * [Visualize](#visualize) 7 * [Multi-step Profiling](#multi-step-profiling) 8 9 ```import tensorflow as tf```. 10 11 ### Parameters and Shapes. 12 ```python 13 # Print trainable variable parameter statistics to stdout. 14 ProfileOptionBuilder = tf.profiler.ProfileOptionBuilder 15 16 param_stats = tf.profiler.profile( 17 tf.get_default_graph(), 18 options=ProfileOptionBuilder.trainable_variables_parameter()) 19 20 # Use code view to associate statistics with Python codes. 21 opts = ProfileOptionBuilder( 22 ProfileOptionBuilder.trainable_variables_parameter() 23 ).with_node_names(show_name_regexes=['.*my_code1.py.*', '.*my_code2.py.*'] 24 ).build() 25 param_stats = tf.profiler.profile( 26 tf.get_default_graph(), 27 cmd='code', 28 options=opts) 29 30 # param_stats can be tensorflow.tfprof.GraphNodeProto or 31 # tensorflow.tfprof.MultiGraphNodeProto, depending on the view. 32 # Let's print the root below. 33 sys.stdout.write('total_params: %d\n' % param_stats.total_parameters) 34 ``` 35 36 ### Float Operations 37 38 #### Note: See [Caveats](profile_model_architecture.md#caveats) in "Profile Model Architecture" Tutorial 39 ``` python 40 # Print to stdout an analysis of the number of floating point operations in the 41 # model broken down by individual operations. 42 tf.profiler.profile( 43 tf.get_default_graph(), 44 options=tf.profiler.ProfileOptionBuilder.float_operation()) 45 ``` 46 47 ### Time and Memory 48 You will first need to run the following set up in your model in order to 49 compute the memory and timing statistics. 50 51 ```python 52 # Generate the RunMetadata that contains the memory and timing information. 53 # 54 # Note: When run on accelerator (e.g. GPU), an operation might perform some 55 # cpu computation, enqueue the accelerator computation. The accelerator 56 # computation is then run asynchronously. The profiler considers 3 57 # times: 1) accelerator computation. 2) cpu computation (might wait on 58 # accelerator). 3) the sum of 1 and 2. 59 # 60 run_metadata = tf.RunMetadata() 61 with tf.Session() as sess: 62 _ = sess.run(train_op, 63 options=tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE), 64 run_metadata=run_metadata) 65 ``` 66 67 Finally, you may run `tf.profiler.profile` to explore the timing and memory 68 information of the model. 69 70 ``` python 71 # Print to stdout an analysis of the memory usage and the timing information 72 # broken down by python codes. 73 ProfileOptionBuilder = tf.profiler.ProfileOptionBuilder 74 opts = ProfileOptionBuilder(ProfileOptionBuilder.time_and_memory() 75 ).with_node_names(show_name_regexes=['.*my_code.py.*']).build() 76 77 tf.profiler.profile( 78 tf.get_default_graph(), 79 run_meta=run_metadata, 80 cmd='code', 81 options=opts) 82 83 # Print to stdout an analysis of the memory usage and the timing information 84 # broken down by operation types. 85 tf.profiler.profile( 86 tf.get_default_graph(), 87 run_meta=run_metadata, 88 cmd='op', 89 options=tf.profiler.ProfileOptionBuilder.time_and_memory()) 90 ``` 91 92 ### Visualize 93 94 ``` 95 To visualize the result of Python API results: 96 Call `with_step(0).with_timeline_output(filename)` to generate a timeline json file. 97 Open a Chrome Browser, type URL `chrome://tracing`, and load the json file. 98 ``` 99 100 Below are 2 examples of graph view and scope view. 101 102 <left> 103 ![CodeTimeline](graph_timeline.png) 104 ![CodeTimeline](scope_timeline.png) 105 </left> 106 107 ### Multi-step Profiling 108 109 tfprof allows you to profile statistics across multiple steps. 110 111 ```python 112 opts = model_analyzer.PRINT_ALL_TIMING_MEMORY.copy() 113 opts['account_type_regexes'] = ['.*'] 114 115 with session.Session() as sess: 116 r1, r2, r3 = lib.BuildSplitableModel() 117 sess.run(variables.global_variables_initializer()) 118 119 # Create a profiler. 120 profiler = model_analyzer.Profiler(sess.graph) 121 # Profile without RunMetadata of any step. 122 pb0 = profiler.profile_name_scope(opts) 123 124 run_meta = config_pb2.RunMetadata() 125 _ = sess.run(r1, 126 options=config_pb2.RunOptions( 127 trace_level=config_pb2.RunOptions.FULL_TRACE), 128 run_metadata=run_meta) 129 130 # Add run_meta of step 1. 131 profiler.add_step(1, run_meta) 132 pb1 = profiler.profile_name_scope(opts) 133 134 run_meta2 = config_pb2.RunMetadata() 135 _ = sess.run(r2, 136 options=config_pb2.RunOptions( 137 trace_level=config_pb2.RunOptions.FULL_TRACE), 138 run_metadata=run_meta2) 139 # Add run_meta of step 2. 140 profiler.add_step(2, run_meta2) 141 pb2 = profiler.profile_name_scope(opts) 142 143 run_meta3 = config_pb2.RunMetadata() 144 _ = sess.run(r3, 145 options=config_pb2.RunOptions( 146 trace_level=config_pb2.RunOptions.FULL_TRACE), 147 run_metadata=run_meta3) 148 # Add run_meta of step 3. 149 profiler.add_step(3, run_meta3) 150 pb3 = profiler.profile_name_scope(opts) 151 ``` 152