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 Tensorflow -> jitrt 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 TfConstTest(test.TestCase): 26 27 def test_const_i32(self): 28 mlir_function = """ 29 func.func @test() -> tensor<1xi32> { 30 %0 = "tf.Const"() { 31 value = dense<1> : tensor<1xi32> 32 } : () -> tensor<1xi32> 33 func.return %0 : tensor<1xi32> 34 }""" 35 36 compiled = jitrt.compile(mlir_function, 'test') 37 [res] = jitrt.execute(compiled, []) 38 np.testing.assert_allclose(res, 1, rtol=0.0) 39 40 def test_constant_folding_i32(self): 41 mlir_function = """ 42 func.func @test() -> tensor<2xi32> { 43 %0 = "tf.Const"() {value = dense<0> : tensor<i32>} : () -> tensor<i32> 44 %1 = "tf.Const"() {value = dense<1> : tensor<i32>} : () -> tensor<i32> 45 %2 = "tf.Pack"(%0, %1) {axis = 0 : i64} 46 : (tensor<i32>, tensor<i32>) -> tensor<2xi32> 47 func.return %2 : tensor<2xi32> 48 }""" 49 50 compiled = jitrt.compile(mlir_function, 'test') 51 [res] = jitrt.execute(compiled, []) 52 np.testing.assert_allclose(res, [0, 1], rtol=0.0) 53 54if __name__ == '__main__': 55 test.main() 56