# AOT ID: ['0_forward']
from ctypes import c_void_p, c_long, c_int
import torch
import math
import random
import os
import tempfile
from math import inf, nan
from torch._inductor.hooks import run_intermediate_hooks
from torch._inductor.utils import maybe_profile
from torch._inductor.codegen.memory_planning import _align as align
from torch import device, empty_strided
from torch._inductor.async_compile import AsyncCompile
from torch._inductor.select_algorithm import extern_kernels
from torch._inductor.codegen.multi_kernel import MultiKernelCall
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid, split_scan_grid, grid_combo_kernels, start_graph, end_graph
from torch._C import _cuda_getCurrentRawStream as get_raw_stream

aten = torch.ops.aten
inductor_ops = torch.ops.inductor
_quantized = torch.ops._quantized
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cpu = torch._C._dynamo.guards._empty_strided_cpu
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
empty_strided_xpu = torch._C._dynamo.guards._empty_strided_xpu
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
alloc_from_pool = torch.ops.inductor._alloc_from_pool
async_compile = AsyncCompile()


# kernel path: runs/run_shard_4/inductor_cache/6d/c6dttqk6xutp77btpuo4ywspudms2jqt6a26lv6ujp22nczd4jrp.py
# Topologically Sorted Source Nodes: [u_hat], Original ATen: [aten.clone]
# Source node to ATen node mapping:
#   u_hat => clone
# Graph fragment:
#   %clone : [num_users=1] = call_function[target=torch.ops.aten.clone.default](args = (%expand,), kwargs = {memory_format: torch.contiguous_format})
triton_poi_fused_clone_0 = async_compile.triton('triton_poi_fused_clone_0', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[1024], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_clone_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 1, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@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)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/my/cmy25ejia5o2rat776uwsbvvmsxs5ygvdojzg6lcj5wdlcpt6zvw.py
# Topologically Sorted Source Nodes: [u_hat], Original ATen: [aten.clone]
# Source node to ATen node mapping:
#   u_hat => clone_1
# Graph fragment:
#   %clone_1 : [num_users=1] = call_function[target=torch.ops.aten.clone.default](args = (%expand_1,), kwargs = {memory_format: torch.contiguous_format})
triton_poi_fused_clone_1 = async_compile.triton('triton_poi_fused_clone_1', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[1024], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_clone_1', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 1, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@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)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/ax/caxtwwdhx4z7xnzczowzqzedsuimqfsdl4ojybtxh57mrvl7r62n.py
# Topologically Sorted Source Nodes: [b, c, mul, s, pow_1, norm], Original ATen: [aten.zeros_like, aten._softmax, aten.mul, aten.sum, aten.pow]
# Source node to ATen node mapping:
#   b => full_default
#   c => amax, div, exp, sub, sum_1
#   mul => mul
#   norm => sum_3
#   pow_1 => pow_1
#   s => sum_2
# Graph fragment:
#   %full_default : [num_users=2] = call_function[target=torch.ops.aten.full.default](args = ([4, 1, 4, 4, 4, 4], 0), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %amax : [num_users=1] = call_function[target=torch.ops.aten.amax.default](args = (%full_default, [2], True), kwargs = {})
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%full_default, %amax), kwargs = {})
#   %exp : [num_users=2] = call_function[target=torch.ops.aten.exp.default](args = (%sub,), kwargs = {})
#   %sum_1 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%exp, [2], True), kwargs = {})
#   %div : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%exp, %sum_1), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div, %view_2), kwargs = {})
#   %sum_2 : [num_users=2] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul, [2], True), kwargs = {})
#   %pow_1 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sum_2, 2), kwargs = {})
#   %sum_3 : [num_users=3] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%pow_1, [-1], True), kwargs = {})
triton_poi_fused__softmax_mul_pow_sum_zeros_like_2 = async_compile.triton('triton_poi_fused__softmax_mul_pow_sum_zeros_like_2', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[64], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused__softmax_mul_pow_sum_zeros_like_2', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 16, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@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)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/ks/ckscyft52mffu7fo4m6ucqhkorymbxszfq2kpaapnmfzmse3koc3.py
# Topologically Sorted Source Nodes: [b, c, mul, s, add, truediv, mul_1, sqrt, add_1, x_1, c_1, mul_3, s_1], Original ATen: [aten.zeros_like, aten._softmax, aten.mul, aten.sum, aten.add, aten.div, aten.sqrt]
# Source node to ATen node mapping:
#   add => add
#   add_1 => add_1
#   b => full_default
#   c => amax, div, exp, sub, sum_1
#   c_1 => div_3, exp_1, sum_4
#   mul => mul
#   mul_1 => mul_1
#   mul_3 => mul_3
#   s => sum_2
#   s_1 => sum_5
#   sqrt => sqrt
#   truediv => div_1
#   x_1 => div_2
# Graph fragment:
#   %full_default : [num_users=2] = call_function[target=torch.ops.aten.full.default](args = ([4, 1, 4, 4, 4, 4], 0), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %amax : [num_users=1] = call_function[target=torch.ops.aten.amax.default](args = (%full_default, [2], True), kwargs = {})
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%full_default, %amax), kwargs = {})
#   %exp : [num_users=2] = call_function[target=torch.ops.aten.exp.default](args = (%sub,), kwargs = {})
#   %sum_1 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%exp, [2], True), kwargs = {})
#   %div : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%exp, %sum_1), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div, %view_2), kwargs = {})
#   %sum_2 : [num_users=2] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul, [2], True), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sum_3, 1), kwargs = {})
#   %div_1 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sum_3, %add), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_1, %sum_2), kwargs = {})
#   %sqrt : [num_users=1] = call_function[target=torch.ops.aten.sqrt.default](args = (%sum_3,), kwargs = {})
#   %add_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sqrt, 1e-08), kwargs = {})
#   %div_2 : [num_users=3] = call_function[target=torch.ops.aten.div.Tensor](args = (%mul_1, %add_1), kwargs = {})
#   %scalar_tensor_default : [num_users=2] = call_function[target=torch.ops.aten.scalar_tensor.default](args = (1,), kwargs = {dtype: torch.float32, device: cuda:0, pin_memory: False})
#   %ge_scalar : [num_users=1] = call_function[target=torch.ops.aten.ge.Scalar](args = (%div_2, 0), kwargs = {})
#   %neg_default : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%scalar_tensor_default,), kwargs = {})
#   %where_self : [num_users=2] = call_function[target=torch.ops.aten.where.self](args = (%ge_scalar, %scalar_tensor_default, %neg_default), kwargs = {})
#   %mul_tensor : [num_users=2] = call_function[target=torch.ops.aten.mul.Tensor](args = (%view_2, %where_self), kwargs = {})
#   %amax_default : [num_users=1] = call_function[target=torch.ops.aten.amax.default](args = (%mul_tensor, [2], True), kwargs = {})
#   %sub_tensor : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%mul_tensor, %amax_default), kwargs = {})
#   %mul_tensor_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%where_self, %div_2), kwargs = {})
#   %mul_tensor_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sub_tensor, %mul_tensor_1), kwargs = {})
#   %exp_1 : [num_users=2] = call_function[target=torch.ops.aten.exp.default](args = (%mul_tensor_2,), kwargs = {})
#   %sum_4 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%exp_1, [2], True), kwargs = {})
#   %div_3 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%exp_1, %sum_4), kwargs = {})
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_3, %view_2), kwargs = {})
#   %sum_5 : [num_users=2] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul_3, [2], True), kwargs = {})
triton_poi_fused__softmax_add_div_mul_sqrt_sum_zeros_like_3 = async_compile.triton('triton_poi_fused__softmax_add_div_mul_sqrt_sum_zeros_like_3', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[256], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused__softmax_add_div_mul_sqrt_sum_zeros_like_3', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 5, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@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)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/pw/cpwf6totvtlynglzhnop5czfqitxnvzindzzqixm2gmwwmxfsc6v.py
# Topologically Sorted Source Nodes: [b_1, pow_2, norm_1, add_3, truediv_2, mul_4, sqrt_1, add_4, x_2, uv_1, b_2, c_2, mul_6, s_2], Original ATen: [aten.add, aten.pow, aten.sum, aten.div, aten.mul, aten.sqrt, aten._softmax]
# Source node to ATen node mapping:
#   add_3 => add_3
#   add_4 => add_4
#   b_1 => mul_2
#   b_2 => add_5
#   c_2 => amax_2, div_6, exp_2, sub_2, sum_7
#   mul_4 => mul_4
#   mul_6 => mul_6
#   norm_1 => sum_6
#   pow_2 => pow_2
#   s_2 => sum_8
#   sqrt_1 => sqrt_1
#   truediv_2 => div_4
#   uv_1 => mul_5
#   x_2 => div_5
# Graph fragment:
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%view_2, %div_2), kwargs = {})
#   %pow_2 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sum_5, 2), kwargs = {})
#   %sum_6 : [num_users=3] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%pow_2, [-1], True), kwargs = {})
#   %add_3 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sum_6, 1), kwargs = {})
#   %div_4 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sum_6, %add_3), kwargs = {})
#   %mul_4 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_4, %sum_5), kwargs = {})
#   %sqrt_1 : [num_users=1] = call_function[target=torch.ops.aten.sqrt.default](args = (%sum_6,), kwargs = {})
#   %add_4 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sqrt_1, 1e-08), kwargs = {})
#   %div_5 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%mul_4, %add_4), kwargs = {})
#   %mul_5 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%view_2, %div_5), kwargs = {})
#   %add_5 : [num_users=2] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul_2, %mul_5), kwargs = {})
#   %amax_2 : [num_users=1] = call_function[target=torch.ops.aten.amax.default](args = (%add_5, [2], True), kwargs = {})
#   %sub_2 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%add_5, %amax_2), kwargs = {})
#   %exp_2 : [num_users=2] = call_function[target=torch.ops.aten.exp.default](args = (%sub_2,), kwargs = {})
#   %sum_7 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%exp_2, [2], True), kwargs = {})
#   %div_6 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%exp_2, %sum_7), kwargs = {})
#   %mul_6 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_6, %view_2), kwargs = {})
#   %sum_8 : [num_users=2] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul_6, [2], True), kwargs = {})
triton_poi_fused__softmax_add_div_mul_pow_sqrt_sum_4 = async_compile.triton('triton_poi_fused__softmax_add_div_mul_pow_sqrt_sum_4', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[256], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused__softmax_add_div_mul_pow_sqrt_sum_4', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 10, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@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)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/vx/cvxq4muzevaqey3caqsmo4qq5qc526ua43ko3bs5mugstm6oxtxh.py
# Topologically Sorted Source Nodes: [pow_3, norm_2, add_6, truediv_4, mul_7, sqrt_2, add_7, x_3], Original ATen: [aten.pow, aten.sum, aten.add, aten.div, aten.mul, aten.sqrt]
# Source node to ATen node mapping:
#   add_6 => add_6
#   add_7 => add_7
#   mul_7 => mul_7
#   norm_2 => sum_9
#   pow_3 => pow_3
#   sqrt_2 => sqrt_2
#   truediv_4 => div_7
#   x_3 => div_8
# Graph fragment:
#   %pow_3 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sum_8, 2), kwargs = {})
#   %sum_9 : [num_users=3] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%pow_3, [-1], True), kwargs = {})
#   %add_6 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sum_9, 1), kwargs = {})
#   %div_7 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sum_9, %add_6), kwargs = {})
#   %mul_7 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_7, %sum_8), kwargs = {})
#   %sqrt_2 : [num_users=1] = call_function[target=torch.ops.aten.sqrt.default](args = (%sum_9,), kwargs = {})
#   %add_7 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sqrt_2, 1e-08), kwargs = {})
#   %div_8 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%mul_7, %add_7), kwargs = {})
triton_poi_fused_add_div_mul_pow_sqrt_sum_5 = async_compile.triton('triton_poi_fused_add_div_mul_pow_sqrt_sum_5', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[256], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_add_div_mul_pow_sqrt_sum_5', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 5, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@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)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

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)
        # Topologically Sorted Source Nodes: [u_hat], Original ATen: [aten.clone]
        stream0 = get_raw_stream(0)
        triton_poi_fused_clone_0.run(primals_1, buf0, 1024, grid=grid(1024), stream=stream0)
        del primals_1
        buf1 = empty_strided_cuda((4, 1, 4, 4, 4, 4), (256, 256, 64, 16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [u_hat], Original ATen: [aten.clone]
        triton_poi_fused_clone_1.run(primals_2, buf1, 1024, grid=grid(1024), stream=stream0)
        del primals_2
        buf2 = empty_strided_cuda((64, 4, 4), (16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [u_hat], Original ATen: [aten.bmm]
        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)
        # Topologically Sorted Source Nodes: [b, c, mul, s, pow_1, norm], Original ATen: [aten.zeros_like, aten._softmax, aten.mul, aten.sum, aten.pow]
        triton_poi_fused__softmax_mul_pow_sum_zeros_like_2.run(buf2, buf3, 64, grid=grid(64), stream=stream0)
        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  # reuse
        # Topologically Sorted Source Nodes: [b, c, mul, s, add, truediv, mul_1, sqrt, add_1, x_1, c_1, mul_3, s_1], Original ATen: [aten.zeros_like, aten._softmax, aten.mul, aten.sum, aten.add, aten.div, aten.sqrt]
        triton_poi_fused__softmax_add_div_mul_sqrt_sum_zeros_like_3.run(buf7, buf3, buf2, buf4, 256, grid=grid(256), stream=stream0)
        del buf3
        buf10 = empty_strided_cuda((4, 1, 1, 4, 4, 4), (64, 256, 256, 16, 4, 1), torch.float32)
        buf11 = buf10; del buf10  # reuse
        # Topologically Sorted Source Nodes: [b_1, pow_2, norm_1, add_3, truediv_2, mul_4, sqrt_1, add_4, x_2, uv_1, b_2, c_2, mul_6, s_2], Original ATen: [aten.add, aten.pow, aten.sum, aten.div, aten.mul, aten.sqrt, aten._softmax]
        triton_poi_fused__softmax_add_div_mul_pow_sqrt_sum_4.run(buf11, buf7, buf2, buf4, 256, grid=grid(256), stream=stream0)
        del buf4
        buf12 = reinterpret_tensor(buf7, (4, 1, 1, 4, 4, 4), (64, 64, 64, 16, 4, 1), 0); del buf7  # reuse
        # Topologically Sorted Source Nodes: [pow_3, norm_2, add_6, truediv_4, mul_7, sqrt_2, add_7, x_3], Original ATen: [aten.pow, aten.sum, aten.add, aten.div, aten.mul, aten.sqrt]
        triton_poi_fused_add_div_mul_pow_sqrt_sum_5.run(buf11, buf12, 256, grid=grid(256), stream=stream0)
        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 benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    primals_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    primals_2 = rand_strided((1, 4, 4, 4, 4), (256, 64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([primals_1, primals_2])
    return print_performance(fn, times=times, repeat=repeat)


if __name__ == "__main__":
    from torch._inductor.wrapper_benchmark import compiled_module_main
    compiled_module_main('None', benchmark_compiled_module)

import torch
import torch.nn as nn
import torch.nn.functional as F


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 RoutingCapsules(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(RoutingCapsules, 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, x):
        x.shape[0]
        x = x.unsqueeze(1).unsqueeze(3)
        u_hat = x @ self.W
        b = torch.zeros_like(u_hat)
        for i in range(self.num_iterations - 1):
            """
            Softmax is applied on all of the input capsules, to calculate probs.
            """
            c = F.softmax(b, dim=2)
            s = (c * u_hat).sum(dim=2, keepdim=True)
            v = squash(s)
            uv = u_hat * v
            b = b + uv
        c = F.softmax(b, dim=2)
        s = (c * u_hat).sum(dim=2, keepdim=True)
        v = squash(s)
        v = v.squeeze()
        return v


def get_inputs():
    return [torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {'capsules_num': 4, 'capsules_dim': 4, 'in_capsules_num': 4,
        'in_capsules_dim': 4}]

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]
