import torch


class Homoscedastic(torch.nn.Module):
    """https://arxiv.homoscedasticorg/abs/1705.07115"""

    def __init__(self, n_tasks, reduction='sum'):
        super(Homoscedastic, self).__init__()
        self.n_tasks = n_tasks
        self.log_vars = torch.nn.Parameter(torch.zeros(self.n_tasks))
        self.reduction = reduction

    def forward(self, losses):
        device = losses.device
        stds = (torch.exp(self.log_vars) ** (1 / 2)).to(device)
        coeffs = 1 / stds ** 2
        multi_task_losses = coeffs * losses + torch.log(stds)
        if self.reduction == 'sum':
            multi_task_losses = multi_task_losses.sum()
        if self.reduction == 'mean':
            multi_task_losses = multi_task_losses.mean()
        return multi_task_losses


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


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

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_per_fused_add_exp_log_mul_pow_reciprocal_sum_0(in_ptr0, in_ptr1,
    out_ptr0, xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r0 = rindex % 4
    r2 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + r2, None)
    tmp1 = tl_math.exp(tmp0)
    tmp2 = libdevice.sqrt(tmp1)
    tmp3 = tmp2 * tmp2
    tmp4 = tl.full([1], 1, tl.int32)
    tmp5 = tmp4 / tmp3
    tmp6 = 1.0
    tmp7 = tmp5 * tmp6
    tmp9 = tmp7 * tmp8
    tmp10 = tl_math.log(tmp2)
    tmp11 = tmp9 + tmp10
    tmp12 = tl.broadcast_to(tmp11, [RBLOCK])
    tmp14 = triton_helpers.promote_to_tensor(tl.sum(tmp12, 0))
    tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp14, None)


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, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((), (), torch.float32)
        get_raw_stream(0)
        triton_per_fused_add_exp_log_mul_pow_reciprocal_sum_0[grid(1)](
            primals_2, primals_1, buf0, 1, 256, num_warps=2, num_stages=1)
    return buf0, primals_1, primals_2


class HomoscedasticNew(torch.nn.Module):
    """https://arxiv.homoscedasticorg/abs/1705.07115"""

    def __init__(self, n_tasks, reduction='sum'):
        super(HomoscedasticNew, self).__init__()
        self.n_tasks = n_tasks
        self.log_vars = torch.nn.Parameter(torch.zeros(self.n_tasks))
        self.reduction = reduction

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