def test_can_reset_shape_after_decreasing_on_preallocated_memory(): ones_arr = np.ones(shape=(1, 3, 32, 32), dtype=np.float32) ones_arr = np.ascontiguousarray(ones_arr) ov_tensor = Tensor(ones_arr, shared_memory=True) ref_shape_1 = [1, 3, 24, 24] ref_shape_2 = [1, 3, 32, 32] assert np.shares_memory(ones_arr, ov_tensor.data) ov_tensor.shape = ref_shape_1 assert list(ov_tensor.shape) == ref_shape_1 ov_tensor.shape = ref_shape_2 assert list(ov_tensor.shape) == ref_shape_2
def test_set_shape(ov_type, numpy_dtype): shape = ov.Shape([1, 3, 32, 32]) ref_shape = ov.Shape([1, 3, 48, 48]) ref_shape_np = [1, 3, 28, 28] ov_tensor = Tensor(ov_type, shape) ov_tensor.shape = ref_shape assert list(ov_tensor.shape) == list(ref_shape) ones_arr = np.ones(list(ov_tensor.shape), numpy_dtype) ov_tensor.data[:] = ones_arr assert np.array_equal(ov_tensor.data, ones_arr) ov_tensor.shape = ref_shape_np assert list(ov_tensor.shape) == ref_shape_np zeros = np.zeros(ref_shape_np, numpy_dtype) ov_tensor.data[:] = zeros assert np.array_equal(ov_tensor.data, zeros)
def test_can_set_smaller_or_same_shape_on_preallocated_memory(ref_shape): ones_arr = np.ones(shape=(1, 3, 32, 32), dtype=np.float32) ones_arr = np.ascontiguousarray(ones_arr) ov_tensor = Tensor(ones_arr, shared_memory=True) assert np.shares_memory(ones_arr, ov_tensor.data) ov_tensor.shape = ref_shape assert list(ov_tensor.shape) == ref_shape
def test_cannot_set_shape_on_preallocated_memory(ref_shape): ones_arr = np.ones(shape=(1, 3, 32, 32), dtype=np.float32) ones_arr = np.ascontiguousarray(ones_arr) ov_tensor = Tensor(ones_arr, shared_memory=True) assert np.shares_memory(ones_arr, ov_tensor.data) with pytest.raises(RuntimeError) as e: ov_tensor.shape = ref_shape assert "Cannot call setShape for Blobs created on top of preallocated memory" in str(e.value)
def test_cannot_set_shape_incorrect_dims(): ov_tensor = Tensor(np.float32, [1, 3, 48, 48]) with pytest.raises(RuntimeError) as e: ov_tensor.shape = [3, 28, 28] assert "Dims and format are inconsistent" in str(e.value)