| 1920 | } |
| 1921 | |
| 1922 | HeartMuLaBackboneResult HeartMuLaWeightsRuntime::backbone_cached_step( |
| 1923 | const std::vector<float> & embedding, |
| 1924 | int64_t batch_size, |
| 1925 | HeartMuLaBackboneCachedState & state, |
| 1926 | int64_t cache_capacity) const { |
| 1927 | const auto & config = assets_->mula_config.backbone; |
| 1928 | if (batch_size <= 0) { |
| 1929 | throw std::runtime_error("HeartMuLa backbone cached step requires positive batch size"); |
| 1930 | } |
| 1931 | if (cache_capacity <= 0) { |
| 1932 | throw std::runtime_error("HeartMuLa backbone cached step requires positive cache capacity"); |
| 1933 | } |
| 1934 | if (cache_capacity > config.max_seq_len) { |
| 1935 | throw std::runtime_error("HeartMuLa backbone cached step exceeds model context length"); |
| 1936 | } |
| 1937 | if (static_cast<int64_t>(embedding.size()) != batch_size * config.embed_dim) { |
| 1938 | throw std::runtime_error("HeartMuLa backbone cached step embedding payload size mismatch"); |
| 1939 | } |
| 1940 | const int64_t current_end = state.graph_has_state_ && backbone_cached_step_graph_ != nullptr |
| 1941 | ? backbone_cached_step_graph_->current_end() |
| 1942 | : state.pending_state_.current_end; |
| 1943 | const int64_t required_capacity = current_end + 1; |
| 1944 | if (required_capacity > config.max_seq_len) { |
| 1945 | throw std::runtime_error("HeartMuLa backbone cached step capacity exceeds model context length"); |
| 1946 | } |
| 1947 | const int64_t prompt_steps = state.prefill_steps_; |
| 1948 | const int64_t old_generated_capacity = backbone_cached_step_graph_ == nullptr |
| 1949 | ? 0 |
| 1950 | : std::max<int64_t>(1, backbone_cached_step_graph_->cache_steps() - prompt_steps); |
| 1951 | const int64_t required_generated_capacity = std::max<int64_t>(1, required_capacity - prompt_steps); |
| 1952 | const int64_t max_generated_capacity = std::max<int64_t>(1, cache_capacity - prompt_steps); |
| 1953 | const int64_t next_generated_capacity = std::min<int64_t>( |
| 1954 | max_generated_capacity, |
| 1955 | std::max<int64_t>( |
| 1956 | required_generated_capacity, |
| 1957 | old_generated_capacity > 0 ? old_generated_capacity * 2 : kInitialBackboneGeneratedCacheFrames)); |
| 1958 | const int64_t graph_capacity = prompt_steps + next_generated_capacity; |
| 1959 | if (backbone_cached_step_graph_ != nullptr && state.graph_has_state_ && |
| 1960 | !backbone_cached_step_graph_->can_run(*this, batch_size, required_capacity)) { |
| 1961 | state.pending_state_ = backbone_cached_step_graph_->export_state(); |
| 1962 | state.graph_has_state_ = false; |
| 1963 | } |
| 1964 | if (backbone_cached_step_graph_ == nullptr || |
| 1965 | !backbone_cached_step_graph_->can_run(*this, batch_size, required_capacity)) { |
| 1966 | backbone_cached_step_graph_.reset(); |
| 1967 | backbone_cached_step_graph_ = std::make_unique<HeartMuLaBackboneCachedStepGraph>( |
| 1968 | *this, |
| 1969 | batch_size, |
| 1970 | graph_capacity); |
| 1971 | } |
| 1972 | if (!state.graph_has_state_) { |
| 1973 | backbone_cached_step_graph_->import_state(state.pending_state_); |
| 1974 | state.pending_state_ = {}; |
| 1975 | state.graph_has_state_ = true; |
| 1976 | } |
| 1977 | return backbone_cached_step_graph_->run_step(embedding); |
| 1978 | } |
| 1979 |
no test coverage detected