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,30 @@
load("//tensorflow:pytype.default.bzl", "pytype_strict_library")
load(
"//tensorflow:tensorflow.default.bzl",
"tf_py_strict_test",
)
package(
# copybara:uncomment default_applicable_licenses = ["@stablehlo//:license"],
default_visibility = [
"//tensorflow:__pkg__",
"//tensorflow/compiler/mlir/quantization/stablehlo/python:internal_visibility_allowlist_package",
],
licenses = ["notice"],
)
pytype_strict_library(
name = "testing",
srcs = ["testing.py"],
tags = ["no_pip"],
visibility = ["//visibility:public"],
)
tf_py_strict_test(
name = "testing_test",
srcs = ["testing_test.py"],
deps = [
":testing",
"//tensorflow/python/platform:client_testlib",
],
)
@@ -0,0 +1,69 @@
# 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.
# ==============================================================================
"""Common testing utilities for quantization libraries."""
import itertools
import os
from typing import Any, Mapping, Sequence
def parameter_combinations(
test_parameters: Sequence[Mapping[str, Sequence[Any]]]
) -> Sequence[Mapping[str, Any]]:
"""Generate all combinations of test parameters.
Args:
test_parameters: List of dictionaries that maps parameter keys and values.
Returns:
real_parameters: All possible combinations of the parameters as list of
dictionaries.
"""
real_parameters = []
for parameters in test_parameters:
keys = parameters.keys()
for curr in itertools.product(*parameters.values()):
real_parameters.append(dict(zip(keys, curr)))
return real_parameters
def get_dir_size(path: str = '.') -> int:
"""Get the total size of files and sub-directories under the path.
Args:
path: Path of a directory or a file to calculate the total size.
Returns:
Total size of the directory or a file.
"""
total = 0
for root, _, files in os.walk(path):
for filename in files:
total += os.path.getsize(os.path.join(root, filename))
return total
def get_size_ratio(path_a: str, path_b: str) -> float:
"""Return the size ratio of the given paths.
Args:
path_a: Path of a directory or a file to be the nominator of the ratio.
path_b: Path of a directory or a file to be the denominator of the ratio.
Returns:
Ratio of size of path_a / size of path_b.
"""
size_a = get_dir_size(path_a)
size_b = get_dir_size(path_b)
return size_a / size_b
@@ -0,0 +1,63 @@
# 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.
# ==============================================================================
from tensorflow.compiler.mlir.quantization.common.python import testing
from tensorflow.python.platform import test
class TestingTest(test.TestCase):
def test_parameter_combinations(self):
"""Tests that parameter_combinations returns correct combinations."""
test_parameters = [{
'shapes': [
[3, 3],
[3, None],
],
'has_bias': [True, False],
}]
combinations = testing.parameter_combinations(test_parameters)
self.assertLen(combinations, 4)
self.assertIn({'shapes': [3, 3], 'has_bias': True}, combinations)
self.assertIn({'shapes': [3, 3], 'has_bias': False}, combinations)
self.assertIn({'shapes': [3, None], 'has_bias': True}, combinations)
self.assertIn({'shapes': [3, None], 'has_bias': False}, combinations)
class FileSizeTestCase(test.TestCase):
def setUp(self):
super().setUp()
self.path_a = self.create_tempdir('dir_a').full_path
self.create_tempfile(file_path='dir_a/w.txt', content='abcd')
self.path_b = self.create_tempdir('dir_b').full_path
self.create_tempfile(file_path='dir_b/x.txt', content='1234')
self.create_tempfile(file_path='dir_b/y.txt', content='56')
self.create_tempfile(file_path='dir_b/z.txt', content='78')
def test_get_dir_size(self):
self.assertEqual(testing.get_dir_size(self.path_a), 4)
self.assertEqual(testing.get_dir_size(self.path_b), 8)
def test_get_size_ratio(self):
self.assertEqual(testing.get_size_ratio(self.path_a, self.path_b), 0.5)
self.assertEqual(testing.get_size_ratio(self.path_b, self.path_a), 2.0)
if __name__ == '__main__':
test.main()