Get the difference between the common dimension of K and T
d = T.unsqueeze(2) - K.unsqueeze(1)
this will be of size (1,3,7,4).  Where the rows were identical, we would have 0,0,0,0. So sum together the last dimension: dsum = d.sum(-1)
Now find out where dsum has zeros:
loc = (dsum==0).nonzero()
since all 3 rows were found somewhere in K, this will have size 3,3, if only 2 rows were found this would have a shape (2,3). You are interested in the locations inside K, so you need loc[:,-1]
Assuming D is same size as K to take out the relevant rows you’d do:
D[:,loc[:,-1],:]