-
Notifications
You must be signed in to change notification settings - Fork 192
Lazy loading large assets #1923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 9 commits
adb7ec9
571c319
c9f1e1d
1ecca81
956617f
95c48f5
89a0054
32f0f37
a33a92e
ced3589
2b4a5b9
3e0802a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,7 +17,12 @@ struct Asset { | |||||||||||||||||||||
| explicit Asset(std::string k) : key(std::move(k)) {} | ||||||||||||||||||||||
| const std::string key; | ||||||||||||||||||||||
| std::vector<std::uint8_t> data; | ||||||||||||||||||||||
| std::filesystem::path path; | ||||||||||||||||||||||
| std::size_t size = 0; | ||||||||||||||||||||||
| bool dataLoaded = false; | ||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these fields be public? If they are exposed, each one can be independently mutable so it could be corrupted. |
||||||||||||||||||||||
| std::uint32_t alignment = 1; | ||||||||||||||||||||||
| std::vector<std::uint8_t>& getData(); | ||||||||||||||||||||||
| std::size_t getSize() const; | ||||||||||||||||||||||
| std::string getRelativeUri(); | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -127,6 +132,7 @@ class AssetManager /*: public Assets*/ { | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// Serializes | ||||||||||||||||||||||
| void serialize(AssetsMutable& assets, std::vector<std::uint8_t>& assetStorage, std::string prefix = "") const; | ||||||||||||||||||||||
| std::size_t getSerializedSize(std::size_t offset = 0) const; | ||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs docstring
Comment on lines
+142
to
+143
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value Document the The docstring answers the earlier request. It does not state what 📝 Proposed docstring- /// Calculates the size of the serialized data
+ /**
+ * Calculates the size of the serialized asset data.
+ *
+ * `@param` offset Starting offset in the aggregate asset storage
+ * `@returns` End offset after all assets, including alignment padding
+ * `@throws` std::runtime_error If an asset alignment is zero, or if the storage would exceed 4 GiB
+ */
std::size_t getSerializedSize(std::size_t offset = 0) const;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| } // namespace dai | ||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| #include <catch2/catch_all.hpp> | ||
| #include <catch2/catch_test_macros.hpp> | ||
| #include <chrono> | ||
| #include <filesystem> | ||
| #include <fstream> | ||
| #include <magic_enum/magic_enum.hpp> | ||
| #include <opencv2/videoio.hpp> | ||
| #include <thread> | ||
|
|
||
| #include "depthai/common/CameraBoardSocket.hpp" | ||
| #include "depthai/depthai.hpp" | ||
|
|
@@ -50,6 +54,67 @@ TEST_CASE("NNArchive API") { | |
| } | ||
| } | ||
|
|
||
| TEST_CASE("RVC4 NeuralNetwork model loading paths", "[rvc4]") { | ||
| std::vector<dai::DeviceModelZoo> supportedDeviceModels; | ||
| { | ||
| dai::Pipeline discoveryPipeline; | ||
| const auto device = discoveryPipeline.getDefaultDevice(); | ||
| if(device->getPlatform() != dai::Platform::RVC4) { | ||
| SKIP("RVC4-only test"); | ||
| } | ||
| supportedDeviceModels = device->getSupportedDeviceModels(); | ||
| } | ||
| REQUIRE_FALSE(supportedDeviceModels.empty()); | ||
|
|
||
| const dai::NNModelDescription description{"yolov6-nano", "RVC4"}; | ||
| const auto archivePath = dai::getModelFromZoo(description); | ||
| const dai::NNArchive archive{archivePath}; | ||
| const auto modelData = archive.getOtherModelFormat(); | ||
| REQUIRE(modelData.has_value()); | ||
|
|
||
| const std::filesystem::path directModelPath = | ||
| std::filesystem::temp_directory_path() | ||
| / ("depthai-rvc4-model-loading-test_" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()) + ".dlc"); | ||
| { | ||
| std::ofstream modelFile(directModelPath, std::ios::binary | std::ios::trunc); | ||
| REQUIRE(modelFile.is_open()); | ||
| modelFile.write(reinterpret_cast<const char*>(modelData->data()), static_cast<std::streamsize>(modelData->size())); | ||
| REQUIRE(modelFile.good()); | ||
| } | ||
|
Comment on lines
+89
to
+97
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win Remove the temporary model file on every exit path. Line 114 runs only after all assertions and pipeline starts succeed. A failed 🤖 Prompt for AI Agents |
||
|
|
||
| const auto startPipeline = [](const std::string& path, const auto& configure) { | ||
| INFO(path); | ||
| dai::Pipeline pipeline; | ||
| auto neuralNetwork = pipeline.create<dai::node::NeuralNetwork>(); | ||
| configure(neuralNetwork); | ||
|
|
||
| // The queue helpers provide the required single input connection while | ||
| // keeping this focused on model initialization rather than inference. | ||
| auto inputQueue = neuralNetwork->input.createInputQueue(); | ||
| auto outputQueue = neuralNetwork->out.createOutputQueue(); | ||
| pipeline.start(); | ||
| REQUIRE(pipeline.isRunning()); | ||
| pipeline.stop(); | ||
| pipeline.wait(); | ||
| std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
| }; | ||
|
|
||
| startPipeline("setModelPath(.dlc)", [&directModelPath](const auto& neuralNetwork) { neuralNetwork->setModelPath(directModelPath); }); | ||
| startPipeline("setOtherModelFormat(.dlc)", [&directModelPath](const auto& neuralNetwork) { neuralNetwork->setOtherModelFormat(directModelPath); }); | ||
| startPipeline("setOtherModelFormat(vector)", [&modelData](const auto& neuralNetwork) { neuralNetwork->setOtherModelFormat(*modelData); }); | ||
| startPipeline("setModelPath(NNArchive)", [&archivePath](const auto& neuralNetwork) { neuralNetwork->setModelPath(archivePath); }); | ||
| startPipeline("setNNArchive(NNArchive)", [&archive](const auto& neuralNetwork) { neuralNetwork->setNNArchive(archive); }); | ||
| startPipeline("setFromModelZoo", [&description](const auto& neuralNetwork) { neuralNetwork->setFromModelZoo(description, true); }); | ||
|
|
||
| for(const auto model : supportedDeviceModels) { | ||
| INFO(magic_enum::enum_name(model)); | ||
| startPipeline("setModelFromDeviceZoo", [model](const auto& neuralNetwork) { neuralNetwork->setModelFromDeviceZoo(model); }); | ||
| } | ||
|
|
||
| std::error_code ec; | ||
| std::filesystem::remove(directModelPath, ec); | ||
| } | ||
|
|
||
| TEST_CASE("Multi-Input NeuralNetwork API") { | ||
| dai::Pipeline p; | ||
| auto camera = p.create<dai::node::Camera>()->build(dai::CameraBoardSocket::CAM_A); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change was necessary as
std::vector<uint8_t>is declared as opaque so the previous version of the binding didn't work.