guyluz11 / guyluz11/flutter_local_summarizer

Performance is slow compered to python

Open
#6 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

The performance when running in flutter inside an app compared to the Python code is like heaven and Hell.
I am running both on the same machine, python code does not need max characters because it is so fast.

Here is a python example

```python
import re
import onnxruntime
import numpy as np
from transformers import AutoTokenizer

import json

from helpers.long_text import GETLONGTEXT, SHORTTEXT

def preprocess_text(text):
text = re.sub(r'[,.]{2,}', ' ', text)
text = re.sub(r'\s+', ' ', text).strip()
text = re.sub(r'[^\x00-\x7F]+', ' ', text)
text = text.lower()
return text

encoder_session = onnxruntime.InferenceSession("../models/flassco_encoder_model.onnx")
decoder_session = onnxruntime.InferenceSession("../models/flassco_decoder_model.onnx")

local_model_dir = "local_model_dir" # Replace with your local directory path
tokenizer = AutoTokenizer.from_pretrained(local_model_dir)

print("CLS Token ID:", tokenizer.cls_token_id)
print("BOS Token ID:", tokenizer.bos_token_id)
print("PAD Token ID:", tokenizer.pad_token_id)
print("EOS Token ID:", tokenizer.eos_token_id)

ARTICLE = GETLONGTEXT.lower()
max_summary_length = 300

inputs = tokenizer(ARTICLE, return_tensors="pt", padding=True, truncation=True)

listInputs = inputs['input_ids'].cpu().numpy()
listAttention = inputs['attention_mask'].cpu().numpy()
print(inputs['input_ids'])
print('or')
print(listInputs)

encoder_inputs = {
'input_ids': listInputs,
'attention_mask': listAttention,
}

encoder_output = encoder_session.run(None, encoder_inputs)

encoder_last_hidden_state = encoder_output[0] # Assuming the first output is hidden states

print('encoder_last_hidden_state')
print(encoder_last_hidden_state)

start_token_id = tokenizer.bos_token_id or tokenizer.cls_token_id or tokenizer.pad_token_id or 0

initial_decoder_input_ids = np.array([[start_token_id]], dtype=np.int64)

with open('encoder_hidden_state.json', 'w') as json_file:
json.dump(encoder_last_hidden_state.tolist(), json_file)
print("Saved as JSON: encoder_hidden_state.json")

current_output = initial_decoder_input_ids
for _ in range(max_summary_length):
decoder_output = decoder_session.run(None, {
'encoder_hidden_states': encoder_last_hidden_state.astype(np.float32),
'encoder_attention_mask': listAttention,
'input_ids': current_output,
})

nextTokeIds = decoder_output[0]
next_token_id = np.argmax(nextTokeIds[:, -1, :], axis=-1).reshape(-1, 1)

current_output = np.hstack([current_output, next_token_id])

if next_token_id[0, 0] == tokenizer.eos_token_id:
break

summarized_text = tokenizer.decode(current_output[0])

print("Summarized Text:", summarized_text)

```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.