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
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_per_fused_sum_0(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.
    constexpr):
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = 20.0
    tmp2 = tmp0 * tmp1
    tmp3 = tl_math.exp(tmp2)
    tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
    tmp6 = tl.sum(tmp4, 1)[:, None]
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp6, None)


@triton.jit
def triton_poi_fused_sum_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp4 = tl.load(in_ptr1 + 0)
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK])
    tmp7 = tl.load(in_ptr0 + (4 + x0), xmask)
    tmp12 = tl.load(in_ptr0 + (8 + x0), xmask)
    tmp17 = tl.load(in_ptr0 + (12 + x0), xmask)
    tmp1 = 20.0
    tmp2 = tmp0 * tmp1
    tmp3 = tl_math.exp(tmp2)
    tmp6 = tmp3 / tmp5
    tmp8 = tmp7 * tmp1
    tmp9 = tl_math.exp(tmp8)
    tmp10 = tmp9 / tmp5
    tmp11 = tmp6 + tmp10
    tmp13 = tmp12 * tmp1
    tmp14 = tl_math.exp(tmp13)
    tmp15 = tmp14 / tmp5
    tmp16 = tmp11 + tmp15
    tmp18 = tmp17 * tmp1
    tmp19 = tl_math.exp(tmp18)
    tmp20 = tmp19 / tmp5
    tmp21 = tmp16 + tmp20
    tl.store(out_ptr0 + x0, tmp21, xmask)


@triton.jit
def triton_poi_fused_sum_2(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 4
    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')
    tmp4 = tl.load(in_ptr1 + 0)
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK])
    tmp7 = tl.load(in_ptr2 + 0)
    tmp8 = tl.broadcast_to(tmp7, [XBLOCK])
    tmp12 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp16 = tl.load(in_ptr2 + 1)
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK])
    tmp21 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp25 = tl.load(in_ptr2 + 2)
    tmp26 = tl.broadcast_to(tmp25, [XBLOCK])
    tmp30 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp34 = tl.load(in_ptr2 + 3)
    tmp35 = tl.broadcast_to(tmp34, [XBLOCK])
    tmp1 = 20.0
    tmp2 = tmp0 * tmp1
    tmp3 = tl_math.exp(tmp2)
    tmp6 = tmp3 / tmp5
    tmp9 = tmp6 / tmp8
    tmp10 = 0.25
    tmp11 = tmp9 * tmp10
    tmp13 = tmp12 * tmp1
    tmp14 = tl_math.exp(tmp13)
    tmp15 = tmp14 / tmp5
    tmp18 = tmp15 / tmp17
    tmp19 = tmp18 * tmp10
    tmp20 = tmp11 + tmp19
    tmp22 = tmp21 * tmp1
    tmp23 = tl_math.exp(tmp22)
    tmp24 = tmp23 / tmp5
    tmp27 = tmp24 / tmp26
    tmp28 = tmp27 * tmp10
    tmp29 = tmp20 + tmp28
    tmp31 = tmp30 * tmp1
    tmp32 = tl_math.exp(tmp31)
    tmp33 = tmp32 / tmp5
    tmp36 = tmp33 / tmp35
    tmp37 = tmp36 * tmp10
    tmp38 = tmp29 + tmp37
    tl.store(out_ptr0 + x0, tmp38, xmask)


@triton.jit
def triton_poi_fused_sum_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp4 = tl.load(in_ptr1 + 0)
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK])
    tmp7 = tl.load(in_ptr2 + x0, xmask)
    tmp11 = tl.load(in_ptr3 + 0)
    tmp12 = tl.broadcast_to(tmp11, [XBLOCK])
    tmp15 = tl.load(in_ptr0 + (4 + x0), xmask)
    tmp21 = tl.load(in_ptr3 + 1)
    tmp22 = tl.broadcast_to(tmp21, [XBLOCK])
    tmp26 = tl.load(in_ptr0 + (8 + x0), xmask)
    tmp32 = tl.load(in_ptr3 + 2)
    tmp33 = tl.broadcast_to(tmp32, [XBLOCK])
    tmp37 = tl.load(in_ptr0 + (12 + x0), xmask)
    tmp43 = tl.load(in_ptr3 + 3)
    tmp44 = tl.broadcast_to(tmp43, [XBLOCK])
    tmp1 = 20.0
    tmp2 = tmp0 * tmp1
    tmp3 = tl_math.exp(tmp2)
    tmp6 = tmp3 / tmp5
    tmp8 = tmp6 / tmp7
    tmp9 = 0.25
    tmp10 = tmp8 * tmp9
    tmp13 = tmp10 / tmp12
    tmp14 = tmp13 * tmp9
    tmp16 = tmp15 * tmp1
    tmp17 = tl_math.exp(tmp16)
    tmp18 = tmp17 / tmp5
    tmp19 = tmp18 / tmp7
    tmp20 = tmp19 * tmp9
    tmp23 = tmp20 / tmp22
    tmp24 = tmp23 * tmp9
    tmp25 = tmp14 + tmp24
    tmp27 = tmp26 * tmp1
    tmp28 = tl_math.exp(tmp27)
    tmp29 = tmp28 / tmp5
    tmp30 = tmp29 / tmp7
    tmp31 = tmp30 * tmp9
    tmp34 = tmp31 / tmp33
    tmp35 = tmp34 * tmp9
    tmp36 = tmp25 + tmp35
    tmp38 = tmp37 * tmp1
    tmp39 = tl_math.exp(tmp38)
    tmp40 = tmp39 / tmp5
    tmp41 = tmp40 / tmp7
    tmp42 = tmp41 * tmp9
    tmp45 = tmp42 / tmp44
    tmp46 = tmp45 * tmp9
    tmp47 = tmp36 + tmp46
    tl.store(out_ptr0 + x0, tmp47, xmask)


@triton.jit
def triton_poi_fused_div_4(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp4 = tl.load(in_ptr1 + 0)
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK])
    tmp7 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last')
    tmp1 = 20.0
    tmp2 = tmp0 * tmp1
    tmp3 = tl_math.exp(tmp2)
    tmp6 = tmp3 / tmp5
    tmp8 = tmp6 / tmp7
    tmp9 = 0.25
    tmp10 = tmp8 * tmp9
    tmp12 = tmp10 / tmp11
    tmp13 = tmp12 * tmp9
    tmp15 = tmp13 / tmp14
    tmp16 = tmp15 * tmp9
    tl.store(out_ptr0 + x2, tmp16, xmask)


@triton.jit
def triton_poi_fused_div_5(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
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tmp9 = 0.25
    tmp10 = tmp8 * tmp9
    tl.store(out_ptr0 + x2, tmp10, xmask)


@triton.jit
def triton_poi_fused_div_6(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
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tmp9 = 0.25
    tmp10 = tmp8 * tmp9
    tl.store(out_ptr0 + x2, tmp10, xmask)


@triton.jit
def triton_poi_fused_mul_7(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 4
    xnumel = 4
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x1 = xindex
    y0 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 4 * x1), xmask & ymask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tmp9 = 0.25
    tmp10 = tmp8 * tmp9
    tmp11 = 4.0
    tmp12 = tmp10 * tmp11
    tl.store(out_ptr0 + (x1 + 4 * y0), tmp12, xmask & ymask)


@triton.jit
def triton_poi_fused_8(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 4
    xnumel = 4
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x1 = xindex
    y0 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 4 * x1), xmask & ymask)
    tl.store(out_ptr0 + (x1 + 4 * y0), tmp0, xmask & ymask)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((), (), torch.float32)
        get_raw_stream(0)
        triton_per_fused_sum_0[grid(1)](arg0_1, buf0, 1, 16, XBLOCK=1,
            num_warps=2, num_stages=1)
        buf1 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        triton_poi_fused_sum_1[grid(4)](arg0_1, buf0, buf1, 4, XBLOCK=4,
            num_warps=1, num_stages=1)
        buf2 = empty_strided_cuda((1, 4), (4, 1), torch.float32)
        triton_poi_fused_sum_2[grid(4)](arg0_1, buf0, buf1, buf2, 4, XBLOCK
            =4, num_warps=1, num_stages=1)
        buf3 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        triton_poi_fused_sum_3[grid(4)](arg0_1, buf0, buf1, buf2, buf3, 4,
            XBLOCK=4, num_warps=1, num_stages=1)
        buf4 = empty_strided_cuda((4, 4), (1, 4), torch.float32)
        triton_poi_fused_div_4[grid(16)](arg0_1, buf0, buf1, buf2, buf3,
            buf4, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del arg0_1
        del buf0
        del buf1
        del buf2
        del buf3
        buf5 = empty_strided_cuda((4, 4), (1, 4), torch.float32)
        triton_poi_fused_div_5[grid(16)](buf4, buf5, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf6 = buf4
        del buf4
        triton_poi_fused_div_6[grid(16)](buf5, buf6, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf7 = reinterpret_tensor(buf5, (4, 4), (4, 1), 0)
        del buf5
        triton_poi_fused_mul_7[grid(4, 4)](buf6, buf7, 4, 4, XBLOCK=4,
            YBLOCK=4, num_warps=1, num_stages=1)
        buf8 = reinterpret_tensor(buf6, (4, 4), (4, 1), 0)
        del buf6
        triton_poi_fused_8[grid(4, 4)](buf7, buf8, 4, 4, XBLOCK=4, YBLOCK=4,
            num_warps=1, num_stages=1)
        del buf7
    return buf8,


class SinkhornKnoppNew(torch.nn.Module):

    def __init__(self, num_iters: 'int'=3, epsilon: 'float'=0.05,
        world_size: 'int'=1):
        """Approximates optimal transport using the Sinkhorn-Knopp algorithm.

        A simple iterative method to approach the double stochastic matrix is to alternately rescale
        rows and columns of the matrix to sum to 1.

        Args:
            num_iters (int, optional):  number of times to perform row and column normalization.
                Defaults to 3.
            epsilon (float, optional): weight for the entropy regularization term. Defaults to 0.05.
            world_size (int, optional): number of nodes for distributed training. Defaults to 1.
        """
        super().__init__()
        self.num_iters = num_iters
        self.epsilon = epsilon
        self.world_size = world_size

    def forward(self, input_0):
        arg0_1 = input_0
        output = call([arg0_1])
        return output[0]


Measured latency_ms: 0.028237