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 import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
import torch.utils.checkpoint
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_clone_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1,
    ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 16
    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
    x2 = xindex
    y0 = yindex % 4
    y1 = yindex // 4
    y3 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 16 * y1), xmask & ymask,
        eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (x2 + 4 * y1), xmask & ymask, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr2 + (x2 + 4 * y1), xmask & ymask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 * tmp1
    tmp4 = tmp3.to(tl.int64)
    tmp5 = y0
    tmp6 = tmp4 == tmp5
    tmp7 = tmp6.to(tl.int64)
    tmp8 = tmp7.to(tl.float32)
    tmp9 = tmp8 * tmp1
    tl.store(out_ptr0 + (x2 + 4 * y3), tmp2, xmask & ymask)
    tl.store(out_ptr1 + (x2 + 4 * y3), tmp9, xmask & ymask)


@triton.jit
def triton_poi_fused_clone_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 48
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4 % 3
    x2 = xindex // 12
    x0 = xindex % 4
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x1 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp6 = tl.load(in_ptr1 + (x1 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp1 = tmp0.to(tl.int64)
    tmp2 = x0
    tmp3 = tmp1 == tmp2
    tmp4 = tmp3.to(tl.int64)
    tmp5 = tmp4.to(tl.float32)
    tmp7 = tmp5 * tmp6
    tl.store(out_ptr0 + x3, tmp7, xmask)


@triton.jit
def triton_poi_fused_clone_2(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 48
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 3
    x2 = xindex // 12
    x1 = xindex // 3 % 4
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (1 + x0 + 4 * x2), xmask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr1 + (1 + x0 + 4 * x2), xmask, eviction_policy=
        'evict_last')
    tmp1 = tmp0.to(tl.int64)
    tmp2 = x1
    tmp3 = tmp1 == tmp2
    tmp4 = tmp3.to(tl.int64)
    tmp5 = tmp4.to(tl.float32)
    tmp7 = tmp5 * tmp6
    tl.store(out_ptr0 + x3, tmp7, xmask)


@triton.jit
def triton_poi_fused_clone_3(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 16
    xnumel = 3
    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
    x2 = xindex
    y0 = yindex % 4
    y1 = yindex // 4
    y3 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 4 * x2 + 12 * y1), xmask & ymask,
        eviction_policy='evict_last')
    tl.store(out_ptr0 + (x2 + 3 * y3), tmp0, xmask & ymask)


@triton.jit
def triton_poi_fused_mul_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)


@triton.jit
def triton_poi_fused_add_exp_max_mul_rsub_sub_sum_5(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + 16 * x1, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 16 * x1), xmask, eviction_policy='evict_last'
        )
    tmp4 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (2 + 16 * x1), xmask, eviction_policy='evict_last'
        )
    tmp8 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (3 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr1 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr2 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp29 = tl.load(in_ptr3 + (4 + x0 + 16 * x1), xmask)
    tmp35 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask)
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = triton_helpers.maximum(tmp2, tmp5)
    tmp9 = tmp7 + tmp8
    tmp10 = triton_helpers.maximum(tmp6, tmp9)
    tmp13 = tmp11 + tmp12
    tmp14 = triton_helpers.maximum(tmp10, tmp13)
    tmp15 = tmp2 - tmp14
    tmp16 = tl_math.exp(tmp15)
    tmp17 = tmp5 - tmp14
    tmp18 = tl_math.exp(tmp17)
    tmp19 = tmp16 + tmp18
    tmp20 = tmp9 - tmp14
    tmp21 = tl_math.exp(tmp20)
    tmp22 = tmp19 + tmp21
    tmp23 = tmp13 - tmp14
    tmp24 = tl_math.exp(tmp23)
    tmp25 = tmp22 + tmp24
    tmp27 = tl_math.log(tmp25)
    tmp28 = tmp14 + tmp27
    tmp30 = tmp29 * tmp26
    tmp31 = tmp28 + tmp30
    tmp32 = tmp26 * tmp31
    tmp33 = 1.0
    tmp34 = tmp33 - tmp26
    tmp36 = tmp34 * tmp35
    tmp37 = tmp32 + tmp36
    tl.store(in_out_ptr0 + x2, tmp37, xmask)


@triton.jit
def triton_poi_fused_add_exp_max_mul_rsub_sub_sum_6(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp12 = tl.load(in_ptr1 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr2 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp29 = tl.load(in_ptr3 + (8 + x0 + 16 * x1), xmask)
    tmp35 = tl.load(in_ptr0 + x2, xmask)
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = triton_helpers.maximum(tmp2, tmp5)
    tmp9 = tmp7 + tmp8
    tmp10 = triton_helpers.maximum(tmp6, tmp9)
    tmp13 = tmp11 + tmp12
    tmp14 = triton_helpers.maximum(tmp10, tmp13)
    tmp15 = tmp2 - tmp14
    tmp16 = tl_math.exp(tmp15)
    tmp17 = tmp5 - tmp14
    tmp18 = tl_math.exp(tmp17)
    tmp19 = tmp16 + tmp18
    tmp20 = tmp9 - tmp14
    tmp21 = tl_math.exp(tmp20)
    tmp22 = tmp19 + tmp21
    tmp23 = tmp13 - tmp14
    tmp24 = tl_math.exp(tmp23)
    tmp25 = tmp22 + tmp24
    tmp27 = tl_math.log(tmp25)
    tmp28 = tmp14 + tmp27
    tmp30 = tmp29 * tmp26
    tmp31 = tmp28 + tmp30
    tmp32 = tmp26 * tmp31
    tmp33 = 1.0
    tmp34 = tmp33 - tmp26
    tmp36 = tmp34 * tmp35
    tmp37 = tmp32 + tmp36
    tl.store(in_out_ptr0 + x2, tmp37, xmask)


@triton.jit
def triton_poi_fused_add_exp_max_mul_rsub_sub_sum_7(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp12 = tl.load(in_ptr1 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr2 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp29 = tl.load(in_ptr3 + (12 + x0 + 16 * x1), xmask)
    tmp35 = tl.load(in_ptr0 + x2, xmask)
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = triton_helpers.maximum(tmp2, tmp5)
    tmp9 = tmp7 + tmp8
    tmp10 = triton_helpers.maximum(tmp6, tmp9)
    tmp13 = tmp11 + tmp12
    tmp14 = triton_helpers.maximum(tmp10, tmp13)
    tmp15 = tmp2 - tmp14
    tmp16 = tl_math.exp(tmp15)
    tmp17 = tmp5 - tmp14
    tmp18 = tl_math.exp(tmp17)
    tmp19 = tmp16 + tmp18
    tmp20 = tmp9 - tmp14
    tmp21 = tl_math.exp(tmp20)
    tmp22 = tmp19 + tmp21
    tmp23 = tmp13 - tmp14
    tmp24 = tl_math.exp(tmp23)
    tmp25 = tmp22 + tmp24
    tmp27 = tl_math.log(tmp25)
    tmp28 = tmp14 + tmp27
    tmp30 = tmp29 * tmp26
    tmp31 = tmp28 + tmp30
    tmp32 = tmp26 * tmp31
    tmp33 = 1.0
    tmp34 = tmp33 - tmp26
    tmp36 = tmp34 * tmp35
    tmp37 = tmp32 + tmp36
    tl.store(in_out_ptr0 + x2, tmp37, xmask)


@triton.jit
def triton_poi_fused_add_sub_8(in_out_ptr0, in_ptr0, in_ptr1, 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')
    tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp20 = tl.load(in_out_ptr0 + x0, xmask)
    tmp21 = tl.load(in_ptr1 + x0, xmask)
    tmp2 = triton_helpers.maximum(tmp0, tmp1)
    tmp4 = triton_helpers.maximum(tmp2, tmp3)
    tmp6 = triton_helpers.maximum(tmp4, tmp5)
    tmp7 = tmp0 - tmp6
    tmp8 = tl_math.exp(tmp7)
    tmp9 = tmp1 - tmp6
    tmp10 = tl_math.exp(tmp9)
    tmp11 = tmp8 + tmp10
    tmp12 = tmp3 - tmp6
    tmp13 = tl_math.exp(tmp12)
    tmp14 = tmp11 + tmp13
    tmp15 = tmp5 - tmp6
    tmp16 = tl_math.exp(tmp15)
    tmp17 = tmp14 + tmp16
    tmp18 = tl_math.log(tmp17)
    tmp19 = tmp6 + tmp18
    tmp22 = tmp20 + tmp21
    tmp23 = tmp19 - tmp22
    tl.store(in_out_ptr0 + x0, tmp23, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_2, (4, 4, 1), (4, 1, 1))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_clone_0[grid(16, 4)](primals_1, primals_2,
            primals_3, buf0, buf1, 16, 4, XBLOCK=4, YBLOCK=16, num_warps=1,
            num_stages=1)
        buf2 = empty_strided_cuda((4, 1, 1), (1, 1, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf0, (4, 1, 16), (16, 0, 1),
            0), reinterpret_tensor(buf1, (4, 16, 1), (16, 1, 0), 0), out=buf2)
        del buf0
        buf3 = empty_strided_cuda((4, 3, 4, 1), (12, 4, 1, 1), torch.float32)
        triton_poi_fused_clone_1[grid(48)](primals_3, primals_2, buf3, 48,
            XBLOCK=64, num_warps=1, num_stages=1)
        buf4 = empty_strided_cuda((1, 12, 4), (48, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf3, (1, 12, 4), (0, 4, 1), 
            0), reinterpret_tensor(primals_4, (1, 4, 4), (16, 4, 1), 0),
            out=buf4)
        buf5 = empty_strided_cuda((4, 4, 3, 1), (12, 3, 1, 1), torch.float32)
        triton_poi_fused_clone_2[grid(48)](primals_3, primals_2, buf5, 48,
            XBLOCK=64, num_warps=1, num_stages=1)
        del primals_3
        buf6 = empty_strided_cuda((4, 4, 3, 1), (12, 3, 1, 1), torch.float32)
        triton_poi_fused_clone_3[grid(16, 3)](buf4, buf6, 16, 3, XBLOCK=4,
            YBLOCK=16, num_warps=1, num_stages=1)
        del buf4
        buf7 = empty_strided_cuda((4, 1, 1), (1, 1, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf5, (4, 1, 12), (12, 0, 1),
            0), reinterpret_tensor(buf6, (4, 12, 1), (12, 1, 0), 0), out=buf7)
        del buf6
        buf8 = buf1
        del buf1
        triton_poi_fused_mul_4[grid(64)](primals_1, primals_2, buf8, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        buf10 = empty_strided_cuda((4, 1, 4), (4, 16, 1), torch.float32)
        buf11 = reinterpret_tensor(buf10, (4, 4), (4, 1), 0)
        del buf10
        triton_poi_fused_add_exp_max_mul_rsub_sub_sum_5[grid(16)](buf11,
            buf8, primals_4, primals_2, primals_1, 16, XBLOCK=16, num_warps
            =1, num_stages=1)
        buf13 = empty_strided_cuda((4, 1, 4), (4, 16, 1), torch.float32)
        buf14 = reinterpret_tensor(buf13, (4, 4), (4, 1), 0)
        del buf13
        triton_poi_fused_add_exp_max_mul_rsub_sub_sum_6[grid(16)](buf14,
            buf11, primals_4, primals_2, primals_1, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf16 = reinterpret_tensor(buf11, (4, 1, 4), (4, 16, 1), 0)
        del buf11
        buf17 = reinterpret_tensor(buf16, (4, 4), (4, 1), 0)
        del buf16
        triton_poi_fused_add_exp_max_mul_rsub_sub_sum_7[grid(16)](buf17,
            buf14, primals_4, primals_2, primals_1, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del buf14
        buf18 = reinterpret_tensor(buf2, (4,), (1,), 0)
        del buf2
        triton_poi_fused_add_sub_8[grid(4)](buf18, buf17, buf7, 4, XBLOCK=4,
            num_warps=1, num_stages=1)
        del buf17
        del buf7
    return buf18, primals_1, primals_2, primals_4, reinterpret_tensor(buf8,
        (4, 4, 1), (16, 1, 1), 0), reinterpret_tensor(buf5, (4, 12, 1), (12,
        1, 12), 0), reinterpret_tensor(buf3, (1, 4, 12), (48, 1, 4), 0)


class CRFLayerNew(nn.Module):
    """
    """

    def __init__(self, output_dim):
        super(CRFLayerNew, self).__init__()
        self.output_dim = output_dim
        self.trans = nn.Parameter(torch.Tensor(output_dim, output_dim))
        self.trans.data.uniform_(-0.1, 0.1)

    def compute_loss(self, y_pred, y_true, mask):
        """
        计算CRF损失
        """
        y_pred = y_pred * mask
        y_true = y_true * mask
        target_score = self.target_score(y_pred, y_true)
        log_norm = self.log_norm_step(y_pred, mask)
        log_norm = self.logsumexp(log_norm, dim=1)
        return log_norm - target_score

    def target_score(self, y_pred, y_true):
        """
        计算状态标签得分 + 转移标签得分
        y_true: (batch, seq_len, out_dim)
        y_pred: (batch, seq_len, out_dim)
        """
        point_score = torch.einsum('bni,bni->b', y_pred, y_true)
        trans_score = torch.einsum('bni,ij,bnj->b', y_true[:, :-1], self.
            trans, y_true[:, 1:])
        return point_score + trans_score

    def log_norm_step(self, y_pred, mask):
        """
        计算归一化因子Z(X)
        """
        state = y_pred[:, 0]
        y_pred = y_pred[:, 1:].contiguous()
        mask = mask[:, 1:].contiguous()
        _batch, seq_len, _out_dim = y_pred.shape
        for t in range(seq_len):
            cur_mask = mask[:, t]
            state = torch.unsqueeze(state, 2)
            g = torch.unsqueeze(self.trans, 0)
            outputs = self.logsumexp(state + g, dim=1)
            outputs = outputs + y_pred[:, t]
            outputs = cur_mask * outputs + (1 - cur_mask) * state.squeeze(-1)
            state = outputs
        return outputs

    def logsumexp(self, x, dim=None, keepdim=False):
        """
        避免溢出
        """
        if dim is None:
            x, dim = x.view(-1), 0
        xm, _ = torch.max(x, dim, keepdim=True)
        out = xm + torch.log(torch.sum(torch.exp(x - xm), dim=dim, keepdim=
            True))
        return out if keepdim else out.squeeze(dim)

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


Measured latency_ms: 0.015328