|
| 1 | +#include "ctranslate2/models/wavlm.h" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | + |
| 5 | +#include "ctranslate2/decoding.h" |
| 6 | + |
| 7 | +#include "dispatch.h" |
| 8 | +#include "dtw.h" |
| 9 | + |
| 10 | +#ifdef CT2_WITH_CUDA |
| 11 | +# include "cuda/utils.h" |
| 12 | +#endif |
| 13 | + |
| 14 | + |
| 15 | +namespace ctranslate2 { |
| 16 | + namespace models { |
| 17 | + |
| 18 | + const Vocabulary& WavLMModel::get_vocabulary() const { |
| 19 | + return *_vocabulary; |
| 20 | + } |
| 21 | + |
| 22 | + size_t WavLMModel::current_spec_revision() const { |
| 23 | + return 3; |
| 24 | + } |
| 25 | + |
| 26 | + void WavLMModel::initialize(ModelReader& model_reader) { |
| 27 | + VocabularyInfo vocab_info; |
| 28 | + vocab_info.unk_token = "[UNK]"; |
| 29 | + vocab_info.bos_token = "<s>"; |
| 30 | + vocab_info.eos_token = "</s>"; |
| 31 | + |
| 32 | + _vocabulary = load_vocabulary(model_reader, "vocabulary", std::move(vocab_info)); |
| 33 | + if (!_vocabulary) |
| 34 | + throw std::runtime_error("Cannot load the vocabulary from the model directory"); |
| 35 | + } |
| 36 | + |
| 37 | + bool WavLMModel::is_quantizable(const std::string& variable_name) const { |
| 38 | + return Model::is_quantizable(variable_name); |
| 39 | + } |
| 40 | + |
| 41 | + bool WavLMModel::is_linear_weight(const std::string& variable_name) const { |
| 42 | + return is_quantizable(variable_name) && variable_name.find("embeddings") == std::string::npos; |
| 43 | + } |
| 44 | + |
| 45 | + std::unique_ptr<Model> WavLMModel::clone() const { |
| 46 | + return std::make_unique<WavLMModel>(*this); |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + std::unique_ptr<WavLMReplica> WavLMReplica::create_from_model(const Model& model) { |
| 51 | + if (!dynamic_cast<const WavLMModel*>(&model)) |
| 52 | + throw std::invalid_argument("The model is not a WavLM model"); |
| 53 | + |
| 54 | + const auto scoped_device_setter = model.get_scoped_device_setter(); |
| 55 | + const auto model_ptr = model.shared_from_this(); |
| 56 | + const auto concrete_model = std::static_pointer_cast<const WavLMModel>(model_ptr); |
| 57 | + return std::make_unique<WavLMReplica>(concrete_model); |
| 58 | + } |
| 59 | + |
| 60 | + WavLMReplica::WavLMReplica(const std::shared_ptr<const WavLMModel>& model) |
| 61 | + : ModelReplica(model) |
| 62 | + , _model(model) |
| 63 | + , _encoder(std::make_unique<layers::WavLMEncoder>(*model, "encoder")) |
| 64 | + { |
| 65 | + } |
| 66 | + |
| 67 | + StorageView WavLMReplica::encode(StorageView features, const bool to_cpu) { |
| 68 | + PROFILE("WavLMReplica::encode"); |
| 69 | + |
| 70 | +#ifdef CT2_WITH_CUDA |
| 71 | + const cuda::UseTrueFp16GemmInScope use_true_fp16_gemm(false); |
| 72 | +#endif |
| 73 | + |
| 74 | + const auto scoped_device_setter = _model->get_scoped_device_setter(); |
| 75 | + const Device device = _model->device(); |
| 76 | + const DataType dtype = _encoder->output_type(); |
| 77 | + features.move_to(device, dtype); |
| 78 | + |
| 79 | + StorageView encoder_output(dtype, device); |
| 80 | + (*_encoder)(features, encoder_output); |
| 81 | + |
| 82 | + if (to_cpu) { |
| 83 | + if (device != Device::CPU) |
| 84 | + encoder_output = encoder_output.to(Device::CPU); |
| 85 | + |
| 86 | + return encoder_output; |
| 87 | + } |
| 88 | + |
| 89 | + // Ensure all operations are finished before returning the output. |
| 90 | + synchronize_stream(device); |
| 91 | + |
| 92 | + return encoder_output; |
| 93 | + } |
| 94 | + |
| 95 | + std::future<StorageView> WavLM::encode(const StorageView& features, const bool to_cpu) { |
| 96 | + return post<StorageView>( |
| 97 | + [features = features.sync_copy(), to_cpu](WavLMReplica& replica) mutable { |
| 98 | + return replica.encode(std::move(features), to_cpu); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | +} |
0 commit comments