1# Copyright 2020 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 Grappler Arithmetic Optimizer.""" 16 17from tensorflow.python.eager import context 18from tensorflow.python.eager import def_function 19from tensorflow.python.ops import array_ops 20from tensorflow.python.ops import math_ops 21from tensorflow.python.platform import test 22 23 24class ArithmeticOptimizerTest(test.TestCase): 25 26 # See b/146524878. 27 def testFunctionArgShapeInference(self): 28 29 @def_function.function 30 def f(x, y): 31 return math_ops.matmul( 32 x, array_ops.reshape(array_ops.transpose(y), [384, 1536])) 33 34 with context.eager_mode(): 35 x = array_ops.ones((1, 384)) 36 y = array_ops.ones((1536, 384)) 37 with context.collect_graphs(optimized=True) as graphs: 38 f(x, y).numpy() 39 self.assertLen(graphs, 1) 40 self.assertLen(graphs[0].node, 4) 41 self.assertEqual(graphs[0].node[2].name, 42 'ArithmeticOptimizer/FoldTransposeIntoMatMul_MatMul') 43 44 45if __name__ == '__main__': 46 test.main() 47