agent-manifesto-kit 1.6.0 → 1.8.0
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.
- package/README.md +49 -0
- package/collection/NOTICE.md +8 -0
- package/collection/skills/astro-preview/SKILL.md +60 -0
- package/collection/skills/kimodo-motion/SKILL.md +101 -0
- package/collection/skills/kimodo-motion/agents/openai.yaml +4 -0
- package/collection/skills/kimodo-motion/patches/kimodo-cpp-keyposes.patch +398 -0
- package/collection/skills/kimodo-motion/scripts/kimodo_motion.py +459 -0
- package/collection/skills/kimodo-motion/scripts/kimodo_retarget.py +280 -0
- package/collection/skills/kimodo-motion/tests/blender_roundtrip.py +105 -0
- package/collection/skills/kimodo-motion/tests/test_kimodo_motion.py +183 -0
- package/collection/skills/roadmap/SKILL.md +49 -0
- package/collection/skills/roadmap/agents/openai.yaml +4 -0
- package/collection/skills/roadmap/assets/post-commit.sh +33 -0
- package/collection/skills/roadmap/assets/template.html +470 -0
- package/collection/skills/roadmap/references/format.md +51 -0
- package/collection/skills/roadmap/scripts/roadmap.py +852 -0
- package/collection/skills/roadmap/scripts/test_roadmap.py +578 -0
- package/collection/skills/self-reflection/SKILL.md +121 -0
- package/collection/skills/self-reflection/agents/openai.yaml +4 -0
- package/collection/skills/self-reflection/assets/report-template.json +8 -0
- package/collection/skills/self-reflection/assets/report.html +64 -0
- package/collection/skills/self-reflection/references/report-format.md +42 -0
- package/collection/skills/self-reflection/scripts/export_claude.py +59 -0
- package/collection/skills/self-reflection/scripts/reflect.py +320 -0
- package/collection/skills/self-reflection/scripts/report.py +322 -0
- package/collection/skills/self-reflection/tests/test_export_claude.py +83 -0
- package/collection/skills/self-reflection/tests/test_reflect.py +189 -0
- package/collection/skills/self-reflection/tests/test_report.py +250 -0
- package/package.json +1 -1
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
diff --git a/README.md b/README.md
|
|
2
|
+
index d048b43..b3b6821 100644
|
|
3
|
+
--- a/README.md
|
|
4
|
+
+++ b/README.md
|
|
5
|
+
@@ -24,9 +24,14 @@ default; set `KIMODO_TEXT_LAYER_CHUNK=1..32` to tune VRAM use.
|
|
6
|
+
Included: checked GGUF loading, safetensors conversion, DDIM sampling, C/C++
|
|
7
|
+
APIs, conditioned multi-prompt transitions, CPU/Vulkan parity tests,
|
|
8
|
+
skeleton-only GLB export, selective LLM2Vec quantisation, divergence reporting,
|
|
9
|
+
-and local generation/comparison viewers. General constraint input, 77-joint
|
|
10
|
+
-SOMA expansion, skinned-mesh GLB export, and motion-denoiser quantisation are
|
|
11
|
+
-not implemented yet.
|
|
12
|
+
+and local generation/comparison viewers. Key poses: upstream's constraints
|
|
13
|
+
+JSON types `fullbody`, `end-effector` and `left/right-hand/foot`
|
|
14
|
+
+(30-joint axis-angle `local_joints_rot`, `root_positions`, optional
|
|
15
|
+
+`smooth_root_2d`) through `kmd-generate ... --constraints FILE` and
|
|
16
|
+
+`model::generate_text_keyposes`; the constraint CFG weight is
|
|
17
|
+
+`KIMODO_CONSTRAINT_CFG` (default 2). Root 2D and SOMA77 constraints, upstream's motion-correction post-processing, 77-joint SOMA
|
|
18
|
+
+expansion, skinned-mesh GLB export, and motion-denoiser quantisation are not
|
|
19
|
+
+implemented yet.
|
|
20
|
+
|
|
21
|
+
## Build and test on Linux
|
|
22
|
+
|
|
23
|
+
diff --git a/include/kimodo/kimodo.hpp b/include/kimodo/kimodo.hpp
|
|
24
|
+
index e774e5a..9dc5077 100644
|
|
25
|
+
--- a/include/kimodo/kimodo.hpp
|
|
26
|
+
+++ b/include/kimodo/kimodo.hpp
|
|
27
|
+
@@ -27,6 +27,22 @@ struct prompt_segment {
|
|
28
|
+
unsigned frames = 0;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
+// One upstream key-pose constraint on a frame: every joint's local rotation
|
|
32
|
+
+// as axis-angle radians [joints*3] and the root (hips) position [3] in
|
|
33
|
+
+// metres, Y up, XZ relative to the canonical origin. As upstream, every key
|
|
34
|
+
+// holds the root height, path and heading; `fullbody` (no end_effectors)
|
|
35
|
+
+// adds every joint's FK position, not rotations; `end-effector` (names
|
|
36
|
+
+// LeftHand, RightHand, LeftFoot, RightFoot, Hips; SOMA30 only) adds each
|
|
37
|
+
+// chain's positions and its first joint's global rotation.
|
|
38
|
+
+struct pose_key {
|
|
39
|
+
+ unsigned frame = 0;
|
|
40
|
+
+ std::vector<float> local_axis_angle;
|
|
41
|
+
+ std::array<float, 3> root_position{};
|
|
42
|
+
+ bool has_smooth_root_2d = false;
|
|
43
|
+
+ std::array<float, 2> smooth_root_2d{}; // [x, z]; default: the root's x, z
|
|
44
|
+
+ std::vector<std::string> end_effectors;
|
|
45
|
+
+};
|
|
46
|
+
+
|
|
47
|
+
class KIMODO_API model {
|
|
48
|
+
public:
|
|
49
|
+
static std::expected<std::unique_ptr<model>, std::string> load(
|
|
50
|
+
@@ -38,6 +54,12 @@ public:
|
|
51
|
+
std::expected<motion_data, std::string> generate_text(
|
|
52
|
+
std::string_view utf8_prompt, unsigned frames, unsigned steps, std::uint64_t seed,
|
|
53
|
+
float text_cfg, float constraint_cfg) const;
|
|
54
|
+
+ // Text plus key poses (separated CFG, constraint row as upstream).
|
|
55
|
+
+ // The first heading is the frame-0 key's heading, else 0 (facing +Z).
|
|
56
|
+
+ std::expected<motion_data, std::string> generate_text_keyposes(
|
|
57
|
+
+ std::string_view utf8_prompt, std::span<const pose_key> keys,
|
|
58
|
+
+ unsigned frames, unsigned steps, std::uint64_t seed,
|
|
59
|
+
+ float text_cfg, float constraint_cfg) const;
|
|
60
|
+
std::expected<motion_data, std::string> generate_text_sequence(
|
|
61
|
+
std::span<const prompt_segment> segments, unsigned transition_frames,
|
|
62
|
+
unsigned steps, std::uint64_t seed, float text_cfg, float constraint_cfg) const;
|
|
63
|
+
diff --git a/src/denoiser.hpp b/src/denoiser.hpp
|
|
64
|
+
index 8749607..0c0ac11 100644
|
|
65
|
+
--- a/src/denoiser.hpp
|
|
66
|
+
+++ b/src/denoiser.hpp
|
|
67
|
+
@@ -8,6 +8,7 @@
|
|
68
|
+
|
|
69
|
+
namespace kimodo::detail {
|
|
70
|
+
class ggml_motion_weights;
|
|
71
|
+
+struct skeleton_spec;
|
|
72
|
+
|
|
73
|
+
// One sequence segment with an already-encoded text condition and caller-
|
|
74
|
+
// supplied initial noise. Continuation noise includes its transition prefix.
|
|
75
|
+
@@ -70,6 +71,16 @@ std::expected<std::vector<float>, std::string> sample_motion_sequence_from_noise
|
|
76
|
+
const ggml_motion_weights &weights, std::span<const sampled_sequence_segment> segments,
|
|
77
|
+
unsigned transition_frames, unsigned steps, float text_weight, float constraint_weight);
|
|
78
|
+
|
|
79
|
+
+// Upstream Full-body/EndEffectorConstraintSet + KimodoMotionRep.create_conditions
|
|
80
|
+
+// for one key pose: local axis-angle rotations [J*3] and the root position go
|
|
81
|
+
+// through FK into the raw (unnormalized) feature row [motion_dim]; the mask
|
|
82
|
+
+// gains the smooth root and heading, the positions of `positions` (empty:
|
|
83
|
+
+// every joint; GLOBAL: subtract the frame's final smooth root x/z after all
|
|
84
|
+
+// its keys) and the global 6D rotations of `rotations`. Returns the heading.
|
|
85
|
+
+float pose_condition(const skeleton_spec &skeleton, std::span<const float> local_axis_angle,
|
|
86
|
+
+ const float root[3], const float *smooth_root_2d, std::span<const unsigned> positions,
|
|
87
|
+
+ std::span<const unsigned> rotations, float *value, float *mask);
|
|
88
|
+
+
|
|
89
|
+
// Build the exact condition consumed by the next `_multiprompt` DDIM run.
|
|
90
|
+
// Exposed for the raw fixture test as well as the runtime orchestrator.
|
|
91
|
+
std::expected<sequence_transition, std::string> prepare_sequence_transition(
|
|
92
|
+
diff --git a/src/generate.cpp b/src/generate.cpp
|
|
93
|
+
index 132f88e..c3d3632 100644
|
|
94
|
+
--- a/src/generate.cpp
|
|
95
|
+
+++ b/src/generate.cpp
|
|
96
|
+
@@ -35,6 +35,132 @@ std::vector<std::string> split_fields(const std::string &line) {
|
|
97
|
+
return fields;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
+// Just enough JSON for upstream's constraints file (`kimodo --constraints`):
|
|
101
|
+
+// a list of objects with numbers, strings and nested lists.
|
|
102
|
+
+struct json {
|
|
103
|
+
+ enum kind_t { null, number, string, array, object } kind = null;
|
|
104
|
+
+ double num = 0;
|
|
105
|
+
+ std::string str;
|
|
106
|
+
+ std::vector<json> items;
|
|
107
|
+
+ std::vector<std::pair<std::string, json>> fields;
|
|
108
|
+
+ const json *get(std::string_view key) const {
|
|
109
|
+
+ for (const auto &[name, value] : fields) if (name == key) return &value;
|
|
110
|
+
+ return nullptr;
|
|
111
|
+
+ }
|
|
112
|
+
+};
|
|
113
|
+
+
|
|
114
|
+
+struct json_parser {
|
|
115
|
+
+ std::string_view text;
|
|
116
|
+
+ size_t at = 0;
|
|
117
|
+
+ void space() { while (at < text.size() && std::isspace(static_cast<unsigned char>(text[at]))) ++at; }
|
|
118
|
+
+ [[noreturn]] void fail(const char *what) { throw std::runtime_error("constraints JSON: " + std::string(what) + " at byte " + std::to_string(at)); }
|
|
119
|
+
+ bool eat(char c) { space(); if (at < text.size() && text[at] == c) { ++at; return true; } return false; }
|
|
120
|
+
+ std::string string() {
|
|
121
|
+
+ if (!eat('"')) fail("expected a string");
|
|
122
|
+
+ std::string out;
|
|
123
|
+
+ while (at < text.size() && text[at] != '"') { if (text[at] == '\\') ++at; if (at < text.size()) out += text[at++]; }
|
|
124
|
+
+ if (!eat('"')) fail("unterminated string");
|
|
125
|
+
+ return out;
|
|
126
|
+
+ }
|
|
127
|
+
+ json value() {
|
|
128
|
+
+ space();
|
|
129
|
+
+ json v;
|
|
130
|
+
+ if (at >= text.size()) fail("unexpected end");
|
|
131
|
+
+ const char c = text[at];
|
|
132
|
+
+ if (c == '{') {
|
|
133
|
+
+ ++at; v.kind = json::object;
|
|
134
|
+
+ if (eat('}')) return v;
|
|
135
|
+
+ do { auto key = string(); if (!eat(':')) fail("expected ':'"); v.fields.emplace_back(key, value()); } while (eat(','));
|
|
136
|
+
+ if (!eat('}')) fail("expected '}'");
|
|
137
|
+
+ } else if (c == '[') {
|
|
138
|
+
+ ++at; v.kind = json::array;
|
|
139
|
+
+ if (eat(']')) return v;
|
|
140
|
+
+ do v.items.push_back(value()); while (eat(','));
|
|
141
|
+
+ if (!eat(']')) fail("expected ']'");
|
|
142
|
+
+ } else if (c == '"') {
|
|
143
|
+
+ v.kind = json::string; v.str = string();
|
|
144
|
+
+ } else if (text.substr(at, 4) == "null") {
|
|
145
|
+
+ at += 4;
|
|
146
|
+
+ } else {
|
|
147
|
+
+ const std::string rest(text.substr(at, 64));
|
|
148
|
+
+ size_t used = 0;
|
|
149
|
+
+ try { v.num = std::stod(rest, &used); } catch (const std::exception &) { fail("expected a value"); }
|
|
150
|
+
+ v.kind = json::number; at += used;
|
|
151
|
+
+ }
|
|
152
|
+
+ return v;
|
|
153
|
+
+ }
|
|
154
|
+
+};
|
|
155
|
+
+
|
|
156
|
+
+std::vector<float> numbers(const json *value, const char *name) {
|
|
157
|
+
+ if (!value) throw std::runtime_error(std::string("constraint lacks ") + name);
|
|
158
|
+
+ std::vector<float> out;
|
|
159
|
+
+ auto walk = [&](auto &self, const json &v) -> void {
|
|
160
|
+
+ if (v.kind == json::number) out.push_back(static_cast<float>(v.num));
|
|
161
|
+
+ else if (v.kind == json::array) for (const auto &item : v.items) self(self, item);
|
|
162
|
+
+ else throw std::runtime_error(std::string("constraint field ") + name + " must hold numbers");
|
|
163
|
+
+ };
|
|
164
|
+
+ walk(walk, *value);
|
|
165
|
+
+ return out;
|
|
166
|
+
+}
|
|
167
|
+
+
|
|
168
|
+
+// Upstream key-pose entries: `fullbody`, `end-effector` (joint_names) and its
|
|
169
|
+
+// shorthands left-hand/right-hand/left-foot/right-foot, each with
|
|
170
|
+
+// frame_indices [T], local_joints_rot [T,J,3] axis-angle, root_positions [T,3]
|
|
171
|
+
+// and optional smooth_root_2d [T,2] (root2d is not read yet). The joint count
|
|
172
|
+
+// comes from the data; the model checks it against its skeleton.
|
|
173
|
+
+std::vector<kimodo::pose_key> read_keyposes(const std::filesystem::path &path) {
|
|
174
|
+
+ std::ifstream file(path);
|
|
175
|
+
+ const std::string text{std::istreambuf_iterator<char>(file), {}};
|
|
176
|
+
+ if (!file && text.empty()) throw std::runtime_error("cannot read constraints " + path.string());
|
|
177
|
+
+ json_parser parser{text};
|
|
178
|
+
+ const json root = parser.value();
|
|
179
|
+
+ if (root.kind != json::array) throw std::runtime_error("constraints JSON must be a list");
|
|
180
|
+
+ std::vector<kimodo::pose_key> keys;
|
|
181
|
+
+ for (const auto &item : root.items) {
|
|
182
|
+
+ const json *type = item.get("type");
|
|
183
|
+
+ const std::string kind = type ? type->str : std::string();
|
|
184
|
+
+ std::vector<std::string> end_effectors;
|
|
185
|
+
+ if (kind == "left-hand") end_effectors = {"LeftHand"};
|
|
186
|
+
+ else if (kind == "right-hand") end_effectors = {"RightHand"};
|
|
187
|
+
+ else if (kind == "left-foot") end_effectors = {"LeftFoot"};
|
|
188
|
+
+ else if (kind == "right-foot") end_effectors = {"RightFoot"};
|
|
189
|
+
+ else if (kind == "end-effector") {
|
|
190
|
+
+ const json *names = item.get("joint_names");
|
|
191
|
+
+ if (!names || names->kind != json::array || names->items.empty()) throw std::runtime_error("end-effector lacks joint_names");
|
|
192
|
+
+ for (const auto &name : names->items) end_effectors.push_back(name.str);
|
|
193
|
+
+ } else if (kind != "fullbody")
|
|
194
|
+
+ throw std::runtime_error("unsupported constraint type '" + kind + "' (fullbody, end-effector, left/right-hand/foot)");
|
|
195
|
+
+ const auto frames = numbers(item.get("frame_indices"), "frame_indices");
|
|
196
|
+
+ const auto rotations = numbers(item.get("local_joints_rot"), "local_joints_rot");
|
|
197
|
+
+ const auto roots = numbers(item.get("root_positions"), "root_positions");
|
|
198
|
+
+ const json *smooth = item.get("smooth_root_2d");
|
|
199
|
+
+ const auto smooth_values = smooth && smooth->kind != json::null ? numbers(smooth, "smooth_root_2d") : std::vector<float>{};
|
|
200
|
+
+ const size_t count = frames.size();
|
|
201
|
+
+ if (!count || rotations.size() % (count*3)) throw std::runtime_error("local_joints_rot must be [T,J,3]");
|
|
202
|
+
+ const size_t joints = rotations.size() / (count*3);
|
|
203
|
+
+ if (roots.size() != count*3 || (!smooth_values.empty() && smooth_values.size() != count*2))
|
|
204
|
+
+ throw std::runtime_error("root_positions / smooth_root_2d do not match frame_indices");
|
|
205
|
+
+ for (size_t t = 0; t < count; ++t) {
|
|
206
|
+
+ if (frames[t] < 0) throw std::runtime_error("negative frame index");
|
|
207
|
+
+ kimodo::pose_key key;
|
|
208
|
+
+ key.frame = static_cast<unsigned>(frames[t]);
|
|
209
|
+
+ key.end_effectors = end_effectors;
|
|
210
|
+
+ key.local_axis_angle.assign(rotations.begin() + static_cast<std::ptrdiff_t>(t*joints*3),
|
|
211
|
+
+ rotations.begin() + static_cast<std::ptrdiff_t>((t+1)*joints*3));
|
|
212
|
+
+ key.root_position = {roots[t*3], roots[t*3+1], roots[t*3+2]};
|
|
213
|
+
+ if (!smooth_values.empty()) { key.has_smooth_root_2d = true; key.smooth_root_2d = {smooth_values[t*2], smooth_values[t*2+1]}; }
|
|
214
|
+
+ keys.push_back(std::move(key));
|
|
215
|
+
+ }
|
|
216
|
+
+ }
|
|
217
|
+
+ return keys;
|
|
218
|
+
+}
|
|
219
|
+
+
|
|
220
|
+
+// Constraint CFG weight for key poses: KIMODO_CONSTRAINT_CFG, default 2 as upstream.
|
|
221
|
+
+float constraint_cfg() {
|
|
222
|
+
+ const char *value = std::getenv("KIMODO_CONSTRAINT_CFG");
|
|
223
|
+
+ return value ? std::stof(value) : 2.F;
|
|
224
|
+
+}
|
|
225
|
+
+
|
|
226
|
+
std::string protocol_error(std::string message) {
|
|
227
|
+
for (char &c : message) if (c == '\n' || c == '\r' || c == '\t') c = ' ';
|
|
228
|
+
return message;
|
|
229
|
+
@@ -91,8 +217,9 @@ int main(int argc, char **argv) try {
|
|
230
|
+
std::cout << "generated " << motion->frames << " frames with " << motion->joints << " joints\n";
|
|
231
|
+
return 0;
|
|
232
|
+
}
|
|
233
|
+
- if (argc != 8) {
|
|
234
|
+
- std::cerr << "usage: " << argv[0] << " MOTION.gguf TEXT_BUNDLE PROMPT.txt FRAMES STEPS SEED OUTPUT_DIR\n"
|
|
235
|
+
+ const bool keyposes = argc == 10 && std::string_view(argv[8]) == "--constraints";
|
|
236
|
+
+ if (argc != 8 && !keyposes) {
|
|
237
|
+
+ std::cerr << "usage: " << argv[0] << " MOTION.gguf TEXT_BUNDLE PROMPT.txt FRAMES STEPS SEED OUTPUT_DIR [--constraints CONSTRAINTS.json]\n"
|
|
238
|
+
<< " or: " << argv[0] << " MOTION.gguf TEXT_BUNDLE --sequence TRANSITION STEPS SEED OUTPUT_DIR FRAME PROMPT.txt [FRAME PROMPT.txt ...]\n";
|
|
239
|
+
return 2;
|
|
240
|
+
}
|
|
241
|
+
@@ -104,7 +231,9 @@ int main(int argc, char **argv) try {
|
|
242
|
+
const auto seed = static_cast<std::uint64_t>(std::stoull(argv[6]));
|
|
243
|
+
auto model = kimodo::model::load(argv[1], argv[2]);
|
|
244
|
+
if (!model) throw std::runtime_error(model.error());
|
|
245
|
+
- auto motion = (*model)->generate_text(prompt, frames, steps, seed, 2.F, 2.F);
|
|
246
|
+
+ auto motion = keyposes
|
|
247
|
+
+ ? (*model)->generate_text_keyposes(prompt, read_keyposes(argv[9]), frames, steps, seed, 2.F, constraint_cfg())
|
|
248
|
+
+ : (*model)->generate_text(prompt, frames, steps, seed, 2.F, 2.F);
|
|
249
|
+
if (!motion) throw std::runtime_error(motion.error());
|
|
250
|
+
write_motion(argv[7], *motion);
|
|
251
|
+
std::cout << "generated " << motion->frames << " frames with " << motion->joints << " joints\n";
|
|
252
|
+
diff --git a/src/model.cpp b/src/model.cpp
|
|
253
|
+
index 91d2ec0..4f42b56 100644
|
|
254
|
+
--- a/src/model.cpp
|
|
255
|
+
+++ b/src/model.cpp
|
|
256
|
+
@@ -117,6 +117,98 @@ std::expected<motion_data, std::string> model::generate_embedding(
|
|
257
|
+
#endif
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
+std::expected<motion_data, std::string> model::generate_text_keyposes(
|
|
261
|
+
+ std::string_view utf8_prompt, std::span<const pose_key> keys, unsigned frames, unsigned steps,
|
|
262
|
+
+ std::uint64_t seed, float text_cfg, float constraint_cfg) const {
|
|
263
|
+
+#ifdef KIMODO_HAVE_GGML
|
|
264
|
+
+ if (!impl_->text) return std::unexpected("model was loaded without a native text bundle");
|
|
265
|
+
+ if (frames < 2 || frames > 10000) return std::unexpected("frames must be in 2..10000");
|
|
266
|
+
+ if (steps == 0 || steps > 1000) return std::unexpected("diffusion_steps must be in 1..1000");
|
|
267
|
+
+ if (!std::isfinite(text_cfg) || !std::isfinite(constraint_cfg)) return std::unexpected("CFG weights must be finite");
|
|
268
|
+
+ const auto &s = *impl_->skeleton;
|
|
269
|
+
+ const size_t D = s.motion_dim(), J = s.joints(), body = s.body_dim();
|
|
270
|
+
+ for (const auto &key : keys) {
|
|
271
|
+
+ if (key.frame >= frames) return std::unexpected("key pose frame outside the clip");
|
|
272
|
+
+ if (key.local_axis_angle.size() != J*3)
|
|
273
|
+
+ return std::unexpected("key pose needs " + std::to_string(J) + " joint rotations");
|
|
274
|
+
+ for (float v : key.local_axis_angle) if (!std::isfinite(v)) return std::unexpected("key pose rotation is not finite");
|
|
275
|
+
+ for (float v : key.root_position) if (!std::isfinite(v)) return std::unexpected("key pose root is not finite");
|
|
276
|
+
+ if (!key.end_effectors.empty() && s.key != "soma30") return std::unexpected("end-effector keys need the SOMA30 model");
|
|
277
|
+
+ }
|
|
278
|
+
+ // upstream SOMASkeleton30.expand_joint_names: chain positions, the chain's first joint rotated
|
|
279
|
+
+ auto chains = [&](const pose_key &key, std::vector<unsigned> &positions, std::vector<unsigned> &rotations) -> std::expected<void, std::string> {
|
|
280
|
+
+ for (const auto &name : key.end_effectors) {
|
|
281
|
+
+ std::vector<std::string_view> chain;
|
|
282
|
+
+ if (name == "LeftHand") chain = {"LeftHand", "LeftHandMiddleEnd"};
|
|
283
|
+
+ else if (name == "RightHand") chain = {"RightHand", "RightHandMiddleEnd"};
|
|
284
|
+
+ else if (name == "LeftFoot") chain = {"LeftFoot", "LeftToeBase"};
|
|
285
|
+
+ else if (name == "RightFoot") chain = {"RightFoot", "RightToeBase"};
|
|
286
|
+
+ else if (name == "Hips") chain = {"Hips"};
|
|
287
|
+
+ else return std::unexpected("unknown end effector " + name);
|
|
288
|
+
+ for (size_t i = 0; i < chain.size(); ++i) {
|
|
289
|
+
+ const auto found = std::find(s.names.begin(), s.names.end(), chain[i]);
|
|
290
|
+
+ const auto joint = static_cast<unsigned>(found - s.names.begin());
|
|
291
|
+
+ positions.push_back(joint);
|
|
292
|
+
+ if (!i) rotations.push_back(joint);
|
|
293
|
+
+ }
|
|
294
|
+
+ positions.push_back(0);
|
|
295
|
+
+ }
|
|
296
|
+
+ return {};
|
|
297
|
+
+ };
|
|
298
|
+
+ auto embedding = impl_->text->encode(utf8_prompt);
|
|
299
|
+
+ if (!embedding) return std::unexpected(embedding.error());
|
|
300
|
+
+ const std::lock_guard inference_lock(impl_->inference_mutex);
|
|
301
|
+
+ if (!impl_->weights) {
|
|
302
|
+
+ auto loaded = detail::ggml_motion_weights::load(impl_->motion_path);
|
|
303
|
+
+ if (!loaded) return std::unexpected(loaded.error());
|
|
304
|
+
+ impl_->weights = std::move(*loaded);
|
|
305
|
+
+ }
|
|
306
|
+
+ auto gm=impl_->weights->f32_values("stats.global_root.mean"), gs=impl_->weights->f32_values("stats.global_root.std");
|
|
307
|
+
+ auto bm=impl_->weights->f32_values("stats.body.mean"), bs=impl_->weights->f32_values("stats.body.std");
|
|
308
|
+
+ if (!gm || !gs || !bm || !bs) return std::unexpected("motion GGUF lacks normalization statistics");
|
|
309
|
+
+ // Upstream normalizes the whole observed tensor; only masked cells reach
|
|
310
|
+
+ // the constraint branch (x*(1-mask) + observed*mask), so zeros elsewhere.
|
|
311
|
+
+ std::vector<float> observed(frames*D, 0.F), mask(frames*D, 0.F);
|
|
312
|
+
+ float heading = 0.F;
|
|
313
|
+
+ // Keys on one frame merge their masks (a fullbody and an end-effector key of
|
|
314
|
+
+ // the same pose); each frame is normalized once.
|
|
315
|
+
+ std::vector<bool> keyed(frames, false);
|
|
316
|
+
+ for (const auto &key : keys) {
|
|
317
|
+
+ std::vector<unsigned> positions, rotations;
|
|
318
|
+
+ if (auto expanded = chains(key, positions, rotations); !expanded) return std::unexpected(expanded.error());
|
|
319
|
+
+ if (key.end_effectors.empty()) positions.clear();
|
|
320
|
+
+ const float angle = detail::pose_condition(s, key.local_axis_angle, key.root_position.data(),
|
|
321
|
+
+ key.has_smooth_root_2d ? key.smooth_root_2d.data() : nullptr, positions, rotations,
|
|
322
|
+
+ observed.data()+key.frame*D, mask.data()+key.frame*D);
|
|
323
|
+
+ if (key.frame == 0) heading = angle;
|
|
324
|
+
+ keyed[key.frame] = true;
|
|
325
|
+
+ }
|
|
326
|
+
+ for (unsigned frame = 0; frame < frames; ++frame) {
|
|
327
|
+
+ if (!keyed[frame]) continue;
|
|
328
|
+
+ float *row = observed.data()+frame*D; const float *row_mask = mask.data()+frame*D;
|
|
329
|
+
+ for (size_t j=0; j<J; ++j) if (row_mask[5+j*3] > 0.F) { row[5+j*3] -= row[0]; row[7+j*3] -= row[2]; }
|
|
330
|
+
+ for (size_t d=0; d<5; ++d) row[d]=(row[d]-(*gm)[d])/std::sqrt((*gs)[d]*(*gs)[d]+1.e-5F);
|
|
331
|
+
+ for (size_t d=0; d<body; ++d) row[5+d]=(row[5+d]-(*bm)[d])/std::sqrt((*bs)[d]*(*bs)[d]+1.e-5F);
|
|
332
|
+
+ for (size_t d=0; d<D; ++d) row[d]*=row_mask[d];
|
|
333
|
+
+ }
|
|
334
|
+
+ std::mt19937_64 rng(seed);
|
|
335
|
+
+ std::normal_distribution<float> normal(0.f, 1.f);
|
|
336
|
+
+ std::vector<float> noise(static_cast<size_t>(frames)*D);
|
|
337
|
+
+ for (float &value : noise) value = normal(rng);
|
|
338
|
+
+ auto sampled = detail::sample_motion_from_noise_conditioned(*impl_->weights, noise, *embedding, observed, mask,
|
|
339
|
+
+ heading, frames, steps, text_cfg, constraint_cfg);
|
|
340
|
+
+ if (!sampled) return std::unexpected(sampled.error());
|
|
341
|
+
+ auto decoded = detail::decode_motion(*sampled, frames, s, *gm, *gs, *bm, *bs);
|
|
342
|
+
+ if (!decoded) return std::unexpected(decoded.error());
|
|
343
|
+
+ motion_data result; result.frames=frames; result.joints=static_cast<unsigned>(J);
|
|
344
|
+
+ result.local_rotations_xyzw=std::move(decoded->local_xyzw); result.root_positions=std::move(decoded->root_positions);
|
|
345
|
+
+ return result;
|
|
346
|
+
+#else
|
|
347
|
+
+ (void) utf8_prompt; (void) keys; (void) frames; (void) steps; (void) seed; (void) text_cfg; (void) constraint_cfg;
|
|
348
|
+
+ return std::unexpected("Kimodo was built without GGML support");
|
|
349
|
+
+#endif
|
|
350
|
+
+}
|
|
351
|
+
+
|
|
352
|
+
std::expected<motion_data, std::string> model::generate_text_sequence(
|
|
353
|
+
std::span<const prompt_segment> segments, unsigned transition_frames,
|
|
354
|
+
unsigned steps, std::uint64_t seed, float text_cfg, float constraint_cfg) const {
|
|
355
|
+
diff --git a/src/sequence.cpp b/src/sequence.cpp
|
|
356
|
+
index 6dbf6da..3ff5d8b 100644
|
|
357
|
+
--- a/src/sequence.cpp
|
|
358
|
+
+++ b/src/sequence.cpp
|
|
359
|
+
@@ -34,6 +34,39 @@ float condition_row(const float *raw, const skeleton_spec &s, float *value) {
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
+float pose_condition(const skeleton_spec &s, std::span<const float> axis_angle,
|
|
364
|
+
+ const float root[3], const float *smooth_root_2d, std::span<const unsigned> positions,
|
|
365
|
+
+ std::span<const unsigned> rotations, float *value, float *mask) {
|
|
366
|
+
+ const size_t J=s.joints(),rotation_begin=5+3*J;
|
|
367
|
+
+ std::vector<mat> global(J);
|
|
368
|
+
+ std::vector<std::array<float,3>> posed(J);
|
|
369
|
+
+ for(size_t j=0;j<J;++j){
|
|
370
|
+
+ const double x=axis_angle[j*3],y=axis_angle[j*3+1],z=axis_angle[j*3+2],angle=std::sqrt(x*x+y*y+z*z);
|
|
371
|
+
+ mat local{{1,0,0,0,1,0,0,0,1}};
|
|
372
|
+
+ if(angle>1e-12){const double c=std::cos(angle),si=std::sin(angle),t=1-c,a=x/angle,b=y/angle,d=z/angle;
|
|
373
|
+
+ local={{t*a*a+c,t*a*b-si*d,t*a*d+si*b,t*a*b+si*d,t*b*b+c,t*b*d-si*a,t*a*d-si*b,t*b*d+si*a,t*d*d+c}};}
|
|
374
|
+
+ const int parent=s.parents[j];
|
|
375
|
+
+ if(parent<0){global[j]=local;posed[j]={root[0],root[1],root[2]};continue;}
|
|
376
|
+
+ global[j]=mul(global[static_cast<size_t>(parent)],local);
|
|
377
|
+
+ float offset[3];rotate(global[static_cast<size_t>(parent)],s.offsets[j],offset);
|
|
378
|
+
+ for(int k=0;k<3;++k)posed[j][k]=posed[static_cast<size_t>(parent)][k]+offset[k];
|
|
379
|
+
+ }
|
|
380
|
+
+ const auto right=s.hips[0],left=s.hips[1];
|
|
381
|
+
+ const float angle=std::atan2(posed[right][2]-posed[left][2],-(posed[right][0]-posed[left][0]));
|
|
382
|
+
+ // Smooth root x/z default to the pelvis; y is the pelvis height. Only this
|
|
383
|
+
+ // key's features are written: another key on the same frame (the other
|
|
384
|
+
+ // hand) keeps its own. Positions stay global here; the caller subtracts
|
|
385
|
+
+ // the frame's final smooth root x/z (create_conditions after merging).
|
|
386
|
+
+ const float sx=smooth_root_2d?smooth_root_2d[0]:root[0],sz=smooth_root_2d?smooth_root_2d[1]:root[2];
|
|
387
|
+
+ value[0]=sx;value[1]=root[1];value[2]=sz;value[3]=std::cos(angle);value[4]=std::sin(angle);
|
|
388
|
+
+ std::fill(mask,mask+5,1.F);
|
|
389
|
+
+ auto position=[&](size_t j){value[5+j*3]=posed[j][0];value[6+j*3]=posed[j][1];value[7+j*3]=posed[j][2];std::fill(mask+5+j*3,mask+8+j*3,1.F);};
|
|
390
|
+
+ if(positions.empty())for(size_t j=0;j<J;++j)position(j);
|
|
391
|
+
+ for(unsigned j:positions)position(j);
|
|
392
|
+
+ for(unsigned j:rotations){for(int d=0;d<6;++d)value[rotation_begin+j*6+static_cast<size_t>(d)]=static_cast<float>(global[j].v[(d%3)*3+d/3]);std::fill(mask+rotation_begin+j*6,mask+rotation_begin+j*6+6,1.F);}
|
|
393
|
+
+ return angle;
|
|
394
|
+
+}
|
|
395
|
+
+
|
|
396
|
+
std::expected<sequence_transition, std::string> prepare_sequence_transition(
|
|
397
|
+
const ggml_motion_weights &weights, std::span<const float> previous,
|
|
398
|
+
std::size_t continuation_frames, unsigned transition_frames) {
|