-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuantMatrix.cs
More file actions
165 lines (136 loc) · 4.22 KB
/
QuantMatrix.cs
File metadata and controls
165 lines (136 loc) · 4.22 KB
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
using System;
using System.Diagnostics;
using System.IO;
namespace FastText
{
public class QuantMatrix : Matrix
{
protected ProductQuantizer pq_;
protected ProductQuantizer npq_;
protected byte[] codes_;
protected byte[] norm_codes_;
protected bool qnorm_;
protected int codesize_;
public QuantMatrix() : base()
{
qnorm_ = false;
codesize_ = 0;
}
public QuantMatrix(DenseMatrix mat, int dsub, bool qnorm)
: base(mat.Size(0), mat.Size(1))
{
qnorm_ = qnorm;
codesize_ = (int)(mat.Size(0) * ((mat.Size(1) + dsub - 1) / dsub));
codes_ = new byte[codesize_];
pq_ = new ProductQuantizer((int)n_, dsub);
if (qnorm_)
{
norm_codes_ = new byte[m_];
npq_ = new ProductQuantizer(1, 1);
}
Quantize(mat);
}
public void QuantizeNorm(float[] norms)
{
Debug.Assert(qnorm_);
Debug.Assert(norms.Length == m_);
npq_.Train((int)m_, norms);
npq_.ComputeCodes(norms, norm_codes_, (int)m_);
}
public void Quantize(DenseMatrix mat)
{
if (qnorm_)
{
var norms = new float[mat.Size(0)];
mat.L2NormRow(norms);
mat.DivideRow(norms);
QuantizeNorm(norms);
}
var data = mat.data;
pq_.Train((int)m_, data);
pq_.ComputeCodes(data, codes_, (int)m_);
}
public override float DotRow(float[] vec, long i)
{
Debug.Assert(i >= 0);
Debug.Assert(i < m_);
Debug.Assert(vec.Length == n_);
float norm = 1;
if (qnorm_)
{
norm = npq_.GetCentroids(0, norm_codes_[i])[0];
}
return pq_.MulCode(vec, codes_, (int)i, norm);
}
public override void AddVectorToRow(float[] vec, long i, float a)
{
throw new InvalidOperationException("Operation not permitted on quantized matrices.");
}
public override void AddRowToVector(float[] x, int i, float a)
{
var norm = 1f;
if (qnorm_)
{
norm = npq_.GetCentroids(0, norm_codes_[i])[0];
}
pq_.AddCode(x, codes_, i, a * norm);
}
public override void AddRowToVector(float[] x, int i)
{
var norm = 1f;
if (qnorm_)
{
norm = npq_.GetCentroids(0, norm_codes_[i])[0];
}
pq_.AddCode(x, codes_, i, norm);
}
public override void Save(BinaryWriter writer)
{
writer.Write(qnorm_);
writer.Write(m_);
writer.Write(n_);
writer.Write(codesize_);
for (int i = 0; i < codesize_; i++)
{
writer.Write(codes_[i]);
}
pq_.Save(writer);
if (qnorm_)
{
for (int i = 0; i < m_; i++)
{
writer.Write(norm_codes_[i]);
}
npq_.Save(writer);
}
}
public override void Load(BinaryReader reader)
{
qnorm_ = reader.ReadBoolean();
m_ = reader.ReadInt64();
n_ = reader.ReadInt64();
codesize_ = reader.ReadInt32();
codes_ = new byte[codesize_];
for(int i = 0; i < codesize_; i++)
{
codes_[i] = reader.ReadByte();
}
pq_ = new ProductQuantizer();
pq_.Load(reader);
if (qnorm_)
{
norm_codes_ = new byte[m_];
for (int i = 0; i < m_; i++)
{
norm_codes_[i] = reader.ReadByte();
}
npq_ = new ProductQuantizer();
npq_.Load(reader);
}
}
public override void Dump(TextWriter writer)
{
throw new InvalidOperationException("Operation not permitted on quantized matrices.");
}
}
}