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 math as tl_math
import torch.nn as nn
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_cat_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
    x1 = xindex // 64
    x0 = xindex % 64
    x2 = xindex
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 2, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x0 + 64 * x1), tmp4 & xmask, other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1], 4, tl.int64)
    tmp9 = tl.load(in_ptr1 + (x0 + 64 * (-2 + x1)), tmp6 & xmask, other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tl.store(out_ptr0 + x2, tmp10, xmask)


@triton.jit
def triton_poi_fused_convolution_relu_1(in_out_ptr0, in_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x3 = xindex
    x1 = xindex // 16 % 64
    tmp0 = tl.load(in_out_ptr0 + x3, None)
    tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tl.store(in_out_ptr0 + x3, tmp4, None)


@triton.jit
def triton_poi_fused_convolution_relu_2(in_out_ptr0, in_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 16 % 16
    tmp0 = tl.load(in_out_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tl.store(in_out_ptr0 + x3, tmp4, xmask)


@triton.jit
def triton_poi_fused__softmax_convolution_3(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = tl.load(in_ptr1 + 0)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK])
    tmp4 = tl.load(in_ptr0 + (16 + x0), xmask)
    tmp7 = tl.load(in_ptr0 + (32 + x0), xmask)
    tmp10 = tl.load(in_ptr0 + (48 + x0), xmask)
    tmp3 = tmp0 + tmp2
    tmp5 = tmp4 + tmp2
    tmp6 = triton_helpers.maximum(tmp3, tmp5)
    tmp8 = tmp7 + tmp2
    tmp9 = triton_helpers.maximum(tmp6, tmp8)
    tmp11 = tmp10 + tmp2
    tmp12 = triton_helpers.maximum(tmp9, tmp11)
    tmp13 = tmp3 - tmp12
    tmp14 = tl_math.exp(tmp13)
    tmp15 = tmp5 - tmp12
    tmp16 = tl_math.exp(tmp15)
    tmp17 = tmp14 + tmp16
    tmp18 = tmp8 - tmp12
    tmp19 = tl_math.exp(tmp18)
    tmp20 = tmp17 + tmp19
    tmp21 = tmp11 - tmp12
    tmp22 = tl_math.exp(tmp21)
    tmp23 = tmp20 + tmp22
    tl.store(out_ptr0 + x0, tmp12, xmask)
    tl.store(out_ptr1 + x0, tmp23, xmask)


@triton.jit
def triton_poi_fused__softmax_convolution_4(in_out_ptr0, in_ptr0, in_ptr1,
    in_ptr2, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 16
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 0)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK])
    tmp4 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tmp0 + tmp2
    tmp5 = tmp3 - tmp4
    tmp6 = tl_math.exp(tmp5)
    tmp8 = tmp6 / tmp7
    tl.store(in_out_ptr0 + x2, tmp8, xmask)


@triton.jit
def triton_poi_fused_add_mul_repeat_5(in_ptr0, in_ptr1, in_ptr2, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 128
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 16
    x2 = xindex // 64
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr1 + x3, xmask)
    tmp3 = tl.load(in_ptr2 + x3, xmask)
    tmp2 = tmp0 * tmp1
    tmp4 = tmp0 * tmp3
    tmp5 = tmp2 + tmp4
    tl.store(out_ptr0 + x3, tmp5, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8) = args
    args.clear()
    assert_size_stride(primals_1, (2, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (2, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_3, (64, 4, 3, 3), (36, 9, 3, 1))
    assert_size_stride(primals_4, (64,), (1,))
    assert_size_stride(primals_5, (16, 64, 1, 1), (64, 1, 1, 1))
    assert_size_stride(primals_6, (16,), (1,))
    assert_size_stride(primals_7, (1, 16, 1, 1), (16, 1, 1, 1))
    assert_size_stride(primals_8, (1,), (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_cat_0[grid(256)](primals_1, primals_2, buf0, 256,
            XBLOCK=256, num_warps=4, num_stages=1)
        buf1 = extern_kernels.convolution(buf0, primals_3, stride=(1, 1),
            padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf1, (4, 64, 4, 4), (1024, 16, 4, 1))
        buf2 = buf1
        del buf1
        triton_poi_fused_convolution_relu_1[grid(4096)](buf2, primals_4, 
            4096, XBLOCK=256, num_warps=4, num_stages=1)
        del primals_4
        buf3 = extern_kernels.convolution(buf2, primals_5, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf3, (4, 16, 4, 4), (256, 16, 4, 1))
        buf4 = buf3
        del buf3
        triton_poi_fused_convolution_relu_2[grid(1024)](buf4, primals_6, 
            1024, XBLOCK=256, num_warps=4, num_stages=1)
        del primals_6
        buf5 = extern_kernels.convolution(buf4, primals_7, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf5, (4, 1, 4, 4), (16, 16, 4, 1))
        buf6 = empty_strided_cuda((1, 1, 4, 4), (16, 16, 4, 1), torch.float32)
        buf7 = empty_strided_cuda((1, 1, 4, 4), (16, 16, 4, 1), torch.float32)
        triton_poi_fused__softmax_convolution_3[grid(16)](buf5, primals_8,
            buf6, buf7, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf8 = buf5
        del buf5
        triton_poi_fused__softmax_convolution_4[grid(64)](buf8, primals_8,
            buf6, buf7, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del buf6
        del buf7
        del primals_8
        buf9 = empty_strided_cuda((2, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_add_mul_repeat_5[grid(128)](buf8, primals_1,
            primals_2, buf9, 128, XBLOCK=128, num_warps=4, num_stages=1)
    return (buf9, primals_1, primals_2, primals_3, primals_5, primals_7,
        buf0, buf2, buf4, buf8)


class AttNetNew(nn.Module):

    def __init__(self, num_input_ch):
        super(AttNetNew, self).__init__()
        self.num_input_ch = num_input_ch
        self.conv1 = nn.Conv2d(self.num_input_ch, 64, 3, padding=1, bias=True)
        self.conv2 = nn.Conv2d(64, 16, 1, bias=True)
        self.conv3 = nn.Conv2d(16, 1, 1, bias=True)

    def forward(self, input_0, input_1):
        primals_3 = self.conv1.weight
        primals_4 = self.conv1.bias
        primals_5 = self.conv2.weight
        primals_6 = self.conv2.bias
        primals_7 = self.conv3.weight
        primals_8 = self.conv3.bias
        primals_1 = input_0
        primals_2 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8])
        return output[0]


Measured latency_ms: 0.113503