def test_profile_graph(self):
        calls = [0]

        def f0(t):
            calls[0] += 1
            time.sleep(t)

        def f1(t):
            calls[0] += 1
            time.sleep(t)

        def f2():
            calls[0] += 1
            f1(0.1)
            f1(0.01)

        def f3():
            calls[0] += 1
            f0(0.2)
            f1(0.5)

        def f4():
            calls[0] += 1
            f2()
            f3()

        ps = profile(f4)[0]  # pylint: disable=W0632
        profile2df(ps, verbose=False, clean_text=lambda x: x.split('/')[-1])
        root, nodes = profile2graph(ps, clean_text=lambda x: x.split('/')[-1])
        self.assertEqual(len(nodes), 6)
        self.assertIsInstance(nodes, dict)
        self.assertIsInstance(root, ProfileNode)
        self.assertIn("(", str(root))
        dicts = root.as_dict()
        self.assertEqual(10, len(dicts))
        text = root.to_text()
        self.assertIn("1  1", text)
        self.assertIn('        f1', text)
        text = root.to_text(fct_width=20)
        self.assertIn('...', text)
        root.to_text(sort_key=SortKey.CUMULATIVE)
        root.to_text(sort_key=SortKey.TIME)
        self.assertRaise(lambda: root.to_text(sort_key=SortKey.NAME),
                         NotImplementedError)
        js = root.to_json(indent=2)
        self.assertIn('"details"', js)
        js = root.to_json(as_str=False)
        self.assertIsInstance(js, dict)
    def test_profile_graph_recursive1(self):
        def f0(t):
            if t < 0.1:
                time.sleep(t)
            else:
                f0(t - 0.1)

        def f4():
            f0(0.15)

        ps = profile(f4)[0]  # pylint: disable=W0632
        profile2df(ps, verbose=False, clean_text=lambda x: x.split('/')[-1])
        root, nodes = profile2graph(ps, clean_text=lambda x: x.split('/')[-1])
        self.assertEqual(len(nodes), 3)
        text = root.to_text()
        self.assertIn("    f0", text)
        js = root.to_json(indent=2)
        self.assertIn('"details"', js)
Exemple #3
0
        return text[pos:]
    pos = text.find('sklearn')
    if pos >= 0:
        return text[pos:]
    pos = text.find('onnxcustom')
    if pos >= 0:
        return text[pos:]
    pos = text.find('site-packages')
    if pos >= 0:
        return text[pos:]
    return text


ps = profile(lambda: benchmark(X_train, y_train,
             nn, train_session, name='NN-CPU'))[0]
root, nodes = profile2graph(ps, clean_text=clean_name)
text = root.to_text()
print(text)

######################################
# if GPU is available
# +++++++++++++++++++

if get_device().upper() == 'GPU':

    train_session = OrtGradientForwardBackwardOptimizer(
        onx, device='cuda', learning_rate=1e-5,
        warm_start=False, max_iter=max_iter, batch_size=batch_size)

    benches.append(benchmark(X_train, y_train, nn,
                   train_session, name='NN-GPU'))
#########################################
# Graph
# +++++

fig, ax = plt.subplots(1, 1)
piv.plot(title="Time processing of serialization functions\n"
         "lower better",
         ax=ax)
ax.set_xlabel("onnx size")
ax.set_ylabel("s")

###########################################
# Conclusion
# ++++++++++
#
# This graph shows that implementing check_model in python is much slower
# than the C++ version. However, protobuf prevents from sharing
# ModelProto from Python to C++ (see `Python Updates
# <https://developers.google.com/protocol-buffers/docs/news/2022-05-06>`_)
# unless the python package is compiled with a specific setting
# (problably slower). A profiling shows that the code spends quite some time
# in function :func:`getattr`.

ps = profile(lambda: check_model_py(onx))[0]
root, nodes = profile2graph(ps, clean_text=lambda x: x.split('/')[-1])
text = root.to_text()
print(text)

# plt.show()