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.triton_helpers import libdevice, math as tl_math
import torch.utils.data
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


@triton.jit
def triton_per_fused_abs_add_div_dot_log_mul_mv_pow_softplus_sub_sum_0(
    in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, out_ptr1,
    out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 4
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.load(in_ptr1 + r0, None)
    tmp10 = tl.load(in_ptr2 + 4 * r0, None, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr1 + 0)
    tmp12 = tl.broadcast_to(tmp11, [XBLOCK, RBLOCK])
    tmp14 = tl.load(in_ptr2 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr1 + 1)
    tmp16 = tl.broadcast_to(tmp15, [XBLOCK, RBLOCK])
    tmp19 = tl.load(in_ptr2 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp20 = tl.load(in_ptr1 + 2)
    tmp21 = tl.broadcast_to(tmp20, [XBLOCK, RBLOCK])
    tmp24 = tl.load(in_ptr2 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp25 = tl.load(in_ptr1 + 3)
    tmp26 = tl.broadcast_to(tmp25, [XBLOCK, RBLOCK])
    tmp29 = tl.load(in_ptr3 + 0)
    tmp30 = tl.broadcast_to(tmp29, [XBLOCK, RBLOCK])
    tmp37 = tl.load(in_ptr0 + 0)
    tmp38 = tl.broadcast_to(tmp37, [XBLOCK, RBLOCK])
    tmp52 = tl.load(in_ptr0 + 1)
    tmp53 = tl.broadcast_to(tmp52, [XBLOCK, RBLOCK])
    tmp60 = tl.load(in_ptr0 + 2)
    tmp61 = tl.broadcast_to(tmp60, [XBLOCK, RBLOCK])
    tmp68 = tl.load(in_ptr0 + 3)
    tmp69 = tl.broadcast_to(tmp68, [XBLOCK, RBLOCK])
    tmp2 = tmp0 * tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.sum(tmp3, 1)[:, None]
    tmp6 = tmp1 * tmp1
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tmp9 = tl.sum(tmp7, 1)[:, None]
    tmp13 = tmp10 * tmp12
    tmp17 = tmp14 * tmp16
    tmp18 = tmp13 + tmp17
    tmp22 = tmp19 * tmp21
    tmp23 = tmp18 + tmp22
    tmp27 = tmp24 * tmp26
    tmp28 = tmp23 + tmp27
    tmp31 = tmp28 + tmp30
    tmp32 = libdevice.tanh(tmp31)
    tmp33 = tmp32 * tmp32
    tmp34 = 1.0
    tmp35 = tmp34 - tmp33
    tmp36 = tmp35 * tmp12
    tmp39 = 20.0
    tmp40 = tmp5 > tmp39
    tmp41 = tl_math.exp(tmp5)
    tmp42 = libdevice.log1p(tmp41)
    tmp43 = tl.where(tmp40, tmp5, tmp42)
    tmp44 = -1.0
    tmp45 = tmp43 + tmp44
    tmp46 = tmp45 - tmp5
    tmp47 = tmp46 * tmp12
    tmp48 = tmp47 / tmp9
    tmp49 = tmp38 + tmp48
    tmp50 = tmp36 * tmp49
    tmp51 = tmp35 * tmp16
    tmp54 = tmp46 * tmp16
    tmp55 = tmp54 / tmp9
    tmp56 = tmp53 + tmp55
    tmp57 = tmp51 * tmp56
    tmp58 = tmp50 + tmp57
    tmp59 = tmp35 * tmp21
    tmp62 = tmp46 * tmp21
    tmp63 = tmp62 / tmp9
    tmp64 = tmp61 + tmp63
    tmp65 = tmp59 * tmp64
    tmp66 = tmp58 + tmp65
    tmp67 = tmp35 * tmp26
    tmp70 = tmp46 * tmp26
    tmp71 = tmp70 / tmp9
    tmp72 = tmp69 + tmp71
    tmp73 = tmp67 * tmp72
    tmp74 = tmp66 + tmp73
    tmp75 = tmp74 + tmp34
    tmp76 = tl_math.abs(tmp75)
    tmp77 = 1e-08
    tmp78 = tmp76 + tmp77
    tmp79 = tl_math.log(tmp78)
    tl.store(out_ptr2 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp31, None)
    tl.store(in_out_ptr0 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp79, None)
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp5, None)
    tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp9, None)


@triton.jit
def triton_poi_fused_add_mul_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
    in_ptr5, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + 0)
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK])
    tmp12 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr4 + 0)
    tmp15 = tl.broadcast_to(tmp14, [XBLOCK])
    tmp18 = tl.load(in_ptr5 + x1, xmask, eviction_policy='evict_last')
    tmp4 = 20.0
    tmp5 = tmp3 > tmp4
    tmp6 = tl_math.exp(tmp3)
    tmp7 = libdevice.log1p(tmp6)
    tmp8 = tl.where(tmp5, tmp3, tmp7)
    tmp9 = -1.0
    tmp10 = tmp8 + tmp9
    tmp11 = tmp10 - tmp3
    tmp13 = tmp11 * tmp12
    tmp16 = tmp13 / tmp15
    tmp17 = tmp1 + tmp16
    tmp19 = libdevice.tanh(tmp18)
    tmp20 = tmp17 * tmp19
    tmp21 = tmp0 + tmp20
    tl.store(out_ptr0 + x2, tmp21, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (4,), (1,))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    assert_size_stride(primals_4, (1,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((), (), torch.float32)
        buf1 = empty_strided_cuda((), (), torch.float32)
        buf2 = empty_strided_cuda((4,), (1,), torch.float32)
        buf4 = empty_strided_cuda((4,), (1,), torch.float32)
        buf5 = buf4
        del buf4
        get_raw_stream(0)
        triton_per_fused_abs_add_div_dot_log_mul_mv_pow_softplus_sub_sum_0[grid
            (1)](buf5, primals_1, primals_2, primals_3, primals_4, buf0,
            buf1, buf2, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_add_mul_1[grid(16)](primals_3, primals_1, buf0,
            primals_2, buf1, buf2, buf3, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        del buf0
        del buf1
        del buf2
    return buf3, buf5, primals_1, primals_2, primals_3, primals_4


class PlanarFlowNew(nn.Module):
    """Planar normalizing flow [Rezende & Mohamed 2015].
    Provides a tighter bound on the ELBO by giving more expressive
    power to the approximate distribution, such as by introducing
    covariance between terms.
    @param in_features: integer
                        number of input dimensions. this is often
                        the dimensionality of the latent variables
    """

    def __init__(self, in_features):
        super(PlanarFlowNew, self).__init__()
        self.u = nn.Parameter(torch.randn(in_features))
        self.w = nn.Parameter(torch.randn(in_features))
        self.b = nn.Parameter(torch.ones(1))

    def forward(self, input_0):
        primals_1 = self.u
        primals_2 = self.w
        primals_4 = self.b
        primals_3 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4])
        return output[0], output[1]


Measured latency_ms: 0.028402