Skip to content

Commit 6ddb1b4

Browse files
[OpenVINO Backend] update __getitem__
1 parent 771b001 commit 6ddb1b4

File tree

2 files changed

+113
-20
lines changed

2 files changed

+113
-20
lines changed

keras/src/backend/openvino/core.py

Lines changed: 113 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import builtins
12
import contextlib
23
import warnings
34

@@ -308,30 +309,123 @@ def __ne__(self, other):
308309
return OpenVINOKerasTensor(ov_opset.not_equal(first, other).output(0))
309310

310311
def __getitem__(self, indices):
311-
# now it has limited functionaly
312-
# and supports only a case with one integer index in indices
313-
# other indices must be None
314312
data = self.output
315-
axis = []
316-
gather_index = None
317-
if isinstance(indices, int):
313+
rank = len(data.get_partial_shape())
314+
axes, gather_indices_nodes = [], []
315+
slice_axes, slice_starts, slice_ends, slice_steps = [], [], [], []
316+
unsqueeze_axes = []
317+
318+
if not isinstance(indices, tuple):
318319
indices = (indices,)
319-
assert isinstance(indices, tuple), "only tuple is supported"
320+
321+
if any(i is Ellipsis for i in indices):
322+
ellipsis_pos = indices.index(Ellipsis)
323+
num_specified = sum(
324+
i is not Ellipsis and i is not None for i in indices
325+
)
326+
num_missing = rank - num_specified
327+
indices = (
328+
indices[:ellipsis_pos]
329+
+ (builtins.slice(None),) * num_missing
330+
+ indices[ellipsis_pos + 1 :]
331+
)
332+
333+
def count_unsqueeze_before(dim):
334+
return sum(1 for i in range(dim) if indices[i] is None)
335+
336+
partial_shape = data.get_partial_shape()
337+
320338
for dim, index in enumerate(indices):
321-
if isinstance(index, int):
322-
axis.append(dim)
323-
gather_index = ov_opset.constant(index, Type.i32)
339+
if isinstance(index, bool):
340+
raise ValueError(
341+
"OpenVINO backend does not support boolean indexing"
342+
)
343+
elif isinstance(index, int):
344+
actual_dim = dim - count_unsqueeze_before(dim)
345+
if not (0 <= actual_dim < rank):
346+
raise IndexError(
347+
f"""Index {index} is out of bounds for
348+
axis {dim} with rank {rank}"""
349+
)
350+
length = partial_shape[actual_dim].get_length()
351+
idx_value = index if index >= 0 else length + index
352+
axes.append(dim)
353+
gather_indices_nodes.append(
354+
ov_opset.constant([idx_value], Type.i32).output(0)
355+
)
356+
elif isinstance(index, builtins.slice):
357+
if index == builtins.slice(None):
358+
continue
359+
if index.step is not None and index.step < 0:
360+
raise ValueError("OpenVINO doesn't support negative steps")
361+
slice_axes.append(dim)
362+
slice_starts.append(0 if index.start is None else index.start)
363+
slice_ends.append(
364+
2**31 - 1 if index.stop is None else index.stop
365+
)
366+
slice_steps.append(1 if index.step is None else index.step)
367+
elif index is None:
368+
unsqueeze_axes.append(dim)
369+
elif isinstance(index, OpenVINOKerasTensor):
370+
axes.append(dim)
371+
gather_indices_nodes.append(get_ov_output(index))
324372
else:
325-
assert (
326-
index.start is None
327-
and index.stop is None
328-
and index.step is None
373+
raise ValueError(
374+
f"""Unsupported index type {type(index)}
375+
in OpenVINOKerasTensor.__getitem__"""
329376
)
330-
assert len(axis) == 1, "axis must contain one element"
331-
axis = ov_opset.constant(axis, Type.i32)
332-
return OpenVINOKerasTensor(
333-
ov_opset.gather(data, gather_index, axis).output(0)
334-
)
377+
378+
if slice_axes:
379+
step = ov_opset.constant(slice_steps, Type.i32).output(0)
380+
start = ov_opset.constant(slice_starts, Type.i32).output(0)
381+
stop = ov_opset.constant(slice_ends, Type.i32).output(0)
382+
adjusted_slice_axes = [
383+
ax - sum(1 for unsq in unsqueeze_axes if unsq <= ax)
384+
for ax in slice_axes
385+
]
386+
axes_const = ov_opset.constant(
387+
adjusted_slice_axes, Type.i32
388+
).output(0)
389+
data = ov_opset.slice(data, start, stop, step, axes_const).output(0)
390+
391+
if axes:
392+
gather_indices_const = (
393+
gather_indices_nodes[0]
394+
if len(gather_indices_nodes) == 1
395+
else ov_opset.concat(gather_indices_nodes, axis=0).output(0)
396+
)
397+
adjusted_axes = [
398+
ax - sum(1 for unsq in unsqueeze_axes if unsq <= ax)
399+
for ax in axes
400+
]
401+
if len(axes) == 1:
402+
data = ov_opset.gather(
403+
data, gather_indices_const, adjusted_axes[0]
404+
).output(0)
405+
data = ov_opset.squeeze(data, adjusted_axes[0]).output(0)
406+
else:
407+
rank = len(data.get_partial_shape())
408+
remaining_axes = [
409+
i for i in range(rank) if i not in adjusted_axes
410+
]
411+
perm = ov_opset.constant(
412+
adjusted_axes + remaining_axes, Type.i32
413+
)
414+
data = ov_opset.transpose(data, perm).output(0)
415+
data = ov_opset.gather_nd(data, gather_indices_const).output(0)
416+
417+
if unsqueeze_axes:
418+
adjusted_unsqueeze = []
419+
for ax in unsqueeze_axes:
420+
ax -= sum(1 for s in axes if s < ax)
421+
ax -= sum(1 for s in slice_axes if s < ax)
422+
adjusted_unsqueeze.append(ax)
423+
unsqueeze_const = ov_opset.constant(
424+
adjusted_unsqueeze, Type.i32
425+
).output(0)
426+
data = ov_opset.unsqueeze(data, unsqueeze_const).output(0)
427+
428+
return OpenVINOKerasTensor(data)
335429

336430
def __len__(self):
337431
ov_output = self.output

keras/src/backend/openvino/excluded_tests.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
keras/src/activations
22
keras/src/backend/common/dtypes_test.py
3-
keras/src/backend/common/variables_test.py
43
keras/src/callbacks/early_stopping_test.py
54
keras/src/dtype_policies/dtype_policy_map_test.py
65
keras/src/layers/attention

0 commit comments

Comments
 (0)