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
import torch.utils.data
import torch.nn.init as init
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_add_exp_max_sub_sum_0(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, 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_ptr0 + (4 + x0 + 16 * x1), xmask)
    tmp3 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (1 + 16 * x1), xmask, eviction_policy='evict_last'
        )
    tmp7 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr0 + (2 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr0 + (3 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp17 = tl.load(in_ptr1 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp5 + tmp1
    tmp8 = tmp6 + tmp7
    tmp9 = triton_helpers.maximum(tmp4, tmp8)
    tmp11 = tmp10 + tmp1
    tmp13 = tmp11 + tmp12
    tmp14 = triton_helpers.maximum(tmp9, tmp13)
    tmp16 = tmp15 + tmp1
    tmp18 = tmp16 + tmp17
    tmp19 = triton_helpers.maximum(tmp14, tmp18)
    tmp20 = tmp4 - tmp19
    tmp21 = tl_math.exp(tmp20)
    tmp22 = tmp8 - tmp19
    tmp23 = tl_math.exp(tmp22)
    tmp24 = tmp21 + tmp23
    tmp25 = tmp13 - tmp19
    tmp26 = tl_math.exp(tmp25)
    tmp27 = tmp24 + tmp26
    tmp28 = tmp18 - tmp19
    tmp29 = tl_math.exp(tmp28)
    tmp30 = tmp27 + tmp29
    tl.store(out_ptr0 + x2, tmp19, xmask)
    tl.store(out_ptr1 + x2, tmp30, xmask)


@triton.jit
def triton_poi_fused_add_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex // 4
    x2 = xindex // 16
    x1 = xindex // 4 % 4
    x0 = xindex % 4
    x4 = xindex % 16
    x5 = xindex
    tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr1 + x3, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr2 + (1 + 4 * x2), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr3 + (x1 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp11 = tl.load(in_ptr3 + (8 + x0 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp13 = tl.load(in_ptr4 + x4, xmask, eviction_policy='evict_last')
    tmp1 = tl_math.log(tmp0)
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 * tmp4
    tmp7 = 1.0
    tmp8 = tmp7 - tmp4
    tmp9 = tmp6 * tmp8
    tmp10 = tmp5 + tmp9
    tmp12 = tmp10 + tmp11
    tmp14 = tmp12 + tmp13
    tl.store(out_ptr0 + x5, tmp14, xmask)


@triton.jit
def triton_poi_fused_add_mul_2(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
    x0 = xindex % 4
    x1 = xindex // 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask)
    tmp1 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask)
    tmp3 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask)
    tmp5 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask)
    tmp20 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp22 = tl.load(in_ptr2 + x2, xmask)
    tmp24 = tl.load(in_out_ptr0 + x2, xmask)
    tmp26 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp28 = tl.load(in_ptr3 + (x0 + 16 * x1), 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 = tmp18 + tmp6
    tmp21 = tmp19 * tmp20
    tmp23 = tl_math.log(tmp22)
    tmp25 = tmp23 + tmp24
    tmp27 = tmp25 * tmp26
    tmp29 = 1.0
    tmp30 = tmp29 - tmp26
    tmp31 = tmp28 * tmp30
    tmp32 = tmp27 + tmp31
    tmp33 = tmp29 - tmp20
    tmp34 = tmp32 * tmp33
    tmp35 = tmp21 + tmp34
    tl.store(in_out_ptr0 + x2, tmp35, xmask)


@triton.jit
def triton_poi_fused_add_exp_max_sub_sum_3(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, out_ptr1, 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 + (12 + x0 + 16 * x1), xmask)
    tmp3 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr2 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp12 = tl.load(in_ptr2 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp17 = tl.load(in_ptr2 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp5 + tmp1
    tmp8 = tmp6 + tmp7
    tmp9 = triton_helpers.maximum(tmp4, tmp8)
    tmp11 = tmp10 + tmp1
    tmp13 = tmp11 + tmp12
    tmp14 = triton_helpers.maximum(tmp9, tmp13)
    tmp16 = tmp15 + tmp1
    tmp18 = tmp16 + tmp17
    tmp19 = triton_helpers.maximum(tmp14, tmp18)
    tmp20 = tmp4 - tmp19
    tmp21 = tl_math.exp(tmp20)
    tmp22 = tmp8 - tmp19
    tmp23 = tl_math.exp(tmp22)
    tmp24 = tmp21 + tmp23
    tmp25 = tmp13 - tmp19
    tmp26 = tl_math.exp(tmp25)
    tmp27 = tmp24 + tmp26
    tmp28 = tmp18 - tmp19
    tmp29 = tl_math.exp(tmp28)
    tmp30 = tmp27 + tmp29
    tl.store(out_ptr0 + x2, tmp19, xmask)
    tl.store(out_ptr1 + x2, tmp30, xmask)


@triton.jit
def triton_poi_fused__to_copy_4(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 = tmp0.to(tl.int64)
    tl.store(out_ptr0 + (x1 + 4 * y0), tmp1, xmask & ymask)


@triton.jit
def triton_poi_fused_slice_5(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 3
    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 + (4 + x1 + 4 * y0), xmask & ymask,
        eviction_policy='evict_last')
    tl.store(out_ptr0 + (y0 + 3 * x1), tmp0, xmask & ymask)


@triton.jit
def triton_poi_fused__to_copy_slice_6(in_ptr0, out_ptr0, ynumel, xnumel,
    YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 3
    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 + (x1 + 4 * y0), xmask & ymask, eviction_policy=
        'evict_last')
    tl.store(out_ptr0 + (y0 + 3 * x1), tmp0, xmask & ymask)


@triton.jit
def triton_poi_fused_add_exp_index_max_mul_sub_sum_7(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, in_ptr7, in_ptr8,
    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 + 3 * x0, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr1 + 3 * x0, xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr3 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp14 = tl.load(in_ptr4 + x0, xmask)
    tmp20 = tl.load(in_ptr3 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr0 + (1 + 3 * x0), xmask, eviction_policy='evict_last'
        )
    tmp28 = tl.load(in_ptr1 + (1 + 3 * x0), xmask, eviction_policy='evict_last'
        )
    tmp34 = tl.load(in_ptr3 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp36 = tl.load(in_ptr4 + (4 + x0), xmask)
    tmp45 = tl.load(in_ptr0 + (2 + 3 * x0), xmask, eviction_policy='evict_last'
        )
    tmp50 = tl.load(in_ptr1 + (2 + 3 * x0), xmask, eviction_policy='evict_last'
        )
    tmp56 = tl.load(in_ptr3 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp58 = tl.load(in_ptr4 + (8 + x0), xmask)
    tmp67 = tl.load(in_ptr6 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp69 = tl.load(in_ptr7 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp72 = tl.load(in_ptr8 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp77 = tl.load(in_ptr6 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp79 = tl.load(in_ptr7 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp82 = tl.load(in_ptr8 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp86 = tl.load(in_ptr6 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp88 = tl.load(in_ptr7 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp91 = tl.load(in_ptr8 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp95 = tl.load(in_ptr6 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp97 = tl.load(in_ptr7 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp100 = tl.load(in_ptr8 + (3 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp117 = tl.load(in_ptr4 + (12 + x0), xmask)
    tmp1 = tl.full([XBLOCK], 4, tl.int32)
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 < 0
    tmp4 = tl.where(tmp3, tmp2, tmp0)
    tl.device_assert((0 <= tmp4) & (tmp4 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp4 < 4')
    tmp7 = tmp6 + tmp1
    tmp8 = tmp6 < 0
    tmp9 = tl.where(tmp8, tmp7, tmp6)
    tl.device_assert((0 <= tmp9) & (tmp9 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp9 < 4')
    tmp11 = tl.load(in_ptr2 + (tmp9 + 4 * tmp4), xmask, eviction_policy=
        'evict_last')
    tmp13 = tmp11 * tmp12
    tmp15 = tmp14 + tmp1
    tmp16 = tmp14 < 0
    tmp17 = tl.where(tmp16, tmp15, tmp14)
    tl.device_assert((0 <= tmp17) & (tmp17 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp17 < 4')
    tmp19 = tl.load(in_ptr5 + (tmp17 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp21 = tmp19 * tmp20
    tmp22 = tmp13 + tmp21
    tmp24 = tmp23 + tmp1
    tmp25 = tmp23 < 0
    tmp26 = tl.where(tmp25, tmp24, tmp23)
    tl.device_assert((0 <= tmp26) & (tmp26 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp26 < 4')
    tmp29 = tmp28 + tmp1
    tmp30 = tmp28 < 0
    tmp31 = tl.where(tmp30, tmp29, tmp28)
    tl.device_assert((0 <= tmp31) & (tmp31 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp31 < 4')
    tmp33 = tl.load(in_ptr2 + (tmp31 + 4 * tmp26), xmask, eviction_policy=
        'evict_last')
    tmp35 = tmp33 * tmp34
    tmp37 = tmp36 + tmp1
    tmp38 = tmp36 < 0
    tmp39 = tl.where(tmp38, tmp37, tmp36)
    tl.device_assert((0 <= tmp39) & (tmp39 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp39 < 4')
    tmp41 = tl.load(in_ptr5 + (4 + tmp39 + 16 * x0), xmask, eviction_policy
        ='evict_last')
    tmp42 = tmp41 * tmp12
    tmp43 = tmp35 + tmp42
    tmp44 = tmp22 + tmp43
    tmp46 = tmp45 + tmp1
    tmp47 = tmp45 < 0
    tmp48 = tl.where(tmp47, tmp46, tmp45)
    tl.device_assert((0 <= tmp48) & (tmp48 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp48 < 4')
    tmp51 = tmp50 + tmp1
    tmp52 = tmp50 < 0
    tmp53 = tl.where(tmp52, tmp51, tmp50)
    tl.device_assert((0 <= tmp53) & (tmp53 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp53 < 4')
    tmp55 = tl.load(in_ptr2 + (tmp53 + 4 * tmp48), xmask, eviction_policy=
        'evict_last')
    tmp57 = tmp55 * tmp56
    tmp59 = tmp58 + tmp1
    tmp60 = tmp58 < 0
    tmp61 = tl.where(tmp60, tmp59, tmp58)
    tl.device_assert((0 <= tmp61) & (tmp61 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp61 < 4')
    tmp63 = tl.load(in_ptr5 + (8 + tmp61 + 16 * x0), xmask, eviction_policy
        ='evict_last')
    tmp64 = tmp63 * tmp34
    tmp65 = tmp57 + tmp64
    tmp66 = tmp44 + tmp65
    tmp68 = tl_math.log(tmp67)
    tmp70 = tmp68 + tmp69
    tmp71 = tmp70 * tmp56
    tmp73 = 1.0
    tmp74 = tmp73 - tmp56
    tmp75 = tmp72 * tmp74
    tmp76 = tmp71 + tmp75
    tmp78 = tl_math.log(tmp77)
    tmp80 = tmp78 + tmp79
    tmp81 = tmp80 * tmp56
    tmp83 = tmp82 * tmp74
    tmp84 = tmp81 + tmp83
    tmp85 = triton_helpers.maximum(tmp76, tmp84)
    tmp87 = tl_math.log(tmp86)
    tmp89 = tmp87 + tmp88
    tmp90 = tmp89 * tmp56
    tmp92 = tmp91 * tmp74
    tmp93 = tmp90 + tmp92
    tmp94 = triton_helpers.maximum(tmp85, tmp93)
    tmp96 = tl_math.log(tmp95)
    tmp98 = tmp96 + tmp97
    tmp99 = tmp98 * tmp56
    tmp101 = tmp100 * tmp74
    tmp102 = tmp99 + tmp101
    tmp103 = triton_helpers.maximum(tmp94, tmp102)
    tmp104 = tmp76 - tmp103
    tmp105 = tl_math.exp(tmp104)
    tmp106 = tmp84 - tmp103
    tmp107 = tl_math.exp(tmp106)
    tmp108 = tmp105 + tmp107
    tmp109 = tmp93 - tmp103
    tmp110 = tl_math.exp(tmp109)
    tmp111 = tmp108 + tmp110
    tmp112 = tmp102 - tmp103
    tmp113 = tl_math.exp(tmp112)
    tmp114 = tmp111 + tmp113
    tmp115 = tl_math.log(tmp114)
    tmp116 = tmp115 + tmp103
    tmp118 = tmp117 + tmp1
    tmp119 = tmp117 < 0
    tmp120 = tl.where(tmp119, tmp118, tmp117)
    tl.device_assert((0 <= tmp120) & (tmp120 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp120 < 4')
    tmp122 = tl.load(in_ptr5 + (12 + tmp120 + 16 * x0), xmask,
        eviction_policy='evict_last')
    tmp123 = tmp122 * tmp56
    tmp124 = tmp123 * tmp56
    tmp125 = tmp66 + tmp124
    tmp126 = tmp116 - tmp125
    tl.store(in_out_ptr0 + x0, tmp126, 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), (4, 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, 1, 4), (4, 16, 1), torch.float32)
        buf1 = empty_strided_cuda((4, 1, 4), (4, 16, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_exp_max_sub_sum_0[grid(16)](primals_1,
            primals_4, buf0, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_add_1[grid(64)](buf1, buf0, primals_3, primals_1,
            primals_4, buf2, 64, XBLOCK=64, num_warps=1, num_stages=1)
        buf3 = reinterpret_tensor(buf0, (4, 4), (4, 1), 0)
        del buf0
        triton_poi_fused_add_mul_2[grid(16)](buf3, buf2, primals_3, buf1,
            primals_1, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del buf2
        buf4 = buf1
        del buf1
        buf5 = empty_strided_cuda((4, 1, 4), (4, 16, 1), torch.float32)
        triton_poi_fused_add_exp_max_sub_sum_3[grid(16)](buf3, primals_1,
            primals_4, buf4, buf5, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf8 = empty_strided_cuda((4, 4), (4, 1), torch.int64)
        triton_poi_fused__to_copy_4[grid(4, 4)](primals_2, buf8, 4, 4,
            XBLOCK=4, YBLOCK=4, num_warps=1, num_stages=1)
        del primals_2
        buf10 = empty_strided_cuda((3, 4), (1, 3), torch.int64)
        triton_poi_fused_slice_5[grid(3, 4)](buf8, buf10, 3, 4, XBLOCK=4,
            YBLOCK=4, num_warps=1, num_stages=1)
        buf9 = empty_strided_cuda((3, 4), (1, 3), torch.int64)
        triton_poi_fused__to_copy_slice_6[grid(3, 4)](buf8, buf9, 3, 4,
            XBLOCK=4, YBLOCK=4, num_warps=1, num_stages=1)
        buf11 = empty_strided_cuda((4,), (1,), torch.float32)
        buf12 = buf11
        del buf11
        triton_poi_fused_add_exp_index_max_mul_sub_sum_7[grid(4)](buf12,
            buf9, buf10, primals_4, primals_3, buf8, primals_1, buf5, buf4,
            buf3, 4, XBLOCK=4, num_warps=1, num_stages=1)
        del buf3
        del buf4
        del buf5
        del buf8
    return buf12, primals_1, primals_3, primals_4, buf9, buf10


def initial_parameter(net, initial_method=None):
    """A method used to initialize the weights of PyTorch models.

    :param net: a PyTorch model
    :param initial_method: str, one of the following initializations

            - xavier_uniform
            - xavier_normal (default)
            - kaiming_normal, or msra
            - kaiming_uniform
            - orthogonal
            - sparse
            - normal
            - uniform

    """
    if initial_method == 'xavier_uniform':
        init_method = init.xavier_uniform_
    elif initial_method == 'xavier_normal':
        init_method = init.xavier_normal_
    elif initial_method == 'kaiming_normal' or initial_method == 'msra':
        init_method = init.kaiming_normal_
    elif initial_method == 'kaiming_uniform':
        init_method = init.kaiming_uniform_
    elif initial_method == 'orthogonal':
        init_method = init.orthogonal_
    elif initial_method == 'sparse':
        init_method = init.sparse_
    elif initial_method == 'normal':
        init_method = init.normal_
    elif initial_method == 'uniform':
        init_method = init.uniform_
    else:
        init_method = init.xavier_normal_

    def weights_init(m):
        if isinstance(m, nn.Conv2d) or isinstance(m, nn.Conv1d) or isinstance(m
            , nn.Conv3d):
            if initial_method is not None:
                init_method(m.weight.data)
            else:
                init.xavier_normal_(m.weight.data)
            init.normal_(m.bias.data)
        elif isinstance(m, nn.LSTM):
            for w in m.parameters():
                if len(w.data.size()) > 1:
                    init_method(w.data)
                else:
                    init.normal_(w.data)
        elif hasattr(m, 'weight') and m.weight.requires_grad:
            init_method(m.weight.data)
        else:
            for w in m.parameters():
                if w.requires_grad:
                    if len(w.data.size()) > 1:
                        init_method(w.data)
                    else:
                        init.normal_(w.data)
    net.apply(weights_init)


def log_sum_exp(x, dim=-1):
    max_value, _ = x.max(dim=dim, keepdim=True)
    res = torch.log(torch.sum(torch.exp(x - max_value), dim=dim, keepdim=True)
        ) + max_value
    return res.squeeze(dim)


class ConditionalRandomFieldNew(nn.Module):

    def __init__(self, tag_size, include_start_end_trans=False,
        initial_method=None):
        """
        :param tag_size: int, num of tags
        :param include_start_end_trans: bool, whether to include start/end tag
        """
        super(ConditionalRandomFieldNew, self).__init__()
        self.include_start_end_trans = include_start_end_trans
        self.tag_size = tag_size
        self.trans_m = nn.Parameter(torch.randn(tag_size, tag_size))
        if self.include_start_end_trans:
            self.start_scores = nn.Parameter(torch.randn(tag_size))
            self.end_scores = nn.Parameter(torch.randn(tag_size))
        initial_parameter(self, initial_method)

    def reset_parameter(self):
        nn.init.xavier_normal_(self.trans_m)
        if self.include_start_end_trans:
            nn.init.normal_(self.start_scores)
            nn.init.normal_(self.end_scores)

    def _normalizer_likelihood(self, logits, mask):
        """
        Computes the (batch_size,) denominator term for the log-likelihood, which is the
        sum of the likelihoods across all possible state sequences.
        :param logits:FloatTensor, max_len x batch_size x tag_size
        :param mask:ByteTensor, max_len x batch_size
        :return:FloatTensor, batch_size
        """
        seq_len, batch_size, n_tags = logits.size()
        alpha = logits[0]
        if self.include_start_end_trans:
            alpha += self.start_scores.view(1, -1)
        for i in range(1, seq_len):
            emit_score = logits[i].view(batch_size, 1, n_tags)
            trans_score = self.trans_m.view(1, n_tags, n_tags)
            tmp = alpha.view(batch_size, n_tags, 1) + emit_score + trans_score
            alpha = log_sum_exp(tmp, 1) * mask[i].view(batch_size, 1
                ) + alpha * (1 - mask[i]).view(batch_size, 1)
        if self.include_start_end_trans:
            alpha += self.end_scores.view(1, -1)
        return log_sum_exp(alpha, 1)

    def _glod_score(self, logits, tags, mask):
        """
        Compute the score for the gold path.
        :param logits: FloatTensor, max_len x batch_size x tag_size
        :param tags: LongTensor, max_len x batch_size
        :param mask: ByteTensor, max_len x batch_size
        :return:FloatTensor, batch_size
        """
        seq_len, batch_size, _ = logits.size()
        batch_idx = torch.arange(batch_size, dtype=torch.long, device=
            logits.device)
        seq_idx = torch.arange(seq_len, dtype=torch.long, device=logits.device)
        trans_score = self.trans_m[tags[:seq_len - 1], tags[1:]] * mask[1:, :]
        emit_score = logits[seq_idx.view(-1, 1), batch_idx.view(1, -1), tags
            ] * mask
        score = trans_score + emit_score[:seq_len - 1, :]
        score = score.sum(0) + emit_score[-1] * mask[-1]
        if self.include_start_end_trans:
            st_scores = self.start_scores.view(1, -1).repeat(batch_size, 1)[
                batch_idx, tags[0]]
            last_idx = mask.long().sum(0) - 1
            ed_scores = self.end_scores.view(1, -1).repeat(batch_size, 1)[
                batch_idx, tags[last_idx, batch_idx]]
            score += st_scores + ed_scores
        return score

    def viterbi_decode(self, data, mask, get_score=False):
        """
        Given a feats matrix, return best decode path and best score.
        :param data:FloatTensor, batch_size x max_len x tag_size
        :param mask:ByteTensor batch_size x max_len
        :param get_score: bool, whether to output the decode score.
        :return: scores, paths
        """
        batch_size, seq_len, n_tags = data.size()
        data = data.transpose(0, 1).data
        mask = mask.transpose(0, 1).data.float()
        vpath = data.new_zeros((seq_len, batch_size, n_tags), dtype=torch.long)
        vscore = data[0]
        if self.include_start_end_trans:
            vscore += self.start_scores.view(1, -1)
        for i in range(1, seq_len):
            prev_score = vscore.view(batch_size, n_tags, 1)
            cur_score = data[i].view(batch_size, 1, n_tags)
            trans_score = self.trans_m.view(1, n_tags, n_tags).data
            score = prev_score + trans_score + cur_score
            best_score, best_dst = score.max(1)
            vpath[i] = best_dst
            vscore = best_score * mask[i].view(batch_size, 1) + vscore * (1 -
                mask[i]).view(batch_size, 1)
        if self.include_start_end_trans:
            vscore += self.end_scores.view(1, -1)
        batch_idx = torch.arange(batch_size, dtype=torch.long, device=data.
            device)
        seq_idx = torch.arange(seq_len, dtype=torch.long, device=data.device)
        lens = mask.long().sum(0) - 1
        idxes = (lens.view(1, -1) - seq_idx.view(-1, 1)) % seq_len
        ans = data.new_empty((seq_len, batch_size), dtype=torch.long)
        ans_score, last_tags = vscore.max(1)
        ans[idxes[0], batch_idx] = last_tags
        for i in range(seq_len - 1):
            last_tags = vpath[idxes[i], batch_idx, last_tags]
            ans[idxes[i + 1], batch_idx] = last_tags
        if get_score:
            return ans_score, ans.transpose(0, 1)
        return ans.transpose(0, 1)

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


Measured latency_ms: 0.039463