You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups.  

You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination.  

Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is:  

python
import torch
import torch.nn as nn
import torch.nn.functional as F

class Model(nn.Module):
def init(self) -> None:
super().init()

def forward(self, a, b):
    return a + b
def get_inputs():
# randomly generate input tensors based on the model architecture
a = torch.randn(1, 128).cuda()
b = torch.randn(1, 128).cuda()
return [a, b]

def get_init_inputs():
# randomly generate tensors required for initialization based on the model architecture
return []


  
The example new arch with custom CUDA kernels looks like this:   
python
import torch
from torch.utils.cpp_extension import load_inline

… CUDA C++ source code for the kernel …
relu_source = “”"
…
“”"

relu_cpp_source = “”"
torch::Tensor relu_cuda(torch::Tensor x);
“”"

Compile the inline CUDA code
relu = load_inline(
name=“relu”,
cpp_sources=relu_cpp_source,
cuda_sources=relu_source,
functions=[“relu_cuda”],
verbose=True
)

class ModelNew(torch.nn.Module):
def init(self):
super(ModelNew, self).init()
self.relu = relu # The module containing the kernel

def forward(self, x):
    return self.relu.relu_cuda(x)


You are given the following architecture:   
  
python
import torch
import torch.nn as nn

class Model(nn.Module):
“”"
Canberra Distance implementation.
Computes the Canberra distance between two sets of vectors.
“”"
def init(self):
super(Model, self).init()

def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:  
    """  
    Compute Canberra distance between x and y.

    Args:  
        x (torch.Tensor): First set of vectors [batch_size, feature_dim]
        y (torch.Tensor): Second set of vectors [batch_size, feature_dim]

    Returns:  
        torch.Tensor: Canberra distances [batch_size]
    """  
    # Input validation
    if x.shape != y.shape:
        raise ValueError(f"Input tensors must have the same shape, got {x.shape} and {y.shape}")
    
    if x.dim() != 2:
        raise ValueError(f"Input tensors must be 2D, got {x.dim()}D")
    
    # Compute Canberra distance: Σ(|x_i - y_i| / (|x_i| + |y_i|))
    # Step 1: Compute absolute differences
    diff = torch.abs(x - y)
    
    # Step 2: Compute denominator (sum of absolute values)
    denom = torch.abs(x) + torch.abs(y)
    
    # Step 3: Handle division by zero (where denominator is 0)
    # When both x_i and y_i are 0, the term is defined as 0
    ratio = torch.where(denom > 0, diff / denom, torch.zeros_like(diff))
    
    # Step 4: Sum along feature dimension
    distance = torch.sum(ratio, dim=1)
    
    return distance  
batch_size = 512
feature_dim = 512

def get_inputs():
# Generate two sets of positive vectors (to avoid sign issues)
x = torch.abs(torch.randn(batch_size, feature_dim)) + 0.1
y = torch.abs(torch.randn(batch_size, feature_dim)) + 0.1
return [x, y]

def get_init_inputs():
return [] # No special initialization inputs needed



Your task is to write a new file `canberra_cudacode.py` that defines a new model `ModelNew` which uses a custom CUDA kernel to accelerate the Canberra distance calculation. The goal is to achieve a significant speedup while maintaining numerical precision.

The recommended implementation strategy is to use a **parallel reduction pattern**:
1.  Launch one thread block for each sample in the batch (`batch_size` number of blocks).
2.  Within each block, have multiple threads collaborate to compute the sum for that single sample.
3.  Each thread should iterate over the feature dimension with a stride equal to the block size, accumulating a partial sum.
4.  Use shared memory to store these partial sums and then perform a standard parallel reduction to get the final distance for that sample.
5.  The first thread of the block should write the final result to the output tensor.
The implementation should be robust, handle input validation, and use `extra_cuda_cflags` like `"-O3"` and `"--use_fast_math"` for performance. The final output should be a single python file containing the CUDA kernel, the compilation logic via `load_inline`, and the new `ModelNew` class.