Skip to content

Commit b5915db

Browse files
authored
Merge pull request #124 from marty1885/apichange
Mostly fixing bugs
2 parents 9be40a5 + 482d1ce commit b5915db

File tree

7 files changed

+35
-25
lines changed

7 files changed

+35
-25
lines changed

Etaler/Algorithms/SpatialPooler.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct ETALER_EXPORT SpatialPooler
4242
size_t globalDensity() const { return global_density_; }
4343

4444
void setBoostingFactor(float f) { boost_factor_ = f; }
45-
float boostFactor() { return boost_factor_; }
45+
float boostFactor() const { return boost_factor_; }
4646

4747
Tensor connections() const {return connections_;}
4848
Tensor permanences() const {return permanences_;}

Etaler/Core/Tensor.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ Tensor Tensor::view(svector<Range> ranges) const
178178

179179
auto resolve_index = [](intmax_t idx, intmax_t size) -> intmax_t {
180180
if(idx < 0)
181-
return size-idx;
181+
return size+idx;
182182
return idx;
183183
};
184184

@@ -202,22 +202,22 @@ Tensor Tensor::view(svector<Range> ranges) const
202202

203203
intmax_t start = r.start().value_or(0);
204204
intmax_t stop = r.stop().value_or(dim_size);
205-
intmax_t step = r.step().value_or(1);
205+
206+
intmax_t real_start = resolve_index(start, dim_size);
207+
intmax_t real_stop = resolve_index(stop, dim_size);
208+
intmax_t step = r.step().value_or(real_stop>real_start?1:-1);
209+
intmax_t size = (std::abs(real_stop - real_start) - 1) / std::abs(step) + 1;
206210

207211
// Indexing validations
212+
if(is_index_valid(stop, dim_size+1) == false)
213+
throw EtError("Stopping index " + std::to_string(stop) + " is out of range in dimension " + std::to_string(i));
208214
if(step == 0)
209215
throw EtError("Error: Step size is zero in dimension " + std::to_string(i));
210216
if(is_index_valid(start, dim_size) == false)
211217
throw EtError("Starting index " + std::to_string(start) + " is out of range in dimension " + std::to_string(i));
212-
if(is_index_valid(stop, dim_size+1) == false)
213-
throw EtError("Stopping index " + std::to_string(stop) + " is out of range in dimension " + std::to_string(i));
214-
215-
intmax_t real_start = resolve_index(start, dim_size);
216-
intmax_t real_stop = resolve_index(stop, dim_size);
217-
intmax_t size = (real_stop - real_start - 1) / step + 1;
218-
219218
if((real_stop - real_start) * step < 0)
220219
throw EtError("Step is going in the wrong direction. Will cause infinate loop");
220+
221221
viewed_strides[i] *= step;
222222

223223
offset.push_back(real_start);

Etaler/Core/Tensor.hpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ struct ETALER_EXPORT TensorIterator
3131

3232
using ThisIterator = TensorIterator<T>;
3333
TensorIterator() = default;
34-
TensorIterator(reference t, intmax_t offset = 0) : t_(&t), offset_(offset)
34+
TensorIterator(reference t, intmax_t offset = 0) : t_(t), offset_(offset)
3535
{static_assert(std::is_same_v<raw_value_type, Tensor>); }
36-
value_type operator*() { return t_->view({offset_}); }
36+
value_type operator*() { return t_.view({offset_}); }
3737
// Unfortunatelly returning a pointer is not doable
38-
pointer operator->() { return std::make_unique<raw_value_type>(*(*this)); }
39-
bool operator==(ThisIterator rhs) const { return offset_ == rhs.offset_ && t_ == rhs.t_; }
38+
pointer operator->() { return std::make_unique<raw_value_type>(this->operator*()); }
39+
bool operator==(ThisIterator rhs) const { return offset_ == rhs.offset_ && t_.pimpl() == rhs.t_.pimpl(); }
4040
bool operator!=(ThisIterator rhs) const { return !(*this == rhs); }
4141
ThisIterator& operator++() {offset_ += 1; return *this;}
4242
ThisIterator operator++(int) {ThisIterator retval = *this; ++(*this); return retval;}
4343
ThisIterator& operator--() {offset_ -= 1; return *this;}
4444
ThisIterator operator--(int) {ThisIterator retval = *this; --(*this); return retval;}
45-
value_type* t_ = nullptr; // Using a pointer because Tensor is a incomplete type here
45+
value_type t_;
4646
intmax_t offset_ = 0;
4747
};
4848

@@ -300,7 +300,12 @@ inline Tensor ravel(const Tensor& t)
300300
inline Tensor cellActivity(const Tensor& x, const Tensor& connections, const Tensor& permeances
301301
, float connected_permeance, size_t active_threshold, bool has_unconnected_synapse=true)
302302
{
303-
return x.backend()->cellActivity(x(), connections(), permeances(), connected_permeance, active_threshold, has_unconnected_synapse);
303+
const Tensor& input = [&](){
304+
if(x.dtype() == DType::Bool)
305+
return x;
306+
return x.cast(DType::Bool);
307+
}();
308+
return x.backend()->cellActivity(input(), connections(), permeances(), connected_permeance, active_threshold, has_unconnected_synapse);
304309
}
305310

306311
inline void learnCorrilation(const Tensor& x, const Tensor& learn, const Tensor& connection

Etaler/Core/Views.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ struct Range
1515
{
1616
Range() = default;
1717
Range(intmax_t start)
18-
: start_(start), stop_(start+1)
18+
: start_(start), stop_(start+(start>=0?1:-1))
1919
{}
2020

21-
Range(intmax_t start, intmax_t stop)
22-
: start_(start), stop_(stop)
23-
{}
24-
25-
Range(intmax_t start, intmax_t stop, intmax_t step)
21+
Range(std::optional<intmax_t> start, intmax_t stop
22+
, std::optional<intmax_t> step = std::nullopt)
2623
: start_(start), stop_(stop), step_(step)
2724
{}
2825

@@ -48,7 +45,7 @@ inline Range range(intmax_t start, intmax_t end)
4845

4946
inline Range range(intmax_t end)
5047
{
51-
return Range(0, end);
48+
return Range(std::nullopt, end);
5249
}
5350

5451
inline Range range(intmax_t start, intmax_t stop, intmax_t step)

Etaler/Encoders/Category.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ std::vector<size_t> category(const Tensor& t, size_t num_categories)
3434
et_assert(t.size()%num_categories == 0);
3535
et_assert(t.dtype() == DType::Bool);
3636

37-
std::vector<uint8_t> vec(t.size());
38-
t.backend()->copyToHost(t.pimpl(), vec.data());
37+
std::vector<uint8_t> vec = t.toHost<uint8_t>();
3938

4039
std::set<size_t> categories;
4140
size_t bits_per_category = vec.size()/num_categories;

docs/source/Contribution.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ If you want to develop the library it self, you are very welcomed! We are excite
2727
* Don't care about swarmming
2828
* follow the KISS (Keep it Simple Stupid) principle
2929
* Configuration files are evil (Looking at you NuPIC)
30+
* Avoid raw pointers. They are evil
3031

3132
If you are adding a dependency to the library. Please discuss it in a relevant thread or make a new issue. For now, we are actively avoiding [Boost](https://www.boost.org/) and try to stick to STL as much as possible. Using Boost might lead to trouble later on. But we might decide use Boost later on.

tests/common_tests.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@ TEST_CASE("Testing Tensor", "[Tensor]")
212212
CHECK(realize(r).isSame(pred));
213213
}
214214

215+
SECTION("Indexing with negative values") {
216+
Tensor q = t.view({3});
217+
Tensor r;
218+
CHECK_NOTHROW(r = t.view({-1}));
219+
CHECK(r.has_value() == true);
220+
CHECK(q.isSame(r));
221+
}
222+
215223
SECTION("View of views") {
216224
Tensor t = ones({4, 4});
217225
Tensor v1 = t[{3}];

0 commit comments

Comments
 (0)