24
24
from absl .testing import flagsaver
25
25
import tensorflow as tf
26
26
27
+ from tensorflow .python import pywrap_tfe_monitoring_reader as monitoring
28
+
27
29
FLAGS = flags .FLAGS
28
30
31
+ # Environment variable that defines extra metrics to include based on streamz.
32
+ # Is a comma separated list of streamz metrics which will result in metrics
33
+ # added to the report where the name of the metric is the basename of the
34
+ # streamz.
35
+ # For example: "/tensorflow/core/tf_function/graph_building_time_usecs"
36
+ # would add one new metric named "graph_building_time_usecs"
37
+ TEST_BENCHMARK_STREAMZ_EXTRA_METRICS = 'BENCHMARK_STREAMZ_EXTRA_METRICS'
38
+
29
39
30
40
class PerfZeroBenchmark (tf .test .Benchmark ):
31
41
"""Common methods used in PerfZero Benchmarks.
@@ -75,6 +85,14 @@ def __init__(self,
75
85
76
86
logging .info ('root_data_dir: %s' , root_data_dir )
77
87
88
+ self .extra_metrics = self ._get_extra_metrics_readers ()
89
+ logging .info ('including extra streamz metrics: %s' , self .extra_metrics )
90
+
91
+ def report_benchmark (self , metrics = None , ** kwargs ):
92
+ """Report a benchmark."""
93
+ metrics = self ._read_extra_metrics () + (metrics or [])
94
+ super ().report_benchmark (metrics = metrics , ** kwargs )
95
+
78
96
@property
79
97
def tpu (self ):
80
98
return self .default_flags .get ('tpu' , None )
@@ -83,6 +101,27 @@ def _get_model_dir(self, folder_name):
83
101
"""Returns directory to store info, e.g. saved model and event log."""
84
102
return os .path .join (self .output_dir , folder_name )
85
103
104
+ def _get_extra_metrics_readers (self ):
105
+ streamz = os .environ .get (TEST_BENCHMARK_STREAMZ_EXTRA_METRICS , None )
106
+ if streamz :
107
+ return [self ._get_metric_reader (x ) for x in streamz .split (',' )]
108
+ return []
109
+
110
+ def _get_metric_reader (self , streamz ):
111
+ return {
112
+ 'name' : os .path .basename (streamz ),
113
+ 'reader' : monitoring .TFE_MonitoringNewCounterReader (streamz ),
114
+ }
115
+
116
+ def _read_extra_metrics (self ):
117
+ return [self ._read_extra_metric (metric ) for metric in self .extra_metrics ]
118
+
119
+ def _read_extra_metric (self , metric ):
120
+ return {
121
+ 'name' : metric ['name' ],
122
+ 'value' : monitoring .TFE_MonitoringReadCounter0 (metric ['reader' ]),
123
+ }
124
+
86
125
def _setup (self ):
87
126
"""Sets up and resets flags before each test."""
88
127
logging .set_verbosity (logging .INFO )
0 commit comments