Skip to content

Commit cdb2234

Browse files
committed
Fix no-op exploit in 19 KernelBench baselines: zero-init weights -> random
18k-50k KernelBench-derived baselines initialized their weight/bias tensors to jnp.zeros in create_inputs. With zero weights, `x @ W (+ b)` is identically zero and independent of the input, so the reference output is a trivial constant (all-zero, or a fixed activation thereof). Any kernel returning that constant -- including a no-op that skips the matmul/conv entirely -- passes np.allclose, so these benchmarks could report large meaningless speedups without computing the operator. This replaces the zero-init weights/biases with small-normal random values (~0.02 scale: input-dependent, bf16-representable, no overflow). Only create_inputs is changed; the workload/op is untouched. After the fix a no-op (all-zero output) fails correctness on all 19. Scope: 19 of the affected baselines are fully fixed by non-zero weights. Five others whose *output* is intrinsically small regardless of weights -- the softmax-terminated 38k/43k/50k (row outputs ~1/N) and the structurally degenerate 25k (GroupNorm->Mean) and 42k (Max-Subtract-GELU) -- are NOT addressed here; they need a tolerance or operator change and are left to a follow-up. Megablox (11p) has a distinct input-underflow variant fixed separately.
1 parent d43d848 commit cdb2234

19 files changed

Lines changed: 81 additions & 43 deletions

File tree

JAXBench/benchmark/18k_Conv2D_ReLU_BiasAdd/baseline.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
def create_inputs(dtype=jnp.float32):
1515
"""Create all inputs including weights."""
1616
key = jax.random.key(0)
17+
rand_key = jax.random.key(0xBADC0DE)
18+
ka, kb, kc = jax.random.split(rand_key, 3)
1719
k1, k2 = jax.random.split(key)
1820
batch_size, in_channels, out_channels, kernel_size = 128, 64, 128, 3
1921
height = width = 128
2022
x = jax.random.uniform(k1, (batch_size, in_channels, height, width), dtype=dtype)
21-
weight = jnp.zeros((out_channels, in_channels, kernel_size, kernel_size), dtype=dtype)
22-
conv_bias = jnp.zeros(out_channels, dtype=dtype)
23-
bias = jnp.zeros((out_channels, 1, 1), dtype=dtype)
23+
weight = jax.random.normal(ka, (out_channels, in_channels, kernel_size, kernel_size), dtype=dtype) * 0.02
24+
conv_bias = jax.random.normal(kb, out_channels, dtype=dtype) * 0.02
25+
bias = jax.random.normal(kc, (out_channels, 1, 1), dtype=dtype) * 0.02
2426
return x, weight, conv_bias, bias
2527

2628

JAXBench/benchmark/19k_Matmul_Subtract_Multiply_ReLU/baseline.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
def create_inputs(dtype=jnp.float32):
1616
"""Create all inputs including weights."""
1717
key = jax.random.key(0)
18+
rand_key = jax.random.key(0xBADC0DE)
19+
ka, kb = jax.random.split(rand_key, 2)
1820
x = jax.random.uniform(key, (4096, 8192), dtype=dtype)
19-
weight = jnp.zeros((8192, 8192), dtype=dtype)
20-
bias = jnp.zeros(8192, dtype=dtype)
21+
weight = jax.random.normal(ka, (8192, 8192), dtype=dtype) * 0.02
22+
bias = jax.random.normal(kb, 8192, dtype=dtype) * 0.02
2123
return x, weight, bias
2224

2325

JAXBench/benchmark/20k_Gemm_Multiply_LeakyReLU/baseline.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
def create_inputs(dtype=jnp.float32):
1616
"""Create all inputs including weights."""
1717
key = jax.random.key(0)
18+
rand_key = jax.random.key(0xBADC0DE)
19+
ka, kb = jax.random.split(rand_key, 2)
1820
x = jax.random.uniform(key, (4096, 8192), dtype=dtype)
19-
weight = jnp.zeros((8192, 8192), dtype=dtype)
20-
bias = jnp.zeros(8192, dtype=dtype)
21+
weight = jax.random.normal(ka, (8192, 8192), dtype=dtype) * 0.02
22+
bias = jax.random.normal(kb, 8192, dtype=dtype) * 0.02
2123
return x, weight, bias
2224

2325

JAXBench/benchmark/22k_Conv2d_InstanceNorm_Divide/baseline.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
def create_inputs(dtype=jnp.float32):
1616
"""Create all inputs including weights."""
1717
key = jax.random.key(0)
18+
rand_key = jax.random.key(0xBADC0DE)
19+
ka, kb = jax.random.split(rand_key, 2)
1820
batch_size, in_channels, out_channels, kernel_size = 128, 64, 128, 3
1921
height = width = 128
2022
x = jax.random.uniform(key, (batch_size, in_channels, height, width), dtype=dtype)
21-
weight = jnp.zeros((out_channels, in_channels, kernel_size, kernel_size), dtype=dtype)
22-
conv_bias = jnp.zeros(out_channels, dtype=dtype)
23+
weight = jax.random.normal(ka, (out_channels, in_channels, kernel_size, kernel_size), dtype=dtype) * 0.02
24+
conv_bias = jax.random.normal(kb, out_channels, dtype=dtype) * 0.02
2325
in_weight = jnp.ones(out_channels, dtype=dtype)
2426
in_bias = jnp.zeros(out_channels, dtype=dtype)
2527
return x, weight, conv_bias, in_weight, in_bias

JAXBench/benchmark/23k_Matmul_Sum_Max_AvgPool_LogSumExp_LogSumExp/baseline.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
def create_inputs(dtype=jnp.float32):
1414
"""Create all inputs including weights."""
1515
key = jax.random.key(0)
16+
rand_key = jax.random.key(0xBADC0DE)
17+
ka, kb = jax.random.split(rand_key, 2)
1618
x = jax.random.uniform(key, (4096, 8192), dtype=dtype)
17-
weight = jnp.zeros((8192, 8192), dtype=dtype)
18-
bias = jnp.zeros(8192, dtype=dtype)
19+
weight = jax.random.normal(ka, (8192, 8192), dtype=dtype) * 0.02
20+
bias = jax.random.normal(kb, 8192, dtype=dtype) * 0.02
1921
return x, weight, bias
2022

2123

JAXBench/benchmark/27k_Matmul_Mish_Mish/baseline.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
def create_inputs(dtype=jnp.float32):
1414
"""Create all inputs including weights."""
1515
key = jax.random.key(0)
16+
rand_key = jax.random.key(0xBADC0DE)
17+
ka, kb = jax.random.split(rand_key, 2)
1618
x = jax.random.uniform(key, (4096, 8192), dtype=dtype)
17-
weight = jnp.zeros((8192, 8192), dtype=dtype)
18-
bias = jnp.zeros(8192, dtype=dtype)
19+
weight = jax.random.normal(ka, (8192, 8192), dtype=dtype) * 0.02
20+
bias = jax.random.normal(kb, 8192, dtype=dtype) * 0.02
1921
return x, weight, bias
2022

2123

JAXBench/benchmark/29k_Matmul_Swish_Sum_GroupNorm/baseline.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
def create_inputs(dtype=jnp.float32):
1515
"""Create all inputs including weights."""
1616
key = jax.random.key(0)
17+
rand_key = jax.random.key(0xBADC0DE)
18+
ka, kb = jax.random.split(rand_key, 2)
1719
batch_size, in_features, out_features, num_groups = 8192, 4096, 4096, 64
1820
x = jax.random.uniform(key, (batch_size, in_features), dtype=dtype)
19-
weight = jnp.zeros((in_features, out_features), dtype=dtype)
20-
bias = jnp.zeros(out_features, dtype=dtype)
21+
weight = jax.random.normal(ka, (in_features, out_features), dtype=dtype) * 0.02
22+
bias = jax.random.normal(kb, out_features, dtype=dtype) * 0.02
2123
gn_weight = jnp.ones(out_features, dtype=dtype)
2224
gn_bias = jnp.zeros(out_features, dtype=dtype)
2325
return x, weight, bias, gn_weight, gn_bias

JAXBench/benchmark/30k_Matmul_Scaling_ResidualAdd/baseline.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
def create_inputs(dtype=jnp.float32):
1515
"""Create all inputs including weights."""
1616
key = jax.random.key(0)
17+
rand_key = jax.random.key(0xBADC0DE)
18+
ka, kb = jax.random.split(rand_key, 2)
1719
x = jax.random.uniform(key, (16384, 4096), dtype=dtype)
18-
weight = jnp.zeros((4096, 4096), dtype=dtype)
19-
bias = jnp.zeros(4096, dtype=dtype)
20+
weight = jax.random.normal(ka, (4096, 4096), dtype=dtype) * 0.02
21+
bias = jax.random.normal(kb, 4096, dtype=dtype) * 0.02
2022
return x, weight, bias
2123

2224

JAXBench/benchmark/33k_Conv3d_Mish_Tanh/baseline.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
def create_inputs(dtype=jnp.float32):
1515
"""Create all inputs including weights."""
1616
key = jax.random.key(0)
17+
rand_key = jax.random.key(0xBADC0DE)
18+
ka, kb = jax.random.split(rand_key, 2)
1719
batch_size, in_channels, out_channels, kernel_size = 16, 32, 64, 3
1820
D, H, W = 32, 64, 64
1921
x = jax.random.uniform(key, (batch_size, in_channels, D, H, W), dtype=dtype)
20-
weight = jnp.zeros((out_channels, in_channels, kernel_size, kernel_size, kernel_size), dtype=dtype)
21-
bias = jnp.zeros(out_channels, dtype=dtype)
22+
weight = jax.random.normal(ka, (out_channels, in_channels, kernel_size, kernel_size, kernel_size), dtype=dtype) * 0.02
23+
bias = jax.random.normal(kb, out_channels, dtype=dtype) * 0.02
2224
return x, weight, bias
2325

2426

JAXBench/benchmark/35k_Gemm_Scaling_Hardtanh_GELU/baseline.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
def create_inputs(dtype=jnp.float32):
1717
"""Create all inputs including weights."""
1818
key = jax.random.key(0)
19+
rand_key = jax.random.key(0xBADC0DE)
20+
ka, kb = jax.random.split(rand_key, 2)
1921
x = jax.random.uniform(key, (4096, 8192), dtype=dtype)
20-
weight = jnp.zeros((8192, 8192), dtype=dtype)
21-
bias = jnp.zeros(8192, dtype=dtype)
22+
weight = jax.random.normal(ka, (8192, 8192), dtype=dtype) * 0.02
23+
bias = jax.random.normal(kb, 8192, dtype=dtype) * 0.02
2224
return x, weight, bias
2325

2426

0 commit comments

Comments
 (0)