rope_head.comp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "types.comp"
  2. #extension GL_EXT_shader_16bit_storage : require
  3. layout(local_size_x = 1, local_size_y = 256, local_size_z = 1) in;
  4. layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
  5. layout (binding = 1) readonly buffer Y {int data_pos[];};
  6. layout (binding = 2) readonly buffer Z {float data_ff[];};
  7. layout (binding = 3) writeonly buffer D {D_TYPE data_d[];};
  8. layout (push_constant) uniform parameter {
  9. uint ncols;
  10. uint n_dims;
  11. float freq_scale;
  12. uint p_delta_rows;
  13. float freq_base;
  14. float ext_factor;
  15. float attn_factor;
  16. float corr_dims[2];
  17. float theta_scale;
  18. uint has_ff;
  19. } p;
  20. float rope_yarn_ramp(const float low, const float high, const uint i0) {
  21. const float y = (i0 / 2 - low) / max(0.001f, high - low);
  22. return 1.0f - min(1.0f, max(0.0f, y));
  23. }
  24. void rope_yarn(const float theta_extrap, const uint i0, out float cos_theta, out float sin_theta) {
  25. float mscale = p.attn_factor;
  26. // Get n-d rotational scaling corrected for extrapolation
  27. float theta_interp = p.freq_scale * theta_extrap;
  28. float theta = theta_interp;
  29. if (p.ext_factor != 0.0f) {
  30. float ramp_mix = rope_yarn_ramp(p.corr_dims[0], p.corr_dims[1], i0) * p.ext_factor;
  31. theta = theta_interp * (1 - ramp_mix) + theta_extrap * ramp_mix;
  32. // Get n-d magnitude scaling corrected for interpolation
  33. mscale *= 1.0f + 0.1f * log(1.0f / p.freq_scale);
  34. }
  35. cos_theta = cos(theta) * mscale;
  36. sin_theta = sin(theta) * mscale;
  37. }