forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tf_Multinomial.py
142 lines (132 loc) · 4.91 KB
/
test_tf_Multinomial.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Copyright (C) 2018-2025 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import pytest
import tensorflow as tf
from common.tf_layer_test_class import CommonTFLayerTest
import numpy as np
class TestMultinomial(CommonTFLayerTest):
def _prepare_input(self, inputs_dict, kwargs):
inputs_dict["num_samples:0"] = np.array(kwargs["num_samples:0"], dtype=np.int32)
inputs_dict["probs:0"] = kwargs["input:0"]
return inputs_dict
def create_tf_multinomial_net_shape(
self, global_seed, op_seed, logits_shape, input_type, out_type
):
tf.compat.v1.reset_default_graph()
# Configuration required to make multinomial randomness predictable across devices, results depends on TF parallel execution.
session_conf = tf.compat.v1.ConfigProto(
intra_op_parallelism_threads=1, inter_op_parallelism_threads=1
)
# Create the graph and model
with tf.compat.v1.Session(config=session_conf) as sess:
probs = tf.compat.v1.placeholder(input_type, logits_shape, "probs")
num_samples = tf.compat.v1.placeholder(tf.int32, [], "num_samples")
if global_seed is not None:
tf.random.set_seed(global_seed)
tf.raw_ops.ZerosLike(
x=tf.raw_ops.Multinomial(
logits=tf.math.log(probs),
num_samples=num_samples,
seed=global_seed,
seed2=op_seed,
output_dtype=out_type,
)
)
tf.compat.v1.global_variables_initializer()
tf_net = sess.graph_def
return tf_net, None
def create_tf_multinomial_net_exact(
self, global_seed, op_seed, logits_shape, input_type, out_type
):
tf.compat.v1.reset_default_graph()
# Configuration required to make multinomial randomness predictable across devices, results depends on TF parallel execution.
session_conf = tf.compat.v1.ConfigProto(
intra_op_parallelism_threads=1, inter_op_parallelism_threads=1
)
# Create the graph and model
with tf.compat.v1.Session(config=session_conf) as sess:
probs = tf.compat.v1.placeholder(input_type, logits_shape, "probs")
num_samples = tf.compat.v1.placeholder(tf.int32, [], "num_samples")
if global_seed is not None:
tf.random.set_seed(global_seed)
tf.raw_ops.Multinomial(
logits=tf.math.log(probs),
num_samples=num_samples,
seed=global_seed,
seed2=op_seed,
output_dtype=out_type,
)
tf.compat.v1.global_variables_initializer()
tf_net = sess.graph_def
return tf_net, None
@pytest.mark.parametrize("out_type", [tf.int32, tf.int64])
@pytest.mark.parametrize(
("input", "num_samples", "seed", "test_type"),
[
(
np.array([[0, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0]], dtype=np.float32),
1024,
[32465, 48971],
"exact",
),
(
np.array(
[
[0.001, 0.001, 0.1, 0.9],
[5, 10, 1e-5, 256],
[1, 1e-5, 1e-5, 1e-5],
],
dtype=np.float64,
),
256,
[32465, 48971],
"shape",
),
(np.array([[1, 1, 1, 1]], dtype=np.float16), 1024, [1, 1], "shape"),
(
np.array([[1, 2, 3, 4], [4, 3, 2, 1], [1, 0, 0, 0]], dtype=np.float32),
1,
[78132, None],
"shape",
),
(
np.array([[7, 7, 7, 7], [7, 7, 7, 7], [7, 7, 7, 7]], dtype=np.float32),
1024,
[32465, None],
"shape",
),
],
)
@pytest.mark.nightly
@pytest.mark.precommit
def test_multinomial_basic(
self,
input,
num_samples,
seed,
out_type,
test_type,
ie_device,
precision,
ir_version,
temp_dir,
use_legacy_frontend,
):
if ie_device == "GPU":
pytest.skip("Multinomial is not supported on GPU")
net = getattr(self, f"create_tf_multinomial_net_{test_type}")
self._test(
*net(
global_seed=seed[0],
op_seed=seed[1],
logits_shape=input.shape,
input_type=input.dtype,
out_type=out_type,
),
ie_device,
precision,
temp_dir=temp_dir,
ir_version=ir_version,
use_legacy_frontend=use_legacy_frontend,
kwargs_to_prepare_input={"input:0": input, "num_samples:0": num_samples},
)