Skip to content

Commit 42a178f

Browse files
authored
Accept integral Double select scalars (pytorch#21387)
Differential Revision: D113588820 Pull Request resolved: pytorch#21387
1 parent 80925df commit 42a178f

2 files changed

Lines changed: 209 additions & 4 deletions

File tree

‎backends/webgpu/runtime/ops/select/Select.cpp‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <webgpu/webgpu.h>
1616

17+
#include <cmath>
1718
#include <cstdint>
1819
#include <stdexcept>
1920
#include <string>
@@ -29,12 +30,27 @@ struct SelectParams {
2930
uint32_t _pad[2];
3031
};
3132

32-
// dim/index are required Ints (SymInt throws); no Null default unlike slice.
33+
// dim/index are static integer scalars (SymInt throws); Vulkan serialization
34+
// can reuse an earlier Double value for an equal integer scalar.
3335
int64_t read_scalar(WebGPUGraph& graph, int id, const char* what) {
34-
if (graph.get_value_type(id) == WebGPUGraph::ValueType::Int) {
35-
return graph.get_int(id);
36+
switch (graph.get_value_type(id)) {
37+
case WebGPUGraph::ValueType::Int:
38+
return graph.get_int(id);
39+
case WebGPUGraph::ValueType::Double: {
40+
const double d = graph.get_double(id);
41+
constexpr double kInt64Limit = 0x1p63;
42+
// Check every invalid case before casting: floating-to-integer conversion
43+
// is undefined for out-of-range values, and fractional values must not be
44+
// silently rounded or truncated. std::trunc(NaN) != NaN rejects NaN.
45+
if (d < -kInt64Limit || d >= kInt64Limit || std::trunc(d) != d) {
46+
throw std::runtime_error(std::string("select: non-integral ") + what);
47+
}
48+
return static_cast<int64_t>(d);
49+
}
50+
default:
51+
throw std::runtime_error(
52+
std::string("select: dynamic/unsupported ") + what);
3653
}
37-
throw std::runtime_error(std::string("select: dynamic/unsupported ") + what);
3854
}
3955

4056
// Build a TensorMeta from live dims, write it to buf, return numel.

‎backends/webgpu/test/test_webgpu_native.cpp‎

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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).
18282012
struct 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+
20122201
TEST(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

Comments
 (0)