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
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_poi_fused_neg_sigmoid_0(in_ptr0, out_ptr0, out_ptr1, 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)
    tmp1 = -tmp0
    tmp2 = tl.sigmoid(tmp1)
    tmp3 = tl.sigmoid(tmp0)
    tl.store(out_ptr0 + x0, tmp2, xmask)
    tl.store(out_ptr1 + x0, tmp3, xmask)


@triton.jit
def triton_poi_fused_stack_1(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 512
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4 % 8
    x0 = xindex % 4
    x4 = xindex // 32
    x2 = xindex // 32 % 4
    x3 = xindex // 128
    x5 = xindex
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x0 + 4 * x1 + 16 * x4), tmp4 & xmask, other=0.0)
    tmp6 = tl.load(in_ptr1 + (x0 + 4 * x1 + 16 * x4), tmp4 & xmask, other=0.0)
    tmp7 = 0.0001
    tmp8 = triton_helpers.maximum(tmp6, tmp7)
    tmp9 = tl_math.log(tmp8)
    tmp10 = tmp5 + tmp9
    tmp11 = tl.full(tmp10.shape, 0.0, tmp10.dtype)
    tmp12 = tl.where(tmp4, tmp10, tmp11)
    tmp13 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp16 = x2
    tmp17 = tl.full([1], 0, tl.int32)
    tmp18 = tmp16 == tmp17
    tmp19 = tl.load(in_ptr0 + (x0 + 4 * (-4 + x1) + 16 * ((3 + x2) % 4) + 
        64 * x3), tmp13 & xmask, other=0.0)
    tmp20 = tl.load(in_ptr2 + (x0 + 4 * (-4 + x1) + 16 * ((3 + x2) % 4) + 
        64 * x3), tmp13 & xmask, other=0.0)
    tmp21 = triton_helpers.maximum(tmp20, tmp7)
    tmp22 = tl_math.log(tmp21)
    tmp23 = tmp19 + tmp22
    tmp24 = float('-inf')
    tmp25 = tl.where(tmp18, tmp24, tmp23)
    tmp26 = tl.full(tmp25.shape, 0.0, tmp25.dtype)
    tmp27 = tl.where(tmp13, tmp25, tmp26)
    tmp28 = tl.where(tmp4, tmp12, tmp27)
    tl.store(out_ptr0 + x5, tmp28, xmask)


@triton.jit
def triton_poi_fused_add_bitwise_not_eq_exp_log_lt_masked_fill_max_sub_sum_2(
    in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x4 = xindex // 256
    x5 = xindex % 64
    x0 = xindex % 4
    x6 = xindex % 16
    x7 = xindex // 16 % 16
    x8 = xindex
    tmp0 = tl.load(in_ptr0 + (x5 + 64 * x4), xmask, eviction_policy=
        'evict_last')
    tmp5 = tl.load(in_ptr1 + (x6 + 32 * x7), xmask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr1 + (16 + x6 + 32 * x7), xmask, eviction_policy=
        'evict_last')
    tmp1 = x0
    tmp2 = tmp1.to(tl.float32)
    tmp3 = tmp2 < tmp0
    tmp4 = tmp3 == 0
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = float('-inf')
    tmp9 = tmp7 == tmp8
    tmp10 = 0.0
    tmp11 = tl.where(tmp9, tmp10, tmp7)
    tmp12 = tmp5 - tmp11
    tmp13 = tl_math.exp(tmp12)
    tmp14 = tmp6 - tmp11
    tmp15 = tl_math.exp(tmp14)
    tmp16 = tmp13 + tmp15
    tmp17 = 1.0
    tmp18 = tl.where(tmp9, tmp17, tmp16)
    tmp19 = tl_math.log(tmp18)
    tmp20 = tl.where(tmp9, tmp8, tmp11)
    tmp21 = tmp19 + tmp20
    tmp22 = tl.where(tmp4, tmp8, tmp21)
    tl.store(out_ptr0 + x8, tmp22, xmask)


def call(args):
    arg0_1, arg1_1, arg2_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_neg_sigmoid_0[grid(256)](arg1_1, buf0, buf1, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        del arg1_1
        buf2 = empty_strided_cuda((4, 4, 8, 4), (128, 32, 4, 1), torch.float32)
        triton_poi_fused_stack_1[grid(512)](arg0_1, buf0, buf1, buf2, 512,
            XBLOCK=256, num_warps=4, num_stages=1)
        del arg0_1
        buf3 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        triton_poi_fused_add_bitwise_not_eq_exp_log_lt_masked_fill_max_sub_sum_2[
            grid(1024)](arg2_1, buf2, buf3, 1024, XBLOCK=256, num_warps=4,
            num_stages=1)
        del arg2_1
        del buf2
    return buf3, buf1, buf0


def log_clamped(x, eps=0.0001):
    clamped_x = torch.clamp(x, min=eps)
    return torch.log(clamped_x)


def logsumexp(x, dim):
    """
    Differentiable LogSumExp: Does not creates nan gradients when all the inputs are -inf
    Args:
        x : torch.Tensor -  The input tensor
        dim: int - The dimension on which the log sum exp has to be applied
    """
    m, _ = x.max(dim=dim)
    mask = m == -float('inf')
    s = (x - m.masked_fill_(mask, 0).unsqueeze(dim=dim)).exp().sum(dim=dim)
    return s.masked_fill_(mask, 1).log() + m.masked_fill_(mask, -float('inf'))


class TransitionModelNew(nn.Module):
    """
    Transition Model of the HMM, it represents the probability of transitioning form current state to all other states
    """

    def __init__(self):
        super(TransitionModelNew, self).__init__()

    def set_staying_and_transitioning_probability(self, staying, transitioning
        ):
        """
        Make reference of the staying and transitioning probabilities as instance parameters of class
        """
        self.staying_probability = staying
        self.transition_probability = transitioning

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


Measured latency_ms: 0.042776