# AOT ID: ['0_forward']
from ctypes import c_void_p, c_long, c_int
import torch
import math
import random
import os
import tempfile
from math import inf, nan
from torch._inductor.hooks import run_intermediate_hooks
from torch._inductor.utils import maybe_profile
from torch._inductor.codegen.memory_planning import _align as align
from torch import device, empty_strided
from torch._inductor.async_compile import AsyncCompile
from torch._inductor.select_algorithm import extern_kernels
from torch._inductor.codegen.multi_kernel import MultiKernelCall
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid, split_scan_grid, grid_combo_kernels, start_graph, end_graph
from torch._C import _cuda_getCurrentRawStream as get_raw_stream

aten = torch.ops.aten
inductor_ops = torch.ops.inductor
_quantized = torch.ops._quantized
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cpu = torch._C._dynamo.guards._empty_strided_cpu
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
empty_strided_xpu = torch._C._dynamo.guards._empty_strided_xpu
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
alloc_from_pool = torch.ops.inductor._alloc_from_pool
async_compile = AsyncCompile()


# kernel path: runs/run_shard_6/inductor_cache/ct/cctkkvozb7bbtwro74xyr3nelrb43drrk2nh3u7rtkrj5hoowovz.py
# Topologically Sorted Source Nodes: [softplus], Original ATen: [aten.softplus]
# Source node to ATen node mapping:
#   softplus => exp, gt, log1p, where
# Graph fragment:
#   %exp : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%primals_1,), kwargs = {})
#   %log1p : [num_users=1] = call_function[target=torch.ops.aten.log1p.default](args = (%exp,), kwargs = {})
#   %gt : [num_users=1] = call_function[target=torch.ops.aten.gt.Scalar](args = (%primals_1, 20), kwargs = {})
#   %where : [num_users=2] = call_function[target=torch.ops.aten.where.self](args = (%gt, %primals_1, %log1p), kwargs = {})
triton_poi_fused_softplus_0 = async_compile.triton('triton_poi_fused_softplus_0', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[16], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_softplus_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 1, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@triton.jit
def triton_poi_fused_softplus_0(in_ptr0, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 16
    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 = 20.0
    tmp2 = tmp0 > tmp1
    tmp3 = tl_math.exp(tmp0)
    tmp4 = libdevice.log1p(tmp3)
    tmp5 = tl.where(tmp2, tmp0, tmp4)
    tl.store(out_ptr0 + (x0), tmp5, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_6/inductor_cache/g5/cg5vjab4tgfcvlipoo6kyzdvnecwsb6yvgoa3gy5w2mmeweujz6a.py
# Topologically Sorted Source Nodes: [getitem, mul, neg, log_sigmoid, mul_1, log_sigmoid_1, mul_2, log_unnormalized_prob, add_1, lgamma, neg_1, add_2, lgamma_1, add_3, lgamma_2, log_normalization, sub, ll, neg_2], Original ATen: [aten.index, aten.mul, aten.neg, aten.log_sigmoid_forward, aten.add, aten.lgamma, aten.sub, aten.sum]
# Source node to ATen node mapping:
#   add_1 => add_1
#   add_2 => add_2
#   add_3 => add_3
#   getitem => index
#   lgamma => lgamma
#   lgamma_1 => lgamma_1
#   lgamma_2 => lgamma_2
#   ll => sum_1
#   log_normalization => add_4
#   log_sigmoid => abs_1, exp_1, full_default, log1p_1, minimum, neg_1, sub
#   log_sigmoid_1 => abs_2, exp_2, log1p_2, minimum_1, neg_2, sub_1
#   log_unnormalized_prob => add
#   mul => mul
#   mul_1 => mul_1
#   mul_2 => mul_2
#   neg => neg
#   neg_1 => neg_3
#   neg_2 => neg_4
#   sub => sub_2
# Graph fragment:
#   %index : [num_users=1] = call_function[target=torch.ops.aten.index.Tensor](args = (%where, [None, %primals_3]), kwargs = {})
#   %mul : [num_users=3] = call_function[target=torch.ops.aten.mul.Tensor](args = (%primals_4, %index), kwargs = {})
#   %neg : [num_users=2] = call_function[target=torch.ops.aten.neg.default](args = (%primals_5,), kwargs = {})
#   %full_default : [num_users=2] = call_function[target=torch.ops.aten.full.default](args = ([], 0), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %minimum : [num_users=1] = call_function[target=torch.ops.aten.minimum.default](args = (%full_default, %neg), kwargs = {})
#   %abs_1 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%neg,), kwargs = {})
#   %neg_1 : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%abs_1,), kwargs = {})
#   %exp_1 : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%neg_1,), kwargs = {})
#   %log1p_1 : [num_users=1] = call_function[target=torch.ops.aten.log1p.default](args = (%exp_1,), kwargs = {})
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%minimum, %log1p_1), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul, %sub), kwargs = {})
#   %minimum_1 : [num_users=1] = call_function[target=torch.ops.aten.minimum.default](args = (%full_default, %primals_5), kwargs = {})
#   %abs_2 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%primals_5,), kwargs = {})
#   %neg_2 : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%abs_2,), kwargs = {})
#   %exp_2 : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%neg_2,), kwargs = {})
#   %log1p_2 : [num_users=1] = call_function[target=torch.ops.aten.log1p.default](args = (%exp_2,), kwargs = {})
#   %sub_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%minimum_1, %log1p_2), kwargs = {})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%permute, %sub_1), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul_1, %mul_2), kwargs = {})
#   %add_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul, %permute), kwargs = {})
#   %lgamma : [num_users=1] = call_function[target=torch.ops.aten.lgamma.default](args = (%add_1,), kwargs = {})
#   %neg_3 : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%lgamma,), kwargs = {})
#   %add_2 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%permute, 1.0), kwargs = {})
#   %lgamma_1 : [num_users=1] = call_function[target=torch.ops.aten.lgamma.default](args = (%add_2,), kwargs = {})
#   %add_3 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%neg_3, %lgamma_1), kwargs = {})
#   %lgamma_2 : [num_users=1] = call_function[target=torch.ops.aten.lgamma.default](args = (%mul,), kwargs = {})
#   %add_4 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_3, %lgamma_2), kwargs = {})
#   %sub_2 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%add, %add_4), kwargs = {})
#   %sum_1 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%sub_2,), kwargs = {})
#   %neg_4 : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%sum_1,), kwargs = {})
triton_red_fused_add_index_lgamma_log_sigmoid_forward_mul_neg_sub_sum_1 = async_compile.triton('triton_red_fused_add_index_lgamma_log_sigmoid_forward_mul_neg_sub_sum_1', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.reduction(
    size_hints=[1, 256],
    reduction_hint=ReductionHint.DEFAULT,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*i64', 3: '*fp32', 4: '*fp32', 5: '*i64', 6: 'i32', 7: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {6: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4, 5, 7), equal_to_1=(6,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_red_fused_add_index_lgamma_log_sigmoid_forward_mul_neg_sub_sum_1', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 4, 'num_reduction': 1, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False}
)
@triton.jit
def triton_red_fused_add_index_lgamma_log_sigmoid_forward_mul_neg_sub_sum_1(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, xnumel, rnumel, XBLOCK : tl.constexpr, RBLOCK : tl.constexpr):
    xnumel = 1
    rnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rbase = tl.arange(0, RBLOCK)[None, :]
    _tmp40 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r3 = rindex
        r0 = rindex % 4
        r1 = (rindex // 4) % 4
        tmp0 = tl.load(in_ptr0 + (r3), rmask, eviction_policy='evict_first', other=0.0)
        tmp1 = tl.load(in_ptr1 + (r0), rmask, eviction_policy='evict_last', other=0.0)
        tmp9 = tl.load(in_ptr3 + (r1), rmask, eviction_policy='evict_last', other=0.0)
        tmp19 = tl.load(in_ptr4 + (r1 + (4*r0)), rmask, eviction_policy='evict_last', other=0.0)
        tmp2 = tl.full([XBLOCK, RBLOCK], 4, tl.int32)
        tmp3 = tmp1 + tmp2
        tmp4 = tmp1 < 0
        tmp5 = tl.where(tmp4, tmp3, tmp1)
        tl.device_assert(((0 <= tmp5) & (tmp5 < 4)) | ~(rmask), "index out of bounds: 0 <= tmp5 < 4")
        tmp7 = tl.load(in_ptr2 + (tmp5 + (4*r1)), rmask, eviction_policy='evict_last', other=0.0)
        tmp8 = tmp0 * tmp7
        tmp10 = -tmp9
        tmp11 = 0.0
        tmp12 = triton_helpers.minimum(tmp11, tmp10)
        tmp13 = tl_math.abs(tmp10)
        tmp14 = -tmp13
        tmp15 = tl_math.exp(tmp14)
        tmp16 = libdevice.log1p(tmp15)
        tmp17 = tmp12 - tmp16
        tmp18 = tmp8 * tmp17
        tmp20 = tmp19.to(tl.float32)
        tmp21 = triton_helpers.minimum(tmp11, tmp9)
        tmp22 = tl_math.abs(tmp9)
        tmp23 = -tmp22
        tmp24 = tl_math.exp(tmp23)
        tmp25 = libdevice.log1p(tmp24)
        tmp26 = tmp21 - tmp25
        tmp27 = tmp20 * tmp26
        tmp28 = tmp18 + tmp27
        tmp29 = tmp8 + tmp20
        tmp30 = libdevice.lgamma(tmp29)
        tmp31 = -tmp30
        tmp32 = 1.0
        tmp33 = tmp20 + tmp32
        tmp34 = libdevice.lgamma(tmp33)
        tmp35 = tmp31 + tmp34
        tmp36 = libdevice.lgamma(tmp8)
        tmp37 = tmp35 + tmp36
        tmp38 = tmp28 - tmp37
        tmp39 = tl.broadcast_to(tmp38, [XBLOCK, RBLOCK])
        tmp41 = _tmp40 + tmp39
        _tmp40 = tl.where(rmask, tmp41, _tmp40)
    tmp40 = tl.sum(_tmp40, 1)[:, None]
    tmp42 = -tmp40
    tl.debug_barrier()
    tl.store(in_out_ptr0 + (tl.full([XBLOCK, 1], 0, tl.int32)), tmp42, None)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (4, ), (1, ))
    assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_5, (4, 1), (1, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [softplus], Original ATen: [aten.softplus]
        stream0 = get_raw_stream(0)
        triton_poi_fused_softplus_0.run(primals_1, buf0, 16, grid=grid(16), stream=stream0)
        buf2 = empty_strided_cuda((), (), torch.float32)
        buf3 = buf2; del buf2  # reuse
        # Topologically Sorted Source Nodes: [getitem, mul, neg, log_sigmoid, mul_1, log_sigmoid_1, mul_2, log_unnormalized_prob, add_1, lgamma, neg_1, add_2, lgamma_1, add_3, lgamma_2, log_normalization, sub, ll, neg_2], Original ATen: [aten.index, aten.mul, aten.neg, aten.log_sigmoid_forward, aten.add, aten.lgamma, aten.sub, aten.sum]
        triton_red_fused_add_index_lgamma_log_sigmoid_forward_mul_neg_sub_sum_1.run(buf3, primals_4, primals_3, buf0, primals_5, primals_2, 1, 256, grid=grid(1), stream=stream0)
    return (buf3, buf0, primals_1, primals_2, primals_3, primals_4, primals_5, buf0, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    primals_1 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_2 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.int64)
    primals_3 = rand_strided((4, ), (1, ), device='cuda:0', dtype=torch.int64)
    primals_4 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    primals_5 = rand_strided((4, 1), (1, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([primals_1, primals_2, primals_3, primals_4, primals_5])
    return print_performance(fn, times=times, repeat=repeat)


if __name__ == "__main__":
    from torch._inductor.wrapper_benchmark import compiled_module_main
    compiled_module_main('None', benchmark_compiled_module)

import torch
import torch as t
import torch.nn as nn
from torch.nn.parameter import Parameter


class ScModel(nn.Module):
    """ Model for singel cell data """

    def __init__(self, n_genes: 'int', n_celltypes: 'int', device: 't.device'
        ) ->None:
        super().__init__()
        self.K = n_celltypes
        self.G = n_genes
        self.theta = Parameter(t.Tensor(self.G, self.K))
        self.R = t.Tensor(self.G, self.K)
        self.o = Parameter(t.Tensor(self.G, 1))
        nn.init.normal_(self.o, mean=0.0, std=1.0)
        nn.init.normal_(self.theta, mean=0.0, std=1.0)
        self.nb = t.distributions.NegativeBinomial
        self.softpl = nn.functional.softplus
        self.logsig = nn.functional.logsigmoid

    def _llnb(self, x: 't.Tensor', meta: 't.LongTensor', sf: 't.Tensor'
        ) ->t.Tensor:
        """Log Likelihood for NB-model

        Returns the log likelihood for rates and logodds
        taken as a function of the observed counts.
        Assumes that single cell data is negative
        binomial distributed.

        Returns
        -------
        The log likelihood

        """
        log_unnormalized_prob = sf * self.R[:, meta] * self.logsig(-self.o
            ) + x * self.logsig(self.o)
        log_normalization = -t.lgamma(sf * self.R[:, meta] + x) + t.lgamma(
            1.0 + x) + t.lgamma(sf * self.R[:, meta])
        ll = t.sum(log_unnormalized_prob - log_normalization)
        return ll

    def forward(self, x: 't.Tensor', meta: 't.LongTensor', sf: 't.Tensor',
        **kwargs) ->t.Tensor:
        """Forward pass during optimization"""
        self.R = self.softpl(self.theta)
        self.loss = -self._llnb(x.transpose(1, 0), meta, sf)
        return self.loss

    def __str__(self):
        return 'sc_model'


def get_inputs():
    return [torch.ones([4, 4], dtype=torch.int64), torch.ones([4], dtype=
        torch.int64), torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {'n_genes': 4, 'n_celltypes': 4, 'device': 0}]

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 libdevice, math as tl_math
import torch as t
import torch.nn as nn
from torch.nn.parameter import Parameter
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_softplus_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 16
    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 = 20.0
    tmp2 = tmp0 > tmp1
    tmp3 = tl_math.exp(tmp0)
    tmp4 = libdevice.log1p(tmp3)
    tmp5 = tl.where(tmp2, tmp0, tmp4)
    tl.store(out_ptr0 + x0, tmp5, xmask)


@triton.jit
def triton_red_fused_add_index_lgamma_log_sigmoid_forward_mul_neg_sub_sum_1(
    in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, xnumel,
    rnumel, XBLOCK: tl.constexpr, RBLOCK: tl.constexpr):
    rnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rbase = tl.arange(0, RBLOCK)[None, :]
    _tmp40 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r3 = rindex
        r0 = rindex % 4
        r1 = rindex // 4 % 4
        tmp0 = tl.load(in_ptr0 + r3, rmask, eviction_policy='evict_first',
            other=0.0)
        tmp1 = tl.load(in_ptr1 + r0, rmask, eviction_policy='evict_last',
            other=0.0)
        tmp9 = tl.load(in_ptr3 + r1, rmask, eviction_policy='evict_last',
            other=0.0)
        tmp19 = tl.load(in_ptr4 + (r1 + 4 * r0), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp2 = tl.full([XBLOCK, RBLOCK], 4, tl.int32)
        tmp3 = tmp1 + tmp2
        tmp4 = tmp1 < 0
        tmp5 = tl.where(tmp4, tmp3, tmp1)
        tl.device_assert((0 <= tmp5) & (tmp5 < 4) | ~rmask,
            'index out of bounds: 0 <= tmp5 < 4')
        tmp7 = tl.load(in_ptr2 + (tmp5 + 4 * r1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp8 = tmp0 * tmp7
        tmp10 = -tmp9
        tmp11 = 0.0
        tmp12 = triton_helpers.minimum(tmp11, tmp10)
        tmp13 = tl_math.abs(tmp10)
        tmp14 = -tmp13
        tmp15 = tl_math.exp(tmp14)
        tmp16 = libdevice.log1p(tmp15)
        tmp17 = tmp12 - tmp16
        tmp18 = tmp8 * tmp17
        tmp20 = tmp19.to(tl.float32)
        tmp21 = triton_helpers.minimum(tmp11, tmp9)
        tmp22 = tl_math.abs(tmp9)
        tmp23 = -tmp22
        tmp24 = tl_math.exp(tmp23)
        tmp25 = libdevice.log1p(tmp24)
        tmp26 = tmp21 - tmp25
        tmp27 = tmp20 * tmp26
        tmp28 = tmp18 + tmp27
        tmp29 = tmp8 + tmp20
        tmp30 = libdevice.lgamma(tmp29)
        tmp31 = -tmp30
        tmp32 = 1.0
        tmp33 = tmp20 + tmp32
        tmp34 = libdevice.lgamma(tmp33)
        tmp35 = tmp31 + tmp34
        tmp36 = libdevice.lgamma(tmp8)
        tmp37 = tmp35 + tmp36
        tmp38 = tmp28 - tmp37
        tmp39 = tl.broadcast_to(tmp38, [XBLOCK, RBLOCK])
        tmp41 = _tmp40 + tmp39
        _tmp40 = tl.where(rmask, tmp41, _tmp40)
    tmp40 = tl.sum(_tmp40, 1)[:, None]
    tmp42 = -tmp40
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp42, None)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (4,), (1,))
    assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_5, (4, 1), (1, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_softplus_0[grid(16)](primals_1, buf0, 16, XBLOCK=
            16, num_warps=1, num_stages=1)
        buf2 = empty_strided_cuda((), (), torch.float32)
        buf3 = buf2
        del buf2
        triton_red_fused_add_index_lgamma_log_sigmoid_forward_mul_neg_sub_sum_1[
            grid(1)](buf3, primals_4, primals_3, buf0, primals_5, primals_2,
            1, 256, XBLOCK=1, RBLOCK=256, num_warps=8, num_stages=1)
    return (buf3, buf0, primals_1, primals_2, primals_3, primals_4,
        primals_5, buf0)


class ScModelNew(nn.Module):
    """ Model for singel cell data """

    def __init__(self, n_genes: 'int', n_celltypes: 'int', device: 't.device'
        ) ->None:
        super().__init__()
        self.K = n_celltypes
        self.G = n_genes
        self.theta = Parameter(t.Tensor(self.G, self.K))
        self.R = t.Tensor(self.G, self.K)
        self.o = Parameter(t.Tensor(self.G, 1))
        nn.init.normal_(self.o, mean=0.0, std=1.0)
        nn.init.normal_(self.theta, mean=0.0, std=1.0)
        self.nb = t.distributions.NegativeBinomial
        self.softpl = nn.functional.softplus
        self.logsig = nn.functional.logsigmoid

    def _llnb(self, x: 't.Tensor', meta: 't.LongTensor', sf: 't.Tensor'
        ) ->t.Tensor:
        """Log Likelihood for NB-model

        Returns the log likelihood for rates and logodds
        taken as a function of the observed counts.
        Assumes that single cell data is negative
        binomial distributed.

        Returns
        -------
        The log likelihood

        """
        log_unnormalized_prob = sf * self.R[:, meta] * self.logsig(-self.o
            ) + x * self.logsig(self.o)
        log_normalization = -t.lgamma(sf * self.R[:, meta] + x) + t.lgamma(
            1.0 + x) + t.lgamma(sf * self.R[:, meta])
        ll = t.sum(log_unnormalized_prob - log_normalization)
        return ll

    def __str__(self):
        return 'sc_model'

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