Calculate loss in this case?

I have 2 lists of the same size that contain tuples: (item ID, item embedding).

My goal is to calculate the loss (and have it backpropagate to the embeddings) using the Levenshtein distance algorithm based on the ids of the items.

How I can do the above while making it possible for autograd to store the computations in it’s graph?