# 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/ec/cec4bhugepwpok2snyxl7spiprjcixegjs44whe5hf2qfvmrp3dq.py
# Topologically Sorted Source Nodes: [sign, abs_1, sqrt, epsilon_in, sign_1, abs_2, sqrt_1, epsilon_out, noise, mul_4, weight], Original ATen: [aten.sign, aten.abs, aten.sqrt, aten.mul, aten.add]
# Source node to ATen node mapping:
#   abs_1 => abs_1
#   abs_2 => abs_2
#   epsilon_in => mul
#   epsilon_out => mul_1
#   mul_4 => mul_4
#   noise => mul_2
#   sign => sign
#   sign_1 => sign_1
#   sqrt => sqrt
#   sqrt_1 => sqrt_1
#   weight => add_1
# Graph fragment:
#   %sign : [num_users=1] = call_function[target=torch.ops.aten.sign.default](args = (%randn,), kwargs = {})
#   %abs_1 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%randn,), kwargs = {})
#   %sqrt : [num_users=1] = call_function[target=torch.ops.aten.sqrt.default](args = (%abs_1,), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sign, %sqrt), kwargs = {})
#   %sign_1 : [num_users=1] = call_function[target=torch.ops.aten.sign.default](args = (%randn_1,), kwargs = {})
#   %abs_2 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%randn_1,), kwargs = {})
#   %sqrt_1 : [num_users=1] = call_function[target=torch.ops.aten.sqrt.default](args = (%abs_2,), kwargs = {})
#   %mul_1 : [num_users=2] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sign_1, %sqrt_1), kwargs = {})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul, %mul_1), kwargs = {})
#   %mul_4 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%primals_5, %mul_2), kwargs = {})
#   %add_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%primals_4, %mul_4), kwargs = {})
triton_poi_fused_abs_add_mul_sign_sqrt_0 = async_compile.triton('triton_poi_fused_abs_add_mul_sign_sqrt_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=[16], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: '*fp32', 5: '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, 5), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_abs_add_mul_sign_sqrt_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 4, '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_abs_add_mul_sign_sqrt_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    x1 = (xindex // 4)
    tmp0 = tl.load(in_ptr0 + (x2), xmask)
    tmp1 = tl.load(in_ptr1 + (x2), xmask)
    tmp2 = tl.load(in_ptr2 + (x0), xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr3 + (x1), xmask, eviction_policy='evict_last')
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = tmp3 < tmp2
    tmp5 = tmp4.to(tl.int8)
    tmp6 = tmp2 < tmp3
    tmp7 = tmp6.to(tl.int8)
    tmp8 = tmp5 - tmp7
    tmp9 = tmp8.to(tmp2.dtype)
    tmp10 = tl_math.abs(tmp2)
    tmp11 = libdevice.sqrt(tmp10)
    tmp12 = tmp9 * tmp11
    tmp14 = tmp3 < tmp13
    tmp15 = tmp14.to(tl.int8)
    tmp16 = tmp13 < tmp3
    tmp17 = tmp16.to(tl.int8)
    tmp18 = tmp15 - tmp17
    tmp19 = tmp18.to(tmp13.dtype)
    tmp20 = tl_math.abs(tmp13)
    tmp21 = libdevice.sqrt(tmp20)
    tmp22 = tmp19 * tmp21
    tmp23 = tmp12 * tmp22
    tmp24 = tmp1 * tmp23
    tmp25 = tmp0 + tmp24
    tl.store(out_ptr0 + (x2), tmp25, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/hm/chmyxgj7fuw2oudp5gls2lbglquvq3o5bmmqu7nhowsj6jmhv5s7.py
# Topologically Sorted Source Nodes: [mul_3, bias, add_2], Original ATen: [aten.mul, aten.add]
# Source node to ATen node mapping:
#   add_2 => add_2
#   bias => add
#   mul_3 => mul_3
# Graph fragment:
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%primals_3, %permute), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%primals_2, %mul_3), kwargs = {})
#   %add_2 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%view_1, %add), kwargs = {})
triton_poi_fused_add_mul_1 = async_compile.triton('triton_poi_fused_add_mul_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=[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_add_mul_1', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 4, '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_mul_1(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
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_out_ptr0 + (x2), xmask)
    tmp1 = tl.load(in_ptr0 + (x0), xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr1 + (x0), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + (x0), xmask, eviction_policy='evict_last')
    tmp4 = tl.full([1], 0, tl.int32)
    tmp5 = tmp4 < tmp3
    tmp6 = tmp5.to(tl.int8)
    tmp7 = tmp3 < tmp4
    tmp8 = tmp7.to(tl.int8)
    tmp9 = tmp6 - tmp8
    tmp10 = tmp9.to(tmp3.dtype)
    tmp11 = tl_math.abs(tmp3)
    tmp12 = libdevice.sqrt(tmp11)
    tmp13 = tmp10 * tmp12
    tmp14 = tmp2 * tmp13
    tmp15 = tmp1 + tmp14
    tmp16 = tmp0 + tmp15
    tl.store(in_out_ptr0 + (x2), tmp16, xmask)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, ), (1, ))
    assert_size_stride(primals_3, (4, ), (1, ))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    assert_size_stride(primals_5, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        # Topologically Sorted Source Nodes: [epsilon_input], Original ATen: [aten.randn]
        buf0 = torch.ops.aten.randn.default([1, 4], device=device(type='cuda', index=0), pin_memory=False)
        buf1 = buf0
        del buf0
        # Topologically Sorted Source Nodes: [epsilon_output], Original ATen: [aten.randn]
        buf2 = torch.ops.aten.randn.default([4, 1], device=device(type='cuda', index=0), pin_memory=False)
        buf3 = buf2
        del buf2
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [sign, abs_1, sqrt, epsilon_in, sign_1, abs_2, sqrt_1, epsilon_out, noise, mul_4, weight], Original ATen: [aten.sign, aten.abs, aten.sqrt, aten.mul, aten.add]
        stream0 = get_raw_stream(0)
        triton_poi_fused_abs_add_mul_sign_sqrt_0.run(primals_4, primals_5, buf1, buf3, buf4, 16, grid=grid(16), stream=stream0)
        del primals_4
        del primals_5
        buf5 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [matmul], Original ATen: [aten.mm]
        extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), reinterpret_tensor(buf4, (4, 4), (1, 4), 0), out=buf5)
        del buf4
        buf6 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0); del buf5  # reuse
        # Topologically Sorted Source Nodes: [mul_3, bias, add_2], Original ATen: [aten.mul, aten.add]
        triton_poi_fused_add_mul_1.run(buf6, primals_2, primals_3, buf3, 256, grid=grid(256), stream=stream0)
        del primals_2
        del primals_3
    return (buf6, buf1, buf3, reinterpret_tensor(primals_1, (64, 4), (4, 1), 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((4, ), (1, ), device='cuda:0', dtype=torch.float32)
    primals_3 = rand_strided((4, ), (1, ), device='cuda:0', dtype=torch.float32)
    primals_4 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_5 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([primals_1, primals_2, primals_3, primals_4, primals_5])
    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 math
import torch
import torch.nn as nn
import torch.nn
import torch.optim


class NoisyLinear(nn.Linear):

    def __init__(self, in_dimension, out_dimension, std_dev_init=0.4) ->None:
        """
        Noisy Networks for Exploration: https://arxiv.org/abs/1706.10295
        Standard linear layer: y = wx + b
        Noise linear layer: y = wx + b, where:
            w = u1 + σ1 * ε and b = u2 + σ2 * ε
            ε ~ N(0, std)
        Using factorized Guassian Noise for performance.

        :param in_dimension: Number of in dimensions
        :param out_dimension: Number of out dimensions
        :param std_dev_init: Standard deviation initialization value
        """
        self.in_dimension, self.out_dimension = in_dimension, out_dimension
        super(NoisyLinear, self).__init__(self.in_dimension, self.
            out_dimension, bias=True)
        sigma_init = std_dev_init / math.sqrt(in_dimension)
        self.sigma_weight = nn.Parameter(torch.Tensor(self.out_dimension,
            self.in_dimension).fill_(sigma_init))
        self.sigma_bias = nn.Parameter(torch.Tensor(self.out_dimension).
            fill_(sigma_init))

    def forward(self, input):
        epsilon_input = torch.randn(1, input.size()[1], device=input.device)
        epsilon_output = torch.randn(self.out_dimension - input.size()[1] +
            input.size()[1], 1, device=input.device)
        epsilon_in = torch.sign(epsilon_input) * torch.sqrt(torch.abs(
            epsilon_input))
        epsilon_out = torch.sign(epsilon_output) * torch.sqrt(torch.abs(
            epsilon_output))
        noise = torch.mul(epsilon_in, epsilon_out)
        bias = self.bias + self.sigma_bias * epsilon_out.t()
        weight = self.weight + self.sigma_weight * noise
        return input.matmul(weight.t()) + bias


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


def get_init_inputs():
    return [[], {'in_dimension': 4, 'out_dimension': 4}]

import torch
from torch import device
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
import math
import torch.nn as nn
import torch.nn
import torch.optim
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_abs_add_mul_sign_sqrt_0(in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x2, xmask)
    tmp2 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = tmp3 < tmp2
    tmp5 = tmp4.to(tl.int8)
    tmp6 = tmp2 < tmp3
    tmp7 = tmp6.to(tl.int8)
    tmp8 = tmp5 - tmp7
    tmp9 = tmp8.to(tmp2.dtype)
    tmp10 = tl_math.abs(tmp2)
    tmp11 = libdevice.sqrt(tmp10)
    tmp12 = tmp9 * tmp11
    tmp14 = tmp3 < tmp13
    tmp15 = tmp14.to(tl.int8)
    tmp16 = tmp13 < tmp3
    tmp17 = tmp16.to(tl.int8)
    tmp18 = tmp15 - tmp17
    tmp19 = tmp18.to(tmp13.dtype)
    tmp20 = tl_math.abs(tmp13)
    tmp21 = libdevice.sqrt(tmp20)
    tmp22 = tmp19 * tmp21
    tmp23 = tmp12 * tmp22
    tmp24 = tmp1 * tmp23
    tmp25 = tmp0 + tmp24
    tl.store(out_ptr0 + x2, tmp25, xmask)


@triton.jit
def triton_poi_fused_add_mul_1(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
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp4 = tl.full([1], 0, tl.int32)
    tmp5 = tmp4 < tmp3
    tmp6 = tmp5.to(tl.int8)
    tmp7 = tmp3 < tmp4
    tmp8 = tmp7.to(tl.int8)
    tmp9 = tmp6 - tmp8
    tmp10 = tmp9.to(tmp3.dtype)
    tmp11 = tl_math.abs(tmp3)
    tmp12 = libdevice.sqrt(tmp11)
    tmp13 = tmp10 * tmp12
    tmp14 = tmp2 * tmp13
    tmp15 = tmp1 + tmp14
    tmp16 = tmp0 + tmp15
    tl.store(in_out_ptr0 + x2, tmp16, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (4,), (1,))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    assert_size_stride(primals_5, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = torch.ops.aten.randn.default([1, 4], device=device(type=
            'cuda', index=0), pin_memory=False)
        buf1 = buf0
        del buf0
        buf2 = torch.ops.aten.randn.default([4, 1], device=device(type=
            'cuda', index=0), pin_memory=False)
        buf3 = buf2
        del buf2
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_abs_add_mul_sign_sqrt_0[grid(16)](primals_4,
            primals_5, buf1, buf3, buf4, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        del primals_4
        del primals_5
        buf5 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (64, 4), (4, 1), 0),
            reinterpret_tensor(buf4, (4, 4), (1, 4), 0), out=buf5)
        del buf4
        buf6 = reinterpret_tensor(buf5, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf5
        triton_poi_fused_add_mul_1[grid(256)](buf6, primals_2, primals_3,
            buf3, 256, XBLOCK=128, num_warps=4, num_stages=1)
        del primals_2
        del primals_3
    return buf6, buf1, buf3, reinterpret_tensor(primals_1, (64, 4), (4, 1), 0)


class NoisyLinearNew(nn.Linear):

    def __init__(self, in_dimension, out_dimension, std_dev_init=0.4) ->None:
        """
        Noisy Networks for Exploration: https://arxiv.org/abs/1706.10295
        Standard linear layer: y = wx + b
        Noise linear layer: y = wx + b, where:
            w = u1 + σ1 * ε and b = u2 + σ2 * ε
            ε ~ N(0, std)
        Using factorized Guassian Noise for performance.

        :param in_dimension: Number of in dimensions
        :param out_dimension: Number of out dimensions
        :param std_dev_init: Standard deviation initialization value
        """
        self.in_dimension, self.out_dimension = in_dimension, out_dimension
        super(NoisyLinearNew, self).__init__(self.in_dimension, self.
            out_dimension, bias=True)
        sigma_init = std_dev_init / math.sqrt(in_dimension)
        self.sigma_weight = nn.Parameter(torch.Tensor(self.out_dimension,
            self.in_dimension).fill_(sigma_init))
        self.sigma_bias = nn.Parameter(torch.Tensor(self.out_dimension).
            fill_(sigma_init))

    def forward(self, input_0):
        primals_4 = self.weight
        primals_2 = self.bias
        primals_5 = self.sigma_weight
        primals_3 = self.sigma_bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
        return output[0]
