| 3261 | } |
| 3262 | |
| 3263 | void update_act_state( |
| 3264 | resizable_tensor& output, |
| 3265 | const tensor& input_data, |
| 3266 | const tensor& halt_probs, |
| 3267 | resizable_tensor& cumulative_halting, |
| 3268 | resizable_tensor& remainders, |
| 3269 | resizable_tensor& n_steps, |
| 3270 | resizable_tensor& effective_weights, |
| 3271 | long batch_size, |
| 3272 | long seq_len, |
| 3273 | long d_model, |
| 3274 | long num_channels, |
| 3275 | float halt_threshold, |
| 3276 | long current_step |
| 3277 | ) |
| 3278 | { |
| 3279 | const float* in_ptr = input_data.host(); |
| 3280 | const float* p_halt = halt_probs.host(); |
| 3281 | float* out_ptr = output.host(); |
| 3282 | float* cum_halt = cumulative_halting.host(); |
| 3283 | float* remain = remainders.host(); |
| 3284 | float* steps = n_steps.host(); |
| 3285 | float* eff_weights = effective_weights.host(); |
| 3286 | |
| 3287 | for (long pos = 0; pos < batch_size * seq_len; ++pos) { |
| 3288 | if (cum_halt[pos] < halt_threshold) { |
| 3289 | const long n = pos / seq_len; |
| 3290 | const long s = pos % seq_len; |
| 3291 | |
| 3292 | float p = p_halt[pos]; |
| 3293 | float r = remain[pos]; |
| 3294 | float effective = std::min(p * r, halt_threshold - cum_halt[pos]); |
| 3295 | |
| 3296 | cum_halt[pos] += effective; |
| 3297 | remain[pos] -= effective; |
| 3298 | steps[pos] = static_cast<float>(current_step + 1); |
| 3299 | eff_weights[pos] += effective; |
| 3300 | |
| 3301 | for (long c = 0; c < num_channels; ++c) { |
| 3302 | for (long d = 0; d < d_model; ++d) { |
| 3303 | const long idx = ((n * num_channels + c) * seq_len + s) * d_model + d; |
| 3304 | out_ptr[idx] += effective * in_ptr[idx]; |
| 3305 | } |
| 3306 | } |
| 3307 | } |
| 3308 | } |
| 3309 | } |
| 3310 | |
| 3311 | void finalize_act_output( |
| 3312 | resizable_tensor& output, |