7.1.8. Example: Load codegen_dir
A sample program that directly reuses the compilation result from Example: Adding Two Vectors (/tmp/add_two_tensors)
Execution Method
$ cd /opt/pfn/pfcomp/codegen/examples/
$ ./exec_with_env.sh python3 load_codegen_dir.py
Expected Output
Computation result (should be equal to the result of
add.py)
result_on_cpu=tensor([[2., 2., 2., 2.],
[2., 2., 2., 2.],
[2., 2., 2., 2.]])
Related Links
Sample Program
1import torch
2from mlsdk import Context, MNDevice, storage
3
4device = MNDevice("mncore2:auto")
5context = Context(device)
6Context.switch_context(context)
7
8# Load the previously compiled function
9loaded_add = context.load_codegen_dir(storage.path("/tmp/add_two_tensors"))
10
11# Now you can use it directly
12result = loaded_add({"x": torch.ones(3, 4), "y": torch.ones(3, 4)})
13result_on_cpu = result["out"].cpu()
14print(f"Result from loaded function: {result_on_cpu=}")
15assert torch.allclose(result_on_cpu, torch.ones(3, 4) * 2)