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 math as tl_math
from torch import Tensor
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_add_exp_mul_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tl_math.exp(tmp1)
    tmp3 = tmp0 * tmp2
    tmp5 = tmp3 + tmp4
    tl.store(out_ptr0 + x2, tmp5, xmask)


@triton.jit
def triton_per_fused_repeat_sum_1(in_ptr0, out_ptr1, 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.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tl.store(out_ptr1 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp3, None)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (1, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_3, (1, 4), (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)
        get_raw_stream(0)
        triton_poi_fused_add_exp_mul_0[grid(256)](primals_2, primals_1,
            primals_3, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1)
        del primals_3
        buf2 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        triton_per_fused_repeat_sum_1[grid(1)](primals_1, buf2, 1, 4,
            XBLOCK=1, num_warps=2, num_stages=1)
    return buf0, buf2, primals_1, primals_2


class FlowBlock(nn.Module):
    """
    Abstract base class for any flow blocks.
    """

    def __init__(self, dimension):
        super(FlowBlock, self).__init__()
        self.dimension = dimension

    def forward(self, x: 'Tensor') ->(Tensor, Tensor):
        """
        When implemented, forward method will represent z = f(x) and log |det f'(x)/dx|
        x: (*, dimension), z: (*, dimension) and log_det: (*, 1)
        """
        raise NotImplementedError('Forward not implemented')

    def inverse(self, z: 'Tensor') ->(Tensor, Tensor):
        """
        When implemented, inverse method will represent x = f^-(z) and log |det f^-'(z)/dz|
        z: (*, dimension), x: (*, dimension) and log_det: (*, 1)
        """
        raise NotImplementedError('Inverse not implemented')


class AffineConstantFlowNew(FlowBlock):
    """
    Scales + Shifts the flow by (learned) constants per dimension.
    In NICE paper there is a Scaling layer which is a special case of this where t is None
    """

    def __init__(self, dimension, scale=True, shift=True):
        super().__init__(dimension)
        zeros = torch.zeros(size=(1, dimension))
        self.s = nn.Parameter(torch.randn(1, dimension, requires_grad=True)
            ) if scale else zeros
        self.t = nn.Parameter(torch.randn(1, dimension, requires_grad=True)
            ) if shift else zeros

    def inverse(self, z) ->(Tensor, Tensor):
        x = (z - self.t) * torch.exp(-self.s)
        log_det = torch.sum(-self.s, dim=1)
        return x, log_det.repeat(z.shape[0], 1)

    def forward(self, input_0):
        primals_1 = self.s
        primals_3 = self.t
        primals_2 = input_0
        output = call([primals_1, primals_2, primals_3])
        return output[0], output[1]


Measured latency_ms: 0.028424