From bf493e9767aa3def7e450350b4805992e1b3831f Mon Sep 17 00:00:00 2001 From: Ryan Kuester Date: Sun, 30 Jun 2024 21:28:16 -0500 Subject: [PATCH 1/2] refactor: move tflite::Span to independent header Hoist the universally useful tflite::Span out of codegen/runtime/micro_codegen_context.h and into an independent header usable from elsewhere in the project. --- codegen/runtime/BUILD | 1 + codegen/runtime/micro_codegen_context.h | 17 +--------- tensorflow/lite/micro/BUILD | 6 ++++ tensorflow/lite/micro/span.h | 42 +++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 tensorflow/lite/micro/span.h diff --git a/codegen/runtime/BUILD b/codegen/runtime/BUILD index a1cb6c160bf..d23cb7078eb 100644 --- a/codegen/runtime/BUILD +++ b/codegen/runtime/BUILD @@ -14,5 +14,6 @@ cc_library( "//tensorflow/lite/micro:micro_context", "//tensorflow/lite/micro:micro_graph", "//tensorflow/lite/micro:micro_log", + "//tensorflow/lite/micro:span", ], ) diff --git a/codegen/runtime/micro_codegen_context.h b/codegen/runtime/micro_codegen_context.h index ca01a63bce4..3693ad27118 100644 --- a/codegen/runtime/micro_codegen_context.h +++ b/codegen/runtime/micro_codegen_context.h @@ -19,25 +19,10 @@ limitations under the License. #include "tensorflow/lite/c/common.h" #include "tensorflow/lite/micro/micro_context.h" #include "tensorflow/lite/micro/micro_graph.h" +#include "tensorflow/lite/micro/span.h" namespace tflite { -// A poor man's std::span, we should consider using the Pigweed span instead. -template -class Span { - public: - constexpr Span(T* data, size_t size) noexcept : data_(data), size_(size) {} - - constexpr T& operator[](size_t idx) const noexcept { return *(data_ + idx); } - - constexpr T* data() const noexcept { return data_; } - constexpr size_t size() const noexcept { return size_; } - - private: - T* data_; - size_t size_; -}; - struct Subgraph { Span inputs; Span outputs; diff --git a/tensorflow/lite/micro/BUILD b/tensorflow/lite/micro/BUILD index 60ace250631..527b85a8ea8 100644 --- a/tensorflow/lite/micro/BUILD +++ b/tensorflow/lite/micro/BUILD @@ -401,6 +401,12 @@ cc_library( ], ) +cc_library( + name = "span", + hdrs = ["span.h"], + copts = micro_copts(), +) + cc_library( name = "system_setup", srcs = [ diff --git a/tensorflow/lite/micro/span.h b/tensorflow/lite/micro/span.h new file mode 100644 index 00000000000..c431053a391 --- /dev/null +++ b/tensorflow/lite/micro/span.h @@ -0,0 +1,42 @@ +/* Copyright 2024 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. +*/ + +// A poor man's std::span, we should consider using the Pigweed span instead. + +#ifndef TENSORFLOW_LITE_MICRO_SPAN_H_ +#define TENSORFLOW_LITE_MICRO_SPAN_H_ + +#include + +namespace tflite { + +template +class Span { + public: + constexpr Span(T* data, size_t size) noexcept : data_(data), size_(size) {} + + constexpr T& operator[](size_t idx) const noexcept { return *(data_ + idx); } + + constexpr T* data() const noexcept { return data_; } + constexpr size_t size() const noexcept { return size_; } + + private: + T* data_; + size_t size_; +}; + +} // end namespace tflite + +#endif // TENSORFLOW_LITE_MICRO_SPAN_H_ From 894ca8eaab09a9ce920d2ef12cd735ef6c0479c3 Mon Sep 17 00:00:00 2001 From: Ryan Kuester Date: Wed, 24 Jul 2024 14:55:53 -0500 Subject: [PATCH 2/2] style: move comment from file-level to class-level --- tensorflow/lite/micro/span.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tensorflow/lite/micro/span.h b/tensorflow/lite/micro/span.h index c431053a391..eeb4c0b6ac5 100644 --- a/tensorflow/lite/micro/span.h +++ b/tensorflow/lite/micro/span.h @@ -13,8 +13,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -// A poor man's std::span, we should consider using the Pigweed span instead. - #ifndef TENSORFLOW_LITE_MICRO_SPAN_H_ #define TENSORFLOW_LITE_MICRO_SPAN_H_ @@ -22,6 +20,7 @@ limitations under the License. namespace tflite { +// A poor man's std::span, we should consider using the Pigweed span instead. template class Span { public: