elelem-candle 0.1.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.
- checksums.yaml +7 -0
- data/.agents/backlog/001-accept-model-option.md +34 -0
- data/.agents/backlog/002-drop-unused-crates.md +38 -0
- data/.agents/backlog/003-gem-metadata-links.md +67 -0
- data/.github/workflows/release.yml +73 -0
- data/.release-please-manifest.json +3 -0
- data/.rspec +1 -0
- data/.rubocop.yml +62 -0
- data/CHANGELOG.md +8 -0
- data/Cargo.lock +4245 -0
- data/Cargo.toml +13 -0
- data/LICENSE.txt +21 -0
- data/README.md +47 -0
- data/Rakefile +15 -0
- data/exe/elelem-candle +8 -0
- data/ext/elelem/candle/Cargo.toml +27 -0
- data/ext/elelem/candle/build.rs +5 -0
- data/ext/elelem/candle/extconf.rb +6 -0
- data/ext/elelem/candle/src/lib.rs +75 -0
- data/lib/elelem/candle/provider.rb +33 -0
- data/lib/elelem/candle/version.rb +7 -0
- data/lib/elelem/candle.rb +18 -0
- data/release-please-config.json +12 -0
- data/spec/elelem/candle/version_spec.rb +7 -0
- data/spec/spec_helper.rb +11 -0
- metadata +104 -0
data/Cargo.toml
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# This Cargo.toml is here to let externals tools (IDEs, etc.) know that this is
|
|
2
|
+
# a Rust project. Your extensions dependencies should be added to the Cargo.toml
|
|
3
|
+
# in the ext/ directory.
|
|
4
|
+
|
|
5
|
+
[workspace]
|
|
6
|
+
members = ["./ext/elelem/candle"]
|
|
7
|
+
resolver = "2"
|
|
8
|
+
|
|
9
|
+
[profile.release]
|
|
10
|
+
# By default, debug symbols are stripped from the final binary which makes it
|
|
11
|
+
# harder to debug if something goes wrong. It's recommended to keep debug
|
|
12
|
+
# symbols in the release build so that you can debug the final binary if needed.
|
|
13
|
+
debug = true
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 mo khan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# elelem-candle
|
|
2
|
+
|
|
3
|
+
An [elelem](https://github.com/elelem-sh/elelem) provider plugin backed by
|
|
4
|
+
[huggingface/candle](https://github.com/huggingface/candle), a pure-Rust
|
|
5
|
+
tensor/ML framework, via a real Ruby native extension (not a spawned
|
|
6
|
+
subprocess).
|
|
7
|
+
|
|
8
|
+
## Status
|
|
9
|
+
|
|
10
|
+
Experimental / hobby project. This is a skeleton: the Ruby <-> Rust boundary
|
|
11
|
+
(`Elelem::Candle::Provider` -> `Elelem::Candle::Model#generate` -> streaming
|
|
12
|
+
block) works end to end, but `Model#generate` in
|
|
13
|
+
`ext/elelem/candle/src/lib.rs` is still a stub that echoes the prompt back
|
|
14
|
+
token-by-token instead of running real inference. See the TODOs in that file
|
|
15
|
+
for what's next (tokenizer + safetensors loading + an actual candle-transformers
|
|
16
|
+
model forward pass).
|
|
17
|
+
|
|
18
|
+
## Why a native extension instead of a subprocess
|
|
19
|
+
|
|
20
|
+
`elelem-ratatui` shells out to a standalone Rust binary and talks JSON over
|
|
21
|
+
stdio. This gem instead uses [`rb_sys`](https://github.com/oxidize-rb/rb_sys) +
|
|
22
|
+
[`magnus`](https://github.com/matsadler/magnus) to compile candle straight
|
|
23
|
+
into a loadable `.so`, called in-process via real Ruby<->Rust FFI (typed
|
|
24
|
+
arguments, Ruby exceptions on Rust errors, streaming callbacks) -- the same
|
|
25
|
+
mechanism most modern Ruby+Rust gems use, and the standard `bundle gem
|
|
26
|
+
--ext=rust` scaffold.
|
|
27
|
+
|
|
28
|
+
## Requirements
|
|
29
|
+
|
|
30
|
+
- Rust toolchain (`cargo`, `rustc`) -- see https://rustup.rs
|
|
31
|
+
- A working C toolchain + `clang`/`libclang` (needed by `rb_sys`'s bindgen
|
|
32
|
+
step to generate Ruby FFI bindings). On Fedora: `sudo dnf install clang`.
|
|
33
|
+
|
|
34
|
+
## Development
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
bundle install
|
|
38
|
+
bundle exec rake compile # builds the Rust extension via rb_sys
|
|
39
|
+
bundle exec rake build # -> bundle exec rake compile
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
export ELELEM_CANDLE_MODEL=/path/to/model
|
|
46
|
+
bundle exec exe/elelem-candle ask "hi"
|
|
47
|
+
```
|
data/Rakefile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/gem_tasks"
|
|
4
|
+
require "rb_sys/extensiontask"
|
|
5
|
+
|
|
6
|
+
task build: :compile
|
|
7
|
+
|
|
8
|
+
GEMSPEC = Gem::Specification.load("elelem-candle.gemspec")
|
|
9
|
+
|
|
10
|
+
RbSys::ExtensionTask.new("candle", GEMSPEC) do |ext|
|
|
11
|
+
ext.ext_dir = "ext/elelem/candle"
|
|
12
|
+
ext.lib_dir = "lib/elelem/candle"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
task default: :compile
|
data/exe/elelem-candle
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "candle"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
authors = ["mo khan <mo@mokhan.ca>"]
|
|
6
|
+
publish = false
|
|
7
|
+
|
|
8
|
+
[lib]
|
|
9
|
+
crate-type = ["cdylib"]
|
|
10
|
+
|
|
11
|
+
[dependencies]
|
|
12
|
+
magnus = { version = "0.8.2" }
|
|
13
|
+
rb-sys = { version = "0.9", features = ["stable-api-compiled-fallback"] }
|
|
14
|
+
|
|
15
|
+
# huggingface/candle: pure-Rust tensor/ML framework. Start CPU-only; add the
|
|
16
|
+
# "cuda"/"metal" candle-core features later once a model is loading end to end.
|
|
17
|
+
candle-core = "0.11"
|
|
18
|
+
candle-nn = "0.11"
|
|
19
|
+
candle-transformers = "0.11"
|
|
20
|
+
tokenizers = { version = "0.23", default-features = false, features = ["onig"] }
|
|
21
|
+
hf-hub = "1.0"
|
|
22
|
+
|
|
23
|
+
[build-dependencies]
|
|
24
|
+
rb-sys-env = "0.2.2"
|
|
25
|
+
|
|
26
|
+
[dev-dependencies]
|
|
27
|
+
rb-sys-test-helpers = { version = "0.2.2" }
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
use magnus::{
|
|
2
|
+
block::Proc, function, method, prelude::*, Error, Ruby,
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
/// Wraps a loaded candle model + tokenizer. Right now this is a skeleton:
|
|
6
|
+
/// `Model::new` just records where the weights *would* load from, and
|
|
7
|
+
/// `generate` proves out the streaming-token-across-FFI plumbing without a
|
|
8
|
+
/// real forward pass yet.
|
|
9
|
+
///
|
|
10
|
+
/// Next steps to make this real (see candle-transformers examples for the
|
|
11
|
+
/// reference implementations):
|
|
12
|
+
/// 1. Load a tokenizer (tokenizers::Tokenizer::from_file / from hf-hub).
|
|
13
|
+
/// 2. Load safetensors weights into a candle_core::Device (Cpu, or Cuda/
|
|
14
|
+
/// Metal once those candle-core features are enabled).
|
|
15
|
+
/// 3. Build the model (e.g. candle_transformers::models::mistral::Model).
|
|
16
|
+
/// 4. In `generate`, run the actual token-by-token forward pass + sampling
|
|
17
|
+
/// loop, calling `token_cb.call` per generated piece instead of the
|
|
18
|
+
/// canned stub output below.
|
|
19
|
+
#[magnus::wrap(class = "Elelem::Candle::Model", free_immediately, size)]
|
|
20
|
+
struct Model {
|
|
21
|
+
path: String,
|
|
22
|
+
device: String,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
impl Model {
|
|
26
|
+
fn new(path: String, device: String) -> Self {
|
|
27
|
+
Self { path, device }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/// Streams generated text back to Ruby by invoking `token_cb` once per
|
|
31
|
+
/// piece. This is the interesting FFI boundary: Rust owns the generation
|
|
32
|
+
/// loop, Ruby supplies a block, and each token crosses the boundary as
|
|
33
|
+
/// it's produced rather than buffering the whole response.
|
|
34
|
+
///
|
|
35
|
+
/// `&Ruby` must come before `self` here -- that's the signature
|
|
36
|
+
/// `RubyMethod2` (what `method!(_, 2)` resolves to for a 2-Ruby-arg
|
|
37
|
+
/// method that also wants the `&Ruby` handle) actually requires:
|
|
38
|
+
/// `Fn(&Ruby, RbSelf, T1, T2) -> Res`. A `&self` receiver puts `self`
|
|
39
|
+
/// first instead, which is why the original shape failed to satisfy
|
|
40
|
+
/// the trait bound at compile time.
|
|
41
|
+
fn generate(ruby: &Ruby, rb_self: &Model, prompt: String, token_cb: Proc) -> Result<(), Error> {
|
|
42
|
+
if rb_self.path.is_empty() {
|
|
43
|
+
return Err(Error::new(
|
|
44
|
+
ruby.exception_runtime_error(),
|
|
45
|
+
"Elelem::Candle::Model: no model path configured",
|
|
46
|
+
));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// TODO(real inference): replace this with the actual candle
|
|
50
|
+
// generation loop. Left as a stub so the Ruby <-> Rust streaming
|
|
51
|
+
// contract (Provider#fetch -> Model#generate -> block) can be
|
|
52
|
+
// exercised end to end before a real model is wired in.
|
|
53
|
+
let stub = format!(
|
|
54
|
+
"[stub:{} on {}] {}",
|
|
55
|
+
rb_self.path, rb_self.device, prompt
|
|
56
|
+
);
|
|
57
|
+
for word in stub.split_whitespace() {
|
|
58
|
+
token_cb.call::<_, magnus::Value>((format!("{word} "),))?;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
Ok(())
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
#[magnus::init]
|
|
66
|
+
fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
67
|
+
let elelem = ruby.define_module("Elelem")?;
|
|
68
|
+
let candle = elelem.define_module("Candle")?;
|
|
69
|
+
let model = candle.define_class("Model", ruby.class_object())?;
|
|
70
|
+
|
|
71
|
+
model.define_singleton_method("new", function!(Model::new, 2))?;
|
|
72
|
+
model.define_method("generate", method!(Model::generate, 2))?;
|
|
73
|
+
|
|
74
|
+
Ok(())
|
|
75
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Elelem
|
|
4
|
+
module Candle
|
|
5
|
+
# Thin Ruby wrapper around the native (Rust/candle) model. The Rust side
|
|
6
|
+
# (ext/elelem/candle/src/lib.rs) does the actual tensor loading/inference;
|
|
7
|
+
# this class just adapts elelem's provider contract to it.
|
|
8
|
+
class Provider
|
|
9
|
+
def initialize(model:, device: ENV.fetch("ELELEM_CANDLE_DEVICE", "cpu"))
|
|
10
|
+
@model = Elelem::Candle::Model.new(model, device)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def fetch(messages, _tools = [], &block)
|
|
14
|
+
# TODO: real chat templating + tool-call parsing, mirroring
|
|
15
|
+
# elelem-llama's provider.rb. For now: single-turn text generation.
|
|
16
|
+
prompt = messages.map { |m| "#{m[:role] || m["role"]}: #{m[:content] || m["content"]}" }.join("\n")
|
|
17
|
+
|
|
18
|
+
content = +""
|
|
19
|
+
# Model#generate (Rust) takes token_cb as an explicit Proc argument,
|
|
20
|
+
# not an implicit Ruby block -- magnus's Proc TryConvert impl
|
|
21
|
+
# converts a positional argument, it doesn't bind an implicit block.
|
|
22
|
+
token_cb = proc do |piece|
|
|
23
|
+
content << piece
|
|
24
|
+
block&.call(type: "thinking", text: piece)
|
|
25
|
+
end
|
|
26
|
+
@model.generate(prompt, token_cb)
|
|
27
|
+
|
|
28
|
+
block&.call(type: "saying", text: content)
|
|
29
|
+
[]
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "elelem"
|
|
5
|
+
|
|
6
|
+
require_relative "candle/version"
|
|
7
|
+
require "elelem/candle/candle" # native extension (ext/elelem/candle)
|
|
8
|
+
require_relative "candle/provider"
|
|
9
|
+
|
|
10
|
+
Elelem.configure do |config|
|
|
11
|
+
config.provider(:candle) do
|
|
12
|
+
Elelem::Candle::Provider.new(
|
|
13
|
+
model: ENV.fetch("ELELEM_CANDLE_MODEL") do
|
|
14
|
+
raise "set ELELEM_CANDLE_MODEL=/path/to/model (safetensors dir or hf repo id)"
|
|
15
|
+
end
|
|
16
|
+
)
|
|
17
|
+
end
|
|
18
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: elelem-candle
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- mo khan
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: elelem
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0.12'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '1'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
25
|
+
requirements:
|
|
26
|
+
- - ">="
|
|
27
|
+
- !ruby/object:Gem::Version
|
|
28
|
+
version: '0.12'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '1'
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: rb_sys
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - "~>"
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '0.9'
|
|
39
|
+
type: :runtime
|
|
40
|
+
prerelease: false
|
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - "~>"
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0.9'
|
|
46
|
+
description: A candle (huggingface/candle) plugin for elelem.
|
|
47
|
+
email:
|
|
48
|
+
- mo@mokhan.ca
|
|
49
|
+
executables:
|
|
50
|
+
- elelem-candle
|
|
51
|
+
extensions:
|
|
52
|
+
- ext/elelem/candle/extconf.rb
|
|
53
|
+
extra_rdoc_files: []
|
|
54
|
+
files:
|
|
55
|
+
- ".agents/backlog/001-accept-model-option.md"
|
|
56
|
+
- ".agents/backlog/002-drop-unused-crates.md"
|
|
57
|
+
- ".agents/backlog/003-gem-metadata-links.md"
|
|
58
|
+
- ".github/workflows/release.yml"
|
|
59
|
+
- ".release-please-manifest.json"
|
|
60
|
+
- ".rspec"
|
|
61
|
+
- ".rubocop.yml"
|
|
62
|
+
- CHANGELOG.md
|
|
63
|
+
- Cargo.lock
|
|
64
|
+
- Cargo.toml
|
|
65
|
+
- LICENSE.txt
|
|
66
|
+
- README.md
|
|
67
|
+
- Rakefile
|
|
68
|
+
- exe/elelem-candle
|
|
69
|
+
- ext/elelem/candle/Cargo.toml
|
|
70
|
+
- ext/elelem/candle/build.rs
|
|
71
|
+
- ext/elelem/candle/extconf.rb
|
|
72
|
+
- ext/elelem/candle/src/lib.rs
|
|
73
|
+
- lib/elelem/candle.rb
|
|
74
|
+
- lib/elelem/candle/provider.rb
|
|
75
|
+
- lib/elelem/candle/version.rb
|
|
76
|
+
- release-please-config.json
|
|
77
|
+
- spec/elelem/candle/version_spec.rb
|
|
78
|
+
- spec/spec_helper.rb
|
|
79
|
+
homepage: https://elelem.sh
|
|
80
|
+
licenses:
|
|
81
|
+
- MIT
|
|
82
|
+
metadata:
|
|
83
|
+
allowed_push_host: https://rubygems.org
|
|
84
|
+
homepage_uri: https://elelem.sh
|
|
85
|
+
source_code_uri: https://github.com/elelem-sh/candle
|
|
86
|
+
rubygems_mfa_required: 'true'
|
|
87
|
+
rdoc_options: []
|
|
88
|
+
require_paths:
|
|
89
|
+
- lib
|
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
91
|
+
requirements:
|
|
92
|
+
- - ">="
|
|
93
|
+
- !ruby/object:Gem::Version
|
|
94
|
+
version: 4.0.0
|
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
|
+
requirements:
|
|
97
|
+
- - ">="
|
|
98
|
+
- !ruby/object:Gem::Version
|
|
99
|
+
version: '0'
|
|
100
|
+
requirements: []
|
|
101
|
+
rubygems_version: 4.0.6
|
|
102
|
+
specification_version: 4
|
|
103
|
+
summary: A candle (huggingface/candle) plugin for elelem.
|
|
104
|
+
test_files: []
|