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 libdevice
from torch import nn
from torch.nn import Parameter
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_div_mul_rsub_tanh_0(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 16
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr1 + x2, xmask)
    tmp9 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp1 = libdevice.tanh(tmp0)
    tmp2 = 0.5
    tmp3 = tmp1 * tmp2
    tmp4 = tmp3 + tmp2
    tmp5 = 1.0
    tmp6 = tmp5 - tmp4
    tmp8 = tmp6 * tmp7
    tmp10 = libdevice.tanh(tmp9)
    tmp11 = 2.2
    tmp12 = tmp10 * tmp11
    tmp13 = tmp4 * tmp12
    tmp14 = tmp8 + tmp13
    tl.store(out_ptr0 + x2, tmp14, xmask)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (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_div_mul_rsub_tanh_0[grid(256)](primals_1,
            primals_3, primals_2, buf0, 256, XBLOCK=128, num_warps=4,
            num_stages=1)
    return buf0, primals_1, primals_2, primals_3


def th(vector):
    return torch.tanh(vector) / 2 + 0.5


def thp(vector):
    return torch.tanh(vector) * 2.2


class Model(nn.Module):
    """
    Base class for models with added support for GradCam activation map
    and a SentiNet defense. The GradCam design is taken from:
https://medium.com/@stepanulyanin/implementing-grad-cam-in-pytorch-ea0937c31e82
    If you are not planning to utilize SentiNet defense just import any model
    you like for your tasks.
    """

    def __init__(self):
        super().__init__()
        self.gradient = None

    def activations_hook(self, grad):
        self.gradient = grad

    def get_gradient(self):
        return self.gradient

    def get_activations(self, x):
        return self.features(x)

    def switch_grads(self, enable=True):
        for i, n in self.named_parameters():
            n.requires_grad_(enable)

    def features(self, x):
        """
        Get latent representation, eg logit layer.
        :param x:
        :return:
        """
        raise NotImplementedError

    def forward(self, x, latent=False):
        raise NotImplementedError


class NCModelNew(Model):

    def __init__(self, size):
        super().__init__()
        self.size = size
        self.pattern = torch.zeros([self.size, self.size], requires_grad=True
            ) + torch.normal(0, 0.5, [self.size, self.size])
        self.mask = torch.zeros([self.size, self.size], requires_grad=True)
        self.mask = Parameter(self.mask)
        self.pattern = Parameter(self.pattern)

    def re_init(self, device):
        p = torch.zeros([self.size, self.size], requires_grad=True
            ) + torch.normal(0, 0.5, [self.size, self.size])
        self.pattern.data = p
        m = torch.zeros([self.size, self.size], requires_grad=True)
        self.mask.data = m

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


Measured latency_ms: 0.015839