Skip to content

Commit af39d8e

Browse files
committed
pre-commit and completing tests
1 parent be23ecb commit af39d8e

File tree

15 files changed

+506
-408
lines changed

15 files changed

+506
-408
lines changed

deep_core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ set(DEEP_CORE_LIB ${PROJECT_NAME}_lib)
3737
add_library(${DEEP_CORE_LIB} SHARED
3838
src/deep_node_base.cpp
3939
src/tensor.cpp
40+
src/backend_memory_allocator.cpp
41+
src/backend_inference_executor.cpp
4042
)
4143
target_include_directories(${DEEP_CORE_LIB} PUBLIC
4244
$<BUILD_INTERFACE:${include_dir}>

deep_core/include/deep_core/deep_node_base.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
#include "deep_core/types/tensor.hpp"
3232

3333
// Forward declarations
34-
namespace bond {
34+
namespace bond
35+
{
3536
class Bond;
3637
}
3738

deep_core/include/deep_core/plugin_interfaces/backend_inference_executor.hpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,57 @@ class BackendInferenceExecutor
3939
* @brief Load a model from file
4040
* @param model_path Path to the model file
4141
* @return true if successful, false otherwise
42+
* @throws std::invalid_argument if model_path is empty or file doesn't exist
4243
*/
43-
virtual bool load_model(const std::filesystem::path & model_path) = 0;
44+
bool load_model(const std::filesystem::path & model_path);
4445

4546
/**
4647
* @brief Run inference on input tensor
4748
* @param input Input tensor
4849
* @return Output tensor
50+
* @throws std::invalid_argument if input tensor is invalid
51+
* @throws std::runtime_error if no model is loaded
4952
*/
50-
virtual Tensor run_inference(Tensor input) = 0;
53+
Tensor run_inference(Tensor input);
5154

5255
/**
5356
* @brief Unload the currently loaded model
5457
*/
55-
virtual void unload_model() = 0;
58+
void unload_model();
59+
60+
/**
61+
* @brief Check if a model is currently loaded
62+
* @return true if model is loaded, false otherwise
63+
*/
64+
bool is_model_loaded() const
65+
{
66+
return model_loaded_;
67+
}
5668

5769
/**
5870
* @brief Get supported model formats
5971
* @return Vector of supported formats (e.g., "onnx", "pb")
6072
*/
6173
virtual std::vector<std::string> supported_model_formats() const = 0;
74+
75+
protected:
76+
/**
77+
* @brief Implementation of load_model (to be overridden by backends)
78+
*/
79+
virtual bool load_model_impl(const std::filesystem::path & model_path) = 0;
80+
81+
/**
82+
* @brief Implementation of run_inference (to be overridden by backends)
83+
*/
84+
virtual Tensor run_inference_impl(Tensor input) = 0;
85+
86+
/**
87+
* @brief Implementation of unload_model (to be overridden by backends)
88+
*/
89+
virtual void unload_model_impl() = 0;
90+
91+
private:
92+
bool model_loaded_ = false;
6293
};
6394

6495
} // namespace deep_ros

deep_core/include/deep_core/plugin_interfaces/backend_memory_allocator.hpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,45 @@ class BackendMemoryAllocator
4949
* @param dst Destination pointer (allocated by this allocator)
5050
* @param src Source pointer (host memory)
5151
* @param bytes Number of bytes to copy
52+
* @throws std::invalid_argument if dst or src is nullptr and bytes > 0
5253
*/
53-
virtual void copy_from_host(void * dst, const void * src, size_t bytes) = 0;
54+
void copy_from_host(void * dst, const void * src, size_t bytes);
5455

5556
/**
5657
* @brief Copy data from allocated memory to host (CPU) memory
5758
* @param dst Destination pointer (host memory)
5859
* @param src Source pointer (allocated by this allocator)
5960
* @param bytes Number of bytes to copy
61+
* @throws std::invalid_argument if dst or src is nullptr and bytes > 0
6062
*/
61-
virtual void copy_to_host(void * dst, const void * src, size_t bytes) = 0;
63+
void copy_to_host(void * dst, const void * src, size_t bytes);
6264

6365
/**
6466
* @brief Copy data between two allocations from this allocator
6567
* @param dst Destination pointer (allocated by this allocator)
6668
* @param src Source pointer (allocated by this allocator)
6769
* @param bytes Number of bytes to copy
70+
* @throws std::invalid_argument if dst or src is nullptr and bytes > 0
6871
*/
69-
virtual void copy_device_to_device(void * dst, const void * src, size_t bytes) = 0;
72+
void copy_device_to_device(void * dst, const void * src, size_t bytes);
7073

74+
protected:
75+
/**
76+
* @brief Implementation of copy_from_host (to be overridden by backends)
77+
*/
78+
virtual void copy_from_host_impl(void * dst, const void * src, size_t bytes) = 0;
79+
80+
/**
81+
* @brief Implementation of copy_to_host (to be overridden by backends)
82+
*/
83+
virtual void copy_to_host_impl(void * dst, const void * src, size_t bytes) = 0;
84+
85+
/**
86+
* @brief Implementation of copy_device_to_device (to be overridden by backends)
87+
*/
88+
virtual void copy_device_to_device_impl(void * dst, const void * src, size_t bytes) = 0;
89+
90+
public:
7191
/**
7292
* @brief Check if this allocator manages device (non-host) memory
7393
* @return true if memory is on device (GPU, etc.), false if host memory
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) 2025-present WATonomous. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "deep_core/plugin_interfaces/backend_inference_executor.hpp"
16+
17+
#include <filesystem>
18+
#include <stdexcept>
19+
#include <utility>
20+
21+
namespace deep_ros
22+
{
23+
24+
bool BackendInferenceExecutor::load_model(const std::filesystem::path & model_path)
25+
{
26+
// Validate path
27+
if (model_path.empty()) {
28+
throw std::invalid_argument("Empty model path provided to load_model");
29+
}
30+
31+
// Call implementation and track state
32+
bool success = load_model_impl(model_path);
33+
model_loaded_ = success;
34+
return success;
35+
}
36+
37+
Tensor BackendInferenceExecutor::run_inference(Tensor input)
38+
{
39+
// Validate input tensor
40+
if (input.data() == nullptr) {
41+
throw std::invalid_argument("Null data pointer in input tensor");
42+
}
43+
44+
if (input.size() == 0) {
45+
throw std::invalid_argument("Empty tensor provided to run_inference");
46+
}
47+
48+
// Check for valid shape
49+
if (input.shape().empty()) {
50+
throw std::invalid_argument("Tensor has empty shape");
51+
}
52+
53+
// Check if model is loaded
54+
if (!model_loaded_) {
55+
throw std::runtime_error("No model loaded for inference");
56+
}
57+
58+
return run_inference_impl(std::move(input));
59+
}
60+
61+
void BackendInferenceExecutor::unload_model()
62+
{
63+
unload_model_impl();
64+
model_loaded_ = false;
65+
}
66+
67+
} // namespace deep_ros
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) 2025-present WATonomous. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "deep_core/plugin_interfaces/backend_memory_allocator.hpp"
16+
17+
#include <stdexcept>
18+
19+
namespace deep_ros
20+
{
21+
22+
void BackendMemoryAllocator::copy_from_host(void * dst, const void * src, size_t bytes)
23+
{
24+
if (bytes > 0 && (dst == nullptr || src == nullptr)) {
25+
throw std::invalid_argument("Null pointer passed to copy_from_host");
26+
}
27+
copy_from_host_impl(dst, src, bytes);
28+
}
29+
30+
void BackendMemoryAllocator::copy_to_host(void * dst, const void * src, size_t bytes)
31+
{
32+
if (bytes > 0 && (dst == nullptr || src == nullptr)) {
33+
throw std::invalid_argument("Null pointer passed to copy_to_host");
34+
}
35+
copy_to_host_impl(dst, src, bytes);
36+
}
37+
38+
void BackendMemoryAllocator::copy_device_to_device(void * dst, const void * src, size_t bytes)
39+
{
40+
if (bytes > 0 && (dst == nullptr || src == nullptr)) {
41+
throw std::invalid_argument("Null pointer passed to copy_device_to_device");
42+
}
43+
copy_device_to_device_impl(dst, src, bytes);
44+
}
45+
46+
} // namespace deep_ros

deep_core/src/tensor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Tensor::Tensor(const std::vector<size_t> & shape, DataType dtype, std::shared_pt
7676
if (shape_.empty()) {
7777
throw std::invalid_argument("Tensor shape cannot be empty");
7878
}
79-
79+
8080
// Check for zero dimensions
8181
for (size_t dim : shape_) {
8282
if (dim == 0) {
@@ -102,7 +102,7 @@ Tensor::Tensor(void * data, const std::vector<size_t> & shape, DataType dtype)
102102
if (shape_.empty()) {
103103
throw std::invalid_argument("Tensor shape cannot be empty");
104104
}
105-
105+
106106
// Check for zero dimensions
107107
for (size_t dim : shape_) {
108108
if (dim == 0) {

deep_test/CMakeLists.txt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,28 +102,25 @@ if(BUILD_TESTING)
102102
LIBRARIES
103103
${std_msgs_TARGETS}
104104
)
105-
105+
106106
# Deep core tests - organized by functionality
107-
add_deep_test(test_simple_tensor
108-
test/test_simple_tensor.cpp
109-
)
110107
add_deep_test(test_tensor
111-
test/test_tensor.cpp
108+
test/deep_core/test_tensor.cpp
112109
LIBRARIES
113110
${DEEP_TEST_LIB}
114111
)
115112
add_deep_test(test_plugin_system
116-
test/test_plugin_system.cpp
113+
test/deep_core/test_plugin_system.cpp
117114
LIBRARIES
118115
${DEEP_TEST_LIB}
119116
)
120117
add_deep_test(test_node_lifecycle
121-
test/test_node_lifecycle.cpp
118+
test/deep_core/test_node_lifecycle.cpp
122119
LIBRARIES
123120
${DEEP_TEST_LIB}
124121
)
125122
add_deep_test(test_integration
126-
test/test_integration.cpp
123+
test/deep_core/test_integration.cpp
127124
LIBRARIES
128125
${DEEP_TEST_LIB}
129126
)

deep_test/include/test_fixtures/mock_backend_fixture.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@
1515
#pragma once
1616

1717
#include <memory>
18+
1819
#include <pluginlib/class_loader.hpp>
19-
#include "deep_core/plugin_interfaces/deep_backend_plugin.hpp"
20+
2021
#include "deep_core/plugin_interfaces/backend_memory_allocator.hpp"
22+
#include "deep_core/plugin_interfaces/deep_backend_plugin.hpp"
2123

2224
namespace deep_ros::test
2325
{
2426

2527
/**
2628
* @brief Mock backend fixture for testing
27-
*
29+
*
2830
* Catch2 test fixture that provides access to the mock backend plugin
2931
* and its allocator for consistent testing across all packages.
3032
*/
@@ -36,8 +38,8 @@ class MockBackendFixture
3638
*/
3739
MockBackendFixture()
3840
{
39-
loader_ = std::make_unique<pluginlib::ClassLoader<deep_ros::DeepBackendPlugin>>(
40-
"deep_core", "deep_ros::DeepBackendPlugin");
41+
loader_ =
42+
std::make_unique<pluginlib::ClassLoader<deep_ros::DeepBackendPlugin>>("deep_core", "deep_ros::DeepBackendPlugin");
4143
mock_backend_ = loader_->createSharedInstance("mock_backend");
4244
allocator_ = mock_backend_->get_allocator();
4345
}
@@ -66,4 +68,4 @@ class MockBackendFixture
6668
std::shared_ptr<deep_ros::BackendMemoryAllocator> allocator_;
6769
};
6870

69-
} // namespace deep_ros::test
71+
} // namespace deep_ros::test

deep_test/include/test_nodes/deep_test_node.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ class DeepTestNode : public DeepNodeBase
6565
};
6666

6767
} // namespace test
68-
} // namespace deep_ros
68+
} // namespace deep_ros

0 commit comments

Comments
 (0)