import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
from torch.nn import Parameter
from torch.distributions.exponential import Exponential
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_poi_fused_add_mul_pow_reciprocal_sin_0(in_ptr0, in_ptr1,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tl.full([1], 1, tl.int32)
    tmp3 = tmp2 / tmp1
    tmp4 = 1.0
    tmp5 = tmp3 * tmp4
    tmp6 = tmp0 * tmp1
    tmp7 = tl_math.sin(tmp6)
    tmp8 = tmp7 * tmp7
    tmp9 = tmp5 * tmp8
    tmp10 = tmp0 + tmp9
    tl.store(out_ptr0 + x2, tmp10, xmask)


def call(args):
    primals_1, primals_2 = args
    args.clear()
    assert_size_stride(primals_1, (4,), (1,))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_mul_pow_reciprocal_sin_0[grid(256)](primals_2,
            primals_1, buf0, 256, XBLOCK=256, num_warps=4, num_stages=1)
    return buf0, primals_1, primals_2


class SnakeNew(nn.Module):
    """         
    Implementation of the serpentine-like sine-based periodic activation function
    
    .. math::
         Snake_a := x + rac{1}{a} sin^2(ax) = x - rac{1}{2a}cos{2ax} + rac{1}{2a}

    Shape:
        - Input: (N, *) where * means, any number of additional
          dimensions
        - Output: (N, *), same shape as the input
        
    Parameters:
        - a - trainable parameter
    
    References:
        - This activation function is from this paper by Liu Ziyin, Tilman Hartwig, Masahito Ueda:
        https://arxiv.org/abs/2006.08195
        
    Examples:
        >>> a1 = snake(256)
        >>> x = torch.randn(256)
        >>> x = a1(x)
    """

    def __init__(self, in_features, a=None, trainable=True):
        """
        Initialization.
        Args:
            in_features: shape of the input
            a: trainable parameter
            trainable: sets `a` as a trainable parameter
            
            `a` is initialized to 1 by default, higher values = higher-frequency, 
            5-50 is a good starting point if you already think your data is periodic, 
            consider starting lower e.g. 0.5 if you think not, but don't worry, 
            `a` will be trained along with the rest of your model. 
        """
        super(SnakeNew, self).__init__()
        self.in_features = in_features if isinstance(in_features, list) else [
            in_features]
        if a is not None:
            self.a = Parameter(torch.ones(self.in_features) * a)
        else:
            m = Exponential(torch.tensor([0.1]))
            self.a = Parameter(m.rsample(self.in_features).squeeze())
        self.a.requiresGrad = trainable

    def forward(self, input_0):
        primals_1 = self.a
        primals_2 = input_0
        output = call([primals_1, primals_2])
        return output[0]


Measured latency_ms: 0.015288