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.triton_helpers import libdevice
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_linalg_vector_norm_0(in_ptr0, out_ptr0, 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 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp1 = tmp0 * tmp0
    tmp3 = tmp2 * tmp2
    tmp4 = tmp1 + tmp3
    tmp6 = tmp5 * tmp5
    tmp7 = tmp4 + tmp6
    tmp9 = tmp8 * tmp8
    tmp10 = tmp7 + tmp9
    tmp11 = libdevice.sqrt(tmp10)
    tl.store(out_ptr0 + x0, tmp11, xmask)


@triton.jit
def triton_poi_fused_div_1(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_ptr0 + x0, xmask)
    tmp1 = tl.load(in_out_ptr0 + x0, xmask)
    tmp2 = tmp0 / tmp1
    tl.store(in_out_ptr0 + x0, tmp2, xmask)


@triton.jit
def triton_poi_fused_add_div_mul_rsub_sub_sum_2(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = xindex // 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask)
    tmp2 = tl.load(in_ptr1 + 4 * x2, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask)
    tmp16 = tl.load(in_ptr1 + (1 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp17 = tl.load(in_ptr2 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp18 = tl.load(in_ptr2 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp27 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask)
    tmp29 = tl.load(in_ptr1 + (2 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp30 = tl.load(in_ptr2 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp31 = tl.load(in_ptr2 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp40 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask)
    tmp42 = tl.load(in_ptr1 + (3 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp43 = tl.load(in_ptr2 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp44 = tl.load(in_ptr2 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp1 = tl.sigmoid(tmp0)
    tmp5 = tmp3 + tmp4
    tmp6 = 0.5
    tmp7 = tmp5 * tmp6
    tmp8 = tmp2 * tmp7
    tmp9 = tmp1 * tmp8
    tmp10 = 1.0
    tmp11 = tmp10 - tmp1
    tmp12 = tmp11 * tmp8
    tmp13 = tmp9 - tmp12
    tmp15 = tl.sigmoid(tmp14)
    tmp19 = tmp17 + tmp18
    tmp20 = tmp19 * tmp6
    tmp21 = tmp16 * tmp20
    tmp22 = tmp15 * tmp21
    tmp23 = tmp10 - tmp15
    tmp24 = tmp23 * tmp21
    tmp25 = tmp22 - tmp24
    tmp26 = tmp13 + tmp25
    tmp28 = tl.sigmoid(tmp27)
    tmp32 = tmp30 + tmp31
    tmp33 = tmp32 * tmp6
    tmp34 = tmp29 * tmp33
    tmp35 = tmp28 * tmp34
    tmp36 = tmp10 - tmp28
    tmp37 = tmp36 * tmp34
    tmp38 = tmp35 - tmp37
    tmp39 = tmp26 + tmp38
    tmp41 = tl.sigmoid(tmp40)
    tmp45 = tmp43 + tmp44
    tmp46 = tmp45 * tmp6
    tmp47 = tmp42 * tmp46
    tmp48 = tmp41 * tmp47
    tmp49 = tmp10 - tmp41
    tmp50 = tmp49 * tmp47
    tmp51 = tmp48 - tmp50
    tmp52 = tmp39 + tmp51
    tl.store(out_ptr0 + x2, tmp52, xmask)


@triton.jit
def triton_poi_fused_add_div_mul_rsub_sub_sum_3(in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = xindex // 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask)
    tmp1 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr2 + 4 * x2, xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr3 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
    tmp16 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask)
    tmp17 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp20 = tl.load(in_ptr2 + (1 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp21 = tl.load(in_ptr3 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp22 = tl.load(in_ptr3 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp31 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask)
    tmp32 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp35 = tl.load(in_ptr2 + (2 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp36 = tl.load(in_ptr3 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp37 = tl.load(in_ptr3 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp46 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask)
    tmp47 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp50 = tl.load(in_ptr2 + (3 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp51 = tl.load(in_ptr3 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp52 = tl.load(in_ptr3 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.sigmoid(tmp2)
    tmp7 = tmp5 + tmp6
    tmp8 = 0.5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp4 * tmp9
    tmp11 = tmp3 * tmp10
    tmp12 = 1.0
    tmp13 = tmp12 - tmp3
    tmp14 = tmp13 * tmp10
    tmp15 = tmp11 - tmp14
    tmp18 = tmp16 + tmp17
    tmp19 = tl.sigmoid(tmp18)
    tmp23 = tmp21 + tmp22
    tmp24 = tmp23 * tmp8
    tmp25 = tmp20 * tmp24
    tmp26 = tmp19 * tmp25
    tmp27 = tmp12 - tmp19
    tmp28 = tmp27 * tmp25
    tmp29 = tmp26 - tmp28
    tmp30 = tmp15 + tmp29
    tmp33 = tmp31 + tmp32
    tmp34 = tl.sigmoid(tmp33)
    tmp38 = tmp36 + tmp37
    tmp39 = tmp38 * tmp8
    tmp40 = tmp35 * tmp39
    tmp41 = tmp34 * tmp40
    tmp42 = tmp12 - tmp34
    tmp43 = tmp42 * tmp40
    tmp44 = tmp41 - tmp43
    tmp45 = tmp30 + tmp44
    tmp48 = tmp46 + tmp47
    tmp49 = tl.sigmoid(tmp48)
    tmp53 = tmp51 + tmp52
    tmp54 = tmp53 * tmp8
    tmp55 = tmp50 * tmp54
    tmp56 = tmp49 * tmp55
    tmp57 = tmp12 - tmp49
    tmp58 = tmp57 * tmp55
    tmp59 = tmp56 - tmp58
    tmp60 = tmp45 + tmp59
    tl.store(out_ptr0 + x2, tmp60, xmask)


@triton.jit
def triton_poi_fused_add_4(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(out_ptr0 + x2, tmp2, xmask)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_2, (1, 4, 4), (16, 4, 1))
    assert_size_stride(primals_3, (4, 4, 4), (16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_linalg_vector_norm_0[grid(16)](primals_1, buf0, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(buf0, reinterpret_tensor(buf0, (4, 1, 4), (4, 16,
            1), 0), out=buf1)
        buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(primals_1, reinterpret_tensor(primals_1, (4, 4, 
            4), (16, 1, 4), 0), out=buf2)
        del primals_1
        buf3 = buf1
        del buf1
        triton_poi_fused_div_1[grid(64)](buf3, buf2, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf4 = buf0
        del buf0
        triton_poi_fused_add_div_mul_rsub_sub_sum_2[grid(16)](primals_3,
            buf3, primals_2, buf4, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf5 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused_add_div_mul_rsub_sub_sum_3[grid(16)](primals_3,
            buf4, buf3, primals_2, buf5, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        buf6 = buf4
        del buf4
        triton_poi_fused_add_div_mul_rsub_sub_sum_3[grid(16)](primals_3,
            buf5, buf3, primals_2, buf6, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        buf7 = buf5
        del buf5
        triton_poi_fused_add_div_mul_rsub_sub_sum_3[grid(16)](primals_3,
            buf6, buf3, primals_2, buf7, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        buf8 = buf6
        del buf6
        triton_poi_fused_add_div_mul_rsub_sub_sum_3[grid(16)](primals_3,
            buf7, buf3, primals_2, buf8, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        buf9 = buf7
        del buf7
        triton_poi_fused_add_div_mul_rsub_sub_sum_3[grid(16)](primals_3,
            buf8, buf3, primals_2, buf9, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        buf10 = buf8
        del buf8
        triton_poi_fused_add_div_mul_rsub_sub_sum_3[grid(16)](primals_3,
            buf9, buf3, primals_2, buf10, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        buf11 = buf9
        del buf9
        triton_poi_fused_add_div_mul_rsub_sub_sum_3[grid(16)](primals_3,
            buf10, buf3, primals_2, buf11, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        buf12 = buf10
        del buf10
        triton_poi_fused_add_div_mul_rsub_sub_sum_3[grid(16)](primals_3,
            buf11, buf3, primals_2, buf12, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        buf13 = buf11
        del buf11
        triton_poi_fused_add_div_mul_rsub_sub_sum_3[grid(16)](primals_3,
            buf12, buf3, primals_2, buf13, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        del buf12
        buf14 = buf2
        del buf2
        triton_poi_fused_add_4[grid(64)](primals_3, buf13, buf14, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del buf13
    return buf14, primals_2, primals_3, buf3


class CRFNew(nn.Module):

    def __init__(self, num_nodes, iteration=10):
        """Initialize the CRF module

        Args:
            num_nodes: int, number of nodes/patches within the fully CRF
            iteration: int, number of mean field iterations, e.g. 10
        """
        super(CRFNew, self).__init__()
        self.num_nodes = num_nodes
        self.iteration = iteration
        self.W = nn.Parameter(torch.zeros(1, num_nodes, num_nodes))

    def __repr__(self):
        return 'CRF(num_nodes={}, iteration={})'.format(self.num_nodes,
            self.iteration)

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


Measured latency_ms: 0.181952