CircularPad2d

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

circular_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 CircularPad2d
# Adds a 30-pixel wrap-around border
pad2d = nn.CircularPad2d(padding=30)

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 content is wrapped from the opposite edges of the image.")

# 3. Start the interactive session
sv.pytorch_web(module=pad2d, terminal_callback=terminal_callback)
Toroidal Spatial Mapping
CircularPad2d seamless tiling visualization
In 2D, circular padding effectively maps the image onto a torus. The left edge meets the right, and the top meets the bottom, creating a seamless tiling effect used often in physics simulations or texture synthesis.