kiribi-multilingual_e5-small 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0a9d42a6343fe87431af5f6b297cc01c4cfa4d48fb7d7e7e94fd21574093a0b3
4
+ data.tar.gz: b95752db5aa79e0f1949e1a9f01a893ea2d8801154ce24df4b054645c82a979a
5
+ SHA512:
6
+ metadata.gz: 67c43cefe6faba082a47e5e02e5b1d46a2176067d429c09e4a9073679ff6b0f46eedc1a77b9f1b98ed7f0914fc44020f60ca808a1829e81ff4210b155e0d8f87
7
+ data.tar.gz: 10f000ad8a5f9a3c23787867037d41aa5472629fe62391ce092075b8ac730cb8c4f7cc58f4940e82996a4eded4a9edb7e6f0845ca5cf65754627880de6623e05
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Katsutoshi Fujihara
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/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "net/http"
5
+ require "pathname"
6
+ require "rubygems/package"
7
+ require "zlib"
8
+
9
+ GEM_NAME = "kiribi-multilingual_e5-small"
10
+ MODEL_URL = "https://github.com/matsudai/kiribi-externals/releases/download/intfloat%2Fmultilingual-e5-small%2Fc007d7e/model_qint8_avx512_vnni.tar.gz"
11
+
12
+ TMP_FILEPATH = File.expand_path(File.join(__dir__, "/../../tmp/model.tar.gz"))
13
+ BUILD_DIRPATH = File.expand_path(File.join(__dir__, "/../../build"))
14
+
15
+ if Dir.exist?(BUILD_DIRPATH)
16
+ puts "#{BUILD_DIRPATH} already exists, skipping download and extraction."
17
+ else
18
+ if File.exist?(TMP_FILEPATH)
19
+ puts "#{TMP_FILEPATH} already exists, skipping download."
20
+ else
21
+ redirect_count = 0
22
+ url = MODEL_URL
23
+ loop do
24
+ raise "Too many redirects" if redirect_count >= 10
25
+
26
+ resp = Net::HTTP.get_response(URI.parse(url))
27
+ case resp
28
+ when Net::HTTPSuccess
29
+ FileUtils.mkdir_p(File.dirname(TMP_FILEPATH))
30
+ File.binwrite(TMP_FILEPATH, resp.body)
31
+ break
32
+ when Net::HTTPRedirection
33
+ url = resp["Location"]
34
+ redirect_count += 1
35
+ else
36
+ raise "HTTP request failed (status code: #{resp.code})"
37
+ end
38
+ end
39
+ end
40
+
41
+ Gem::Package::TarReader.new(Zlib::GzipReader.open(TMP_FILEPATH)) do |archive|
42
+ archive.each do |entry|
43
+ filepath = File.join(BUILD_DIRPATH, *Pathname(entry.full_name).each_filename.to_a[1..])
44
+
45
+ if entry.directory?
46
+ FileUtils.mkdir_p(filepath)
47
+ elsif entry.file?
48
+ FileUtils.mkdir_p(File.dirname(filepath))
49
+ File.binwrite(filepath, entry.read)
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ File.write("Makefile", "all install clean:\n\t@echo \"Nothing to do for $(TARGET)\"\n")
56
+ FileUtils.rm_f(TMP_FILEPATH)
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kiribi
4
+ module MultilingualE5
5
+ module Small
6
+ VERSION = "0.0.1"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "small/version"
4
+ require "kiribi"
5
+ require "onnxruntime"
6
+ require "tokenizers"
7
+
8
+ module Kiribi
9
+ module MultilingualE5
10
+ extend Kiribi::Loader
11
+
12
+ module Small
13
+ extend Kiribi::Loader
14
+
15
+ TOKENIZER_FILEPATH = File.expand_path(File.join(__dir__, "/../../../build/tokenizer.json"))
16
+ MODEL_FILEPATH = File.expand_path(File.join(__dir__, "/../../../build/model_qint8_avx512_vnni.onnx"))
17
+
18
+ class Model
19
+ attr_reader :onnx_model, :tokenizer
20
+
21
+ def initialize
22
+ @tokenizer = Tokenizers.from_file(TOKENIZER_FILEPATH)
23
+ @onnx_model = OnnxRuntime::Model.new(MODEL_FILEPATH)
24
+ end
25
+
26
+ def embedding_query(input)
27
+ embedding(:query, input)
28
+ end
29
+
30
+ def embedding_passage(input)
31
+ embedding(:passage, input)
32
+ end
33
+
34
+ def embedding(prefix, input)
35
+ prefix = prefix.to_s
36
+
37
+ raise ArgumentError, "prefix must be :query or :passage" unless %w[query passage].include?(prefix)
38
+ encoded = tokenizer.encode("#{prefix}: #{input}")
39
+ batch = {
40
+ input_ids: [encoded.ids],
41
+ attention_mask: [encoded.attention_mask],
42
+ token_type_ids: [[0] * encoded.ids.length]
43
+ }
44
+ outputs = onnx_model.predict(batch)
45
+ last_hidden_state = outputs["last_hidden_state"][0]
46
+ valid_tokens = encoded.attention_mask.sum
47
+ last_hidden_state[0...valid_tokens][0..384].transpose.map(&:sum).map { it / valid_tokens }
48
+ end
49
+ end
50
+
51
+ def self.instantiate
52
+ Model.new
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ Kiribi.register(Kiribi::MultilingualE5::Small, order: 100_100_100)
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kiribi-multilingual_e5-small
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - matsudai
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: kiribi
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 0.0.1
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 0.0.1
26
+ - !ruby/object:Gem::Dependency
27
+ name: onnxruntime
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.10.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 0.10.0
40
+ - !ruby/object:Gem::Dependency
41
+ name: tokenizers
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 0.6.0
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 0.6.0
54
+ executables: []
55
+ extensions: []
56
+ extra_rdoc_files: []
57
+ files:
58
+ - LICENSE.txt
59
+ - Rakefile
60
+ - ext/kiribi-multilingual_e5-small/extconf.rb
61
+ - lib/kiribi/multilingual_e5/small.rb
62
+ - lib/kiribi/multilingual_e5/small/version.rb
63
+ homepage: https://github.com/matsudai/kiribi
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 3.4.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.6.9
82
+ specification_version: 4
83
+ summary: Easy to use some onnx models.
84
+ test_files: []