mirror of
https://github.com/StepanovPlaton/NeuralNetwork.git
synced 2026-04-04 04:40:40 +04:00
Forward with new tensors math
This commit is contained in:
@@ -93,12 +93,11 @@ class Tensor0Math : public TensorMath<Tensor0>, public ITensor0Math<Tensor0> {};
|
||||
|
||||
class Tensor1Math : public TensorMath<Tensor1>, public ITensor1Math<Tensor1> {};
|
||||
|
||||
class Tensor2Math : public TensorMath<Tensor2>, public ITensor2Math<Tensor2> {
|
||||
class Tensor2Math : public TensorMath<Tensor2>,
|
||||
public ITensor2Math<Tensor2, Tensor1> {
|
||||
private:
|
||||
Tensor2 mult_tiled(const Tensor2 &a, const Tensor2 &b, bool transpose = false,
|
||||
float bias = 0.0f, Activation type = Activation::LINEAR,
|
||||
float alpha = 0.01f) {
|
||||
validateMultDimensions(a, b, transpose);
|
||||
Tensor2 mult_tiled(const Tensor2 &a, const Tensor2 &b, bool transpose,
|
||||
const Vector &bias, Activation type, float alpha) {
|
||||
Tensor2 result(a.getRows(), transpose ? b.getRows() : b.getCols(), false,
|
||||
&queue);
|
||||
|
||||
@@ -111,7 +110,7 @@ private:
|
||||
kernels[Method::MULT].setArg(0, *a.getBuffer());
|
||||
kernels[Method::MULT].setArg(1, *b.getBuffer());
|
||||
kernels[Method::MULT].setArg(2, *result.getBuffer());
|
||||
kernels[Method::MULT].setArg(3, bias);
|
||||
kernels[Method::MULT].setArg(3, *bias.getBuffer());
|
||||
kernels[Method::MULT].setArg(4, static_cast<int>(type));
|
||||
kernels[Method::MULT].setArg(5, alpha);
|
||||
kernels[Method::MULT].setArg(6, result.getRows());
|
||||
@@ -122,16 +121,14 @@ private:
|
||||
global_size, local_size);
|
||||
return result;
|
||||
}
|
||||
Tensor2 mult_small(const Tensor2 &a, const Tensor2 &b, bool transpose = false,
|
||||
float bias = 0.0f, Activation type = Activation::LINEAR,
|
||||
float alpha = 0.01f) {
|
||||
validateMultDimensions(a, b, transpose);
|
||||
Tensor2 mult_small(const Tensor2 &a, const Tensor2 &b, bool transpose,
|
||||
const Vector &bias, Activation type, float alpha) {
|
||||
Tensor2 result(a.getRows(), transpose ? b.getRows() : b.getCols(), false,
|
||||
&queue);
|
||||
kernels[Method::MULT_SMALL].setArg(0, *a.getBuffer());
|
||||
kernels[Method::MULT_SMALL].setArg(1, *b.getBuffer());
|
||||
kernels[Method::MULT_SMALL].setArg(2, *result.getBuffer());
|
||||
kernels[Method::MULT_SMALL].setArg(3, bias);
|
||||
kernels[Method::MULT_SMALL].setArg(3, *bias.getBuffer());
|
||||
kernels[Method::MULT_SMALL].setArg(4, static_cast<int>(type));
|
||||
kernels[Method::MULT_SMALL].setArg(5, alpha);
|
||||
kernels[Method::MULT_SMALL].setArg(6, result.getRows());
|
||||
@@ -145,13 +142,20 @@ private:
|
||||
|
||||
public:
|
||||
Tensor2 mult(const Tensor2 &a, const Tensor2 &b, bool transpose = false,
|
||||
float bias = 0.0f, Activation type = Activation::LINEAR,
|
||||
const Vector *bias = nullptr,
|
||||
Activation type = Activation::LINEAR,
|
||||
float alpha = 0.01f) override {
|
||||
validateMultDimensions(a, b, transpose);
|
||||
const Vector defaultBias(a.getRows(), 0.0f, &queue);
|
||||
if (bias != nullptr)
|
||||
validateBiasDimensions(b, *bias, transpose);
|
||||
if (a.getRows() > 64 || a.getCols() > 64 || b.getRows() > 64 ||
|
||||
b.getCols() > 64)
|
||||
return mult_tiled(a, b, transpose, bias, type, alpha);
|
||||
return mult_tiled(a, b, transpose, bias == nullptr ? defaultBias : *bias,
|
||||
type, alpha);
|
||||
else
|
||||
return mult_small(a, b, transpose, bias, type, alpha);
|
||||
return mult_small(a, b, transpose, bias == nullptr ? defaultBias : *bias,
|
||||
type, alpha);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -69,12 +69,41 @@ public:
|
||||
if (fill)
|
||||
createBuf(getShapeSize(shape), 0.0f, queue);
|
||||
}
|
||||
Tensor(const Tensor &) = delete;
|
||||
Tensor &operator=(const Tensor &) = delete;
|
||||
Tensor(Tensor &&other) : ITensor(other.shape), buffer(other.buffer) {
|
||||
|
||||
Tensor(const Tensor &other, const cl::CommandQueue *queue = nullptr)
|
||||
: ITensor(other) {
|
||||
cl::CommandQueue q = queue == nullptr ? openCL.getDefaultQueue() : *queue;
|
||||
createBuf(other.getSize(), &q);
|
||||
q.enqueueCopyBuffer(*other.buffer, *buffer, 0, 0,
|
||||
other.getSize() * sizeof(float));
|
||||
};
|
||||
Tensor &operator=(const Tensor &other) {
|
||||
if (buffer != nullptr)
|
||||
delete buffer;
|
||||
ITensor::operator=(other);
|
||||
createBuf(other.getSize(), &openCL.getDefaultQueue());
|
||||
openCL.getDefaultQueue().enqueueCopyBuffer(*other.buffer, *buffer, 0, 0,
|
||||
other.getSize() * sizeof(float));
|
||||
return *this;
|
||||
};
|
||||
Tensor(Tensor &&other) : ITensor(other), buffer(other.buffer) {
|
||||
other.buffer = nullptr;
|
||||
};
|
||||
Tensor &operator=(Tensor &&other) = delete;
|
||||
Tensor &operator=(Tensor &&other) {
|
||||
if (this != &other) {
|
||||
if (buffer != nullptr)
|
||||
delete buffer;
|
||||
ITensor::operator=(std::move(other));
|
||||
buffer = other.buffer;
|
||||
other.buffer = nullptr;
|
||||
}
|
||||
return *this;
|
||||
};
|
||||
|
||||
~Tensor() {
|
||||
if (buffer != nullptr)
|
||||
delete buffer;
|
||||
}
|
||||
|
||||
std::vector<float> toVector(const cl::CommandQueue *queue = nullptr) {
|
||||
size_t size = getShapeSize(shape);
|
||||
@@ -144,17 +173,25 @@ public:
|
||||
if (shape.size() != 0)
|
||||
throw std::invalid_argument("Tensor0 dimension must be 0");
|
||||
}
|
||||
Tensor0(const cl::CommandQueue *queue = nullptr) : Tensor({}, queue) {
|
||||
Tensor0(const cl::CommandQueue *queue = nullptr)
|
||||
: Tensor(std::vector<int>{}, queue) {
|
||||
createBuf(1, queue);
|
||||
}
|
||||
Tensor0(float value, const cl::CommandQueue *queue = nullptr)
|
||||
: Tensor({}, queue) {
|
||||
: Tensor(std::vector<int>{}, queue) {
|
||||
createBuf(1, value, queue);
|
||||
}
|
||||
Tensor0(const Tensor0 &) = delete;
|
||||
Tensor0 &operator=(const Tensor0 &) = delete;
|
||||
Tensor0(const Tensor0 &other, const cl::CommandQueue *queue = nullptr)
|
||||
: Tensor(other, queue) {};
|
||||
Tensor0 &operator=(const Tensor0 &other) {
|
||||
Tensor::operator=(other);
|
||||
return *this;
|
||||
};
|
||||
Tensor0(Tensor0 &&other) : Tensor(std::move(other)) {};
|
||||
Tensor0 &operator=(Tensor0 &&other) = delete;
|
||||
Tensor0 &operator=(Tensor0 &&other) {
|
||||
Tensor::operator=(std::move(other));
|
||||
return *this;
|
||||
};
|
||||
};
|
||||
|
||||
class Tensor1 : public Tensor, public ITensor1 {
|
||||
@@ -180,10 +217,17 @@ public:
|
||||
: Tensor({(int)values.size()}, false, queue) {
|
||||
fillBuf(values, queue);
|
||||
}
|
||||
Tensor1(const Tensor1 &) = delete;
|
||||
Tensor1 &operator=(const Tensor1 &) = delete;
|
||||
Tensor1(Tensor1 &&other) : Tensor(std::move(other)) {}
|
||||
Tensor1 &operator=(Tensor1 &&other) = delete;
|
||||
Tensor1(const Tensor1 &other, const cl::CommandQueue *queue = nullptr)
|
||||
: Tensor(other, queue) {};
|
||||
Tensor1 &operator=(const Tensor1 &other) {
|
||||
Tensor::operator=(other);
|
||||
return *this;
|
||||
};
|
||||
Tensor1(Tensor1 &&other) : Tensor(std::move(other)) {};
|
||||
Tensor1 &operator=(Tensor1 &&other) {
|
||||
Tensor::operator=(std::move(other));
|
||||
return *this;
|
||||
};
|
||||
|
||||
int getSize() const override { return shape[0]; }
|
||||
};
|
||||
@@ -223,10 +267,17 @@ public:
|
||||
fillBuf(v, queue);
|
||||
}
|
||||
|
||||
Tensor2(const Tensor2 &) = delete;
|
||||
Tensor2 &operator=(const Tensor2 &) = delete;
|
||||
Tensor2(Tensor2 &&other) : Tensor(std::move(other)) {}
|
||||
Tensor2 &operator=(Tensor2 &&other) = delete;
|
||||
Tensor2(const Tensor2 &other, const cl::CommandQueue *queue = nullptr)
|
||||
: Tensor(other, queue) {};
|
||||
Tensor2 &operator=(const Tensor2 &other) {
|
||||
Tensor::operator=(other);
|
||||
return *this;
|
||||
};
|
||||
Tensor2(Tensor2 &&other) : Tensor(std::move(other)) {};
|
||||
Tensor2 &operator=(Tensor2 &&other) {
|
||||
Tensor::operator=(std::move(other));
|
||||
return *this;
|
||||
};
|
||||
|
||||
int getRows() const override { return shape[0]; }
|
||||
int getCols() const override { return shape[1]; }
|
||||
@@ -269,10 +320,17 @@ public:
|
||||
}
|
||||
fillBuf(v, queue);
|
||||
}
|
||||
Tensor3(const Tensor3 &) = delete;
|
||||
Tensor3 &operator=(const Tensor3 &) = delete;
|
||||
Tensor3(Tensor3 &&other) : Tensor(std::move(other)) {}
|
||||
Tensor3 &operator=(Tensor3 &&other) = delete;
|
||||
Tensor3(const Tensor3 &other, const cl::CommandQueue *queue = nullptr)
|
||||
: Tensor(other, queue) {};
|
||||
Tensor3 &operator=(const Tensor3 &other) {
|
||||
Tensor::operator=(other);
|
||||
return *this;
|
||||
};
|
||||
Tensor3(Tensor3 &&other) : Tensor(std::move(other)) {};
|
||||
Tensor3 &operator=(Tensor3 &&other) {
|
||||
Tensor::operator=(std::move(other));
|
||||
return *this;
|
||||
};
|
||||
};
|
||||
|
||||
typedef Tensor0 Scalar;
|
||||
|
||||
Reference in New Issue
Block a user