Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
swfsql committed Feb 29, 2024
1 parent 864ab5c commit 9688b21
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tokenizers = { version = "0.13.4", default-features = false, features = [
version = "0.3.2"
# path = "../hf-hub"
git = "https://github.com/swfsql/hf-hub.git"
rev = "fcaade15a9a844c4cde1145d1f40388ae2be998f"
rev = "a411dde1a7ab30d782cb978955b922551fb7eeb1"
default-features = false
features = ["online"]

Expand All @@ -71,6 +71,6 @@ yew = { version = "0.21.0", features = ["csr"] }
version = "0.3.2"
# path = "../hf-hub"
git = "https://github.com/swfsql/hf-hub.git"
rev = "fcaade15a9a844c4cde1145d1f40388ae2be998f"
rev = "a411dde1a7ab30d782cb978955b922551fb7eeb1"
default-features = false
features = ["wasm"]
11 changes: 10 additions & 1 deletion src/wasm/yew_ui/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use self::model::ModelSelection;
use hf_hub::{
api::wasm::{Api, Metadata},
api::wasm::{Api, ApiError, Metadata},
types::TmpFileBlobKeyList,
};
use indexed_db_futures::web_sys::DomException;
pub use model::{Connection, Model};
use yew::prelude::*;

Expand All @@ -18,31 +19,37 @@ pub enum Msg {
StartConnectApi,
/// Concludes the huggingface api connection (reqwest and indexeddb clients).
FinishConnectApi(Api),
FailConnectApi(ApiError),
/// Starts the huggingface api disconnection (reqwest and indexeddb clients).
StartDisconnectApi,
/// Concludes the huggingface api disconnection (reqwest and indexeddb clients).
FinishDisconnectApi,
FailDisconnectApi,
/// Starts checking information about the data of a model (size, etc).
StartModelDataCheck(ModelSelection),
/// Concludes checking information about the data of a model (size, etc).
FinishModelDataCheck(ModelSelection, Metadata, TmpFileBlobKeyList),
FailModelDataCheck,
/// Starts fetching a model data.
StartModelDataFetch(ModelSelection),
/// Concludes fetching a single chunk of a model data.
/// This is useful to state about the fetching progress.
FinishModelDataFetchSingle(ModelSelection, usize),
FailModelDataFetchSingle(ModelSelection, usize, ApiError),
/// Concludes fetching a model data (all chunks).
FinishModelDataFetch(ModelSelection),
/// Starts uploading a model data.
/// This is an alternative to the "fetch and cache read" mechanism.
StartModelDataUpload(ModelSelection),
/// Concludes uploading a model data.
FinishModelDataUpload(ModelSelection),
FailModelDataUpload,
/// Starts loading (reading) a model data.
/// The goal is to have bytes into the memory.
StartModelDataLoad(ModelSelection),
/// Concludes loading (reading) a model data.
FinishModelDataLoad(ModelSelection, Vec<u8>),
FailModelDataLoad(ModelSelection, DomException),
/// Unloads a model data.
/// The goal is to clear memory usage.
/// If the model was built, it also get's unbuilt.
Expand All @@ -53,11 +60,13 @@ pub enum Msg {
StartModelDataErase(ModelSelection),
/// Concludes erasing a model data from the cache.
FinishModelDataErase(ModelSelection),
FailModelDataErase(ModelSelection, DomException),
/// Starts building a model from the model data.
/// This is when the data stops being raw bytes and become tensors (etc) instead.
StartModelBuild(ModelSelection),
/// Concludes building a model from the model data.
FinishModelBuild(ModelSelection),
FailModelBuild,
/// If all required models are built, we move to the next step of being to use the models
/// for inference (etc).
TryFinilizeModelsBuilding,
Expand Down
74 changes: 63 additions & 11 deletions src/wasm/yew_ui/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ impl model::Model {
assert!(self.cache_api.is_exactly_disconnected());
self.cache_api = Connection::Connecting;
ctx.link().send_future(async {
let api = Api::new().await.unwrap();
Msg::FinishConnectApi(api)
match Api::new().await {
Ok(ok) => Msg::FinishConnectApi(ok),
Err(err) => Msg::FailConnectApi(err),
}
});
true
}
Expand All @@ -35,14 +37,21 @@ impl model::Model {
}
true
}
Msg::FailConnectApi(err) => {
self.cache_api = Connection::Disconnected;
log::error!("failed to connect to the api: {err}");
true
}
Msg::StartDisconnectApi => {
log::error!("Msg::StartDisconnectApi not yet implemented");
// todo!()
false
}
Msg::FinishDisconnectApi => {
log::error!("Msg::FinishDisconnectApi not yet implemented");
// todo!()
false
}
Msg::FailDisconnectApi => {
log::error!("Msg::FailDisconnectApi not yet implemented");
false
}
Msg::StartModelDataCheck(selection) => {
Expand All @@ -67,6 +76,9 @@ impl model::Model {
model_data.cache.fetching.chunk_list = chunk_list;
true
}
Msg::FailModelDataCheck => {
todo!()
}
Msg::StartModelDataFetch(selection) => {
let api = self.cache_api.as_connected().unwrap().clone();
let model_data = self.select_mut(&selection);
Expand All @@ -80,7 +92,12 @@ impl model::Model {
ctx.link().send_future(async move {
for (i, chunk_file) in chunk_files.into_iter().enumerate() {
if let Err(chunk_file) = chunk_file {
let () = api_repo.download_tempfile(&url, &chunk_file).await.unwrap();
match api_repo.download_tempfile(&url, &chunk_file).await {
Ok(()) => {}
Err(err) => {
return Msg::FailModelDataFetchSingle(selection, i, err)
}
}
}
link.send_message(Msg::FinishModelDataFetchSingle(selection, i));
}
Expand All @@ -97,6 +114,13 @@ impl model::Model {
}
true
}
Msg::FailModelDataFetchSingle(selection, i, err) => {
log::error!("failed to fetch chunk {i} for {selection:?}; err: {err}");
let model_data = self.select_mut(&selection);
model_data.cache.is_busy = false;
model_data.cache.is_done = false;
true
}
Msg::FinishModelDataFetch(selection) => {
let model_data = self.select_mut(&selection);
model_data.cache.is_busy = false;
Expand All @@ -105,12 +129,14 @@ impl model::Model {
}
Msg::StartModelDataUpload(_selection) => {
log::error!("Msg::StartModelDataUpload not yet implemented");
// todo!()
false
}
Msg::FinishModelDataUpload(_selection) => {
log::error!("Msg::FinishModelDataUpload not yet implemented");
// todo!()
false
}
Msg::FailModelDataUpload => {
log::error!("Msg::FailModelDataUpload not yet implemented");
false
}
Msg::StartModelDataLoad(selection) => {
Expand All @@ -126,8 +152,10 @@ impl model::Model {
.collect::<Result<Vec<_>, _>>()
.unwrap();
ctx.link().send_future(async move {
let data = api.load_bytes(&chunks_keys).await;
Msg::FinishModelDataLoad(selection, data)
match api.load_bytes(&chunks_keys).await {
Ok(ok) => Msg::FinishModelDataLoad(selection, ok),
Err(err) => Msg::FailModelDataLoad(selection, err),
}
});
true
}
Expand All @@ -137,6 +165,12 @@ impl model::Model {
ctx.link().send_message(Msg::StartModelBuild(selection));
false
}
Msg::FailModelDataLoad(selection, err) => {
log::error!("failed to load data for {selection:?}; err: {err:?}");
let model_data = self.select_mut(&selection);
model_data.load.is_busy = false;
true
}
Msg::ModelDataUnload(selection) => {
// stop any in-progress generation
if self.is_generating {
Expand Down Expand Up @@ -190,8 +224,10 @@ impl model::Model {
.unwrap();

ctx.link().send_future(async move {
let () = api.delete_bytes(&chunks_keys).await;
Msg::FinishModelDataErase(selection)
match api.delete_bytes(&chunks_keys).await {
Ok(()) => Msg::FinishModelDataErase(selection),
Err(err) => Msg::FailModelDataErase(selection, err),
}
});

model_data.cache.is_busy = true;
Expand All @@ -210,6 +246,19 @@ impl model::Model {

true
}
Msg::FailModelDataErase(selection, err) => {
log::error!("failed to erase data for {selection:?}; err: {err:?}");
let model_data = self.select_mut(&selection);
model_data.cache.is_busy = false;
model_data.cache.is_done = false;
// re-check if the data is cached or not
//
// note: if partial data has been erased, then the button will switch to
// the "click to fetch" option.
model_data.cache.is_checking = true;
ctx.link().send_message(Msg::StartModelDataCheck(selection));
true
}
Msg::StartModelBuild(selection) => {
let model_data = self.select_mut(&selection);
let data = std::mem::take(&mut model_data.load.data);
Expand All @@ -225,6 +274,9 @@ impl model::Model {
ctx.link().send_message(Msg::TryFinilizeModelsBuilding);
true
}
Msg::FailModelBuild => {
todo!()
}
Msg::TryFinilizeModelsBuilding => {
// consume the built models if they are all ready
if self.models_wrapper_builder.is_ready() {
Expand Down

0 comments on commit 9688b21

Please sign in to comment.