File: test_tensorflow.cpp

package info (click to toggle)
nanobind 2.9.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,060 kB
  • sloc: cpp: 11,838; python: 5,862; ansic: 4,820; makefile: 22; sh: 15
file content (25 lines) | stat: -rw-r--r-- 713 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <nanobind/nanobind.h>
#include <nanobind/ndarray.h>
namespace nb = nanobind;

int destruct_count = 0;


NB_MODULE(test_tensorflow_ext, m) {
    m.def("destruct_count", []() { return destruct_count; });
    m.def("ret_tensorflow", []() {
        struct alignas(256) Buf {
            float f[8];
        };
        Buf *buf = new Buf({ 1, 2, 3, 4, 5, 6, 7, 8 });
        size_t shape[2] = { 2, 4 };

        nb::capsule deleter(buf, [](void *data) noexcept {
           destruct_count++;
           delete (Buf *) data;
        });

        return nb::ndarray<nb::tensorflow, float, nb::shape<2, 4>>(buf->f, 2, shape,
                                                                   deleter);
    });
}