Ejemplo n.º 1
0
def _test_loop():
    x = ops.Const(5)
    y = ops.Const(0)
    with ops.loop():
        ops.stop_if(ops.EQ([x, ops.Const(0)]))
        ops.Add([x, ops.Const(-1)], [x])
        ops.Add([y, ops.Const(1)], [y])
    return y
Ejemplo n.º 2
0
 def test_multiple_definition(self):
     job = example_job()
     with job:
         with Task():
             ops.Add([ops.Const(0), ops.Const(1)], 'out1')
         with Task():
             ops.Add([ops.Const(2), ops.Const(3)], 'out1')
     with self.assertRaises(AssertionError):
         net_printer.analyze(job)
Ejemplo n.º 3
0
    def test_create_plan_from_proto_correctly(self):
        from caffe2.python.net_builder import ops
        with Node('trainer'), Task(name='my_task', num_instances=2) as task:
            with ops.task_init():
                globl = ops.Const(0)
            with ops.task_instance_init():
                local = ops.Const(0)
            with ops.loop(100):
                ops.Copy(globl, local)
            with ops.task_instance_exit():
                ops.Add([globl, local], [globl])
            with ops.task_exit():
                ops.Mul([globl, globl], [globl])

        plan = core.Plan(task.get_step())
        test_plan = core.Plan.create_from_proto(plan.Proto())

        self.assertEqual(len(plan.Steps()), 1)
        self.assertEqual(len(test_plan.Steps()), 1)
        self.assertEqual(len(plan.Proto().network), 9)
        self.assertEqual(len(test_plan.Proto().network), 9)
        self.assertEqual(len(plan.Proto().execution_step), 1)
        self.assertEqual(len(test_plan.Proto().execution_step), 1)
        self.assertEqual(plan.Steps()[0].Name(), test_plan.Steps()[0].Name())
        self.assertEqual(len(plan.Nets()), len(test_plan.Nets()))
        for idx in range(0, len(plan.Nets())):
            # When we create Net for test_plan, we will end up with new Net
            # name with postfix.
            net_1 = plan.Nets()[idx]
            net_2 = test_plan.Nets()[idx]
            trim_size = len(net_1.Name())
            self.assertEqual(net_1.Name(), net_2.Name()[:trim_size])
Ejemplo n.º 4
0
 def test_undefined_blob(self):
     job = example_job()
     with job:
         with Task():
             ops.Add(['a', 'b'])
     with self.assertRaises(AssertionError):
         net_printer.analyze(job)
Ejemplo n.º 5
0
 def test_valid_job(self):
     job = example_job()
     with job:
         with Task():
             # distributed_ctx_init_* ignored by analyzer
             ops.Add(['distributed_ctx_init_a', 'distributed_ctx_init_b'])
     net_printer.analyze(example_job())
Ejemplo n.º 6
0
 def proc2(rec):
     with core.NameScope('proc2'):
         out = NewRecord(ops, rec)
     out.uid.set(blob=rec.uid(), unsafe=True)
     ops.Sub([rec.value(), rec.value()], [out.value()])
     ops.Add([counter, ONE], [counter])
     return out
Ejemplo n.º 7
0
def example_loop():
    with Task():
        total = ops.Const(0)
        total_large = ops.Const(0)
        total_small = ops.Const(0)
        total_tiny = ops.Const(0)
        with ops.loop(10) as loop:
            outer = ops.Mul([loop.iter(), ops.Const(10)])
            with ops.loop(loop.iter()) as inner:
                val = ops.Add([outer, inner.iter()])
                with ops.If(ops.GE([val, ops.Const(80)])) as c:
                    ops.Add([total_large, val], [total_large])
                with c.Elif(ops.GE([val, ops.Const(50)])) as c:
                    ops.Add([total_small, val], [total_small])
                with c.Else():
                    ops.Add([total_tiny, val], [total_tiny])
                ops.Add([total, val], total)
Ejemplo n.º 8
0
 def _actual_loop(self):
     total = ops.Const(0)
     total_large = ops.Const(0)
     total_small = ops.Const(0)
     total_tiny = ops.Const(0)
     with ops.loop(10) as loop:
         outer = ops.Mul([loop.iter(), ops.Const(10)])
         with ops.loop(loop.iter()) as inner:
             val = ops.Add([outer, inner.iter()])
             with ops.If(ops.GE([val, ops.Const(80)])) as c:
                 ops.Add([total_large, val], [total_large])
             with c.Elif(ops.GE([val, ops.Const(50)])) as c:
                 ops.Add([total_small, val], [total_small])
             with c.Else():
                 ops.Add([total_tiny, val], [total_tiny])
             ops.Add([total, val], total)
     return map(final_output, (total, total_large, total_small, total_tiny))
Ejemplo n.º 9
0
 def test_undefined_blob(self):
     job = example_job()
     with job:
         with Task():
             ops.Add(['a', 'b'])
     with self.assertRaises(AssertionError) as e:
         net_printer.analyze(job)
     self.assertEqual("Blob undefined: a", str(e.exception))
Ejemplo n.º 10
0
def example_task():
    with Task():
        with ops.task_init():
            one = ops.Const(1)
        two = ops.Add([one, one])
        with ops.task_init():
            three = ops.Const(3)
        accum = ops.Add([two, three])
        # here, accum should be 5
        with ops.task_exit():
            # here, accum should be 6, since this executes after lines below
            seven_1 = ops.Add([accum, one])
        six = ops.Add([accum, one])
        ops.Add([accum, one], [accum])
        seven_2 = ops.Add([accum, one])
        o6 = final_output(six)
        o7_1 = final_output(seven_1)
        o7_2 = final_output(seven_2)

    with Task(num_instances=2):
        with ops.task_init():
            one = ops.Const(1)
        with ops.task_instance_init():
            local = ops.Const(2)
        ops.Add([one, local], [one])
        ops.LogInfo('ble')

    return o6, o7_1, o7_2
Ejemplo n.º 11
0
    def test_while_net(self):
        with NetBuilder() as nb:
            x = ops.Const(0)
            y = ops.Const(0)
            with ops.WhileNet():
                with ops.Condition():
                    ops.Add([x, ops.Const(1)], [x])
                    ops.LT([x, ops.Const(7)])
                ops.Add([x, y], [y])

        plan = Plan('while_net_test')
        plan.AddStep(to_execution_step(nb))
        ws = workspace.C.Workspace()
        ws.run(plan)

        x_value = ws.blobs[str(x)].fetch()
        y_value = ws.blobs[str(y)].fetch()

        self.assertEqual(x_value, 7)
        self.assertEqual(y_value, 21)
Ejemplo n.º 12
0
    def testWhile(self):
        with NetBuilder(_use_control_ops=True) as nb:
            ops.Copy(ops.Const(0), "i")
            ops.Copy(ops.Const(1), "one")
            ops.Copy(ops.Const(2), "two")
            ops.Copy(ops.Const(2.0), "x")
            ops.Copy(ops.Const(3.0), "y")
            ops.Copy(ops.Const(2.0), "z")
            # raises x to the power of 4 and y to the power of 2
            # and z to the power of 3
            with ops.WhileNet():
                with ops.Condition():
                    ops.Add(["i", "one"], "i")
                    ops.LE(["i", "two"])
                ops.Pow("x", "x", exponent=2.0)
                with ops.IfNet(ops.LT(["i", "two"])):
                    ops.Pow("y", "y", exponent=2.0)
                with ops.Else():
                    ops.Pow("z", "z", exponent=3.0)

            ops.Add(["x", "y"], "x_plus_y")
            ops.Add(["x_plus_y", "z"], "s")

        assert len(nb.get()) == 1, "Expected a single net produced"
        net = nb.get()[0]

        net.AddGradientOperators(["s"])
        workspace.RunNetOnce(net)
        # (x^4)' = 4x^3
        self.assertAlmostEqual(workspace.FetchBlob("x_grad"), 32)
        self.assertAlmostEqual(workspace.FetchBlob("x"), 16)
        # (y^2)' = 2y
        self.assertAlmostEqual(workspace.FetchBlob("y_grad"), 6)
        self.assertAlmostEqual(workspace.FetchBlob("y"), 9)
        # (z^3)' = 3z^2
        self.assertAlmostEqual(workspace.FetchBlob("z_grad"), 12)
        self.assertAlmostEqual(workspace.FetchBlob("z"), 8)
Ejemplo n.º 13
0
 def test_setup(self):
     with Task() as task:
         with ops.task_init():
             one = ops.Const(1)
         two = ops.Add([one, one])
         with ops.task_init():
             three = ops.Const(3)
         accum = ops.Add([two, three])
         # here, accum should be 5
         with ops.task_exit():
             # here, accum should be 6, since this executes after lines below
             seven_1 = ops.Add([accum, one])
         six = ops.Add([accum, one])
         ops.Add([accum, one], [accum])
         seven_2 = ops.Add([accum, one])
         o6 = final_output(six)
         o7_1 = final_output(seven_1)
         o7_2 = final_output(seven_2)
     with LocalSession() as session:
         session.run(task)
         self.assertEquals(o6.fetch(), 6)
         self.assertEquals(o7_1.fetch(), 7)
         self.assertEquals(o7_2.fetch(), 7)
Ejemplo n.º 14
0
RunNetOnce(model.net)
print("x = ", FetchBlob("x"))
print("y = ", FetchBlob("y"))

# ### Loops

# Another important control flow operator is 'While' that allows repeated execution of a fragment of net. Let's consider NetBuilder's version of While first.

# In[9]:

with NetBuilder() as nb:
    ops.Const(0, blob_out="i")
    ops.Const(0, blob_out="y")
    with ops.WhileNet():
        with ops.Condition():
            ops.Add(["i", ops.Const(1)], ["i"])
            ops.LE(["i", ops.Const(7)])
        ops.Add(["i", "y"], ["y"])

# This example illustrates the usage of 'while' loop with NetBuilder. As with an 'If' operator, standard block semantic rules apply. Note the usage of ops.Condition clause that should immediately follow ops.WhileNet and contains code that is executed before each iteration. The last operator in the condition clause is expected to have a single boolean output that determines whether the other iteration is executed.

# In the example above we increment the counter ("i") before each iteration and accumulate its values in "y" blob, the loop's body is executed 7 times, the resulting blob values:

# In[10]:

plan = Plan('while_net_test')
plan.AddStep(to_execution_step(nb))
ws = workspace.C.Workspace()
ws.run(plan)
print("i = ", ws.blobs["i"].fetch())
print("y = ", ws.blobs["y"].fetch())
Ejemplo n.º 15
0
    def test_if_net(self):
        with NetBuilder() as nb:
            x0 = ops.Const(0)
            x1 = ops.Const(1)
            x2 = ops.Const(2)
            y0 = ops.Const(0)
            y1 = ops.Const(1)
            y2 = ops.Const(2)

            # basic logic
            first_res = ops.Const(0)
            with ops.IfNet(ops.Const(True)):
                ops.Const(1, blob_out=first_res)
            with ops.Else():
                ops.Const(2, blob_out=first_res)

            second_res = ops.Const(0)
            with ops.IfNet(ops.Const(False)):
                ops.Const(1, blob_out=second_res)
            with ops.Else():
                ops.Const(2, blob_out=second_res)

            # nested and sequential ifs,
            # empty then/else,
            # passing outer blobs into branches,
            # writing into outer blobs, incl. into input blob
            # using local blobs
            with ops.IfNet(ops.LT([x0, x1])):
                local_blob = ops.Const(900)
                ops.Add([ops.Const(100), local_blob], [y0])

                gt = ops.GT([x1, x2])
                with ops.IfNet(gt):
                    # empty then
                    pass
                with ops.Else():
                    ops.Add([y1, local_blob], [local_blob])
                    ops.Add([ops.Const(100), y1], [y1])

                with ops.IfNet(ops.EQ([local_blob, ops.Const(901)])):
                    ops.Const(7, blob_out=y2)
                    ops.Add([y1, y2], [y2])
            with ops.Else():
                # empty else
                pass

        plan = Plan('if_net_test')
        plan.AddStep(to_execution_step(nb))
        ws = workspace.C.Workspace()
        ws.run(plan)

        first_res_value = ws.blobs[str(first_res)].fetch()
        second_res_value = ws.blobs[str(second_res)].fetch()
        y0_value = ws.blobs[str(y0)].fetch()
        y1_value = ws.blobs[str(y1)].fetch()
        y2_value = ws.blobs[str(y2)].fetch()

        self.assertEquals(first_res_value, 1)
        self.assertEquals(second_res_value, 2)
        self.assertEquals(y0_value, 1000)
        self.assertEquals(y1_value, 101)
        self.assertEquals(y2_value, 108)
        self.assertTrue(str(local_blob) not in ws.blobs)
Ejemplo n.º 16
0
    def testIf(self):
        W_a_values = [2.0, 1.5]
        B_a_values = [0.5]
        W_b_values = [7.0, 3.5]
        B_b_values = [1.5]

        with NetBuilder(_use_control_ops=True) as init_nb:
            W_a = ops.UniformFill([], "W_a", shape=[1, 2], min=-1., max=1.)
            B_a = ops.ConstantFill([], "B_a", shape=[1], value=0.0)
            W_b = ops.UniformFill([], "W_b", shape=[1, 2], min=-1., max=1.)
            B_b = ops.ConstantFill([], "B_b", shape=[1], value=0.0)

            W_gt_a = ops.GivenTensorFill(
                [], "W_gt_a", shape=[1, 2], values=W_a_values)
            B_gt_a = ops.GivenTensorFill([], "B_gt_a", shape=[1], values=B_a_values)
            W_gt_b = ops.GivenTensorFill(
                [], "W_gt_b", shape=[1, 2], values=W_b_values)
            B_gt_b = ops.GivenTensorFill([], "B_gt_b", shape=[1], values=B_b_values)

        params = [W_gt_a, B_gt_a, W_a, B_a, W_gt_b, B_gt_b, W_b, B_b]

        with NetBuilder(_use_control_ops=True, initial_scope=params) as train_nb:
            Y_pred = ops.ConstantFill([], "Y_pred", shape=[1], value=0.0)
            Y_noise = ops.ConstantFill([], "Y_noise", shape=[1], value=0.0)

            switch = ops.UniformFill(
                [], "switch", shape=[1], min=-1., max=1., run_once=0)
            zero = ops.ConstantFill([], "zero", shape=[1], value=0.0)
            X = ops.GaussianFill(
                [], "X", shape=[4096, 2], mean=0.0, std=1.0, run_once=0)
            noise = ops.GaussianFill(
                [], "noise", shape=[4096, 1], mean=0.0, std=1.0, run_once=0)

            with ops.IfNet(ops.LT([switch, zero])):
                Y_gt = ops.FC([X, W_gt_a, B_gt_a], "Y_gt")
                ops.Add([Y_gt, noise], Y_noise)
                ops.FC([X, W_a, B_a], Y_pred)
            with ops.Else():
                Y_gt = ops.FC([X, W_gt_b, B_gt_b], "Y_gt")
                ops.Add([Y_gt, noise], Y_noise)
                ops.FC([X, W_b, B_b], Y_pred)

            dist = ops.SquaredL2Distance([Y_noise, Y_pred], "dist")
            loss = dist.AveragedLoss([], ["loss"])

        assert len(init_nb.get()) == 1, "Expected a single init net produced"
        assert len(train_nb.get()) == 1, "Expected a single train net produced"

        train_net = train_nb.get()[0]
        gradient_map = train_net.AddGradientOperators([loss])

        init_net = init_nb.get()[0]
        ITER = init_net.ConstantFill(
            [], "ITER", shape=[1], value=0, dtype=core.DataType.INT64)
        train_net.Iter(ITER, ITER)
        LR = train_net.LearningRate(ITER, "LR", base_lr=-0.1,
                                        policy="step", stepsize=20, gamma=0.9)
        ONE = init_net.ConstantFill([], "ONE", shape=[1], value=1.)
        train_net.WeightedSum([W_a, ONE, gradient_map[W_a], LR], W_a)
        train_net.WeightedSum([B_a, ONE, gradient_map[B_a], LR], B_a)
        train_net.WeightedSum([W_b, ONE, gradient_map[W_b], LR], W_b)
        train_net.WeightedSum([B_b, ONE, gradient_map[B_b], LR], B_b)

        workspace.RunNetOnce(init_net)
        workspace.CreateNet(train_net)
        # print("Before training, W_a is: {}".format(workspace.FetchBlob("W_a")))
        # print("Before training, B_a is: {}".format(workspace.FetchBlob("B_a")))
        # print("Before training, W_b is: {}".format(workspace.FetchBlob("W_b")))
        # print("Before training, B_b is: {}".format(workspace.FetchBlob("B_b")))

        for _epoch in range(1000):
            workspace.RunNet(train_net.Proto().name)

        # print("After training, W_a is: {}".format(workspace.FetchBlob("W_a")))
        # print("After training, B_a is: {}".format(workspace.FetchBlob("B_a")))
        # print("After training, W_b is: {}".format(workspace.FetchBlob("W_b")))
        # print("After training, B_b is: {}".format(workspace.FetchBlob("B_b")))
        # print("Ground truth W_a is: {}".format(workspace.FetchBlob("W_gt_a")))
        # print("Ground truth B_a is: {}".format(workspace.FetchBlob("B_gt_a")))
        # print("Ground truth W_b is: {}".format(workspace.FetchBlob("W_gt_b")))
        # print("Ground truth B_b is: {}".format(workspace.FetchBlob("B_gt_b")))

        values_map = {
            "W_a": W_a_values,
            "B_a": B_a_values,
            "W_b": W_b_values,
            "B_b": B_b_values,
        }

        train_eps = 0.01

        for blob_name, values in values_map.items():
            trained_values = workspace.FetchBlob(blob_name)
            if trained_values.ndim == 2:
                self.assertEqual(trained_values.shape[0], 1)
                trained_values = trained_values[0][:]
            else:
                self.assertEqual(trained_values.ndim, 1)

            self.assertEqual(trained_values.size, len(values))
            for idx in range(len(trained_values)):
                self.assertTrue(abs(trained_values[idx] - values[idx]) < train_eps)
Ejemplo n.º 17
0
 def inc_total(rec):
     ops.Add([total, rec.val()], [total])
Ejemplo n.º 18
0
 def proc1(rec):
     with core.NameScope('proc1'):
         out = NewRecord(ops, rec)
     ops.Add([rec.uid(), rec.uid()], [out.uid()])
     out.value.set(blob=rec.value(), unsafe=True)
     return out