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 import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
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
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 % 16
    x2 = xindex // 64
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * 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 % 256
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tl.store(out_ptr0 + x2, tmp0, xmask)


@triton.jit
def triton_poi_fused__softmax_mul_pow_sum_zeros_like_2(in_ptr0, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 16
    x1 = xindex // 16
    x2 = xindex
    tmp6 = tl.load(in_ptr0 + (4 * x0 + 256 * x1), xmask, eviction_policy=
        'evict_last')
    tmp8 = tl.load(in_ptr0 + (64 + 4 * x0 + 256 * x1), xmask,
        eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (128 + 4 * x0 + 256 * x1), xmask,
        eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr0 + (192 + 4 * x0 + 256 * x1), xmask,
        eviction_policy='evict_last')
    tmp18 = tl.load(in_ptr0 + (1 + 4 * x0 + 256 * x1), xmask,
        eviction_policy='evict_last')
    tmp20 = tl.load(in_ptr0 + (65 + 4 * x0 + 256 * x1), xmask,
        eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr0 + (129 + 4 * x0 + 256 * x1), xmask,
        eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr0 + (193 + 4 * x0 + 256 * x1), xmask,
        eviction_policy='evict_last')
    tmp31 = tl.load(in_ptr0 + (2 + 4 * x0 + 256 * x1), xmask,
        eviction_policy='evict_last')
    tmp33 = tl.load(in_ptr0 + (66 + 4 * x0 + 256 * x1), xmask,
        eviction_policy='evict_last')
    tmp36 = tl.load(in_ptr0 + (130 + 4 * x0 + 256 * x1), xmask,
        eviction_policy='evict_last')
    tmp39 = tl.load(in_ptr0 + (194 + 4 * x0 + 256 * x1), xmask,
        eviction_policy='evict_last')
    tmp44 = tl.load(in_ptr0 + (3 + 4 * x0 + 256 * x1), xmask,
        eviction_policy='evict_last')
    tmp46 = tl.load(in_ptr0 + (67 + 4 * x0 + 256 * x1), xmask,
        eviction_policy='evict_last')
    tmp49 = tl.load(in_ptr0 + (131 + 4 * x0 + 256 * x1), xmask,
        eviction_policy='evict_last')
    tmp52 = tl.load(in_ptr0 + (195 + 4 * x0 + 256 * x1), xmask,
        eviction_policy='evict_last')
    tmp0 = 0.0
    tmp1 = tl_math.exp(tmp0)
    tmp2 = tmp1 + tmp1
    tmp3 = tmp2 + tmp1
    tmp4 = tmp3 + tmp1
    tmp5 = tmp1 / tmp4
    tmp7 = tmp5 * tmp6
    tmp9 = tmp5 * tmp8
    tmp10 = tmp7 + tmp9
    tmp12 = tmp5 * tmp11
    tmp13 = tmp10 + tmp12
    tmp15 = tmp5 * tmp14
    tmp16 = tmp13 + tmp15
    tmp17 = tmp16 * tmp16
    tmp19 = tmp5 * tmp18
    tmp21 = tmp5 * tmp20
    tmp22 = tmp19 + tmp21
    tmp24 = tmp5 * tmp23
    tmp25 = tmp22 + tmp24
    tmp27 = tmp5 * tmp26
    tmp28 = tmp25 + tmp27
    tmp29 = tmp28 * tmp28
    tmp30 = tmp17 + tmp29
    tmp32 = tmp5 * tmp31
    tmp34 = tmp5 * tmp33
    tmp35 = tmp32 + tmp34
    tmp37 = tmp5 * tmp36
    tmp38 = tmp35 + tmp37
    tmp40 = tmp5 * tmp39
    tmp41 = tmp38 + tmp40
    tmp42 = tmp41 * tmp41
    tmp43 = tmp30 + tmp42
    tmp45 = tmp5 * tmp44
    tmp47 = tmp5 * tmp46
    tmp48 = tmp45 + tmp47
    tmp50 = tmp5 * tmp49
    tmp51 = tmp48 + tmp50
    tmp53 = tmp5 * tmp52
    tmp54 = tmp51 + tmp53
    tmp55 = tmp54 * tmp54
    tmp56 = tmp43 + tmp55
    tl.store(out_ptr0 + x2, tmp56, xmask)


@triton.jit
def triton_poi_fused__softmax_add_div_mul_sqrt_sum_zeros_like_3(in_out_ptr0,
    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
    x4 = xindex // 4
    x2 = xindex // 64
    x3 = xindex % 64
    x5 = xindex
    tmp0 = tl.load(in_ptr0 + x4, xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr1 + (x3 + 256 * x2), xmask)
    tmp12 = tl.load(in_ptr1 + (64 + x3 + 256 * x2), xmask)
    tmp15 = tl.load(in_ptr1 + (128 + x3 + 256 * x2), xmask)
    tmp18 = tl.load(in_ptr1 + (192 + x3 + 256 * x2), xmask)
    tmp1 = 1.0
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 / tmp2
    tmp4 = 0.0
    tmp5 = tl_math.exp(tmp4)
    tmp6 = tmp5 + tmp5
    tmp7 = tmp6 + tmp5
    tmp8 = tmp7 + tmp5
    tmp9 = tmp5 / tmp8
    tmp11 = tmp9 * tmp10
    tmp13 = tmp9 * tmp12
    tmp14 = tmp11 + tmp13
    tmp16 = tmp9 * tmp15
    tmp17 = tmp14 + tmp16
    tmp19 = tmp9 * tmp18
    tmp20 = tmp17 + tmp19
    tmp21 = tmp3 * tmp20
    tmp22 = libdevice.sqrt(tmp0)
    tmp23 = 1e-08
    tmp24 = tmp22 + tmp23
    tmp25 = tmp21 / tmp24
    tmp26 = tmp25 >= tmp4
    tmp27 = -1.0
    tmp28 = tl.where(tmp26, tmp1, tmp27)
    tmp29 = tmp10 * tmp28
    tmp30 = tmp12 * tmp28
    tmp31 = triton_helpers.maximum(tmp29, tmp30)
    tmp32 = tmp15 * tmp28
    tmp33 = triton_helpers.maximum(tmp31, tmp32)
    tmp34 = tmp18 * tmp28
    tmp35 = triton_helpers.maximum(tmp33, tmp34)
    tmp36 = tmp29 - tmp35
    tmp37 = tmp28 * tmp25
    tmp38 = tmp36 * tmp37
    tmp39 = tl_math.exp(tmp38)
    tmp40 = tmp30 - tmp35
    tmp41 = tmp40 * tmp37
    tmp42 = tl_math.exp(tmp41)
    tmp43 = tmp39 + tmp42
    tmp44 = tmp32 - tmp35
    tmp45 = tmp44 * tmp37
    tmp46 = tl_math.exp(tmp45)
    tmp47 = tmp43 + tmp46
    tmp48 = tmp34 - tmp35
    tmp49 = tmp48 * tmp37
    tmp50 = tl_math.exp(tmp49)
    tmp51 = tmp47 + tmp50
    tmp52 = tmp39 / tmp51
    tmp53 = tmp52 * tmp10
    tmp54 = tmp42 / tmp51
    tmp55 = tmp54 * tmp12
    tmp56 = tmp53 + tmp55
    tmp57 = tmp46 / tmp51
    tmp58 = tmp57 * tmp15
    tmp59 = tmp56 + tmp58
    tmp60 = tmp50 / tmp51
    tmp61 = tmp60 * tmp18
    tmp62 = tmp59 + tmp61
    tl.store(out_ptr0 + x5, tmp25, xmask)
    tl.store(in_out_ptr0 + x5, tmp62, xmask)


@triton.jit
def triton_poi_fused__softmax_add_div_mul_pow_sqrt_sum_4(in_out_ptr0,
    in_ptr0, in_ptr1, in_ptr2, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4
    x4 = xindex
    x2 = xindex % 64
    x3 = xindex // 64
    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 + x4, xmask)
    tmp20 = tl.load(in_ptr1 + (x2 + 256 * x3), xmask)
    tmp21 = tl.load(in_ptr2 + x4, xmask)
    tmp25 = tl.load(in_ptr1 + (64 + x2 + 256 * x3), xmask)
    tmp30 = tl.load(in_ptr1 + (128 + x2 + 256 * x3), xmask)
    tmp35 = tl.load(in_ptr1 + (192 + x2 + 256 * x3), 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 = tmp13 * tmp14
    tmp16 = libdevice.sqrt(tmp10)
    tmp17 = 1e-08
    tmp18 = tmp16 + tmp17
    tmp19 = tmp15 / tmp18
    tmp22 = tmp20 * tmp21
    tmp23 = tmp20 * tmp19
    tmp24 = tmp22 + tmp23
    tmp26 = tmp25 * tmp21
    tmp27 = tmp25 * tmp19
    tmp28 = tmp26 + tmp27
    tmp29 = triton_helpers.maximum(tmp24, tmp28)
    tmp31 = tmp30 * tmp21
    tmp32 = tmp30 * tmp19
    tmp33 = tmp31 + tmp32
    tmp34 = triton_helpers.maximum(tmp29, tmp33)
    tmp36 = tmp35 * tmp21
    tmp37 = tmp35 * tmp19
    tmp38 = tmp36 + tmp37
    tmp39 = triton_helpers.maximum(tmp34, tmp38)
    tmp40 = tmp24 - tmp39
    tmp41 = tl_math.exp(tmp40)
    tmp42 = tmp28 - tmp39
    tmp43 = tl_math.exp(tmp42)
    tmp44 = tmp41 + tmp43
    tmp45 = tmp33 - tmp39
    tmp46 = tl_math.exp(tmp45)
    tmp47 = tmp44 + tmp46
    tmp48 = tmp38 - tmp39
    tmp49 = tl_math.exp(tmp48)
    tmp50 = tmp47 + tmp49
    tmp51 = tmp41 / tmp50
    tmp52 = tmp51 * tmp20
    tmp53 = tmp43 / tmp50
    tmp54 = tmp53 * tmp25
    tmp55 = tmp52 + tmp54
    tmp56 = tmp46 / tmp50
    tmp57 = tmp56 * tmp30
    tmp58 = tmp55 + tmp57
    tmp59 = tmp49 / tmp50
    tmp60 = tmp59 * tmp35
    tmp61 = tmp58 + tmp60
    tl.store(in_out_ptr0 + x4, tmp61, xmask)


@triton.jit
def triton_poi_fused_add_div_mul_pow_sqrt_sum_5(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 = tmp13 * tmp14
    tmp16 = libdevice.sqrt(tmp10)
    tmp17 = 1e-08
    tmp18 = tmp16 + tmp17
    tmp19 = tmp15 / tmp18
    tl.store(out_ptr0 + x2, tmp19, xmask)


def call(args):
    primals_1, primals_2 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 1, 4, 4, 4, 4), (256, 256, 64, 16, 4,
            1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_clone_0[grid(1024)](primals_1, buf0, 1024, XBLOCK=
            256, num_warps=4, num_stages=1)
        del primals_1
        buf1 = empty_strided_cuda((4, 1, 4, 4, 4, 4), (256, 256, 64, 16, 4,
            1), torch.float32)
        triton_poi_fused_clone_1[grid(1024)](primals_2, buf1, 1024, XBLOCK=
            256, num_warps=4, num_stages=1)
        del primals_2
        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)
        del buf1
        buf3 = empty_strided_cuda((4, 1, 1, 4, 4, 1), (16, 64, 64, 4, 1, 64
            ), torch.float32)
        triton_poi_fused__softmax_mul_pow_sum_zeros_like_2[grid(64)](buf2,
            buf3, 64, XBLOCK=64, num_warps=1, num_stages=1)
        buf4 = empty_strided_cuda((4, 1, 1, 4, 4, 4), (64, 256, 256, 16, 4,
            1), torch.float32)
        buf6 = empty_strided_cuda((4, 1, 1, 4, 4, 4), (64, 256, 256, 16, 4,
            1), torch.float32)
        buf7 = buf6
        del buf6
        triton_poi_fused__softmax_add_div_mul_sqrt_sum_zeros_like_3[grid(256)](
            buf7, buf3, buf2, buf4, 256, XBLOCK=256, num_warps=4, num_stages=1)
        del buf3
        buf10 = empty_strided_cuda((4, 1, 1, 4, 4, 4), (64, 256, 256, 16, 4,
            1), torch.float32)
        buf11 = buf10
        del buf10
        triton_poi_fused__softmax_add_div_mul_pow_sqrt_sum_4[grid(256)](buf11,
            buf7, buf2, buf4, 256, XBLOCK=256, num_warps=4, num_stages=1)
        del buf4
        buf12 = reinterpret_tensor(buf7, (4, 1, 1, 4, 4, 4), (64, 64, 64, 
            16, 4, 1), 0)
        del buf7
        triton_poi_fused_add_div_mul_pow_sqrt_sum_5[grid(256)](buf11, buf12,
            256, XBLOCK=256, num_warps=4, num_stages=1)
        del buf11
    return reinterpret_tensor(buf12, (4, 4, 4, 4), (64, 16, 4, 1), 0
        ), buf2, reinterpret_tensor(buf0, (64, 4, 4), (16, 1, 4), 0)


def squash(x, dim=-1, epsilon=1e-08):
    norm = (x ** 2).sum(dim=dim, keepdim=True)
    x = norm / (norm + 1) * x / (torch.sqrt(norm) + epsilon)
    return x


class RoutingCapsulesNew(nn.Module):
    """
    input
        capsules_num: new feature, num to duplicate
        capsules_dim: new feature, each capsules' dim
        in_capsules_num: last layer's capsules_num
        in_capsules_dim: last layer's capsules_dim
    """

    def __init__(self, capsules_num, capsules_dim, in_capsules_num,
        in_capsules_dim, num_iterations=3):
        super(RoutingCapsulesNew, self).__init__()
        self.capsules_num = capsules_num
        self.capsules_dim = capsules_dim
        self.in_capsules_num = in_capsules_num
        self.in_capsules_dim = in_capsules_dim
        self.num_iterations = num_iterations
        self.W = nn.Parameter(torch.randn(1, capsules_num, in_capsules_num,
            in_capsules_dim, capsules_dim))

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


Measured latency_ms: 0.041198