chore: import upstream snapshot with attribution
Docker Image CI / build-ubuntu2004 (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 13:36:55 +08:00
commit c8a779b1bb
1887 changed files with 3245738 additions and 0 deletions
@@ -0,0 +1,81 @@
#
# SPDX-FileCopyrightText: Copyright (c) 1993-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# 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.
#
import os
import tempfile
import pytest
from polygraphy import constants, util
from polygraphy.backend.tf import (
GraphFromFrozen,
ModifyGraphOutputs,
SaveGraph,
graph_from_frozen,
)
from polygraphy.logger import G_LOGGER
from tests.helper import is_file_non_empty
from tests.models.meta import TF_MODELS
tf = pytest.importorskip("tensorflow")
class TestLoggerCallbacks:
@pytest.mark.parametrize("sev", G_LOGGER.SEVERITY_LETTER_MAPPING.keys())
def test_set_severity(self, sev):
G_LOGGER.module_severity = sev
class TestFrozenGraphLoader:
def test_load_graph(self):
with tf.compat.v1.Graph().as_default() as graph:
inp = tf.placeholder(shape=(1, 1, 1, 1), dtype=tf.float32)
out = tf.identity(inp)
graph, outputs = graph_from_frozen(graph)
assert graph
assert outputs
def test_load_pb(self):
tf_loader = GraphFromFrozen(TF_MODELS["identity"].path)
tf_loader()
class TestModifyGraph:
def test_layerwise(self):
load_frozen = GraphFromFrozen(TF_MODELS["identity"].path)
modify_tf = ModifyGraphOutputs(load_frozen, outputs=constants.MARK_ALL)
graph, outputs = modify_tf()
assert graph
assert outputs
class TestSaveGraph:
def test_save_pb(self):
with util.NamedTemporaryFile() as outpath:
tf_loader = SaveGraph(
GraphFromFrozen(TF_MODELS["identity"].path), path=outpath.name
)
tf_loader()
assert is_file_non_empty(outpath.name)
def test_save_tensorboard(self):
with tempfile.TemporaryDirectory() as outdir:
tf_loader = SaveGraph(
GraphFromFrozen(TF_MODELS["identity"].path), tensorboard_dir=outdir
)
tf_loader()
assert os.path.exists(tf_loader.tensorboard_dir)
@@ -0,0 +1,93 @@
#
# SPDX-FileCopyrightText: Copyright (c) 1993-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# 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.
#
import numpy as np
import pytest
from polygraphy import util
from polygraphy.backend.tf import SessionFromGraph, TfRunner
from polygraphy.exception import PolygraphyException
from tests.helper import is_file_non_empty
from tests.models.meta import TF_MODELS
pytest.importorskip("tensorflow")
class TestTfRunner:
def test_can_name_runner(self):
NAME = "runner"
runner = TfRunner(None, name=NAME)
assert runner.name == NAME
def test_basic(self):
model = TF_MODELS["identity"]
with TfRunner(SessionFromGraph(model.loader)) as runner:
assert runner.is_active
model.check_runner(runner)
assert runner.last_inference_time() is not None
assert not runner.is_active
@pytest.mark.serial
def test_warn_if_impl_methods_called(self, check_warnings_on_runner_impl_methods):
model = TF_MODELS["identity"]
runner = TfRunner(SessionFromGraph(model.loader))
check_warnings_on_runner_impl_methods(runner)
@pytest.mark.skip(reason="Non-trivial to set up - requires CUPTI")
def test_save_timeline(self):
model = TF_MODELS["identity"]
with util.NamedTemporaryFile() as outpath:
with TfRunner(
SessionFromGraph(model.loader),
allow_growth=True,
save_timeline=outpath.name,
) as runner:
model.check_runner(runner)
assert is_file_non_empty(outpath.name)
@pytest.mark.parametrize(
"names, err",
[
(["fake-input", "Input:0"], "Extra inputs in"),
(["fake-input"], "The following inputs were not found"),
([], "The following inputs were not found"),
],
)
def test_error_on_wrong_name_feed_dict(self, names, err):
model = TF_MODELS["identity"]
with TfRunner(SessionFromGraph(model.loader)) as runner:
with pytest.raises(PolygraphyException, match=err):
runner.infer(
{
name: np.ones(shape=(1, 15, 25, 30), dtype=np.float32)
for name in names
}
)
def test_error_on_wrong_dtype_feed_dict(self):
model = TF_MODELS["identity"]
with TfRunner(SessionFromGraph(model.loader)) as runner:
with pytest.raises(PolygraphyException, match="unexpected dtype."):
runner.infer(
{"Input:0": np.ones(shape=(1, 15, 25, 30), dtype=np.int32)}
)
def test_error_on_wrong_shape_feed_dict(self):
model = TF_MODELS["identity"]
with TfRunner(SessionFromGraph(model.loader)) as runner:
with pytest.raises(PolygraphyException, match="incompatible shape."):
runner.infer(
{"Input:0": np.ones(shape=(1, 1, 25, 30), dtype=np.float32)}
)