import torch
from torch._inductor.select_algorithm import extern_kernels
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 import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice
import math
import torch.nn as nn
import torch.utils.data
from itertools import product as product
from math import sqrt as sqrt
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_convolution_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_out_ptr0 + x0, xmask)
    tmp1 = tl.load(in_ptr0 + 0)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK])
    tmp3 = tmp0 + tmp2
    tl.store(in_out_ptr0 + x0, tmp3, xmask)


@triton.jit
def triton_poi_fused_clamp_mul_tanh_view_1(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
    x3 = xindex
    x0 = xindex % 16
    x2 = xindex // 64
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr1 + (x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = libdevice.tanh(tmp1)
    tmp3 = 0.0
    tmp4 = triton_helpers.maximum(tmp2, tmp3)
    tmp5 = tmp0 * tmp4
    tl.store(out_ptr0 + x3, tmp5, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (1, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_2, (1,), (1,))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_3, primals_1, stride=(1, 
            1), padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 1, 4, 4), (16, 16, 4, 1))
        buf1 = buf0
        del buf0
        get_raw_stream(0)
        triton_poi_fused_convolution_0[grid(64)](buf1, primals_2, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del primals_2
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_clamp_mul_tanh_view_1[grid(256)](primals_4, buf1,
            buf2, 256, XBLOCK=128, num_warps=4, num_stages=1)
    return buf2, primals_1, primals_3, primals_4, buf1


class SpatialGateNew(nn.Module):

    def __init__(self, in_channels: 'int', num_groups: 'int'=1, kernel_size:
        'int'=1, padding: 'int'=0, stride: 'int'=1, gate_activation: 'str'=
        'ReTanH', gate_activation_kargs: 'dict'=None, get_running_cost:
        'callable'=None):
        super(SpatialGateNew, self).__init__()
        self.num_groups = num_groups
        self.gate_conv = nn.Conv2d(in_channels, num_groups, kernel_size,
            padding=padding, stride=stride)
        self.gate_activation = gate_activation
        self.gate_activation_kargs = gate_activation_kargs
        if gate_activation == 'ReTanH':
            self.gate_activate = lambda x: torch.tanh(x).clamp(min=0)
        elif gate_activation == 'Sigmoid':
            self.gate_activate = lambda x: torch.sigmoid(x)
        elif gate_activation == 'GeReTanH':
            assert 'tau' in gate_activation_kargs
            tau = gate_activation_kargs['tau']
            ttau = math.tanh(tau)
            self.gate_activate = lambda x: ((torch.tanh(x - tau) + ttau) /
                (1 + ttau)).clamp(min=0)
        else:
            raise NotImplementedError()
        self.get_running_cost = get_running_cost
        self.running_cost = None
        self.init_parameters()

    def init_parameters(self, init_gate=0.99):
        if self.gate_activation == 'ReTanH':
            bias_value = 0.5 * math.log((1 + init_gate) / (1 - init_gate))
        elif self.gate_activation == 'Sigmoid':
            bias_value = 0.5 * math.log(init_gate / (1 - init_gate))
        elif self.gate_activation == 'GeReTanH':
            tau = self.gate_activation_kargs['tau']
            bias_value = 0.5 * math.log((1 + init_gate * math.exp(2 * tau)) /
                (1 - init_gate))
        nn.init.normal_(self.gate_conv.weight, std=0.01)
        nn.init.constant_(self.gate_conv.bias, bias_value)

    def encode(self, *inputs):
        outputs = [x.view(x.shape[0] * self.num_groups, -1, *x.shape[2:]) for
            x in inputs]
        return outputs

    def decode(self, *inputs):
        outputs = [x.view(x.shape[0] // self.num_groups, -1, *x.shape[2:]) for
            x in inputs]
        return outputs

    def update_running_cost(self, gate):
        if self.get_running_cost is not None:
            cost = self.get_running_cost(gate)
            if self.running_cost is not None:
                self.running_cost = [(x + y) for x, y in zip(self.
                    running_cost, cost)]
            else:
                self.running_cost = cost

    def clear_running_cost(self):
        self.running_cost = None

    def forward(self, input_0, input_1):
        primals_1 = self.gate_conv.weight
        primals_2 = self.gate_conv.bias
        primals_3 = input_0
        primals_4 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4])
        return output[0]


Measured latency_ms: 0.044167