Pippe commited on
Commit
ca685d7
1 Parent(s): d61a486

Upgraded to sentis 1.4.0-pre3

Browse files
DebertaV3.cs CHANGED
@@ -13,33 +13,52 @@ public sealed class DebertaV3 : MonoBehaviour
13
  public string hypothesisTemplate = "This example is about {}";
14
  public string[] classes = { "politics", "economy", "entertainment", "environment" };
15
 
16
- Ops ops;
17
  IWorker engine;
18
- ITensorAllocator allocator;
19
  string[] vocabularyTokens;
20
 
21
  const int padToken = 0;
22
  const int startToken = 1;
23
  const int separatorToken = 2;
24
  const int vocabToTokenOffset = 260;
25
- const BackendType backend = BackendType.GPUCompute;
26
 
27
  void Start()
28
  {
29
- vocabularyTokens = vocabulary.text.Replace("\r", "").Split("\n");
30
-
31
- allocator = new TensorCachingAllocator();
32
- ops = WorkerFactory.CreateOps(backend, allocator);
33
-
34
- Model loadedModel = ModelLoader.Load(model);
35
- engine = WorkerFactory.CreateWorker(backend, loadedModel);
36
-
37
  if (classes.Length == 0)
38
  {
39
  Debug.LogError("There need to be more than 0 classes");
40
  return;
41
  }
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  string[] hypotheses = classes.Select(x => hypothesisTemplate.Replace("{}", x)).ToArray();
44
  Batch batch = GetTokenizedBatch(text, hypotheses);
45
  float[] scores = GetBatchScores(batch);
@@ -57,15 +76,15 @@ public sealed class DebertaV3 : MonoBehaviour
57
 
58
  Dictionary<string, Tensor> inputs = new()
59
  {
60
- {"input_ids", inputIds},
61
- {"attention_mask", attentionMask}
62
  };
63
 
64
  engine.Execute(inputs);
65
- TensorFloat logits = (TensorFloat)engine.PeekOutput("logits");
66
- float[] scores = ScoresFromLogits(logits);
67
 
68
- return scores;
69
  }
70
 
71
  Batch GetTokenizedBatch(string prompt, string[] hypotheses)
@@ -105,39 +124,6 @@ public sealed class DebertaV3 : MonoBehaviour
105
  return batch;
106
  }
107
 
108
- float[] ScoresFromLogits(TensorFloat logits)
109
- {
110
- // The logits represent the model's predictions for entailment and non-entailment for each example in the batch.
111
- // They are of shape [batch size, 2], with two values per example.
112
- // To obtain a single value (score) per example, a softmax function is applied
113
-
114
- TensorFloat tensorScores;
115
- if (multipleTrueClasses || logits.shape.Length(0, 1) == 1)
116
- {
117
- // Softmax over the entailment vs. contradiction dimension for each label independently
118
- tensorScores = ops.Softmax(logits, -1);
119
- }
120
- else
121
- {
122
- // Softmax over all candidate labels
123
- tensorScores = ops.Softmax(logits, 0);
124
- }
125
-
126
- tensorScores.MakeReadable();
127
- float[] tensorArray = tensorScores.ToReadOnlyArray();
128
-
129
- tensorScores.Dispose();
130
-
131
- // Select the first column which is the column where the scores are stored
132
- float[] scores = new float[tensorArray.Length / 2];
133
- for (int i = 0; i < scores.Length; i++)
134
- {
135
- scores[i] = tensorArray[i * 2];
136
- }
137
-
138
- return scores;
139
- }
140
-
141
  List<int> Tokenize(string input)
142
  {
143
  string[] words = input.Split(null);
@@ -164,12 +150,7 @@ public sealed class DebertaV3 : MonoBehaviour
164
  return ids;
165
  }
166
 
167
- void OnDestroy()
168
- {
169
- engine?.Dispose();
170
- allocator?.Dispose();
171
- ops?.Dispose();
172
- }
173
 
174
  struct Batch
175
  {
 
13
  public string hypothesisTemplate = "This example is about {}";
14
  public string[] classes = { "politics", "economy", "entertainment", "environment" };
15
 
 
16
  IWorker engine;
 
17
  string[] vocabularyTokens;
18
 
19
  const int padToken = 0;
20
  const int startToken = 1;
21
  const int separatorToken = 2;
22
  const int vocabToTokenOffset = 260;
 
23
 
24
  void Start()
25
  {
 
 
 
 
 
 
 
 
26
  if (classes.Length == 0)
27
  {
28
  Debug.LogError("There need to be more than 0 classes");
29
  return;
30
  }
31
 
32
+ vocabularyTokens = vocabulary.text.Replace("\r", "").Split("\n");
33
+
34
+ Model baseModel = ModelLoader.Load(model);
35
+ Model modelWithScoring = Functional.Compile(
36
+ input =>
37
+ {
38
+ // The logits represent the model's predictions for entailment and non-entailment for each example in the batch.
39
+ // They are of shape [batch size, 2] i.e. with two values per example.
40
+ // To obtain a single score per example, a softmax function is applied
41
+ FunctionalTensor logits = baseModel.Forward(input)[0];
42
+
43
+ if (multipleTrueClasses || classes.Length == 1)
44
+ {
45
+ // Softmax over the entailment vs. contradiction dimension for each label independently
46
+ logits = Functional.Softmax(logits);
47
+ }
48
+ else
49
+ {
50
+ // Softmax over all candidate labels
51
+ logits = Functional.Softmax(logits, 0);
52
+ }
53
+
54
+ // The scores are stored along the first column
55
+ return new []{logits[.., 0]};
56
+ },
57
+ InputDef.FromModel(baseModel)
58
+ );
59
+
60
+ engine = WorkerFactory.CreateWorker(BackendType.GPUCompute, modelWithScoring);
61
+
62
  string[] hypotheses = classes.Select(x => hypothesisTemplate.Replace("{}", x)).ToArray();
63
  Batch batch = GetTokenizedBatch(text, hypotheses);
64
  float[] scores = GetBatchScores(batch);
 
76
 
77
  Dictionary<string, Tensor> inputs = new()
78
  {
79
+ {"input_0", inputIds},
80
+ {"input_1", attentionMask}
81
  };
82
 
83
  engine.Execute(inputs);
84
+ TensorFloat scores = (TensorFloat)engine.PeekOutput("output_0");
85
+ scores.CompleteOperationsAndDownload();
86
 
87
+ return scores.ToReadOnlyArray();
88
  }
89
 
90
  Batch GetTokenizedBatch(string prompt, string[] hypotheses)
 
124
  return batch;
125
  }
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  List<int> Tokenize(string input)
128
  {
129
  string[] words = input.Split(null);
 
150
  return ids;
151
  }
152
 
153
+ void OnDestroy() => engine?.Dispose();
 
 
 
 
 
154
 
155
  struct Batch
156
  {
README.md CHANGED
@@ -14,7 +14,7 @@ There are more models of different sizes that are compatible made by [MoritzLaur
14
 
15
  ## How to Use
16
 
17
- * Ensure Sentis version >= 1.3.0-pre.3
18
  * Create a new scene in Unity 2023
19
  * Add the DebertaV3.cs file to a GameObject in the scene
20
  * Assign model and vocabulary
 
14
 
15
  ## How to Use
16
 
17
+ * Ensure Sentis version is 1.4.0-pre.3
18
  * Create a new scene in Unity 2023
19
  * Add the DebertaV3.cs file to a GameObject in the scene
20
  * Assign model and vocabulary
deberta-v3-xsmall-zeroshot-v1.1-all-33.sentis CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:c847e53df0918823cd4b5dcb73c9cca343a3b7d9d7d6f528cf685e02b5ed9efe
3
- size 302793605
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2d1eec4f37a0c222acb32d6ae67945c1099019cac8bd22cdc9ff7b548adcaf9d
3
+ size 301898184