@@ -1824,6 +1824,190 @@ static bool test_slice_double_start() {
18241824 return ok;
18251825}
18261826
1827+ // Regression for serialized integer select arguments that alias an earlier
1828+ // floating-point scalar in the Vulkan scalar cache. A production graph has
1829+ // select calls whose dim and index both reference Double 0.0.
1830+ static void finish_select_scalar_graph (
1831+ ::flatbuffers::FlatBufferBuilder& fbb,
1832+ double dim,
1833+ double index,
1834+ uint32_t out_len,
1835+ bool symint_dim = false ) {
1836+ namespace vk = vkgraph;
1837+ std::vector<uint32_t > in_dims = {2u , 3u };
1838+ std::vector<uint32_t > out_dims = {out_len};
1839+
1840+ std::vector<::flatbuffers::Offset<vk::VkValue>> values;
1841+ values.push_back (vk::CreateVkValue (
1842+ fbb,
1843+ vk::GraphTypes::VkTensor,
1844+ vk::CreateVkTensorDirect (
1845+ fbb,
1846+ vk::VkDataType::FLOAT32 ,
1847+ &in_dims,
1848+ /* constant_id=*/ -1 ,
1849+ /* mem_obj_id=*/ 0 )
1850+ .Union ()));
1851+ if (symint_dim) {
1852+ values.push_back (vk::CreateVkValue (
1853+ fbb,
1854+ vk::GraphTypes::SymInt,
1855+ vk::CreateSymInt (fbb, /* value=*/ 0 ).Union ()));
1856+ } else {
1857+ values.push_back (vk::CreateVkValue (
1858+ fbb,
1859+ vk::GraphTypes::Double,
1860+ vk::CreateDouble (fbb, /* double_val=*/ dim).Union ()));
1861+ }
1862+ values.push_back (vk::CreateVkValue (
1863+ fbb,
1864+ vk::GraphTypes::Double,
1865+ vk::CreateDouble (fbb, /* double_val=*/ index).Union ()));
1866+ values.push_back (vk::CreateVkValue (
1867+ fbb,
1868+ vk::GraphTypes::VkTensor,
1869+ vk::CreateVkTensorDirect (
1870+ fbb,
1871+ vk::VkDataType::FLOAT32 ,
1872+ &out_dims,
1873+ /* constant_id=*/ -1 ,
1874+ /* mem_obj_id=*/ 1 )
1875+ .Union ()));
1876+
1877+ std::vector<int32_t > args = {0 , 1 , 2 , 3 };
1878+ std::vector<::flatbuffers::Offset<vk::OperatorCall>> chain;
1879+ chain.push_back (
1880+ vk::CreateOperatorCallDirect (fbb, 0 , " aten.select_copy.int" , &args));
1881+ std::vector<uint32_t > input_ids = {0 };
1882+ std::vector<uint32_t > output_ids = {3 };
1883+ auto root = vk::CreateVkGraphDirect (
1884+ fbb, " 0" , &chain, &values, &input_ids, &output_ids);
1885+ vk::FinishVkGraphBuffer (fbb, root);
1886+ }
1887+
1888+ static bool test_select_double_scalar_case (
1889+ double dim,
1890+ double index,
1891+ const std::vector<float >& expected) {
1892+ printf (
1893+ " \n --- Test: select Double scalars (dim=%g, index=%g) ---\n " , dim, index);
1894+ ::flatbuffers::FlatBufferBuilder fbb;
1895+ finish_select_scalar_graph (
1896+ fbb, dim, index, static_cast <uint32_t >(expected.size ()));
1897+
1898+ WebGPUGraph graph;
1899+ try {
1900+ graph.build (fbb.GetBufferPointer (), nullptr , nullptr );
1901+ } catch (const std::exception& e) {
1902+ printf (" FAIL: graph build threw: %s\n " , e.what ());
1903+ return false ;
1904+ }
1905+
1906+ std::vector<float > in = {0 .5f , 1 .5f , 2 .5f , 3 .5f , 4 .5f , 5 .5f };
1907+ std::vector<InputData> inputs = {
1908+ {in.data (), in.size () * sizeof (float ), false }};
1909+ std::vector<float > out (expected.size (), -1 .0f );
1910+ std::vector<std::pair<void *, size_t >> outputs = {
1911+ {out.data (), out.size () * sizeof (float )}};
1912+ try {
1913+ graph.copy_inputs (inputs);
1914+ graph.execute ();
1915+ graph.copy_outputs (outputs);
1916+ } catch (const std::exception& e) {
1917+ printf (" FAIL: select execute threw: %s\n " , e.what ());
1918+ return false ;
1919+ }
1920+
1921+ if (out != expected) {
1922+ printf (" FAIL: select Double-scalar gather mismatch\n " );
1923+ return false ;
1924+ }
1925+ printf (" PASS: select Double scalars\n " );
1926+ return true ;
1927+ }
1928+
1929+ static bool test_select_scalar_build_error (
1930+ double dim,
1931+ double index,
1932+ const char * expected_error,
1933+ bool symint_dim = false ) {
1934+ ::flatbuffers::FlatBufferBuilder fbb;
1935+ finish_select_scalar_graph (fbb, dim, index, /* out_len=*/ 3u , symint_dim);
1936+
1937+ WebGPUGraph graph;
1938+ try {
1939+ graph.build (fbb.GetBufferPointer (), nullptr , nullptr );
1940+ } catch (const std::exception& e) {
1941+ const std::string error = e.what ();
1942+ if (error.find (expected_error) != std::string::npos) {
1943+ printf (" PASS: rejected with expected error: %s\n " , e.what ());
1944+ return true ;
1945+ }
1946+ printf (
1947+ " FAIL: rejection error %s did not contain %s\n " ,
1948+ e.what (),
1949+ expected_error);
1950+ return false ;
1951+ }
1952+ printf (" FAIL: expected graph build to reject select scalars\n " );
1953+ return false ;
1954+ }
1955+
1956+ static bool test_select_double_scalars () {
1957+ constexpr double kInt64Limit = 0x1p63;
1958+ const double below_int64_min =
1959+ std::nextafter (-kInt64Limit , -std::numeric_limits<double >::infinity ());
1960+
1961+ bool ok = true ;
1962+ // Production artifact representation: both schema-int arguments alias 0.0.
1963+ ok = test_select_double_scalar_case (
1964+ /* dim=*/ 0.0 , /* index=*/ 0.0 , {0 .5f , 1 .5f , 2 .5f }) &&
1965+ ok;
1966+ // Negative Double indices retain normal select index normalization.
1967+ ok = test_select_double_scalar_case (
1968+ /* dim=*/ 1.0 , /* index=*/ -1.0 , {2 .5f , 5 .5f }) &&
1969+ ok;
1970+
1971+ ok = test_select_scalar_build_error (
1972+ /* dim=*/ 0.5 ,
1973+ /* index=*/ 0.0 ,
1974+ " select: non-integral dim" ) &&
1975+ ok;
1976+ ok = test_select_scalar_build_error (
1977+ /* dim=*/ 0.0 ,
1978+ /* index=*/ 0.5 ,
1979+ " select: non-integral index" ) &&
1980+ ok;
1981+ ok = test_select_scalar_build_error (
1982+ std::numeric_limits<double >::quiet_NaN (),
1983+ /* index=*/ 0.0 ,
1984+ " select: non-integral dim" ) &&
1985+ ok;
1986+ ok = test_select_scalar_build_error (
1987+ std::numeric_limits<double >::infinity (),
1988+ /* index=*/ 0.0 ,
1989+ " select: non-integral dim" ) &&
1990+ ok;
1991+ ok = test_select_scalar_build_error (
1992+ kInt64Limit , /* index=*/ 0.0 , " select: non-integral dim" ) &&
1993+ ok;
1994+ ok = test_select_scalar_build_error (
1995+ below_int64_min, /* index=*/ 0.0 , " select: non-integral dim" ) &&
1996+ ok;
1997+ // -2^63 is representable: conversion succeeds, then the normal dim check
1998+ // rejects it as out of range for this rank-2 input.
1999+ ok = test_select_scalar_build_error (
2000+ -kInt64Limit , /* index=*/ 0.0 , " select: dim out of range" ) &&
2001+ ok;
2002+ ok = test_select_scalar_build_error (
2003+ /* dim=*/ 0.0 ,
2004+ /* index=*/ 0.0 ,
2005+ " select: dynamic/unsupported dim" ,
2006+ /* symint_dim=*/ true ) &&
2007+ ok;
2008+ return ok;
2009+ }
2010+
18272011// apply_rotary_emb on-GPU configs: multi + decode (env-gated, run-if-present).
18282012struct RopeConfig {
18292013 const char * name;
@@ -2009,6 +2193,11 @@ TEST(WebGPUNative, SliceDoubleStart) {
20092193 << " slice Double-start gather/reject checks failed" ;
20102194}
20112195
2196+ TEST (WebGPUNative, SelectDoubleScalars) {
2197+ EXPECT_TRUE (test_select_double_scalars ())
2198+ << " select Double-scalar compatibility checks failed" ;
2199+ }
2200+
20122201TEST (WebGPUNative, Prepack2) {
20132202 if (g_prepack2_model_path.empty () || g_prepack2_golden_path.empty ()) {
20142203 GTEST_SKIP () << " WEBGPU_TEST_PREPACK2_MODEL/GOLDEN not set" ;
0 commit comments