Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug:inversed_idx do not match the origin index #321

Merged
merged 1 commit into from
Dec 29, 2023
Merged

fix bug:inversed_idx do not match the origin index #321

merged 1 commit into from
Dec 29, 2023

Conversation

pleb631
Copy link
Contributor

@pleb631 pleb631 commented Dec 28, 2023

import torch
def _lamp(imp): # Layer-adaptive Sparsity for the Magnitude-based Pruning
    argsort_idx = torch.argsort(imp, dim=0, descending=True)
    sorted_imp = imp[argsort_idx.tolist()]
   
    inversed_idx1 = torch.argsort(argsort_idx).tolist()
    inversed_idx2 = torch.arange(len(sorted_imp))[
        argsort_idx
    ].tolist()  # [0, 1, 2, 3, ..., ]
    return sorted_imp[inversed_idx1],sorted_imp[inversed_idx2]

a=torch.tensor([3,1,2,4,5])
out1,out2 = _lamp(a)
print(a)
print(out1)
print(out2)

#-------out
#tensor([3, 1, 2, 4, 5])
#tensor([3, 1, 2, 4, 5])
#tensor([1, 2, 5, 3, 4])

Obviously, the index inversed_idx2 cannot restore sorted_imp to imp,
inversed_idx = torch.arange(len(sorted_imp))[argsort_idx] is equivalent to inversed_idx=argsort_idx

@VainF VainF merged commit aeb9bef into VainF:master Dec 29, 2023
@VainF
Copy link
Owner

VainF commented Dec 29, 2023

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants