Skip to content
This repository was archived by the owner on Dec 21, 2023. It is now read-only.

Commit ccc5eb9

Browse files
authored
Allow float_array.cpp to build without other dependencies (#3127)
1 parent 3b49bb7 commit ccc5eb9

File tree

11 files changed

+131
-61
lines changed

11 files changed

+131
-61
lines changed

src/ml/neural_net/float_array.cpp

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#include <cassert>
1111
#include <numeric>
1212

13-
#include <core/logging/assertions.hpp>
14-
1513
namespace turi {
1614
namespace neural_net {
1715

@@ -74,9 +72,8 @@ shared_float_array::shared_float_array(
7472
}
7573

7674
shared_float_array shared_float_array::operator[](size_t idx) const {
77-
78-
ASSERT_GT(dim_, 0);
79-
ASSERT_LT(idx, shape_[0]);
75+
assert(dim_ > 0);
76+
assert(idx < shape_[0]);
8077

8178
size_t stride = size_ / shape_[0];
8279
size_t offset = stride * idx;
@@ -99,27 +96,6 @@ std::ostream &operator<<(std::ostream &os, const float_array &arr) {
9996
return os;
10097
}
10198

102-
void shared_float_array::save(oarchive& oarc) const {
103-
// Write shape.
104-
serialize_iterator(oarc, shape(), shape() + dim(), dim());
105-
106-
// Write data.
107-
serialize_iterator(oarc, data(), data() + size(), size());
108-
}
109-
110-
void shared_float_array::load(iarchive& iarc) {
111-
// Read shape.
112-
std::vector<size_t> shape;
113-
iarc >> shape;
114-
115-
// Read data.
116-
std::vector<float> data;
117-
iarc >> data;
118-
119-
// Overwrite self with a new float_array wrapping the deserialized data.
120-
*this = wrap(std::move(data), std::move(shape));
121-
}
122-
12399
// static
124100
std::shared_ptr<float_array> shared_float_array::default_value() {
125101
// n.b. static variables should have trivial destructors

src/ml/neural_net/float_array.hpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@
44
* be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
55
*/
66

7-
#ifndef UNITY_TOOLKITS_NEURAL_NET_FLOAT_ARRAY_HPP_
8-
#define UNITY_TOOLKITS_NEURAL_NET_FLOAT_ARRAY_HPP_
7+
#pragma once
98

109
#include <cstddef>
1110
#include <future>
1211
#include <map>
1312
#include <memory>
13+
#include <ostream>
1414
#include <string>
1515
#include <vector>
1616

17-
#include <core/storage/serialization/serialization_includes.hpp>
18-
1917
namespace turi {
2018
namespace neural_net {
2119

@@ -160,10 +158,6 @@ class shared_float_array: public float_array {
160158

161159
// TODO: Operations such as reshape, slice, etc.?
162160

163-
// Serialization.
164-
void save(oarchive& oarc) const;
165-
void load(iarchive& iarc);
166-
167161
protected:
168162
shared_float_array(std::shared_ptr<float_array> impl, size_t offset,
169163
const size_t* shape, size_t dim);
@@ -211,5 +205,3 @@ std::ostream &operator<<(std::ostream &out, const float_array &arr);
211205

212206
} // namespace neural_net
213207
} // namespace turi
214-
215-
#endif // UNITY_TOOLKITS_NEURAL_NET_FLOAT_ARRAY_HPP_

src/ml/neural_net/style_transfer/mps_style_transfer_backend.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
* https://opensource.org/licenses/BSD-3-Clause
66
*/
77

8-
#ifndef TURI_STYLE_TRANSFER_MPS_STYLE_TRANSFER_BACKEND_H_
9-
#define TURI_STYLE_TRANSFER_MPS_STYLE_TRANSFER_BACKEND_H_
8+
#pragma once
109

1110
#include <functional>
1211
#include <map>
1312
#include <memory>
1413

14+
#include <core/export.hpp>
1515
#include <ml/neural_net/float_array.hpp>
1616
#include <ml/neural_net/model_backend.hpp>
1717
#include <ml/neural_net/mps_command_queue.hpp>
@@ -49,5 +49,3 @@ class EXPORT mps_style_transfer : public turi::neural_net::model_backend {
4949
} // namespace turi
5050

5151
#endif // #ifdef HAS_MACOS_10_15
52-
53-
#endif

src/ml/neural_net/style_transfer/mps_style_transfer_backend.mm

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66

77
#ifdef HAS_MACOS_10_15
88

9-
#import <ml/neural_net/style_transfer/mps_style_transfer_backend.hpp>
10-
#import <ml/neural_net/style_transfer/mps_style_transfer.h>
9+
#include <ml/neural_net/style_transfer/mps_style_transfer_backend.hpp>
10+
11+
#include <numeric>
12+
13+
#include <core/logging/logger_includes.hpp>
14+
#include <ml/neural_net/model_spec.hpp>
1115

12-
#import <ml/neural_net/model_spec.hpp>
1316
#import <ml/neural_net/mps_device_manager.h>
1417
#import <ml/neural_net/mps_utils.h>
15-
18+
#import <ml/neural_net/style_transfer/mps_style_transfer.h>
1619
#import <ml/neural_net/style_transfer/mps_style_transfer_weights.h>
1720

18-
#include <numeric>
19-
2021
using namespace turi::neural_net;
2122

2223
@interface TCMPSStyleTransferHelpers:NSObject
@@ -283,4 +284,4 @@ float_array_map convert_weights_mps_coreml(const float_array_map &mps_weights) {
283284
} // namespace style_transfer
284285
} // namespace turi
285286

286-
#endif // #ifdef HAS_MACOS_10_15
287+
#endif // #ifdef HAS_MACOS_10_15

src/toolkits/activity_classification/activity_classifier.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <timer/timer.hpp>
2020
#include <toolkits/coreml_export/neural_net_models_exporter.hpp>
2121
#include <toolkits/evaluation/metrics.hpp>
22+
#include <toolkits/util/float_array_serialization.hpp>
2223
#include <toolkits/util/training_utils.hpp>
2324

2425
namespace turi {
@@ -109,16 +110,16 @@ void activity_classifier::save_impl(oarchive& oarc) const {
109110
variant_deep_save(state, oarc);
110111

111112
// Save neural net weights.
112-
oarc << read_model_spec()->export_params_view();
113+
save_float_array_map(read_model_spec()->export_params_view(), oarc);
113114
}
114115

115116
void activity_classifier::load_version(iarchive& iarc, size_t version) {
116117
// Load model attributes.
117118
variant_deep_load(state, iarc);
118119

119120
// Load neural net weights.
120-
float_array_map nn_params;
121-
iarc >> nn_params;
121+
float_array_map nn_params = load_float_array_map(iarc);
122+
122123
bool use_random_init = false;
123124
nn_spec_ = init_model(use_random_init);
124125
nn_spec_->update_params(nn_params);

src/toolkits/drawing_classifier/drawing_classifier.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <toolkits/coreml_export/neural_net_models_exporter.hpp>
2121
#include <toolkits/evaluation/metrics.hpp>
2222
#include <toolkits/supervised_learning/automatic_model_creation.hpp>
23+
#include <toolkits/util/float_array_serialization.hpp>
2324
#include <toolkits/util/training_utils.hpp>
2425

2526
#include <toolkits/drawing_classifier/data_preparation.hpp>
@@ -87,7 +88,7 @@ void drawing_classifier::save_impl(oarchive& oarc) const {
8788
variant_deep_save(state, oarc);
8889

8990
// Save neural net weights.
90-
oarc << nn_spec_->export_params_view();
91+
save_float_array_map(nn_spec_->export_params_view(), oarc);
9192
}
9293

9394
void drawing_classifier::load_version(iarchive& iarc, size_t version) {
@@ -96,8 +97,7 @@ void drawing_classifier::load_version(iarchive& iarc, size_t version) {
9697
variant_deep_load(state, iarc);
9798

9899
// Load neural net weights.
99-
float_array_map nn_params;
100-
iarc >> nn_params;
100+
float_array_map nn_params = load_float_array_map(iarc);
101101

102102
nn_spec_ = init_model(false);
103103
nn_spec_->update_params(nn_params);

src/toolkits/object_detection/od_serialization.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <cstdio>
1212

1313
#include <toolkits/object_detection/od_yolo.hpp>
14+
#include <toolkits/util/float_array_serialization.hpp>
1415

1516
#include <toolkits/coreml_export/mlmodel_include.hpp>
1617

@@ -48,7 +49,7 @@ void _save_impl(oarchive& oarc,
4849
variant_deep_save(state, oarc);
4950

5051
// Save neural net weights.
51-
oarc << weights;
52+
save_float_array_map(weights, oarc);
5253
}
5354

5455
void _load_version(iarchive& iarc, size_t version,
@@ -58,7 +59,7 @@ void _load_version(iarchive& iarc, size_t version,
5859
variant_deep_load(*state, iarc);
5960

6061
// Load neural net weights.
61-
iarc >> *weights;
62+
*weights = load_float_array_map(iarc);
6263
}
6364

6465
void init_darknet_yolo(model_spec& nn_spec, size_t num_classes,

src/toolkits/style_transfer/style_transfer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <model_server/lib/variant_deep_serialize.hpp>
1919
#include <toolkits/style_transfer/st_resnet16_model_trainer.hpp>
2020
#include <toolkits/style_transfer/style_transfer_model_definition.hpp>
21+
#include <toolkits/util/float_array_serialization.hpp>
2122
#include <toolkits/util/training_utils.hpp>
2223

2324
#ifdef HAS_MPS
@@ -328,14 +329,13 @@ size_t style_transfer::get_version() const { return STYLE_TRANSFER_VERSION; }
328329

329330
void style_transfer::save_impl(oarchive& oarc) const {
330331
variant_deep_save(state, oarc);
331-
oarc << read_checkpoint().weights();
332+
save_float_array_map(read_checkpoint().weights(), oarc);
332333
}
333334

334335
void style_transfer::load_version(iarchive& iarc, size_t version) {
335336
variant_deep_load(state, iarc);
336337

337-
float_array_map nn_params;
338-
iarc >> nn_params;
338+
float_array_map nn_params = load_float_array_map(iarc);
339339
checkpoint_ = load_checkpoint(nn_params);
340340
}
341341

src/toolkits/util/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ project(Turi)
22

33
make_library(unity_util OBJECT
44
SOURCES
5-
precision_recall.cpp
5+
class_registrations.cpp
66
data_generators.cpp
7+
float_array_serialization.cpp
78
indexed_sframe_tools.cpp
8-
sframe_utils.cpp
9-
class_registrations.cpp
9+
precision_recall.cpp
1010
random_sframe_generation.cpp
11+
sframe_utils.cpp
1112
REQUIRES
1213
unity_core
14+
unity_neural_net
1315
)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* Copyright © 2020 Apple Inc. All rights reserved.
2+
*
3+
* Use of this source code is governed by a BSD-3-clause license that can
4+
* be found in the LICENSE.txt file or at
5+
* https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
#include <toolkits/util/float_array_serialization.hpp>
8+
9+
namespace turi {
10+
11+
using neural_net::float_array_map;
12+
using neural_net::shared_float_array;
13+
14+
class float_array_serialization_wrapper {
15+
public:
16+
float_array_serialization_wrapper() = default;
17+
explicit float_array_serialization_wrapper(shared_float_array array)
18+
: impl_(std::move(array)) {}
19+
20+
const shared_float_array& get() const { return impl_; }
21+
22+
void save(oarchive& oarc) const {
23+
// Write shape.
24+
serialize_iterator(oarc, impl_.shape(), impl_.shape() + impl_.dim(),
25+
impl_.dim());
26+
27+
// Write data.
28+
serialize_iterator(oarc, impl_.data(), impl_.data() + impl_.size(),
29+
impl_.size());
30+
}
31+
32+
void load(iarchive& iarc) {
33+
// Read shape.
34+
std::vector<size_t> shape;
35+
iarc >> shape;
36+
37+
// Read data.
38+
std::vector<float> data;
39+
iarc >> data;
40+
41+
// Overwrite self with a new float_array wrapping the deserialized data.
42+
impl_ = shared_float_array::wrap(std::move(data), std::move(shape));
43+
}
44+
45+
private:
46+
shared_float_array impl_;
47+
};
48+
49+
void save_float_array_map(const float_array_map& weights, oarchive& oarc) {
50+
// Wrap each shared_float_array in weights in a wrapper that knows how to
51+
// write itself to the oarchive.
52+
std::map<std::string, float_array_serialization_wrapper> wrapped_weights;
53+
for (const auto& key_value : weights) {
54+
wrapped_weights[key_value.first] =
55+
float_array_serialization_wrapper(key_value.second);
56+
}
57+
58+
oarc << wrapped_weights;
59+
}
60+
61+
float_array_map load_float_array_map(iarchive& iarc) {
62+
// Read the iarchive into wrappers around the underlying weights.
63+
std::map<std::string, float_array_serialization_wrapper> wrapped_weights;
64+
iarc >> wrapped_weights;
65+
66+
// Obtain direct references to the underlying weights.
67+
float_array_map weights;
68+
for (const auto& key_value : wrapped_weights) {
69+
weights[key_value.first] = key_value.second.get();
70+
}
71+
72+
return weights;
73+
}
74+
75+
} // namespace turi

0 commit comments

Comments
 (0)