Skip to content

Commit cb1a1b4

Browse files
committed
feat/serialization: add serialization
1 parent de0eb7c commit cb1a1b4

13 files changed

+440
-23
lines changed

Cargo.toml

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ readme = "README.md"
1313
keywords = ["deep-learning", "neural-networks", "machine-learning", "framework"]
1414
license = "MIT OR Apache-2.0"
1515

16+
build = "build.rs"
17+
1618
[dependencies]
1719
collenchyma = { version = "0.0.8", default-features = false, features = ["native"] } # native feature to read/write data into tensors
1820
collenchyma-blas = { version = "0.2.0", default-features = false, features = ["native"] } # only compiles with native feature
@@ -22,10 +24,15 @@ log = "0.3.2"
2224
rand = "0.3.0"
2325
num = "0.1"
2426

25-
clippy = { version = "0.0.41", optional = true }
27+
capnp = "0.6.2"
2628

2729
timeit = "0.1.2"
2830

31+
clippy = { version = "0.0.41", optional = true }
32+
33+
[build-dependencies]
34+
capnpc = "0.6.1"
35+
2936
[dev-dependencies]
3037
env_logger = "0.3"
3138

build.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern crate capnpc;
2+
3+
fn main() {
4+
::capnpc::compile("capnp", &["capnp/leaf.capnp"]).unwrap();
5+
}

capnp/leaf.capnp

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
@0x8316e0f30c445924;
2+
3+
# The structs here try to mirror all the *Config structs as close as possible.
4+
# Before changing anything take a look at https://capnproto.org/language.html#evolving-your-protocol
5+
6+
struct Weight {
7+
name @0 :Text;
8+
tensor @1 :Tensor;
9+
}
10+
11+
struct Tensor {
12+
shape @0 :List(UInt64);
13+
data @1 :List(Float32);
14+
}
15+
16+
struct Layer {
17+
name @0 :Text;
18+
config @1 :LayerConfig;
19+
weightsData @2 :List(Weight);
20+
}
21+
22+
struct LayerConfig {
23+
name @0 :Text;
24+
layerType :union {
25+
# Common layers
26+
convolution @1 :ConvolutionConfig;
27+
linear @2 :LinearConfig;
28+
logSoftmax @3 :Void;
29+
pooling @4 :PoolingConfig;
30+
sequential @5 :SequentialConfig;
31+
softmax @6 :Void;
32+
# Activation layers
33+
relu @7 :Void;
34+
sigmoid @8 :Void;
35+
# Loss layers
36+
negativeLogLikelihood @9 :NegativeLogLikelihoodConfig;
37+
# Utility layers
38+
reshape @10 :ReshapeConfig;
39+
}
40+
41+
outputs @11 :List(Text);
42+
inputs @12 :List(Text);
43+
params @13 :List(WeightConfig);
44+
propagateDown @14 :List(Bool);
45+
}
46+
47+
# TODO: incomplete since WeightConfig isn't really used internally in Leaf.
48+
struct WeightConfig {
49+
name @0 :Text;
50+
}
51+
52+
struct ConvolutionConfig {
53+
numOutput @0 :UInt64;
54+
filterShape @1 :List(UInt64);
55+
stride @2 :List(UInt64);
56+
padding @3 :List(UInt64);
57+
}
58+
59+
struct LinearConfig {
60+
outputSize @0 :UInt64;
61+
}
62+
63+
struct PoolingConfig {
64+
mode @0 :PoolingMode;
65+
filterShape @1 :List(UInt64);
66+
stride @2 :List(UInt64);
67+
padding @3 :List(UInt64);
68+
}
69+
70+
enum PoolingMode {
71+
max @0;
72+
average @1; # not implemented yet, but we can't create a single variant enum so this is better than a meaningless "Dummy" value.
73+
}
74+
75+
struct SequentialConfig {
76+
layers @0 :List(LayerConfig);
77+
inputs @1 :List(ShapedInput);
78+
forceBackward @2 :Bool;
79+
}
80+
81+
struct ShapedInput {
82+
name @0 :Text;
83+
shape @1 :List(UInt64);
84+
}
85+
86+
struct NegativeLogLikelihoodConfig {
87+
numClasses @0 :UInt64;
88+
}
89+
90+
struct ReshapeConfig {
91+
shape @0 :List(UInt64);
92+
}

src/capnp_util.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! Provides functionality for Cap'n Proto (de)serialization.
2+
3+
pub trait CapnpWrite<'a> {
4+
/// The Builder that was autogenerated by capnp.
5+
type Builder;
6+
7+
/// Write the struct into the message that is being built by the Builder.
8+
fn write_capnp(&self, builder: &mut Self::Builder);
9+
}

0 commit comments

Comments
 (0)