chrono_batch_render/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
58 / 78
74.4%
Branches:
38 / 139
27.3%
Functions:
8 / 10, 1 excl
80.0%

python/src/bindings.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2 #pragma once
3
4 #include <cstddef>
5 #include <cstdint>
6 #include <memory>
7 #include <optional>
8 #include <string>
9 #include <string_view>
10 #include <vector>
11
12 #include <dlpack/dlpack.h>
13 #include <nanobind/nanobind.h>
14
15 #include "chrono_batch_render/chrono_batch_render.h"
16 #include "chrono_batch_render/device/queue.h"
17 #include "chrono_batch_render/stage/views/layout.h"
18
19 namespace chrono::batch_render::python {
20
21 SchemaId FindSchema(std::string_view name);
22 nanobind::object Numpy(std::vector<std::byte> bytes, const std::vector<size_t>& shape,
23 DLDataType dtype);
24
25 DLDataType DataType(device::DType type);
26 nanobind::tuple TensorDevice(const device::Tensor& tensor);
27 nanobind::object ExportTensor(const device::Tensor& tensor, std::optional<int64_t> stream,
28 std::optional<nanobind::tuple> maxVersion,
29 std::optional<nanobind::tuple> dlDevice, std::optional<bool> copy);
30
31 1319 inline Stream ToStream(std::optional<uintptr_t> stream) {
32
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1319 times.
1319 return stream ? Stream::Compute(reinterpret_cast<void*>(*stream)) : Stream::None();
33 }
34
35 struct TakenTensor {
36 DLManagedTensorVersioned* managed = nullptr;
37 Stream producer = Stream::None();
38 };
39
40 TakenTensor TakeTensor(nanobind::handle source, std::optional<uintptr_t> stream);
41
42 1170 inline std::shared_ptr<void> Hold(DLManagedTensorVersioned* managed) {
43 1170 return std::shared_ptr<void>(managed, [](void* held) {
44 1170 auto* tensor = static_cast<DLManagedTensorVersioned*>(held);
45
1/8
<unknown function 42>:
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
chrono::batch_render::python::ToStream(std::optional<unsigned long>):
✓ Branch 0 taken 1170 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1170 if (tensor->deleter) {
46
0/2
<unknown function 42>:
✗ Branch 2 not taken.
chrono::batch_render::python::ToStream(std::optional<unsigned long>):
✗ Branch 2 not taken.
1170 tensor->deleter(tensor);
47 }
48
0/1
✗ Branch 1 not taken.
1170 });
49 }
50
51
6/31
<unknown function 42>:
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
chrono::batch_render::python::ToStream(std::optional<unsigned long>):
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1138 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
1154 class HeldTensors {
52 public:
53 5690 const DLTensor* Take(nanobind::handle value, std::optional<uintptr_t> stream) {
54
2/2
✓ Branch 0 taken 1140 times.
✓ Branch 1 taken 4550 times.
5690 if (value.is_none()) {
55 return nullptr;
56 }
57 1140 return &Keep(value, stream).managed->dl_tensor;
58 }
59
60 22 device::Tensor Import(device::Device& device, nanobind::handle value,
61 std::optional<uintptr_t> stream, std::string label) {
62 22 const TakenTensor taken = Keep(value, stream);
63
1/1
✓ Branch 2 taken 22 times.
44 return device.Import(taken.managed->dl_tensor, taken.producer, std::move(label));
64 }
65
66 2276 Stream Producer() const {
67
1/7
✓ Branch 0 taken 1138 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
2276 return m_producer;
68 }
69
70 1146 void Retire(Scene& scene, const device::Operation& done) {
71
1/1
✓ Branch 2 taken 1146 times.
1146 scene.GetDevice().GetQueue().Retire(std::move(m_held), done);
72 1146 }
73
74 private:
75 1162 TakenTensor Keep(nanobind::handle value, std::optional<uintptr_t> stream) {
76 1162 const TakenTensor taken = TakeTensor(value, stream);
77
2/3
✓ Branch 2 taken 1162 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1162 times.
1162 m_held->push_back(Hold(taken.managed));
78
2/2
✓ Branch 0 taken 1150 times.
✓ Branch 1 taken 12 times.
1162 if (!m_taken) {
79 1150 m_producer = taken.producer;
80 1150 m_taken = true;
81 }
82 1162 return taken;
83 }
84
85 std::shared_ptr<std::vector<std::shared_ptr<void>>> m_held =
86 std::make_shared<std::vector<std::shared_ptr<void>>>();
87 Stream m_producer = Stream::None();
88 bool m_taken = false;
89 };
90
91 void BindDevice(nanobind::module_& module);
92 void BindScene(nanobind::module_& module);
93 void BindRender(nanobind::module_& module);
94 void BindImport(nanobind::module_& module);
95 void BindIo(nanobind::module_& module);
96 void BindSensorPipeline(nanobind::module_& module);
97 void BindRotation(nanobind::module_& module);
98
99 }
100
101 namespace nanobind::detail {
102
103
10/42
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 8 taken 6 times.
✓ Branch 9 taken 42 times.
✓ Branch 9 taken 23 times.
✗ Branch 10 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 41262 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 8 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 time.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 22 not taken.
✓ Branch 23 taken 1 time.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 27 not taken.
✓ Branch 28 taken 1 time.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 32 not taken.
✓ Branch 33 taken 3 times.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
41383 template <class T> struct type_caster<chrono::batch_render::Ref<T>> {
104 using Caster = make_caster<T>;
105
1/1
✓ Branch 1 taken 1 time.
1 NB_TYPE_CASTER(chrono::batch_render::Ref<T>, Caster::Name)
106
107 41640 bool from_python(handle src, uint32_t flags, cleanup_list* cleanup) noexcept {
108 Caster caster;
109
2/6
nanobind::detail::type_caster<chrono::batch_render::Ref<chrono::batch_render::Overlays>, int>::from_python(nanobind::handle, unsigned int, nanobind::detail::cleanup_list*):
✗ Branch 1 not taken.
✗ Branch 2 not taken.
nanobind::detail::type_caster<chrono::batch_render::Ref<chrono::batch_render::Scene>, int>::from_python(nanobind::handle, unsigned int, nanobind::detail::cleanup_list*):
✓ Branch 1 taken 41598 times.
✗ Branch 2 not taken.
nanobind::detail::type_caster<chrono::batch_render::Ref<chrono::batch_render::device::Device>, int>::from_python(nanobind::handle, unsigned int, nanobind::detail::cleanup_list*):
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
41640 if (!caster.from_python(src, flags_for_local_caster<T>(flags), cleanup)) {
110 return false;
111 }
112
2/6
nanobind::detail::type_caster<chrono::batch_render::Ref<chrono::batch_render::Overlays>, int>::from_python(nanobind::handle, unsigned int, nanobind::detail::cleanup_list*):
✗ Branch 1 not taken.
✗ Branch 2 not taken.
nanobind::detail::type_caster<chrono::batch_render::Ref<chrono::batch_render::Scene>, int>::from_python(nanobind::handle, unsigned int, nanobind::detail::cleanup_list*):
✗ Branch 1 not taken.
✓ Branch 2 taken 41598 times.
nanobind::detail::type_caster<chrono::batch_render::Ref<chrono::batch_render::device::Device>, int>::from_python(nanobind::handle, unsigned int, nanobind::detail::cleanup_list*):
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
41640 value = chrono::batch_render::Ref<T>(caster.operator T*());
113 41640 return true;
114 }
115
116
1/4
nanobind::detail::type_caster<chrono::batch_render::Ref<chrono::batch_render::Overlays>, int>::from_cpp(chrono::batch_render::Ref<chrono::batch_render::Overlays> const&, nanobind::rv_policy, nanobind::detail::cleanup_list*):
✗ Branch 0 not taken.
✗ Branch 1 not taken.
nanobind::detail::type_caster<chrono::batch_render::Ref<chrono::batch_render::device::Device>, int>::from_cpp(chrono::batch_render::Ref<chrono::batch_render::device::Device> const&, nanobind::rv_policy, nanobind::detail::cleanup_list*):
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
60 static handle from_cpp(const chrono::batch_render::Ref<T>& ref, rv_policy,
117 cleanup_list* cleanup) noexcept {
118
1/4
nanobind::detail::type_caster<chrono::batch_render::Ref<chrono::batch_render::Overlays>, int>::from_cpp(chrono::batch_render::Ref<chrono::batch_render::Overlays> const&, nanobind::rv_policy, nanobind::detail::cleanup_list*):
✗ Branch 0 not taken.
✗ Branch 1 not taken.
nanobind::detail::type_caster<chrono::batch_render::Ref<chrono::batch_render::device::Device>, int>::from_cpp(chrono::batch_render::Ref<chrono::batch_render::device::Device> const&, nanobind::rv_policy, nanobind::detail::cleanup_list*):
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
60 if (!ref) {
119 ✗ return none().release();
120 }
121
1/4
nanobind::detail::type_caster<chrono::batch_render::Ref<chrono::batch_render::Overlays>, int>::from_cpp(chrono::batch_render::Ref<chrono::batch_render::Overlays> const&, nanobind::rv_policy, nanobind::detail::cleanup_list*):
✗ Branch 1 not taken.
✗ Branch 2 not taken.
nanobind::detail::type_caster<chrono::batch_render::Ref<chrono::batch_render::device::Device>, int>::from_cpp(chrono::batch_render::Ref<chrono::batch_render::device::Device> const&, nanobind::rv_policy, nanobind::detail::cleanup_list*):
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
77 if (object existing = find(*ref.Get())) {
122 17 return existing.release();
123 }
124
2/8
nanobind::detail::type_caster<chrono::batch_render::Ref<chrono::batch_render::Overlays>, int>::from_cpp(chrono::batch_render::Ref<chrono::batch_render::Overlays> const&, nanobind::rv_policy, nanobind::detail::cleanup_list*):
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
nanobind::detail::type_caster<chrono::batch_render::Ref<chrono::batch_render::device::Device>, int>::from_cpp(chrono::batch_render::Ref<chrono::batch_render::device::Device> const&, nanobind::rv_policy, nanobind::detail::cleanup_list*):
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
86 const handle created = Caster::from_cpp(ref.Get(), rv_policy::reference, cleanup);
125
1/4
nanobind::detail::type_caster<chrono::batch_render::Ref<chrono::batch_render::Overlays>, int>::from_cpp(chrono::batch_render::Ref<chrono::batch_render::Overlays> const&, nanobind::rv_policy, nanobind::detail::cleanup_list*):
✗ Branch 0 not taken.
✗ Branch 1 not taken.
nanobind::detail::type_caster<chrono::batch_render::Ref<chrono::batch_render::device::Device>, int>::from_cpp(chrono::batch_render::Ref<chrono::batch_render::device::Device> const&, nanobind::rv_policy, nanobind::detail::cleanup_list*):
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
43 if (created.is_valid()) {
126 43 ref->AddRef();
127 43 keep_alive_cb(created, ref.Get(),
128 [](void* held) noexcept { static_cast<T*>(held)->Release(); });
129 }
130 43 return created;
131 }
132 };
133
134 }
135