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
import torch.nn as nn
import torch.cuda
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_clamp_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp12 = tl.load(in_ptr0 + x0, xmask)
    tmp0 = tl.full([1], 0, tl.int64)
    tmp1 = tl.full([1], 2, tl.int64)
    tmp2 = tmp0 < tmp1
    tmp3 = tmp0 == tmp0
    tmp4 = tmp2 & tmp3
    tmp5 = tl.full([1], 0, tl.int32)
    tmp6 = tmp5 == tmp5
    tmp7 = tl.load(in_ptr0 + x0, tmp4 & xmask, other=0.0)
    tmp8 = tl.sigmoid(tmp7)
    tmp9 = tl.where(tmp6, tmp8, tmp7)
    tmp10 = tl.full(tmp9.shape, 0.0, tmp9.dtype)
    tmp11 = tl.where(tmp4, tmp9, tmp10)
    tmp13 = tl.where(tmp4, tmp11, tmp12)
    tmp14 = 0.0001
    tmp15 = triton_helpers.maximum(tmp13, tmp14)
    tmp16 = 0.9999
    tmp17 = triton_helpers.minimum(tmp15, tmp16)
    tl.store(out_ptr0 + x0, tmp17, xmask)


@triton.jit
def triton_poi_fused_sigmoid_1(in_ptr0, 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
    tmp47 = tl.load(in_ptr0 + x2, xmask)
    tmp0 = x1
    tmp1 = tl.full([1], 1, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.full([1], 2, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = (-1 + x1) % 4
    tmp6 = tl.full([1], 0, tl.int64)
    tmp7 = tmp5 == tmp6
    tmp8 = tmp2 & tmp4
    tmp9 = tmp8 & tmp7
    tmp10 = triton_helpers.div_floor_integer(-1 + x1, 4)
    tmp11 = tl.full([1], 0, tl.int32)
    tmp12 = tmp10 == tmp11
    tmp13 = tmp1 < tmp3
    tmp14 = tmp1 == tmp6
    tmp15 = tmp13 & tmp14
    tmp16 = tmp15 & tmp9
    tmp17 = tmp11 == tmp11
    tmp18 = tl.load(in_ptr0 + x0, tmp16 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp19 = tl.sigmoid(tmp18)
    tmp20 = tl.where(tmp17, tmp19, tmp18)
    tmp21 = tl.full(tmp20.shape, 0.0, tmp20.dtype)
    tmp22 = tl.where(tmp16, tmp20, tmp21)
    tmp23 = tl.load(in_ptr0 + (64 + x0), tmp9 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp24 = tl.where(tmp15, tmp22, tmp23)
    tmp25 = tl.sigmoid(tmp24)
    tmp26 = 1 + 4 * triton_helpers.div_floor_integer(-1 + x1, 4)
    tmp27 = tmp26 < tmp3
    tmp28 = tmp27 & tmp14
    tmp29 = tmp28 & tmp9
    tmp30 = tl.load(in_ptr0 + x0, tmp29 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp31 = tl.sigmoid(tmp30)
    tmp32 = tl.where(tmp12, tmp31, tmp30)
    tmp33 = tl.full(tmp32.shape, 0.0, tmp32.dtype)
    tmp34 = tl.where(tmp29, tmp32, tmp33)
    tmp35 = tl.load(in_ptr0 + (64 + x0 + 256 * triton_helpers.
        div_floor_integer(-1 + x1, 4)), tmp9 & xmask, other=0.0)
    tmp36 = tl.where(tmp28, tmp34, tmp35)
    tmp37 = tl.where(tmp12, tmp25, tmp36)
    tmp38 = tl.full(tmp37.shape, 0.0, tmp37.dtype)
    tmp39 = tl.where(tmp9, tmp37, tmp38)
    tmp40 = tmp0 == tmp6
    tmp41 = tmp4 & tmp40
    tmp42 = tl.load(in_ptr0 + x0, tmp41 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp43 = tl.sigmoid(tmp42)
    tmp44 = tl.where(tmp17, tmp43, tmp42)
    tmp45 = tl.full(tmp44.shape, 0.0, tmp44.dtype)
    tmp46 = tl.where(tmp41, tmp44, tmp45)
    tmp48 = tl.where(tmp41, tmp46, tmp47)
    tmp49 = tl.where(tmp9, tmp39, tmp48)
    tl.store(out_ptr0 + x2, tmp49, xmask)


@triton.jit
def triton_poi_fused_clamp_2(in_ptr0, out_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 + (64 + x0), xmask)
    tmp1 = 0.0001
    tmp2 = triton_helpers.maximum(tmp0, tmp1)
    tmp3 = 0.9999
    tmp4 = triton_helpers.minimum(tmp2, tmp3)
    tl.store(out_ptr0 + x0, tmp4, xmask)


@triton.jit
def triton_poi_fused_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tl.store(out_ptr0 + x0, tmp0, xmask)


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, (7, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_clamp_0[grid(64)](arg0_1, buf0, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_sigmoid_1[grid(256)](arg0_1, buf1, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_clamp_2[grid(64)](buf1, buf2, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        triton_poi_fused_3[grid(256)](buf1, arg0_1, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        del arg0_1
    return buf0, reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 0
        ), buf2, reinterpret_tensor(buf1, (0, 4, 4, 4), (256, 16, 4, 1), 128
        ), reinterpret_tensor(buf1, (0, 4, 4, 4), (256, 16, 4, 1), 192
        ), reinterpret_tensor(buf1, (4, 4, 4), (16, 4, 1), 128
        ), reinterpret_tensor(buf1, (4, 4, 4), (16, 4, 1), 192
        ), reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 64
        ), reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 128
        ), reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 192
        ), reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 256
        ), reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 320
        ), reinterpret_tensor(arg1_1, (4, 4, 4), (16, 4, 1), 384)


def _ae_loss(tag0, tag1, mask):
    num = mask.sum(dim=1, keepdim=True).float()
    tag0 = tag0.squeeze()
    tag1 = tag1.squeeze()
    tag_mean = (tag0 + tag1) / 2
    tag0 = torch.pow(tag0 - tag_mean, 2) / (num + 0.0001)
    tag0 = tag0[mask].sum()
    tag1 = torch.pow(tag1 - tag_mean, 2) / (num + 0.0001)
    tag1 = tag1[mask].sum()
    pull = tag0 + tag1
    mask = mask.unsqueeze(1) + mask.unsqueeze(2)
    mask = mask.eq(2)
    num = num.unsqueeze(2)
    num2 = (num - 1) * num
    dist = tag_mean.unsqueeze(1) - tag_mean.unsqueeze(2)
    dist = 1 - torch.abs(dist)
    dist = nn.functional.relu(dist, inplace=True)
    dist = dist - 1 / (num + 0.0001)
    dist = dist / (num2 + 0.0001)
    dist = dist[mask]
    push = dist.sum()
    return pull, push


def _neg_loss(preds, gt, lamda, lamdb):
    pos_inds = gt.eq(1)
    neg_inds = gt.lt(1)
    neg_weights = torch.pow(1 - gt[neg_inds], lamda)
    loss = 0
    for pred in preds:
        pos_pred = pred[pos_inds]
        neg_pred = pred[neg_inds]
        pos_loss = torch.log(pos_pred) * torch.pow(1 - pos_pred, lamdb)
        neg_loss = torch.log(1 - neg_pred) * torch.pow(neg_pred, lamdb
            ) * neg_weights
        num_pos = pos_inds.float().sum()
        pos_loss = pos_loss.sum()
        neg_loss = neg_loss.sum()
        if pos_pred.nelement() == 0:
            loss = loss - neg_loss
        else:
            loss = loss - (pos_loss + neg_loss) / num_pos
    return loss


def _regr_loss(regr, gt_regr, mask):
    num = mask.float().sum()
    mask = mask.unsqueeze(2).expand_as(gt_regr)
    regr = regr[mask]
    gt_regr = gt_regr[mask]
    regr_loss = nn.functional.smooth_l1_loss(regr, gt_regr, size_average=False)
    regr_loss = regr_loss / (num + 0.0001)
    return regr_loss


def _sigmoid(x):
    x = torch.clamp(x.sigmoid_(), min=0.0001, max=1 - 0.0001)
    return x


def _offset_loss(offset, gt_offset):
    offset_loss = nn.functional.smooth_l1_loss(offset, gt_offset,
        size_average=True)
    return offset_loss


class fully_connected(nn.Module):

    def __init__(self, inp_dim, out_dim, with_bn=True):
        super(fully_connected, self).__init__()
        self.with_bn = with_bn
        self.linear = nn.Linear(inp_dim, out_dim)
        if self.with_bn:
            self.bn = nn.BatchNorm1d(out_dim)
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        linear = self.linear(x)
        bn = self.bn(linear) if self.with_bn else linear
        relu = self.relu(bn)
        return relu


class cls(nn.Module):

    def __init__(self, k, inp_dim, out_dim, cat_num, stride=1, with_bn=True):
        super(cls, self).__init__()
        pad = (k - 1) // 2
        self.conv1 = nn.Conv2d(inp_dim, out_dim, (k, k), padding=(pad, pad),
            stride=(stride, stride), bias=not with_bn)
        self.bn1 = nn.BatchNorm2d(out_dim) if with_bn else nn.Sequential()
        self.relu = nn.ReLU(inplace=True)
        self.pool1 = nn.MaxPool2d(2, 2, padding=(pad, pad))
        self.conv2 = nn.Conv2d(inp_dim, out_dim, (k, k), padding=(pad, pad),
            stride=(stride, stride), bias=not with_bn)
        self.bn2 = nn.BatchNorm2d(out_dim) if with_bn else nn.Sequential()
        self.pool2 = nn.MaxPool2d(2, 2, padding=(pad, pad))
        self.final = fully_connected(out_dim, cat_num)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.pool1(x)
        x = self.conv2(x)
        x = self.bn2(x)
        x = self.relu(x)
        x = self.pool2(x)
        x = torch.max(x, 2)[0]
        x = torch.max(x, 2)[0]
        final = self.final(x)
        return final


class offset(nn.Module):

    def __init__(self, k, inp_dim, out_dim, cat_num, stride=1, with_bn=True):
        super(offset, self).__init__()
        pad = (k - 1) // 2
        self.conv1 = nn.Conv2d(inp_dim, out_dim, (k, k), padding=(pad, pad),
            stride=(stride, stride), bias=not with_bn)
        self.bn1 = nn.BatchNorm2d(out_dim) if with_bn else nn.Sequential()
        self.relu = nn.ReLU(inplace=True)
        self.pool1 = nn.AvgPool2d(2, 2, padding=(pad, pad))
        self.conv2 = nn.Conv2d(inp_dim, out_dim, (k, k), padding=(pad, pad),
            stride=(stride, stride), bias=not with_bn)
        self.bn2 = nn.BatchNorm2d(out_dim) if with_bn else nn.Sequential()
        self.pool2 = nn.AvgPool2d(2, 2, padding=(pad, pad))
        self.final = fully_connected(out_dim, cat_num)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.pool1(x)
        x = self.conv2(x)
        x = self.bn2(x)
        x = self.relu(x)
        x = self.pool2(x)
        x = torch.max(x, 2)[0]
        x = torch.max(x, 2)[0]
        final = self.final(x)
        return final


class AELossPureClsNew(nn.Module):

    def __init__(self, pull_weight=1, push_weight=1, regr_weight=1,
        focal_loss=_neg_loss, lamda=4, lamdb=2):
        super(AELossPureClsNew, self).__init__()
        self.pull_weight = pull_weight
        self.push_weight = push_weight
        self.regr_weight = regr_weight
        self.focal_loss = focal_loss
        self.ae_loss = _ae_loss
        self.regr_loss = _regr_loss
        self.lamda = lamda
        self.lamdb = lamdb
        self.cls_loss = nn.CrossEntropyLoss(size_average=True)
        self.offset_loss = _offset_loss

    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.055259