Executable

class tripy.Executable(executable, arg_names, output_devices)[source]

Bases: object

Represents a compiled executable generated by the compiler.

See also

compile()

static load(path: str) Executable[source]

Loads a executable from the provided path.

Parameters:

path (str) – The path from which to load the exectuable.

Returns:

The executable object loaded from the file.

Return type:

Executable

Example: Save and load executable
Save and load executable
 1import os
 2
 3def add(a, b):
 4    return a + b
 5
 6compiled_add = tp.compile(
 7    add,
 8    args=[
 9        tp.InputInfo(([1, 2, 3],), dtype=tp.float32),
10        tp.InputInfo(([1, 2, 3],), dtype=tp.float32),
11    ],
12)
13
14
15# Assuming `out_dir` is the directory containing the executable:
16executable_file = os.path.join(out_dir, "executable.json")
17loaded_executable = tp.Executable.load(executable_file)
>>> loaded_executable
Executable(a: Tensor, b: Tensor) -> Tensor
Output:
[I] /tmp/tmpnynvez0q does not exist, creating now.
__call__(*args, **kwargs) Tensor | Sequence[Tensor][source]

Invokes the executable with the specified tensor arguments.

Parameters:
  • *args – Positional arguments. Must be of type Tensor .

  • **kwargs – Keyword arguments. Must be of type Tensor .

Returns:

The output Tensor s of the compiled function.

Return type:

Tensor | Sequence[Tensor]

Example
Example
 1def add(a, b):
 2    return a + b
 3
 4compiled_add = tp.compile(
 5    add,
 6    args=[
 7        tp.InputInfo((1,), dtype=tp.float32),
 8        tp.InputInfo((1,), dtype=tp.float32),
 9    ],
10)
11
12a = tp.ones((1,), dtype=tp.float32)
13b = tp.ones((1,), dtype=tp.float32)
14
15out = compiled_add(a, b)
>>> a
tensor([1.0000], dtype=float32, loc=gpu:0, shape=(1,))
>>> b
tensor([1.0000], dtype=float32, loc=gpu:0, shape=(1,))
>>> out
tensor([2.0000], dtype=float32, loc=gpu:0, shape=(1,))
save(path: str) None[source]

Saves this executable to the provided path.

Parameters:

path (str) – The path at which to save the executable.

Return type:

None

Example: Save executable
Save executable
 1import os
 2
 3def add(a, b):
 4    return a + b
 5
 6compiled_add = tp.compile(
 7    add,
 8    args=[
 9        tp.InputInfo(([1, 2, 3],), dtype=tp.float32),
10        tp.InputInfo(([1, 2, 3],), dtype=tp.float32),
11    ],
12)
13
14# Assuming `out_dir` is the desired output directory:
15executable_file = os.path.join(out_dir, "executable.json")
16compiled_add.save(executable_file)
Output:
[I] /tmp/tmpnkonb_g_ does not exist, creating now.