Cherry-pick @ibarria0’s fix for KMeans