NVIDIA / NVIDIA/CUDALibrarySamples
Does nvComp support data_type for bfloat16 in python?
Open
@naveenaero is already working on this.
Since Jun 27, 2025.
nvCOMP
- Dominant language
- Cuda
- Stars
- 2.5k
- Forks
- 478
- PR merge metrics
- No merged PRs in 30d
Description
I would like to test compressing bfloat16 in Python. I found in the documentation that the device API supports this data type. However, I couldn't find any information in the documentation on how to compress bfloat16 using the Python API. Could you confirm whether this is supported?
import torch
import nvidia.nvcomp as nvcomp
import time
tensor = torch.rand(1000000, dtype=torch.bfloat16).cuda()
codec = nvcomp.Codec(algorithm="ANS", dtype="<f2")
start_time = time.perf_counter()
tensor_nvcomp = nvcomp.as_array(tensor)
compressed_tensor = codec.encode(tensor_nvcomp)
compression_time = time.perf_counter() - start_time
print(f"原始大小: {tensor.element_size() * tensor.numel()} bytes, 压缩后: {compressed_tensor.buffer_size} bytes")
print(f"压缩用时: {compression_time:.4f} 秒")
start_time = time.perf_counter()
decompressed_tensor_nvcomp = codec.decode(compressed_tensor, data_type="<f2")
decompressed_tensor = torch.as_tensor(decompressed_tensor_nvcomp, dtype=torch.bfloat16, device="cuda")
decompression_time = time.perf_counter() - start_time
print(f"解压用时: {decompression_time:.4f} 秒")
assert torch.allclose(tensor, decompressed_tensor), "false" # it return false to me
When I change torch.bfloat16 to float16, the assert passes successfully.
Additionally, where can I find a complete list of all supported decode data_type values? It took me a long time to discover that float16 corresponds to "<f2"...
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.