Utils¶
hyperbench.utils
¶
to_0based_ids(original_ids, ids_to_rebase=None)
¶
Remap IDs to contiguous 0-based indices.
If ids_to_rebase is provided, only IDs present in it are kept and remapped.
If ids_to_rebase is not provided, all unique IDs in original_ids are remapped.
Examples:
>>> to_0based_ids(torch.tensor([1, 3, 3, 7]), torch.tensor([3, 7]))
... -> tensor([0, 0, 1]) # 1 is excluded, 3 -> 0, 7 -> 1
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_ids
|
Tensor
|
Tensor of original IDs. |
required |
ids_to_rebase
|
Tensor | None
|
Optional tensor of IDs to keep and remap. If None, all unique IDs are used. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor of 0-based IDs. |
Source code in hyperbench/utils/data_utils.py
validate_hif_json(filename)
¶
Validate a JSON file against the HIF (Hypergraph Interchange Format) schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to the JSON file to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in hyperbench/utils/hif_utils.py
maxmin_scatter(src, index, dim, dim_size=None)
¶
Performs a scatter reduction that computes the channel-wise range (max - min) for each index group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
Tensor
|
The source tensor containing the values to scatter. |
required |
index
|
Tensor
|
The indices of elements to scatter. |
required |
dim
|
int
|
The axis along which to index. |
required |
dim_size
|
int | None
|
The size of the output tensor along the scatter dimension. If not provided, it will be inferred from the maximum index value. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
A tensor containing the max-min values for each index group. |
Source code in hyperbench/utils/nn_utils.py
sparse_dropout(sparse_tensor, dropout_prob, fill_value=0.0)
¶
Dropout function for sparse matrix.
Returns a new sparse matrix with the same shape as the input sparse matrix, but with some elements dropped out.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sparse_tensor
|
Tensor
|
The sparse matrix with format |
required |
dropout_prob
|
float
|
Probability of an element to be dropped. |
required |
fill_value
|
float
|
The fill value for dropped elements. Defaults to |
0.0
|
Returns:
| Type | Description |
|---|---|
Tensor
|
A new sparse matrix with the same shape as the input sparse matrix, but with some elements dropped out. |
Source code in hyperbench/utils/sparse_utils.py
decompress_zst(zst_path)
¶
Decompresses a .zst file and returns the path to the decompressed JSON file. Args: zst_path: The path to the .zst file to decompress. Returns: The path to the decompressed JSON file.
Source code in hyperbench/utils/file_utils.py
compress_to_zst(json_path)
¶
Compresses a JSON file to .zst format and returns the compressed bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_path
|
str
|
The path to the JSON file to compress. |
required |
Returns: The compressed content as bytes.
Source code in hyperbench/utils/file_utils.py
write_to_disk(dataset_name, content, output_dir=None)
¶
Writes the compressed content to disk in the specified output directory or a default location. Args: dataset_name: The name of the dataset. content: The compressed content as bytes. output_dir: The directory to write the file to. If None, a default location is used.