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 25# Metadata operations that are noop at runtime, but exist in the Tensorflow 26# graphs purely to facilitate graph construction and transformations. 27class TfMetadataOpsTest(test.TestCase): 28 29 def test_stop_gradient(self): 30 mlir_function = """ 31 func.func @test(%arg0: tensor<?xf32>) -> tensor<?xf32> { 32 %0 = "tf.StopGradient"(%arg0) : (tensor<?xf32>) -> tensor<?xf32> 33 func.return %0 : tensor<?xf32> 34 }""" 35 compiled = jitrt.compile(mlir_function, 'test') 36 arg0 = np.random.uniform(0.0, 1.0, size=(10)).astype(np.float32) 37 [res] = jitrt.execute(compiled, [arg0]) 38 np.testing.assert_allclose(res, arg0, rtol=0.0) 39 40 41if __name__ == '__main__': 42 test.main() 43