Skip to content

Getting model predictions in parallel #280

@oliver-lloyd

Description

@oliver-lloyd

Hi

I have a model from which I am trying to get scores from a large number of triples. To speed up the process I would like to get these scores in parallel, but I am having an issue where the models will only return predictions in serial. When running in parallel with multiprocessing the program runs indefinitely without returning anything. Please see code below.

Any help would be much appreciated!

def score_triples(triple_list, model):
    s = Tensor([edge[0] for edge in triple_list])
    p = Tensor([edge[1]for edge in triple_list])
    o = Tensor([edge[2] for edge in triple_list])
    preds = model.score_spo(s, p, o).tolist()
    triples_scored = pd.DataFrame(triple_list, columns=['head', 'relation', 'tail'])
    triples_scored['score'] = preds
    return triples_scored

# Load model
checkpoint = load_checkpoint('checkpoint.pt')
model = KgeModel.create_from(checkpoint)

# Group triples by relation
test_drugs = [3565, 5022, 8174, 11516, 8361, 4264]
test_rels = [1151, 1223, 1193, 1786]
triple_sets = [
    list(product(
        test_drugs,
        [rel],
        test_drugs
    ))
    for rel in test_rels
]

# Group args for multiprocessing
mp_args = [[trip_set, model] for trip_set in triple_sets]

# Runs fine when predicting for one set of triples
score_triples(*mp_args[0])

# Process runs forever when predicting for several sets in parallel
with mp.Pool(mp.cpu_count()) as pool:
    results = pool.starmap(score_triples, mp_args)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions