|
6 | 6 | from keras.src.backend.openvino.core import OPENVINO_DTYPES
|
7 | 7 | from keras.src.backend.openvino.core import OpenVINOKerasTensor
|
8 | 8 | from keras.src.backend.openvino.core import convert_to_numpy
|
| 9 | +from keras.src.backend.openvino.core import get_ov_output |
9 | 10 | from keras.src.random.seed_generator import SeedGenerator
|
10 | 11 | from keras.src.random.seed_generator import draw_seed
|
11 | 12 | from keras.src.random.seed_generator import make_default_seed
|
@@ -39,9 +40,61 @@ def uniform(shape, minval=0.0, maxval=1.0, dtype=None, seed=None):
|
39 | 40 |
|
40 | 41 |
|
41 | 42 | def categorical(logits, num_samples, dtype="int64", seed=None):
|
42 |
| - raise NotImplementedError( |
43 |
| - "`categorical` is not supported with openvino backend" |
44 |
| - ) |
| 43 | + dtype = dtype or "int64" |
| 44 | + ov_dtype = OPENVINO_DTYPES[dtype] |
| 45 | + logits = get_ov_output(logits) |
| 46 | + |
| 47 | + zero_const = ov_opset.constant(0, Type.i32).output(0) |
| 48 | + one_const = ov_opset.constant(1, Type.i32).output(0) |
| 49 | + neg_one_const = ov_opset.constant(-1, Type.i32).output(0) |
| 50 | + |
| 51 | + # Compute probabilities and cumulative sum |
| 52 | + probs = ov_opset.softmax(logits, axis=-1).output(0) |
| 53 | + cumsum_probs = ov_opset.cumsum(probs, neg_one_const).output(0) |
| 54 | + |
| 55 | + # Get shape and compute batch dimensions efficiently |
| 56 | + logits_shape = ov_opset.shape_of(logits, Type.i32).output(0) |
| 57 | + rank = ov_opset.shape_of(logits_shape, Type.i32).output(0) |
| 58 | + rank_scalar = ov_opset.squeeze(rank, zero_const).output(0) |
| 59 | + rank_minus_1 = ov_opset.subtract(rank_scalar, one_const).output(0) |
| 60 | + |
| 61 | + # Extract batch shape (all dimensions except last) |
| 62 | + batch_indices = ov_opset.range( |
| 63 | + zero_const, rank_minus_1, one_const, output_type=Type.i32 |
| 64 | + ).output(0) |
| 65 | + batch_shape = ov_opset.gather(logits_shape, batch_indices, axis=0).output(0) |
| 66 | + |
| 67 | + # Create final shape [batch_dims..., num_samples] |
| 68 | + num_samples_const = ov_opset.constant([num_samples], Type.i32).output(0) |
| 69 | + final_shape = ov_opset.concat( |
| 70 | + [batch_shape, num_samples_const], axis=0 |
| 71 | + ).output(0) |
| 72 | + |
| 73 | + seed_tensor = draw_seed(seed) |
| 74 | + if isinstance(seed_tensor, OpenVINOKerasTensor): |
| 75 | + seed1, seed2 = convert_to_numpy(seed_tensor) |
| 76 | + else: |
| 77 | + seed1, seed2 = seed_tensor.data |
| 78 | + |
| 79 | + probs_dtype = probs.get_element_type() |
| 80 | + zero_float = ov_opset.constant(0.0, probs_dtype).output(0) |
| 81 | + one_float = ov_opset.constant(1.0, probs_dtype).output(0) |
| 82 | + |
| 83 | + rand = ov_opset.random_uniform( |
| 84 | + final_shape, zero_float, one_float, probs_dtype, seed1, seed2 |
| 85 | + ).output(0) |
| 86 | + |
| 87 | + rand_unsqueezed = ov_opset.unsqueeze(rand, neg_one_const).output(0) |
| 88 | + cumsum_unsqueezed = ov_opset.unsqueeze(cumsum_probs, one_const).output(0) |
| 89 | + |
| 90 | + # Count how many cumulative probabilities each random number exceeds |
| 91 | + greater = ov_opset.greater(rand_unsqueezed, cumsum_unsqueezed).output(0) |
| 92 | + samples = ov_opset.reduce_sum( |
| 93 | + ov_opset.convert(greater, Type.i32).output(0), neg_one_const |
| 94 | + ).output(0) |
| 95 | + |
| 96 | + result = ov_opset.convert(samples, ov_dtype).output(0) |
| 97 | + return OpenVINOKerasTensor(result) |
45 | 98 |
|
46 | 99 |
|
47 | 100 | def randint(shape, minval, maxval, dtype="int32", seed=None):
|
|
0 commit comments