|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "ctranslate2/generation.h" |
| 4 | +#include "ctranslate2/layers/moonshine.h" |
| 5 | +#include "ctranslate2/models/model.h" |
| 6 | +#include "ctranslate2/replica_pool.h" |
| 7 | + |
| 8 | +namespace ctranslate2 { |
| 9 | + namespace models { |
| 10 | + |
| 11 | + struct MoonshineOptions { |
| 12 | + // Beam size to use for beam search (set 1 to run greedy search). |
| 13 | + size_t beam_size = 5; |
| 14 | + |
| 15 | + // Beam search patience factor, as described in https://arxiv.org/abs/2204.05424. |
| 16 | + // The decoding will continue until beam_size*patience hypotheses are finished. |
| 17 | + float patience = 1; |
| 18 | + |
| 19 | + // Exponential penalty applied to the length during beam search. |
| 20 | + float length_penalty = 1; |
| 21 | + |
| 22 | + // Penalty applied to the score of previously generated tokens, as described in |
| 23 | + // https://arxiv.org/abs/1909.05858 (set > 1 to penalize). |
| 24 | + float repetition_penalty = 1; |
| 25 | + |
| 26 | + // Prevent repetitions of ngrams with this size (set 0 to disable). |
| 27 | + size_t no_repeat_ngram_size = 0; |
| 28 | + |
| 29 | + // Maximum generation length. |
| 30 | + size_t max_length = 448; |
| 31 | + |
| 32 | + // Randomly sample from the top K candidates (set 0 to sample from the full distribution). |
| 33 | + size_t sampling_topk = 1; |
| 34 | + |
| 35 | + // High temperatures increase randomness. |
| 36 | + float sampling_temperature = 1; |
| 37 | + |
| 38 | + // Number of hypotheses to include in the result. |
| 39 | + size_t num_hypotheses = 1; |
| 40 | + |
| 41 | + // Include scores in the result. |
| 42 | + bool return_scores = false; |
| 43 | + |
| 44 | + // Suppress blank outputs at the beginning of the sampling. |
| 45 | + bool suppress_blank = true; |
| 46 | + |
| 47 | + // List of token IDs to suppress. |
| 48 | + // -1 will suppress a default set of symbols as defined in the model config.json file. |
| 49 | + std::vector<int> suppress_tokens = {-1}; |
| 50 | + }; |
| 51 | + |
| 52 | + struct MoonshineGenerationResult { |
| 53 | + std::vector<std::vector<std::string>> sequences; |
| 54 | + std::vector<std::vector<size_t>> sequences_ids; |
| 55 | + std::vector<float> scores; |
| 56 | + |
| 57 | + size_t num_sequences() const { |
| 58 | + return sequences.size(); |
| 59 | + } |
| 60 | + |
| 61 | + bool has_scores() const { |
| 62 | + return !scores.empty(); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + class MoonshineModel : public Model { |
| 67 | + public: |
| 68 | + const Vocabulary& get_vocabulary() const; |
| 69 | + |
| 70 | + size_t current_spec_revision() const override; |
| 71 | + bool is_quantizable(const std::string& variable_name) const override; |
| 72 | + bool is_linear_weight(const std::string& variable_name) const override; |
| 73 | + std::unique_ptr<Model> clone() const override; |
| 74 | + |
| 75 | + bool use_global_int16_scale() const override { |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + protected: |
| 80 | + void initialize(ModelReader& model_reader) override; |
| 81 | + |
| 82 | + private: |
| 83 | + std::shared_ptr<const Vocabulary> _vocabulary; |
| 84 | + }; |
| 85 | + |
| 86 | + class MoonshineReplica : public ModelReplica { |
| 87 | + public: |
| 88 | + static std::unique_ptr<MoonshineReplica> create_from_model(const Model& model); |
| 89 | + |
| 90 | + MoonshineReplica(const std::shared_ptr<const MoonshineModel>& model); |
| 91 | + |
| 92 | + StorageView encode(StorageView features, const bool to_cpu); |
| 93 | + |
| 94 | + std::vector<MoonshineGenerationResult> |
| 95 | + generate(StorageView features, |
| 96 | + const std::vector<std::vector<std::string>>& prompts, |
| 97 | + const MoonshineOptions& options); |
| 98 | + |
| 99 | + std::vector<MoonshineGenerationResult> |
| 100 | + generate(StorageView features, |
| 101 | + const std::vector<std::vector<size_t>>& prompts, |
| 102 | + const MoonshineOptions& options); |
| 103 | + |
| 104 | + private: |
| 105 | + const std::shared_ptr<const MoonshineModel> _model; |
| 106 | + const std::unique_ptr<layers::MoonshinePreprocessor> _preprocessor; |
| 107 | + const std::unique_ptr<layers::MoonshineEncoder> _encoder; |
| 108 | + const std::unique_ptr<layers::MoonshineDecoder> _decoder; |
| 109 | + |
| 110 | + size_t _sot_id; |
| 111 | + size_t _eot_id; |
| 112 | + |
| 113 | + StorageView maybe_encode(StorageView features); |
| 114 | + }; |
| 115 | + |
| 116 | + class Moonshine : public ReplicaPool<MoonshineReplica> { |
| 117 | + public: |
| 118 | + using ReplicaPool::ReplicaPool; |
| 119 | + |
| 120 | + std::future<StorageView> encode(const StorageView& features, const bool to_cpu); |
| 121 | + |
| 122 | + std::vector<std::future<MoonshineGenerationResult>> |
| 123 | + generate(const StorageView& features, |
| 124 | + std::vector<std::vector<std::string>> prompts, |
| 125 | + MoonshineOptions options = {}); |
| 126 | + |
| 127 | + std::vector<std::future<MoonshineGenerationResult>> |
| 128 | + generate(const StorageView& features, |
| 129 | + std::vector<std::vector<size_t>> prompts, |
| 130 | + MoonshineOptions options = {}); |
| 131 | + }; |
| 132 | + |
| 133 | + } |
| 134 | +} |
0 commit comments