Skip to content

Commit b54dace

Browse files
authored
DEV: add BFLOAT16 and FLOAT16 vector examples (#1067)
1 parent 903735e commit b54dace

File tree

1 file changed

+27
-0
lines changed
  • content/develop/interact/search-and-query/advanced-concepts

1 file changed

+27
-0
lines changed

content/develop/interact/search-and-query/advanced-concepts/vectors.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,33 @@ FT.SEARCH products "(@type:{shirt} @year:[2020 2022]) | @description_vector:[VEC
483483

484484
To explore additional Python vector search examples, review recipes for the [`Redis Python`](https://github.com/redis-developer/redis-ai-resources/blob/main/python-recipes/vector-search/00_redispy.ipynb) client library and the [`Redis Vector Library`](https://github.com/redis-developer/redis-ai-resources/blob/main/python-recipes/vector-search/01_redisvl.ipynb).
485485

486+
## Memory consumption comparison
487+
488+
Following is a Python+NumPy example of vector sizes for the supported vector types; `BFLOAT16`, `FLOAT16`, `FLOAT32`, and `FLOAT64`.
489+
490+
```python
491+
import numpy as np
492+
493+
#install ml_dtypes from pip install ml-dtypes
494+
from ml_dtypes import bfloat16
495+
496+
# random float64 100 dimensions
497+
double_precision_vec = np.random.rand(100)
498+
499+
# for float64 and float32
500+
print(f'length of float64 vector: {len(double_precision_vec.tobytes())}') # >>> 800
501+
print(f'length of float32 vector: {len(double_precision_vec.astype(np.float32).tobytes())}') # >>> 400
502+
503+
# for float16
504+
np_data_type = np.float16
505+
half_precision_vec_float16 = double_precision_vec.astype(np_data_type)
506+
print(f'length of float16 vector: {len(half_precision_vec_float16.tobytes())}') # >>> 200
507+
508+
# for bfloat16
509+
bfloat_dtype = bfloat16
510+
half_precision_vec_bfloat16 = double_precision_vec.astype(bfloat_dtype)
511+
print(f'length of bfloat16 vector: {len(half_precision_vec_bfloat16.tobytes())}') # >>> 200
512+
```
486513

487514
## Next steps
488515

0 commit comments

Comments
 (0)