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.triton_helpers import libdevice, 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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_clone_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 64
    x2 = xindex // 256
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tl.store(out_ptr0 + x3, tmp0, xmask)


@triton.jit
def triton_poi_fused_clone_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = xindex // 4 % 4
    x2 = xindex // 16 % 4
    x3 = xindex // 64 % 4
    x5 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 16 * x1 + 64 * x3), xmask,
        eviction_policy='evict_last')
    tl.store(out_ptr0 + x5, tmp0, xmask)


@triton.jit
def triton_poi_fused_add_tanh_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
    x2 = xindex
    x0 = xindex % 256
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = libdevice.tanh(tmp2)
    tl.store(out_ptr0 + x2, tmp3, xmask)


@triton.jit
def triton_poi_fused_add_3(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x5 = xindex
    x0 = xindex % 64
    x2 = xindex // 256
    tmp0 = tl.load(in_out_ptr0 + x5, xmask)
    tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(in_out_ptr0 + x5, tmp2, xmask)


@triton.jit
def triton_poi_fused_abs_add_log_mul_pow_rsub_sum_tanh_4(in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x4 = xindex % 64
    x0 = xindex % 16
    tmp0 = tl.load(in_ptr0 + 4 * x3, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + 4 * x4, xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr2 + 16 * x0, xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr3 + 16 * x0, xmask, eviction_policy='evict_last')
    tmp16 = tl.load(in_ptr0 + (1 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp17 = tl.load(in_ptr1 + (1 + 4 * x4), xmask, eviction_policy='evict_last'
        )
    tmp22 = tl.load(in_ptr2 + (5 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp23 = tl.load(in_ptr3 + (5 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp31 = tl.load(in_ptr0 + (2 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp32 = tl.load(in_ptr1 + (2 + 4 * x4), xmask, eviction_policy='evict_last'
        )
    tmp37 = tl.load(in_ptr2 + (10 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp38 = tl.load(in_ptr3 + (10 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp46 = tl.load(in_ptr0 + (3 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp47 = tl.load(in_ptr1 + (3 + 4 * x4), xmask, eviction_policy='evict_last'
        )
    tmp52 = tl.load(in_ptr2 + (15 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp53 = tl.load(in_ptr3 + (15 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = libdevice.tanh(tmp2)
    tmp4 = tmp3 * tmp3
    tmp5 = 1.0
    tmp6 = tmp5 - tmp4
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 * tmp9
    tmp11 = tmp10 + tmp5
    tmp12 = tl_math.abs(tmp11)
    tmp13 = 1e-08
    tmp14 = tmp12 + tmp13
    tmp15 = tl_math.log(tmp14)
    tmp18 = tmp16 + tmp17
    tmp19 = libdevice.tanh(tmp18)
    tmp20 = tmp19 * tmp19
    tmp21 = tmp5 - tmp20
    tmp24 = tmp22 * tmp23
    tmp25 = tmp21 * tmp24
    tmp26 = tmp25 + tmp5
    tmp27 = tl_math.abs(tmp26)
    tmp28 = tmp27 + tmp13
    tmp29 = tl_math.log(tmp28)
    tmp30 = tmp15 + tmp29
    tmp33 = tmp31 + tmp32
    tmp34 = libdevice.tanh(tmp33)
    tmp35 = tmp34 * tmp34
    tmp36 = tmp5 - tmp35
    tmp39 = tmp37 * tmp38
    tmp40 = tmp36 * tmp39
    tmp41 = tmp40 + tmp5
    tmp42 = tl_math.abs(tmp41)
    tmp43 = tmp42 + tmp13
    tmp44 = tl_math.log(tmp43)
    tmp45 = tmp30 + tmp44
    tmp48 = tmp46 + tmp47
    tmp49 = libdevice.tanh(tmp48)
    tmp50 = tmp49 * tmp49
    tmp51 = tmp5 - tmp50
    tmp54 = tmp52 * tmp53
    tmp55 = tmp51 * tmp54
    tmp56 = tmp55 + tmp5
    tmp57 = tl_math.abs(tmp56)
    tmp58 = tmp57 + tmp13
    tmp59 = tl_math.log(tmp58)
    tmp60 = tmp45 + tmp59
    tl.store(out_ptr0 + x3, tmp60, xmask)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_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))
    assert_size_stride(arg3_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, 4), (256, 64, 16, 4, 1),
            torch.float32)
        get_raw_stream(0)
        triton_poi_fused_clone_0[grid(1024)](arg0_1, buf0, 1024, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        triton_poi_fused_clone_1[grid(1024)](arg2_1, buf1, 1024, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf2 = empty_strided_cuda((64, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf0, (64, 4, 4), (16, 4, 1),
            0), reinterpret_tensor(buf1, (64, 4, 4), (16, 4, 1), 0), out=buf2)
        buf3 = buf1
        del buf1
        triton_poi_fused_add_tanh_2[grid(1024)](buf2, arg3_1, buf3, 1024,
            XBLOCK=256, num_warps=4, num_stages=1)
        buf4 = buf0
        del buf0
        triton_poi_fused_clone_1[grid(1024)](arg1_1, buf4, 1024, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf5 = empty_strided_cuda((64, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf3, (64, 4, 4), (16, 4, 1),
            0), reinterpret_tensor(buf4, (64, 4, 4), (16, 4, 1), 0), out=buf5)
        del buf3
        del buf4
        buf6 = reinterpret_tensor(buf5, (4, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0
            )
        del buf5
        buf7 = buf6
        del buf6
        triton_poi_fused_add_3[grid(1024)](buf7, arg0_1, 1024, XBLOCK=128,
            num_warps=4, num_stages=1)
        del arg0_1
        buf8 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_abs_add_log_mul_pow_rsub_sum_tanh_4[grid(256)](buf2,
            arg3_1, arg1_1, arg2_1, buf8, 256, XBLOCK=128, num_warps=4,
            num_stages=1)
        del arg1_1
        del arg2_1
        del arg3_1
        del buf2
    return buf7, buf8


class TriangularSylvesterNew(nn.Module):
    """
    Sylvester normalizing flow with Q=P or Q=I.
    """

    def __init__(self, z_size):
        super(TriangularSylvesterNew, self).__init__()
        self.z_size = z_size
        self.h = nn.Tanh()

    def der_h(self, x):
        return self.der_tanh(x)

    def der_tanh(self, x):
        return 1 - self.h(x) ** 2

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


Measured latency_ms: 0.09416