• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15"""Tests for tf.Mean JIT compilation."""
16
17import numpy as np
18
19from tensorflow.compiler.mlir.tfrt.jit.python_binding import tf_jitrt
20from tensorflow.python.platform import test
21
22jitrt = tf_jitrt.TfJitRtExecutor()
23
24
25class TfMeanTest(test.TestCase):
26
27  def test_mean_2d(self):
28    mlir_function = """
29      func.func @mean(%arg0: tensor<?x?xf32>) -> tensor<?x1xf32> {
30        %dim = "tf.Const"() {value = dense<1> : tensor<1xi32>}
31               : () -> tensor<1xi32>
32        %0 = "tf.Mean"(%arg0, %dim) { keep_dims = true }
33             : (tensor<?x?xf32>, tensor<1xi32>) -> tensor<?x1xf32>
34        func.return %0 : tensor<?x1xf32>
35      }"""
36    compiled = jitrt.compile(mlir_function, 'mean')
37    arg0 = np.random.uniform(0.0, 1.0, size=(100, 200)).astype(np.float32)
38    [res] = jitrt.execute(compiled, [arg0])
39    mean = np.mean(arg0, axis=1, keepdims=True)
40    np.testing.assert_allclose(res, mean, rtol=1e-05)
41
42
43if __name__ == '__main__':
44  np.random.seed(0)
45  test.main()
46