Assuming that your inputs are 2D, the code below shows how to do it
 import torch

def loss_function(p, y, b):
    eps = 0.0000000001
    losses = 0
    k = len(y[0])
    ones = torch.ones(k)
    for i in range(b):
        loss1 = -((ones-y[i])@(((ones-p[i])+(eps)).log()))
        prod = (ones-y[i])*k - y[i]*((p[i]+ eps).log())
        loss2 = torch.min(prod)
        losses = losses + (loss2 + loss1)
    return (losses/b)

def new_loss_function(p, y, b):
    eps = 0.0000000001
    losses = 0
    k = len(y[0])
    ones = torch.ones(1, 1).expand(b, k)
    loss1 = -((ones-y)*(((ones-p)+(eps)).log())).sum(dim=1)
    prod = (ones-y)*k - y*((p+ eps).log())
    loss2 = torch.min(prod, dim=1)[0]
    losses = (loss1 + loss2).sum()
    return losses / b

b = 10
k = 5

# Are there the right input size?
p = torch.rand(b, k)
y = torch.rand(b, k)

old_res = loss_function(p, y, b)
new_res = new_loss_function(p, y, b)

print((old_res - new_res).abs().max())