Skip to content

Commit 2168c2b

Browse files
committed
fix: mark some methods const in the BaseModel
1 parent 2dbaa15 commit 2168c2b

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

packages/react-native-executorch/common/rnexecutorch/models/BaseModel.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ BaseModel::BaseModel(const std::string &modelSource,
3030
}
3131

3232
std::vector<int32_t> BaseModel::getInputShape(std::string method_name,
33-
int32_t index) {
33+
int32_t index) const {
3434
if (!module_) {
3535
throw std::runtime_error("Model not loaded: Cannot get input shape");
3636
}
@@ -88,7 +88,7 @@ BaseModel::getAllInputShapes(std::string methodName) const {
8888
/// to JS. It is not meant to be used within C++. If you want to call forward
8989
/// from C++ on a BaseModel, please use BaseModel::forward.
9090
std::vector<JSTensorViewOut>
91-
BaseModel::forwardJS(std::vector<JSTensorViewIn> tensorViewVec) {
91+
BaseModel::forwardJS(std::vector<JSTensorViewIn> tensorViewVec) const {
9292
if (!module_) {
9393
throw std::runtime_error("Model not loaded: Cannot perform forward pass");
9494
}
@@ -136,7 +136,7 @@ BaseModel::forwardJS(std::vector<JSTensorViewIn> tensorViewVec) {
136136
}
137137

138138
Result<executorch::runtime::MethodMeta>
139-
BaseModel::getMethodMeta(const std::string &methodName) {
139+
BaseModel::getMethodMeta(const std::string &methodName) const {
140140
if (!module_) {
141141
throw std::runtime_error("Model not loaded: Cannot get method meta!");
142142
}
@@ -161,7 +161,7 @@ BaseModel::forward(const std::vector<EValue> &input_evalues) const {
161161

162162
Result<std::vector<EValue>>
163163
BaseModel::execute(const std::string &methodName,
164-
const std::vector<EValue> &input_value) {
164+
const std::vector<EValue> &input_value) const {
165165
if (!module_) {
166166
throw std::runtime_error("Model not loaded, cannot run execute.");
167167
}
@@ -175,7 +175,7 @@ std::size_t BaseModel::getMemoryLowerBound() const noexcept {
175175
void BaseModel::unload() noexcept { module_.reset(nullptr); }
176176

177177
std::vector<int32_t>
178-
BaseModel::getTensorShape(const executorch::aten::Tensor &tensor) {
178+
BaseModel::getTensorShape(const executorch::aten::Tensor &tensor) const {
179179
auto sizes = tensor.sizes();
180180
return std::vector<int32_t>(sizes.begin(), sizes.end());
181181
}

packages/react-native-executorch/common/rnexecutorch/models/BaseModel.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@ class BaseModel {
2525
Module::LoadMode loadMode = Module::LoadMode::MmapUseMlockIgnoreErrors);
2626
std::size_t getMemoryLowerBound() const noexcept;
2727
void unload() noexcept;
28-
std::vector<int32_t> getInputShape(std::string method_name, int32_t index);
28+
std::vector<int32_t> getInputShape(std::string method_name,
29+
int32_t index) const;
2930
std::vector<std::vector<int32_t>>
3031
getAllInputShapes(std::string methodName = "forward") const;
3132
std::vector<JSTensorViewOut>
32-
forwardJS(std::vector<JSTensorViewIn> tensorViewVec);
33+
forwardJS(std::vector<JSTensorViewIn> tensorViewVec) const;
3334
Result<std::vector<EValue>> forward(const EValue &input_value) const;
3435
Result<std::vector<EValue>>
3536
forward(const std::vector<EValue> &input_value) const;
36-
Result<std::vector<EValue>> execute(const std::string &methodName,
37-
const std::vector<EValue> &input_value);
37+
Result<std::vector<EValue>>
38+
execute(const std::string &methodName,
39+
const std::vector<EValue> &input_value) const;
3840
Result<executorch::runtime::MethodMeta>
39-
getMethodMeta(const std::string &methodName);
41+
getMethodMeta(const std::string &methodName) const;
4042

4143
protected:
4244
// If possible, models should not use the JS runtime to keep JSI internals
@@ -49,7 +51,8 @@ class BaseModel {
4951
std::size_t memorySizeLowerBound{0};
5052

5153
private:
52-
std::vector<int32_t> getTensorShape(const executorch::aten::Tensor &tensor);
54+
std::vector<int32_t>
55+
getTensorShape(const executorch::aten::Tensor &tensor) const;
5356
};
5457
} // namespace models
5558

0 commit comments

Comments
 (0)