Thanks @lucastononrodrigues  for your quick and efficient feedback.
I tried to use nn.DataParallel. Fortunately, it just worked for a sec and throws another error called:

AttributeError: ‘YOLOLayer’ object has no attribute ‘gridsize’

The function related to this are given below:

def create_grids(self, img_size=416, gridsize=(13, 13), device=‘cpu’, type=torch.float32):
“”" calculate a grid, with the defines gridsize over the input-image
    img_size: (width and height) of the input-image
    gridsize: size of the grid projected over the input-image
    device: cpu or gpu-index where to run the calculation on
    type: type of (float) nvidia of cpu to use on device
"""
nx, ny = gridsize  # x and y grid size
try:
    self.img_size = max(img_size) #take the biggest dimentions out of width or height, to calculate stride
except TypeError: #if only one dimention is given, take that
    self.img_size = int(img_size)
self.stride = self.img_size / max(gridsize)

# build xy offsets
yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)])
self.grid_xy = torch.stack((xv, yv), 2).to(device).type(type).view((1, 1, ny, nx, 2))

# build wh gains
self.anchor_vec = self.anchors.to(device) / self.stride
self.anchor_wh = self.anchor_vec.view(1, self.number_of_anchers, 1, 1, 2).to(device).type(type)
self.gridsize = torch.Tensor(gridsize).to(device)
self.nx = nx
self.ny = ny


This error coming from this lines now:

for yololayer in yolo_layers_list:
    # get number of grid points and anchor vec for this yolo layer
    ng, anchor_vec = yololayer.gridsize, yololayer.anchor_vec


The complete function is given below:

def build_targets(model, targets):
# targets = [image, class, x, y, w, h]
number_of_targets = len(targets)
tcls, tbox, indices, av = [], [], [], []

#get the yolo-layers list and the number_of_classes
multi_gpu = type(model) in (nn.parallel.DataParallel, nn.parallel.DistributedDataParallel) #check if this instance of the model runs distributed
if multi_gpu:
    yolo_layers_list = model.module.Get_YOLO_layers_list() #if model runs distributed than the Get_YOLO_layers_list() function is stored under modules
    number_of_classes = model.module.num_classes #if model runs distributed than the num_classes variable is stored under modules
else:
    yolo_layers_list = model.Get_YOLO_layers_list()
    number_of_classes = model.num_classes

#go over the yolo-detector layers in the model
for yololayer in yolo_layers_list:
    # get number of grid points and anchor vec for this yolo layer
    ng, anchor_vec = yololayer.gridsize, yololayer.anchor_vec

    # iou of targets-anchors
    t, a = targets, []
    gwh = t[:, 4:6] * ng
    if number_of_targets:
        # use all anchors
        iou = torch.stack([wh_iou(x, gwh) for x in anchor_vec], 0)
        number_of_anchors = len(anchor_vec)
        a = torch.arange(number_of_anchors).view((-1, 1)).repeat([1, number_of_targets]).view(-1)
        t = targets.repeat([number_of_anchors, 1])
        gwh = gwh.repeat([number_of_anchors, 1])
        iou = iou.view(-1)  # use all ious

        # reject anchors below iou_thres (OPTIONAL, increases P, lowers R)
        j = iou > model.hyp['iou_t']
        t, a, gwh = t[j], a[j], gwh[j]

    # Indices
    b, c = t[:, :2].long().t()  # target image, class
    gxy = t[:, 2:4] * ng  # grid x, y
    gi, gj = gxy.long().t()  # grid x, y indices
    indices.append((b, a, gj, gi))

    # GIoU
    gxy -= gxy.floor()  # xy
    tbox.append(torch.cat((gxy, gwh), 1))  # xywh (grids)
    av.append(anchor_vec[a])  # anchor vec

    # Class
    tcls.append(c)
    if c.shape[0]:  # if any targets
        assert c.max() <= number_of_classes, 'Target classes exceed model classes'

return tcls, tbox, indices, av