| 1537 | dphi.resize(static_cast<size_t>(frames * latent_dim), 0.0F); |
| 1538 | const size_t row_elems = static_cast<size_t>(frames * latent_dim); |
| 1539 | for (size_t i = 0; i < row_elems; ++i) { |
| 1540 | const float uncond_value = estimate.values[i]; |
| 1541 | const float cond_value = estimate.values[row_elems + i]; |
| 1542 | dphi[i] = uncond_value + guidance_scale * (cond_value - uncond_value); |
| 1543 | } |
| 1544 | } else { |
| 1545 | auto input = make_flow_input(x, incontext_x, mu, batch, frames, latent_dim, config.dim, true); |
| 1546 | const std::vector<float> timesteps = {t}; |
| 1547 | dphi = runtime.estimate_flow(input, 1, frames, timesteps).values; |
| 1548 | } |
| 1549 | for (size_t i = 0; i < x.size(); ++i) { |
| 1550 | x[i] += dt * dphi[i]; |
| 1551 | } |
| 1552 | t += dt; |
| 1553 | if (step < num_steps) { |
| 1554 | dt = static_cast<float>(step + 1) / static_cast<float>(num_steps) - t; |
| 1555 | } |
| 1556 | } |
| 1557 | return x; |
| 1558 | } |
| 1559 | |
| 1560 | std::vector<float> run_flow_chunk( |
| 1561 | const HeartCodecWeightsRuntime & runtime, |
| 1562 | const std::vector<int32_t> & codes_bqt, |
| 1563 | int64_t code_frames, |
| 1564 | int64_t latent_length, |
| 1565 | int64_t incontext_length, |
| 1566 | const std::vector<float> & true_latents, |
| 1567 | int64_t num_steps, |
| 1568 | float guidance_scale, |
| 1569 | uint64_t seed, |
| 1570 | uint64_t & randn_philox_offset, |
| 1571 | uint64_t randn_call_offset_blocks, |
| 1572 | const engine::sampling::TorchCudaSamplingPolicy & sampling_policy) { |
| 1573 | const auto & config = runtime.assets().codec_config; |
| 1574 | auto conditioning = runtime.build_conditioning(codes_bqt, 1, code_frames); |
| 1575 | if (conditioning.frames <= 0 || conditioning.channels != config.dim) { |
| 1576 | throw std::runtime_error("HeartCodec conditioning output shape mismatch"); |
| 1577 | } |
| 1578 | apply_inactive_conditioning( |
| 1579 | conditioning.values, |
| 1580 | conditioning.frames, |
| 1581 | conditioning.channels, |
| 1582 | latent_length, |
| 1583 | runtime.weights().flow.zero_cond_embedding_host.values); |
| 1584 | if (randn_call_offset_blocks == 0) { |
| 1585 | throw std::runtime_error("HeartCodec CUDA randn call offset must be positive"); |
| 1586 | } |
| 1587 | auto latents = engine::sampling::generate_torch_cuda_tensor_iterator_randn( |
| 1588 | static_cast<size_t>(conditioning.frames * config.out_channels), |
| 1589 | seed, |
| 1590 | randn_philox_offset, |
| 1591 | sampling_policy); |
| 1592 | randn_philox_offset += randn_call_offset_blocks; |
| 1593 | std::vector<float> incontext(static_cast<size_t>(conditioning.frames * config.out_channels), 0.0F); |
| 1594 | if (static_cast<int64_t>(true_latents.size()) != latent_length * config.out_channels) { |
| 1595 | throw std::runtime_error("HeartCodec true-latent payload shape mismatch"); |
| 1596 | } |
no test coverage detected