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
import torch.nn as nn
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_max_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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_ptr1 + 0)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK])
    tmp4 = tl.load(in_ptr2 + 0)
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK])
    tmp7 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr0 + (1 + 16 * x1), xmask, eviction_policy='evict_last'
        )
    tmp10 = tl.load(in_ptr1 + 1)
    tmp11 = tl.broadcast_to(tmp10, [XBLOCK])
    tmp13 = tl.load(in_ptr2 + 1)
    tmp14 = tl.broadcast_to(tmp13, [XBLOCK])
    tmp16 = tl.load(in_ptr3 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp19 = tl.load(in_ptr0 + (2 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp20 = tl.load(in_ptr1 + 2)
    tmp21 = tl.broadcast_to(tmp20, [XBLOCK])
    tmp23 = tl.load(in_ptr2 + 2)
    tmp24 = tl.broadcast_to(tmp23, [XBLOCK])
    tmp26 = tl.load(in_ptr3 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp29 = tl.load(in_ptr0 + (3 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp30 = tl.load(in_ptr1 + 3)
    tmp31 = tl.broadcast_to(tmp30, [XBLOCK])
    tmp33 = tl.load(in_ptr2 + 3)
    tmp34 = tl.broadcast_to(tmp33, [XBLOCK])
    tmp36 = tl.load(in_ptr3 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp3 = tmp0 + tmp2
    tmp6 = tmp3 + tmp5
    tmp8 = tmp6 + tmp7
    tmp12 = tmp9 + tmp11
    tmp15 = tmp12 + tmp14
    tmp17 = tmp15 + tmp16
    tmp18 = triton_helpers.maximum(tmp8, tmp17)
    tmp22 = tmp19 + tmp21
    tmp25 = tmp22 + tmp24
    tmp27 = tmp25 + tmp26
    tmp28 = triton_helpers.maximum(tmp18, tmp27)
    tmp32 = tmp29 + tmp31
    tmp35 = tmp32 + tmp34
    tmp37 = tmp35 + tmp36
    tmp38 = triton_helpers.maximum(tmp28, tmp37)
    tmp39 = tmp8 > tmp17
    tmp40 = tmp8 == tmp17
    tmp41 = tmp8 != tmp8
    tmp42 = tmp17 != tmp17
    tmp43 = tmp41 > tmp42
    tmp44 = tmp39 | tmp43
    tmp45 = tmp41 & tmp42
    tmp46 = tmp40 | tmp45
    tmp47 = tl.full([1], 0, tl.int64)
    tmp48 = tl.full([1], 1, tl.int64)
    tmp49 = tmp47 < tmp48
    tmp50 = tmp46 & tmp49
    tmp51 = tmp44 | tmp50
    tmp52 = tl.where(tmp51, tmp8, tmp17)
    tmp53 = tl.where(tmp51, tmp47, tmp48)
    tmp54 = tmp52 > tmp27
    tmp55 = tmp52 == tmp27
    tmp56 = tmp52 != tmp52
    tmp57 = tmp27 != tmp27
    tmp58 = tmp56 > tmp57
    tmp59 = tmp54 | tmp58
    tmp60 = tmp56 & tmp57
    tmp61 = tmp55 | tmp60
    tmp62 = tl.full([1], 2, tl.int64)
    tmp63 = tmp53 < tmp62
    tmp64 = tmp61 & tmp63
    tmp65 = tmp59 | tmp64
    tmp66 = tl.where(tmp65, tmp52, tmp27)
    tmp67 = tl.where(tmp65, tmp53, tmp62)
    tmp68 = tmp66 > tmp37
    tmp69 = tmp66 == tmp37
    tmp70 = tmp66 != tmp66
    tmp71 = tmp37 != tmp37
    tmp72 = tmp70 > tmp71
    tmp73 = tmp68 | tmp72
    tmp74 = tmp70 & tmp71
    tmp75 = tmp69 | tmp74
    tmp76 = tl.full([1], 3, tl.int64)
    tmp77 = tmp67 < tmp76
    tmp78 = tmp75 & tmp77
    tmp79 = tmp73 | tmp78
    tl.where(tmp79, tmp66, tmp37)
    tmp81 = tl.where(tmp79, tmp67, tmp76)
    tl.store(out_ptr0 + x2, tmp38, xmask)
    tl.store(out_ptr1 + x2, tmp81, xmask)


@triton.jit
def triton_poi_fused_add_max_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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 + (4 + 16 * x1), xmask, eviction_policy='evict_last'
        )
    tmp2 = tl.load(in_ptr2 + 0)
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK])
    tmp6 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + (5 + 16 * x1), xmask, eviction_policy='evict_last'
        )
    tmp10 = tl.load(in_ptr2 + 1)
    tmp11 = tl.broadcast_to(tmp10, [XBLOCK])
    tmp14 = tl.load(in_ptr3 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp17 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp18 = tl.load(in_ptr1 + (6 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp19 = tl.load(in_ptr2 + 2)
    tmp20 = tl.broadcast_to(tmp19, [XBLOCK])
    tmp23 = tl.load(in_ptr3 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp27 = tl.load(in_ptr1 + (7 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp28 = tl.load(in_ptr2 + 3)
    tmp29 = tl.broadcast_to(tmp28, [XBLOCK])
    tmp32 = tl.load(in_ptr3 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp4 = tmp1 + tmp3
    tmp5 = tmp0 + tmp4
    tmp7 = tmp5 + tmp6
    tmp12 = tmp9 + tmp11
    tmp13 = tmp8 + tmp12
    tmp15 = tmp13 + tmp14
    tmp16 = triton_helpers.maximum(tmp7, tmp15)
    tmp21 = tmp18 + tmp20
    tmp22 = tmp17 + tmp21
    tmp24 = tmp22 + tmp23
    tmp25 = triton_helpers.maximum(tmp16, tmp24)
    tmp30 = tmp27 + tmp29
    tmp31 = tmp26 + tmp30
    tmp33 = tmp31 + tmp32
    tmp34 = triton_helpers.maximum(tmp25, tmp33)
    tmp35 = tmp7 > tmp15
    tmp36 = tmp7 == tmp15
    tmp37 = tmp7 != tmp7
    tmp38 = tmp15 != tmp15
    tmp39 = tmp37 > tmp38
    tmp40 = tmp35 | tmp39
    tmp41 = tmp37 & tmp38
    tmp42 = tmp36 | tmp41
    tmp43 = tl.full([1], 0, tl.int64)
    tmp44 = tl.full([1], 1, tl.int64)
    tmp45 = tmp43 < tmp44
    tmp46 = tmp42 & tmp45
    tmp47 = tmp40 | tmp46
    tmp48 = tl.where(tmp47, tmp7, tmp15)
    tmp49 = tl.where(tmp47, tmp43, tmp44)
    tmp50 = tmp48 > tmp24
    tmp51 = tmp48 == tmp24
    tmp52 = tmp48 != tmp48
    tmp53 = tmp24 != tmp24
    tmp54 = tmp52 > tmp53
    tmp55 = tmp50 | tmp54
    tmp56 = tmp52 & tmp53
    tmp57 = tmp51 | tmp56
    tmp58 = tl.full([1], 2, tl.int64)
    tmp59 = tmp49 < tmp58
    tmp60 = tmp57 & tmp59
    tmp61 = tmp55 | tmp60
    tmp62 = tl.where(tmp61, tmp48, tmp24)
    tmp63 = tl.where(tmp61, tmp49, tmp58)
    tmp64 = tmp62 > tmp33
    tmp65 = tmp62 == tmp33
    tmp66 = tmp62 != tmp62
    tmp67 = tmp33 != tmp33
    tmp68 = tmp66 > tmp67
    tmp69 = tmp64 | tmp68
    tmp70 = tmp66 & tmp67
    tmp71 = tmp65 | tmp70
    tmp72 = tl.full([1], 3, tl.int64)
    tmp73 = tmp63 < tmp72
    tmp74 = tmp71 & tmp73
    tmp75 = tmp69 | tmp74
    tl.where(tmp75, tmp62, tmp33)
    tmp77 = tl.where(tmp75, tmp63, tmp72)
    tl.store(out_ptr0 + x2, tmp34, xmask)
    tl.store(out_ptr1 + x2, tmp77, xmask)


@triton.jit
def triton_poi_fused_add_max_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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 + (8 + 16 * x1), xmask, eviction_policy='evict_last'
        )
    tmp2 = tl.load(in_ptr2 + 0)
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK])
    tmp6 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + (9 + 16 * x1), xmask, eviction_policy='evict_last'
        )
    tmp10 = tl.load(in_ptr2 + 1)
    tmp11 = tl.broadcast_to(tmp10, [XBLOCK])
    tmp14 = tl.load(in_ptr3 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp17 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp18 = tl.load(in_ptr1 + (10 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp19 = tl.load(in_ptr2 + 2)
    tmp20 = tl.broadcast_to(tmp19, [XBLOCK])
    tmp23 = tl.load(in_ptr3 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp27 = tl.load(in_ptr1 + (11 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp28 = tl.load(in_ptr2 + 3)
    tmp29 = tl.broadcast_to(tmp28, [XBLOCK])
    tmp32 = tl.load(in_ptr3 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp4 = tmp1 + tmp3
    tmp5 = tmp0 + tmp4
    tmp7 = tmp5 + tmp6
    tmp12 = tmp9 + tmp11
    tmp13 = tmp8 + tmp12
    tmp15 = tmp13 + tmp14
    tmp16 = triton_helpers.maximum(tmp7, tmp15)
    tmp21 = tmp18 + tmp20
    tmp22 = tmp17 + tmp21
    tmp24 = tmp22 + tmp23
    tmp25 = triton_helpers.maximum(tmp16, tmp24)
    tmp30 = tmp27 + tmp29
    tmp31 = tmp26 + tmp30
    tmp33 = tmp31 + tmp32
    tmp34 = triton_helpers.maximum(tmp25, tmp33)
    tmp35 = tmp7 > tmp15
    tmp36 = tmp7 == tmp15
    tmp37 = tmp7 != tmp7
    tmp38 = tmp15 != tmp15
    tmp39 = tmp37 > tmp38
    tmp40 = tmp35 | tmp39
    tmp41 = tmp37 & tmp38
    tmp42 = tmp36 | tmp41
    tmp43 = tl.full([1], 0, tl.int64)
    tmp44 = tl.full([1], 1, tl.int64)
    tmp45 = tmp43 < tmp44
    tmp46 = tmp42 & tmp45
    tmp47 = tmp40 | tmp46
    tmp48 = tl.where(tmp47, tmp7, tmp15)
    tmp49 = tl.where(tmp47, tmp43, tmp44)
    tmp50 = tmp48 > tmp24
    tmp51 = tmp48 == tmp24
    tmp52 = tmp48 != tmp48
    tmp53 = tmp24 != tmp24
    tmp54 = tmp52 > tmp53
    tmp55 = tmp50 | tmp54
    tmp56 = tmp52 & tmp53
    tmp57 = tmp51 | tmp56
    tmp58 = tl.full([1], 2, tl.int64)
    tmp59 = tmp49 < tmp58
    tmp60 = tmp57 & tmp59
    tmp61 = tmp55 | tmp60
    tmp62 = tl.where(tmp61, tmp48, tmp24)
    tmp63 = tl.where(tmp61, tmp49, tmp58)
    tmp64 = tmp62 > tmp33
    tmp65 = tmp62 == tmp33
    tmp66 = tmp62 != tmp62
    tmp67 = tmp33 != tmp33
    tmp68 = tmp66 > tmp67
    tmp69 = tmp64 | tmp68
    tmp70 = tmp66 & tmp67
    tmp71 = tmp65 | tmp70
    tmp72 = tl.full([1], 3, tl.int64)
    tmp73 = tmp63 < tmp72
    tmp74 = tmp71 & tmp73
    tmp75 = tmp69 | tmp74
    tl.where(tmp75, tmp62, tmp33)
    tmp77 = tl.where(tmp75, tmp63, tmp72)
    tl.store(out_ptr0 + x2, tmp34, xmask)
    tl.store(out_ptr1 + x2, tmp77, xmask)


@triton.jit
def triton_poi_fused_add_gather_max_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    in_ptr4, in_ptr5, in_ptr6, out_ptr0, out_ptr1, out_ptr2, out_ptr3,
    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_ptr1 + (12 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp2 = tl.load(in_ptr2 + 0)
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK])
    tmp6 = tl.load(in_ptr3 + 0)
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK])
    tmp9 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr1 + (13 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp11 = tl.load(in_ptr2 + 1)
    tmp12 = tl.broadcast_to(tmp11, [XBLOCK])
    tmp15 = tl.load(in_ptr3 + 1)
    tmp16 = tl.broadcast_to(tmp15, [XBLOCK])
    tmp33 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp34 = tl.load(in_ptr1 + (14 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp35 = tl.load(in_ptr2 + 2)
    tmp36 = tl.broadcast_to(tmp35, [XBLOCK])
    tmp39 = tl.load(in_ptr3 + 2)
    tmp40 = tl.broadcast_to(tmp39, [XBLOCK])
    tmp56 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp57 = tl.load(in_ptr1 + (15 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp58 = tl.load(in_ptr2 + 3)
    tmp59 = tl.broadcast_to(tmp58, [XBLOCK])
    tmp62 = tl.load(in_ptr3 + 3)
    tmp63 = tl.broadcast_to(tmp62, [XBLOCK])
    tmp4 = tmp1 + tmp3
    tmp5 = tmp0 + tmp4
    tmp8 = tmp5 + tmp7
    tmp13 = tmp10 + tmp12
    tmp14 = tmp9 + tmp13
    tmp17 = tmp14 + tmp16
    tmp18 = tmp8 > tmp17
    tmp19 = tmp8 == tmp17
    tmp20 = tmp8 != tmp8
    tmp21 = tmp17 != tmp17
    tmp22 = tmp20 > tmp21
    tmp23 = tmp18 | tmp22
    tmp24 = tmp20 & tmp21
    tmp25 = tmp19 | tmp24
    tmp26 = tl.full([1], 0, tl.int64)
    tmp27 = tl.full([1], 1, tl.int64)
    tmp28 = tmp26 < tmp27
    tmp29 = tmp25 & tmp28
    tmp30 = tmp23 | tmp29
    tmp31 = tl.where(tmp30, tmp8, tmp17)
    tmp32 = tl.where(tmp30, tmp26, tmp27)
    tmp37 = tmp34 + tmp36
    tmp38 = tmp33 + tmp37
    tmp41 = tmp38 + tmp40
    tmp42 = tmp31 > tmp41
    tmp43 = tmp31 == tmp41
    tmp44 = tmp31 != tmp31
    tmp45 = tmp41 != tmp41
    tmp46 = tmp44 > tmp45
    tmp47 = tmp42 | tmp46
    tmp48 = tmp44 & tmp45
    tmp49 = tmp43 | tmp48
    tmp50 = tl.full([1], 2, tl.int64)
    tmp51 = tmp32 < tmp50
    tmp52 = tmp49 & tmp51
    tmp53 = tmp47 | tmp52
    tmp54 = tl.where(tmp53, tmp31, tmp41)
    tmp55 = tl.where(tmp53, tmp32, tmp50)
    tmp60 = tmp57 + tmp59
    tmp61 = tmp56 + tmp60
    tmp64 = tmp61 + tmp63
    tmp65 = tmp54 > tmp64
    tmp66 = tmp54 == tmp64
    tmp67 = tmp54 != tmp54
    tmp68 = tmp64 != tmp64
    tmp69 = tmp67 > tmp68
    tmp70 = tmp65 | tmp69
    tmp71 = tmp67 & tmp68
    tmp72 = tmp66 | tmp71
    tmp73 = tl.full([1], 3, tl.int64)
    tmp74 = tmp55 < tmp73
    tmp75 = tmp72 & tmp74
    tmp76 = tmp70 | tmp75
    tl.where(tmp76, tmp54, tmp64)
    tmp78 = tl.where(tmp76, tmp55, tmp73)
    tmp79 = tl.full([XBLOCK], 4, tl.int32)
    tmp80 = tmp78 + tmp79
    tmp81 = tmp78 < 0
    tmp82 = tl.where(tmp81, tmp80, tmp78)
    tl.device_assert((0 <= tmp82) & (tmp82 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp82 < 4')
    tmp84 = tl.load(in_ptr4 + (tmp82 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp85 = tmp84 + tmp79
    tmp86 = tmp84 < 0
    tmp87 = tl.where(tmp86, tmp85, tmp84)
    tl.device_assert((0 <= tmp87) & (tmp87 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp87 < 4')
    tmp89 = tl.load(in_ptr5 + (tmp87 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp90 = tmp89 + tmp79
    tmp91 = tmp89 < 0
    tmp92 = tl.where(tmp91, tmp90, tmp89)
    tl.device_assert((0 <= tmp92) & (tmp92 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp92 < 4')
    tmp94 = tl.load(in_ptr6 + (tmp92 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tl.store(out_ptr0 + 4 * x0, tmp78, xmask)
    tl.store(out_ptr1 + 4 * x0, tmp94, xmask)
    tl.store(out_ptr2 + 4 * x0, tmp89, xmask)
    tl.store(out_ptr3 + 4 * x0, tmp84, xmask)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4), (4, 1))
    assert_size_stride(arg1_1, (4,), (1,))
    assert_size_stride(arg2_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(arg3_1, (4,), (1,))
    assert_size_stride(arg4_1, (4, 4), (4, 1))
    assert_size_stride(arg5_1, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(arg2_1, (16, 4), (4, 1), 0),
            reinterpret_tensor(arg0_1, (4, 4), (1, 4), 0), out=buf0)
        del arg0_1
        del arg2_1
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.int64)
        get_raw_stream(0)
        triton_poi_fused_add_max_0[grid(16)](buf0, arg1_1, arg3_1, arg4_1,
            buf1, buf2, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del arg3_1
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.int64)
        triton_poi_fused_add_max_1[grid(16)](buf1, buf0, arg1_1, arg4_1,
            buf3, buf4, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf5 = buf1
        del buf1
        buf6 = empty_strided_cuda((4, 4), (4, 1), torch.int64)
        triton_poi_fused_add_max_2[grid(16)](buf3, buf0, arg1_1, arg4_1,
            buf5, buf6, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del arg4_1
        del buf3
        buf11 = empty_strided_cuda((4, 4), (4, 1), torch.int64)
        buf7 = reinterpret_tensor(buf11, (4, 1), (4, 1), 3)
        buf8 = reinterpret_tensor(buf11, (4, 1), (4, 1), 0)
        buf9 = reinterpret_tensor(buf11, (4, 1), (4, 1), 1)
        buf10 = reinterpret_tensor(buf11, (4, 1), (4, 1), 2)
        triton_poi_fused_add_gather_max_3[grid(4)](buf5, buf0, arg1_1,
            arg5_1, buf6, buf4, buf2, buf7, buf8, buf9, buf10, 4, XBLOCK=4,
            num_warps=1, num_stages=1)
        del arg1_1
        del arg5_1
        del buf0
        del buf2
        del buf4
        del buf5
        del buf6
    return buf11,


class CRF(nn.Module):
    """
    Implements Conditional Random Fields that can be trained via
    backpropagation. 
    """

    def __init__(self, num_tags):
        super(CRF, self).__init__()
        self.num_tags = num_tags
        self.transitions = nn.Parameter(torch.Tensor(num_tags, num_tags))
        self.start_transitions = nn.Parameter(torch.randn(num_tags))
        self.stop_transitions = nn.Parameter(torch.randn(num_tags))
        nn.init.xavier_normal_(self.transitions)

    def forward(self, feats):
        if len(feats.shape) != 3:
            raise ValueError('feats must be 3-d got {}-d'.format(feats.shape))
        return self._viterbi(feats)

    def loss(self, feats, tags):
        """
        Computes negative log likelihood between features and tags.
        Essentially difference between individual sequence scores and 
        sum of all possible sequence scores (partition function)
        Parameters:
            feats: Input features [batch size, sequence length, number of tags]
            tags: Target tag indices [batch size, sequence length]. Should be between
                    0 and num_tags
        Returns:
            Negative log likelihood [a scalar] 
        """
        if len(feats.shape) != 3:
            raise ValueError('feats must be 3-d got {}-d'.format(feats.shape))
        if len(tags.shape) != 2:
            raise ValueError('tags must be 2-d but got {}-d'.format(tags.shape)
                )
        if feats.shape[:2] != tags.shape:
            raise ValueError(
                'First two dimensions of feats and tags must match')
        sequence_score = self._sequence_score(feats, tags)
        partition_function = self._partition_function(feats)
        log_probability = sequence_score - partition_function
        return -log_probability.mean()

    def _sequence_score(self, feats, tags):
        """
        Parameters:
            feats: Input features [batch size, sequence length, number of tags]
            tags: Target tag indices [batch size, sequence length]. Should be between
                    0 and num_tags
        Returns: Sequence score of shape [batch size]
        """
        feats.shape[0]
        feat_score = feats.gather(2, tags.unsqueeze(-1)).squeeze(-1).sum(dim=-1
            )
        tags_pairs = tags.unfold(1, 2, 1)
        indices = tags_pairs.permute(2, 0, 1).chunk(2)
        trans_score = self.transitions[indices].squeeze(0).sum(dim=-1)
        start_score = self.start_transitions[tags[:, 0]]
        stop_score = self.stop_transitions[tags[:, -1]]
        return feat_score + start_score + trans_score + stop_score

    def _partition_function(self, feats):
        """
        Computes the partitition function for CRF using the forward algorithm.
        Basically calculate scores for all possible tag sequences for 
        the given feature vector sequence
        Parameters:
            feats: Input features [batch size, sequence length, number of tags]
        Returns:
            Total scores of shape [batch size]
        """
        _, seq_size, num_tags = feats.shape
        if self.num_tags != num_tags:
            raise ValueError('num_tags should be {} but got {}'.format(self
                .num_tags, num_tags))
        a = feats[:, 0] + self.start_transitions.unsqueeze(0)
        transitions = self.transitions.unsqueeze(0)
        for i in range(1, seq_size):
            feat = feats[:, i].unsqueeze(1)
            a = self._log_sum_exp(a.unsqueeze(-1) + transitions + feat, 1)
        return self._log_sum_exp(a + self.stop_transitions.unsqueeze(0), 1)

    def _viterbi(self, feats):
        """
        Uses Viterbi algorithm to predict the best sequence
        Parameters:
            feats: Input features [batch size, sequence length, number of tags]
        Returns: Best tag sequence [batch size, sequence length]
        """
        _, seq_size, num_tags = feats.shape
        if self.num_tags != num_tags:
            raise ValueError('num_tags should be {} but got {}'.format(self
                .num_tags, num_tags))
        v = feats[:, 0] + self.start_transitions.unsqueeze(0)
        transitions = self.transitions.unsqueeze(0)
        paths = []
        for i in range(1, seq_size):
            feat = feats[:, i]
            v, idx = (v.unsqueeze(-1) + transitions).max(1)
            paths.append(idx)
            v = v + feat
        v, tag = (v + self.stop_transitions.unsqueeze(0)).max(1, True)
        tags = [tag]
        for idx in reversed(paths):
            tag = idx.gather(1, tag)
            tags.append(tag)
        tags.reverse()
        return torch.cat(tags, 1)

    def _log_sum_exp(self, logits, dim):
        """
        Computes log-sum-exp in a stable way
        """
        max_val, _ = logits.max(dim)
        return max_val + (logits - max_val.unsqueeze(dim)).exp().sum(dim).log()


class OutputLayer(nn.Module):
    """
    Abstract base class for output layer. 
    Handles projection to output labels
    """

    def __init__(self, hidden_size, output_size):
        super(OutputLayer, self).__init__()
        self.output_size = output_size
        self.output_projection = nn.Linear(hidden_size, output_size)

    def loss(self, hidden, labels):
        raise NotImplementedError('Must implement {}.loss'.format(self.
            __class__.__name__))


class CRFOutputLayerNew(OutputLayer):
    """
    Implements a CRF based output layer
    """

    def __init__(self, hidden_size, output_size):
        super(CRFOutputLayerNew, self).__init__(hidden_size, output_size)
        self.crf = CRF(output_size)

    def loss(self, hidden, labels):
        feats = self.output_projection(hidden)
        return self.crf.loss(feats, labels)

    def forward(self, input_0):
        arg0_1 = self.output_projection.weight
        arg1_1 = self.output_projection.bias
        arg4_1 = self.crf.transitions
        arg3_1 = self.crf.start_transitions
        arg5_1 = self.crf.stop_transitions
        arg2_1 = input_0
        output = call([arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1])
        return output[0]


Measured latency_ms: 0.034983