Adds Signal Library Window Op (#2019)
First Op for the TFLM Signal Processing Ops library.
Doc linked in bug.
The Window OP is a custom signal processing OP similar to what is found in tf.signal library, but specific for integer (int16) purposes.
You can directly use this as a builtin op via the op resolver as:
* `op_resolver.AddWindow()`
* or via python as shown in `window_op_test.py`
3 testing options are provided:
* Micro(C++): `bazel run signal/micro/kernels:window_test`
* Tensorflow/Micro(Python): `bazel run python/tflite_micro/signal:window_op_test`
* Makefile(C++): `make -f tensorflow/lite/micro/tools/make/Makefile test_kernel_window_test`
BUG=[259145369](http://b/259145369)
diff --git a/python/tflite_micro/signal/tflm_signal.bzl b/python/tflite_micro/signal/tflm_signal.bzl
new file mode 100644
index 0000000..dea8426
--- /dev/null
+++ b/python/tflite_micro/signal/tflm_signal.bzl
@@ -0,0 +1,88 @@
+"""Build rule for wrapping a custom TF OP from .cc to python."""
+
+# TODO(b/286890280): refactor to be more generic build target for any custom OP
+def py_tflm_signal_library(
+ name,
+ srcs = [],
+ deps = [],
+ visibility = None,
+ cc_op_defs = [],
+ cc_op_kernels = []):
+ """Creates build rules for signal ops as shared libraries.
+
+ Defines three targets:
+ <name>
+ Python library that exposes all ops defined in `cc_op_defs` and `py_srcs`.
+ <name>_cc
+ C++ library that registers any c++ ops in `cc_op_defs`, and includes the
+ kernels from `cc_op_kernels`.
+ ops/_<name>.so
+ Shared library exposing the <name>_cc library.
+ Args:
+ name: The name for the python library target build by this rule.
+ srcs: Python source files for the Python library.
+ deps: Dependencies for the Python library.
+ visibility: Visibility for the Python library.
+ cc_op_defs: A list of c++ src files containing REGISTER_OP definitions.
+ cc_op_kernels: A list of c++ targets containing kernels that are used
+ by the Python library.
+ """
+ binary_path = "ops"
+ if srcs:
+ binary_path_end_pos = srcs[0].rfind("/")
+ binary_path = srcs[0][0:binary_path_end_pos]
+ binary_name = binary_path + "/_" + cc_op_kernels[0][1:] + ".so"
+ if cc_op_defs:
+ binary_name = "ops/_" + name + ".so"
+ library_name = name + "_cc"
+ native.cc_library(
+ name = library_name,
+ srcs = cc_op_defs,
+ copts = ["--std=c++17"] + select({
+ "//conditions:default": ["-pthread"],
+ }),
+ alwayslink = 1,
+ deps =
+ cc_op_kernels +
+ ["@tensorflow_cc_deps//:cc_library"] +
+ select({"//conditions:default": []}),
+ )
+
+ native.cc_binary(
+ name = binary_name,
+ copts = ["--std=c++17"] + select({
+ "//conditions:default": ["-pthread"],
+ }),
+ linkshared = 1,
+ linkopts = [],
+ deps = [
+ ":" + library_name,
+ "@tensorflow_cc_deps//:cc_library",
+ ] + select({"//conditions:default": []}),
+ )
+
+ native.py_library(
+ name = name,
+ srcs = srcs,
+ srcs_version = "PY2AND3",
+ visibility = visibility,
+ data = [":" + binary_name],
+ deps = deps,
+ )
+
+# A rule to build a TensorFlow OpKernel.
+def tflm_signal_kernel_library(
+ name,
+ srcs = [],
+ hdrs = [],
+ deps = [],
+ copts = [],
+ alwayslink = 1):
+ native.cc_library(
+ name = name,
+ srcs = srcs,
+ hdrs = hdrs,
+ deps = deps,
+ copts = copts + ["--std=c++17"],
+ alwayslink = alwayslink,
+ )