kinfer 0.4.2__tar.gz → 0.5.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {kinfer-0.4.2 → kinfer-0.5.1}/Cargo.toml +1 -1
- {kinfer-0.4.2 → kinfer-0.5.1}/PKG-INFO +6 -3
- kinfer-0.5.1/kinfer/export/__init__.py +3 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer/export/jax.py +13 -11
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer/export/pytorch.py +4 -14
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer/export/serialize.py +18 -27
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer/requirements.txt +5 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer/rust/Cargo.toml +3 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer/rust/src/lib.rs +3 -0
- kinfer-0.5.1/kinfer/rust/src/logger.rs +135 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer/rust/src/model.rs +112 -93
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer/rust/src/runtime.rs +12 -9
- kinfer-0.5.1/kinfer/rust/src/types.rs +86 -0
- kinfer-0.5.1/kinfer/rust_bindings/src/lib.rs +431 -0
- kinfer-0.5.1/kinfer/scripts/plot_ndjson.py +177 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer.egg-info/PKG-INFO +6 -3
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer.egg-info/SOURCES.txt +4 -3
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer.egg-info/requires.txt +5 -2
- {kinfer-0.4.2 → kinfer-0.5.1}/setup.py +1 -1
- kinfer-0.5.1/tests/test_common.py +58 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/tests/test_jax.py +38 -34
- {kinfer-0.4.2 → kinfer-0.5.1}/tests/test_pytorch.py +36 -41
- kinfer-0.4.2/kinfer/common/__init__.py +0 -0
- kinfer-0.4.2/kinfer/common/types.py +0 -12
- kinfer-0.4.2/kinfer/export/__init__.py +0 -0
- kinfer-0.4.2/kinfer/export/common.py +0 -41
- kinfer-0.4.2/kinfer/rust_bindings/src/lib.rs +0 -342
- {kinfer-0.4.2 → kinfer-0.5.1}/.cargo/config.toml +0 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/LICENSE +0 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/MANIFEST.in +0 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/README.md +0 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer/__init__.py +0 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer/py.typed +0 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer/rust_bindings/Cargo.toml +0 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer/rust_bindings/pyproject.toml +0 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer/rust_bindings/src/bin/stub_gen.rs +0 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer.egg-info/dependency_links.txt +0 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer.egg-info/not-zip-safe +0 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/kinfer.egg-info/top_level.txt +0 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/pyproject.toml +0 -0
- {kinfer-0.4.2 → kinfer-0.5.1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: kinfer
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.5.1
|
4
4
|
Summary: Tool to make it easier to run a model on a real robot
|
5
5
|
Home-page: https://github.com/kscalelabs/kinfer.git
|
6
6
|
Author: K-Scale Labs
|
@@ -10,6 +10,9 @@ License-File: LICENSE
|
|
10
10
|
Requires-Dist: onnx
|
11
11
|
Requires-Dist: onnxruntime==1.20.0
|
12
12
|
Requires-Dist: pydantic
|
13
|
+
Requires-Dist: matplotlib
|
14
|
+
Requires-Dist: numpy
|
15
|
+
Requires-Dist: pathlib
|
13
16
|
Provides-Extra: dev
|
14
17
|
Requires-Dist: black; extra == "dev"
|
15
18
|
Requires-Dist: darglint; extra == "dev"
|
@@ -21,7 +24,7 @@ Provides-Extra: pytorch
|
|
21
24
|
Requires-Dist: torch; extra == "pytorch"
|
22
25
|
Provides-Extra: jax
|
23
26
|
Requires-Dist: tensorflow; extra == "jax"
|
24
|
-
Requires-Dist: tf2onnx; extra == "jax"
|
27
|
+
Requires-Dist: tf2onnx>=1.16.0; extra == "jax"
|
25
28
|
Requires-Dist: jax; extra == "jax"
|
26
29
|
Requires-Dist: equinox; extra == "jax"
|
27
30
|
Requires-Dist: numpy<2; extra == "jax"
|
@@ -34,7 +37,7 @@ Requires-Dist: ruff; extra == "all"
|
|
34
37
|
Requires-Dist: types-tensorflow; extra == "all"
|
35
38
|
Requires-Dist: torch; extra == "all"
|
36
39
|
Requires-Dist: tensorflow; extra == "all"
|
37
|
-
Requires-Dist: tf2onnx; extra == "all"
|
40
|
+
Requires-Dist: tf2onnx>=1.16.0; extra == "all"
|
38
41
|
Requires-Dist: jax; extra == "all"
|
39
42
|
Requires-Dist: equinox; extra == "all"
|
40
43
|
Requires-Dist: numpy<2; extra == "all"
|
@@ -10,20 +10,27 @@ from jax._src.stages import Wrapped
|
|
10
10
|
from jax.experimental import jax2tf
|
11
11
|
from onnx.onnx_pb import ModelProto
|
12
12
|
|
13
|
-
from kinfer.
|
13
|
+
from kinfer.rust_bindings import PyInputType, PyModelMetadata
|
14
14
|
|
15
15
|
logger = logging.getLogger(__name__)
|
16
16
|
|
17
17
|
|
18
18
|
def export_fn(
|
19
19
|
model: Wrapped,
|
20
|
+
metadata: PyModelMetadata,
|
20
21
|
*,
|
21
|
-
num_joints: int | None = None,
|
22
|
-
num_commands: int | None = None,
|
23
|
-
carry_shape: tuple[int, ...] | None = None,
|
24
22
|
opset: int = 13,
|
25
23
|
) -> ModelProto:
|
26
|
-
"""Export a JAX function to ONNX.
|
24
|
+
"""Export a JAX function to ONNX.
|
25
|
+
|
26
|
+
Args:
|
27
|
+
model: The model to export.
|
28
|
+
metadata: The metadata for the model.
|
29
|
+
opset: The ONNX opset to use.
|
30
|
+
|
31
|
+
Returns:
|
32
|
+
The ONNX model as a `ModelProto`.
|
33
|
+
"""
|
27
34
|
if not isinstance(model, Wrapped):
|
28
35
|
raise ValueError("Model must be a Wrapped function")
|
29
36
|
|
@@ -33,12 +40,7 @@ def export_fn(
|
|
33
40
|
# Gets the dummy input tensors for exporting the model.
|
34
41
|
tf_args = []
|
35
42
|
for name in input_names:
|
36
|
-
shape = get_shape(
|
37
|
-
name,
|
38
|
-
num_joints=num_joints,
|
39
|
-
num_commands=num_commands,
|
40
|
-
carry_shape=carry_shape,
|
41
|
-
)
|
43
|
+
shape = PyInputType(name).get_shape(metadata)
|
42
44
|
tf_args.append(tf.TensorSpec(shape, tf.float32, name=name))
|
43
45
|
|
44
46
|
finalised_fn = finalise_fn(model)
|
@@ -12,23 +12,18 @@ import torch
|
|
12
12
|
from onnx.onnx_pb import ModelProto
|
13
13
|
from torch._C import FunctionSchema
|
14
14
|
|
15
|
-
from kinfer.
|
15
|
+
from kinfer.rust_bindings import PyInputType, PyModelMetadata
|
16
16
|
|
17
17
|
|
18
18
|
def export_fn(
|
19
19
|
model: torch.jit.ScriptFunction,
|
20
|
-
|
21
|
-
num_joints: int | None = None,
|
22
|
-
num_commands: int | None = None,
|
23
|
-
carry_shape: tuple[int, ...] | None = None,
|
20
|
+
metadata: PyModelMetadata,
|
24
21
|
) -> ModelProto:
|
25
22
|
"""Exports a PyTorch function to ONNX.
|
26
23
|
|
27
24
|
Args:
|
28
25
|
model: The model to export.
|
29
|
-
|
30
|
-
num_commands: The number of commands in the model.
|
31
|
-
carry_shape: The shape of the carry tensor.
|
26
|
+
metadata: The metadata for the model.
|
32
27
|
|
33
28
|
Returns:
|
34
29
|
The ONNX model as a `ModelProto`.
|
@@ -42,12 +37,7 @@ def export_fn(
|
|
42
37
|
# Gets the dummy input tensors for exporting the model.
|
43
38
|
args = []
|
44
39
|
for name in input_names:
|
45
|
-
shape = get_shape(
|
46
|
-
name,
|
47
|
-
num_joints=num_joints,
|
48
|
-
num_commands=num_commands,
|
49
|
-
carry_shape=carry_shape,
|
50
|
-
)
|
40
|
+
shape = PyInputType(name).get_shape(metadata)
|
51
41
|
args.append(torch.zeros(shape))
|
52
42
|
|
53
43
|
buffer = io.BytesIO()
|
@@ -4,34 +4,30 @@ __all__ = [
|
|
4
4
|
"pack",
|
5
5
|
]
|
6
6
|
|
7
|
-
|
8
7
|
import io
|
8
|
+
import logging
|
9
9
|
import tarfile
|
10
10
|
|
11
11
|
from onnx.onnx_pb import ModelProto
|
12
12
|
|
13
|
-
from kinfer.
|
14
|
-
|
13
|
+
from kinfer.rust_bindings import PyInputType, PyModelMetadata
|
14
|
+
|
15
|
+
logger = logging.getLogger(__name__)
|
15
16
|
|
16
17
|
|
17
18
|
def pack(
|
18
19
|
init_fn: ModelProto,
|
19
20
|
step_fn: ModelProto,
|
20
|
-
|
21
|
-
num_commands: int | None = None,
|
22
|
-
carry_shape: tuple[int, ...] | None = None,
|
21
|
+
metadata: PyModelMetadata,
|
23
22
|
) -> bytes:
|
24
23
|
"""Packs the initialization function and step function into a directory.
|
25
24
|
|
26
25
|
Args:
|
27
26
|
init_fn: The initialization function.
|
28
27
|
step_fn: The step function.
|
29
|
-
|
30
|
-
expects them to be provided.
|
31
|
-
num_commands: The number of commands in the model.
|
32
|
-
carry_shape: The shape of the carry tensor.
|
28
|
+
metadata: The metadata for the model.
|
33
29
|
"""
|
34
|
-
num_joints = len(joint_names)
|
30
|
+
num_joints = len(metadata.joint_names) # type: ignore[attr-defined]
|
35
31
|
|
36
32
|
# Checks the `init` function.
|
37
33
|
if len(init_fn.graph.input) > 0:
|
@@ -40,20 +36,21 @@ def pack(
|
|
40
36
|
raise ValueError(f"`init` function should have exactly 1 output! Got {len(init_fn.graph.output)}")
|
41
37
|
init_carry = init_fn.graph.output[0]
|
42
38
|
init_carry_shape = tuple(dim.dim_value for dim in init_carry.type.tensor_type.shape.dim)
|
43
|
-
|
44
|
-
|
39
|
+
|
40
|
+
if metadata.carry_size != init_carry_shape: # type: ignore[attr-defined]
|
41
|
+
logger.warning(
|
42
|
+
"Updating carry size from %s to %s to match the `init` function",
|
43
|
+
metadata.carry_size, # type: ignore[attr-defined]
|
44
|
+
init_carry_shape,
|
45
|
+
)
|
46
|
+
metadata.carry_size = init_carry_shape # type: ignore[attr-defined]
|
45
47
|
|
46
48
|
# Checks the `step` function.
|
47
49
|
for step_input in step_fn.graph.input:
|
48
50
|
step_input_type = step_input.type.tensor_type
|
49
51
|
shape = tuple(dim.dim_value for dim in step_input_type.shape.dim)
|
50
|
-
expected_shape = get_shape(
|
51
|
-
|
52
|
-
num_joints=num_joints,
|
53
|
-
num_commands=num_commands,
|
54
|
-
carry_shape=carry_shape,
|
55
|
-
)
|
56
|
-
if shape != expected_shape:
|
52
|
+
expected_shape = PyInputType(step_input.name).get_shape(metadata)
|
53
|
+
if shape != tuple(expected_shape):
|
57
54
|
raise ValueError(f"Expected shape {expected_shape} for input `{step_input.name}`, got {shape}")
|
58
55
|
|
59
56
|
if len(step_fn.graph.output) != 2:
|
@@ -69,12 +66,6 @@ def pack(
|
|
69
66
|
if output_carry_shape != init_carry_shape:
|
70
67
|
raise ValueError(f"Expected carry shape {init_carry_shape} for output carry, got {output_carry_shape}")
|
71
68
|
|
72
|
-
# Builds the metadata object.
|
73
|
-
metadata = Metadata(
|
74
|
-
joint_names=joint_names,
|
75
|
-
num_commands=num_commands,
|
76
|
-
)
|
77
|
-
|
78
69
|
buffer = io.BytesIO()
|
79
70
|
|
80
71
|
with tarfile.open(fileobj=buffer, mode="w:gz") as tar:
|
@@ -86,7 +77,7 @@ def pack(
|
|
86
77
|
|
87
78
|
add_file_bytes("init_fn.onnx", init_fn.SerializeToString())
|
88
79
|
add_file_bytes("step_fn.onnx", step_fn.SerializeToString())
|
89
|
-
add_file_bytes("metadata.json", metadata.
|
80
|
+
add_file_bytes("metadata.json", metadata.to_json().encode("utf-8"))
|
90
81
|
|
91
82
|
buffer.seek(0)
|
92
83
|
|
@@ -17,8 +17,11 @@ crate-type = ["cdylib", "rlib"]
|
|
17
17
|
[dependencies]
|
18
18
|
|
19
19
|
async-trait = "0.1"
|
20
|
+
chrono = "0.4.41"
|
21
|
+
crossbeam-channel = "0.5.15"
|
20
22
|
flate2 = "1.0"
|
21
23
|
futures-util = "0.3.30"
|
24
|
+
log = "0.4"
|
22
25
|
ndarray = "0.16.1"
|
23
26
|
ort = { version = "2.0.0-rc.9", features = [ "load-dynamic" ] }
|
24
27
|
serde = { version = "1.0", features = ["derive"] }
|
@@ -0,0 +1,135 @@
|
|
1
|
+
use std::{
|
2
|
+
fs::OpenOptions,
|
3
|
+
io::{BufWriter, Write},
|
4
|
+
path::{Path, PathBuf},
|
5
|
+
sync::atomic::{AtomicU64, Ordering},
|
6
|
+
thread,
|
7
|
+
};
|
8
|
+
|
9
|
+
use crossbeam_channel::{bounded, Sender};
|
10
|
+
use log::{info, warn};
|
11
|
+
use serde::Serialize;
|
12
|
+
|
13
|
+
#[derive(Serialize)]
|
14
|
+
struct NdjsonStep<'a> {
|
15
|
+
step_id: u64,
|
16
|
+
t_us: u64,
|
17
|
+
joint_angles: Option<&'a [f32]>,
|
18
|
+
joint_vels: Option<&'a [f32]>,
|
19
|
+
projected_g: Option<&'a [f32]>,
|
20
|
+
accel: Option<&'a [f32]>,
|
21
|
+
gyro: Option<&'a [f32]>,
|
22
|
+
command: Option<&'a [f32]>,
|
23
|
+
output: Option<&'a [f32]>,
|
24
|
+
}
|
25
|
+
|
26
|
+
// Channel capacity for non-blocking logging.
|
27
|
+
// ~1000 entires at 50Hz is 20 seconds of buffering.
|
28
|
+
// Warns if messages are dropped due to full buffer.
|
29
|
+
const CHANNEL_CAP: usize = 1024;
|
30
|
+
|
31
|
+
// Flush buffered writes every 100 log entries.
|
32
|
+
// At 50Hz control frequency, this flushes every 2 seconds.
|
33
|
+
const FLUSH_EVERY: u64 = 100;
|
34
|
+
|
35
|
+
pub struct StepLogger {
|
36
|
+
tx: Option<Sender<Vec<u8>>>,
|
37
|
+
worker: Option<thread::JoinHandle<()>>,
|
38
|
+
next_id: AtomicU64,
|
39
|
+
}
|
40
|
+
|
41
|
+
impl StepLogger {
|
42
|
+
pub fn new(path: impl AsRef<Path>) -> std::io::Result<Self> {
|
43
|
+
let path: PathBuf = path.as_ref().into();
|
44
|
+
if let Some(parent) = path.parent() {
|
45
|
+
std::fs::create_dir_all(parent)?;
|
46
|
+
}
|
47
|
+
info!("kinfer: logging to NDJSON: {}", path.display());
|
48
|
+
|
49
|
+
// I/O objects created here, but moved into the worker thread.
|
50
|
+
let file = OpenOptions::new().create(true).append(true).open(&path)?;
|
51
|
+
let mut bw = BufWriter::new(file);
|
52
|
+
|
53
|
+
// Bounded channel -> back-pressure capped at CHANNEL_CAP lines
|
54
|
+
let (tx, rx) = bounded::<Vec<u8>>(CHANNEL_CAP);
|
55
|
+
|
56
|
+
let worker = thread::spawn(move || {
|
57
|
+
let mut line_ctr: u64 = 0;
|
58
|
+
for msg in rx {
|
59
|
+
// drains until all senders dropped
|
60
|
+
let _ = bw.write_all(&msg);
|
61
|
+
line_ctr += 1;
|
62
|
+
if line_ctr % FLUSH_EVERY == 0 {
|
63
|
+
let _ = bw.flush();
|
64
|
+
}
|
65
|
+
}
|
66
|
+
// Final flush on graceful shutdown
|
67
|
+
let _ = bw.flush();
|
68
|
+
});
|
69
|
+
|
70
|
+
Ok(Self {
|
71
|
+
tx: Some(tx),
|
72
|
+
worker: Some(worker),
|
73
|
+
next_id: AtomicU64::new(0),
|
74
|
+
})
|
75
|
+
}
|
76
|
+
|
77
|
+
#[inline]
|
78
|
+
fn now_us() -> u128 {
|
79
|
+
std::time::SystemTime::now()
|
80
|
+
.duration_since(std::time::UNIX_EPOCH)
|
81
|
+
.unwrap()
|
82
|
+
.as_micros()
|
83
|
+
}
|
84
|
+
|
85
|
+
/// Non-blocking; drops a line if the channel is full.
|
86
|
+
#[allow(clippy::too_many_arguments)]
|
87
|
+
pub fn log_step(
|
88
|
+
&self,
|
89
|
+
joint_angles: Option<&[f32]>,
|
90
|
+
joint_vels: Option<&[f32]>,
|
91
|
+
projected_g: Option<&[f32]>,
|
92
|
+
accel: Option<&[f32]>,
|
93
|
+
gyro: Option<&[f32]>,
|
94
|
+
command: Option<&[f32]>,
|
95
|
+
output: Option<&[f32]>,
|
96
|
+
) {
|
97
|
+
let record = NdjsonStep {
|
98
|
+
step_id: self.next_id.fetch_add(1, Ordering::Relaxed),
|
99
|
+
t_us: Self::now_us() as u64,
|
100
|
+
joint_angles,
|
101
|
+
joint_vels,
|
102
|
+
projected_g,
|
103
|
+
accel,
|
104
|
+
gyro,
|
105
|
+
command,
|
106
|
+
output,
|
107
|
+
};
|
108
|
+
|
109
|
+
// Serialise directly into a Vec<u8>; then push newline and send.
|
110
|
+
if let Ok(mut line) = serde_json::to_vec(&record) {
|
111
|
+
line.push(b'\n');
|
112
|
+
if let Some(tx) = &self.tx {
|
113
|
+
if tx.try_send(line).is_err() {
|
114
|
+
warn!(
|
115
|
+
"kinfer: logging buffer full, dropped message (step_id: {})",
|
116
|
+
record.step_id
|
117
|
+
);
|
118
|
+
}
|
119
|
+
}
|
120
|
+
}
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
/// Ensure the worker drains and flushes before program exit.
|
125
|
+
impl Drop for StepLogger {
|
126
|
+
fn drop(&mut self) {
|
127
|
+
if let Some(tx) = self.tx.take() {
|
128
|
+
drop(tx); // Drop sender to close channel
|
129
|
+
}
|
130
|
+
// Wait for worker to finish
|
131
|
+
if let Some(worker) = self.worker.take() {
|
132
|
+
let _ = worker.join();
|
133
|
+
}
|
134
|
+
}
|
135
|
+
}
|