Skip to content

NNEF

Classes

Nnef

Represent a NNEF context in tract.

NNEF is a neural model interchange format, similar to ONNX but focusing on the needs of an inference engine instead of a training framework.

tract can natively load NNEF models. It can also save models it tract internal format as tract-opl models. tract-opl is a set of proprierary extensions to NNEF allowing to serializeing most of the models tract can handle. These extension can be activated by the with_*() methods.

Source code in tract/nnef.py
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
class Nnef:
    """
    Represent a NNEF context in tract.

    NNEF is a neural model interchange format, similar to ONNX but focusing on the needs
    of an inference engine instead of a training framework.

    `tract` can natively load NNEF models. It can also save models it tract internal format
    as `tract-opl` models. `tract-opl` is a set of proprierary extensions to NNEF allowing to
    serializeing most of the models tract can handle. These extension can be activated by the
    `with_*() methods`.
    """

    def __init__(self):
        ptr = c_void_p()
        check(lib.tract_nnef_create(byref(ptr)))
        self.ptr = ptr

    def __del__(self):
        check(lib.tract_nnef_destroy(byref(self.ptr)))

    def _valid(self):
        if self.ptr == None:
            raise TractError("invalid inference model (maybe already consumed ?)")

    def model_for_path(self, path: Union[str, Path]) -> Model:
        """
        Load an NNEF model from the file or folder at `path`

        ```python
        model = (
            tract.nnef()
            .model_for_path("mobilenet_v2_1.0.onnx.nnef.tgz")
            .into_optimized()
            .into_runnable()
        )
        ```
        """
        self._valid()
        model = c_void_p()
        path = str(path).encode("utf-8")
        check(lib.tract_nnef_model_for_path(self.ptr, path, byref(model)))
        return Model(model)

    def with_tract_core(self) -> "Nnef":
        """
        Enable tract-opl extensions to NNEF to covers tract-core operator set
        """
        self._valid()
        check(lib.tract_nnef_enable_tract_core(self.ptr))
        return self

    def with_onnx(self) -> "Nnef":
        """
        Enable tract-opl extensions to NNEF to covers (more or) ONNX operator set
        """
        self._valid()
        check(lib.tract_nnef_enable_onnx(self.ptr))
        return self

    def with_pulse(self) -> "Nnef":
        """
        Enable tract-opl extensions to NNEF for tract pulse operators (for audio streaming)
        """
        self._valid()
        check(lib.tract_nnef_enable_pulse(self.ptr))
        return self

    def with_extended_identifier_syntax(self) -> "Nnef":
        """
        Enable tract-opl extensions to NNEF for extended identifiers (will support PyTorch 2 path-like ids)
        """
        self._valid()
        check(lib.tract_nnef_allow_extended_identifier_syntax(self.ptr, True))
        return self

    def write_model_to_dir(self, model: Model, path: Union[str, Path]) -> None:
        """
        Save `model` as a NNEF directory model in `path`.

        tract tries to stick to strict NNEF even if extensions has been enabled.
        """
        self._valid()
        model._valid()
        if not isinstance(model, Model):
            raise TractError("Expected a Model, called with " + model);
        path = str(path).encode("utf-8")
        check(lib.tract_nnef_write_model_to_dir(self.ptr, path, model.ptr))

    def write_model_to_tar(self, model: Model, path: Union[str, Path]) -> None:
        """
        Save `model` as a NNEF tar archive in `path`.

        tract tries to stick to strict NNEF even if extensions has been enabled.
        """
        self._valid()
        model._valid()
        if not isinstance(model, Model):
            raise TractError("Expected a Model, called with " + model);
        path = str(path).encode("utf-8")
        check(lib.tract_nnef_write_model_to_tar(self.ptr, path, model.ptr))

    def write_model_to_tar_gz(self, model: Model, path: Union[str, Path]) -> None:
        """
        Save `model` as a NNEF tar compressed archive in `path`.

        tract tries to stick to strict NNEF even if extensions has been enabled.
        """
        self._valid()
        model._valid()
        if not isinstance(model, Model):
            raise TractError("Expected a Model, called with " + model);
        path = str(path).encode("utf-8")
        check(lib.tract_nnef_write_model_to_tar_gz(self.ptr, path, model.ptr))

Functions

model_for_path(path: Union[str, Path]) -> Model

Load an NNEF model from the file or folder at path

model = (
    tract.nnef()
    .model_for_path("mobilenet_v2_1.0.onnx.nnef.tgz")
    .into_optimized()
    .into_runnable()
)
Source code in tract/nnef.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def model_for_path(self, path: Union[str, Path]) -> Model:
    """
    Load an NNEF model from the file or folder at `path`

    ```python
    model = (
        tract.nnef()
        .model_for_path("mobilenet_v2_1.0.onnx.nnef.tgz")
        .into_optimized()
        .into_runnable()
    )
    ```
    """
    self._valid()
    model = c_void_p()
    path = str(path).encode("utf-8")
    check(lib.tract_nnef_model_for_path(self.ptr, path, byref(model)))
    return Model(model)
with_tract_core() -> Nnef

Enable tract-opl extensions to NNEF to covers tract-core operator set

Source code in tract/nnef.py
51
52
53
54
55
56
57
def with_tract_core(self) -> "Nnef":
    """
    Enable tract-opl extensions to NNEF to covers tract-core operator set
    """
    self._valid()
    check(lib.tract_nnef_enable_tract_core(self.ptr))
    return self
with_onnx() -> Nnef

Enable tract-opl extensions to NNEF to covers (more or) ONNX operator set

Source code in tract/nnef.py
59
60
61
62
63
64
65
def with_onnx(self) -> "Nnef":
    """
    Enable tract-opl extensions to NNEF to covers (more or) ONNX operator set
    """
    self._valid()
    check(lib.tract_nnef_enable_onnx(self.ptr))
    return self
with_pulse() -> Nnef

Enable tract-opl extensions to NNEF for tract pulse operators (for audio streaming)

Source code in tract/nnef.py
67
68
69
70
71
72
73
def with_pulse(self) -> "Nnef":
    """
    Enable tract-opl extensions to NNEF for tract pulse operators (for audio streaming)
    """
    self._valid()
    check(lib.tract_nnef_enable_pulse(self.ptr))
    return self
with_extended_identifier_syntax() -> Nnef

Enable tract-opl extensions to NNEF for extended identifiers (will support PyTorch 2 path-like ids)

Source code in tract/nnef.py
75
76
77
78
79
80
81
def with_extended_identifier_syntax(self) -> "Nnef":
    """
    Enable tract-opl extensions to NNEF for extended identifiers (will support PyTorch 2 path-like ids)
    """
    self._valid()
    check(lib.tract_nnef_allow_extended_identifier_syntax(self.ptr, True))
    return self
write_model_to_dir(model: Model, path: Union[str, Path]) -> None

Save model as a NNEF directory model in path.

tract tries to stick to strict NNEF even if extensions has been enabled.

Source code in tract/nnef.py
83
84
85
86
87
88
89
90
91
92
93
94
def write_model_to_dir(self, model: Model, path: Union[str, Path]) -> None:
    """
    Save `model` as a NNEF directory model in `path`.

    tract tries to stick to strict NNEF even if extensions has been enabled.
    """
    self._valid()
    model._valid()
    if not isinstance(model, Model):
        raise TractError("Expected a Model, called with " + model);
    path = str(path).encode("utf-8")
    check(lib.tract_nnef_write_model_to_dir(self.ptr, path, model.ptr))
write_model_to_tar(model: Model, path: Union[str, Path]) -> None

Save model as a NNEF tar archive in path.

tract tries to stick to strict NNEF even if extensions has been enabled.

Source code in tract/nnef.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def write_model_to_tar(self, model: Model, path: Union[str, Path]) -> None:
    """
    Save `model` as a NNEF tar archive in `path`.

    tract tries to stick to strict NNEF even if extensions has been enabled.
    """
    self._valid()
    model._valid()
    if not isinstance(model, Model):
        raise TractError("Expected a Model, called with " + model);
    path = str(path).encode("utf-8")
    check(lib.tract_nnef_write_model_to_tar(self.ptr, path, model.ptr))
write_model_to_tar_gz(model: Model, path: Union[str, Path]) -> None

Save model as a NNEF tar compressed archive in path.

tract tries to stick to strict NNEF even if extensions has been enabled.

Source code in tract/nnef.py
109
110
111
112
113
114
115
116
117
118
119
120
def write_model_to_tar_gz(self, model: Model, path: Union[str, Path]) -> None:
    """
    Save `model` as a NNEF tar compressed archive in `path`.

    tract tries to stick to strict NNEF even if extensions has been enabled.
    """
    self._valid()
    model._valid()
    if not isinstance(model, Model):
        raise TractError("Expected a Model, called with " + model);
    path = str(path).encode("utf-8")
    check(lib.tract_nnef_write_model_to_tar_gz(self.ptr, path, model.ptr))