"""Tests for server-side _resolve_3party_api (KERNEL dual-form port)."""
import pytest
from ttk.remote.server import executor
torch = pytest.importorskip("torch")
def test_torch_resolve_snake():
f, name = executor._resolve_3party_api("add", "Add", "torch")
assert callable(f)
assert name == "add"
def test_torch_resolve_exception_dict():
f, name = executor._resolve_3party_api("kl_div", "KlDiv", "torch")
assert callable(f)
assert name == "kl_div"
def test_torch_resolve_camel_is_nop():
f, name = executor._resolve_3party_api("add", "Add", "torch")
assert callable(f)
assert name == "add"
def test_resolve_returns_none_when_unresolvable():
f, name = executor._resolve_3party_api("totally_bogus_xyz", "TotallyBogusXyz", "torch")
assert f is None and name is None
def test_resolve_callable_api_none_routes_to_3party():
f, label = executor._resolve_callable("api", "torch", None, "add", "Add", None, None)
assert callable(f)
assert label == "torch.add"
def test_resolve_callable_explicit_api_still_works():
f, label = executor._resolve_callable("api", "torch", "torch.nn.functional.softmax",
"ignored", "Ignored", None, None)
assert callable(f)
assert label == "torch.nn.functional.softmax"
def test_torch_resolve_suffix_stripping_v2():
f = executor._torch_resolve("add_v2")
assert callable(f)
def test_torch_func_exception_dict_floor_div_is_partial():
f = executor._torch_resolve("floor_div")
assert callable(f)
def test_aclnn_resolve_strips_prefix_and_finds_torch():
f, name = executor._aclnn_resolve("aclnnAdd")
assert callable(f)
assert name == "add"
def test_aclnn_resolve_recursive_suffix():
f, name = executor._aclnn_resolve("aclnnAdd")
assert callable(f)
assert name == "add"
def test_resolve_3party_aclnn_branch():
f, name = executor._resolve_3party_api("aclnnAdd", None, "torch")
assert callable(f)
assert name == "add"
def test_camel_to_snake_copied_locally():
assert hasattr(executor, "camel_to_snake")
assert executor.camel_to_snake("BatchNormV3") == "batch_norm_v3"
def test_aclnn_resolve_unresolvable_returns_none():
f, name = executor._aclnn_resolve("aclnnTotallyBogusXyz")
assert f is None and name is None
def test_resolve_3party_skips_empty_names():
f, name = executor._resolve_3party_api(None, None, "torch")
assert f is None and name is None
f, name = executor._resolve_3party_api("", "", "torch")
assert f is None and name is None
import importlib.util as _ilu, subprocess as _sp, sys as _sys
_tf_ok = False
if _ilu.find_spec("tensorflow") is not None:
try:
_tf_ok = _sp.run([_sys.executable, "-c", "import tensorflow"],
capture_output=True, timeout=90).returncode == 0
except Exception:
_tf_ok = False
if not _tf_ok:
pytest.skip("tensorflow not importable (crashes on import)", allow_module_level=True)
tf = pytest.importorskip("tensorflow")
def test_tf_resolve_camel():
f, name = executor._resolve_3party_api("relu", "Relu", "tf")
assert callable(f)
assert name in ("relu", "Relu")
def test_tf_resolve_snake():
f, name = executor._resolve_3party_api("multiply", "Multiply", "tf")
assert callable(f)
assert name == "multiply"
def test_tf_resolve_exception_dict_batch_norm_v3():
f, name = executor._resolve_3party_api("batch_norm_v3", "BatchNormV3", "tf")
assert callable(f)
assert name == "batch_norm_v3"
def test_tf_resolve_exception_dict_spence():
f, name = executor._resolve_3party_api("spence", "Spence", "tf")
assert callable(f)
assert name == "spence"
def test_tf_resolve_exception_dict_space_to_batch():
f, name = executor._resolve_3party_api("space_to_batch", "SpaceToBatch", "tf")
assert callable(f)
assert name == "space_to_batch"
def test_tf_resolve_suffix_stripping_v2():
f = executor._tf_resolve("Relu_v2")
assert callable(f)
def test_tf_resolve_returns_none_when_unresolvable():
f, name = executor._resolve_3party_api("totally_bogus_xyz", "TotallyBogusXyz", "tf")
assert f is None and name is None
def test_resolve_callable_api_none_routes_to_3party_tf():
f, label = executor._resolve_callable("api", "tf", None, "relu", "Relu", None, None)
assert callable(f)
assert label in ("tf.relu", "tf.Relu")
def test_resolve_callable_explicit_api_still_works_tf():
f, label = executor._resolve_callable("api", "tf", "tf.nn.relu",
"ignored", "Ignored", None, None)
assert callable(f)
assert label == "tf.nn.relu"