import torch
from torch import nn
from torch.nn import MSELoss


class SkipLastTargetChannelWrapper(nn.Module):
    """
    Loss wrapper which removes additional target channel
    """

    def __init__(self, loss, squeeze_channel=False):
        super(SkipLastTargetChannelWrapper, self).__init__()
        self.loss = loss
        self.squeeze_channel = squeeze_channel

    def forward(self, input, target):
        assert target.size(1
            ) > 1, 'Target tensor has a singleton channel dimension, cannot remove channel'
        target = target[:, :-1, ...]
        if self.squeeze_channel:
            target = torch.squeeze(target, dim=1)
        return self.loss(input, target)


def get_inputs():
    return [torch.rand([4, 3, 4, 4]), torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {'loss': MSELoss()}]

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 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_mse_loss_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel,
    rnumel, XBLOCK: tl.constexpr):
    rnumel = 192
    RBLOCK: tl.constexpr = 256
    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, :]
    rmask = rindex < rnumel
    r2 = rindex
    r0 = rindex % 48
    r1 = rindex // 48
    tmp0 = tl.load(in_ptr0 + r2, rmask, other=0.0)
    tmp1 = tl.load(in_ptr1 + (r0 + 64 * r1), rmask, other=0.0)
    tmp2 = tmp0 - tmp1
    tmp3 = tmp2 * tmp2
    tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
    tmp6 = tl.where(rmask, tmp4, 0)
    tmp7 = tl.sum(tmp6, 1)[:, None]
    tmp8 = 192.0
    tmp9 = tmp7 / tmp8
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp9, 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, 3, 4, 4), (48, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((), (), torch.float32)
        buf1 = buf0
        del buf0
        get_raw_stream(0)
        triton_per_fused_mse_loss_0[grid(1)](buf1, arg1_1, arg0_1, 1, 192,
            XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
    return buf1,


class SkipLastTargetChannelWrapperNew(nn.Module):
    """
    Loss wrapper which removes additional target channel
    """

    def __init__(self, loss, squeeze_channel=False):
        super(SkipLastTargetChannelWrapperNew, self).__init__()
        self.loss = loss
        self.squeeze_channel = squeeze_channel

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