chore: import upstream snapshot with attribution
cffconvert / validate (push) Has been skipped
License Check / license-check (push) Failing after 2s

This commit is contained in:
wehub-resource-sync
2026-07-13 12:14:16 +08:00
commit 8a852e4b4e
36502 changed files with 9277225 additions and 0 deletions
@@ -0,0 +1,38 @@
load("@xla//third_party/rules_python/python:py_library.bzl", "py_library")
load("//tensorflow:tensorflow.default.bzl", "cuda_py_strict_test")
package(
# copybara:uncomment default_applicable_licenses = ["//tensorflow:LICENSE"],
licenses = ["notice"],
)
py_library(
name = "test_util",
srcs = ["test_util.py"],
strict_deps = True,
deps = [
"//tensorflow/lite/python:interpreter",
"//tensorflow/lite/python:lite",
"//tensorflow/python/eager:def_function",
],
)
cuda_py_strict_test(
name = "window_ops_test",
srcs = ["window_ops_test.py"],
shard_count = 4,
tags = [
"no_rocm",
"no_windows_gpu",
],
deps = [
":test_util",
"//tensorflow/python/framework:for_generated_wrappers",
"//tensorflow/python/framework:tensor_spec",
"//tensorflow/python/framework:test_lib",
"//tensorflow/python/ops/signal:window_ops",
"//tensorflow/python/platform:client_testlib",
"//third_party/py/numpy",
"@absl_py//absl/testing:parameterized",
],
)
@@ -0,0 +1,69 @@
# Copyright 2017 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.
# ==============================================================================
"""Test utilities for tf.signal."""
from tensorflow.lite.python import interpreter
from tensorflow.lite.python import lite
from tensorflow.python.eager import def_function
def tflite_convert(fn, input_templates):
"""Converts the provided fn to tf.lite model.
Args:
fn: A callable that expects a list of inputs like input_templates that
returns a tensor or structure of tensors.
input_templates: A list of Tensors, ndarrays or TensorSpecs describing the
inputs that fn expects. The actual values of the Tensors or ndarrays are
unused.
Returns:
The serialized tf.lite model.
"""
fn = def_function.function(fn)
concrete_func = fn.get_concrete_function(*input_templates)
converter = lite.TFLiteConverterV2([concrete_func])
return converter.convert()
def evaluate_tflite_model(tflite_model, input_ndarrays):
"""Evaluates the provided tf.lite model with the given input ndarrays.
Args:
tflite_model: bytes. The serialized tf.lite model.
input_ndarrays: A list of NumPy arrays to feed as input to the model.
Returns:
A list of ndarrays produced by the model.
Raises:
ValueError: If the number of input arrays does not match the number of
inputs the model expects.
"""
the_interpreter = interpreter.Interpreter(model_content=tflite_model)
the_interpreter.allocate_tensors()
input_details = the_interpreter.get_input_details()
output_details = the_interpreter.get_output_details()
if len(input_details) != len(input_ndarrays):
raise ValueError('Wrong number of inputs: provided=%s, '
'input_details=%s output_details=%s' % (
input_ndarrays, input_details, output_details))
for input_tensor, data in zip(input_details, input_ndarrays):
the_interpreter.set_tensor(input_tensor['index'], data)
the_interpreter.invoke()
return [the_interpreter.get_tensor(details['index'])
for details in output_details]
@@ -0,0 +1,60 @@
# Copyright 2017 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.
# ==============================================================================
"""Tests for window_ops."""
from absl.testing import parameterized
import numpy as np
from tensorflow.lite.python.kernel_tests.signal import test_util
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import tensor_spec
from tensorflow.python.framework import test_util as tf_test_util
from tensorflow.python.ops.signal import window_ops
from tensorflow.python.platform import test
@tf_test_util.run_all_in_graph_and_eager_modes
class WindowOpsTest(test.TestCase, parameterized.TestCase):
@parameterized.parameters(
# Only float32 is supported.
(window_ops.hann_window, 10, False, dtypes.float32),
(window_ops.hann_window, 10, True, dtypes.float32),
(window_ops.hamming_window, 10, False, dtypes.float32),
(window_ops.hamming_window, 10, True, dtypes.float32),
(window_ops.vorbis_window, 12, None, dtypes.float32),
)
def test_tflite_convert(self, window_fn, window_length, periodic, dtype):
def fn(window_length):
try:
return window_fn(window_length, periodic=periodic, dtype=dtype)
except TypeError:
return window_fn(window_length, dtype=dtype)
tflite_model = test_util.tflite_convert(
fn, [tensor_spec.TensorSpec(shape=[], dtype=dtypes.int32)]
)
window_length = np.array(window_length).astype(np.int32)
(actual_output,) = test_util.evaluate_tflite_model(
tflite_model, [window_length]
)
expected_output = self.evaluate(fn(window_length))
self.assertAllClose(actual_output, expected_output, rtol=1e-6, atol=1e-6)
if __name__ == '__main__':
test.main()