mirror of
https://github.com/facebookresearch/faiss.git
synced 2025-06-03 21:54:02 +08:00
Summary: Pull Request resolved: https://github.com/facebookresearch/faiss/pull/3422 Found vec_io failing when running some benchmarking. There is no such field named big_endian in sys. So, reverting it to original field byteorder Reviewed By: algoriddle Differential Revision: D56718607 fbshipit-source-id: 553f1d2d6bc967581142a92282e534f3f164e8f9
61 lines
1.3 KiB
Python
61 lines
1.3 KiB
Python
# Copyright (c) Facebook, Inc. and its affiliates.
|
|
#
|
|
# This source code is licensed under the MIT license found in the
|
|
# LICENSE file in the root directory of this source tree.
|
|
|
|
import sys
|
|
import numpy as np
|
|
|
|
"""
|
|
I/O functions in fvecs, bvecs, ivecs formats
|
|
definition of the formats here: http://corpus-texmex.irisa.fr/
|
|
"""
|
|
|
|
|
|
def ivecs_read(fname):
|
|
a = np.fromfile(fname, dtype='int32')
|
|
if sys.byteorder == 'big':
|
|
a.byteswap(inplace=True)
|
|
d = a[0]
|
|
return a.reshape(-1, d + 1)[:, 1:].copy()
|
|
|
|
|
|
def fvecs_read(fname):
|
|
return ivecs_read(fname).view('float32')
|
|
|
|
|
|
def ivecs_mmap(fname):
|
|
assert sys.byteorder != 'big'
|
|
a = np.memmap(fname, dtype='int32', mode='r')
|
|
d = a[0]
|
|
return a.reshape(-1, d + 1)[:, 1:]
|
|
|
|
|
|
def fvecs_mmap(fname):
|
|
return ivecs_mmap(fname).view('float32')
|
|
|
|
|
|
def bvecs_mmap(fname):
|
|
x = np.memmap(fname, dtype='uint8', mode='r')
|
|
if sys.byteorder == 'big':
|
|
da = x[:4][::-1].copy()
|
|
d = da.view('int32')[0]
|
|
else:
|
|
d = x[:4].view('int32')[0]
|
|
return x.reshape(-1, d + 4)[:, 4:]
|
|
|
|
|
|
def ivecs_write(fname, m):
|
|
n, d = m.shape
|
|
m1 = np.empty((n, d + 1), dtype='int32')
|
|
m1[:, 0] = d
|
|
m1[:, 1:] = m
|
|
if sys.byteorder == 'big':
|
|
m1.byteswap(inplace=True)
|
|
m1.tofile(fname)
|
|
|
|
|
|
def fvecs_write(fname, m):
|
|
m = m.astype('float32')
|
|
ivecs_write(fname, m.view('int32'))
|