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
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__softmax_0(in_ptr0, 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
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tmp0 - tmp7
    tmp9 = tl_math.exp(tmp8)
    tl.store(out_ptr0 + x2, tmp9, xmask)


@triton.jit
def triton_poi_fused__softmax_1(in_ptr0, 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
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tl.store(out_ptr0 + x2, tmp8, xmask)


@triton.jit
def triton_poi_fused_mul_sum_2(in_ptr0, in_ptr1, 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 // 4
    x0 = xindex % 4
    x1 = xindex // 4 % 16
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x3, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x3), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (4 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp7 = tl.load(in_ptr0 + (2 + 4 * x3), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (8 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp11 = tl.load(in_ptr0 + (3 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp12 = tl.load(in_ptr1 + (12 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tl.store(out_ptr0 + x4, tmp14, xmask)


@triton.jit
def triton_poi_fused_div_mul_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tmp0 * tmp0
    tmp3 = tmp2 * tmp2
    tmp4 = tmp1 + tmp3
    tmp6 = tmp5 * tmp5
    tmp7 = tmp4 + tmp6
    tmp9 = tmp8 * tmp8
    tmp10 = tmp7 + tmp9
    tmp11 = 1.0
    tmp12 = tmp10 + tmp11
    tmp13 = tmp10 / tmp12
    tmp15 = 1e-07
    tmp16 = tmp10 + tmp15
    tmp17 = libdevice.sqrt(tmp16)
    tmp18 = tmp14 / tmp17
    tmp19 = tmp13 * tmp18
    tl.store(out_ptr0 + x2, tmp19, xmask)


@triton.jit
def triton_poi_fused_add_mul_sum_4(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
    x3 = xindex
    x4 = xindex % 64
    x5 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr1 + 4 * x4, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + 4 * x5, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (1 + 4 * x4), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr2 + (1 + 4 * x5), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (2 + 4 * x4), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr2 + (2 + 4 * x5), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr1 + (3 + 4 * x4), xmask, eviction_policy='evict_last'
        )
    tmp13 = tl.load(in_ptr2 + (3 + 4 * x5), xmask, eviction_policy='evict_last'
        )
    tmp3 = tmp1 * tmp2
    tmp6 = tmp4 * tmp5
    tmp7 = tmp3 + tmp6
    tmp10 = tmp8 * tmp9
    tmp11 = tmp7 + tmp10
    tmp14 = tmp12 * tmp13
    tmp15 = tmp11 + tmp14
    tmp16 = tmp0 + tmp15
    tl.store(out_ptr0 + x3, tmp16, xmask)


@triton.jit
def triton_poi_fused_mul_sum_5(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = xindex // 4 % 16
    x3 = xindex // 4
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr1 + 4 * x3, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr1 + (1 + 4 * x3), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp8 = tl.load(in_ptr1 + (2 + 4 * x3), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr1 + (3 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tl.store(out_ptr0 + x4, tmp14, xmask)


@triton.jit
def triton_poi_fused_add_mul_sum_6(in_out_ptr0, in_ptr0, in_ptr1, 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
    x5 = xindex // 4
    tmp0 = tl.load(in_out_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x4, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr1 + 4 * x5, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (1 + 4 * x4), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr1 + (1 + 4 * x5), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (2 + 4 * x4), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + (2 + 4 * x5), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr0 + (3 + 4 * x4), xmask, eviction_policy='evict_last'
        )
    tmp13 = tl.load(in_ptr1 + (3 + 4 * x5), xmask, eviction_policy='evict_last'
        )
    tmp3 = tmp1 * tmp2
    tmp6 = tmp4 * tmp5
    tmp7 = tmp3 + tmp6
    tmp10 = tmp8 * tmp9
    tmp11 = tmp7 + tmp10
    tmp14 = tmp12 * tmp13
    tmp15 = tmp11 + tmp14
    tmp16 = tmp0 + tmp15
    tl.store(in_out_ptr0 + x3, tmp16, xmask)


def call(args):
    primals_1, primals_2 = args
    args.clear()
    assert_size_stride(primals_1, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((1, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        get_raw_stream(0)
        triton_poi_fused__softmax_0[grid(256)](primals_1, buf0, 256, XBLOCK
            =128, num_warps=4, num_stages=1)
        buf1 = empty_strided_cuda((1, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        triton_poi_fused__softmax_1[grid(256)](buf0, buf1, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf2 = buf0
        del buf0
        triton_poi_fused_mul_sum_2[grid(256)](buf1, primals_2, buf2, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf3 = buf1
        del buf1
        triton_poi_fused_div_mul_3[grid(256)](buf2, buf3, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf4 = buf2
        del buf2
        triton_poi_fused_add_mul_sum_4[grid(256)](primals_1, primals_2,
            buf3, buf4, 256, XBLOCK=128, num_warps=4, num_stages=1)
        buf5 = buf3
        del buf3
        triton_poi_fused__softmax_0[grid(256)](buf4, buf5, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf6 = empty_strided_cuda((1, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        triton_poi_fused__softmax_1[grid(256)](buf5, buf6, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf7 = buf5
        del buf5
        triton_poi_fused_mul_sum_5[grid(256)](primals_2, buf6, buf7, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf8 = buf6
        del buf6
        triton_poi_fused_div_mul_3[grid(256)](buf7, buf8, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf9 = buf4
        del buf4
        triton_poi_fused_add_mul_sum_6[grid(256)](buf9, primals_2, buf8, 
            256, XBLOCK=128, num_warps=4, num_stages=1)
        buf10 = buf8
        del buf8
        triton_poi_fused__softmax_0[grid(256)](buf9, buf10, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf11 = buf7
        del buf7
        triton_poi_fused__softmax_1[grid(256)](buf10, buf11, 256, XBLOCK=
            256, num_warps=4, num_stages=1)
        buf12 = buf10
        del buf10
        triton_poi_fused_mul_sum_5[grid(256)](primals_2, buf11, buf12, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf13 = buf11
        del buf11
        triton_poi_fused_div_mul_3[grid(256)](buf12, buf13, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf14 = buf9
        del buf9
        triton_poi_fused_add_mul_sum_6[grid(256)](buf14, primals_2, buf13, 
            256, XBLOCK=128, num_warps=4, num_stages=1)
        buf15 = buf13
        del buf13
        triton_poi_fused__softmax_0[grid(256)](buf14, buf15, 256, XBLOCK=
            128, num_warps=4, num_stages=1)
        buf16 = buf12
        del buf12
        triton_poi_fused__softmax_1[grid(256)](buf15, buf16, 256, XBLOCK=
            256, num_warps=4, num_stages=1)
        buf17 = buf15
        del buf15
        triton_poi_fused_mul_sum_5[grid(256)](primals_2, buf16, buf17, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf18 = buf16
        del buf16
        triton_poi_fused_div_mul_3[grid(256)](buf17, buf18, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf19 = buf14
        del buf14
        triton_poi_fused_add_mul_sum_6[grid(256)](buf19, primals_2, buf18, 
            256, XBLOCK=128, num_warps=4, num_stages=1)
        buf20 = buf18
        del buf18
        triton_poi_fused__softmax_0[grid(256)](buf19, buf20, 256, XBLOCK=
            128, num_warps=4, num_stages=1)
        buf21 = buf19
        del buf19
        triton_poi_fused__softmax_1[grid(256)](buf20, buf21, 256, XBLOCK=
            256, num_warps=4, num_stages=1)
        buf22 = buf20
        del buf20
        triton_poi_fused_mul_sum_5[grid(256)](primals_2, buf21, buf22, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf23 = buf17
        del buf17
        triton_poi_fused_div_mul_3[grid(256)](buf22, buf23, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        del buf22
    return buf23, buf21, primals_1, primals_2, buf21


def squash(s, axis=-1, epsilon=1e-07):
    squared_norm = torch.sum(s * s, dim=axis)
    safe_norm = torch.sqrt(squared_norm + epsilon)
    squash_factor = squared_norm / (1.0 + squared_norm)
    unit_vector = torch.div(s, safe_norm.unsqueeze(-1))
    return torch.mul(squash_factor.unsqueeze(-1), unit_vector)


def safe_norm(s, axis=-1, epsilon=1e-07):
    squared_norm = torch.mul(s, s).sum(dim=axis)
    return torch.sqrt(squared_norm + epsilon)


class Agreement_Routing_DownNew(torch.nn.Module):
    """This is the localised agreement routing algorithm. It takes in the total
    prediction vectors from a layer l and computes the routing weights for 
    those predictions. It then squashes the prediction vectors using the 
    custom squash function."""

    def __init__(self, bias, input_caps_maps, input_caps_dim,
        output_caps_maps, output_caps_dim, new_hl, new_wl, num_iterations):
        super(Agreement_Routing_DownNew, self).__init__()
        self.input_caps_maps = input_caps_maps
        self.input_caps_dim = input_caps_dim
        self.output_caps_maps = output_caps_maps
        self.output_caps_dim = output_caps_dim
        self.new_hl = int(new_hl)
        self.new_wl = int(new_wl)
        self.num_iterations = num_iterations
        self.softmax = torch.nn.Softmax(dim=-1)
        self.b = torch.nn.Parameter(torch.zeros((1, self.output_caps_maps,
            self.new_hl, self.new_wl, self.input_caps_maps)))

    def forward(self, input_0):
        primals_1 = self.b
        primals_2 = input_0
        output = call([primals_1, primals_2])
        return output[0]


Measured latency_ms: 0.278089