예제 #1
0
  def testMinimizePeakMemoryList(self):
    mtf_graph = mtf.Graph()
    mesh = mtf.Mesh(mtf_graph, 'my_mesh')
    x = mtf.Constant(mesh, 0,
                     shape=mtf.convert_to_shape('a:3,b:4'),
                     dtype=tf.int32,
                     name='X').outputs[0]
    y = mtf.Constant(mesh, 0,
                     shape=mtf.convert_to_shape('b:4,c:5'),
                     dtype=tf.int32,
                     name='Y').outputs[0]
    mtf.EinsumOperation([x, y], mtf.convert_to_shape('a:3,b:4,c:5'), name='Z')
    w = mtf.EinsumOperation([x, y], mtf.convert_to_shape('a:3,c:5'),
                            name='W').outputs[0]
    mtf.BroadcastOperation(w, mtf.convert_to_shape('a:3,b:4,c:5'), name='V')

    graph = graph_interface.GraphInterface(mtf_graph)
    graph.set_tensor_final('Z:0')
    graph.set_tensor_final('V:0')
    schedule = list(scheduler.minimize_peak_memory(graph, 'LIST'))

    # List Scheduler prefers to schedule things that free the most memory.
    # When nothing is scheduled:
    #   X frees -12 entries.
    #   Y frees -20 entries.
    # After [X] scheduled:
    #   Y frees -20 entries.
    # After [X, Y] scheduled:
    #   Z frees -60 entries.
    #   W frees -15 entries.
    # After [X, Y, W] scheduled:
    #   Z frees -28 entries.
    #   V frees -45 entries.
    # Hence the schedule should be [X, Y, W, Z, V].
    self.assertEqual(schedule, [0, 1, 3, 2, 4])
예제 #2
0
    def testMinimizePeakMemoryList_SingleUseTensor(self):
        mtf_graph = mtf.Graph()
        mesh = mtf.Mesh(mtf_graph, 'my_mesh')
        mtf.Constant(mesh,
                     0,
                     shape=mtf.convert_to_shape('a:4'),
                     dtype=tf.int32,
                     name='X')
        y = mtf.Constant(mesh,
                         0,
                         shape=mtf.convert_to_shape('b:3'),
                         dtype=tf.int32,
                         name='Y').outputs[0]
        mtf.BroadcastOperation(y, mtf.convert_to_shape('b:3,c:2'), name='Z')

        graph = graph_interface.GraphInterface(mtf_graph)
        graph.set_tensor_final('X:0')
        graph.set_tensor_final('Z:0')
        schedule = list(scheduler.minimize_peak_memory(graph, 'LIST'))
        # When nothing is scheduled:
        #   X frees -4 entries
        #   Y frees -3 entries
        # After [Y] scheduled:
        #   X frees -4 entries
        #   Z frees -3 entries
        # Hence the schedule should be [Y, Z, X].
        self.assertEqual(schedule, [1, 2, 0])
    def testMeshTensorFlowGraph(self):
        mtf_graph = mtf.Graph()
        mesh = mtf.Mesh(mtf_graph, "my_mesh")
        x = mtf.Constant(mesh,
                         0,
                         shape=mtf.convert_to_shape("a:3,b:4"),
                         dtype=tf.int32,
                         name="X").outputs[0]
        y = mtf.Constant(mesh,
                         0,
                         shape=mtf.convert_to_shape("b:4,c:5"),
                         dtype=tf.int32,
                         name="Y").outputs[0]
        mtf.EinsumOperation([x, y], mtf.convert_to_shape("a:3,c:5"), name="Z1")
        mtf.EinsumOperation([x, y], mtf.convert_to_shape("a:3,c:5"), name="Z2")

        graph = graph_interface.GraphInterface(mtf_graph)
        self.VerifyGraphInterface(graph)

        self.assertCountEqual(graph.get_operation_mtf_dimension_names("X"),
                              ["a", "b"])
        self.assertCountEqual(graph.get_operation_mtf_dimension_names("Y"),
                              ["b", "c"])
        self.assertCountEqual(graph.get_operation_mtf_dimension_names("Z1"),
                              ["a", "b", "c"])
        self.assertCountEqual(graph.get_operation_mtf_dimension_names("Z2"),
                              ["a", "b", "c"])

        self.assertCountEqual(graph.get_tensor_mtf_dimension_names("X:0"),
                              ["a", "b"])
        self.assertCountEqual(graph.get_tensor_mtf_dimension_names("Y:0"),
                              ["b", "c"])
        self.assertCountEqual(graph.get_tensor_mtf_dimension_names("Z1:0"),
                              ["a", "c"])
        self.assertCountEqual(graph.get_tensor_mtf_dimension_names("Z1:0"),
                              ["a", "c"])

        self.assertIsNone(graph.get_tensor_device("X:0"))
        self.assertIsNone(graph.get_tensor_device("Y:0"))
        self.assertIsNone(graph.get_tensor_device("Z1:0"))
        self.assertIsNone(graph.get_tensor_device("Z2:0"))

        self.assertTrue(graph.is_tensor_on_canonical_device("X:0"))
        self.assertTrue(graph.is_tensor_on_canonical_device("Y:0"))
        self.assertTrue(graph.is_tensor_on_canonical_device("Z1:0"))
        self.assertTrue(graph.is_tensor_on_canonical_device("Z2:0"))

        self.assertEqual(graph.compute_cost_graph().SerializeToString(),
                         self._deviceless_cost_graph_string)
        self.assertEqual(
            graph.compute_cost_graph(devices=[]).SerializeToString(),
            self._deviceless_cost_graph_string)
    def testTensorFlowGraph(self):
        tf_graph = tf.Graph()
        with tf_graph.as_default():
            with tf.device("/device:CPU:0"):
                x = tf.zeros([3, 4], dtype=tf.int32, name="X")
                y = tf.zeros([4, 5], dtype=tf.int32, name="Y")
                tf.matmul(x, y, name="Z1")
                tf.matmul(x, y, name="Z2")

        graph = graph_interface.GraphInterface(
            tf_graph, canonical_device="/device:CPU:0")
        self.VerifyGraphInterface(graph)

        self.assertCountEqual(graph.get_operation_mtf_dimension_names("X"), [])
        self.assertCountEqual(graph.get_operation_mtf_dimension_names("Y"), [])
        self.assertCountEqual(graph.get_operation_mtf_dimension_names("Z1"),
                              [])
        self.assertCountEqual(graph.get_operation_mtf_dimension_names("Z2"),
                              [])

        self.assertCountEqual(graph.get_tensor_mtf_dimension_names("X:0"), [])
        self.assertCountEqual(graph.get_tensor_mtf_dimension_names("Y:0"), [])
        self.assertCountEqual(graph.get_tensor_mtf_dimension_names("Z1:0"), [])
        self.assertCountEqual(graph.get_tensor_mtf_dimension_names("Z2:0"), [])

        self.assertEqual(graph.get_tensor_device("X:0"), "/device:CPU:0")
        self.assertEqual(graph.get_tensor_device("Y:0"), "/device:CPU:0")
        self.assertEqual(graph.get_tensor_device("Z1:0"), "/device:CPU:0")
        self.assertEqual(graph.get_tensor_device("Z2:0"), "/device:CPU:0")

        self.assertTrue(graph.is_tensor_on_canonical_device("X:0"))
        self.assertTrue(graph.is_tensor_on_canonical_device("Y:0"))
        self.assertTrue(graph.is_tensor_on_canonical_device("Z1:0"))
        self.assertTrue(graph.is_tensor_on_canonical_device("Z2:0"))

        self.assertEqual(graph.compute_cost_graph().SerializeToString(),
                         self._cost_graph_string)
        self.assertEqual(
            graph.compute_cost_graph(
                devices=["/device:CPU:0"]).SerializeToString(),
            self._cost_graph_string)
        self.assertEqual(
            graph.compute_cost_graph(devices=[]).SerializeToString(),
            self._sizeless_cost_graph_string)
예제 #5
0
  def testReturnsTopoSort(self, scheduler_alg):
    mtf_graph = mtf.Graph()
    mesh = mtf.Mesh(mtf_graph, 'my_mesh')
    x = mtf.Constant(mesh, 0,
                     shape=mtf.convert_to_shape('a:3,b:4'),
                     dtype=tf.int32,
                     name='X').outputs[0]
    y = mtf.Constant(mesh, 0,
                     shape=mtf.convert_to_shape('b:4,c:5'),
                     dtype=tf.int32,
                     name='Y').outputs[0]
    mtf.EinsumOperation([x, y], mtf.convert_to_shape('a:3,c:5'), name='Z1')
    mtf.EinsumOperation([x, y], mtf.convert_to_shape('a:3,c:5'), name='Z2')

    graph = graph_interface.GraphInterface(mtf_graph)
    graph.set_tensor_final('Z1:0')
    graph.set_tensor_final('Z2:0')
    schedule = list(scheduler.minimize_peak_memory(graph, scheduler_alg))

    self.assertCountEqual(schedule[0:2], [0, 1])
    self.assertCountEqual(schedule[2:4], [2, 3])
예제 #6
0
 def _compute_graph_interface(self):
     """Computes self._graph_interface."""
     self._graph_interface = graph_interface.GraphInterface(self.mtf_graph)
     for mtf_output in self.mtf_outputs:
         self._graph_interface.set_tensor_final(mtf_output.name)