Example #1
0
def test_tensor_get_debug_info():
    """ Test the getDebugInfo() method of a popart._internal.ir.Tensor object.
    """
    ir = _ir.Ir()
    g = ir.createGraph("g")
    t = _ir.Tensor("t", _ir.TensorType.ActGrad, g)
    _ = t.getDebugInfo()
Example #2
0
def test_graph_id_member():
    """ Test .id member binding. """
    ir = _ir.Ir()
    g1Id = _ir.GraphId("g1")
    g1 = ir.createGraph(g1Id)

    assert g1.id == g1Id
Example #3
0
def test_tensor_get_batch_axis():
    """ Test the getBatchAxis() method of a popart._internal.ir.Tensor object.
    """
    ir = _ir.Ir()
    g = ir.createGraph("g")
    t = _ir.Tensor("t", _ir.TensorType.ActGrad, g)
    assert t.getBatchAxis() == -1
Example #4
0
def test_tensor_str():
    """ Test the str() method of a popart._internal.ir.Tensor object. """
    ir = _ir.Ir()
    g = ir.createGraph("g")
    Tensor = lambda id: _ir.Tensor(id, _ir.TensorType.ActGrad, g)
    assert Tensor("t0").str() == "t0"
    assert Tensor("t1").str() == "t1"
Example #5
0
def test_check_applying_patterns_level(
        patterns_level: _ir.patterns.PatternsLevel) -> None:
    """Test you can set the patterns object via the PatternsLevelEnum

    Args:
        patterns_level (_ir.patterns.PatternsLevel): The patterns level enum.
    """
    ir = _ir.Ir()
    g1Id = _ir.GraphId("g1")
    _ = ir.createGraph(g1Id)
    g2Id = _ir.GraphId("g2")
    _ = ir.createGraph(g2Id)

    for g in ir.getAllGraphs():
        in0 = add_actgrad_tensor("in0", [1, 2, 3], g)
        in1 = add_random_tensor("in1", _ir.TensorType.Variable, [1, 2, 3], g)
        out0 = add_actgrad_tensor("out0", [1, 2, 3], g)
        ins = {0: in0, 1: in1}
        outs = {0: out0}
        _ = create_new_op(ins, outs, "AddOp", g)

    p = _ir.patterns.Patterns(patterns_level)

    if patterns_level == _ir.patterns.PatternsLevel.NoPatterns:
        p = p.enableRuntimeAsserts(False)

    ir.setPatterns(p)

    for g in ir.getAllGraphs():
        ir.applyPreAliasPatterns(g)
        ir.applyInplacePattern(g)
Example #6
0
def test_ir_graph_management4():
    """ Test we can get the main graph. """
    ir = _ir.Ir()
    a = _ir.GraphId("A")
    b = _ir.GraphId("B")

    mainGraph = ir.getMainGraph()
    assert ir.hasGraph(mainGraph.id)
Example #7
0
def test_tensor_tensor_type0():
    """ Test the tensorType() method of a popart._internal.ir.Tensor object. """
    ir = _ir.Ir()
    g = ir.createGraph("g")
    Tensor = lambda id, type: _ir.Tensor(id, type, g)
    tTypes = [_ir.TensorType.ActGrad, _ir.TensorType.Const]
    for i, tType in enumerate(tTypes):
        assert Tensor(f"t{i}", tType).tensorType() == tType
Example #8
0
def test_graph_get_graph_string():
    """ Test getGraphString binding. """
    ir = _ir.Ir()
    g1Id = _ir.GraphId("g1")
    g1 = ir.createGraph(g1Id)

    assert ir.getMainGraph().getGraphString() == "the main graph"
    assert g1.getGraphString() == "subgraph 'g1'"
Example #9
0
def test_tensor_clone():
    """ Test the clone() method of a popart._internal.ir.Tensor object. """
    ir = _ir.Ir()
    g = ir.createGraph("g")
    t0 = _ir.Tensor("t0", _ir.TensorType.ActGrad, g)
    t1 = t0.clone(g)
    assert f"clone_{t0.str()}" == t1.str()
    assert t0.info == t1.info
Example #10
0
def test_tensor_construction():
    """ Test that we can construct a popart._internal.ir.Tensor object. """
    ir = _ir.Ir()
    g = ir.createGraph("g")
    tId = "t"
    tType = _ir.TensorType.ActGrad
    dc = _ir.DebugContext()
    _ = _ir.Tensor(tId, tType, g)
    _ = _ir.Tensor(tId, tType, g, dc)
Example #11
0
def test_tensor_tensor_type1():
    """ Test the tensor_type() method of a popart._internal.ir.Tensor object.
    """
    ir = _ir.Ir()
    g = ir.createGraph("g")
    Tensor = lambda id, type: _ir.Tensor(id, type, g)
    tTypes = {_ir.TensorType.ActGrad: "ActGrad", _ir.TensorType.Const: "Const"}
    for i, (tType, tTypeStr) in enumerate(tTypes.items()):
        assert Tensor(f"t{i}", tType).tensor_type() == tTypeStr
Example #12
0
def test_tensor_get_ir():
    """ Test the getIr() method of a popart._internal.ir.Tensor object. """
    ir = _ir.Ir()
    g = ir.createGraph("g")
    t = _ir.Tensor("t", _ir.TensorType.ActGrad, g)
    irFromTensor = t.getIr()
    assert g.id == irFromTensor.getAllGraphs()[1].id
    irFromTensor = t.getIr_const()
    assert g.id == irFromTensor.getAllGraphs()[1].id
Example #13
0
def test_tensor_get_graph():
    """ Test the getGraph() method of a popart._internal.ir.Tensor object. """
    ir = _ir.Ir()
    g = ir.createGraph("g")
    t = _ir.Tensor("t", _ir.TensorType.ActGrad, g)
    gFromTensor = t.getGraph()
    assert g.id == gFromTensor.id
    gFromTensor = t.getGraph_const()
    assert g.id == gFromTensor.id
Example #14
0
def test_tensor_has_virtual_graph_id():
    """ Test the hasVirtualGraphId() method of a popart._internal.ir.Tensor
    object.
    """
    ir = _ir.Ir()
    g = ir.createGraph("g")
    t = _ir.Tensor("t", _ir.TensorType.ActGrad, g)
    # TODO(T42205): Test that hasVirtualGraphId() returns the expected values.
    t.hasVirtualGraphId()
Example #15
0
def test_ir_graph_management1():
    """ Test that we can remove graphs. """
    ir = _ir.Ir()
    a = _ir.GraphId("A")

    check_does_not_have_graph(ir, a)
    _ = ir.createGraph(a)
    check_has_graph(ir, a)
    _ = ir.removeGraph(a)
    check_does_not_have_graph(ir, a)
Example #16
0
def test_tensor_get_set_replicated_streaming_mode():
    """ Test the getReplicatedStreamMode() and setReplicatedStreamMode() methods
    of a popart._internal.ir.Tensor object.
    """
    ir = _ir.Ir()
    g = ir.createGraph("g")
    t = _ir.Tensor("t", _ir.TensorType.ActGrad, g)
    assert t.getReplicatedStreamMode() == popart.ReplicatedStreamMode.Replicate
    t.setReplicatedStreamMode(popart.ReplicatedStreamMode.Broadcast)
    assert t.getReplicatedStreamMode() == popart.ReplicatedStreamMode.Broadcast
Example #17
0
def test_tensor_get_virtual_graph_id_unsafe():
    """ Test the getVirtualGraphIdUnsafe() method of a
    popart._internal.ir.Tensor object.
    """
    ir = _ir.Ir()
    g = ir.createGraph("g")
    t = _ir.Tensor("t", _ir.TensorType.ActGrad, g)
    # TODO(T42205): Test that getVirtualGraphIdUnsafe() returns the expected
    # values.
    t.getVirtualGraphIdUnsafe()
Example #18
0
def test_tensor_has_tensor_data():
    """ Test the hasTensorData() method of a popart._internal.ir.Tensor object.
    """
    ir = _ir.Ir()
    g = ir.createGraph("g")
    t = _ir.Tensor("t", _ir.TensorType.ActGrad, g)
    assert t.hasTensorData() == False
    buffer = np.random.rand(2, 3, 4)
    tInfo = _ir.TensorInfo(_ir.DataType.FLOAT, buffer.shape)
    t.setTensorData(tInfo, buffer)
    assert t.hasTensorData() == True
Example #19
0
def test_tensor_get_virtual_graph_id():
    """ Test the getVirtualGraphId() method of a popart._internal.ir.Tensor
    object.
    """
    ir = _ir.Ir()
    g = ir.createGraph("g")
    t = _ir.Tensor("t", _ir.TensorType.ActGrad, g)
    with pytest.raises(popart.popart_exception) as e_info:
        t.getVirtualGraphId()
        assert e_info.value.args[0] == (
            "Invalid call to getVirtualGraphId, Tensor does not have one")
Example #20
0
def test_tensor_set_tensor_type():
    """ Test the setTensorType() method of a popart._internal.ir.Tensor object.
    """
    ir = _ir.Ir()
    g = ir.createGraph("g")
    tTypeOld = _ir.TensorType.ActGrad
    tTypeNew = _ir.TensorType.Const
    t = _ir.Tensor("t", tTypeOld, g)
    assert t.tensorType() == tTypeOld
    t.setTensorType(tTypeNew)
    assert t.tensorType() == tTypeNew
Example #21
0
def test_ir_graph_management3():
    """ Test we can get all graphs. """
    ir = _ir.Ir()
    a = _ir.GraphId("A")
    b = _ir.GraphId("B")

    check_existing_graphs(ir, [''])
    _ = ir.createGraph(a)
    check_existing_graphs(ir, ['', 'A'])
    _ = ir.createGraph(b)
    check_existing_graphs(ir, ['', 'A', 'B'])
Example #22
0
def test_ir_graph_management2():
    """ Test that we can / can't get graphs. """
    ir = _ir.Ir()
    a = _ir.GraphId("A")
    b = _ir.GraphId("B")

    check_cant_get_graph(ir, a)
    check_cant_get_graph(ir, b)
    _ = ir.createGraph(a)
    check_can_get_graph(ir, a)
    check_cant_get_graph(ir, b)
    _ = ir.createGraph(b)
    check_can_get_graph(ir, a)
    check_can_get_graph(ir, b)
Example #23
0
def test_ir_graph_management0():
    """ Test that we can create / test for presence of graphs. """
    ir = _ir.Ir()
    a = _ir.GraphId("A")
    b = _ir.GraphId("B")

    check_does_not_have_graph(ir, a)
    check_does_not_have_graph(ir, b)
    _ = ir.createGraph(a)
    check_has_graph(ir, a)
    check_does_not_have_graph(ir, b)
    _ = ir.createGraph(b)
    check_has_graph(ir, a)
    check_has_graph(ir, b)
Example #24
0
    def __init__(self):
        """Initialises a new `Ir`."""
        self._pb_ir = _ir.Ir()
        # Set better defaults for popart.ir programs.
        # Some parts of graph construction use the session options to make decisions,
        # such as inheritPlacementAttributes and scheduling priorities. So we must set
        # the options when we construct the _internal IR before the user starts constructing
        # the graphs.
        opts = self._pb_ir.getSessionOptions()
        opts.virtualGraphMode = popart.VirtualGraphMode.Manual
        opts.useHostCopyOps = True
        opts.aliasZeroCopy = True
        opts.enableExplicitMainLoops = True
        opts.explicitRecomputation = True

        Ir._ir_cache[self.id] = self
        self._graph_cache: Dict[str, Graph] = {}
Example #25
0
def create_ir(graph_ids: List[str] = []):
    """Small helper function to create an Ir with some graphs

    Args:
        graph_ids (List[str]): List of graph Ids to create

    Returns:
        Ir: An Ir with the required graphs.
    """
    ir = _ir.Ir()
    graphs = [ir.getMainGraph()]
    for name in graph_ids:
        id = _ir.GraphId(name)
        g = _ir.Graph(ir, id)
        graphs.append(g)

    return ir, graphs
Example #26
0
def test_dataflow():
    """Test we can set/get the dataflow for the Ir"""
    ir = _ir.Ir()
    # Check default dataflow
    dataFlow = ir.getDataFlow()
    assert not ir.getDataFlow().isAnchored("out")
    # Can't get art for anchor we haven't anchored.
    with pytest.raises(popart.popart_exception) as e_info:
        assert ir.getDataFlow().art("out").id()
        assert e_info.value.args[0].endswith("is not an anchor")

    dataFlow = popart.DataFlow(1, {"out": popart.AnchorReturnType("All")})
    ir.setDataFlow(dataFlow)
    assert ir.getDataFlow().isAnchored("out")
    assert ir.getDataFlow().art("out").id() == popart.AnchorReturnType(
        "All").id()
    # No equality operator for dataflow, so just check we can get it.
    assert ir.getDataFlow()
Example #27
0
def test_graph_scope_functions():
    """ Test we can scope functions. """
    ir = _ir.Ir()
    g1Id = _ir.GraphId("g1")
    g1 = ir.createGraph(g1Id)

    # Test addScope
    assert _ir.addScope(g1, "tensor1") == "g1/tensor1"
    assert _ir.addScope(g1, "foobar") == "g1/foobar"

    # Test removeScope
    assert _ir.removeScope(g1, "g1/tensor1") == "tensor1"
    assert _ir.removeScope(g1, "g1/foobar") == "foobar"

    with pytest.raises(popart.popart_exception) as excinfo:
        _ir.removeScope(g1, "h1/tensor1")

    # Test getScope
    assert g1.getScope().str() == "g1"
Example #28
0
def test_graph_graph_outputs():
    """ Test we can add/remove graph outputs. """
    ir = _ir.Ir()
    g1Id = _ir.GraphId("g1")
    g1 = ir.createGraph(g1Id)

    # We add inputs as a way of adding tensors to the graph that we can mark as
    # outputs.
    g1.addInput("t0", _ir.TensorInfo(_ir.DataType.FLOAT16, [5, 5]))
    g1.addInput("t1", _ir.TensorInfo(_ir.DataType.FLOAT16, [5, 5]))
    g1.addInput("t2", _ir.TensorInfo(_ir.DataType.FLOAT16, [5, 5]))

    # Check markAsInput.
    check_graph_outputs(g1, [])
    g1.markAsOutput("t0")
    check_graph_outputs(g1, ["t0"])
    g1.markAsOutput(0, "t1", False)
    check_graph_outputs(g1, ["t1", "t0"])
    g1.markAsOutput(0, "t2", True)
    check_graph_outputs(g1, ["t2", "t0"])

    # Check getOutputId.
    assert g1.getOutputId(0) == "t2"
    assert g1.getOutputId(1) == "t0"

    # Check getOutputIndex
    assert g1.getOutputIndex("t2") == 0
    assert g1.getOutputIndex("t0") == 1
    with pytest.raises(popart.popart_exception) as excinfo:
        g1.getOutputIndex("nonExistingTensor")

    # Check hasOutputId.
    assert g1.hasOutputId("t0")
    assert g1.hasOutputId("t2")
    assert not g1.hasOutputId("t1")

    # Check removeInput.
    g1.removeOutput(1)
    check_graph_outputs(g1, ["t2"])
    g1.removeOutput("t2")
    check_graph_outputs(g1, [])
Example #29
0
def test_graph_graph_inputs():
    """ Test we can add/remove graph inputs. """
    ir = _ir.Ir()
    g1Id = _ir.GraphId("g1")
    g1 = ir.createGraph(g1Id)

    # Check initially graph inputs are empty.
    check_graph_inputs(g1, [])

    # Check addInput.
    g1.addInput("inputA", _ir.TensorInfo(_ir.DataType.FLOAT16, [5, 5]))
    check_graph_inputs(g1, ["inputA"])
    g1.addInput("inputB", _ir.TensorInfo(_ir.DataType.FLOAT, [65, 5]))
    check_graph_inputs(g1, ["inputA", "inputB"])
    g1.addInput(1, "input1", _ir.TensorInfo(_ir.DataType.FLOAT, [65, 5]),
                False)
    check_graph_inputs(g1, ["inputA", "input1", "inputB"])
    g1.addInput(1, "input2", _ir.TensorInfo(_ir.DataType.FLOAT, [65, 5]), True)
    check_graph_inputs(g1, ["inputA", "input2", "inputB"])

    # Check getInputId.
    assert g1.getInputId(0) == "inputA"
    assert g1.getInputId(1) == "input2"
    assert g1.getInputId(2) == "inputB"

    # Check getInputIndex
    assert g1.getInputIndex("inputA") == 0
    assert g1.getInputIndex("input2") == 1
    assert g1.getInputIndex("inputB") == 2
    with pytest.raises(popart.popart_exception) as excinfo:
        g1.getInputIndex("nonExistingTensor")

    # Check hasInputId.
    assert g1.hasInputId("inputA")
    assert not g1.hasInputId("input1")

    # Check removeInput.
    g1.removeInput(1)
    check_graph_inputs(g1, ["inputA", "inputB"])
    g1.removeInput("inputA")
    check_graph_inputs(g1, ["inputB"])
Example #30
0
def test_tensor_tensor_data():
    """ Test the tensorData() and setTensorData() methods of a
    popart._internal.ir.Tensor object.
    """
    ir = _ir.Ir()
    g = ir.createGraph("g")
    t = _ir.Tensor("t", _ir.TensorType.ActGrad, g)

    with pytest.raises(popart.popart_exception) as e_info:
        t.tensorData()
        assert e_info.value.args[0] == "Data not set for t"
    with pytest.raises(popart.popart_exception) as e_info:
        t.tensorData_const()
        assert e_info.value.args[0] == "Data not set for t"

    buffer = np.random.rand(2, 3, 4)
    tInfo = _ir.TensorInfo(_ir.DataType.FLOAT, buffer.shape)
    t.setTensorData(tInfo, buffer)

    # TODO(T42205): Test that the returned tensor data matches the one that was
    # set.
    t.tensorData()
    t.tensorData_const()