|
| 1 | +using System.Globalization; |
| 2 | +using System.Text; |
| 3 | +using LLama; |
| 4 | +using LLama.Common; |
| 5 | +using LLama.Native; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using Microsoft.Extensions.Options; |
| 8 | +using WindowTranslator.Modules; |
| 9 | +using WindowTranslator.Plugin.PLaMoPlugin.Properties; |
| 10 | + |
| 11 | +namespace WindowTranslator.Plugin.PLaMoPlugin; |
| 12 | + |
| 13 | +public sealed class PLaMoTranslator : ITranslateModule, IDisposable |
| 14 | +{ |
| 15 | + private readonly string sourceLang; |
| 16 | + private readonly string targetLang; |
| 17 | + private readonly LLamaWeights? weights; |
| 18 | + private readonly ModelParams? modelParams; |
| 19 | + private readonly ILogger<PLaMoTranslator> logger; |
| 20 | + private readonly InferenceParams inferenceParams; |
| 21 | + |
| 22 | + static PLaMoTranslator() |
| 23 | + => NativeLibraryConfig.LLama.WithSelectingPolicy(LLamaSharpNativeLibrarySelectingPolicy.Instance); |
| 24 | + |
| 25 | + public PLaMoTranslator(IOptionsSnapshot<PLaMoOptions> plamoOptions, IOptionsSnapshot<LanguageOptions> langOptions, ILogger<PLaMoTranslator> logger) |
| 26 | + { |
| 27 | + var options = plamoOptions.Value; |
| 28 | + |
| 29 | + // PLaMoモデル用の言語名を取得 |
| 30 | + this.sourceLang = GetLanguageName(langOptions.Value.Source); |
| 31 | + this.targetLang = GetLanguageName(langOptions.Value.Target); |
| 32 | + |
| 33 | + // ダウンロードされたモデルのパスを取得 |
| 34 | + var modelPath = PLaMoOptions.ModelPath; |
| 35 | + |
| 36 | + if (!File.Exists(modelPath)) |
| 37 | + { |
| 38 | + throw new AppUserException(Resources.ModelFileNotFound); |
| 39 | + } |
| 40 | + |
| 41 | + if (!NativeLibraryConfig.LLama.LibraryHasLoaded) |
| 42 | + { |
| 43 | + NativeLibraryConfig.LLama.WithLogCallback(logger); |
| 44 | + } |
| 45 | + |
| 46 | + this.modelParams = new ModelParams(modelPath) |
| 47 | + { |
| 48 | + ContextSize = (uint)options.ContextSize, |
| 49 | + GpuLayerCount = CalcGpuLayersFromGB(options.VRAM, options.ContextSize), |
| 50 | + }; |
| 51 | + this.inferenceParams = new InferenceParams |
| 52 | + { |
| 53 | + MaxTokens = options.ContextSize / 2, |
| 54 | + AntiPrompts = ["<|plamo:op|>"], |
| 55 | + }; |
| 56 | + |
| 57 | + this.weights = LLamaWeights.LoadFromFile(this.modelParams); |
| 58 | + this.logger = logger; |
| 59 | + } |
| 60 | + |
| 61 | + private static string GetLanguageName(string cultureCode) |
| 62 | + { |
| 63 | + // PLaMoモデルが認識する言語名に変換 |
| 64 | + var culture = CultureInfo.GetCultureInfo(cultureCode); |
| 65 | + return culture.TwoLetterISOLanguageName switch |
| 66 | + { |
| 67 | + "ja" => "Japanese", |
| 68 | + "en" => "English", |
| 69 | + "zh" => "Chinese", |
| 70 | + "ko" => "Korean", |
| 71 | + "de" => "German", |
| 72 | + "fr" => "French", |
| 73 | + "es" => "Spanish", |
| 74 | + "it" => "Italian", |
| 75 | + "pt" => "Portuguese", |
| 76 | + "ru" => "Russian", |
| 77 | + "ar" => "Arabic", |
| 78 | + "vi" => "Vietnamese", |
| 79 | + _ => culture.EnglishName.Split(' ')[0] // フォールバック: 英語名の最初の単語 |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + static int CalcGpuLayersFromGB(int vramGB, int ctx, double L = 0.17, double W = 1.30) |
| 84 | + { |
| 85 | + if (vramGB < 0) return -1; |
| 86 | + if (vramGB == 0) return 0; |
| 87 | + var kvGB = Math.Ceiling(40.0 * ctx / 1024.0) / 1024.0; // 40KB/token, safe |
| 88 | + var n = (int)Math.Floor((vramGB - W - kvGB) / L); |
| 89 | + if (n < 0) return 0; |
| 90 | + if (n > 32) return -1; |
| 91 | + return n; |
| 92 | + } |
| 93 | + |
| 94 | + public async ValueTask<string[]> TranslateAsync(TextInfo[] srcTexts) |
| 95 | + { |
| 96 | + if (this.weights is null || this.modelParams is null) |
| 97 | + { |
| 98 | + throw new InvalidOperationException(Resources.ModelNotInitialized); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + using var context = this.weights.CreateContext(this.modelParams, this.logger); |
| 104 | + var executor = new StatelessExecutor(this.weights, this.modelParams); |
| 105 | + var responseBuilder = new StringBuilder(); |
| 106 | + var responses = new List<string>(); |
| 107 | + |
| 108 | + foreach (var text in srcTexts) |
| 109 | + { |
| 110 | + // PLaMo専用のプロンプトフォーマット |
| 111 | + var prompt = $""" |
| 112 | + <|plamo:op|>dataset |
| 113 | + translation |
| 114 | + <|plamo:op|>input lang={this.sourceLang} |
| 115 | + {text.SourceText} |
| 116 | + <|plamo:op|>output lang={this.targetLang} |
| 117 | +
|
| 118 | + """.ReplaceLineEndings("\n"); |
| 119 | + responseBuilder.Clear(); |
| 120 | + await foreach (var token in executor.InferAsync(prompt, this.inferenceParams)) |
| 121 | + { |
| 122 | + responseBuilder.Append(token); |
| 123 | + } |
| 124 | + var response = responseBuilder.ToString().Trim(); |
| 125 | + responses.Add(response); |
| 126 | + this.logger.LogDebug("PLaMo translated: {Original} => {Translated}", text.SourceText, response); |
| 127 | + } |
| 128 | + |
| 129 | + return [.. responses]; |
| 130 | + } |
| 131 | + |
| 132 | + // PLaMoモデルは用語集をサポートしないため、何もしない |
| 133 | + public ValueTask RegisterGlossaryAsync(IReadOnlyDictionary<string, string> glossary) |
| 134 | + => default; |
| 135 | + |
| 136 | + // PLaMoモデルはコンテキストをサポートしないため、何もしない |
| 137 | + public void RegisterContext(string context) |
| 138 | + { |
| 139 | + } |
| 140 | + |
| 141 | + public void Dispose() |
| 142 | + { |
| 143 | + this.weights?.Dispose(); |
| 144 | + } |
| 145 | +} |
0 commit comments