载入中...
搜索中...
未找到
OnnxByteStorage.h
浏览该文件的文档.
1#pragma once
2#include <span>
3#include <stdexcept>
4#include "tensor/OnnxModel.h"
7// Internal copy-on-write host view. Const reads materialize only at CPU boundaries.
8// GPU aliases share immutable storage and never force a readback.
11 mutable OnnxBuffer buffer_;
12 std::shared_ptr<std::vector<uint8_t>> writable_;
13 const std::vector<uint8_t>& get() const {
14 if (!buffer_.host) {
15 if (!buffer_.device) throw std::runtime_error("Missing ONNX storage");
16 auto r = buffer_.device->readback();
17 if (!r.ok()) throw std::runtime_error(r.error()->message());
18 if (r.value().size() != buffer_.size) throw std::runtime_error("GPU byte count mismatch");
19 buffer_.host = std::make_shared<const std::vector<uint8_t>>(std::move(r.value()));
20 }
21 return *buffer_.host;
22 }
23 std::vector<uint8_t>& write() {
24 // CPU mutation creates a new authoritative storage identity.
25 if (writable_ && buffer_.host.use_count() == 2) return *writable_;
26 auto v = std::make_shared<std::vector<uint8_t>>(get());
27 buffer_ = {v, {}, v->size()};
28 writable_ = v;
29 return *v;
30 }
31
32public:
34 ByteStorage() : ByteStorage(std::vector<uint8_t>{}) {}
36 ByteStorage(std::vector<uint8_t> v) : buffer_{std::make_shared<const std::vector<uint8_t>>(std::move(v)), {}, 0} {
37 buffer_.size = buffer_.host->size();
38 }
40 ByteStorage(OnnxBuffer b) : buffer_(std::move(b)) {}
42 void retainAcrossRuns() { buffer_.persistent = true; }
44 const OnnxBuffer& buffer() const { return buffer_; }
46 size_t size() const { return buffer_.size; }
48 bool empty() const { return !size(); }
52 const uint8_t* data() const { return get().data(); }
56 uint8_t* data() { return write().data(); }
58 uint8_t operator[](size_t i) const { return get()[i]; }
60 uint8_t& operator[](size_t i) { return write()[i]; }
62 auto begin() { return write().begin(); }
64 auto end() { return write().end(); }
66 auto begin() const { return get().begin(); }
68 auto end() const { return get().end(); }
70 void resize(size_t n) {
71 auto& v = write();
72 v.resize(n);
73 buffer_.size = n;
74 }
77 template <class I>
79 void assign(I a, I b) {
80 *this = ByteStorage(std::vector<uint8_t>(a, b));
81 }
82 operator std::span<const uint8_t>() const { return get(); }
83 operator std::vector<uint8_t>() const { return get(); }
84};
87 OnnxElement element = OnnxElement::Float32;
88 std::vector<int64_t> shape;
90};
91} // namespace eve::tensor::onnx_detail
glm::vec3 n
Definition Grass.cpp:63
double r
float v
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::string element
float size
Definition TreeMesh.cpp:156
const OnnxBuffer & buffer() const
Buffer.
uint8_t & operator[](size_t i)
Operator [].
void assign(I a, I b)
Copy borrowed iterators into owning CPU storage. @lifetime Input iterators are borrowed only for this...
uint8_t * data()
Borrow mutable CPU data until mutation/destruction; detaches graph aliases before writing....
size_t size() const
Returns the size of size.
uint8_t operator[](size_t i) const
Operator [].
ByteStorage(OnnxBuffer b)
Constructs a ByteStorage.
ByteStorage()
Constructs a ByteStorage.
ByteStorage(std::vector< uint8_t > v)
Constructs a ByteStorage.
void retainAcrossRuns()
Retain across runs.
const uint8_t * data() const
Borrow CPU data until this storage is mutated or destroyed; device-thread only for lazy data....
OnnxElement
ONNX wire element types; distinct from block-quantized Tensor storage.
Definition OnnxModel.h:18
Immutable buffer shared by graph aliases; exactly one storage is authoritative.
Definition OnnxStorage.h:19
std::shared_ptr< OnnxDeviceStorage > device
Definition OnnxStorage.h:21
std::shared_ptr< const std::vector< uint8_t > > host
Definition OnnxStorage.h:20