Skip to content

Commit 91aa315

Browse files
[OpenVINO Backend] update __getitem__
1 parent 771b001 commit 91aa315

File tree

2 files changed

+81
-17
lines changed

2 files changed

+81
-17
lines changed

keras/src/backend/openvino/core.py

Lines changed: 81 additions & 16 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,94 @@ 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+
axes, gather_indices_nodes = [], []
314+
slice_axes, slice_starts, slice_ends, slice_steps = [], [], [], []
315+
unsqueeze_axes, ellipsis_index = [], None
316+
317+
if not isinstance(indices, tuple):
318318
indices = (indices,)
319-
assert isinstance(indices, tuple), "only tuple is supported"
319+
320320
for dim, index in enumerate(indices):
321321
if isinstance(index, int):
322-
axis.append(dim)
323-
gather_index = ov_opset.constant(index, Type.i32)
324-
else:
325-
assert (
322+
axes.append(dim)
323+
index_node = ov_opset.constant([index], Type.i32).output(0)
324+
gather_indices_nodes.append(index_node)
325+
elif isinstance(index, builtins.slice):
326+
if (
326327
index.start is None
327328
and index.stop is None
328329
and index.step is None
330+
):
331+
continue
332+
slice_axes.append(dim)
333+
slice_starts.append(0 if index.start is None else index.start)
334+
slice_ends.append(
335+
2**31 - 1 if index.stop is None else index.stop
329336
)
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-
)
337+
slice_steps.append(1 if index.step is None else index.step)
338+
elif index is Ellipsis:
339+
ellipsis_index = dim
340+
elif index is None:
341+
unsqueeze_axes.append(dim)
342+
elif isinstance(index, OpenVINOKerasTensor):
343+
axes.append(dim)
344+
gather_indices_nodes.append(get_ov_output(index))
345+
else:
346+
raise ValueError(
347+
f"""Unsupported index type {type(index)}
348+
in OpenVINOKerasTensor.__getitem__"""
349+
)
350+
351+
if slice_axes:
352+
step = ov_opset.constant(slice_steps, Type.i32).output(0)
353+
start = ov_opset.constant(slice_starts, Type.i32).output(0)
354+
stop = ov_opset.constant(slice_ends, Type.i32).output(0)
355+
axes_const = ov_opset.constant(slice_axes, Type.i32).output(0)
356+
data = ov_opset.slice(data, start, stop, step, axes_const).output(0)
357+
358+
if axes:
359+
gather_indices_const = (
360+
gather_indices_nodes[0]
361+
if len(gather_indices_nodes) == 1
362+
else ov_opset.concat(gather_indices_nodes, axis=0).output(0)
363+
)
364+
if len(axes) == 1:
365+
data = ov_opset.gather(
366+
data, gather_indices_const, axes[0]
367+
).output(0)
368+
data = ov_opset.squeeze(data, axes[0]).output(0)
369+
else:
370+
adjusted_axes = [
371+
ax - sum(1 for unsq in unsqueeze_axes if unsq <= ax)
372+
for ax in axes
373+
]
374+
rank = len(data.get_partial_shape())
375+
remaining_axes = [
376+
i for i in range(rank) if i not in adjusted_axes
377+
]
378+
perm = ov_opset.constant(
379+
adjusted_axes + remaining_axes, Type.i32
380+
)
381+
data = ov_opset.transpose(data, perm).output(0)
382+
data = ov_opset.gather_nd(data, gather_indices_const).output(0)
383+
384+
if unsqueeze_axes:
385+
expanded_rank = data.get_partial_shape().rank.get_length() + len(
386+
unsqueeze_axes
387+
)
388+
adjusted_unsqueeze = []
389+
for ax in unsqueeze_axes:
390+
ax -= sum(1 for s in axes if s < ax)
391+
if ellipsis_index is not None and ax > ellipsis_index:
392+
ax += expanded_rank - len(indices)
393+
adjusted_unsqueeze.append(ax)
394+
unsqueeze_const = ov_opset.constant(
395+
adjusted_unsqueeze, Type.i32
396+
).output(0)
397+
data = ov_opset.unsqueeze(data, unsqueeze_const).output(0)
398+
399+
return OpenVINOKerasTensor(data)
335400

336401
def __len__(self):
337402
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)