| 233 | } |
| 234 | |
| 235 | VARP StableDiffusion::unet(VARP text_embeddings, int iterNum, int randomSeed, std::function<void(int)> progressCallback) { |
| 236 | if(mMemoryMode != 1) { |
| 237 | mModules[0].reset(); |
| 238 | } |
| 239 | if(mInitNoise.size() != 16384) { |
| 240 | mInitNoise.resize(16384); |
| 241 | } |
| 242 | #ifdef MNN_DUMP_DATA |
| 243 | std::ostringstream fileName; |
| 244 | fileName << "random.txt"; |
| 245 | std::ifstream input(fileName.str().c_str()); |
| 246 | for (int i = 0; i < 16384; ++i) { |
| 247 | input >> mInitNoise[i]; |
| 248 | } |
| 249 | #else |
| 250 | int seed = randomSeed < 0 ? std::random_device()() : randomSeed; |
| 251 | std::mt19937 rng; |
| 252 | rng.seed(seed); |
| 253 | |
| 254 | std::normal_distribution<float> normal(0, 1); |
| 255 | for (int i = 0; i < 16384; i++) { |
| 256 | mInitNoise[i] = normal(rng); |
| 257 | } |
| 258 | #endif |
| 259 | |
| 260 | memcpy((void *)mLatentVar->writeMap<int8_t>(), mInitNoise.data(), 16384*sizeof(float)); |
| 261 | |
| 262 | VARP scalevar = _Input({1}, NCHW, halide_type_of<float>()); |
| 263 | auto scaleptr = scalevar->writeMap<float>(); |
| 264 | scaleptr[0] = 7.5; |
| 265 | |
| 266 | auto floatVar = _Input({1}, NCHW, halide_type_of<float>()); |
| 267 | auto ptr = floatVar->writeMap<float>(); |
| 268 | auto plms = mLatentVar; |
| 269 | |
| 270 | for (int i = 0; i < mTimeSteps.size(); i++) { |
| 271 | AUTOTIME; |
| 272 | |
| 273 | int timestep = mTimeSteps[i]; |
| 274 | ptr[0] = timestep; |
| 275 | auto temp = _Cast(floatVar, halide_type_of<int>()); |
| 276 | mTimestepVar->input(temp); |
| 277 | |
| 278 | mSampleVar = _Concat({plms, plms}, 0); |
| 279 | auto outputs = forwardWithResizeCache(1, {mSampleVar, mTimestepVar, text_embeddings}); |
| 280 | auto output = _Convert(outputs[0], NCHW); |
| 281 | |
| 282 | auto noise_pred = output; |
| 283 | |
| 284 | auto splitvar = _Split(noise_pred, {2}, 0); |
| 285 | auto noise_pred_uncond = splitvar[0]; |
| 286 | auto noise_pred_text = splitvar[1]; |
| 287 | |
| 288 | noise_pred = scalevar * (noise_pred_text - noise_pred_uncond) + noise_pred_uncond; |
| 289 | |
| 290 | plms = step_plms(plms, noise_pred, i); |
| 291 | |
| 292 | if (progressCallback) { |
nothing calls this directly
no test coverage detected