# AOT ID: ['0_inference']
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_7/inductor_cache/cf/ccfaaa5kclaw67tibkvhjvjyrizh4wgzcjfpbivpndxlftfqnq34.py
# Topologically Sorted Source Nodes: [tensor, mul, numerator, pow_1, sum_2, sqrt, denominator, cossim, cossim_1, pow_2, mul_1, loss, loss_1, tensor_1, mul_2, numerator_1, pow_3, sum_4, sqrt_1, denominator_1, cossim_2, cossim_3, pow_4, mul_3, loss_2, loss_3, tensor_2, mul_4, numerator_2, pow_5, sum_6, sqrt_2, denominator_2, cossim_4, cossim_5, pow_6, mul_5, loss_4, loss_5, tensor_3, mul_6, numerator_3, pow_7, sum_8, sqrt_3, denominator_3, cossim_6, cossim_7, pow_8, mul_7, loss_6, loss_7], Original ATen: [aten.lift_fresh, aten.mul, aten.sum, aten.pow, aten.sqrt, aten.add, aten.div, aten.maximum, aten.neg]
# Source node to ATen node mapping:
#   cossim => div
#   cossim_1 => maximum
#   cossim_2 => div_1
#   cossim_3 => maximum_1
#   cossim_4 => div_2
#   cossim_5 => maximum_2
#   cossim_6 => div_3
#   cossim_7 => maximum_3
#   denominator => add
#   denominator_1 => add_2
#   denominator_2 => add_4
#   denominator_3 => add_6
#   loss => neg
#   loss_1 => add_1
#   loss_2 => neg_1
#   loss_3 => add_3
#   loss_4 => neg_2
#   loss_5 => add_5
#   loss_6 => neg_3
#   loss_7 => add_7
#   mul => mul
#   mul_1 => mul_1
#   mul_2 => mul_2
#   mul_3 => mul_3
#   mul_4 => mul_4
#   mul_5 => mul_5
#   mul_6 => mul_6
#   mul_7 => mul_7
#   numerator => sum_1
#   numerator_1 => sum_3
#   numerator_2 => sum_5
#   numerator_3 => sum_7
#   pow_1 => pow_1
#   pow_2 => pow_2
#   pow_3 => pow_3
#   pow_4 => pow_4
#   pow_5 => pow_5
#   pow_6 => pow_6
#   pow_7 => pow_7
#   pow_8 => pow_8
#   sqrt => sqrt
#   sqrt_1 => sqrt_1
#   sqrt_2 => sqrt_2
#   sqrt_3 => sqrt_3
#   sum_2 => sum_2
#   sum_4 => sum_4
#   sum_6 => sum_6
#   sum_8 => sum_8
#   tensor => full_default
#   tensor_1 => full_default_1
#   tensor_2 => full_default_2
#   tensor_3 => full_default_3
# Graph fragment:
#   %full_default : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([], 0.10000000149011612), kwargs = {dtype: torch.float32, layout: torch.strided, device: cpu, pin_memory: False})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%select, %select_1), kwargs = {})
#   %sum_1 : [num_users=2] = call_function[target=torch.ops.aten.sum.default](args = (%mul,), kwargs = {})
#   %pow_1 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%select_1, 2), kwargs = {})
#   %sum_2 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%pow_1,), kwargs = {})
#   %sqrt : [num_users=1] = call_function[target=torch.ops.aten.sqrt.default](args = (%sum_2,), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sqrt, 1e-06), kwargs = {})
#   %div : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sum_1, %add), kwargs = {})
#   %maximum : [num_users=1] = call_function[target=torch.ops.aten.maximum.default](args = (%full_default, %div), kwargs = {})
#   %pow_2 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sum_1, 1.0), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%maximum, %pow_2), kwargs = {})
#   %neg : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%mul_1,), kwargs = {})
#   %add_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%neg, 0.0), kwargs = {})
#   %full_default_1 : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([], 0.10000000149011612), kwargs = {dtype: torch.float32, layout: torch.strided, device: cpu, pin_memory: False})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%select_2, %select_3), kwargs = {})
#   %sum_3 : [num_users=2] = call_function[target=torch.ops.aten.sum.default](args = (%mul_2,), kwargs = {})
#   %pow_3 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%select_3, 2), kwargs = {})
#   %sum_4 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%pow_3,), kwargs = {})
#   %sqrt_1 : [num_users=1] = call_function[target=torch.ops.aten.sqrt.default](args = (%sum_4,), kwargs = {})
#   %add_2 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sqrt_1, 1e-06), kwargs = {})
#   %div_1 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sum_3, %add_2), kwargs = {})
#   %maximum_1 : [num_users=1] = call_function[target=torch.ops.aten.maximum.default](args = (%full_default_1, %div_1), kwargs = {})
#   %pow_4 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sum_3, 1.0), kwargs = {})
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%maximum_1, %pow_4), kwargs = {})
#   %neg_1 : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%mul_3,), kwargs = {})
#   %add_3 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_1, %neg_1), kwargs = {})
#   %full_default_2 : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([], 0.10000000149011612), kwargs = {dtype: torch.float32, layout: torch.strided, device: cpu, pin_memory: False})
#   %mul_4 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%select_4, %select_5), kwargs = {})
#   %sum_5 : [num_users=2] = call_function[target=torch.ops.aten.sum.default](args = (%mul_4,), kwargs = {})
#   %pow_5 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%select_5, 2), kwargs = {})
#   %sum_6 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%pow_5,), kwargs = {})
#   %sqrt_2 : [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_2, 1e-06), kwargs = {})
#   %div_2 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sum_5, %add_4), kwargs = {})
#   %maximum_2 : [num_users=1] = call_function[target=torch.ops.aten.maximum.default](args = (%full_default_2, %div_2), kwargs = {})
#   %pow_6 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sum_5, 1.0), kwargs = {})
#   %mul_5 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%maximum_2, %pow_6), kwargs = {})
#   %neg_2 : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%mul_5,), kwargs = {})
#   %add_5 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_3, %neg_2), kwargs = {})
#   %full_default_3 : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([], 0.10000000149011612), kwargs = {dtype: torch.float32, layout: torch.strided, device: cpu, pin_memory: False})
#   %mul_6 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%select_6, %select_7), kwargs = {})
#   %sum_7 : [num_users=2] = call_function[target=torch.ops.aten.sum.default](args = (%mul_6,), kwargs = {})
#   %pow_7 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%select_7, 2), kwargs = {})
#   %sum_8 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%pow_7,), kwargs = {})
#   %sqrt_3 : [num_users=1] = call_function[target=torch.ops.aten.sqrt.default](args = (%sum_8,), kwargs = {})
#   %add_6 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sqrt_3, 1e-06), kwargs = {})
#   %div_3 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sum_7, %add_6), kwargs = {})
#   %maximum_3 : [num_users=1] = call_function[target=torch.ops.aten.maximum.default](args = (%full_default_3, %div_3), kwargs = {})
#   %pow_8 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sum_7, 1.0), kwargs = {})
#   %mul_7 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%maximum_3, %pow_8), kwargs = {})
#   %neg_3 : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%mul_7,), kwargs = {})
#   %add_7 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_5, %neg_3), kwargs = {})
triton_per_fused_add_div_lift_fresh_maximum_mul_neg_pow_sqrt_sum_0 = async_compile.triton('triton_per_fused_add_div_lift_fresh_maximum_mul_neg_pow_sqrt_sum_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.persistent_reduction(
    size_hints=[1, 64],
    reduction_hint=ReductionHint.INNER,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: 'i32', 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': {3: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 4), equal_to_1=(3,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_add_div_lift_fresh_maximum_mul_neg_pow_sqrt_sum_0', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 8, 'num_reduction': 8, '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}
)
@triton.jit
def triton_per_fused_add_div_lift_fresh_maximum_mul_neg_pow_sqrt_sum_0(in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK : tl.constexpr):
    xnumel = 1
    rnumel = 64
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    roffset = 0
    rmask = tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + (r0), None)
    tmp1 = tl.load(in_ptr1 + (r0), None)
    tmp10 = tl.load(in_ptr0 + (64 + r0), None)
    tmp11 = tl.load(in_ptr1 + (64 + r0), None)
    tmp20 = tl.load(in_ptr0 + (128 + r0), None)
    tmp21 = tl.load(in_ptr1 + (128 + r0), None)
    tmp30 = tl.load(in_ptr0 + (192 + r0), None)
    tmp31 = tl.load(in_ptr1 + (192 + r0), None)
    tmp2 = tmp0 * tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.sum(tmp3, 1)[:, None]
    tmp6 = tmp1 * tmp1
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tmp9 = tl.sum(tmp7, 1)[:, None]
    tmp12 = tmp10 * tmp11
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
    tmp15 = tl.sum(tmp13, 1)[:, None]
    tmp16 = tmp11 * tmp11
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp19 = tl.sum(tmp17, 1)[:, None]
    tmp22 = tmp20 * tmp21
    tmp23 = tl.broadcast_to(tmp22, [XBLOCK, RBLOCK])
    tmp25 = tl.sum(tmp23, 1)[:, None]
    tmp26 = tmp21 * tmp21
    tmp27 = tl.broadcast_to(tmp26, [XBLOCK, RBLOCK])
    tmp29 = tl.sum(tmp27, 1)[:, None]
    tmp32 = tmp30 * tmp31
    tmp33 = tl.broadcast_to(tmp32, [XBLOCK, RBLOCK])
    tmp35 = tl.sum(tmp33, 1)[:, None]
    tmp36 = tmp31 * tmp31
    tmp37 = tl.broadcast_to(tmp36, [XBLOCK, RBLOCK])
    tmp39 = tl.sum(tmp37, 1)[:, None]
    tmp40 = libdevice.sqrt(tmp9)
    tmp41 = 1e-06
    tmp42 = tmp40 + tmp41
    tmp43 = tmp5 / tmp42
    tmp44 = 0.10000000149011612
    tmp45 = triton_helpers.maximum(tmp44, tmp43)
    tmp46 = tmp45 * tmp5
    tmp47 = -tmp46
    tmp48 = 0.0
    tmp49 = tmp47 + tmp48
    tmp50 = libdevice.sqrt(tmp19)
    tmp51 = tmp50 + tmp41
    tmp52 = tmp15 / tmp51
    tmp53 = triton_helpers.maximum(tmp44, tmp52)
    tmp54 = tmp53 * tmp15
    tmp55 = -tmp54
    tmp56 = tmp49 + tmp55
    tmp57 = libdevice.sqrt(tmp29)
    tmp58 = tmp57 + tmp41
    tmp59 = tmp25 / tmp58
    tmp60 = triton_helpers.maximum(tmp44, tmp59)
    tmp61 = tmp60 * tmp25
    tmp62 = -tmp61
    tmp63 = tmp56 + tmp62
    tmp64 = libdevice.sqrt(tmp39)
    tmp65 = tmp64 + tmp41
    tmp66 = tmp35 / tmp65
    tmp67 = triton_helpers.maximum(tmp44, tmp66)
    tmp68 = tmp67 * tmp35
    tmp69 = -tmp68
    tmp70 = tmp63 + tmp69
    tl.debug_barrier()
    tl.store(in_out_ptr0 + (tl.full([XBLOCK, 1], 0, tl.int32)), tmp70, None)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((), (), torch.float32)
        buf8 = buf0; del buf0  # reuse
        # Topologically Sorted Source Nodes: [tensor, mul, numerator, pow_1, sum_2, sqrt, denominator, cossim, cossim_1, pow_2, mul_1, loss, loss_1, tensor_1, mul_2, numerator_1, pow_3, sum_4, sqrt_1, denominator_1, cossim_2, cossim_3, pow_4, mul_3, loss_2, loss_3, tensor_2, mul_4, numerator_2, pow_5, sum_6, sqrt_2, denominator_2, cossim_4, cossim_5, pow_6, mul_5, loss_4, loss_5, tensor_3, mul_6, numerator_3, pow_7, sum_8, sqrt_3, denominator_3, cossim_6, cossim_7, pow_8, mul_7, loss_6, loss_7], Original ATen: [aten.lift_fresh, aten.mul, aten.sum, aten.pow, aten.sqrt, aten.add, aten.div, aten.maximum, aten.neg]
        stream0 = get_raw_stream(0)
        triton_per_fused_add_div_lift_fresh_maximum_mul_neg_pow_sqrt_sum_0.run(buf8, arg0_1, arg1_1, 1, 64, grid=grid(1), stream=stream0)
        del arg0_1
        del arg1_1
    return (buf8, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    arg0_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    arg1_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([arg0_1, arg1_1])
    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 resize_4d_tensor_by_size(x, height, width):
    res = F.interpolate(x, size=(height, width), mode='bilinear')
    return res


class CaricatureLoss(nn.Module):

    def __init__(self, power=1.0):
        super().__init__()
        self.power = power

    def cosine_dissimilarity(self, x, y, eps=1e-06):
        """
        tried my best to replicate: 
            https://github.com/tensorflow/lucid/blob/6dcc927e4ff4e7ef4d9c54d27b0352849dadd1bb/lucid/recipes/caricature.py#L21
            
        if I missed something out, please get in touch with me on Distill slack: @Mayukh 

        or email me: 
            mayukhmainak2000@gmail.com
        or find me on github: 
            github.com/mayukhdeb
        """
        if x.shape != y.shape:
            """
            if their shapes are not equal (likely due to using static caricatures), then resize the target accordingly
            """
            y = resize_4d_tensor_by_size(y.unsqueeze(0), height=x.shape[-2],
                width=x.shape[-1]).squeeze(0)
        y = y.detach()
        numerator = (x * y.detach()).sum()
        denominator = torch.sqrt((y ** 2).sum()) + eps
        cossim = numerator / denominator
        cossim = torch.maximum(torch.tensor(0.1), cossim)
        loss = -(cossim * numerator ** self.power)
        return loss

    def loss(self, x, y):
        loss = self.cosine_dissimilarity(x, y)
        return loss

    def forward(self, layer_outputs, ideal_layer_outputs):
        assert len(layer_outputs) == len(ideal_layer_outputs)
        loss = 0.0
        for i in range(len(layer_outputs)):
            l = self.loss(layer_outputs[i], ideal_layer_outputs[i])
            loss += l
        return loss


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


def get_init_inputs():
    return [[], {}]

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
import torch.nn as nn
import torch.nn.functional as F
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_div_lift_fresh_maximum_mul_neg_pow_sqrt_sum_0(
    in_out_ptr0, in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.load(in_ptr1 + r0, None)
    tmp10 = tl.load(in_ptr0 + (64 + r0), None)
    tmp11 = tl.load(in_ptr1 + (64 + r0), None)
    tmp20 = tl.load(in_ptr0 + (128 + r0), None)
    tmp21 = tl.load(in_ptr1 + (128 + r0), None)
    tmp30 = tl.load(in_ptr0 + (192 + r0), None)
    tmp31 = tl.load(in_ptr1 + (192 + r0), None)
    tmp2 = tmp0 * tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.sum(tmp3, 1)[:, None]
    tmp6 = tmp1 * tmp1
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tmp9 = tl.sum(tmp7, 1)[:, None]
    tmp12 = tmp10 * tmp11
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
    tmp15 = tl.sum(tmp13, 1)[:, None]
    tmp16 = tmp11 * tmp11
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp19 = tl.sum(tmp17, 1)[:, None]
    tmp22 = tmp20 * tmp21
    tmp23 = tl.broadcast_to(tmp22, [XBLOCK, RBLOCK])
    tmp25 = tl.sum(tmp23, 1)[:, None]
    tmp26 = tmp21 * tmp21
    tmp27 = tl.broadcast_to(tmp26, [XBLOCK, RBLOCK])
    tmp29 = tl.sum(tmp27, 1)[:, None]
    tmp32 = tmp30 * tmp31
    tmp33 = tl.broadcast_to(tmp32, [XBLOCK, RBLOCK])
    tmp35 = tl.sum(tmp33, 1)[:, None]
    tmp36 = tmp31 * tmp31
    tmp37 = tl.broadcast_to(tmp36, [XBLOCK, RBLOCK])
    tmp39 = tl.sum(tmp37, 1)[:, None]
    tmp40 = libdevice.sqrt(tmp9)
    tmp41 = 1e-06
    tmp42 = tmp40 + tmp41
    tmp43 = tmp5 / tmp42
    tmp44 = 0.10000000149011612
    tmp45 = triton_helpers.maximum(tmp44, tmp43)
    tmp46 = tmp45 * tmp5
    tmp47 = -tmp46
    tmp48 = 0.0
    tmp49 = tmp47 + tmp48
    tmp50 = libdevice.sqrt(tmp19)
    tmp51 = tmp50 + tmp41
    tmp52 = tmp15 / tmp51
    tmp53 = triton_helpers.maximum(tmp44, tmp52)
    tmp54 = tmp53 * tmp15
    tmp55 = -tmp54
    tmp56 = tmp49 + tmp55
    tmp57 = libdevice.sqrt(tmp29)
    tmp58 = tmp57 + tmp41
    tmp59 = tmp25 / tmp58
    tmp60 = triton_helpers.maximum(tmp44, tmp59)
    tmp61 = tmp60 * tmp25
    tmp62 = -tmp61
    tmp63 = tmp56 + tmp62
    tmp64 = libdevice.sqrt(tmp39)
    tmp65 = tmp64 + tmp41
    tmp66 = tmp35 / tmp65
    tmp67 = triton_helpers.maximum(tmp44, tmp66)
    tmp68 = tmp67 * tmp35
    tmp69 = -tmp68
    tmp70 = tmp63 + tmp69
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp70, None)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((), (), torch.float32)
        buf8 = buf0
        del buf0
        get_raw_stream(0)
        triton_per_fused_add_div_lift_fresh_maximum_mul_neg_pow_sqrt_sum_0[grid
            (1)](buf8, arg0_1, arg1_1, 1, 64, XBLOCK=1, num_warps=2,
            num_stages=1)
        del arg0_1
        del arg1_1
    return buf8,


def resize_4d_tensor_by_size(x, height, width):
    res = F.interpolate(x, size=(height, width), mode='bilinear')
    return res


class CaricatureLossNew(nn.Module):

    def __init__(self, power=1.0):
        super().__init__()
        self.power = power

    def cosine_dissimilarity(self, x, y, eps=1e-06):
        """
        tried my best to replicate: 
            https://github.com/tensorflow/lucid/blob/6dcc927e4ff4e7ef4d9c54d27b0352849dadd1bb/lucid/recipes/caricature.py#L21
            
        if I missed something out, please get in touch with me on Distill slack: @Mayukh 

        or email me: 
            mayukhmainak2000@gmail.com
        or find me on github: 
            github.com/mayukhdeb
        """
        if x.shape != y.shape:
            """
            if their shapes are not equal (likely due to using static caricatures), then resize the target accordingly
            """
            y = resize_4d_tensor_by_size(y.unsqueeze(0), height=x.shape[-2],
                width=x.shape[-1]).squeeze(0)
        y = y.detach()
        numerator = (x * y.detach()).sum()
        denominator = torch.sqrt((y ** 2).sum()) + eps
        cossim = numerator / denominator
        cossim = torch.maximum(torch.tensor(0.1), cossim)
        loss = -(cossim * numerator ** self.power)
        return loss

    def loss(self, x, y):
        loss = self.cosine_dissimilarity(x, y)
        return loss

    def forward(self, input_0, input_1):
        arg0_1 = input_0
        arg1_1 = input_1
        output = call([arg0_1, arg1_1])
        return output[0]
