Skip to content

Commit

Permalink
use numpy's quantile
Browse files Browse the repository at this point in the history
  • Loading branch information
yujiepan-work committed May 22, 2024
1 parent 2b620b5 commit 066c51c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def do_inference(self, model: TModel, dataset: Dataset):
for input_data in track(
dataset.get_inference_data(),
total=dataset.get_length(),
description="Activation sparsifier calibration",
description="Activation Sparsifier Calibration",
):
engine.infer(input_data)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from typing import Dict, List, TypeVar

import numpy as np
import torch
import torch.nn as nn

Expand Down Expand Up @@ -84,7 +85,12 @@ def _calculate_threshold(self, x: torch.Tensor, target_sparsity: float) -> torch
:param target_sparsity: The target sparsity level on the input tensor.
:return: The threshold value.
"""
return x.abs().float().view(-1).quantile(q=target_sparsity, dim=-1)
# uses numpy's quantile implementation as torch's cannot handle large tensor
value = np.quantile(
x.detach().abs().cpu().numpy(),
q=target_sparsity,
)
return torch.tensor(value, device=x.device, dtype=x.dtype)

def _update(self, threshold: torch.Tensor) -> torch.Tensor:
"""
Expand Down

0 comments on commit 066c51c

Please sign in to comment.