| 112 | } |
| 113 | |
| 114 | bool DiffusionSD35::loadModule(int index) { |
| 115 | if (mModules[index]) |
| 116 | return true; |
| 117 | |
| 118 | MNN::Express::Executor::getGlobalExecutor()->gc(MNN::Express::Executor::FULL); |
| 119 | |
| 120 | Module::Config module_config; |
| 121 | module_config.shapeMutable = false; |
| 122 | |
| 123 | std::vector<std::string> modelNames = {"text_encoder.mnn", "text_encoder_2.mnn", "text_encoder_3.mnn", |
| 124 | "transformer.mnn", "vae_decoder.mnn"}; |
| 125 | |
| 126 | std::string model_path = mModelPath + "/" + modelNames[index]; |
| 127 | MNN_PRINT("Load %s\n", model_path.c_str()); |
| 128 | |
| 129 | std::vector<std::string> inputs, outputs; |
| 130 | if (index == 0) { // text_encoder |
| 131 | inputs = {"input_ids"}; |
| 132 | outputs = {"last_hidden_state", "text_embeds"}; |
| 133 | } else if (index == 1) { // text_encoder_2 |
| 134 | inputs = {"input_ids"}; |
| 135 | outputs = {"last_hidden_state", "text_embeds"}; |
| 136 | } else if (index == 2) { // text_encoder_3 |
| 137 | inputs = {"input_ids"}; |
| 138 | outputs = {"last_hidden_state"}; |
| 139 | } else if (index == 3) { // transformer |
| 140 | inputs = {"hidden_states", "encoder_hidden_states", "pooled_projections", "timestep"}; |
| 141 | outputs = {"out_hidden_states"}; |
| 142 | } else if (index == 4) { // vae_decoder |
| 143 | inputs = {"latent_sample"}; |
| 144 | outputs = {"sample"}; |
| 145 | } |
| 146 | |
| 147 | mModules[index].reset(Module::load(inputs, outputs, model_path.c_str(), runtime_manager_, &module_config)); |
| 148 | if (!mModules[index]) { |
| 149 | MNN_ERROR("Failed to load model %s\n", model_path.c_str()); |
| 150 | if (index == 2) { |
| 151 | MNN_PRINT("Warning: T5 text encoder failed to load. Will use zeros.\n"); |
| 152 | } else { |
| 153 | return false; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if (mModules[index]) |
| 158 | mModules[index]->traceOrOptimize(MNN::Interpreter::Session_Resize_Fix); |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | std::pair<VARP, VARP> DiffusionSD35::encode_prompt(const std::string& prompt) { |
| 163 | auto run_encoder = [&](int module_index, VARP input_ids, const char* name) { |
nothing calls this directly
no test coverage detected