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 import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
from torch import 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_per_fused__log_softmax__softmax_0(in_ptr0, out_ptr0, out_ptr1,
    out_ptr2, out_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0)
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.where(xmask, tmp3, float('-inf'))
    tmp6 = triton_helpers.max2(tmp5, 1)[:, None]
    tmp7 = tmp2 - tmp6
    tmp8 = tmp7 * tmp1
    tmp9 = tl_math.exp(tmp8)
    tmp10 = tl.broadcast_to(tmp9, [XBLOCK, RBLOCK])
    tmp12 = tl.where(xmask, tmp10, 0)
    tmp13 = tl.sum(tmp12, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp6, xmask)
    tl.store(out_ptr1 + x0, tmp13, xmask)
    tl.store(out_ptr2 + x0, tmp6, xmask)
    tl.store(out_ptr3 + x0, tmp13, xmask)


@triton.jit
def triton_per_fused__log_softmax_1(in_ptr0, out_ptr0, out_ptr1, xnumel,
    rnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0)
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.where(xmask, tmp3, float('-inf'))
    tmp6 = triton_helpers.max2(tmp5, 1)[:, None]
    tmp7 = tmp2 - tmp6
    tmp8 = tmp7 * tmp1
    tmp9 = tl_math.exp(tmp8)
    tmp10 = tl.broadcast_to(tmp9, [XBLOCK, RBLOCK])
    tmp12 = tl.where(xmask, tmp10, 0)
    tmp13 = tl.sum(tmp12, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp6, xmask)
    tl.store(out_ptr1 + x0, tmp13, xmask)


@triton.jit
def triton_per_fused__log_softmax__softmax_div_mul_sub_sum_2(in_out_ptr0,
    in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7,
    xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r2 = rindex
    r1 = rindex // 16
    tmp0 = tl.load(in_ptr0 + r2, None)
    tmp3 = tl.load(in_ptr1 + r1, None, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr2 + r1, None, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr3 + r1, None, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr4 + r1, None, eviction_policy='evict_last')
    tmp16 = tl.load(in_ptr5 + r2, None)
    tmp18 = tl.load(in_ptr6 + r1, None, eviction_policy='evict_last')
    tmp21 = tl.load(in_ptr7 + r1, None, eviction_policy='evict_last')
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tmp4 = tmp2 - tmp3
    tmp5 = tmp4 * tmp1
    tmp6 = tl_math.exp(tmp5)
    tmp8 = tmp6 / tmp7
    tmp10 = tmp2 - tmp9
    tmp11 = tmp10 * tmp1
    tmp13 = tl_math.log(tmp12)
    tmp14 = tmp11 - tmp13
    tmp15 = tmp8 * tmp14
    tmp17 = tmp16 * tmp1
    tmp19 = tmp17 - tmp18
    tmp20 = tmp19 * tmp1
    tmp22 = tl_math.log(tmp21)
    tmp23 = tmp20 - tmp22
    tmp24 = tmp8 * tmp23
    tmp25 = tmp15 - tmp24
    tmp26 = tl.broadcast_to(tmp25, [RBLOCK])
    tmp28 = triton_helpers.promote_to_tensor(tl.sum(tmp26, 0))
    tmp29 = tmp28 * tmp1
    tmp30 = tmp29 * tmp1
    tmp31 = 0.0625
    tmp32 = tmp30 * tmp31
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp32, None)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((16, 1), (1, 16), torch.float32)
        buf1 = empty_strided_cuda((16, 1), (1, 16), torch.float32)
        buf2 = empty_strided_cuda((16, 1), (1, 16), torch.float32)
        buf3 = empty_strided_cuda((16, 1), (1, 16), torch.float32)
        get_raw_stream(0)
        triton_per_fused__log_softmax__softmax_0[grid(16)](arg1_1, buf0,
            buf1, buf2, buf3, 16, 16, XBLOCK=1, num_warps=2, num_stages=1)
        buf4 = empty_strided_cuda((16, 1), (1, 16), torch.float32)
        buf5 = empty_strided_cuda((16, 1), (1, 16), torch.float32)
        triton_per_fused__log_softmax_1[grid(16)](arg0_1, buf4, buf5, 16, 
            16, XBLOCK=1, num_warps=2, num_stages=1)
        buf6 = empty_strided_cuda((), (), torch.float32)
        buf7 = buf6
        del buf6
        triton_per_fused__log_softmax__softmax_div_mul_sub_sum_2[grid(1)](buf7,
            arg1_1, buf0, buf1, buf2, buf3, arg0_1, buf4, buf5, 1, 256,
            num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        del buf0
        del buf1
        del buf2
        del buf3
        del buf4
        del buf5
    return buf7,


class ChannelWiseDivergenceNew(nn.Module):
    """PyTorch version of `Channel-wise Distillation for Semantic Segmentation.

    <https://arxiv.org/abs/2011.13256>`_.

    Args:
        tau (float): Temperature coefficient. Defaults to 1.0.
        loss_weight (float): Weight of loss. Defaults to 1.0.
    """

    def __init__(self, tau=1.0, loss_weight=1.0):
        super(ChannelWiseDivergenceNew, self).__init__()
        self.tau = tau
        self.loss_weight = loss_weight

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


Measured latency_ms: 0.026987