def _serve_time_series(self, request): ctx = plugin_util.context(request.environ) experiment = plugin_util.experiment_id(request.environ) series_requests_string = request.form.get("requests") if not series_requests_string: raise errors.InvalidArgumentError("Missing 'requests' field") try: series_requests = json.loads(series_requests_string) except ValueError: raise errors.InvalidArgumentError( "Unable to parse 'requests' as JSON") response = self._time_series_impl(ctx, experiment, series_requests) return http_util.Respond(request, response, "application/json")
def _checkBeginEndIndices(self, begin, end, total_count): if begin < 0: raise errors.InvalidArgumentError("Invalid begin index (%d)" % begin) if end > total_count: raise errors.InvalidArgumentError( "end index (%d) out of bounds (%d)" % (end, total_count)) if end >= 0 and end < begin: raise errors.InvalidArgumentError( "end index (%d) is unexpectedly less than begin index (%d)" % (end, begin)) if end < 0: # This means all digests. end = total_count return end
def _serve_image_data(self, request): """Serves an individual image.""" ctx = plugin_util.context(request.environ) blob_key = request.args["imageId"] if not blob_key: raise errors.InvalidArgumentError("Missing 'imageId' field") (data, content_type) = self._image_data_impl(ctx, blob_key) return http_util.Respond(request, data, content_type)
def _serve_individual_audio(self, request): """Serve encoded audio data.""" experiment = plugin_util.experiment_id(request.environ) mime_type = request.args["content_type"] if mime_type not in _ALLOWED_MIME_TYPES: raise errors.InvalidArgumentError("Illegal mime type %r" % mime_type) blob_key = request.args["blob_key"] data = self._data_provider.read_blob(blob_key) return http_util.Respond(request, data, mime_type)
def _translate_grpc_error(): try: yield except grpc.RpcError as e: if e.code() == grpc.StatusCode.INVALID_ARGUMENT: raise errors.InvalidArgumentError(e.details()) if e.code() == grpc.StatusCode.NOT_FOUND: raise errors.NotFoundError(e.details()) if e.code() == grpc.StatusCode.PERMISSION_DENIED: raise errors.PermissionDeniedError(e.details()) raise
def Alerts(self, run, begin, end, alert_type_filter=None): """Get alerts from the debugged TensorFlow program. Args: run: The tfdbg2 run to get Alerts from. begin: Beginning alert index. end: Ending alert index. alert_type_filter: Optional filter string for alert type, used to restrict retrieved alerts data to a single type. If used, `begin` and `end` refer to the beginning and ending indices within the filtered alert type. """ from tensorflow.python.debug.lib import debug_events_monitors runs = self.Runs() if run not in runs: # TODO(cais): This should generate a 400 response instead. return None alerts = [] alerts_breakdown = dict() alerts_by_type = dict() for monitor in self._monitors: monitor_alerts = monitor.alerts() if not monitor_alerts: continue alerts.extend(monitor_alerts) # TODO(cais): Replace this with Alert.to_json() when # monitor.alert_type() is available. if isinstance(monitor, debug_events_monitors.InfNanMonitor): alert_type = "InfNanAlert" else: alert_type = "__MiscellaneousAlert__" alerts_breakdown[alert_type] = len(monitor_alerts) alerts_by_type[alert_type] = monitor_alerts num_alerts = len(alerts) if alert_type_filter is not None: if alert_type_filter not in alerts_breakdown: raise errors.InvalidArgumentError( "Filtering of alerts failed: alert type %s does not exist" % alert_type_filter ) alerts = alerts_by_type[alert_type_filter] end = self._checkBeginEndIndices(begin, end, len(alerts)) return { "begin": begin, "end": end, "alert_type": alert_type_filter, "num_alerts": num_alerts, "alerts_breakdown": alerts_breakdown, "per_type_alert_limit": DEFAULT_PER_TYPE_ALERT_LIMIT, "alerts": [_alert_to_json(alert) for alert in alerts[begin:end]], }
def scalars_route(self, request): """Given a tag and single run, return array of ScalarEvents.""" tag = request.args.get("tag") run = request.args.get("run") if tag is None or run is None: raise errors.InvalidArgumentError( "Both run and tag must be specified: tag=%r, run=%r" % (tag, run)) ctx = plugin_util.context(request.environ) experiment = plugin_util.experiment_id(request.environ) output_format = request.args.get("format") (body, mime_type) = self.scalars_impl(ctx, tag, run, experiment, output_format) return http_util.Respond(request, body, mime_type)
def scalars_multirun_route(self, request): """Given a tag and list of runs, return dict of ScalarEvent arrays.""" if request.method != "POST": raise werkzeug.exceptions.MethodNotAllowed(["POST"]) tags = request.form.getlist("tag") runs = request.form.getlist("runs") if len(tags) != 1: raise errors.InvalidArgumentError( "tag must be specified exactly once") tag = tags[0] ctx = plugin_util.context(request.environ) experiment = plugin_util.experiment_id(request.environ) (body, mime_type) = self.scalars_multirun_impl(ctx, tag, runs, experiment) return http_util.Respond(request, body, mime_type)
def test_http_code(self): self.assertEqual(errors.InvalidArgumentError().http_code, 400)
def test_with_details(self): e = errors.InvalidArgumentError("expected absolute path; got './foo'") expected_msg = "Invalid argument: expected absolute path; got './foo'" self.assertEqual(str(e), expected_msg)
def test_no_details(self): e = errors.InvalidArgumentError() expected_msg = "Invalid argument" self.assertEqual(str(e), expected_msg)
def test_headers(self): e1 = errors.InvalidArgumentError() e2 = errors.InvalidArgumentError() self.assertEmpty(e1.headers) self.assertIsNot(e1.headers, e2.headers)