Skip to content

Defaults #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ set(GLOW_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(GLOW_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${GLOW_BINARY_DIR}/bin)

set(TRAINING_TENSOR_ELEMENT_TYPE float)

include_directories(BEFORE
${CMAKE_CURRENT_BINARY_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/include
Expand All @@ -36,11 +34,6 @@ add_custom_target(CollectHeaders SOURCES ${header_files})

find_package(PNG)

configure_file (
"${GLOW_SOURCE_DIR}/include/Config.h.in"
"${GLOW_BINARY_DIR}/Config.h"
)

add_subdirectory(external)
add_subdirectory(src/glow/)
add_subdirectory(examples/)
Expand Down
8 changes: 4 additions & 4 deletions examples/cifar10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void testCIFAR10() {
size_t idx = 0;

auto labelsH = labels.getHandle<size_t>();
auto imagesH = images.getHandle<FloatTy>();
auto imagesH = images.getHandle<>();
for (unsigned w = 0; w < cifarNumImages; w++) {
labelsH.at({w, 0}) = static_cast<uint8_t>(dbInput.get());
idx++;
Expand All @@ -48,7 +48,7 @@ void testCIFAR10() {
for (unsigned y = 0; y < 32; y++) {
for (unsigned x = 0; x < 32; x++) {
imagesH.at({w, x, y, z}) =
FloatTy(static_cast<uint8_t>(dbInput.get())) / 255.0;
static_cast<float>(static_cast<uint8_t>(dbInput.get())) / 255.0;
idx++;
}
}
Expand Down Expand Up @@ -117,8 +117,8 @@ void testCIFAR10() {
Tensor &res = result->getOutput()->getPayload();

for (unsigned int iter = 0; iter < minibatchSize; iter++) {
auto T = res.getHandle<FloatTy>().extractSlice(iter);
size_t guess = T.getHandle<FloatTy>().maxArg();
auto T = res.getHandle<>().extractSlice(iter);
size_t guess = T.getHandle<>().maxArg();
size_t correct = labelsH.at({minibatchSize * i + iter, 0});
score += guess == correct;

Expand Down
14 changes: 7 additions & 7 deletions examples/mnist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ unsigned loadMNIST(Tensor &imageInputs, Tensor &labelInputs) {
size_t idx = 0;

auto LIH = labelInputs.getHandle<size_t>();
auto IIH = imageInputs.getHandle<FloatTy>();
auto IIH = imageInputs.getHandle<>();

for (unsigned w = 0; w < mnistNumImages; w++) {
LIH.at({w, 0}) = labels[w];
Expand Down Expand Up @@ -124,19 +124,19 @@ void testMNIST() {
Tensor &res = result->getOutput()->getPayload();

for (unsigned int iter = 0; iter < minibatchSize; iter++) {
auto T = res.getHandle<FloatTy>().extractSlice(iter);
size_t guess = T.getHandle<FloatTy>().maxArg();
auto T = res.getHandle<>().extractSlice(iter);
size_t guess = T.getHandle<>().maxArg();

size_t correct = LIH.at(iter);
rightAnswer += (guess == correct);

auto I = sample.getHandle<FloatTy>().extractSlice(iter);
auto J = I.getHandle<FloatTy>().extractSlice(0);
auto I = sample.getHandle<>().extractSlice(iter);
auto J = I.getHandle<>().extractSlice(0);

J.getHandle<FloatTy>().dumpAscii("MNIST Input");
J.getHandle<>().dumpAscii("MNIST Input");
std::cout << "Expected: " << correct << " Guessed: " << guess << "\n";

T.getHandle<FloatTy>().dump("", "\n");
T.getHandle<>().dump("", "\n");
std::cout << "\n-------------\n";
}

Expand Down
2 changes: 0 additions & 2 deletions include/Config.h.in

This file was deleted.

9 changes: 2 additions & 7 deletions include/glow/Base/Tensor.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef GLOW_BASE_TENSOR_H
#define GLOW_BASE_TENSOR_H

#include "Config.h"

#include "glow/Base/Type.h"
#include "glow/Support/Compiler.h"
#include "glow/Support/Random.h"
Expand All @@ -20,9 +18,6 @@

namespace glow {

/// This is the default floating point type used for training.
using FloatTy = TRAINING_TENSOR_ELEMENT_TYPE;

template <class ElemTy> static char valueToChar(ElemTy val) {
char ch = ' ';
if (val > 0.2) {
Expand Down Expand Up @@ -114,7 +109,7 @@ class Tensor final {
/// Initialize from a list of float literals.
Tensor(const std::initializer_list<double> &vec) {
reset(ElemKind::FloatTy, {vec.size()});
auto *data = getRawDataPointer<FloatTy>();
auto *data = getRawDataPointer<float>();
int i = 0;
for (auto &f : vec) {
data[i++] = f;
Expand Down Expand Up @@ -233,7 +228,7 @@ class Tensor final {
}

/// \return a new handle that points and manages this tensor.
template <class ElemTy> Handle<ElemTy> getHandle();
template <class ElemTy = float> Handle<ElemTy> getHandle();
};

/// A class that provides indexed access to a tensor. This class has value
Expand Down
4 changes: 2 additions & 2 deletions include/glow/ExecutionEngine/ExecutionEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ class ExecutionEngine final {
llvm::ArrayRef<Tensor *> inputs);

/// \returns a float-handle to the tensor that is stored at \p v.
Handle<FloatTy> getWeightHandle(Variable *v) const;
Handle<float> getWeightHandle(Variable *v) const;

/// \returns a float-handle to the tensor that is stored at \p v.
Handle<FloatTy> getGradHandle(Variable *v);
Handle<float> getGradHandle(Variable *v);

private:
/// Update the inputs for all variables \p vars with data from the inputs \p
Expand Down
2 changes: 1 addition & 1 deletion include/glow/Graph/Nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Variable final : public Node {

Tensor &getPayload() { return payload_; }

template <class ElemTy> Handle<ElemTy> getHandle() {
template <class ElemTy = float> Handle<ElemTy> getHandle() {
return getPayload().getHandle<ElemTy>();
}

Expand Down
4 changes: 2 additions & 2 deletions include/glow/Interpreter/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ class Interpreter final {
Tensor *getOrCreateGradTensor(const Value *v);

/// \returns a float-handle to the tensor that is stored at \p v.
Handle<FloatTy> getWeightHandle(Value *v) const;
Handle<float> getWeightHandle(Value *v) const;

/// \returns a float-handle to the tensor that is stored at \p v.
Handle<FloatTy> getGradHandle(Value *v);
Handle<float> getGradHandle(Value *v);

/// Perform a single forward scan of the network, interpreting all of the
/// instructions.
Expand Down
4 changes: 2 additions & 2 deletions src/glow/Base/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bool glow::readPngImage(Tensor *T, const char *filename,
fclose(fp);

T->reset(ElemKind::FloatTy, {width, height, 3});
auto H = T->getHandle<FloatTy>();
auto H = T->getHandle<>();

float scale = ((range.second - range.first) / 255.0);
float bias = range.first;
Expand Down Expand Up @@ -131,7 +131,7 @@ bool glow::writePngImage(Tensor *T, const char *filename,
return true;
}

auto H = T->getHandle<FloatTy>();
auto H = T->getHandle<>();

auto odim = H.dims();
assert(odim[2] < 4 && "Invalid buffer to save");
Expand Down
18 changes: 9 additions & 9 deletions src/glow/Base/Train.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,38 @@ void Trainer::train(Tensor *weights, Tensor *gradients, size_t batchSize) {
float momentum = config.momentum;

auto sz = weights->size();
auto W = weights->getHandle<FloatTy>();
auto G = gradients->getHandle<FloatTy>();
auto Gsum = Handle<FloatTy>::createInvalidHandle();
auto W = weights->getHandle<>();
auto G = gradients->getHandle<>();
auto Gsum = Handle<float>::createInvalidHandle();

/// If we are using the momentum technique then we need to allocate an array
/// for the gradient sum.
if (momentum > 0.0) {
auto it = gsum_.find(gradients);

if (it != gsum_.end()) {
Gsum = it->second->getHandle<FloatTy>();
Gsum = it->second->getHandle<>();
} else {
auto *gs = new Tensor();
gs->reset(gradients);
gsum_[gradients] = gs;
Gsum = gs->getHandle<FloatTy>();
Gsum = gs->getHandle<>();
}
}

// For each weight/gradient pair:
for (size_t x = 0; x < sz; x++) {
// Do a simple SGD update:
FloatTy L1Grad = L1Decay * (W.raw(x) > 0 ? 1 : -1);
FloatTy L2Grad = L2Decay * (W.raw(x));
FloatTy gij = (L2Grad + L1Grad + G.raw(x)) / batchSize;
float L1Grad = L1Decay * (W.raw(x) > 0 ? 1 : -1);
float L2Grad = L2Decay * (W.raw(x));
float gij = (L2Grad + L1Grad + G.raw(x)) / batchSize;

// Use the momentum to improve the gradient descent:
// http://ufldl.stanford.edu/tutorial/supervised/
// OptimizationStochasticGradientDescent/
if (momentum > 0.0) {
// Momentum update:
FloatTy dx = momentum * Gsum.raw(x) - learningRate * gij;
float dx = momentum * Gsum.raw(x) - learningRate * gij;
// Save this value for the next iteration:
Gsum.raw(x) = dx;
// Apply the gradient.
Expand Down
5 changes: 3 additions & 2 deletions src/glow/ExecutionEngine/ExecutionEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,14 @@ void ExecutionEngine::optimize(OptimizationMode mode) {
}

/// \returns a float-handle to the tensor that is stored at \p v.
Handle<FloatTy> ExecutionEngine::getWeightHandle(Variable *v) const {
Handle<float>
ExecutionEngine::getWeightHandle(Variable *v) const {
auto val = M_->getWeightForNode(v);
return IP_->getWeightHandle(val);
}

/// \returns a float-handle to the tensor that is stored at \p v.
Handle<FloatTy> ExecutionEngine::getGradHandle(Variable *v) {
Handle<float> ExecutionEngine::getGradHandle(Variable *v) {
auto val = M_->getWeightForNode(v);
return IP_->getGradHandle(val);
}
6 changes: 3 additions & 3 deletions src/glow/Importer/Caffe2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void caffe2ModelLoader::loadOperator(const caffe2::OperatorDef &op) {
// TODO: need to test this code with a large batch size to verify that the
// conversion is correct.
Tensor wtag;
w->getHandle<FloatTy>().transpose(&wtag, {0, 2, 3, 1});
w->getHandle<>().transpose(&wtag, {0, 2, 3, 1});

// The structure of the conv weigts is: NHWC. We take the C, which is the
// number of filters. We use this value to calculate the size of the bias
Expand Down Expand Up @@ -385,7 +385,7 @@ void caffe2ModelLoader::loadWeights(caffe2::NetDef &net) {

auto dim = getShape(dict["shape"]);
T->reset(ElemKind::FloatTy, dim);
auto TH = T->getHandle<FloatTy>();
auto TH = T->getHandle<>();
size_t i = 0;
for (auto f : dict["values"]->floats()) {
TH.raw(i++) = f;
Expand Down Expand Up @@ -420,7 +420,7 @@ void caffe2ModelLoader::loadWeights(caffe2::NetDef &net) {

auto dim = getShape(dict["shape"]);
T->reset(ElemKind::FloatTy, dim);
auto TH = T->getHandle<FloatTy>();
auto TH = T->getHandle<>();
TH.clear();
continue;
}
Expand Down
8 changes: 4 additions & 4 deletions src/glow/Interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ Tensor *Interpreter::getOrCreateGradTensor(const Value *v) {
return N;
}

Handle<FloatTy> Interpreter::getWeightHandle(Value *v) const {
return getTensor(v)->getHandle<FloatTy>();
Handle<float> Interpreter::getWeightHandle(Value *v) const {
return getTensor(v)->getHandle<>();
}

Handle<FloatTy> Interpreter::getGradHandle(Value *v) {
return getOrCreateGradTensor(v)->getHandle<FloatTy>();
Handle<float> Interpreter::getGradHandle(Value *v) {
return getOrCreateGradTensor(v)->getHandle<>();
}

Tensor *Interpreter::getOrCreateTensor(const Value *v) {
Expand Down
Loading