-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathTextClassificationTrainer.cs
378 lines (322 loc) · 17.1 KB
/
TextClassificationTrainer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Data.IO;
using Microsoft.ML.Internal.Utilities;
using Microsoft.ML.Runtime;
using Microsoft.ML.Tokenizers;
using Microsoft.ML.TorchSharp.Extensions;
using Microsoft.ML.TorchSharp.NasBert;
using Microsoft.ML.TorchSharp.NasBert.Models;
using TorchSharp;
using static Microsoft.ML.TorchSharp.NasBert.NasBertTrainer;
[assembly: LoadableClass(typeof(TextClassificationTransformer), null, typeof(SignatureLoadModel),
TextClassificationTransformer.UserName, TextClassificationTransformer.LoaderSignature)]
[assembly: LoadableClass(typeof(IRowMapper), typeof(TextClassificationTransformer), null, typeof(SignatureLoadRowMapper),
TextClassificationTransformer.UserName, TextClassificationTransformer.LoaderSignature)]
namespace Microsoft.ML.TorchSharp.NasBert
{
/// <summary>
/// The <see cref="IEstimator{TTransformer}"/> for training a Deep Neural Network (DNN) to classify text.
/// </summary>
/// <remarks>
/// <format type="text/markdown"><).
///
/// ### Input and output columns
/// The input label column data must be [key](xref:Microsoft.ML.Data.KeyDataViewType) type and the sentence columns must be of type <xref:Microsoft.ML.Data.TextDataViewType>.
///
/// This trainer outputs the following columns:
///
/// | Output column name | Column type | Description|
/// | -- | -- | -- |
/// | `PredictedLabel` | [key](xref:Microsoft.ML.Data.KeyDataViewType) type | The predicted label's index. If its value is `i`, the actual label would be the `i`-th category in the key-valued input label type. |
/// | `Score` | Vector of<xref:System.Single> | The scores of all classes. Higher value means higher probability to fall into the associated class. If the `i`-th element has the largest value, the predicted label index would be `i`. Note that `i` is a zero-based index. |
///
/// ### Trainer characteristics
/// | Characteristic | Value |
/// | -- | -- |
/// | Machine learning task | Multiclass classification |
/// | Is normalization required? | No |
/// | Is caching required? | No |
/// | Required NuGet in addition to Microsoft.ML | Microsoft.ML.TorchSharp and libtorch-cpu or libtorch-cuda-11.3 or any of the OS specific variants. |
/// | Exportable to ONNX | No |
///
/// ### Training algorithm details
/// Trains a Deep Neural Network (DNN) by leveraging an existing, pretrained NAS-BERT roBERTa model for the purpose of classifying text.
/// ]]>
/// </format>
/// </remarks>
///
public class TextClassificationTrainer : NasBertTrainer<UInt32, long>
{
public class TextClassificationOptions : NasBertTrainer.NasBertOptions
{
public TextClassificationOptions()
{
TaskType = BertTaskType.TextClassification;
BatchSize = 32;
MaxEpoch = 10;
}
}
internal TextClassificationTrainer(IHostEnvironment env, TextClassificationOptions options) : base(env, options)
{
}
internal TextClassificationTrainer(IHostEnvironment env,
string labelColumnName = DefaultColumnNames.Label,
string predictionColumnName = DefaultColumnNames.PredictedLabel,
string scoreColumnName = DefaultColumnNames.Score,
string sentence1ColumnName = "Sentence1",
string sentence2ColumnName = default,
int batchSize = 32,
int maxEpochs = 10,
IDataView validationSet = null,
BertArchitecture architecture = BertArchitecture.Roberta) :
this(env, new TextClassificationOptions
{
PredictionColumnName = predictionColumnName,
ScoreColumnName = scoreColumnName,
Sentence1ColumnName = sentence1ColumnName,
Sentence2ColumnName = sentence2ColumnName,
LabelColumnName = labelColumnName,
BatchSize = batchSize,
MaxEpoch = maxEpochs,
ValidationSet = validationSet,
TaskType = BertTaskType.TextClassification
})
{
}
private protected override TrainerBase CreateTrainer(TorchSharpBaseTrainer<uint, long> parent, IChannel ch, IDataView input)
{
return new Trainer(parent, ch, input);
}
private protected override TorchSharpBaseTransformer<uint, long> CreateTransformer(IHost host, Options options, torch.nn.Module model, DataViewSchema.DetachedColumn labelColumn)
{
return new TextClassificationTransformer(host, options as NasBertOptions, model as NasBertModel, labelColumn);
}
private protected class Trainer : NasBertTrainerBase
{
private const string ModelUrlString = "models/NasBert2000000.tsm";
public Trainer(TorchSharpBaseTrainer<uint, long> parent, IChannel ch, IDataView input) : base(parent, ch, input, ModelUrlString)
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private protected override long AddToTargets(uint target)
{
// keys are 1 based but the model is 0 based
return target - 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private protected override torch.Tensor CreateTargetsTensor(ref List<long> targets, torch.Device device)
{
return torch.tensor(targets, device: Device);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private protected override int GetNumCorrect(torch.Tensor predictions, torch.Tensor targets)
{
predictions = predictions ?? throw new ArgumentNullException(nameof(predictions));
return (int)predictions.eq(targets).sum().ToInt64();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private protected override torch.Tensor GetPredictions(torch.Tensor logits)
{
logits = logits ?? throw new ArgumentNullException(nameof(logits));
var (_, indexes) = logits.max(-1, false);
return indexes;
}
private protected override int GetRowCountAndSetLabelCount(IDataView input)
{
var labelCol = input.GetColumn<uint>(Parent.Option.LabelColumnName);
var rowCount = 0;
var uniqueLabels = new HashSet<uint>();
foreach (var label in labelCol)
{
rowCount++;
uniqueLabels.Add(label);
}
Parent.Option.NumberOfClasses = uniqueLabels.Count;
return rowCount;
}
private protected override torch.Tensor GetTargets(torch.Tensor labels)
{
return labels.view(-1);
}
}
}
public sealed class TextClassificationTransformer : NasBertTransformer<UInt32, long>
{
internal const string LoadName = "TextClassTrainer";
internal const string UserName = "Text Classification Trainer";
internal const string ShortName = "TXTCLSS";
internal const string Summary = "NLP with NAS-BERT";
internal const string LoaderSignature = "TXTCLSS";
private static readonly FuncStaticMethodInfo1<object, Delegate> _decodeInitMethodInfo
= new FuncStaticMethodInfo1<object, Delegate>(DecodeInit<int>);
private static VersionInfo GetVersionInfo()
{
return new VersionInfo(
modelSignature: "TXT-CLSS",
//verWrittenCur: 0x00010001, // Initial
verWrittenCur: 0x00010002, // New refactor format
verReadableCur: 0x00010002,
verWeCanReadBack: 0x00010002,
loaderSignature: LoaderSignature,
loaderAssemblyName: typeof(TextClassificationTransformer).Assembly.FullName);
}
internal TextClassificationTransformer(IHostEnvironment env, NasBertOptions options, NasBertModel model, DataViewSchema.DetachedColumn labelColumn) : base(env, options, model, labelColumn)
{
}
private protected override IRowMapper GetRowMapper(TorchSharpBaseTransformer<uint, long> parent, DataViewSchema schema)
{
return new Mapper(parent, schema);
}
private protected override void SaveModel(ModelSaveContext ctx)
{
// *** Binary format ***
// BaseModel
// int: id of label column name
// int: id of the score column name
// int: id of output column name
// int: number of classes
// BinaryStream: TS Model
// int: id of sentence 1 column name
// int: id of sentence 2 column name
// LabelValues
SaveBaseModel(ctx, GetVersionInfo());
var labelColType = LabelColumn.Annotations.Schema[AnnotationUtils.Kinds.KeyValues].Type as VectorDataViewType;
Microsoft.ML.Internal.Utilities.Utils.MarshalActionInvoke(SaveLabelValues<int>, labelColType.ItemType.RawType, ctx);
}
private void SaveLabelValues<T>(ModelSaveContext ctx)
{
ValueGetter<VBuffer<T>> getter = LabelColumn.Annotations.GetGetter<VBuffer<T>>(LabelColumn.Annotations.Schema[AnnotationUtils.Kinds.KeyValues]);
var val = default(VBuffer<T>);
getter(ref val);
BinarySaver saver = new BinarySaver(Host, new BinarySaver.Arguments());
int bytesWritten;
var labelColType = LabelColumn.Annotations.Schema[AnnotationUtils.Kinds.KeyValues].Type as VectorDataViewType;
if (!saver.TryWriteTypeAndValue<VBuffer<T>>(ctx.Writer.BaseStream, labelColType, ref val, out bytesWritten))
throw Host.Except("We do not know how to serialize label names of type '{0}'", labelColType.ItemType);
}
//Factory method for SignatureLoadRowMapper.
private static IRowMapper Create(IHostEnvironment env, ModelLoadContext ctx, DataViewSchema inputSchema)
=> Create(env, ctx).MakeRowMapper(inputSchema);
// Factory method for SignatureLoadModel.
private static TextClassificationTransformer Create(IHostEnvironment env, ModelLoadContext ctx)
{
Contracts.CheckValue(env, nameof(env));
env.CheckValue(ctx, nameof(ctx));
ctx.CheckAtModel(GetVersionInfo());
// *** Binary format ***
// BaseModel
// int: id of label column name
// int: id of the score column name
// int: id of output column name
// int: number of classes
// BinaryStream: TS Model
// int: id of sentence 1 column name
// int: id of sentence 2 column name
// LabelValues
var options = new NasBertOptions()
{
LabelColumnName = ctx.LoadString(),
ScoreColumnName = ctx.LoadString(),
PredictionColumnName = ctx.LoadString(),
NumberOfClasses = ctx.Reader.ReadInt32(),
};
var ch = env.Start("Load Model");
var tokenizer = TokenizerExtensions.GetInstance(ch);
EnglishRobertaTokenizer tokenizerModel = tokenizer.RobertaModel();
var model = new ModelForPrediction(options, tokenizerModel.PadIndex, tokenizerModel.SymbolsCount, options.NumberOfClasses);
if (!ctx.TryLoadBinaryStream("TSModel", r => model.load(r)))
throw env.ExceptDecode();
options.Sentence1ColumnName = ctx.LoadString();
options.Sentence2ColumnName = ctx.LoadStringOrNull();
options.TaskType = BertTaskType.TextClassification;
BinarySaver saver = new BinarySaver(env, new BinarySaver.Arguments());
DataViewType type;
object value;
env.CheckDecode(saver.TryLoadTypeAndValue(ctx.Reader.BaseStream, out type, out value));
var vecType = type as VectorDataViewType;
env.CheckDecode(vecType != null);
env.CheckDecode(value != null);
var labelGetter = Microsoft.ML.Internal.Utilities.Utils.MarshalInvoke(_decodeInitMethodInfo, vecType.ItemType.RawType, value);
var meta = new DataViewSchema.Annotations.Builder();
meta.Add(AnnotationUtils.Kinds.KeyValues, type, labelGetter);
var labelCol = new DataViewSchema.DetachedColumn(options.LabelColumnName, type, meta.ToAnnotations());
return new TextClassificationTransformer(env, options, model, labelCol);
}
private static Delegate DecodeInit<T>(object value)
{
VBuffer<T> buffValue = (VBuffer<T>)value;
ValueGetter<VBuffer<T>> buffGetter = (ref VBuffer<T> dst) => buffValue.CopyTo(ref dst);
return buffGetter;
}
private sealed class Mapper : NasBertMapper
{
public Mapper(TorchSharpBaseTransformer<uint, long> parent, DataViewSchema inputSchema) : base(parent, inputSchema)
{
}
private protected override Delegate CreateGetter(DataViewRow input, int iinfo, TensorCacher outputCacher)
{
var ch = Host.Start("Make Getter");
if (iinfo == 0)
return MakePredictedLabelGetter(input, ch, outputCacher);
else
return MakeScoreGetter(input, ch, outputCacher);
}
private Delegate MakeScoreGetter(DataViewRow input, IChannel ch, TensorCacher outputCacher)
{
ValueGetter<ReadOnlyMemory<char>> getSentence1 = default;
ValueGetter<ReadOnlyMemory<char>> getSentence2 = default;
Tokenizer tokenizer = TokenizerExtensions.GetInstance(ch);
getSentence1 = input.GetGetter<ReadOnlyMemory<char>>(input.Schema[Parent.SentenceColumn.Name]);
if (Parent.SentenceColumn2.IsValid)
getSentence2 = input.GetGetter<ReadOnlyMemory<char>>(input.Schema[Parent.SentenceColumn2.Name]);
ReadOnlyMemory<char> sentence1 = default;
ReadOnlyMemory<char> sentence2 = default;
ValueGetter<VBuffer<float>> score = (ref VBuffer<float> dst) =>
{
using var disposeScope = torch.NewDisposeScope();
var editor = VBufferEditor.Create(ref dst, Parent.Options.NumberOfClasses);
UpdateCacheIfNeeded(input.Position, outputCacher, ref sentence1, ref sentence2, ref getSentence1, ref getSentence2, tokenizer);
var values = (outputCacher as BertTensorCacher).Result.cpu().ToArray<float>();
for (var i = 0; i < values.Length; i++)
{
editor.Values[i] = values[i];
}
dst = editor.Commit();
};
return score;
}
private Delegate MakePredictedLabelGetter(DataViewRow input, IChannel ch, TensorCacher outputCacher)
{
ValueGetter<ReadOnlyMemory<char>> getSentence1 = default;
ValueGetter<ReadOnlyMemory<char>> getSentence2 = default;
Tokenizer tokenizer = TokenizerExtensions.GetInstance(ch);
getSentence1 = input.GetGetter<ReadOnlyMemory<char>>(input.Schema[Parent.SentenceColumn.Name]);
if (Parent.SentenceColumn2.IsValid)
getSentence2 = input.GetGetter<ReadOnlyMemory<char>>(input.Schema[Parent.SentenceColumn2.Name]);
ReadOnlyMemory<char> sentence1 = default;
ReadOnlyMemory<char> sentence2 = default;
ValueGetter<UInt32> classification = (ref UInt32 dst) =>
{
using var disposeScope = torch.NewDisposeScope();
UpdateCacheIfNeeded(input.Position, outputCacher, ref sentence1, ref sentence2, ref getSentence1, ref getSentence2, tokenizer);
dst = (UInt32)(outputCacher as BertTensorCacher).Result.argmax(-1).cpu().item<long>() + 1;
};
return classification;
}
private protected override Func<int, bool> GetDependenciesCore(Func<int, bool> activeOutput)
{
return col => (activeOutput(0) || activeOutput(1)) && InputColIndices.Any(i => i == col);
}
}
}
}