From a7a97eaf9731017aaaf90347956776fcc0eb7992 Mon Sep 17 00:00:00 2001 From: "A. Unique TensorFlower" Date: Fri, 6 Dec 2019 09:26:48 -0800 Subject: [PATCH] Add op stack class for op processing. PiperOrigin-RevId: 284199882 Change-Id: I6c86c024a03d0677c5b77e0a749f85898fb6f4c1 --- tensorflow/core/profiler/convert/BUILD | 8 +++ tensorflow/core/profiler/convert/op_stack.h | 69 +++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 tensorflow/core/profiler/convert/op_stack.h diff --git a/tensorflow/core/profiler/convert/BUILD b/tensorflow/core/profiler/convert/BUILD index 51af083ca5b..cfb50111c1b 100644 --- a/tensorflow/core/profiler/convert/BUILD +++ b/tensorflow/core/profiler/convert/BUILD @@ -15,6 +15,14 @@ cc_library( ], ) +cc_library( + name = "op_stack", + hdrs = ["op_stack.h"], + deps = [ + "//tensorflow/core:lib", + ], +) + cc_library( name = "op_stats_to_tf_stats", srcs = ["op_stats_to_tf_stats.cc"], diff --git a/tensorflow/core/profiler/convert/op_stack.h b/tensorflow/core/profiler/convert/op_stack.h new file mode 100644 index 00000000000..6bfa4d77643 --- /dev/null +++ b/tensorflow/core/profiler/convert/op_stack.h @@ -0,0 +1,69 @@ +/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ + +#ifndef TENSORFLOW_CORE_PROFILER_CONVERT_OP_STACK_H_ +#define TENSORFLOW_CORE_PROFILER_CONVERT_OP_STACK_H_ + +#include +#include +#include + +#include "tensorflow/core/platform/types.h" + +namespace tensorflow { +namespace profiler { + +template +class OpStack { + public: + // Pushes an Op onto the stack. + void Push(uint32 op_id, std::unique_ptr op_info) { + stack_.emplace_back(op_id, std::move(op_info)); + } + + // Pops the Op with the given op_id from the stack. + std::unique_ptr Pop(uint32 op_id) { + // Pop until match or stack_ is empty. + std::unique_ptr result; + while (!stack_.empty()) { + auto back = std::move(stack_.back()); + stack_.pop_back(); + if (op_id == back.first) { + result = std::move(back.second); + break; + } + } + return result; + } + + // Returns the Op at the top of the stack. + OpInfo* Top() const { + return stack_.empty() ? nullptr : stack_.back().second.get(); + } + + // Returns true if the stack is empty. + bool Empty() const { return stack_.empty(); } + + // Clears the stack. + void Clear() { stack_.clear(); } + + private: + std::vector>> stack_; +}; + +} // namespace profiler +} // namespace tensorflow + +#endif // TENSORFLOW_CORE_PROFILER_CONVERT_OP_STACK_H_