diff --git a/benchmarks/NPUKernelBench/level3/10_ConvTranspose2d.py b/benchmarks/NPUKernelBench/level3/10_ConvTranspose2d.py index 952678e7..d27ff81e 100644 --- a/benchmarks/NPUKernelBench/level3/10_ConvTranspose2d.py +++ b/benchmarks/NPUKernelBench/level3/10_ConvTranspose2d.py @@ -10,7 +10,7 @@ class Model(nn.Module): def __init__(self): super(Model, self).__init__() - def forward(self, x: torch.Tensor, in_channels, out_channels, kernel_size, stride=1, padding=0, bias=True) -> torch.Tensor: + def forward(self, x: torch.Tensor, in_channels, out_channels, kernel_size, stride=1, padding=0, bias=True, seed=0) -> torch.Tensor: """ Applies transpose 2D convolution to the input tensor. @@ -22,11 +22,15 @@ def forward(self, x: torch.Tensor, in_channels, out_channels, kernel_size, strid stride (int or tuple, optional): Stride of the convolution. Default: 1. padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 0. bias (bool, optional): If True, adds a learnable bias to the output. Default: True. + seed (int, optional): RNG seed for deterministic conv weight init. Required so + ref Model.forward and candidate ModelNew.forward produce IDENTICAL random + weights when invoked back-to-back inside one verification case. Default: 0. Returns: torch.Tensor: Output tensor after performing nn.ConvTranspose2d. """ - conv = nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, bias=bias) + torch.manual_seed(seed) + conv = nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, bias=bias).to(x.device) return conv(x) @@ -57,8 +61,9 @@ def get_input_groups(): stride = attr_inputs.get("stride", 1) padding = attr_inputs.get("padding", 0) bias = attr_inputs.get("bias", True) - - input_groups.append([x, in_channels, out_channels, kernel_size, stride, padding, bias]) + seed = attr_inputs.get("seed", 0) + + input_groups.append([x, in_channels, out_channels, kernel_size, stride, padding, bias, seed]) return input_groups diff --git a/benchmarks/NPUKernelBench/level3/6_ConvStandard1d.py b/benchmarks/NPUKernelBench/level3/6_ConvStandard1d.py index cf74fe6c..83a6fd80 100644 --- a/benchmarks/NPUKernelBench/level3/6_ConvStandard1d.py +++ b/benchmarks/NPUKernelBench/level3/6_ConvStandard1d.py @@ -10,7 +10,7 @@ class Model(nn.Module): def __init__(self): super(Model, self).__init__() - def forward(self, x: torch.Tensor, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True) -> torch.Tensor: + def forward(self, x: torch.Tensor, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, seed=0) -> torch.Tensor: """ Applies standard 1D convolution to the input tensor. @@ -24,11 +24,17 @@ def forward(self, x: torch.Tensor, in_channels, out_channels, kernel_size, strid dilation (int or tuple, optional): Spacing between kernel elements. Default: 1. groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1. bias (bool, optional): If True, adds a learnable bias to the output. Default: True. + seed (int, optional): RNG seed used to initialize conv weights deterministically. + Required so that ref Model.forward and candidate ModelNew.forward produce + IDENTICAL random weights when the verification harness invokes both + back-to-back inside one test case. Without this, RNG advances between the + two calls and ref ≠ cand by construction. Default: 0. Returns: torch.Tensor: Output tensor after performing nn.Conv1d. """ - conv = nn.Conv1d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias) + torch.manual_seed(seed) + conv = nn.Conv1d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias).to(x.device) return conv(x) @@ -61,8 +67,9 @@ def get_input_groups(): dilation = attr_inputs.get("dilation", 1) groups = attr_inputs.get("groups", 1) bias = attr_inputs.get("bias", True) - - input_groups.append([x, in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias]) + seed = attr_inputs.get("seed", 0) + + input_groups.append([x, in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias, seed]) return input_groups diff --git a/benchmarks/NPUKernelBench/level3/7_ConvStandard2d.py b/benchmarks/NPUKernelBench/level3/7_ConvStandard2d.py index 4a6763b0..3f26238a 100644 --- a/benchmarks/NPUKernelBench/level3/7_ConvStandard2d.py +++ b/benchmarks/NPUKernelBench/level3/7_ConvStandard2d.py @@ -10,7 +10,7 @@ class Model(nn.Module): def __init__(self): super(Model, self).__init__() - def forward(self, x: torch.Tensor, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True) -> torch.Tensor: + def forward(self, x: torch.Tensor, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, seed=0) -> torch.Tensor: """ Applies standard 2D convolution to the input tensor. @@ -24,11 +24,15 @@ def forward(self, x: torch.Tensor, in_channels, out_channels, kernel_size, strid dilation (int or tuple, optional): Spacing between kernel elements. Default: 1. groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1. bias (bool, optional): If True, adds a learnable bias to the output. Default: True. + seed (int, optional): RNG seed for deterministic conv weight init. Required so + ref Model.forward and candidate ModelNew.forward produce IDENTICAL random + weights when invoked back-to-back inside one verification case. Default: 0. Returns: torch.Tensor: Output tensor after performing nn.Conv2d. """ - conv = nn.Conv2d(in_channels, out_channels, (kernel_size, kernel_size), stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias) + torch.manual_seed(seed) + conv = nn.Conv2d(in_channels, out_channels, (kernel_size, kernel_size), stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias).to(x.device) return conv(x) @@ -61,8 +65,9 @@ def get_input_groups(): dilation = attr_inputs.get("dilation", 1) groups = attr_inputs.get("groups", 1) bias = attr_inputs.get("bias", True) - - input_groups.append([x, in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias]) + seed = attr_inputs.get("seed", 0) + + input_groups.append([x, in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias, seed]) return input_groups diff --git a/benchmarks/NPUKernelBench/level3/8_ConvStandard3d.py b/benchmarks/NPUKernelBench/level3/8_ConvStandard3d.py index b27d34eb..7ce6dac3 100644 --- a/benchmarks/NPUKernelBench/level3/8_ConvStandard3d.py +++ b/benchmarks/NPUKernelBench/level3/8_ConvStandard3d.py @@ -10,7 +10,7 @@ class Model(nn.Module): def __init__(self): super(Model, self).__init__() - def forward(self, x: torch.Tensor, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True) -> torch.Tensor: + def forward(self, x: torch.Tensor, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, seed=0) -> torch.Tensor: """ Applies standard 3D convolution to the input tensor. @@ -24,11 +24,15 @@ def forward(self, x: torch.Tensor, in_channels, out_channels, kernel_size, strid dilation (int or tuple, optional): Spacing between kernel elements. Default: 1. groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1. bias (bool, optional): If True, adds a learnable bias to the output. Default: True. + seed (int, optional): RNG seed for deterministic conv weight init. Required so + ref Model.forward and candidate ModelNew.forward produce IDENTICAL random + weights when invoked back-to-back inside one verification case. Default: 0. Returns: torch.Tensor: Output tensor after performing nn.Conv3d. """ - conv = nn.Conv3d(in_channels, out_channels, (kernel_size, kernel_size, 1), stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias) + torch.manual_seed(seed) + conv = nn.Conv3d(in_channels, out_channels, (kernel_size, kernel_size, 1), stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias).to(x.device) return conv(x) @@ -61,8 +65,9 @@ def get_input_groups(): dilation = attr_inputs.get("dilation", 1) groups = attr_inputs.get("groups", 1) bias = attr_inputs.get("bias", True) - - input_groups.append([x, in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias]) + seed = attr_inputs.get("seed", 0) + + input_groups.append([x, in_channels, out_channels, kernel_size, stride, padding, dilation, groups, bias, seed]) return input_groups diff --git a/benchmarks/NPUKernelBench/level3/9_ConvDepthwise2d.py b/benchmarks/NPUKernelBench/level3/9_ConvDepthwise2d.py index a61c0232..70557e47 100644 --- a/benchmarks/NPUKernelBench/level3/9_ConvDepthwise2d.py +++ b/benchmarks/NPUKernelBench/level3/9_ConvDepthwise2d.py @@ -10,7 +10,7 @@ class Model(nn.Module): def __init__(self): super(Model, self).__init__() - def forward(self, x: torch.Tensor, in_channels, kernel_size, stride=1, padding=0, bias=True) -> torch.Tensor: + def forward(self, x: torch.Tensor, in_channels, kernel_size, stride=1, padding=0, bias=True, seed=0) -> torch.Tensor: """ Applies depthwise 2D convolution to the input tensor. @@ -21,11 +21,15 @@ def forward(self, x: torch.Tensor, in_channels, kernel_size, stride=1, padding=0 stride (int or tuple, optional): Stride of the convolution. Default: 1. padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 0. bias (bool, optional): If True, adds a learnable bias to the output. Default: True. + seed (int, optional): RNG seed for deterministic conv weight init. Required so + ref Model.forward and candidate ModelNew.forward produce IDENTICAL random + weights when invoked back-to-back inside one verification case. Default: 0. Returns: torch.Tensor: Output tensor of shape (batch, in_channels, height, width) after performing depthwise nn.Conv2d. Depthwise convolution processes each input channel independently, so output channels equal input channels. """ - conv = nn.Conv2d(in_channels, in_channels, kernel_size=(kernel_size, kernel_size), stride=stride, padding=padding, groups=in_channels, bias=bias) + torch.manual_seed(seed) + conv = nn.Conv2d(in_channels, in_channels, kernel_size=(kernel_size, kernel_size), stride=stride, padding=padding, groups=in_channels, bias=bias).to(x.device) return conv(x) @@ -55,8 +59,9 @@ def get_input_groups(): stride = attr_inputs.get("stride", 1) padding = attr_inputs.get("padding", 0) bias = attr_inputs.get("bias", True) - - input_groups.append([x, in_channels, kernel_size, stride, padding, bias]) + seed = attr_inputs.get("seed", 0) + + input_groups.append([x, in_channels, kernel_size, stride, padding, bias, seed]) return input_groups