Skip to content

A platform for community discussion. Free, open, simple.

PERF: reduce embedding concurrent generation (#33781)

Samb36639f94647
diff --git a/plugins/discourse-ai/lib/embeddings/vector.rb b/plugins/discourse-ai/lib/embeddings/vector.rb
index 91ff4c2e4ce..1755e90e7fc 100644
--- a/plugins/discourse-ai/lib/embeddings/vector.rb
+++ b/plugins/discourse-ai/lib/embeddings/vector.rb
@@ -16,43 +16,61 @@ module DiscourseAi
delegate :tokenizer, to: :vdef
+ MAX_CONCURRENT_EMBEDDINGS = 40
+
def gen_bulk_reprensentations(relation)
- http_pool_size = 100
pool =
- Concurrent::CachedThreadPool.new(
+ Scheduler::ThreadPool.new(
min_threads: 0,
- max_threads: http_pool_size,
- idletime: 30,
+ max_threads: MAX_CONCURRENT_EMBEDDINGS,
+ idle_time: 30,
)
schema = DiscourseAi::Embeddings::Schema.for(relation.first.class, vector_def: @vdef)
embedding_gen = vdef.inference_client
- promised_embeddings =
- relation
- .map do |record|
- prepared_text = vdef.prepare_target_text(record)
- next if prepared_text.blank?
-
- new_digest = OpenSSL::Digest::SHA1.hexdigest(prepared_text)
- next if schema.find_by_target(record)&.digest == new_digest
-
- Concurrent::Promises
- .fulfilled_future({ target: record, text: prepared_text, digest: new_digest }, pool)
- .then_on(pool) do |w_prepared_text|
- w_prepared_text.merge(embedding: embedding_gen.perform!(w_prepared_text[:text]))
- end
- .rescue { nil } # We log the error during #perform. Skip failed embeddings.
- end
- .compact
-
- Concurrent::Promises
- .zip(*promised_embeddings)
- .value!
- .each { |e| schema.store(e[:target], e[:embedding], e[:digest]) if e.present? }
+
+ queued = 0
+ results = Queue.new
+ # map so we release the DB connection
+ relation.map do |record|
+ prepared_text = vdef.prepare_target_text(record)
+ next if prepared_text.blank?
+
+ new_digest = OpenSSL::Digest::SHA1.hexdigest(prepared_text)
+ next if schema.find_by_target(record)&.digest == new_digest
+
+ pool.post do
+ results << { target: record, text: prepared_text, digest: new_digest }.merge(
+ embedding: embedding_gen.perform!(prepared_text),
+ )
+ rescue StandardError => e
+ results << e
+ end
+ queued += 1
+ end
+
+ errors = []
+ while queued > 0
+ result = results.pop
+ if result.is_a?(StandardError)
+ errors << result
+ else
+ schema.store(result[:target], result[:embedding], result[:digest]) if result.present?
+ end
+ queued -= 1
+ end
+
+ if errors.any?
+ Discourse.warn_exception(
+ errors[0],
+ message:
+ "Discourse AI: Errors during bulk classification: Failed to generate embeddings on #{errors.count} posts",
+ )
+ end
ensure
pool.shutdown
- pool.wait_for_termination
+ pool.wait_for_termination(timeout: 30)
end
def generate_representation_from(target)
diff --git a/plugins/discourse-ai/lib/sentiment/post_classification.rb b/plugins/discourse-ai/lib/sentiment/post_classification.rb
index 843805b98b3..becc8cdc800 100644
--- a/plugins/discourse-ai/lib/sentiment/post_classification.rb
+++ b/plugins/discourse-ai/lib/sentiment/post_classification.rb
@@ -101,7 +101,8 @@ module DiscourseAi
example_posts = errors.map { |e| e[:target].id }.take(5).join(", ")
Discourse.warn_exception(
errors[0][:error],
- "Discourse AI: Errors during bulk classification: Failed to classify #{errors.count} posts (example ids: #{example_posts})",
+ message:
+ "Discourse AI: Errors during bulk classification: Failed to classify #{errors.count} posts (example ids: #{example_posts})",
)
end
ensure