ReplicationPad2d

https://pytorch.org/docs/stable/generated/torch.nn.ReplicationPad2d.html

replication_pad2d_demo.py
import svetoviz_webgpu as sv

# 1. Prepare 2D image data
img_np = np.array(img).astype(np.float32) / 255.0
input_tensor = torch.from_numpy(img_np).permute(2, 0, 1).unsqueeze(0)

# 2. Define ReplicationPad2d (20-pixel repeated border)
pad2d = nn.ReplicationPad2d(padding=20)

def terminal_callback(buffer, message, images, files):
    output = pad2d(input_tensor)

    buffer.send_system_message(f"Original Resolution: {input_tensor.shape[2:]}")
    buffer.send_system_message(f"Padded Resolution: {output.shape[2:]}")
    buffer.send_system_message("Border created by repeating the outermost edge pixels.")

# 3. Start the interactive session
sv.pytorch_web(module=pad2d, terminal_callback=terminal_callback)
Spatial Boundary Extension
ReplicationPad2d border repetition visualization
In 2D, the edge pixels are extruded outwards. This creates a "stretched" border effect, effectively keeping the boundary gradients near zero during spatial operations.