module onnx_tools.onnx_grammar.node_visitor_translator#

Inheritance diagram of mlprodict.onnx_tools.onnx_grammar.node_visitor_translator

Short summary#

module mlprodict.onnx_tools.onnx_grammar.node_visitor_translator

One class which visits a syntax tree.

source on GitHub

Classes#

class

truncated documentation

CodeNodeVisitor

Defines a visitor which walks though the syntax tree of the code.

Properties#

property

truncated documentation

Rows

returns a list of dictionaries with all the elements of the code

Static Methods#

staticmethod

truncated documentation

print_node

Debugging purpose.

Methods#

method

truncated documentation

__init__

export

Calls method export from the translator class.

generic_visit

Overrides generic_visit to check it is not used.

generic_visit_args

Overrides generic_visit to keep track of the indentation and the node parent. The function will add field …

make_msg

Displays line and column information into a string.

print_tree

Displays the tree of instructions.

push

Pushes an element into a list.

visit

Visits a node, a method must exist for every object class.

visit_

If an element is not found…

visit_Add

visit_arg

visit_arguments

visit_Assign

visit_Attribute

visit_BinOp

visit_Call

visit_Compare

visit_Div

visit_FunctionDef

visit_Gt

visit_keyword

visit_List

visit_Load

visit_Lt

visit_MatMult

visit_Module

visit_Mult

visit_Name

visit_NameConstant

A name.

visit_Num

visit_Pow

visit_Return

visit_Store

visit_Str

visit_Sub

visit_UnaryOp

visit_USub

Documentation#

One class which visits a syntax tree.

source on GitHub

class mlprodict.onnx_tools.onnx_grammar.node_visitor_translator.CodeNodeVisitor(translator=None)#

Bases: NodeVisitor

Defines a visitor which walks though the syntax tree of the code.

Get the tree of a simple function

The following code uses Python syntax but follows a SQL logic.

<<<

import ast
import inspect
from textwrap import dedent
from mlprodict.onnx_tools.onnx_grammar import CodeNodeVisitor


def norm2(x, y):
    delta = x - y
    n = delta ** 2
    return n


code = dedent(inspect.getsource(norm2))
node = ast.parse(code)
v = CodeNodeVisitor()
v.visit(node)
for r in v.Rows:
    print("{0}{1}: {2}".format("    " * r["indent"], r["type"], r["str"]))

>>>

    Module: 
        FunctionDef: norm2
            arguments: 
                arg: x
                arg: y
            Assign: 
                Name: delta
                BinOp: 
                    Name: x
                    Sub: 
                    Name: y
            Assign: 
                Name: n
                BinOp: 
                    Name: delta
                    Pow: 
                    Num: 2
            Return: 
                Name: n

source on GitHub

Parameters:

translatorCodeTranslator

By default the translator is OnnxTranslator.

source on GitHub

property Rows#

returns a list of dictionaries with all the elements of the code

source on GitHub

__init__(translator=None)#
Parameters:

translatorCodeTranslator

By default the translator is OnnxTranslator.

source on GitHub

export(context=None, **kwargs)#

Calls method export from the translator class.

Parameters:
  • context – known python needed to run the translated function

  • kwargs – whatever the method export from the translator class ingests

Returns:

whatever the method export from the translator class returns

source on GitHub

generic_visit(node)#

Overrides generic_visit to check it is not used.

source on GitHub

generic_visit_args(node, row)#

Overrides generic_visit to keep track of the indentation and the node parent. The function will add field row["children"] = visited nodes from here.

Parameters:
  • node – node which needs to be visited

  • row – row (a dictionary)

Returns:

See ast.NodeVisitor.generic_visit

source on GitHub

make_msg(node)#

Displays line and column information into a string.

source on GitHub

static print_node(node)#

Debugging purpose.

source on GitHub

print_tree()#

Displays the tree of instructions.

Returns:

string

source on GitHub

push(row)#

Pushes an element into a list.

source on GitHub

visit(node)#

Visits a node, a method must exist for every object class.

source on GitHub

visit_(node)#

If an element is not found…

source on GitHub

visit_NameConstant(node)#

A name.

source on GitHub