Skip to content

Commit

Permalink
performance: paralelize inference (#32)
Browse files Browse the repository at this point in the history
* performance: paralelize inference

* Update intent_manager.py
  • Loading branch information
JarbasAl authored Dec 11, 2024
1 parent d5965cb commit 69f9632
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
52 changes: 44 additions & 8 deletions ovos_padatious/intent_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,58 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import time
from concurrent.futures import ThreadPoolExecutor
from typing import List
from ovos_padatious.intent import Intent
from ovos_padatious.match_data import MatchData
from ovos_padatious.training_manager import TrainingManager
from ovos_padatious.util import tokenize
from ovos_utils.log import LOG


class IntentManager(TrainingManager):
def __init__(self, cache):
super(IntentManager, self).__init__(Intent, cache)
"""
Manages intents and performs matching using the OVOS Padatious framework.
Args:
cache (str): Path to the cache directory for storing trained models.
"""
def __init__(self, cache: str, debug: bool = False):
super().__init__(Intent, cache)
self.debug = debug

def calc_intents(self, query: str, entity_manager) -> List[MatchData]:
"""
Calculate matches for the given query against all registered intents.
Args:
query (str): The input query to match.
entity_manager: The entity manager for resolving entities in the query.
def calc_intents(self, query, entity_manager):
Returns:
List[MatchData]: A list of matches sorted by confidence.
"""
sent = tokenize(query)
matches = []
for i in self.objects:
match = i.match(sent, entity_manager)
match.detokenize()
matches.append(match)

def match_intent(intent):
start_time = time.monotonic()
try:
match = intent.match(sent, entity_manager)
match.detokenize()
if self.debug:
LOG.debug(f"Inference for intent '{intent.name}' took {time.monotonic() - start_time} seconds")
return match
except Exception as e:
LOG.error(f"Error processing intent '{intent.name}': {e}")
return None

# Parallelize matching
with ThreadPoolExecutor() as executor:
matches = list(executor.map(match_intent, self.objects))

# Filter out None results from failed matches
matches = [match for match in matches if match]

return matches
2 changes: 1 addition & 1 deletion ovos_padatious/simple_intent.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def calc_weight(w): return pow(len(w), 3.0)

train_data = fann.training_data()
train_data.set_train_data(inputs, outputs)
LOG.debug(f"Training {self.name} with samples: {n_pos} positive + {n_neg} negative")
LOG.debug(f"Training {self.name} with {len(self.ids)} inputs and samples: {n_pos} positive + {n_neg} negative")
for _ in range(10):
self.configure_net()
self.net.train_on_data(train_data, 1000, 0, 0)
Expand Down

0 comments on commit 69f9632

Please sign in to comment.