1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# frozen_string_literal: true
# TrustFormeRS Ruby bindings for high-performance transformer models
#
# TrustFormeRS provides efficient implementations of popular transformer architectures
# with support for text generation, classification, question answering, and other NLP tasks.
#
# @example Basic usage
# # Initialize TrustFormeRS with default configuration
# trustformers = TrustFormeRS.new
#
# # Create a text generation pipeline
# pipeline = trustformers.create_pipeline(
# task: :text_generation,
# model_id: "gpt2",
# temperature: 0.7,
# max_new_tokens: 100
# )
#
# # Generate text
# result = pipeline.generate("The future of AI is")
# puts result
#
# @example Text classification
# pipeline = trustformers.create_pipeline(
# task: :text_classification,
# model_id: "cardiffnlp/twitter-roberta-base-sentiment-latest"
# )
#
# results = pipeline.classify("I love this new framework!")
# results.each { |result| puts "#{result[:label]}: #{result[:score]}" }
#
class << self
# Get the version of TrustFormeRS
# @return [String] Version string
Native.trustformers_version
end
# Check if GPU is available on the system
# @return [Boolean] True if GPU is available
Native.trustformers_is_gpu_available
end
# Check if CUDA is available on the system
# @return [Boolean] True if CUDA is available
Native.trustformers_is_cuda_available
end
# Check if Metal is available (macOS only)
# @return [Boolean] True if Metal is available
return false unless RUBY_PLATFORM.include?()
Native.trustformers_is_metal_available
end
# Get available devices on the system
# @return [Array<String>] List of available devices
devices = []
devices << if cuda_available?
devices << if metal_available?
devices << if gpu_available?
devices.uniq
end
# Get system information
# @return [Hash] System information including device capabilities
{
version: version,
devices: available_devices,
gpu_available: gpu_available?,
cuda_available: cuda_available?,
metal_available: metal_available?,
ruby_version: RUBY_VERSION,
platform: RUBY_PLATFORM
}
end
end
# Main TrustFormeRS class for creating pipelines and managing models
attr_reader :config
# Initialize TrustFormeRS client
# @param config [Configuration, Hash] Configuration options
@config = config.is_a?(Configuration) ? config : Configuration.new(config)
@handle = Native.trustformers_init(@config.to_h)
raise InitializationError, if @handle.null?
end
# Create a new pipeline for a specific task
# @param task [Symbol] Task type (:text_generation, :text_classification, etc.)
# @param model_id [String] Model identifier (Hugging Face model ID or local path)
# @param options [Hash] Pipeline-specific options
# @return [Pipeline] Configured pipeline for the task
pipeline_config = {
task: task,
model_id: model_id,
**options
}
Pipeline.new(self, pipeline_config)
end
# Load a model from a path or Hugging Face ID
# @param model_id [String] Model identifier
# @param options [Hash] Model loading options
# @return [Model] Loaded model
Model.new(self, model_id, options)
end
# Load a tokenizer from a path or Hugging Face ID
# @param tokenizer_id [String] Tokenizer identifier
# @param options [Hash] Tokenizer loading options
# @return [Tokenizer] Loaded tokenizer
Tokenizer.new(self, tokenizer_id, options)
end
# Enable GPU acceleration if available
# @param device [String] Specific device to use ("cuda", "metal", "auto")
# @return [Boolean] True if GPU was enabled successfully
return false unless TrustFormeRS.gpu_available?
device = detect_best_gpu_device if device ==
Native.trustformers_enable_gpu(@handle, device)
end
# Disable GPU acceleration and use CPU only
# @return [Boolean] True if CPU mode was enabled successfully
Native.trustformers_disable_gpu(@handle)
end
# Get current device being used
# @return [String] Current device ("cpu", "cuda", "metal", etc.)
Native.trustformers_get_current_device(@handle)
end
# Get memory usage statistics
# @return [Hash] Memory usage information
stats = Native.trustformers_get_memory_stats(@handle)
{
total_allocated: stats[:total_allocated],
currently_allocated: stats[:currently_allocated],
peak_allocated: stats[:peak_allocated],
device_memory: stats[:device_memory]
}
end
# Clean up resources
return unless @handle && !@handle.null?
Native.trustformers_free(@handle)
@handle = nil
end
# Automatic cleanup when object is garbage collected
close
end
# Get the native handle (for internal use)
# @api private
@handle
end
private
return if TrustFormeRS.metal_available?
return if TrustFormeRS.cuda_available?
end
end
# Convenience method to create a new TrustFormeRS client
# @param config [Configuration, Hash] Configuration options
# @return [Client] New TrustFormeRS client
Client.new(config)
end
end