|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cstddef> |
| 4 | +#include <cstdint> |
| 5 | +#include <filesystem> |
| 6 | +#include <memory> |
| 7 | +#include <string> |
| 8 | +#include <utility> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +namespace robotcpp::starvla { |
| 12 | + |
| 13 | +struct FastCodecConfig { |
| 14 | + double scale = 0.0; |
| 15 | + int32_t min_token = 0; |
| 16 | + size_t vocab_size = 0; |
| 17 | + size_t time_horizon = 0; |
| 18 | + size_t action_dim = 0; |
| 19 | +}; |
| 20 | + |
| 21 | +struct FastDecodeResult { |
| 22 | + size_t batch_size = 0; |
| 23 | + size_t time_horizon = 0; |
| 24 | + size_t action_dim = 0; |
| 25 | + std::vector<double> actions; |
| 26 | +}; |
| 27 | + |
| 28 | +class FastCodec { |
| 29 | +public: |
| 30 | + static std::unique_ptr<FastCodec> create( |
| 31 | + FastCodecConfig config, std::vector<std::string> vocab_by_id, |
| 32 | + std::vector<int32_t> fast_to_vlm_id, std::string & error); |
| 33 | + |
| 34 | + // Constructs directly from the converter-compiled ByteLevel pieces stored |
| 35 | + // in policy GGUF. offsets has vocab_size + 1 entries and indexes the flat |
| 36 | + // byte buffer; no external tokenizer JSON is consulted. |
| 37 | + static std::unique_ptr<FastCodec> create_compiled( |
| 38 | + FastCodecConfig config, std::vector<int32_t> token_offsets, |
| 39 | + std::vector<uint8_t> token_bytes, |
| 40 | + std::vector<int32_t> fast_to_vlm_id, std::string & error); |
| 41 | + |
| 42 | + static std::unique_ptr<FastCodec> load_hf_assets( |
| 43 | + const std::filesystem::path & tokenizer_json, |
| 44 | + const std::filesystem::path & processor_config_json, |
| 45 | + const std::filesystem::path & action_token_map_json, |
| 46 | + size_t time_horizon, size_t action_dim, std::string & error); |
| 47 | + |
| 48 | + const FastCodecConfig & config() const; |
| 49 | + const std::vector<int32_t> & fast_to_vlm_ids() const; |
| 50 | + |
| 51 | + bool map_fast_to_vlm(const std::vector<int32_t> & fast_ids, |
| 52 | + std::vector<int32_t> & vlm_ids, std::string & error) const; |
| 53 | + bool map_vlm_to_fast(const std::vector<int32_t> & vlm_ids, |
| 54 | + std::vector<int32_t> & fast_ids, std::string & error) const; |
| 55 | + |
| 56 | + // Extracts every mapped action token from a generated Qwen sequence in order. |
| 57 | + // EOS stopping remains the generator's responsibility; ordinary EOS/pad/text |
| 58 | + // IDs in the returned sequence are ignored and do not terminate this scan. |
| 59 | + bool extract_fast_tokens(const std::vector<int32_t> & generated_ids, |
| 60 | + std::vector<int32_t> & fast_ids, std::string & error) const; |
| 61 | + |
| 62 | + // Exposed for focused parity diagnostics. This is the Hugging Face ByteLevel |
| 63 | + // decoder output before min_token adjustment and inverse DCT. |
| 64 | + bool byte_level_decode(const std::vector<int32_t> & fast_ids, |
| 65 | + std::vector<uint32_t> & codepoints, std::string & error) const; |
| 66 | + |
| 67 | + bool decode_fast_tokens(const std::vector<std::vector<int32_t>> & batch_fast_ids, |
| 68 | + FastDecodeResult & result, std::string & error) const; |
| 69 | + |
| 70 | + // Strict low-level API: every input ID must be an action token. Use |
| 71 | + // decode_generated_tokens for complete Qwen sequences containing text. |
| 72 | + bool decode_vlm_action_tokens(const std::vector<std::vector<int32_t>> & batch_vlm_ids, |
| 73 | + FastDecodeResult & result, std::string & error) const; |
| 74 | + |
| 75 | + // Production entry point for complete Qwen generated_ids. Ordinary text and |
| 76 | + // control tokens are filtered through the explicit inverse action-token map. |
| 77 | + bool decode_generated_tokens(const std::vector<std::vector<int32_t>> & batch_generated_ids, |
| 78 | + FastDecodeResult & result, std::string & error) const; |
| 79 | + |
| 80 | +private: |
| 81 | + FastCodec(FastCodecConfig config, std::vector<std::vector<uint8_t>> token_bytes, |
| 82 | + std::vector<int32_t> fast_to_vlm_id); |
| 83 | + |
| 84 | + FastCodecConfig config_; |
| 85 | + std::vector<std::vector<uint8_t>> token_bytes_; |
| 86 | + std::vector<int32_t> fast_to_vlm_id_; |
| 87 | + std::vector<std::pair<int32_t, int32_t>> vlm_to_fast_id_; |
| 88 | +}; |
| 89 | + |
| 90 | +} // namespace robotcpp::starvla |
0 commit comments