Skip to content

Commit

Permalink
[Python API] Support of FP16 blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
akuporos committed Jan 27, 2021
1 parent 344494a commit 0c869a3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ cdef class Blob:
fp64_array_memview = self._array_data
self._ptr = C.make_shared_blob[double](c_tensor_desc, &fp64_array_memview[0], fp64_array_memview.shape[0])
elif precision == "FP16":
raise RuntimeError("Currently, it's impossible to set_blob with FP16 precision")
ar = np.short(self._array_data)
I16_array_memview = ar
#I16_array_memview = self._array_data
self._ptr = C.make_shared_blob[int16_t](c_tensor_desc, &I16_array_memview[0], I16_array_memview.shape[0])
elif precision == "I16":
I16_array_memview = self._array_data
self._ptr = C.make_shared_blob[int16_t](c_tensor_desc, &I16_array_memview[0], I16_array_memview.shape[0])
Expand Down Expand Up @@ -203,10 +206,14 @@ cdef class Blob:
## Blob's memory as numpy.ndarray representation
@property
def buffer(self):
print("zdes")
representation_shape = self._initial_shape if self._initial_shape is not None else []
cdef BlobBuffer buffer = BlobBuffer()
buffer.reset(self._ptr, representation_shape)
return buffer.to_numpy()
print("after_reset")
l = buffer.to_numpy()
print("before return")
return l

## TensorDesc of created Blob
@property
Expand Down Expand Up @@ -1443,6 +1450,7 @@ cdef class BlobBuffer:
"""Copy-less accessor for Inference Engine Blob"""

cdef reset(self, CBlob.Ptr & ptr, vector[size_t] representation_shape = []):
print("reset")
self.ptr = ptr
cdef CTensorDesc desc = deref(ptr).getTensorDesc()
cdef SizeVector shape
Expand All @@ -1466,9 +1474,11 @@ cdef class BlobBuffer:

self.total_stride = total_stride
self.format = self._get_blob_format(desc)
print(self.format)
self.item_size = itemsize

def __getbuffer__(self, Py_buffer *buffer, int flags):
print("get_buf", "flags", flags)
buffer.buf = C.get_buffer[char](deref(self.ptr))
buffer.format = self.format
buffer.internal = NULL
Expand All @@ -1482,6 +1492,7 @@ cdef class BlobBuffer:
buffer.suboffsets = NULL

cdef char*_get_blob_format(self, const CTensorDesc & desc):
print("get_blob_format")
cdef Precision precision = desc.getPrecision()
name = bytes(precision.name()).decode()
# todo: half floats
Expand All @@ -1504,4 +1515,6 @@ cdef class BlobBuffer:
return precision_to_format[name].encode()

def to_numpy(self):
return np.asarray(self)
f = np.asarray(self)
print("to_numpy", f)
return f
2 changes: 1 addition & 1 deletion inference-engine/ie_bridges/python/tests/test_Blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ def test_write_to_buffer_fp64():
assert np.array_equal(blob.buffer, ones_arr)


@pytest.mark.skip(reason="Need to figure out how to implement right conversion")
def test_write_to_buffer_fp16():
tensor_desc = TensorDesc("FP16", [1, 3, 127, 127], "NCHW")
array = np.zeros(shape=(1, 3, 127, 127), dtype=np.float16)
blob = Blob(tensor_desc, array)
ones_arr = np.ones(shape=(1, 3, 127, 127), dtype=np.float16)
blob.buffer[:] = ones_arr
blob.buffer[:] = array
assert np.array_equal(blob.buffer, ones_arr)


Expand Down

0 comments on commit 0c869a3

Please sign in to comment.