-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#22 add skeletons for Conv1D, Pooling1D and ZeroPadding1D implementat…
…ions
- Loading branch information
Showing
11 changed files
with
430 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* @author Felix Thielke | ||
*/ | ||
|
||
#include "Conv1D.h" | ||
#include "Platform/BHAssert.h" | ||
|
||
namespace NeuralNetwork | ||
{ | ||
namespace CompiledNNImpl | ||
{ | ||
void Conv1DCompiler::initialize() | ||
{ | ||
// Declare constants | ||
constants.clear(); | ||
|
||
// Store weights | ||
constants.emplace_back(); | ||
NetworkConstants& weights = constants.back(); | ||
weights.data.clear(); | ||
ASSERT(p.weights->rank() == 3); | ||
unsigned int outputBatchSize = 4 * (settings.xmmRegs() - std::max(2u, ActivationFunctionHandler::neededSpares(p.postActivation))); | ||
for(unsigned int outputOffset = 0; outputOffset < p.weights->dims(2); outputOffset += outputBatchSize) | ||
{ | ||
const unsigned int outputBatchEnd = std::min(outputOffset + outputBatchSize, p.weights->dims(2)); | ||
|
||
for(unsigned int input = 0; input < p.weights->dims(0) * p.weights->dims(1); input += 4) | ||
{ | ||
const unsigned int remainingInputs = std::min(4u, p.weights->dims(0) * p.weights->dims(1) - input); | ||
|
||
for(unsigned int shuffle = remainingInputs; shuffle; --shuffle) | ||
{ | ||
for(unsigned int output = outputOffset; output < outputBatchEnd; output += 4) | ||
{ | ||
const unsigned int remainingOutputs = std::min(4u, outputBatchEnd - output); | ||
|
||
for(unsigned int i = 0; i < remainingOutputs; i++) | ||
{ | ||
const float w = (*p.weights)[(input + ((remainingInputs - shuffle + i) % remainingInputs)) * p.weights->dims(2) + output + i]; | ||
if(p.batchNormalization) | ||
weights.data.emplace_back(w * (*p.batchNormalization->factor)[output + i]); | ||
else | ||
weights.data.emplace_back(w); | ||
} | ||
for(unsigned int i = remainingOutputs; i < 4; i++) | ||
weights.data.emplace_back(0.f); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Store biases | ||
if(p.biases || p.batchNormalization) { | ||
constants.emplace_back(); | ||
NetworkConstants& biases = constants.back(); | ||
if(p.biases) | ||
biases.data = *p.biases; | ||
else | ||
biases.data.resize(p.weights->dims(2), 0.f); | ||
if(p.batchNormalization) | ||
{ | ||
for(size_t i = 0; i < biases.data.size(); i++) | ||
biases.data[i] = biases.data[i] * (*p.batchNormalization->factor)[i] + (*p.batchNormalization->offset)[i]; | ||
} | ||
} | ||
} | ||
|
||
void Conv1DCompiler::compile(x86::Assembler& a, ActivationFunctionHandler&, const TensorPointerXf& input, const TensorPointerXf& output) const | ||
{ | ||
ASSERT(input.rank() == 2); | ||
ASSERT(output.rank() == 2); | ||
ASSERT(input.dims(1) == p.weights->dims(1)); | ||
ASSERT(output.dims(1) == p.weights->dims(2)); | ||
|
||
//const NetworkConstants& weights = constants[0]; | ||
|
||
// Load input/output base addresses | ||
a.mov(a.zsi(), imm(input.data())); | ||
a.mov(a.zdi(), a.zsi()); | ||
|
||
FAIL("Not implemented"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* @author Felix Thielke | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "../ActivationFunctions.h" | ||
#include "../CompiledNNImplBase.h" | ||
#include "BatchNormalization.h" | ||
|
||
namespace NeuralNetwork | ||
{ | ||
namespace CompiledNNImpl | ||
{ | ||
struct Conv1DCompiler : public SISOOperationCompiler | ||
{ | ||
struct Parameters final | ||
{ | ||
const BatchNormalizationCompiler::Parameters* batchNormalization = nullptr; | ||
const Tensor<float, 1>* weights; | ||
const std::vector<float>* biases; | ||
unsigned int stride; | ||
ActivationFunctionDescriptor postActivation; | ||
|
||
bool operator==(const Parameters& other) const | ||
{ | ||
return batchNormalization == other.batchNormalization && | ||
weights == other.weights && | ||
biases == other.biases && | ||
stride == other.stride && | ||
postActivation == other.postActivation; | ||
} | ||
}; | ||
const Parameters p; | ||
|
||
Conv1DCompiler(const CompilationSettings& settings, const Parameters& p) : SISOOperationCompiler(settings), p(p) {} | ||
|
||
void initialize() override; | ||
void compile(x86::Assembler& a, ActivationFunctionHandler& afHandler, const TensorPointerXf& input, const TensorPointerXf& output) const override; | ||
|
||
inline bool canBeInplace() const override | ||
{ | ||
return false; | ||
} | ||
|
||
inline std::vector<unsigned int> calcOutputDimensions(const std::vector<unsigned int>& inputDimensions) const override | ||
{ | ||
ASSERT(inputDimensions.size() == 2); | ||
return {{(inputDimensions[0] - p.weights->dims(0) + p.stride) / p.stride, p.weights->dims(2)}}; | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.